[lld] [lld-macho] Use Symbols as branch target for safe_thunks ICF (PR #126835)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 11:17:08 PST 2025


================
@@ -263,6 +265,33 @@ void ICF::forEachClassRange(size_t begin, size_t end,
   }
 }
 
+// Find or create a symbol at offset 0 in the given section
+static Symbol *getThunkTargetSymbol(ConcatInputSection *isec) {
+  for (Symbol *sym : isec->symbols) {
+    if (auto *d = dyn_cast<Defined>(sym)) {
+      if (d->value == 0)
+        return sym;
+    }
+  }
+
+  std::string thunkName;
+  if (isec->symbols.size() == 0)
+    thunkName = isec->getName().str() + ".icf.0";
+  else
+    thunkName = isec->getName().str() + "icf.thunk.target" +
+                std::to_string(icfThunkCounter++);
+
+  // If no symbol found at offset 0, create one
+  auto *sym = make<Defined>(thunkName, /*file=*/nullptr, isec,
+                            /*value=*/0, /*size=*/isec->getSize(),
+                            /*isWeakDef=*/false, /*isExternal=*/false,
+                            /*isPrivateExtern=*/false, /*isThumb=*/false,
+                            /*isReferencedDynamically=*/false,
+                            /*noDeadStrip=*/false);
+  isec->symbols.push_back(sym);
----------------
alx32 wrote:

This is not a common scenario so perf shouldn't be a problem here. 

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


More information about the llvm-commits mailing list