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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 06:52:41 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))
----------------
alx32 wrote:

I think they might be different enough to remain separate because: 
- In InputFiles it requires the offset to be to the start of the symbol: 
```
// The offset should point at the exact address of a symbol (with no addend.)
```
Here the offset can be to the inside of the symbol - see [here](https://github.com/llvm/llvm-project/pull/91238/files#diff-71cf08c0f7962a48ce0fef8466a82e328c49072bd05724aae1322bb12313839bR245) 
```
.quad	__DATA__TtC11SimpleClass11SimpleClass+2
```
- In InputFiles it relies on the symbols being ordered - which is not necessarily the case inside optimization passes. So here we have to do a linear scan. I don't think in practice this would be a restriction as the stage of this pass I don't think we run anything that reorders the symbols for these sections - but it's not a hard requirement to have them sorted as far as I know. 

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


More information about the llvm-commits mailing list