[lld] [lld-macho] Fix thunk insertion for multiple .text sections scenario (PR #175704)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 20 10:03:37 PST 2026


================
@@ -114,41 +115,61 @@ void ConcatOutputSection::addInput(ConcatInputSection *input) {
 
 DenseMap<Symbol *, ThunkInfo> lld::macho::thunkMap;
 
-// Determine whether we need thunks, which depends on the target arch -- RISC
-// (i.e., ARM) generally does because it has limited-range branch/call
-// instructions, whereas CISC (i.e., x86) generally doesn't. RISC only needs
-// thunks for programs so large that branch source & destination addresses
-// might differ more than the range of branch instruction(s).
-bool TextOutputSection::needsThunks() const {
-  if (!target->usesThunks())
-    return false;
-  uint64_t isecAddr = addr;
+namespace {
+
+// Returns true if `osec` can be the target of a BRANCH relocation.
+// Branch targets include code sections and stub sections for dynamic calls.
+bool isBranchTargetSection(const OutputSection *osec) {
+  assert(osec->parent && "section must be in a segment");
+  return sections::isCodeSection(osec->name, osec->parent->name, osec->flags) ||
+         osec->name == section_names::stubs ||
+         osec->name == section_names::objcStubs;
+}
+
+} // namespace
+
+uint64_t TextOutputSection::estimateEndVA(uint64_t startVA) const {
+  uint64_t endVA = startVA;
   for (ConcatInputSection *isec : inputs)
-    isecAddr = alignToPowerOf2(isecAddr, isec->align) + isec->getSize();
-  // Other sections besides __text might be small enough to pass this
-  // test but nevertheless need thunks for calling into other sections.
-  // An imperfect heuristic to use in this case is that if a section
-  // we've already processed in this segment needs thunks, so do the
-  // rest.
-  bool needsThunks = parent && parent->needsThunks;
+    endVA = alignToPowerOf2(endVA, isec->align) + isec->getSize();
+  return endVA;
+}
 
-  // Calculate the total size of all branch target sections
-  uint64_t branchTargetsSize = in.stubs->getSize();
+uint64_t TextOutputSection::estimateFurthestBranchTargetEndVA() const {
+  uint64_t curVA = estimateEndVA(addr);
+  uint64_t furthestTargetEndVA = isBranchTargetSection(this) ? curVA : addr;
 
-  // Add the size of __objc_stubs section if it exists
-  if (in.objcStubs && in.objcStubs->isNeeded())
-    branchTargetsSize += in.objcStubs->getSize();
+  // Find this section in the segment's section list.
+  const std::vector<OutputSection *> &sections = parent->getSections();
+  auto it = llvm::find(sections, this);
+  assert(it != sections.end() && "section not found in parent segment");
----------------
ellishg wrote:

```suggestion
  ArrayRef<OutputSection *> sections = parent->getSections();
  auto subsequentSections = sections.take_until(this);
```

Then the for loop below simple becomes `for (auto osec : subsequentSections)`

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


More information about the llvm-commits mailing list