133706191SJez Ng //===- ConcatOutputSection.cpp --------------------------------------------===//
233706191SJez Ng //
333706191SJez Ng // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
433706191SJez Ng // See https://llvm.org/LICENSE.txt for license information.
533706191SJez Ng // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
633706191SJez Ng //
733706191SJez Ng //===----------------------------------------------------------------------===//
833706191SJez Ng
933706191SJez Ng #include "ConcatOutputSection.h"
1033706191SJez Ng #include "Config.h"
1133706191SJez Ng #include "OutputSegment.h"
1233706191SJez Ng #include "SymbolTable.h"
1333706191SJez Ng #include "Symbols.h"
1433706191SJez Ng #include "SyntheticSections.h"
1533706191SJez Ng #include "Target.h"
1683d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h"
1733706191SJez Ng #include "llvm/BinaryFormat/MachO.h"
1833706191SJez Ng #include "llvm/Support/ScopedPrinter.h"
19f27e4548SGreg McGary #include "llvm/Support/TimeProfiler.h"
2033706191SJez Ng
2133706191SJez Ng using namespace llvm;
2233706191SJez Ng using namespace llvm::MachO;
2333706191SJez Ng using namespace lld;
2433706191SJez Ng using namespace lld::macho;
2533706191SJez Ng
26428a7c1bSJez Ng MapVector<NamePair, ConcatOutputSection *> macho::concatOutputSections;
27428a7c1bSJez Ng
addInput(ConcatInputSection * input)2804259cdeSJez Ng void ConcatOutputSection::addInput(ConcatInputSection *input) {
29428a7c1bSJez Ng assert(input->parent == this);
3033706191SJez Ng if (inputs.empty()) {
3133706191SJez Ng align = input->align;
32f6b6e721SJez Ng flags = input->getFlags();
3333706191SJez Ng } else {
3433706191SJez Ng align = std::max(align, input->align);
35366df11aSVy Nguyen finalizeFlags(input);
3633706191SJez Ng }
3733706191SJez Ng inputs.push_back(input);
3833706191SJez Ng }
3933706191SJez Ng
4033706191SJez Ng // Branch-range extension can be implemented in two ways, either through ...
4133706191SJez Ng //
4233706191SJez Ng // (1) Branch islands: Single branch instructions (also of limited range),
4333706191SJez Ng // that might be chained in multiple hops to reach the desired
4433706191SJez Ng // destination. On ARM64, as 16 branch islands are needed to hop between
4533706191SJez Ng // opposite ends of a 2 GiB program. LD64 uses branch islands exclusively,
4633706191SJez Ng // even when it needs excessive hops.
4733706191SJez Ng //
4833706191SJez Ng // (2) Thunks: Instruction(s) to load the destination address into a scratch
4933706191SJez Ng // register, followed by a register-indirect branch. Thunks are
5033706191SJez Ng // constructed to reach any arbitrary address, so need not be
5133706191SJez Ng // chained. Although thunks need not be chained, a program might need
5233706191SJez Ng // multiple thunks to the same destination distributed throughout a large
5333706191SJez Ng // program so that all call sites can have one within range.
5433706191SJez Ng //
5583df9406SNico Weber // The optimal approach is to mix islands for destinations within two hops,
5633706191SJez Ng // and use thunks for destinations at greater distance. For now, we only
5733706191SJez Ng // implement thunks. TODO: Adding support for branch islands!
5833706191SJez Ng //
5933706191SJez Ng // Internally -- as expressed in LLD's data structures -- a
6010cda6e3SNico Weber // branch-range-extension thunk consists of:
6133706191SJez Ng //
6210cda6e3SNico Weber // (1) new Defined symbol for the thunk named
6333706191SJez Ng // <FUNCTION>.thunk.<SEQUENCE>, which references ...
6433706191SJez Ng // (2) new InputSection, which contains ...
6533706191SJez Ng // (3.1) new data for the instructions to load & branch to the far address +
6633706191SJez Ng // (3.2) new Relocs on instructions to load the far address, which reference ...
6710cda6e3SNico Weber // (4.1) existing Defined symbol for the real function in __text, or
6833706191SJez Ng // (4.2) existing DylibSymbol for the real function in a dylib
6933706191SJez Ng //
7033706191SJez Ng // Nearly-optimal thunk-placement algorithm features:
7133706191SJez Ng //
7233706191SJez Ng // * Single pass: O(n) on the number of call sites.
7333706191SJez Ng //
7433706191SJez Ng // * Accounts for the exact space overhead of thunks - no heuristics
7533706191SJez Ng //
7633706191SJez Ng // * Exploits the full range of call instructions - forward & backward
7733706191SJez Ng //
7833706191SJez Ng // Data:
7933706191SJez Ng //
8033706191SJez Ng // * DenseMap<Symbol *, ThunkInfo> thunkMap: Maps the function symbol
8133706191SJez Ng // to its thunk bookkeeper.
8233706191SJez Ng //
8333706191SJez Ng // * struct ThunkInfo (bookkeeper): Call instructions have limited range, and
8433706191SJez Ng // distant call sites might be unable to reach the same thunk, so multiple
8533706191SJez Ng // thunks are necessary to serve all call sites in a very large program. A
8633706191SJez Ng // thunkInfo stores state for all thunks associated with a particular
87663a7fa7SNico Weber // function:
88663a7fa7SNico Weber // (a) thunk symbol
89663a7fa7SNico Weber // (b) input section containing stub code, and
90663a7fa7SNico Weber // (c) sequence number for the active thunk incarnation.
91663a7fa7SNico Weber // When an old thunk goes out of range, we increment the sequence number and
92663a7fa7SNico Weber // create a new thunk named <FUNCTION>.thunk.<SEQUENCE>.
9333706191SJez Ng //
94663a7fa7SNico Weber // * A thunk consists of
95663a7fa7SNico Weber // (a) a Defined symbol pointing to
96663a7fa7SNico Weber // (b) an InputSection holding machine code (similar to a MachO stub), and
97663a7fa7SNico Weber // (c) relocs referencing the real function for fixing up the stub code.
9833706191SJez Ng //
9933706191SJez Ng // * std::vector<InputSection *> MergedInputSection::thunks: A vector parallel
10033706191SJez Ng // to the inputs vector. We store new thunks via cheap vector append, rather
10133706191SJez Ng // than costly insertion into the inputs vector.
10233706191SJez Ng //
10333706191SJez Ng // Control Flow:
10433706191SJez Ng //
10533706191SJez Ng // * During address assignment, MergedInputSection::finalize() examines call
10633706191SJez Ng // sites by ascending address and creates thunks. When a function is beyond
10733706191SJez Ng // the range of a call site, we need a thunk. Place it at the largest
10833706191SJez Ng // available forward address from the call site. Call sites increase
10933706191SJez Ng // monotonically and thunks are always placed as far forward as possible;
11033706191SJez Ng // thus, we place thunks at monotonically increasing addresses. Once a thunk
11133706191SJez Ng // is placed, it and all previous input-section addresses are final.
11233706191SJez Ng //
11328be02f3SNico Weber // * ConcatInputSection::finalize() and ConcatInputSection::writeTo() merge
11433706191SJez Ng // the inputs and thunks vectors (both ordered by ascending address), which
11533706191SJez Ng // is simple and cheap.
11633706191SJez Ng
11733706191SJez Ng DenseMap<Symbol *, ThunkInfo> lld::macho::thunkMap;
11833706191SJez Ng
11933706191SJez Ng // Determine whether we need thunks, which depends on the target arch -- RISC
12033706191SJez Ng // (i.e., ARM) generally does because it has limited-range branch/call
12133706191SJez Ng // instructions, whereas CISC (i.e., x86) generally doesn't. RISC only needs
12233706191SJez Ng // thunks for programs so large that branch source & destination addresses
12333706191SJez Ng // might differ more than the range of branch instruction(s).
needsThunks() const124b440c257SJez Ng bool TextOutputSection::needsThunks() const {
12533706191SJez Ng if (!target->usesThunks())
12633706191SJez Ng return false;
12733706191SJez Ng uint64_t isecAddr = addr;
12840bcbe48SJez Ng for (ConcatInputSection *isec : inputs)
12933706191SJez Ng isecAddr = alignTo(isecAddr, isec->align) + isec->getSize();
13097211975SNico Weber if (isecAddr - addr + in.stubs->getSize() <=
13197211975SNico Weber std::min(target->backwardBranchRange, target->forwardBranchRange))
13233706191SJez Ng return false;
13333706191SJez Ng // Yes, this program is large enough to need thunks.
13440bcbe48SJez Ng for (ConcatInputSection *isec : inputs) {
13533706191SJez Ng for (Reloc &r : isec->relocs) {
13633706191SJez Ng if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
13733706191SJez Ng continue;
13833706191SJez Ng auto *sym = r.referent.get<Symbol *>();
13933706191SJez Ng // Pre-populate the thunkMap and memoize call site counts for every
14033706191SJez Ng // InputSection and ThunkInfo. We do this for the benefit of
141b440c257SJez Ng // estimateStubsInRangeVA().
14233706191SJez Ng ThunkInfo &thunkInfo = thunkMap[sym];
14333706191SJez Ng // Knowing ThunkInfo call site count will help us know whether or not we
14433706191SJez Ng // might need to create more for this referent at the time we are
14583df9406SNico Weber // estimating distance to __stubs in estimateStubsInRangeVA().
14633706191SJez Ng ++thunkInfo.callSiteCount;
14740bcbe48SJez Ng // We can avoid work on InputSections that have no BRANCH relocs.
14840bcbe48SJez Ng isec->hasCallSites = true;
14933706191SJez Ng }
15033706191SJez Ng }
15133706191SJez Ng return true;
15233706191SJez Ng }
15333706191SJez Ng
15433706191SJez Ng // Since __stubs is placed after __text, we must estimate the address
15533706191SJez Ng // beyond which stubs are within range of a simple forward branch.
15683df9406SNico Weber // This is called exactly once, when the last input section has been finalized.
estimateStubsInRangeVA(size_t callIdx) const157b440c257SJez Ng uint64_t TextOutputSection::estimateStubsInRangeVA(size_t callIdx) const {
15883df9406SNico Weber // Tally the functions which still have call sites remaining to process,
15983df9406SNico Weber // which yields the maximum number of thunks we might yet place.
16033706191SJez Ng size_t maxPotentialThunks = 0;
16133706191SJez Ng for (auto &tp : thunkMap) {
16233706191SJez Ng ThunkInfo &ti = tp.second;
16383df9406SNico Weber // This overcounts: Only sections that are in forward jump range from the
16483df9406SNico Weber // currently-active section get finalized, and all input sections are
16583df9406SNico Weber // finalized when estimateStubsInRangeVA() is called. So only backward
16683df9406SNico Weber // jumps will need thunks, but we count all jumps.
16783df9406SNico Weber if (ti.callSitesUsed < ti.callSiteCount)
16883df9406SNico Weber maxPotentialThunks += 1;
16933706191SJez Ng }
17033706191SJez Ng // Tally the total size of input sections remaining to process.
17183df9406SNico Weber uint64_t isecVA = inputs[callIdx]->getVA();
17283df9406SNico Weber uint64_t isecEnd = isecVA;
17383df9406SNico Weber for (size_t i = callIdx; i < inputs.size(); i++) {
17433706191SJez Ng InputSection *isec = inputs[i];
17533706191SJez Ng isecEnd = alignTo(isecEnd, isec->align) + isec->getSize();
17633706191SJez Ng }
17733706191SJez Ng // Estimate the address after which call sites can safely call stubs
17833706191SJez Ng // directly rather than through intermediary thunks.
17997211975SNico Weber uint64_t forwardBranchRange = target->forwardBranchRange;
18083df9406SNico Weber assert(isecEnd > forwardBranchRange &&
18183df9406SNico Weber "should not run thunk insertion if all code fits in jump range");
18283df9406SNico Weber assert(isecEnd - isecVA <= forwardBranchRange &&
18383df9406SNico Weber "should only finalize sections in jump range");
18433706191SJez Ng uint64_t stubsInRangeVA = isecEnd + maxPotentialThunks * target->thunkSize +
18597211975SNico Weber in.stubs->getSize() - forwardBranchRange;
18633706191SJez Ng log("thunks = " + std::to_string(thunkMap.size()) +
18733706191SJez Ng ", potential = " + std::to_string(maxPotentialThunks) +
18833706191SJez Ng ", stubs = " + std::to_string(in.stubs->getSize()) + ", isecVA = " +
189*b62e3a73SCorentin Jabot utohexstr(isecVA) + ", threshold = " + utohexstr(stubsInRangeVA) +
190*b62e3a73SCorentin Jabot ", isecEnd = " + utohexstr(isecEnd) +
191*b62e3a73SCorentin Jabot ", tail = " + utohexstr(isecEnd - isecVA) +
192*b62e3a73SCorentin Jabot ", slop = " + utohexstr(forwardBranchRange - (isecEnd - isecVA)));
19333706191SJez Ng return stubsInRangeVA;
19433706191SJez Ng }
19533706191SJez Ng
finalizeOne(ConcatInputSection * isec)196b440c257SJez Ng void ConcatOutputSection::finalizeOne(ConcatInputSection *isec) {
197b440c257SJez Ng size = alignTo(size, isec->align);
198b440c257SJez Ng fileSize = alignTo(fileSize, isec->align);
199b440c257SJez Ng isec->outSecOff = size;
20033706191SJez Ng isec->isFinal = true;
201b440c257SJez Ng size += isec->getSize();
202b440c257SJez Ng fileSize += isec->getFileSize();
203b440c257SJez Ng }
20433706191SJez Ng
finalizeContents()205b440c257SJez Ng void ConcatOutputSection::finalizeContents() {
206b440c257SJez Ng for (ConcatInputSection *isec : inputs)
207b440c257SJez Ng finalizeOne(isec);
208b440c257SJez Ng }
209b440c257SJez Ng
finalize()210b440c257SJez Ng void TextOutputSection::finalize() {
21133706191SJez Ng if (!needsThunks()) {
21204259cdeSJez Ng for (ConcatInputSection *isec : inputs)
21333706191SJez Ng finalizeOne(isec);
21433706191SJez Ng return;
21533706191SJez Ng }
21633706191SJez Ng
21797211975SNico Weber uint64_t forwardBranchRange = target->forwardBranchRange;
21897211975SNico Weber uint64_t backwardBranchRange = target->backwardBranchRange;
21933706191SJez Ng uint64_t stubsInRangeVA = TargetInfo::outOfRangeVA;
22033706191SJez Ng size_t thunkSize = target->thunkSize;
22133706191SJez Ng size_t relocCount = 0;
22233706191SJez Ng size_t callSiteCount = 0;
22333706191SJez Ng size_t thunkCallCount = 0;
22433706191SJez Ng size_t thunkCount = 0;
22533706191SJez Ng
22686c8f395SNico Weber // Walk all sections in order. Finalize all sections that are less than
22786c8f395SNico Weber // forwardBranchRange in front of it.
22886c8f395SNico Weber // isecVA is the address of the current section.
229b440c257SJez Ng // addr + size is the start address of the first non-finalized section.
23086c8f395SNico Weber
23133706191SJez Ng // inputs[finalIdx] is for finalization (address-assignment)
23233706191SJez Ng size_t finalIdx = 0;
23333706191SJez Ng // Kick-off by ensuring that the first input section has an address
23433706191SJez Ng for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx;
23533706191SJez Ng ++callIdx) {
23633706191SJez Ng if (finalIdx == callIdx)
23733706191SJez Ng finalizeOne(inputs[finalIdx++]);
23804259cdeSJez Ng ConcatInputSection *isec = inputs[callIdx];
23933706191SJez Ng assert(isec->isFinal);
24033706191SJez Ng uint64_t isecVA = isec->getVA();
24186c8f395SNico Weber
24286c8f395SNico Weber // Assign addresses up-to the forward branch-range limit.
24386c8f395SNico Weber // Every call instruction needs a small number of bytes (on Arm64: 4),
2441b2c36aaSNico Weber // and each inserted thunk needs a slightly larger number of bytes
24586c8f395SNico Weber // (on Arm64: 12). If a section starts with a branch instruction and
24686c8f395SNico Weber // contains several branch instructions in succession, then the distance
24786c8f395SNico Weber // from the current position to the position where the thunks are inserted
24886c8f395SNico Weber // grows. So leave room for a bunch of thunks.
249a963bc49SVincent Lee unsigned slop = 256 * thunkSize;
250b440c257SJez Ng while (finalIdx < endIdx && addr + size + inputs[finalIdx]->getSize() <
25186c8f395SNico Weber isecVA + forwardBranchRange - slop)
25233706191SJez Ng finalizeOne(inputs[finalIdx++]);
25386c8f395SNico Weber
25440bcbe48SJez Ng if (!isec->hasCallSites)
25533706191SJez Ng continue;
25686c8f395SNico Weber
25733706191SJez Ng if (finalIdx == endIdx && stubsInRangeVA == TargetInfo::outOfRangeVA) {
25833706191SJez Ng // When we have finalized all input sections, __stubs (destined
25933706191SJez Ng // to follow __text) comes within range of forward branches and
26033706191SJez Ng // we can estimate the threshold address after which we can
26133706191SJez Ng // reach any stub with a forward branch. Note that although it
26233706191SJez Ng // sits in the middle of a loop, this code executes only once.
26333706191SJez Ng // It is in the loop because we need to call it at the proper
26433706191SJez Ng // time: the earliest call site from which the end of __text
26533706191SJez Ng // (and start of __stubs) comes within range of a forward branch.
26633706191SJez Ng stubsInRangeVA = estimateStubsInRangeVA(callIdx);
26733706191SJez Ng }
26833706191SJez Ng // Process relocs by ascending address, i.e., ascending offset within isec
26933706191SJez Ng std::vector<Reloc> &relocs = isec->relocs;
270bcaf57caSJez Ng // FIXME: This property does not hold for object files produced by ld64's
271bcaf57caSJez Ng // `-r` mode.
27233706191SJez Ng assert(is_sorted(relocs,
27333706191SJez Ng [](Reloc &a, Reloc &b) { return a.offset > b.offset; }));
27433706191SJez Ng for (Reloc &r : reverse(relocs)) {
27533706191SJez Ng ++relocCount;
27633706191SJez Ng if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
27733706191SJez Ng continue;
27833706191SJez Ng ++callSiteCount;
27933706191SJez Ng // Calculate branch reachability boundaries
28033706191SJez Ng uint64_t callVA = isecVA + r.offset;
28197211975SNico Weber uint64_t lowVA =
28297211975SNico Weber backwardBranchRange < callVA ? callVA - backwardBranchRange : 0;
28397211975SNico Weber uint64_t highVA = callVA + forwardBranchRange;
28433706191SJez Ng // Calculate our call referent address
28533706191SJez Ng auto *funcSym = r.referent.get<Symbol *>();
28633706191SJez Ng ThunkInfo &thunkInfo = thunkMap[funcSym];
28733706191SJez Ng // The referent is not reachable, so we need to use a thunk ...
28833706191SJez Ng if (funcSym->isInStubs() && callVA >= stubsInRangeVA) {
28983df9406SNico Weber assert(callVA != TargetInfo::outOfRangeVA);
29033706191SJez Ng // ... Oh, wait! We are close enough to the end that __stubs
29133706191SJez Ng // are now within range of a simple forward branch.
29233706191SJez Ng continue;
29333706191SJez Ng }
29433706191SJez Ng uint64_t funcVA = funcSym->resolveBranchVA();
29533706191SJez Ng ++thunkInfo.callSitesUsed;
29697211975SNico Weber if (lowVA <= funcVA && funcVA <= highVA) {
29733706191SJez Ng // The referent is reachable with a simple call instruction.
29833706191SJez Ng continue;
29933706191SJez Ng }
30033706191SJez Ng ++thunkInfo.thunkCallCount;
30133706191SJez Ng ++thunkCallCount;
30233706191SJez Ng // If an existing thunk is reachable, use it ...
30333706191SJez Ng if (thunkInfo.sym) {
30433706191SJez Ng uint64_t thunkVA = thunkInfo.isec->getVA();
30597211975SNico Weber if (lowVA <= thunkVA && thunkVA <= highVA) {
30633706191SJez Ng r.referent = thunkInfo.sym;
30733706191SJez Ng continue;
30833706191SJez Ng }
30933706191SJez Ng }
31083df9406SNico Weber // ... otherwise, create a new thunk.
311b440c257SJez Ng if (addr + size > highVA) {
31286c8f395SNico Weber // There were too many consecutive branch instructions for `slop`
31386c8f395SNico Weber // above. If you hit this: For the current algorithm, just bumping up
31486c8f395SNico Weber // slop above and trying again is probably simplest. (See also PR51578
31586c8f395SNico Weber // comment 5).
31633706191SJez Ng fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
31733706191SJez Ng }
318f6b6e721SJez Ng thunkInfo.isec =
3192b78ef06SJez Ng makeSyntheticInputSection(isec->getSegName(), isec->getName());
32033706191SJez Ng thunkInfo.isec->parent = this;
32128be02f3SNico Weber
32228be02f3SNico Weber // This code runs after dead code removal. Need to set the `live` bit
32328be02f3SNico Weber // on the thunk isec so that asserts that check that only live sections
32428be02f3SNico Weber // get written are happy.
32528be02f3SNico Weber thunkInfo.isec->live = true;
32628be02f3SNico Weber
32783d59e05SAlexandre Ganea StringRef thunkName = saver().save(funcSym->getName() + ".thunk." +
32833706191SJez Ng std::to_string(thunkInfo.sequence++));
32910cda6e3SNico Weber if (!isa<Defined>(funcSym) || cast<Defined>(funcSym)->isExternal()) {
33033706191SJez Ng r.referent = thunkInfo.sym = symtab->addDefined(
331c0ec1036SVy Nguyen thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
332c0ec1036SVy Nguyen /*isWeakDef=*/false, /*isPrivateExtern=*/true,
333a5645513SNico Weber /*isThumb=*/false, /*isReferencedDynamically=*/false,
3349b29dae3SVy Nguyen /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false);
33510cda6e3SNico Weber } else {
33610cda6e3SNico Weber r.referent = thunkInfo.sym = make<Defined>(
3371cff723fSJez Ng thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, thunkSize,
3381cff723fSJez Ng /*isWeakDef=*/false, /*isExternal=*/false, /*isPrivateExtern=*/true,
3391cff723fSJez Ng /*includeInSymtab=*/true, /*isThumb=*/false,
34010cda6e3SNico Weber /*isReferencedDynamically=*/false, /*noDeadStrip=*/false,
34110cda6e3SNico Weber /*isWeakDefCanBeHidden=*/false);
34210cda6e3SNico Weber }
343002eda70SJez Ng thunkInfo.sym->used = true;
34433706191SJez Ng target->populateThunk(thunkInfo.isec, funcSym);
34533706191SJez Ng finalizeOne(thunkInfo.isec);
34633706191SJez Ng thunks.push_back(thunkInfo.isec);
34733706191SJez Ng ++thunkCount;
34833706191SJez Ng }
34933706191SJez Ng }
35033706191SJez Ng
35133706191SJez Ng log("thunks for " + parent->name + "," + name +
35233706191SJez Ng ": funcs = " + std::to_string(thunkMap.size()) +
35333706191SJez Ng ", relocs = " + std::to_string(relocCount) +
35433706191SJez Ng ", all calls = " + std::to_string(callSiteCount) +
35533706191SJez Ng ", thunk calls = " + std::to_string(thunkCallCount) +
35633706191SJez Ng ", thunks = " + std::to_string(thunkCount));
35733706191SJez Ng }
35833706191SJez Ng
writeTo(uint8_t * buf) const35933706191SJez Ng void ConcatOutputSection::writeTo(uint8_t *buf) const {
360b440c257SJez Ng for (ConcatInputSection *isec : inputs)
361b440c257SJez Ng isec->writeTo(buf + isec->outSecOff);
362b440c257SJez Ng }
363b440c257SJez Ng
writeTo(uint8_t * buf) const364b440c257SJez Ng void TextOutputSection::writeTo(uint8_t *buf) const {
36533706191SJez Ng // Merge input sections from thunk & ordinary vectors
36633706191SJez Ng size_t i = 0, ie = inputs.size();
36733706191SJez Ng size_t t = 0, te = thunks.size();
36833706191SJez Ng while (i < ie || t < te) {
3693f35dd06SVy Nguyen while (i < ie && (t == te || inputs[i]->empty() ||
37033706191SJez Ng inputs[i]->outSecOff < thunks[t]->outSecOff)) {
371b2a07390SJez Ng inputs[i]->writeTo(buf + inputs[i]->outSecOff);
37233706191SJez Ng ++i;
37333706191SJez Ng }
37433706191SJez Ng while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) {
375b2a07390SJez Ng thunks[t]->writeTo(buf + thunks[t]->outSecOff);
37633706191SJez Ng ++t;
37733706191SJez Ng }
37833706191SJez Ng }
37933706191SJez Ng }
38033706191SJez Ng
finalizeFlags(InputSection * input)381366df11aSVy Nguyen void ConcatOutputSection::finalizeFlags(InputSection *input) {
382f6b6e721SJez Ng switch (sectionType(input->getFlags())) {
383366df11aSVy Nguyen default /*type-unspec'ed*/:
384afdeb432SNico Weber // FIXME: Add additional logic here when supporting emitting obj files.
385366df11aSVy Nguyen break;
386366df11aSVy Nguyen case S_4BYTE_LITERALS:
387366df11aSVy Nguyen case S_8BYTE_LITERALS:
388366df11aSVy Nguyen case S_16BYTE_LITERALS:
389366df11aSVy Nguyen case S_CSTRING_LITERALS:
390366df11aSVy Nguyen case S_ZEROFILL:
391366df11aSVy Nguyen case S_LAZY_SYMBOL_POINTERS:
392366df11aSVy Nguyen case S_MOD_TERM_FUNC_POINTERS:
393366df11aSVy Nguyen case S_THREAD_LOCAL_REGULAR:
394366df11aSVy Nguyen case S_THREAD_LOCAL_ZEROFILL:
395366df11aSVy Nguyen case S_THREAD_LOCAL_VARIABLES:
396366df11aSVy Nguyen case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
397366df11aSVy Nguyen case S_THREAD_LOCAL_VARIABLE_POINTERS:
398366df11aSVy Nguyen case S_NON_LAZY_SYMBOL_POINTERS:
399366df11aSVy Nguyen case S_SYMBOL_STUBS:
400f6b6e721SJez Ng flags |= input->getFlags();
401366df11aSVy Nguyen break;
402366df11aSVy Nguyen }
40333706191SJez Ng }
404428a7c1bSJez Ng
405428a7c1bSJez Ng ConcatOutputSection *
getOrCreateForInput(const InputSection * isec)406428a7c1bSJez Ng ConcatOutputSection::getOrCreateForInput(const InputSection *isec) {
407428a7c1bSJez Ng NamePair names = maybeRenameSection({isec->getSegName(), isec->getName()});
408428a7c1bSJez Ng ConcatOutputSection *&osec = concatOutputSections[names];
409b440c257SJez Ng if (!osec) {
410b440c257SJez Ng if (isec->getSegName() == segment_names::text &&
411b440c257SJez Ng isec->getName() != section_names::gccExceptTab &&
412b440c257SJez Ng isec->getName() != section_names::ehFrame)
413b440c257SJez Ng osec = make<TextOutputSection>(names.second);
414b440c257SJez Ng else
415428a7c1bSJez Ng osec = make<ConcatOutputSection>(names.second);
416b440c257SJez Ng }
417428a7c1bSJez Ng return osec;
418428a7c1bSJez Ng }
419428a7c1bSJez Ng
maybeRenameSection(NamePair key)420428a7c1bSJez Ng NamePair macho::maybeRenameSection(NamePair key) {
421428a7c1bSJez Ng auto newNames = config->sectionRenameMap.find(key);
422428a7c1bSJez Ng if (newNames != config->sectionRenameMap.end())
423428a7c1bSJez Ng return newNames->second;
424428a7c1bSJez Ng return key;
425428a7c1bSJez Ng }
426