[lld] [lld][macho] Restructure thunk generation algorithm (PR #193367)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Wed May 6 10:27:44 PDT 2026


================
@@ -120,19 +120,16 @@ bool TextOutputSection::isTargetKnownInRange(const ConcatInputSection &isec,
 }
 
 Defined *TextOutputSection::getThunkInRange(const ConcatInputSection &isec,
-                                            const Relocation &r) const {
+                                            const Relocation &r,
+                                            const ThunkInfo &thunkInfo) const {
   assert(!isTargetKnownInRange(isec, r));
+  if (!thunkInfo.sym)
+    return nullptr;
   uint64_t callVA = isec.getVA() + r.offset;
   uint64_t lowVA = target->backwardBranchRange < callVA
                        ? callVA - target->backwardBranchRange
                        : 0;
   uint64_t highVA = callVA + target->forwardBranchRange;
-  auto *funcSym = cast<Symbol *>(r.referent);
-  auto it = thunkMap.find(ThunkKey{funcSym, r.addend});
-  if (it == thunkMap.end())
-    return nullptr;
-  auto &thunkInfo = it->second;
-  assert(thunkInfo.sym);
----------------
ellishg wrote:

https://github.com/llvm/llvm-project/pull/193367/changes/e4c72c78233d3ab4de38b39975b4f51b08721615#diff-449d49c0cd04b7f82e059c92869f3d30576ff005a4def728a279357ac113df22L277-L283

Yeah, in this commit I changed the logic around creating `thunkInfo`s. Before, I would only create a entry in `thunkMap` in `createThunk()`, guaranteeing that if an entry is in `thunkInfo` it will have a valid `sym`. Now I optimistically create `thunkMap` entries and use `sym` to determine if the thunk exists or not. This is actually more aligned with the original behavior where it would create `thunkMap` entries even if the target is directly reachable.

https://github.com/llvm/llvm-project/blob/3c3e7e0784befd7b80a7fe265a3e4eb7e7e12d2f/lld/MachO/ConcatOutputSection.cpp#L353-L379

If I add `assert(thunkInfo.sym);` then many existing tests do fail.

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


More information about the llvm-commits mailing list