xref: /freebsd-src/contrib/llvm-project/lld/MachO/ConcatOutputSection.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fe6060f1SDimitry Andric //===- ConcatOutputSection.cpp --------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #include "ConcatOutputSection.h"
10fe6060f1SDimitry Andric #include "Config.h"
11fe6060f1SDimitry Andric #include "OutputSegment.h"
12fe6060f1SDimitry Andric #include "SymbolTable.h"
13fe6060f1SDimitry Andric #include "Symbols.h"
14fe6060f1SDimitry Andric #include "SyntheticSections.h"
15fe6060f1SDimitry Andric #include "Target.h"
1604eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
17fe6060f1SDimitry Andric #include "llvm/BinaryFormat/MachO.h"
18fe6060f1SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
19fe6060f1SDimitry Andric #include "llvm/Support/TimeProfiler.h"
20fe6060f1SDimitry Andric 
21fe6060f1SDimitry Andric using namespace llvm;
22fe6060f1SDimitry Andric using namespace llvm::MachO;
23fe6060f1SDimitry Andric using namespace lld;
24fe6060f1SDimitry Andric using namespace lld::macho;
25fe6060f1SDimitry Andric 
26fe6060f1SDimitry Andric MapVector<NamePair, ConcatOutputSection *> macho::concatOutputSections;
27fe6060f1SDimitry Andric 
28fe6060f1SDimitry Andric void ConcatOutputSection::addInput(ConcatInputSection *input) {
29fe6060f1SDimitry Andric   assert(input->parent == this);
30fe6060f1SDimitry Andric   if (inputs.empty()) {
31fe6060f1SDimitry Andric     align = input->align;
32fe6060f1SDimitry Andric     flags = input->getFlags();
33fe6060f1SDimitry Andric   } else {
34fe6060f1SDimitry Andric     align = std::max(align, input->align);
35fe6060f1SDimitry Andric     finalizeFlags(input);
36fe6060f1SDimitry Andric   }
37fe6060f1SDimitry Andric   inputs.push_back(input);
38fe6060f1SDimitry Andric }
39fe6060f1SDimitry Andric 
40fe6060f1SDimitry Andric // Branch-range extension can be implemented in two ways, either through ...
41fe6060f1SDimitry Andric //
42fe6060f1SDimitry Andric // (1) Branch islands: Single branch instructions (also of limited range),
43fe6060f1SDimitry Andric //     that might be chained in multiple hops to reach the desired
44fe6060f1SDimitry Andric //     destination. On ARM64, as 16 branch islands are needed to hop between
45fe6060f1SDimitry Andric //     opposite ends of a 2 GiB program. LD64 uses branch islands exclusively,
46fe6060f1SDimitry Andric //     even when it needs excessive hops.
47fe6060f1SDimitry Andric //
48fe6060f1SDimitry Andric // (2) Thunks: Instruction(s) to load the destination address into a scratch
49fe6060f1SDimitry Andric //     register, followed by a register-indirect branch. Thunks are
50fe6060f1SDimitry Andric //     constructed to reach any arbitrary address, so need not be
51fe6060f1SDimitry Andric //     chained. Although thunks need not be chained, a program might need
52fe6060f1SDimitry Andric //     multiple thunks to the same destination distributed throughout a large
53fe6060f1SDimitry Andric //     program so that all call sites can have one within range.
54fe6060f1SDimitry Andric //
55349cc55cSDimitry Andric // The optimal approach is to mix islands for destinations within two hops,
56fe6060f1SDimitry Andric // and use thunks for destinations at greater distance. For now, we only
57fe6060f1SDimitry Andric // implement thunks. TODO: Adding support for branch islands!
58fe6060f1SDimitry Andric //
59fe6060f1SDimitry Andric // Internally -- as expressed in LLD's data structures -- a
6081ad6265SDimitry Andric // branch-range-extension thunk consists of:
61fe6060f1SDimitry Andric //
6281ad6265SDimitry Andric // (1) new Defined symbol for the thunk named
63fe6060f1SDimitry Andric //     <FUNCTION>.thunk.<SEQUENCE>, which references ...
64fe6060f1SDimitry Andric // (2) new InputSection, which contains ...
65fe6060f1SDimitry Andric // (3.1) new data for the instructions to load & branch to the far address +
66fe6060f1SDimitry Andric // (3.2) new Relocs on instructions to load the far address, which reference ...
6781ad6265SDimitry Andric // (4.1) existing Defined symbol for the real function in __text, or
68fe6060f1SDimitry Andric // (4.2) existing DylibSymbol for the real function in a dylib
69fe6060f1SDimitry Andric //
70fe6060f1SDimitry Andric // Nearly-optimal thunk-placement algorithm features:
71fe6060f1SDimitry Andric //
72fe6060f1SDimitry Andric // * Single pass: O(n) on the number of call sites.
73fe6060f1SDimitry Andric //
74fe6060f1SDimitry Andric // * Accounts for the exact space overhead of thunks - no heuristics
75fe6060f1SDimitry Andric //
76fe6060f1SDimitry Andric // * Exploits the full range of call instructions - forward & backward
77fe6060f1SDimitry Andric //
78fe6060f1SDimitry Andric // Data:
79fe6060f1SDimitry Andric //
80fe6060f1SDimitry Andric // * DenseMap<Symbol *, ThunkInfo> thunkMap: Maps the function symbol
81fe6060f1SDimitry Andric //   to its thunk bookkeeper.
82fe6060f1SDimitry Andric //
83fe6060f1SDimitry Andric // * struct ThunkInfo (bookkeeper): Call instructions have limited range, and
84fe6060f1SDimitry Andric //   distant call sites might be unable to reach the same thunk, so multiple
85fe6060f1SDimitry Andric //   thunks are necessary to serve all call sites in a very large program. A
86fe6060f1SDimitry Andric //   thunkInfo stores state for all thunks associated with a particular
8781ad6265SDimitry Andric //   function:
8881ad6265SDimitry Andric //     (a) thunk symbol
8981ad6265SDimitry Andric //     (b) input section containing stub code, and
9081ad6265SDimitry Andric //     (c) sequence number for the active thunk incarnation.
9181ad6265SDimitry Andric //   When an old thunk goes out of range, we increment the sequence number and
9281ad6265SDimitry Andric //   create a new thunk named <FUNCTION>.thunk.<SEQUENCE>.
93fe6060f1SDimitry Andric //
9481ad6265SDimitry Andric // * A thunk consists of
9581ad6265SDimitry Andric //     (a) a Defined symbol pointing to
9681ad6265SDimitry Andric //     (b) an InputSection holding machine code (similar to a MachO stub), and
9781ad6265SDimitry Andric //     (c) relocs referencing the real function for fixing up the stub code.
98fe6060f1SDimitry Andric //
99fe6060f1SDimitry Andric // * std::vector<InputSection *> MergedInputSection::thunks: A vector parallel
100fe6060f1SDimitry Andric //   to the inputs vector. We store new thunks via cheap vector append, rather
101fe6060f1SDimitry Andric //   than costly insertion into the inputs vector.
102fe6060f1SDimitry Andric //
103fe6060f1SDimitry Andric // Control Flow:
104fe6060f1SDimitry Andric //
105fe6060f1SDimitry Andric // * During address assignment, MergedInputSection::finalize() examines call
106fe6060f1SDimitry Andric //   sites by ascending address and creates thunks.  When a function is beyond
107fe6060f1SDimitry Andric //   the range of a call site, we need a thunk. Place it at the largest
108fe6060f1SDimitry Andric //   available forward address from the call site. Call sites increase
109fe6060f1SDimitry Andric //   monotonically and thunks are always placed as far forward as possible;
110fe6060f1SDimitry Andric //   thus, we place thunks at monotonically increasing addresses. Once a thunk
111fe6060f1SDimitry Andric //   is placed, it and all previous input-section addresses are final.
112fe6060f1SDimitry Andric //
113349cc55cSDimitry Andric // * ConcatInputSection::finalize() and ConcatInputSection::writeTo() merge
114fe6060f1SDimitry Andric //   the inputs and thunks vectors (both ordered by ascending address), which
115fe6060f1SDimitry Andric //   is simple and cheap.
116fe6060f1SDimitry Andric 
117fe6060f1SDimitry Andric DenseMap<Symbol *, ThunkInfo> lld::macho::thunkMap;
118fe6060f1SDimitry Andric 
119fe6060f1SDimitry Andric // Determine whether we need thunks, which depends on the target arch -- RISC
120fe6060f1SDimitry Andric // (i.e., ARM) generally does because it has limited-range branch/call
121fe6060f1SDimitry Andric // instructions, whereas CISC (i.e., x86) generally doesn't. RISC only needs
122fe6060f1SDimitry Andric // thunks for programs so large that branch source & destination addresses
123fe6060f1SDimitry Andric // might differ more than the range of branch instruction(s).
12481ad6265SDimitry Andric bool TextOutputSection::needsThunks() const {
125fe6060f1SDimitry Andric   if (!target->usesThunks())
126fe6060f1SDimitry Andric     return false;
127fe6060f1SDimitry Andric   uint64_t isecAddr = addr;
1280eae32dcSDimitry Andric   for (ConcatInputSection *isec : inputs)
12906c3fb27SDimitry Andric     isecAddr = alignToPowerOf2(isecAddr, isec->align) + isec->getSize();
130349cc55cSDimitry Andric   if (isecAddr - addr + in.stubs->getSize() <=
131349cc55cSDimitry Andric       std::min(target->backwardBranchRange, target->forwardBranchRange))
132fe6060f1SDimitry Andric     return false;
133fe6060f1SDimitry Andric   // Yes, this program is large enough to need thunks.
1340eae32dcSDimitry Andric   for (ConcatInputSection *isec : inputs) {
135fe6060f1SDimitry Andric     for (Reloc &r : isec->relocs) {
136fe6060f1SDimitry Andric       if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
137fe6060f1SDimitry Andric         continue;
138fe6060f1SDimitry Andric       auto *sym = r.referent.get<Symbol *>();
139fe6060f1SDimitry Andric       // Pre-populate the thunkMap and memoize call site counts for every
140fe6060f1SDimitry Andric       // InputSection and ThunkInfo. We do this for the benefit of
14181ad6265SDimitry Andric       // estimateStubsInRangeVA().
142fe6060f1SDimitry Andric       ThunkInfo &thunkInfo = thunkMap[sym];
143fe6060f1SDimitry Andric       // Knowing ThunkInfo call site count will help us know whether or not we
144fe6060f1SDimitry Andric       // might need to create more for this referent at the time we are
145349cc55cSDimitry Andric       // estimating distance to __stubs in estimateStubsInRangeVA().
146fe6060f1SDimitry Andric       ++thunkInfo.callSiteCount;
1470eae32dcSDimitry Andric       // We can avoid work on InputSections that have no BRANCH relocs.
1480eae32dcSDimitry Andric       isec->hasCallSites = true;
149fe6060f1SDimitry Andric     }
150fe6060f1SDimitry Andric   }
151fe6060f1SDimitry Andric   return true;
152fe6060f1SDimitry Andric }
153fe6060f1SDimitry Andric 
154fe6060f1SDimitry Andric // Since __stubs is placed after __text, we must estimate the address
155fe6060f1SDimitry Andric // beyond which stubs are within range of a simple forward branch.
156349cc55cSDimitry Andric // This is called exactly once, when the last input section has been finalized.
15781ad6265SDimitry Andric uint64_t TextOutputSection::estimateStubsInRangeVA(size_t callIdx) const {
158349cc55cSDimitry Andric   // Tally the functions which still have call sites remaining to process,
159349cc55cSDimitry Andric   // which yields the maximum number of thunks we might yet place.
160fe6060f1SDimitry Andric   size_t maxPotentialThunks = 0;
161fe6060f1SDimitry Andric   for (auto &tp : thunkMap) {
162fe6060f1SDimitry Andric     ThunkInfo &ti = tp.second;
163349cc55cSDimitry Andric     // This overcounts: Only sections that are in forward jump range from the
164349cc55cSDimitry Andric     // currently-active section get finalized, and all input sections are
165349cc55cSDimitry Andric     // finalized when estimateStubsInRangeVA() is called. So only backward
166349cc55cSDimitry Andric     // jumps will need thunks, but we count all jumps.
167349cc55cSDimitry Andric     if (ti.callSitesUsed < ti.callSiteCount)
168349cc55cSDimitry Andric       maxPotentialThunks += 1;
169fe6060f1SDimitry Andric   }
170fe6060f1SDimitry Andric   // Tally the total size of input sections remaining to process.
171349cc55cSDimitry Andric   uint64_t isecVA = inputs[callIdx]->getVA();
172349cc55cSDimitry Andric   uint64_t isecEnd = isecVA;
173349cc55cSDimitry Andric   for (size_t i = callIdx; i < inputs.size(); i++) {
174fe6060f1SDimitry Andric     InputSection *isec = inputs[i];
17506c3fb27SDimitry Andric     isecEnd = alignToPowerOf2(isecEnd, isec->align) + isec->getSize();
176fe6060f1SDimitry Andric   }
177fe6060f1SDimitry Andric   // Estimate the address after which call sites can safely call stubs
178fe6060f1SDimitry Andric   // directly rather than through intermediary thunks.
179349cc55cSDimitry Andric   uint64_t forwardBranchRange = target->forwardBranchRange;
180349cc55cSDimitry Andric   assert(isecEnd > forwardBranchRange &&
181349cc55cSDimitry Andric          "should not run thunk insertion if all code fits in jump range");
182349cc55cSDimitry Andric   assert(isecEnd - isecVA <= forwardBranchRange &&
183349cc55cSDimitry Andric          "should only finalize sections in jump range");
184fe6060f1SDimitry Andric   uint64_t stubsInRangeVA = isecEnd + maxPotentialThunks * target->thunkSize +
185349cc55cSDimitry Andric                             in.stubs->getSize() - forwardBranchRange;
186fe6060f1SDimitry Andric   log("thunks = " + std::to_string(thunkMap.size()) +
187fe6060f1SDimitry Andric       ", potential = " + std::to_string(maxPotentialThunks) +
188fe6060f1SDimitry Andric       ", stubs = " + std::to_string(in.stubs->getSize()) + ", isecVA = " +
18981ad6265SDimitry Andric       utohexstr(isecVA) + ", threshold = " + utohexstr(stubsInRangeVA) +
19081ad6265SDimitry Andric       ", isecEnd = " + utohexstr(isecEnd) +
19181ad6265SDimitry Andric       ", tail = " + utohexstr(isecEnd - isecVA) +
19281ad6265SDimitry Andric       ", slop = " + utohexstr(forwardBranchRange - (isecEnd - isecVA)));
193fe6060f1SDimitry Andric   return stubsInRangeVA;
194fe6060f1SDimitry Andric }
195fe6060f1SDimitry Andric 
19681ad6265SDimitry Andric void ConcatOutputSection::finalizeOne(ConcatInputSection *isec) {
19706c3fb27SDimitry Andric   size = alignToPowerOf2(size, isec->align);
19806c3fb27SDimitry Andric   fileSize = alignToPowerOf2(fileSize, isec->align);
19981ad6265SDimitry Andric   isec->outSecOff = size;
200fe6060f1SDimitry Andric   isec->isFinal = true;
20181ad6265SDimitry Andric   size += isec->getSize();
20281ad6265SDimitry Andric   fileSize += isec->getFileSize();
20381ad6265SDimitry Andric }
204fe6060f1SDimitry Andric 
20581ad6265SDimitry Andric void ConcatOutputSection::finalizeContents() {
20681ad6265SDimitry Andric   for (ConcatInputSection *isec : inputs)
20781ad6265SDimitry Andric     finalizeOne(isec);
20881ad6265SDimitry Andric }
20981ad6265SDimitry Andric 
21081ad6265SDimitry Andric void TextOutputSection::finalize() {
211fe6060f1SDimitry Andric   if (!needsThunks()) {
212fe6060f1SDimitry Andric     for (ConcatInputSection *isec : inputs)
213fe6060f1SDimitry Andric       finalizeOne(isec);
214fe6060f1SDimitry Andric     return;
215fe6060f1SDimitry Andric   }
216fe6060f1SDimitry Andric 
217349cc55cSDimitry Andric   uint64_t forwardBranchRange = target->forwardBranchRange;
218349cc55cSDimitry Andric   uint64_t backwardBranchRange = target->backwardBranchRange;
219fe6060f1SDimitry Andric   uint64_t stubsInRangeVA = TargetInfo::outOfRangeVA;
220fe6060f1SDimitry Andric   size_t thunkSize = target->thunkSize;
221fe6060f1SDimitry Andric   size_t relocCount = 0;
222fe6060f1SDimitry Andric   size_t callSiteCount = 0;
223fe6060f1SDimitry Andric   size_t thunkCallCount = 0;
224fe6060f1SDimitry Andric   size_t thunkCount = 0;
225fe6060f1SDimitry Andric 
226349cc55cSDimitry Andric   // Walk all sections in order. Finalize all sections that are less than
227349cc55cSDimitry Andric   // forwardBranchRange in front of it.
228349cc55cSDimitry Andric   // isecVA is the address of the current section.
22981ad6265SDimitry Andric   // addr + size is the start address of the first non-finalized section.
230349cc55cSDimitry Andric 
231fe6060f1SDimitry Andric   // inputs[finalIdx] is for finalization (address-assignment)
232fe6060f1SDimitry Andric   size_t finalIdx = 0;
233fe6060f1SDimitry Andric   // Kick-off by ensuring that the first input section has an address
234fe6060f1SDimitry Andric   for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx;
235fe6060f1SDimitry Andric        ++callIdx) {
236fe6060f1SDimitry Andric     if (finalIdx == callIdx)
237fe6060f1SDimitry Andric       finalizeOne(inputs[finalIdx++]);
238fe6060f1SDimitry Andric     ConcatInputSection *isec = inputs[callIdx];
239fe6060f1SDimitry Andric     assert(isec->isFinal);
240fe6060f1SDimitry Andric     uint64_t isecVA = isec->getVA();
241349cc55cSDimitry Andric 
242349cc55cSDimitry Andric     // Assign addresses up-to the forward branch-range limit.
243349cc55cSDimitry Andric     // Every call instruction needs a small number of bytes (on Arm64: 4),
244349cc55cSDimitry Andric     // and each inserted thunk needs a slightly larger number of bytes
245349cc55cSDimitry Andric     // (on Arm64: 12). If a section starts with a branch instruction and
246349cc55cSDimitry Andric     // contains several branch instructions in succession, then the distance
247349cc55cSDimitry Andric     // from the current position to the position where the thunks are inserted
248349cc55cSDimitry Andric     // grows. So leave room for a bunch of thunks.
24906c3fb27SDimitry Andric     unsigned slop = 256 * thunkSize;
25006c3fb27SDimitry Andric     while (finalIdx < endIdx) {
25106c3fb27SDimitry Andric       uint64_t expectedNewSize =
25206c3fb27SDimitry Andric           alignToPowerOf2(addr + size, inputs[finalIdx]->align) +
25306c3fb27SDimitry Andric           inputs[finalIdx]->getSize();
25406c3fb27SDimitry Andric       if (expectedNewSize >= isecVA + forwardBranchRange - slop)
25506c3fb27SDimitry Andric         break;
256fe6060f1SDimitry Andric       finalizeOne(inputs[finalIdx++]);
25706c3fb27SDimitry Andric     }
258349cc55cSDimitry Andric 
2590eae32dcSDimitry Andric     if (!isec->hasCallSites)
260fe6060f1SDimitry Andric       continue;
261349cc55cSDimitry Andric 
262fe6060f1SDimitry Andric     if (finalIdx == endIdx && stubsInRangeVA == TargetInfo::outOfRangeVA) {
263fe6060f1SDimitry Andric       // When we have finalized all input sections, __stubs (destined
264fe6060f1SDimitry Andric       // to follow __text) comes within range of forward branches and
265fe6060f1SDimitry Andric       // we can estimate the threshold address after which we can
266fe6060f1SDimitry Andric       // reach any stub with a forward branch. Note that although it
267fe6060f1SDimitry Andric       // sits in the middle of a loop, this code executes only once.
268fe6060f1SDimitry Andric       // It is in the loop because we need to call it at the proper
269fe6060f1SDimitry Andric       // time: the earliest call site from which the end of __text
270fe6060f1SDimitry Andric       // (and start of __stubs) comes within range of a forward branch.
271fe6060f1SDimitry Andric       stubsInRangeVA = estimateStubsInRangeVA(callIdx);
272fe6060f1SDimitry Andric     }
273fe6060f1SDimitry Andric     // Process relocs by ascending address, i.e., ascending offset within isec
274fe6060f1SDimitry Andric     std::vector<Reloc> &relocs = isec->relocs;
275fe6060f1SDimitry Andric     // FIXME: This property does not hold for object files produced by ld64's
276fe6060f1SDimitry Andric     // `-r` mode.
277fe6060f1SDimitry Andric     assert(is_sorted(relocs,
278fe6060f1SDimitry Andric                      [](Reloc &a, Reloc &b) { return a.offset > b.offset; }));
279fe6060f1SDimitry Andric     for (Reloc &r : reverse(relocs)) {
280fe6060f1SDimitry Andric       ++relocCount;
281fe6060f1SDimitry Andric       if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
282fe6060f1SDimitry Andric         continue;
283fe6060f1SDimitry Andric       ++callSiteCount;
284fe6060f1SDimitry Andric       // Calculate branch reachability boundaries
285fe6060f1SDimitry Andric       uint64_t callVA = isecVA + r.offset;
286349cc55cSDimitry Andric       uint64_t lowVA =
287349cc55cSDimitry Andric           backwardBranchRange < callVA ? callVA - backwardBranchRange : 0;
288349cc55cSDimitry Andric       uint64_t highVA = callVA + forwardBranchRange;
289fe6060f1SDimitry Andric       // Calculate our call referent address
290fe6060f1SDimitry Andric       auto *funcSym = r.referent.get<Symbol *>();
291fe6060f1SDimitry Andric       ThunkInfo &thunkInfo = thunkMap[funcSym];
292fe6060f1SDimitry Andric       // The referent is not reachable, so we need to use a thunk ...
293fe6060f1SDimitry Andric       if (funcSym->isInStubs() && callVA >= stubsInRangeVA) {
294349cc55cSDimitry Andric         assert(callVA != TargetInfo::outOfRangeVA);
295fe6060f1SDimitry Andric         // ... Oh, wait! We are close enough to the end that __stubs
296fe6060f1SDimitry Andric         // are now within range of a simple forward branch.
297fe6060f1SDimitry Andric         continue;
298fe6060f1SDimitry Andric       }
299fe6060f1SDimitry Andric       uint64_t funcVA = funcSym->resolveBranchVA();
300fe6060f1SDimitry Andric       ++thunkInfo.callSitesUsed;
301349cc55cSDimitry Andric       if (lowVA <= funcVA && funcVA <= highVA) {
302fe6060f1SDimitry Andric         // The referent is reachable with a simple call instruction.
303fe6060f1SDimitry Andric         continue;
304fe6060f1SDimitry Andric       }
305fe6060f1SDimitry Andric       ++thunkInfo.thunkCallCount;
306fe6060f1SDimitry Andric       ++thunkCallCount;
307fe6060f1SDimitry Andric       // If an existing thunk is reachable, use it ...
308fe6060f1SDimitry Andric       if (thunkInfo.sym) {
309fe6060f1SDimitry Andric         uint64_t thunkVA = thunkInfo.isec->getVA();
310349cc55cSDimitry Andric         if (lowVA <= thunkVA && thunkVA <= highVA) {
311fe6060f1SDimitry Andric           r.referent = thunkInfo.sym;
312fe6060f1SDimitry Andric           continue;
313fe6060f1SDimitry Andric         }
314fe6060f1SDimitry Andric       }
315349cc55cSDimitry Andric       // ... otherwise, create a new thunk.
31681ad6265SDimitry Andric       if (addr + size > highVA) {
317349cc55cSDimitry Andric         // There were too many consecutive branch instructions for `slop`
318349cc55cSDimitry Andric         // above. If you hit this: For the current algorithm, just bumping up
319349cc55cSDimitry Andric         // slop above and trying again is probably simplest. (See also PR51578
320349cc55cSDimitry Andric         // comment 5).
321fe6060f1SDimitry Andric         fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
322fe6060f1SDimitry Andric       }
323fe6060f1SDimitry Andric       thunkInfo.isec =
32481ad6265SDimitry Andric           makeSyntheticInputSection(isec->getSegName(), isec->getName());
325fe6060f1SDimitry Andric       thunkInfo.isec->parent = this;
326*0fca6ea1SDimitry Andric       assert(thunkInfo.isec->live);
327349cc55cSDimitry Andric 
32804eeddc0SDimitry Andric       StringRef thunkName = saver().save(funcSym->getName() + ".thunk." +
329fe6060f1SDimitry Andric                                          std::to_string(thunkInfo.sequence++));
33081ad6265SDimitry Andric       if (!isa<Defined>(funcSym) || cast<Defined>(funcSym)->isExternal()) {
331fe6060f1SDimitry Andric         r.referent = thunkInfo.sym = symtab->addDefined(
33281ad6265SDimitry Andric             thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
33381ad6265SDimitry Andric             /*isWeakDef=*/false, /*isPrivateExtern=*/true,
33406c3fb27SDimitry Andric             /*isReferencedDynamically=*/false, /*noDeadStrip=*/false,
33506c3fb27SDimitry Andric             /*isWeakDefCanBeHidden=*/false);
33681ad6265SDimitry Andric       } else {
33781ad6265SDimitry Andric         r.referent = thunkInfo.sym = make<Defined>(
33881ad6265SDimitry Andric             thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
33981ad6265SDimitry Andric             /*isWeakDef=*/false, /*isExternal=*/false, /*isPrivateExtern=*/true,
34006c3fb27SDimitry Andric             /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
34106c3fb27SDimitry Andric             /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false);
34281ad6265SDimitry Andric       }
343349cc55cSDimitry Andric       thunkInfo.sym->used = true;
344fe6060f1SDimitry Andric       target->populateThunk(thunkInfo.isec, funcSym);
345fe6060f1SDimitry Andric       finalizeOne(thunkInfo.isec);
346fe6060f1SDimitry Andric       thunks.push_back(thunkInfo.isec);
347fe6060f1SDimitry Andric       ++thunkCount;
348fe6060f1SDimitry Andric     }
349fe6060f1SDimitry Andric   }
350fe6060f1SDimitry Andric 
351fe6060f1SDimitry Andric   log("thunks for " + parent->name + "," + name +
352fe6060f1SDimitry Andric       ": funcs = " + std::to_string(thunkMap.size()) +
353fe6060f1SDimitry Andric       ", relocs = " + std::to_string(relocCount) +
354fe6060f1SDimitry Andric       ", all calls = " + std::to_string(callSiteCount) +
355fe6060f1SDimitry Andric       ", thunk calls = " + std::to_string(thunkCallCount) +
356fe6060f1SDimitry Andric       ", thunks = " + std::to_string(thunkCount));
357fe6060f1SDimitry Andric }
358fe6060f1SDimitry Andric 
359fe6060f1SDimitry Andric void ConcatOutputSection::writeTo(uint8_t *buf) const {
36081ad6265SDimitry Andric   for (ConcatInputSection *isec : inputs)
36181ad6265SDimitry Andric     isec->writeTo(buf + isec->outSecOff);
36281ad6265SDimitry Andric }
36381ad6265SDimitry Andric 
36481ad6265SDimitry Andric void TextOutputSection::writeTo(uint8_t *buf) const {
365fe6060f1SDimitry Andric   // Merge input sections from thunk & ordinary vectors
366fe6060f1SDimitry Andric   size_t i = 0, ie = inputs.size();
367fe6060f1SDimitry Andric   size_t t = 0, te = thunks.size();
368fe6060f1SDimitry Andric   while (i < ie || t < te) {
369349cc55cSDimitry Andric     while (i < ie && (t == te || inputs[i]->empty() ||
370fe6060f1SDimitry Andric                       inputs[i]->outSecOff < thunks[t]->outSecOff)) {
371fe6060f1SDimitry Andric       inputs[i]->writeTo(buf + inputs[i]->outSecOff);
372fe6060f1SDimitry Andric       ++i;
373fe6060f1SDimitry Andric     }
374fe6060f1SDimitry Andric     while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) {
375fe6060f1SDimitry Andric       thunks[t]->writeTo(buf + thunks[t]->outSecOff);
376fe6060f1SDimitry Andric       ++t;
377fe6060f1SDimitry Andric     }
378fe6060f1SDimitry Andric   }
379fe6060f1SDimitry Andric }
380fe6060f1SDimitry Andric 
381fe6060f1SDimitry Andric void ConcatOutputSection::finalizeFlags(InputSection *input) {
382fe6060f1SDimitry Andric   switch (sectionType(input->getFlags())) {
383fe6060f1SDimitry Andric   default /*type-unspec'ed*/:
384fe6060f1SDimitry Andric     // FIXME: Add additional logic here when supporting emitting obj files.
385fe6060f1SDimitry Andric     break;
386fe6060f1SDimitry Andric   case S_4BYTE_LITERALS:
387fe6060f1SDimitry Andric   case S_8BYTE_LITERALS:
388fe6060f1SDimitry Andric   case S_16BYTE_LITERALS:
389fe6060f1SDimitry Andric   case S_CSTRING_LITERALS:
390fe6060f1SDimitry Andric   case S_ZEROFILL:
391fe6060f1SDimitry Andric   case S_LAZY_SYMBOL_POINTERS:
392fe6060f1SDimitry Andric   case S_MOD_TERM_FUNC_POINTERS:
393fe6060f1SDimitry Andric   case S_THREAD_LOCAL_REGULAR:
394fe6060f1SDimitry Andric   case S_THREAD_LOCAL_ZEROFILL:
395fe6060f1SDimitry Andric   case S_THREAD_LOCAL_VARIABLES:
396fe6060f1SDimitry Andric   case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
397fe6060f1SDimitry Andric   case S_THREAD_LOCAL_VARIABLE_POINTERS:
398fe6060f1SDimitry Andric   case S_NON_LAZY_SYMBOL_POINTERS:
399fe6060f1SDimitry Andric   case S_SYMBOL_STUBS:
400fe6060f1SDimitry Andric     flags |= input->getFlags();
401fe6060f1SDimitry Andric     break;
402fe6060f1SDimitry Andric   }
403fe6060f1SDimitry Andric }
404fe6060f1SDimitry Andric 
405fe6060f1SDimitry Andric ConcatOutputSection *
406fe6060f1SDimitry Andric ConcatOutputSection::getOrCreateForInput(const InputSection *isec) {
407fe6060f1SDimitry Andric   NamePair names = maybeRenameSection({isec->getSegName(), isec->getName()});
408fe6060f1SDimitry Andric   ConcatOutputSection *&osec = concatOutputSections[names];
40981ad6265SDimitry Andric   if (!osec) {
41081ad6265SDimitry Andric     if (isec->getSegName() == segment_names::text &&
41181ad6265SDimitry Andric         isec->getName() != section_names::gccExceptTab &&
41281ad6265SDimitry Andric         isec->getName() != section_names::ehFrame)
41381ad6265SDimitry Andric       osec = make<TextOutputSection>(names.second);
41481ad6265SDimitry Andric     else
415fe6060f1SDimitry Andric       osec = make<ConcatOutputSection>(names.second);
41681ad6265SDimitry Andric   }
417fe6060f1SDimitry Andric   return osec;
418fe6060f1SDimitry Andric }
419fe6060f1SDimitry Andric 
420fe6060f1SDimitry Andric NamePair macho::maybeRenameSection(NamePair key) {
421fe6060f1SDimitry Andric   auto newNames = config->sectionRenameMap.find(key);
422fe6060f1SDimitry Andric   if (newNames != config->sectionRenameMap.end())
423fe6060f1SDimitry Andric     return newNames->second;
424fe6060f1SDimitry Andric   return key;
425fe6060f1SDimitry Andric }
426