[lld] [lld][macho] Restructure thunk generation algorithm (PR #193367)
Zhaoxuan Jiang via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 27 04:39:20 PDT 2026
================
@@ -276,171 +115,196 @@ void TextOutputSection::finalize() {
uint64_t forwardBranchRange = target->forwardBranchRange;
uint64_t backwardBranchRange = target->backwardBranchRange;
- uint64_t branchTargetThresholdVA = TargetInfo::outOfRangeVA;
size_t thunkSize = target->thunkSize;
- size_t relocCount = 0;
- size_t callSiteCount = 0;
size_t thunkCallCount = 0;
size_t thunkCount = 0;
- // Walk all sections in order. Finalize all sections that are less than
- // forwardBranchRange in front of it.
- // isecVA is the address of the current section.
- // addr + size is the start address of the first non-finalized section.
-
- // inputs[finalIdx] is for finalization (address-assignment)
- size_t finalIdx = 0;
- // Kick-off by ensuring that the first input section has an address
- for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx;
- ++callIdx) {
- if (finalIdx == callIdx)
- finalizeOne(inputs[finalIdx++]);
- ConcatInputSection *isec = inputs[callIdx];
- assert(isec->isFinal);
- uint64_t isecVA = isec->getVA();
-
- // Assign addresses up-to the forward branch-range limit.
- // Every call instruction needs a small number of bytes (on Arm64: 4),
- // and each inserted thunk needs a slightly larger number of bytes
- // (on Arm64: 12). If a section starts with a branch instruction and
- // contains several branch instructions in succession, then the distance
- // from the current position to the position where the thunks are inserted
- // grows. So leave room for a bunch of thunks.
- unsigned slop = config->slopScale * thunkSize;
- while (finalIdx < endIdx) {
- uint64_t expectedNewSize =
- alignToPowerOf2(addr + size, inputs[finalIdx]->align) +
- inputs[finalIdx]->getSize();
- if (expectedNewSize >= isecVA + forwardBranchRange - slop)
+ auto isTargetInRange = [&](const ConcatInputSection &isec,
+ const Relocation &r) -> bool {
+ uint64_t callVA = isec.getVA() + r.offset;
+ uint64_t lowVA =
+ backwardBranchRange < callVA ? callVA - backwardBranchRange : 0;
+ uint64_t highVA = callVA + forwardBranchRange;
+ auto *funcSym = cast<Symbol *>(r.referent);
+ uint64_t funcVA = resolveSymbolOffsetVA(funcSym, r.type, r.addend);
+ // Check if the referent is reachable with a simple call instruction.
+ return lowVA <= funcVA && funcVA <= highVA;
+ };
+ auto getThunkInRange = [&](const ConcatInputSection &isec,
+ const Relocation &r) -> Defined * {
+ assert(!isTargetInRange(isec, r));
+ uint64_t callVA = isec.getVA() + r.offset;
+ uint64_t lowVA =
+ backwardBranchRange < callVA ? callVA - backwardBranchRange : 0;
+ uint64_t highVA = callVA + forwardBranchRange;
+ auto *funcSym = cast<Symbol *>(r.referent);
+ ThunkInfo &thunkInfo = thunkMap[ThunkKey{funcSym, r.addend}];
+ if (thunkInfo.sym) {
+ uint64_t thunkVA = thunkInfo.isec->getVA();
+ if (lowVA <= thunkVA && thunkVA <= highVA)
+ return thunkInfo.sym;
+ }
+ return nullptr;
+ };
+ auto createThunk = [&](const ConcatInputSection &isec, Relocation &r) {
+ assert(getThunkInRange(isec, r) == nullptr);
+ assert(isec.isFinal);
+ uint64_t highVA = isec.getVA() + r.offset + forwardBranchRange;
+ if (addr + size > highVA) {
+ // There were too many consecutive branch instructions for `slop`
+ // below. If you hit this: For the current algorithm, just bumping up
+ // slop above and trying again is probably simplest. (See also PR51578
+ // comment 5).
+ fatal(Twine(__FUNCTION__) +
+ ": FIXME: thunk range overrun. Consider increasing the "
+ "slop-scale with `--slop-scale=<unsigned_int>`.");
+ }
+ auto *funcSym = cast<Symbol *>(r.referent);
+ ThunkInfo &thunkInfo = thunkMap[ThunkKey{funcSym, r.addend}];
+ thunkInfo.isec =
+ makeSyntheticInputSection(isec.getSegName(), isec.getName());
+ thunkInfo.isec->parent = this;
+ assert(thunkInfo.isec->live);
+
+ std::string addendSuffix;
+ if (r.addend != 0)
+ addendSuffix = "+" + std::to_string(r.addend);
+ StringRef thunkName =
+ saver().save(funcSym->getName() + addendSuffix + ".thunk." +
+ std::to_string(thunkInfo.sequence++));
+ if (!isa<Defined>(funcSym) || cast<Defined>(funcSym)->isExternal()) {
+ r.referent = thunkInfo.sym = symtab->addDefined(
+ thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
+ /*isWeakDef=*/false, /*isPrivateExtern=*/true,
+ /*isReferencedDynamically=*/false, /*noDeadStrip=*/false,
+ /*isWeakDefCanBeHidden=*/false);
+ } else {
+ r.referent = thunkInfo.sym = make<Defined>(
+ thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
+ /*isWeakDef=*/false, /*isExternal=*/false, /*isPrivateExtern=*/true,
+ /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
+ /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false);
+ }
+ thunkInfo.sym->used = true;
+ target->populateThunk(thunkInfo.isec, funcSym, r.addend);
+ // The thunk itself bakes in the addend, so the call-site reloc must
+ // branch to the thunk start with no extra offset.
+ r.addend = 0;
+ ++thunkCallCount;
+ finalizeOne(thunkInfo.isec);
+ thunks.push_back(thunkInfo.isec);
+ ++thunkCount;
+ };
+
+ std::deque<std::tuple<ConcatInputSection *, Relocation *, ThunkKey>>
+ branchesToProcess;
+ SmallVector<std::tuple<ConcatInputSection *, Relocation *, Defined *>>
+ deferredBranchRedirects;
+
+ const uint64_t slop = config->slopScale * thunkSize;
+ for (auto *isec : inputs) {
+ while (!branchesToProcess.empty()) {
+ auto &[callerIsec, r, thunkKey] = branchesToProcess.front();
+ assert(callerIsec->isFinal);
+ if (isTargetInRange(*callerIsec, *r)) {
+ branchesToProcess.pop_front();
+ continue;
+ }
+ if (auto *sym = getThunkInRange(*callerIsec, *r)) {
+ deferredBranchRedirects.emplace_back(callerIsec, r, sym);
+ branchesToProcess.pop_front();
+ continue;
+ }
+ uint64_t highVA = callerIsec->getVA() + r->offset + forwardBranchRange;
+ uint64_t nextEnd =
+ alignToPowerOf2(addr + size, isec->align) + isec->getSize();
+ if (nextEnd + slop <= highVA)
break;
- finalizeOne(inputs[finalIdx++]);
+
+ createThunk(*callerIsec, *r);
+ branchesToProcess.pop_front();
}
+ finalizeOne(isec);
- if (!isec->hasCallSites)
+ bool hasCallsite = llvm::any_of(isec->relocs, [](Relocation &r) {
+ return target->hasAttr(r.type, RelocAttrBits::BRANCH);
+ });
+ if (!hasCallsite)
continue;
- if (finalIdx == endIdx &&
- branchTargetThresholdVA == TargetInfo::outOfRangeVA) {
- // When we have finalized all input sections, branch target sections (like
- // __stubs and __objc_stubs) (destined to follow __text) come within range
- // of forward branches and we can estimate the threshold address after
- // which we can reach any branch target with a forward branch. Note that
- // although it sits in the middle of a loop, this code executes only once.
- // It is in the loop because we need to call it at the proper
- // time: the earliest call site from which the end of __text
- // (and start of branch target sections) comes within range of a forward
- // branch.
- branchTargetThresholdVA = estimateBranchTargetThresholdVA(callIdx);
- }
// Process relocs by ascending address, i.e., ascending offset within isec
- std::vector<Relocation> &relocs = isec->relocs;
// FIXME: This property does not hold for object files produced by ld64's
// `-r` mode.
- assert(is_sorted(relocs, [](Relocation &a, Relocation &b) {
+ assert(is_sorted(isec->relocs, [](Relocation &a, Relocation &b) {
return a.offset > b.offset;
}));
- for (Relocation &r : reverse(relocs)) {
- ++relocCount;
+ for (Relocation &r : reverse(isec->relocs)) {
if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
continue;
- ++callSiteCount;
- // Calculate branch reachability boundaries
- uint64_t callVA = isecVA + r.offset;
- uint64_t lowVA =
- backwardBranchRange < callVA ? callVA - backwardBranchRange : 0;
- uint64_t highVA = callVA + forwardBranchRange;
- // Calculate our call referent address
- auto *funcSym = cast<Symbol *>(r.referent);
- ThunkInfo &thunkInfo = thunkMap[ThunkKey{funcSym, r.addend}];
- // The referent is not reachable, so we need to use a thunk... unless we
- // are close enough to the end that branch target sections (__stubs,
- // __objc_stubs) are now within range of a simple forward branch -- BUT
- // only for zero-addend branches. The writer's resolveSymbolOffsetVA()
- // resolves non-zero-addend branches against the symbol body rather than
- // the stub, so __stubs reachability says nothing about whether such a
- // call can be emitted directly. Hence the `r.addend == 0` guard below.
- // See INTERP check lines in arm64-thunk-branch-addend.s.
- if (r.addend == 0 &&
- (funcSym->isInStubs() ||
- (in.objcStubs && in.objcStubs->isNeeded() &&
- ObjCStubsSection::isObjCStubSymbol(funcSym))) &&
- callVA >= branchTargetThresholdVA) {
- assert(callVA != TargetInfo::outOfRangeVA);
+ if (isTargetInRange(*isec, r))
continue;
- }
- // Use the same resolution rules as the writer: for non-zero addends this
- // goes directly to the symbol body rather than any stub trampoline.
- // See INTERP check lines in arm64-thunk-branch-addend.s.
- uint64_t funcVA = resolveSymbolOffsetVA(funcSym, r.type, r.addend);
- ++thunkInfo.callSitesUsed;
- if (lowVA <= funcVA && funcVA <= highVA) {
- // The referent is reachable with a simple call instruction.
+ if (auto *sym = getThunkInRange(*isec, r)) {
+ deferredBranchRedirects.emplace_back(isec, &r, sym);
continue;
}
- ++thunkInfo.thunkCallCount;
- ++thunkCallCount;
- // If an existing thunk is reachable, use it ...
- if (thunkInfo.sym) {
- uint64_t thunkVA = thunkInfo.isec->getVA();
- if (lowVA <= thunkVA && thunkVA <= highVA) {
- r.referent = thunkInfo.sym;
- // The thunk itself bakes in the addend, so the call-site reloc must
- // branch to the thunk start with no extra offset.
- r.addend = 0;
- continue;
- }
- }
- // ... otherwise, create a new thunk.
- if (addr + size > highVA) {
- // There were too many consecutive branch instructions for `slop`
- // above. If you hit this: For the current algorithm, just bumping up
- // slop above and trying again is probably simplest. (See also PR51578
- // comment 5).
- fatal(Twine(__FUNCTION__) +
- ": FIXME: thunk range overrun. Consider increasing the "
- "slop-scale with `--slop-scale=<unsigned_int>`.");
- }
- thunkInfo.isec =
- makeSyntheticInputSection(isec->getSegName(), isec->getName());
- thunkInfo.isec->parent = this;
- assert(thunkInfo.isec->live);
-
- std::string addendSuffix;
- if (r.addend != 0)
- addendSuffix = "+" + std::to_string(r.addend);
- StringRef thunkName =
- saver().save(funcSym->getName() + addendSuffix + ".thunk." +
- std::to_string(thunkInfo.sequence++));
- if (!isa<Defined>(funcSym) || cast<Defined>(funcSym)->isExternal()) {
- r.referent = thunkInfo.sym = symtab->addDefined(
- thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
- /*isWeakDef=*/false, /*isPrivateExtern=*/true,
- /*isReferencedDynamically=*/false, /*noDeadStrip=*/false,
- /*isWeakDefCanBeHidden=*/false);
- } else {
- r.referent = thunkInfo.sym = make<Defined>(
- thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
- /*isWeakDef=*/false, /*isExternal=*/false, /*isPrivateExtern=*/true,
- /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
- /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false);
- }
- thunkInfo.sym->used = true;
- target->populateThunk(thunkInfo.isec, funcSym, r.addend);
+ auto *funcSym = cast<Symbol *>(r.referent);
+ ThunkKey key{funcSym, r.addend};
+ branchesToProcess.emplace_back(isec, &r, key);
+ }
+ }
+ for (auto [callerIsec, r, thunk] : deferredBranchRedirects) {
+ if (isTargetInRange(*callerIsec, *r))
+ continue;
+ r->referent = thunk;
+ r->addend = 0;
+ ++thunkCallCount;
+ }
+ deferredBranchRedirects.clear();
+
+ llvm::erase_if(branchesToProcess, [&](auto &tuple) {
+ auto [callerIsec, r, thunkKey] = tuple;
+ return isTargetInRange(*callerIsec, *r);
+ });
+ // Count the number of new thunks we will need to create
+ DenseSet<ThunkKey, ThunkMapKeyInfo> branchTargets;
+ for (auto [callerIsec, r, thunkKey] : branchesToProcess)
+ if (!getThunkInRange(*callerIsec, *r))
+ branchTargets.insert(thunkKey);
+
+ uint64_t estimatedTextEnd = addr + size + branchTargets.size() * thunkSize;
+ uint64_t estimatedStubsEnd =
+ alignToPowerOf2(estimatedTextEnd, in.stubs->align) + in.stubs->getSize();
+ if (in.objcStubs && in.objcStubs->isNeeded())
+ estimatedStubsEnd =
+ alignToPowerOf2(estimatedStubsEnd, in.objcStubs->align) +
+ in.objcStubs->getSize();
+
+ for (auto [isec, r, thunkKey] : branchesToProcess) {
+ uint64_t highVA = isec->getVA() + r->offset + forwardBranchRange;
+ auto *funcSym = cast<Symbol *>(r->referent);
+ if ((funcSym->isInStubs() ||
+ (in.objcStubs && in.objcStubs->isNeeded() &&
+ ObjCStubsSection::isObjCStubSymbol(funcSym))) &&
+ r->addend == 0 && estimatedStubsEnd <= highVA) {
+ // The branch target can reach any section in __stubs or __objc_stubs
+ assert(isec->getVA() != TargetInfo::outOfRangeVA);
+ continue;
+ }
+ if (auto *sym = getThunkInRange(*isec, *r)) {
+ r->referent = sym;
----------------
nocchijiang wrote:
I do not think a separate file is necessary here, moving a few of the larger lambdas into private `TextOutputSection` helpers would be enough. Also, once branch island support is added, I expect this area will likely need another non-trivial rewrite anyway, so I am fine keeping the current structure for now.
https://github.com/llvm/llvm-project/pull/193367
More information about the llvm-commits
mailing list