Skip to content

Commit c77d3a6

Browse files
committed
StaticBinaryELF: When finding a symbol by address allow a closest match.
- If an address does not fall inside a symbol's region then return the symbol with the smallest gap between then end of the symbol's region and the address.
1 parent 7e0bcf9 commit c77d3a6

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

stdlib/public/runtime/StaticBinaryELF.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,28 @@ class StaticBinaryELF {
177177
if (symbolTable.hasValue()) {
178178
auto searchAddr = reinterpret_cast<Elf_Addr>(addr);
179179
auto symbols = symbolTable->data<Elf_Sym>();
180+
const Elf_Sym *bestMatch = nullptr;
181+
unsigned long bestDistance = ULONG_MAX;
180182

181183
for (size_t idx = 0; idx < symbols.size(); idx++) {
182184
auto symbol = &symbols[idx];
183185
if (ELF_ST_TYPE(symbol->st_info) == STT_FUNC
184-
&& searchAddr >= symbol->st_value
185-
&& searchAddr < (symbol->st_value + symbol->st_size)) {
186-
return symbol;
186+
&& searchAddr >= symbol->st_value) {
187+
188+
auto tmpDistance = searchAddr - symbol->st_value;
189+
if (tmpDistance < symbol->st_size) {
190+
return symbol;
191+
}
192+
// The searchAddress is past the end of this symbol's region, keep
193+
// track of which symbol end address the searchAddress is closest to.
194+
tmpDistance -= symbol->st_size;
195+
if (tmpDistance < bestDistance) {
196+
bestMatch = symbol;
197+
tmpDistance = bestDistance;
198+
}
187199
}
188200
}
201+
return bestMatch;
189202
}
190203
return nullptr;
191204
}
@@ -334,7 +347,7 @@ swift::lookupSymbol(const void *address, SymbolInfo *info) {
334347
auto symbol = binary.findSymbol(address);
335348
if (symbol != nullptr) {
336349
info->symbolAddress = reinterpret_cast<void *>(symbol->st_value);
337-
info->symbolName = binary.symbolName(symbol);
350+
info->symbolName.reset(binary.symbolName(symbol));
338351
} else {
339352
info->symbolAddress = nullptr;
340353
info->symbolName = nullptr;

0 commit comments

Comments
 (0)