xref: /openbsd-src/gnu/llvm/lld/MachO/ConcatOutputSection.cpp (revision dfe94b169149f14cc1aee2cf6dad58a8d9a1860c)
11cf9926bSpatrick //===- ConcatOutputSection.cpp --------------------------------------------===//
21cf9926bSpatrick //
31cf9926bSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41cf9926bSpatrick // See https://llvm.org/LICENSE.txt for license information.
51cf9926bSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61cf9926bSpatrick //
71cf9926bSpatrick //===----------------------------------------------------------------------===//
81cf9926bSpatrick 
91cf9926bSpatrick #include "ConcatOutputSection.h"
101cf9926bSpatrick #include "Config.h"
111cf9926bSpatrick #include "OutputSegment.h"
121cf9926bSpatrick #include "SymbolTable.h"
131cf9926bSpatrick #include "Symbols.h"
141cf9926bSpatrick #include "SyntheticSections.h"
151cf9926bSpatrick #include "Target.h"
16*dfe94b16Srobert #include "lld/Common/CommonLinkerContext.h"
171cf9926bSpatrick #include "llvm/BinaryFormat/MachO.h"
181cf9926bSpatrick #include "llvm/Support/ScopedPrinter.h"
191cf9926bSpatrick #include "llvm/Support/TimeProfiler.h"
201cf9926bSpatrick 
211cf9926bSpatrick using namespace llvm;
221cf9926bSpatrick using namespace llvm::MachO;
231cf9926bSpatrick using namespace lld;
241cf9926bSpatrick using namespace lld::macho;
251cf9926bSpatrick 
261cf9926bSpatrick MapVector<NamePair, ConcatOutputSection *> macho::concatOutputSections;
271cf9926bSpatrick 
addInput(ConcatInputSection * input)281cf9926bSpatrick void ConcatOutputSection::addInput(ConcatInputSection *input) {
291cf9926bSpatrick   assert(input->parent == this);
301cf9926bSpatrick   if (inputs.empty()) {
311cf9926bSpatrick     align = input->align;
321cf9926bSpatrick     flags = input->getFlags();
331cf9926bSpatrick   } else {
341cf9926bSpatrick     align = std::max(align, input->align);
351cf9926bSpatrick     finalizeFlags(input);
361cf9926bSpatrick   }
371cf9926bSpatrick   inputs.push_back(input);
381cf9926bSpatrick }
391cf9926bSpatrick 
401cf9926bSpatrick // Branch-range extension can be implemented in two ways, either through ...
411cf9926bSpatrick //
421cf9926bSpatrick // (1) Branch islands: Single branch instructions (also of limited range),
431cf9926bSpatrick //     that might be chained in multiple hops to reach the desired
441cf9926bSpatrick //     destination. On ARM64, as 16 branch islands are needed to hop between
451cf9926bSpatrick //     opposite ends of a 2 GiB program. LD64 uses branch islands exclusively,
461cf9926bSpatrick //     even when it needs excessive hops.
471cf9926bSpatrick //
481cf9926bSpatrick // (2) Thunks: Instruction(s) to load the destination address into a scratch
491cf9926bSpatrick //     register, followed by a register-indirect branch. Thunks are
501cf9926bSpatrick //     constructed to reach any arbitrary address, so need not be
511cf9926bSpatrick //     chained. Although thunks need not be chained, a program might need
521cf9926bSpatrick //     multiple thunks to the same destination distributed throughout a large
531cf9926bSpatrick //     program so that all call sites can have one within range.
541cf9926bSpatrick //
55*dfe94b16Srobert // The optimal approach is to mix islands for destinations within two hops,
561cf9926bSpatrick // and use thunks for destinations at greater distance. For now, we only
571cf9926bSpatrick // implement thunks. TODO: Adding support for branch islands!
581cf9926bSpatrick //
591cf9926bSpatrick // Internally -- as expressed in LLD's data structures -- a
60*dfe94b16Srobert // branch-range-extension thunk consists of:
611cf9926bSpatrick //
62*dfe94b16Srobert // (1) new Defined symbol for the thunk named
631cf9926bSpatrick //     <FUNCTION>.thunk.<SEQUENCE>, which references ...
641cf9926bSpatrick // (2) new InputSection, which contains ...
651cf9926bSpatrick // (3.1) new data for the instructions to load & branch to the far address +
661cf9926bSpatrick // (3.2) new Relocs on instructions to load the far address, which reference ...
67*dfe94b16Srobert // (4.1) existing Defined symbol for the real function in __text, or
681cf9926bSpatrick // (4.2) existing DylibSymbol for the real function in a dylib
691cf9926bSpatrick //
701cf9926bSpatrick // Nearly-optimal thunk-placement algorithm features:
711cf9926bSpatrick //
721cf9926bSpatrick // * Single pass: O(n) on the number of call sites.
731cf9926bSpatrick //
741cf9926bSpatrick // * Accounts for the exact space overhead of thunks - no heuristics
751cf9926bSpatrick //
761cf9926bSpatrick // * Exploits the full range of call instructions - forward & backward
771cf9926bSpatrick //
781cf9926bSpatrick // Data:
791cf9926bSpatrick //
801cf9926bSpatrick // * DenseMap<Symbol *, ThunkInfo> thunkMap: Maps the function symbol
811cf9926bSpatrick //   to its thunk bookkeeper.
821cf9926bSpatrick //
831cf9926bSpatrick // * struct ThunkInfo (bookkeeper): Call instructions have limited range, and
841cf9926bSpatrick //   distant call sites might be unable to reach the same thunk, so multiple
851cf9926bSpatrick //   thunks are necessary to serve all call sites in a very large program. A
861cf9926bSpatrick //   thunkInfo stores state for all thunks associated with a particular
87*dfe94b16Srobert //   function:
88*dfe94b16Srobert //     (a) thunk symbol
89*dfe94b16Srobert //     (b) input section containing stub code, and
90*dfe94b16Srobert //     (c) sequence number for the active thunk incarnation.
91*dfe94b16Srobert //   When an old thunk goes out of range, we increment the sequence number and
92*dfe94b16Srobert //   create a new thunk named <FUNCTION>.thunk.<SEQUENCE>.
931cf9926bSpatrick //
94*dfe94b16Srobert // * A thunk consists of
95*dfe94b16Srobert //     (a) a Defined symbol pointing to
96*dfe94b16Srobert //     (b) an InputSection holding machine code (similar to a MachO stub), and
97*dfe94b16Srobert //     (c) relocs referencing the real function for fixing up the stub code.
981cf9926bSpatrick //
991cf9926bSpatrick // * std::vector<InputSection *> MergedInputSection::thunks: A vector parallel
1001cf9926bSpatrick //   to the inputs vector. We store new thunks via cheap vector append, rather
1011cf9926bSpatrick //   than costly insertion into the inputs vector.
1021cf9926bSpatrick //
1031cf9926bSpatrick // Control Flow:
1041cf9926bSpatrick //
1051cf9926bSpatrick // * During address assignment, MergedInputSection::finalize() examines call
1061cf9926bSpatrick //   sites by ascending address and creates thunks.  When a function is beyond
1071cf9926bSpatrick //   the range of a call site, we need a thunk. Place it at the largest
1081cf9926bSpatrick //   available forward address from the call site. Call sites increase
1091cf9926bSpatrick //   monotonically and thunks are always placed as far forward as possible;
1101cf9926bSpatrick //   thus, we place thunks at monotonically increasing addresses. Once a thunk
1111cf9926bSpatrick //   is placed, it and all previous input-section addresses are final.
1121cf9926bSpatrick //
113*dfe94b16Srobert // * ConcatInputSection::finalize() and ConcatInputSection::writeTo() merge
1141cf9926bSpatrick //   the inputs and thunks vectors (both ordered by ascending address), which
1151cf9926bSpatrick //   is simple and cheap.
1161cf9926bSpatrick 
1171cf9926bSpatrick DenseMap<Symbol *, ThunkInfo> lld::macho::thunkMap;
1181cf9926bSpatrick 
1191cf9926bSpatrick // Determine whether we need thunks, which depends on the target arch -- RISC
1201cf9926bSpatrick // (i.e., ARM) generally does because it has limited-range branch/call
1211cf9926bSpatrick // instructions, whereas CISC (i.e., x86) generally doesn't. RISC only needs
1221cf9926bSpatrick // thunks for programs so large that branch source & destination addresses
1231cf9926bSpatrick // might differ more than the range of branch instruction(s).
needsThunks() const124*dfe94b16Srobert bool TextOutputSection::needsThunks() const {
1251cf9926bSpatrick   if (!target->usesThunks())
1261cf9926bSpatrick     return false;
1271cf9926bSpatrick   uint64_t isecAddr = addr;
128*dfe94b16Srobert   for (ConcatInputSection *isec : inputs)
1291cf9926bSpatrick     isecAddr = alignTo(isecAddr, isec->align) + isec->getSize();
130*dfe94b16Srobert   if (isecAddr - addr + in.stubs->getSize() <=
131*dfe94b16Srobert       std::min(target->backwardBranchRange, target->forwardBranchRange))
1321cf9926bSpatrick     return false;
1331cf9926bSpatrick   // Yes, this program is large enough to need thunks.
134*dfe94b16Srobert   for (ConcatInputSection *isec : inputs) {
1351cf9926bSpatrick     for (Reloc &r : isec->relocs) {
1361cf9926bSpatrick       if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
1371cf9926bSpatrick         continue;
1381cf9926bSpatrick       auto *sym = r.referent.get<Symbol *>();
1391cf9926bSpatrick       // Pre-populate the thunkMap and memoize call site counts for every
1401cf9926bSpatrick       // InputSection and ThunkInfo. We do this for the benefit of
141*dfe94b16Srobert       // estimateStubsInRangeVA().
1421cf9926bSpatrick       ThunkInfo &thunkInfo = thunkMap[sym];
1431cf9926bSpatrick       // Knowing ThunkInfo call site count will help us know whether or not we
1441cf9926bSpatrick       // might need to create more for this referent at the time we are
145*dfe94b16Srobert       // estimating distance to __stubs in estimateStubsInRangeVA().
1461cf9926bSpatrick       ++thunkInfo.callSiteCount;
147*dfe94b16Srobert       // We can avoid work on InputSections that have no BRANCH relocs.
148*dfe94b16Srobert       isec->hasCallSites = true;
1491cf9926bSpatrick     }
1501cf9926bSpatrick   }
1511cf9926bSpatrick   return true;
1521cf9926bSpatrick }
1531cf9926bSpatrick 
1541cf9926bSpatrick // Since __stubs is placed after __text, we must estimate the address
1551cf9926bSpatrick // beyond which stubs are within range of a simple forward branch.
156*dfe94b16Srobert // This is called exactly once, when the last input section has been finalized.
estimateStubsInRangeVA(size_t callIdx) const157*dfe94b16Srobert uint64_t TextOutputSection::estimateStubsInRangeVA(size_t callIdx) const {
158*dfe94b16Srobert   // Tally the functions which still have call sites remaining to process,
159*dfe94b16Srobert   // which yields the maximum number of thunks we might yet place.
1601cf9926bSpatrick   size_t maxPotentialThunks = 0;
1611cf9926bSpatrick   for (auto &tp : thunkMap) {
1621cf9926bSpatrick     ThunkInfo &ti = tp.second;
163*dfe94b16Srobert     // This overcounts: Only sections that are in forward jump range from the
164*dfe94b16Srobert     // currently-active section get finalized, and all input sections are
165*dfe94b16Srobert     // finalized when estimateStubsInRangeVA() is called. So only backward
166*dfe94b16Srobert     // jumps will need thunks, but we count all jumps.
167*dfe94b16Srobert     if (ti.callSitesUsed < ti.callSiteCount)
168*dfe94b16Srobert       maxPotentialThunks += 1;
1691cf9926bSpatrick   }
1701cf9926bSpatrick   // Tally the total size of input sections remaining to process.
171*dfe94b16Srobert   uint64_t isecVA = inputs[callIdx]->getVA();
172*dfe94b16Srobert   uint64_t isecEnd = isecVA;
173*dfe94b16Srobert   for (size_t i = callIdx; i < inputs.size(); i++) {
1741cf9926bSpatrick     InputSection *isec = inputs[i];
1751cf9926bSpatrick     isecEnd = alignTo(isecEnd, isec->align) + isec->getSize();
1761cf9926bSpatrick   }
1771cf9926bSpatrick   // Estimate the address after which call sites can safely call stubs
1781cf9926bSpatrick   // directly rather than through intermediary thunks.
179*dfe94b16Srobert   uint64_t forwardBranchRange = target->forwardBranchRange;
180*dfe94b16Srobert   assert(isecEnd > forwardBranchRange &&
181*dfe94b16Srobert          "should not run thunk insertion if all code fits in jump range");
182*dfe94b16Srobert   assert(isecEnd - isecVA <= forwardBranchRange &&
183*dfe94b16Srobert          "should only finalize sections in jump range");
1841cf9926bSpatrick   uint64_t stubsInRangeVA = isecEnd + maxPotentialThunks * target->thunkSize +
185*dfe94b16Srobert                             in.stubs->getSize() - forwardBranchRange;
1861cf9926bSpatrick   log("thunks = " + std::to_string(thunkMap.size()) +
1871cf9926bSpatrick       ", potential = " + std::to_string(maxPotentialThunks) +
1881cf9926bSpatrick       ", stubs = " + std::to_string(in.stubs->getSize()) + ", isecVA = " +
189*dfe94b16Srobert       utohexstr(isecVA) + ", threshold = " + utohexstr(stubsInRangeVA) +
190*dfe94b16Srobert       ", isecEnd = " + utohexstr(isecEnd) +
191*dfe94b16Srobert       ", tail = " + utohexstr(isecEnd - isecVA) +
192*dfe94b16Srobert       ", slop = " + utohexstr(forwardBranchRange - (isecEnd - isecVA)));
1931cf9926bSpatrick   return stubsInRangeVA;
1941cf9926bSpatrick }
1951cf9926bSpatrick 
finalizeOne(ConcatInputSection * isec)196*dfe94b16Srobert void ConcatOutputSection::finalizeOne(ConcatInputSection *isec) {
197*dfe94b16Srobert   size = alignTo(size, isec->align);
198*dfe94b16Srobert   fileSize = alignTo(fileSize, isec->align);
199*dfe94b16Srobert   isec->outSecOff = size;
2001cf9926bSpatrick   isec->isFinal = true;
201*dfe94b16Srobert   size += isec->getSize();
202*dfe94b16Srobert   fileSize += isec->getFileSize();
203*dfe94b16Srobert }
2041cf9926bSpatrick 
finalizeContents()205*dfe94b16Srobert void ConcatOutputSection::finalizeContents() {
206*dfe94b16Srobert   for (ConcatInputSection *isec : inputs)
207*dfe94b16Srobert     finalizeOne(isec);
208*dfe94b16Srobert }
209*dfe94b16Srobert 
finalize()210*dfe94b16Srobert void TextOutputSection::finalize() {
2111cf9926bSpatrick   if (!needsThunks()) {
2121cf9926bSpatrick     for (ConcatInputSection *isec : inputs)
2131cf9926bSpatrick       finalizeOne(isec);
2141cf9926bSpatrick     return;
2151cf9926bSpatrick   }
2161cf9926bSpatrick 
217*dfe94b16Srobert   uint64_t forwardBranchRange = target->forwardBranchRange;
218*dfe94b16Srobert   uint64_t backwardBranchRange = target->backwardBranchRange;
2191cf9926bSpatrick   uint64_t stubsInRangeVA = TargetInfo::outOfRangeVA;
2201cf9926bSpatrick   size_t thunkSize = target->thunkSize;
2211cf9926bSpatrick   size_t relocCount = 0;
2221cf9926bSpatrick   size_t callSiteCount = 0;
2231cf9926bSpatrick   size_t thunkCallCount = 0;
2241cf9926bSpatrick   size_t thunkCount = 0;
2251cf9926bSpatrick 
226*dfe94b16Srobert   // Walk all sections in order. Finalize all sections that are less than
227*dfe94b16Srobert   // forwardBranchRange in front of it.
228*dfe94b16Srobert   // isecVA is the address of the current section.
229*dfe94b16Srobert   // addr + size is the start address of the first non-finalized section.
230*dfe94b16Srobert 
2311cf9926bSpatrick   // inputs[finalIdx] is for finalization (address-assignment)
2321cf9926bSpatrick   size_t finalIdx = 0;
2331cf9926bSpatrick   // Kick-off by ensuring that the first input section has an address
2341cf9926bSpatrick   for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx;
2351cf9926bSpatrick        ++callIdx) {
2361cf9926bSpatrick     if (finalIdx == callIdx)
2371cf9926bSpatrick       finalizeOne(inputs[finalIdx++]);
2381cf9926bSpatrick     ConcatInputSection *isec = inputs[callIdx];
2391cf9926bSpatrick     assert(isec->isFinal);
2401cf9926bSpatrick     uint64_t isecVA = isec->getVA();
241*dfe94b16Srobert 
242*dfe94b16Srobert     // Assign addresses up-to the forward branch-range limit.
243*dfe94b16Srobert     // Every call instruction needs a small number of bytes (on Arm64: 4),
244*dfe94b16Srobert     // and each inserted thunk needs a slightly larger number of bytes
245*dfe94b16Srobert     // (on Arm64: 12). If a section starts with a branch instruction and
246*dfe94b16Srobert     // contains several branch instructions in succession, then the distance
247*dfe94b16Srobert     // from the current position to the position where the thunks are inserted
248*dfe94b16Srobert     // grows. So leave room for a bunch of thunks.
249*dfe94b16Srobert     unsigned slop = 1024 * thunkSize;
250*dfe94b16Srobert     while (finalIdx < endIdx && addr + size + inputs[finalIdx]->getSize() <
251*dfe94b16Srobert                                     isecVA + forwardBranchRange - slop)
2521cf9926bSpatrick       finalizeOne(inputs[finalIdx++]);
253*dfe94b16Srobert 
254*dfe94b16Srobert     if (!isec->hasCallSites)
2551cf9926bSpatrick       continue;
256*dfe94b16Srobert 
2571cf9926bSpatrick     if (finalIdx == endIdx && stubsInRangeVA == TargetInfo::outOfRangeVA) {
2581cf9926bSpatrick       // When we have finalized all input sections, __stubs (destined
2591cf9926bSpatrick       // to follow __text) comes within range of forward branches and
2601cf9926bSpatrick       // we can estimate the threshold address after which we can
2611cf9926bSpatrick       // reach any stub with a forward branch. Note that although it
2621cf9926bSpatrick       // sits in the middle of a loop, this code executes only once.
2631cf9926bSpatrick       // It is in the loop because we need to call it at the proper
2641cf9926bSpatrick       // time: the earliest call site from which the end of __text
2651cf9926bSpatrick       // (and start of __stubs) comes within range of a forward branch.
2661cf9926bSpatrick       stubsInRangeVA = estimateStubsInRangeVA(callIdx);
2671cf9926bSpatrick     }
2681cf9926bSpatrick     // Process relocs by ascending address, i.e., ascending offset within isec
2691cf9926bSpatrick     std::vector<Reloc> &relocs = isec->relocs;
2701cf9926bSpatrick     // FIXME: This property does not hold for object files produced by ld64's
2711cf9926bSpatrick     // `-r` mode.
2721cf9926bSpatrick     assert(is_sorted(relocs,
2731cf9926bSpatrick                      [](Reloc &a, Reloc &b) { return a.offset > b.offset; }));
2741cf9926bSpatrick     for (Reloc &r : reverse(relocs)) {
2751cf9926bSpatrick       ++relocCount;
2761cf9926bSpatrick       if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
2771cf9926bSpatrick         continue;
2781cf9926bSpatrick       ++callSiteCount;
2791cf9926bSpatrick       // Calculate branch reachability boundaries
2801cf9926bSpatrick       uint64_t callVA = isecVA + r.offset;
281*dfe94b16Srobert       uint64_t lowVA =
282*dfe94b16Srobert           backwardBranchRange < callVA ? callVA - backwardBranchRange : 0;
283*dfe94b16Srobert       uint64_t highVA = callVA + forwardBranchRange;
2841cf9926bSpatrick       // Calculate our call referent address
2851cf9926bSpatrick       auto *funcSym = r.referent.get<Symbol *>();
2861cf9926bSpatrick       ThunkInfo &thunkInfo = thunkMap[funcSym];
2871cf9926bSpatrick       // The referent is not reachable, so we need to use a thunk ...
2881cf9926bSpatrick       if (funcSym->isInStubs() && callVA >= stubsInRangeVA) {
289*dfe94b16Srobert         assert(callVA != TargetInfo::outOfRangeVA);
2901cf9926bSpatrick         // ... Oh, wait! We are close enough to the end that __stubs
2911cf9926bSpatrick         // are now within range of a simple forward branch.
2921cf9926bSpatrick         continue;
2931cf9926bSpatrick       }
2941cf9926bSpatrick       uint64_t funcVA = funcSym->resolveBranchVA();
2951cf9926bSpatrick       ++thunkInfo.callSitesUsed;
296*dfe94b16Srobert       if (lowVA <= funcVA && funcVA <= highVA) {
2971cf9926bSpatrick         // The referent is reachable with a simple call instruction.
2981cf9926bSpatrick         continue;
2991cf9926bSpatrick       }
3001cf9926bSpatrick       ++thunkInfo.thunkCallCount;
3011cf9926bSpatrick       ++thunkCallCount;
3021cf9926bSpatrick       // If an existing thunk is reachable, use it ...
3031cf9926bSpatrick       if (thunkInfo.sym) {
3041cf9926bSpatrick         uint64_t thunkVA = thunkInfo.isec->getVA();
305*dfe94b16Srobert         if (lowVA <= thunkVA && thunkVA <= highVA) {
3061cf9926bSpatrick           r.referent = thunkInfo.sym;
3071cf9926bSpatrick           continue;
3081cf9926bSpatrick         }
3091cf9926bSpatrick       }
310*dfe94b16Srobert       // ... otherwise, create a new thunk.
311*dfe94b16Srobert       if (addr + size > highVA) {
312*dfe94b16Srobert         // There were too many consecutive branch instructions for `slop`
313*dfe94b16Srobert         // above. If you hit this: For the current algorithm, just bumping up
314*dfe94b16Srobert         // slop above and trying again is probably simplest. (See also PR51578
315*dfe94b16Srobert         // comment 5).
3161cf9926bSpatrick         fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
3171cf9926bSpatrick       }
3181cf9926bSpatrick       thunkInfo.isec =
319*dfe94b16Srobert           makeSyntheticInputSection(isec->getSegName(), isec->getName());
3201cf9926bSpatrick       thunkInfo.isec->parent = this;
321*dfe94b16Srobert 
322*dfe94b16Srobert       // This code runs after dead code removal. Need to set the `live` bit
323*dfe94b16Srobert       // on the thunk isec so that asserts that check that only live sections
324*dfe94b16Srobert       // get written are happy.
325*dfe94b16Srobert       thunkInfo.isec->live = true;
326*dfe94b16Srobert 
327*dfe94b16Srobert       StringRef thunkName = saver().save(funcSym->getName() + ".thunk." +
3281cf9926bSpatrick                                          std::to_string(thunkInfo.sequence++));
329*dfe94b16Srobert       if (!isa<Defined>(funcSym) || cast<Defined>(funcSym)->isExternal()) {
3301cf9926bSpatrick         r.referent = thunkInfo.sym = symtab->addDefined(
331*dfe94b16Srobert             thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
332*dfe94b16Srobert             /*isWeakDef=*/false, /*isPrivateExtern=*/true,
3331cf9926bSpatrick             /*isThumb=*/false, /*isReferencedDynamically=*/false,
334*dfe94b16Srobert             /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false);
335*dfe94b16Srobert       } else {
336*dfe94b16Srobert         r.referent = thunkInfo.sym = make<Defined>(
337*dfe94b16Srobert             thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
338*dfe94b16Srobert             /*isWeakDef=*/false, /*isExternal=*/false, /*isPrivateExtern=*/true,
339*dfe94b16Srobert             /*includeInSymtab=*/true, /*isThumb=*/false,
340*dfe94b16Srobert             /*isReferencedDynamically=*/false, /*noDeadStrip=*/false,
341*dfe94b16Srobert             /*isWeakDefCanBeHidden=*/false);
342*dfe94b16Srobert       }
343*dfe94b16Srobert       thunkInfo.sym->used = true;
3441cf9926bSpatrick       target->populateThunk(thunkInfo.isec, funcSym);
3451cf9926bSpatrick       finalizeOne(thunkInfo.isec);
3461cf9926bSpatrick       thunks.push_back(thunkInfo.isec);
3471cf9926bSpatrick       ++thunkCount;
3481cf9926bSpatrick     }
3491cf9926bSpatrick   }
3501cf9926bSpatrick 
3511cf9926bSpatrick   log("thunks for " + parent->name + "," + name +
3521cf9926bSpatrick       ": funcs = " + std::to_string(thunkMap.size()) +
3531cf9926bSpatrick       ", relocs = " + std::to_string(relocCount) +
3541cf9926bSpatrick       ", all calls = " + std::to_string(callSiteCount) +
3551cf9926bSpatrick       ", thunk calls = " + std::to_string(thunkCallCount) +
3561cf9926bSpatrick       ", thunks = " + std::to_string(thunkCount));
3571cf9926bSpatrick }
3581cf9926bSpatrick 
writeTo(uint8_t * buf) const3591cf9926bSpatrick void ConcatOutputSection::writeTo(uint8_t *buf) const {
360*dfe94b16Srobert   for (ConcatInputSection *isec : inputs)
361*dfe94b16Srobert     isec->writeTo(buf + isec->outSecOff);
362*dfe94b16Srobert }
363*dfe94b16Srobert 
writeTo(uint8_t * buf) const364*dfe94b16Srobert void TextOutputSection::writeTo(uint8_t *buf) const {
3651cf9926bSpatrick   // Merge input sections from thunk & ordinary vectors
3661cf9926bSpatrick   size_t i = 0, ie = inputs.size();
3671cf9926bSpatrick   size_t t = 0, te = thunks.size();
3681cf9926bSpatrick   while (i < ie || t < te) {
369*dfe94b16Srobert     while (i < ie && (t == te || inputs[i]->empty() ||
3701cf9926bSpatrick                       inputs[i]->outSecOff < thunks[t]->outSecOff)) {
3711cf9926bSpatrick       inputs[i]->writeTo(buf + inputs[i]->outSecOff);
3721cf9926bSpatrick       ++i;
3731cf9926bSpatrick     }
3741cf9926bSpatrick     while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) {
3751cf9926bSpatrick       thunks[t]->writeTo(buf + thunks[t]->outSecOff);
3761cf9926bSpatrick       ++t;
3771cf9926bSpatrick     }
3781cf9926bSpatrick   }
3791cf9926bSpatrick }
3801cf9926bSpatrick 
finalizeFlags(InputSection * input)3811cf9926bSpatrick void ConcatOutputSection::finalizeFlags(InputSection *input) {
3821cf9926bSpatrick   switch (sectionType(input->getFlags())) {
3831cf9926bSpatrick   default /*type-unspec'ed*/:
3841cf9926bSpatrick     // FIXME: Add additional logic here when supporting emitting obj files.
3851cf9926bSpatrick     break;
3861cf9926bSpatrick   case S_4BYTE_LITERALS:
3871cf9926bSpatrick   case S_8BYTE_LITERALS:
3881cf9926bSpatrick   case S_16BYTE_LITERALS:
3891cf9926bSpatrick   case S_CSTRING_LITERALS:
3901cf9926bSpatrick   case S_ZEROFILL:
3911cf9926bSpatrick   case S_LAZY_SYMBOL_POINTERS:
3921cf9926bSpatrick   case S_MOD_TERM_FUNC_POINTERS:
3931cf9926bSpatrick   case S_THREAD_LOCAL_REGULAR:
3941cf9926bSpatrick   case S_THREAD_LOCAL_ZEROFILL:
3951cf9926bSpatrick   case S_THREAD_LOCAL_VARIABLES:
3961cf9926bSpatrick   case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
3971cf9926bSpatrick   case S_THREAD_LOCAL_VARIABLE_POINTERS:
3981cf9926bSpatrick   case S_NON_LAZY_SYMBOL_POINTERS:
3991cf9926bSpatrick   case S_SYMBOL_STUBS:
4001cf9926bSpatrick     flags |= input->getFlags();
4011cf9926bSpatrick     break;
4021cf9926bSpatrick   }
4031cf9926bSpatrick }
4041cf9926bSpatrick 
4051cf9926bSpatrick ConcatOutputSection *
getOrCreateForInput(const InputSection * isec)4061cf9926bSpatrick ConcatOutputSection::getOrCreateForInput(const InputSection *isec) {
4071cf9926bSpatrick   NamePair names = maybeRenameSection({isec->getSegName(), isec->getName()});
4081cf9926bSpatrick   ConcatOutputSection *&osec = concatOutputSections[names];
409*dfe94b16Srobert   if (!osec) {
410*dfe94b16Srobert     if (isec->getSegName() == segment_names::text &&
411*dfe94b16Srobert         isec->getName() != section_names::gccExceptTab &&
412*dfe94b16Srobert         isec->getName() != section_names::ehFrame)
413*dfe94b16Srobert       osec = make<TextOutputSection>(names.second);
414*dfe94b16Srobert     else
4151cf9926bSpatrick       osec = make<ConcatOutputSection>(names.second);
416*dfe94b16Srobert   }
4171cf9926bSpatrick   return osec;
4181cf9926bSpatrick }
4191cf9926bSpatrick 
maybeRenameSection(NamePair key)4201cf9926bSpatrick NamePair macho::maybeRenameSection(NamePair key) {
4211cf9926bSpatrick   auto newNames = config->sectionRenameMap.find(key);
4221cf9926bSpatrick   if (newNames != config->sectionRenameMap.end())
4231cf9926bSpatrick     return newNames->second;
4241cf9926bSpatrick   return key;
4251cf9926bSpatrick }
426