Sort macho symbol table entries

macho symbol table entries are not always in order, which breaks
finding the next symbol to find the size of the target symbol.

Test: build_version_test
Change-Id: I41d1c3c3ff9929694e9ec2b034553d6b7ddef937
This commit is contained in:
Colin Cross 2018-02-23 22:43:24 -08:00
parent 64c6d4bf14
commit c4a18e0291

View file

@ -18,6 +18,7 @@ import (
"debug/macho"
"fmt"
"io"
"sort"
)
func findMachoSymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) {
@ -29,14 +30,22 @@ func findMachoSymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) {
// symbols in macho files seem to be prefixed with an underscore
symbolName = "_" + symbolName
for i, symbol := range machoFile.Symtab.Syms {
symbols := machoFile.Symtab.Syms
sort.Slice(symbols, func(i, j int) bool {
if symbols[i].Sect != symbols[j].Sect {
return symbols[i].Sect < symbols[j].Sect
}
return symbols[i].Value < symbols[j].Value
})
for i, symbol := range symbols {
if symbol.Sect == 0 {
continue
}
if symbol.Name == symbolName {
var nextSymbol *macho.Symbol
if i+1 < len(machoFile.Symtab.Syms) {
nextSymbol = &machoFile.Symtab.Syms[i+1]
if i+1 < len(symbols) {
nextSymbol = &symbols[i+1]
}
return calculateMachoSymbolOffset(machoFile, symbol, nextSymbol)
}