diff options
author | Colin Cross <ccross@android.com> | 2018-03-01 05:43:13 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-03-01 05:43:13 +0000 |
commit | fd316aa69c82cb5af4b51738c15da21447d622bf (patch) | |
tree | b8ce2c6b64e1fafdec892b5fc2a122cc688650a7 | |
parent | b1e6641a77c1d98156134ea7f7dc7592f63e712b (diff) | |
parent | 8d43b7cf8169721d85330bea5de28db66667426e (diff) | |
download | build_soong-fd316aa69c82cb5af4b51738c15da21447d622bf.tar.gz build_soong-fd316aa69c82cb5af4b51738c15da21447d622bf.tar.bz2 build_soong-fd316aa69c82cb5af4b51738c15da21447d622bf.zip |
Merge "Fix finding next symbol when multiple symbols have the same address"
am: 8d43b7cf81
Change-Id: I188c96f3bd6fab327e961b06817813ee4e02bd4f
-rw-r--r-- | cmd/symbol_inject/macho.go | 50 | ||||
-rw-r--r-- | cmd/symbol_inject/pe.go | 54 |
2 files changed, 50 insertions, 54 deletions
diff --git a/cmd/symbol_inject/macho.go b/cmd/symbol_inject/macho.go index 4a3ecc74..478e6de8 100644 --- a/cmd/symbol_inject/macho.go +++ b/cmd/symbol_inject/macho.go @@ -38,37 +38,33 @@ func findMachoSymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) { 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(symbols) { - nextSymbol = &symbols[i+1] + for _, symbol := range symbols { + if symbol.Name == symbolName && symbol.Sect != 0 { + // Find the next symbol in the same section with a higher address + n := sort.Search(len(symbols), func(i int) bool { + return symbols[i].Sect == symbol.Sect && + symbols[i].Value > symbol.Value + }) + + section := machoFile.Sections[symbol.Sect-1] + + var end uint64 + if n < len(symbols) { + end = symbols[n].Value + } else { + end = section.Addr + section.Size } - return calculateMachoSymbolOffset(machoFile, symbol, nextSymbol) - } - } - return maxUint64, maxUint64, fmt.Errorf("symbol not found") -} + if end <= symbol.Value && end > symbol.Value+4096 { + return maxUint64, maxUint64, fmt.Errorf("symbol end address does not seem valid, %x:%x", symbol.Value, end) + } -func calculateMachoSymbolOffset(file *macho.File, symbol macho.Symbol, nextSymbol *macho.Symbol) (uint64, uint64, error) { - section := file.Sections[symbol.Sect-1] + size := end - symbol.Value - 1 + offset := uint64(section.Offset) + (symbol.Value - section.Addr) - var end uint64 - if nextSymbol != nil && nextSymbol.Sect != symbol.Sect { - nextSymbol = nil - } - if nextSymbol != nil { - end = nextSymbol.Value - } else { - end = section.Addr + section.Size + return offset, size, nil + } } - size := end - symbol.Value - 1 - offset := uint64(section.Offset) + (symbol.Value - section.Addr) - - return offset, size, nil + return maxUint64, maxUint64, fmt.Errorf("symbol not found") } diff --git a/cmd/symbol_inject/pe.go b/cmd/symbol_inject/pe.go index dad530d4..82fdff8c 100644 --- a/cmd/symbol_inject/pe.go +++ b/cmd/symbol_inject/pe.go @@ -32,41 +32,41 @@ func findPESymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) { symbolName = "_" + symbolName } - sort.Slice(peFile.Symbols, func(i, j int) bool { - if peFile.Symbols[i].SectionNumber != peFile.Symbols[j].SectionNumber { - return peFile.Symbols[i].SectionNumber < peFile.Symbols[j].SectionNumber + symbols := peFile.Symbols + sort.Slice(symbols, func(i, j int) bool { + if symbols[i].SectionNumber != symbols[j].SectionNumber { + return symbols[i].SectionNumber < symbols[j].SectionNumber } - return peFile.Symbols[i].Value < peFile.Symbols[j].Value + return symbols[i].Value < symbols[j].Value }) - for i, symbol := range peFile.Symbols { + for _, symbol := range symbols { if symbol.Name == symbolName { - var nextSymbol *pe.Symbol - if i+1 < len(peFile.Symbols) { - nextSymbol = peFile.Symbols[i+1] + // Find the next symbol (n the same section with a higher address + n := sort.Search(len(symbols), func(i int) bool { + return symbols[i].SectionNumber == symbol.SectionNumber && + symbols[i].Value > symbol.Value + }) + + section := peFile.Sections[symbol.SectionNumber-1] + + var end uint32 + if n < len(symbols) { + end = symbols[n].Value + } else { + end = section.Size } - return calculatePESymbolOffset(peFile, symbol, nextSymbol) - } - } - return maxUint64, maxUint64, fmt.Errorf("symbol not found") -} + if end <= symbol.Value && end > symbol.Value+4096 { + return maxUint64, maxUint64, fmt.Errorf("symbol end address does not seem valid, %x:%x", symbol.Value, end) + } -func calculatePESymbolOffset(file *pe.File, symbol *pe.Symbol, nextSymbol *pe.Symbol) (uint64, uint64, error) { - section := file.Sections[symbol.SectionNumber-1] + size := end - symbol.Value - 1 + offset := section.Offset + symbol.Value - var end uint32 - if nextSymbol != nil && nextSymbol.SectionNumber != symbol.SectionNumber { - nextSymbol = nil - } - if nextSymbol != nil { - end = nextSymbol.Value - } else { - end = section.Size + return uint64(offset), uint64(size), nil + } } - size := end - symbol.Value - 1 - offset := section.Offset + symbol.Value - - return uint64(offset), uint64(size), nil + return maxUint64, maxUint64, fmt.Errorf("symbol not found") } |