[PATCH] D127670: [lld-macho] Use binary search in InputSection::getLocation

Daniel Bertalan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 13 10:15:25 PDT 2022


BertalanD created this revision.
BertalanD added reviewers: thakis, int3, lld-macho.
Herald added projects: lld-macho, All.
BertalanD requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

[lld-macho] Use binary search in InputSection::getLocation

`symbols` is a sorted vector, so we can search for the containing symbol
using binary search.

This commit also fixes the issue that getLocation always printed the
name of the first symbol in the section. The name of symbols.first() was
printed instead of symbols[i].

Please commit this diff with the following author info:
Daniel Bertalan <dani at danielbertalan.dev>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127670

Files:
  lld/MachO/InputSection.cpp


Index: lld/MachO/InputSection.cpp
===================================================================
--- lld/MachO/InputSection.cpp
+++ 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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127670.436457.patch
Type: text/x-patch
Size: 1131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220613/ccaea3b9/attachment.bin>


More information about the llvm-commits mailing list