diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp --- a/lld/MachO/InputSection.cpp +++ b/lld/MachO/InputSection.cpp @@ -58,12 +58,14 @@ 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) { diff --git a/lld/test/MachO/invalid/range-check.s b/lld/test/MachO/invalid/range-check.s --- a/lld/test/MachO/invalid/range-check.s +++ b/lld/test/MachO/invalid/range-check.s @@ -17,6 +17,11 @@ _bar: #--- test.s + +# Added to ensure that the error mentions the right function. +_baz: + ret + .globl _main, _foo _main: