[lld-macho] Fix symbol name returned from InputSection::getLocation

This commit fixes the issue that getLocation always printed the name of
the first symbol in the section.

For clarity, upper_bound is used instead of a linear search for finding
the closest symbol name. Note that this change does not affect
performance: this function is only called when printing errors and
`symbols` typically contains a single symbol because of
.subsections_via_symbols.

Differential Revision: https://reviews.llvm.org/D127670
This commit is contained in:
Daniel Bertalan 2022-06-13 15:49:27 -04:00 committed by Nico Weber
parent c39b76ae2e
commit 5f627cc225
2 changed files with 13 additions and 6 deletions

View File

@ -58,12 +58,14 @@ static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
std::string InputSection::getLocation(uint64_t off) const {
// First, try to find a symbol that's near the offset. Use it as a reference
// point.
for (size_t i = 0; i < symbols.size(); ++i)
if (symbols[i]->value <= off &&
(i + 1 == symbols.size() || symbols[i + 1]->value > off))
return (toString(getFile()) + ":(symbol " + symbols.front()->getName() +
"+0x" + Twine::utohexstr(off - symbols[i]->value) + ")")
.str();
auto *nextSym = llvm::upper_bound(
symbols, off, [](uint64_t a, const Defined *b) { return a < b->value; });
if (nextSym != symbols.begin()) {
auto &sym = *std::prev(nextSym);
return (toString(getFile()) + ":(symbol " + sym->getName() + "+0x" +
Twine::utohexstr(off - sym->value) + ")")
.str();
}
// If that fails, use the section itself as a reference point.
for (const Subsection &subsec : section.subsections) {

View File

@ -17,6 +17,11 @@
_bar:
#--- test.s
# Added to ensure that the error mentions the right function.
_baz:
ret
.globl _main, _foo
_main: