[lld] [lld-macho] Category merger: handle addends when getting symbol at offset (PR #91238)

Vincent Lee via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 18:06:13 PDT 2024


================
@@ -518,12 +519,32 @@ void ObjcCategoryMerger::collectSectionWriteInfoFromIsec(
 Symbol *
 ObjcCategoryMerger::tryGetSymbolAtIsecOffset(const ConcatInputSection *isec,
                                              uint32_t offset) {
+  if (!isec)
+    return nullptr;
   const Reloc *reloc = isec->getRelocAt(offset);
 
   if (!reloc)
     return nullptr;
 
-  return reloc->referent.get<Symbol *>();
+  Symbol *sym = reloc->referent.get<Symbol *>();
+
+  if (reloc->addend) {
+    assert(isa<Defined>(sym) && "Expected defined for non-zero addend");
+    Defined *definedSym = cast<Defined>(sym);
+    sym = tryFindDefinedOnIsec(definedSym->isec(),
+                               definedSym->value + reloc->addend);
+  }
+
+  return sym;
+}
+
+Defined *ObjcCategoryMerger::tryFindDefinedOnIsec(const InputSection *isec,
+                                                  uint32_t offset) {
+  for (Defined *sym : isec->symbols)
+    if ((sym->value <= offset) && (sym->value + sym->size > offset))
----------------
thevinster wrote:

I know the comment says that but the implementation seems to handle it for the case when it doesn't. Okay with the linear scan, but it would be a nice to make it faster if we know it's already sorted (probably worth checking). If it is sorted, it's worth adding an assert in that case as well to make sure we do do not invalidate correctness behavior. 

https://github.com/llvm/llvm-project/pull/91238


More information about the llvm-commits mailing list