[PATCH] D123301: [lld-macho][nfc] Factor out & micro-optimize findSymbolAtOffset

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 7 05:37:35 PDT 2022


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

Our compact unwind handling code currently has some logic to locate a
symbol at a given offset in an InputSection. The EH frame code will need
to do something similar, so let's factor out the code.

I've also micro-optimized this code, adding a fast path that handles the
most common case when we have `.subsections_via_symbols` enabled. No
stat sig change when linking chromium_framework on my Mac Pro.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123301

Files:
  lld/MachO/InputFiles.cpp


Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -389,6 +389,29 @@
   return it->isec;
 }
 
+// Find a symbol at offset `off` within `isec`.
+static Defined *findSymbolAtOffset(const ConcatInputSection *isec,
+                                   uint64_t off) {
+  if (isec->symbols.size() == 1) {
+    // The common case when we have .subsections_via_symbols
+    Defined *d = isec->symbols[0];
+    if (d->value == 0)
+      return d;
+    assert(isec->wasCoalesced);
+    return nullptr;
+  }
+
+  auto it = llvm::lower_bound(isec->symbols, off, [](Defined *d, uint64_t off) {
+    return d->value < off;
+  });
+  // The offset should point at the exact address of a symbol (with no addend.)
+  if (it == isec->symbols.end() || (*it)->value != off) {
+    assert(isec->wasCoalesced);
+    return nullptr;
+  }
+  return *it;
+}
+
 template <class SectionHeader>
 static bool validateRelocationInfo(InputFile *file, const SectionHeader &sec,
                                    relocation_info rel) {
@@ -1010,17 +1033,9 @@
       // The functionAddress relocations are typically section relocations.
       // However, unwind info operates on a per-symbol basis, so we search for
       // the function symbol here.
-      auto symIt = llvm::lower_bound(
-          referentIsec->symbols, add,
-          [](Defined *d, uint64_t add) { return d->value < add; });
-      // The relocation should point at the exact address of a symbol (with no
-      // addend).
-      if (symIt == referentIsec->symbols.end() || (*symIt)->value != add) {
-        assert(referentIsec->wasCoalesced);
-        ++it;
-        continue;
-      }
-      (*symIt)->unwindEntry = isec;
+      Defined *d = findSymbolAtOffset(referentIsec, add);
+      if (d)
+        d->unwindEntry = isec;
       // Since we've sliced away the functionAddress, we should remove the
       // corresponding relocation too. Given that clang emits relocations in
       // reverse order of address, this relocation should be at the end of the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123301.421169.patch
Type: text/x-patch
Size: 2118 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220407/2d05fcfd/attachment-0001.bin>


More information about the llvm-commits mailing list