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" 16fe6060f1SDimitry Andric #include "lld/Common/ErrorHandler.h" 17fe6060f1SDimitry Andric #include "lld/Common/Memory.h" 18fe6060f1SDimitry Andric #include "llvm/BinaryFormat/MachO.h" 19fe6060f1SDimitry Andric #include "llvm/Support/ScopedPrinter.h" 20fe6060f1SDimitry Andric #include "llvm/Support/TimeProfiler.h" 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric using namespace llvm; 23fe6060f1SDimitry Andric using namespace llvm::MachO; 24fe6060f1SDimitry Andric using namespace lld; 25fe6060f1SDimitry Andric using namespace lld::macho; 26fe6060f1SDimitry Andric 27fe6060f1SDimitry Andric MapVector<NamePair, ConcatOutputSection *> macho::concatOutputSections; 28fe6060f1SDimitry Andric 29fe6060f1SDimitry Andric void ConcatOutputSection::addInput(ConcatInputSection *input) { 30fe6060f1SDimitry Andric assert(input->parent == this); 31fe6060f1SDimitry Andric if (inputs.empty()) { 32fe6060f1SDimitry Andric align = input->align; 33fe6060f1SDimitry Andric flags = input->getFlags(); 34fe6060f1SDimitry Andric } else { 35fe6060f1SDimitry Andric align = std::max(align, input->align); 36fe6060f1SDimitry Andric finalizeFlags(input); 37fe6060f1SDimitry Andric } 38fe6060f1SDimitry Andric inputs.push_back(input); 39fe6060f1SDimitry Andric } 40fe6060f1SDimitry Andric 41fe6060f1SDimitry Andric // Branch-range extension can be implemented in two ways, either through ... 42fe6060f1SDimitry Andric // 43fe6060f1SDimitry Andric // (1) Branch islands: Single branch instructions (also of limited range), 44fe6060f1SDimitry Andric // that might be chained in multiple hops to reach the desired 45fe6060f1SDimitry Andric // destination. On ARM64, as 16 branch islands are needed to hop between 46fe6060f1SDimitry Andric // opposite ends of a 2 GiB program. LD64 uses branch islands exclusively, 47fe6060f1SDimitry Andric // even when it needs excessive hops. 48fe6060f1SDimitry Andric // 49fe6060f1SDimitry Andric // (2) Thunks: Instruction(s) to load the destination address into a scratch 50fe6060f1SDimitry Andric // register, followed by a register-indirect branch. Thunks are 51fe6060f1SDimitry Andric // constructed to reach any arbitrary address, so need not be 52fe6060f1SDimitry Andric // chained. Although thunks need not be chained, a program might need 53fe6060f1SDimitry Andric // multiple thunks to the same destination distributed throughout a large 54fe6060f1SDimitry Andric // program so that all call sites can have one within range. 55fe6060f1SDimitry Andric // 56*349cc55cSDimitry Andric // The optimal approach is to mix islands for destinations within two hops, 57fe6060f1SDimitry Andric // and use thunks for destinations at greater distance. For now, we only 58fe6060f1SDimitry Andric // implement thunks. TODO: Adding support for branch islands! 59fe6060f1SDimitry Andric // 60fe6060f1SDimitry Andric // Internally -- as expressed in LLD's data structures -- a 61fe6060f1SDimitry Andric // branch-range-extension thunk comprises ... 62fe6060f1SDimitry Andric // 63fe6060f1SDimitry Andric // (1) new Defined privateExtern symbol for the thunk named 64fe6060f1SDimitry Andric // <FUNCTION>.thunk.<SEQUENCE>, which references ... 65fe6060f1SDimitry Andric // (2) new InputSection, which contains ... 66fe6060f1SDimitry Andric // (3.1) new data for the instructions to load & branch to the far address + 67fe6060f1SDimitry Andric // (3.2) new Relocs on instructions to load the far address, which reference ... 68fe6060f1SDimitry Andric // (4.1) existing Defined extern symbol for the real function in __text, or 69fe6060f1SDimitry Andric // (4.2) existing DylibSymbol for the real function in a dylib 70fe6060f1SDimitry Andric // 71fe6060f1SDimitry Andric // Nearly-optimal thunk-placement algorithm features: 72fe6060f1SDimitry Andric // 73fe6060f1SDimitry Andric // * Single pass: O(n) on the number of call sites. 74fe6060f1SDimitry Andric // 75fe6060f1SDimitry Andric // * Accounts for the exact space overhead of thunks - no heuristics 76fe6060f1SDimitry Andric // 77fe6060f1SDimitry Andric // * Exploits the full range of call instructions - forward & backward 78fe6060f1SDimitry Andric // 79fe6060f1SDimitry Andric // Data: 80fe6060f1SDimitry Andric // 81fe6060f1SDimitry Andric // * DenseMap<Symbol *, ThunkInfo> thunkMap: Maps the function symbol 82fe6060f1SDimitry Andric // to its thunk bookkeeper. 83fe6060f1SDimitry Andric // 84fe6060f1SDimitry Andric // * struct ThunkInfo (bookkeeper): Call instructions have limited range, and 85fe6060f1SDimitry Andric // distant call sites might be unable to reach the same thunk, so multiple 86fe6060f1SDimitry Andric // thunks are necessary to serve all call sites in a very large program. A 87fe6060f1SDimitry Andric // thunkInfo stores state for all thunks associated with a particular 88fe6060f1SDimitry Andric // function: (a) thunk symbol, (b) input section containing stub code, and 89fe6060f1SDimitry Andric // (c) sequence number for the active thunk incarnation. When an old thunk 90fe6060f1SDimitry Andric // goes out of range, we increment the sequence number and create a new 91fe6060f1SDimitry Andric // thunk named <FUNCTION>.thunk.<SEQUENCE>. 92fe6060f1SDimitry Andric // 93fe6060f1SDimitry Andric // * A thunk incarnation comprises (a) private-extern Defined symbol pointing 94fe6060f1SDimitry Andric // to (b) an InputSection holding machine instructions (similar to a MachO 95fe6060f1SDimitry Andric // stub), and (c) Reloc(s) that reference the real function for fixing-up 96fe6060f1SDimitry Andric // the stub code. 97fe6060f1SDimitry Andric // 98fe6060f1SDimitry Andric // * std::vector<InputSection *> MergedInputSection::thunks: A vector parallel 99fe6060f1SDimitry Andric // to the inputs vector. We store new thunks via cheap vector append, rather 100fe6060f1SDimitry Andric // than costly insertion into the inputs vector. 101fe6060f1SDimitry Andric // 102fe6060f1SDimitry Andric // Control Flow: 103fe6060f1SDimitry Andric // 104fe6060f1SDimitry Andric // * During address assignment, MergedInputSection::finalize() examines call 105fe6060f1SDimitry Andric // sites by ascending address and creates thunks. When a function is beyond 106fe6060f1SDimitry Andric // the range of a call site, we need a thunk. Place it at the largest 107fe6060f1SDimitry Andric // available forward address from the call site. Call sites increase 108fe6060f1SDimitry Andric // monotonically and thunks are always placed as far forward as possible; 109fe6060f1SDimitry Andric // thus, we place thunks at monotonically increasing addresses. Once a thunk 110fe6060f1SDimitry Andric // is placed, it and all previous input-section addresses are final. 111fe6060f1SDimitry Andric // 112*349cc55cSDimitry Andric // * ConcatInputSection::finalize() and ConcatInputSection::writeTo() merge 113fe6060f1SDimitry Andric // the inputs and thunks vectors (both ordered by ascending address), which 114fe6060f1SDimitry Andric // is simple and cheap. 115fe6060f1SDimitry Andric 116fe6060f1SDimitry Andric DenseMap<Symbol *, ThunkInfo> lld::macho::thunkMap; 117fe6060f1SDimitry Andric 118fe6060f1SDimitry Andric // Determine whether we need thunks, which depends on the target arch -- RISC 119fe6060f1SDimitry Andric // (i.e., ARM) generally does because it has limited-range branch/call 120fe6060f1SDimitry Andric // instructions, whereas CISC (i.e., x86) generally doesn't. RISC only needs 121fe6060f1SDimitry Andric // thunks for programs so large that branch source & destination addresses 122fe6060f1SDimitry Andric // might differ more than the range of branch instruction(s). 123fe6060f1SDimitry Andric bool ConcatOutputSection::needsThunks() const { 124fe6060f1SDimitry Andric if (!target->usesThunks()) 125fe6060f1SDimitry Andric return false; 126fe6060f1SDimitry Andric uint64_t isecAddr = addr; 127fe6060f1SDimitry Andric for (InputSection *isec : inputs) 128fe6060f1SDimitry Andric isecAddr = alignTo(isecAddr, isec->align) + isec->getSize(); 129*349cc55cSDimitry Andric if (isecAddr - addr + in.stubs->getSize() <= 130*349cc55cSDimitry Andric std::min(target->backwardBranchRange, target->forwardBranchRange)) 131fe6060f1SDimitry Andric return false; 132fe6060f1SDimitry Andric // Yes, this program is large enough to need thunks. 133fe6060f1SDimitry Andric for (InputSection *isec : inputs) { 134fe6060f1SDimitry Andric for (Reloc &r : isec->relocs) { 135fe6060f1SDimitry Andric if (!target->hasAttr(r.type, RelocAttrBits::BRANCH)) 136fe6060f1SDimitry Andric continue; 137fe6060f1SDimitry Andric auto *sym = r.referent.get<Symbol *>(); 138fe6060f1SDimitry Andric // Pre-populate the thunkMap and memoize call site counts for every 139fe6060f1SDimitry Andric // InputSection and ThunkInfo. We do this for the benefit of 140fe6060f1SDimitry Andric // ConcatOutputSection::estimateStubsInRangeVA() 141fe6060f1SDimitry Andric ThunkInfo &thunkInfo = thunkMap[sym]; 142fe6060f1SDimitry Andric // Knowing ThunkInfo call site count will help us know whether or not we 143fe6060f1SDimitry Andric // might need to create more for this referent at the time we are 144*349cc55cSDimitry Andric // estimating distance to __stubs in estimateStubsInRangeVA(). 145fe6060f1SDimitry Andric ++thunkInfo.callSiteCount; 146fe6060f1SDimitry Andric // Knowing InputSection call site count will help us avoid work on those 147fe6060f1SDimitry Andric // that have no BRANCH relocs. 148fe6060f1SDimitry Andric ++isec->callSiteCount; 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. 156*349cc55cSDimitry Andric // This is called exactly once, when the last input section has been finalized. 157fe6060f1SDimitry Andric uint64_t ConcatOutputSection::estimateStubsInRangeVA(size_t callIdx) const { 158*349cc55cSDimitry Andric // Tally the functions which still have call sites remaining to process, 159*349cc55cSDimitry 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; 163*349cc55cSDimitry Andric // This overcounts: Only sections that are in forward jump range from the 164*349cc55cSDimitry Andric // currently-active section get finalized, and all input sections are 165*349cc55cSDimitry Andric // finalized when estimateStubsInRangeVA() is called. So only backward 166*349cc55cSDimitry Andric // jumps will need thunks, but we count all jumps. 167*349cc55cSDimitry Andric if (ti.callSitesUsed < ti.callSiteCount) 168*349cc55cSDimitry Andric maxPotentialThunks += 1; 169fe6060f1SDimitry Andric } 170fe6060f1SDimitry Andric // Tally the total size of input sections remaining to process. 171*349cc55cSDimitry Andric uint64_t isecVA = inputs[callIdx]->getVA(); 172*349cc55cSDimitry Andric uint64_t isecEnd = isecVA; 173*349cc55cSDimitry Andric for (size_t i = callIdx; i < inputs.size(); i++) { 174fe6060f1SDimitry Andric InputSection *isec = inputs[i]; 175fe6060f1SDimitry Andric isecEnd = alignTo(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. 179*349cc55cSDimitry Andric uint64_t forwardBranchRange = target->forwardBranchRange; 180*349cc55cSDimitry Andric assert(isecEnd > forwardBranchRange && 181*349cc55cSDimitry Andric "should not run thunk insertion if all code fits in jump range"); 182*349cc55cSDimitry Andric assert(isecEnd - isecVA <= forwardBranchRange && 183*349cc55cSDimitry Andric "should only finalize sections in jump range"); 184fe6060f1SDimitry Andric uint64_t stubsInRangeVA = isecEnd + maxPotentialThunks * target->thunkSize + 185*349cc55cSDimitry 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 = " + 189fe6060f1SDimitry Andric to_hexString(isecVA) + ", threshold = " + to_hexString(stubsInRangeVA) + 190fe6060f1SDimitry Andric ", isecEnd = " + to_hexString(isecEnd) + 191fe6060f1SDimitry Andric ", tail = " + to_hexString(isecEnd - isecVA) + 192*349cc55cSDimitry Andric ", slop = " + to_hexString(forwardBranchRange - (isecEnd - isecVA))); 193fe6060f1SDimitry Andric return stubsInRangeVA; 194fe6060f1SDimitry Andric } 195fe6060f1SDimitry Andric 196fe6060f1SDimitry Andric void ConcatOutputSection::finalize() { 197fe6060f1SDimitry Andric uint64_t isecAddr = addr; 198fe6060f1SDimitry Andric uint64_t isecFileOff = fileOff; 199fe6060f1SDimitry Andric auto finalizeOne = [&](ConcatInputSection *isec) { 200fe6060f1SDimitry Andric isecAddr = alignTo(isecAddr, isec->align); 201fe6060f1SDimitry Andric isecFileOff = alignTo(isecFileOff, isec->align); 202fe6060f1SDimitry Andric isec->outSecOff = isecAddr - addr; 203fe6060f1SDimitry Andric isec->isFinal = true; 204fe6060f1SDimitry Andric isecAddr += isec->getSize(); 205fe6060f1SDimitry Andric isecFileOff += isec->getFileSize(); 206fe6060f1SDimitry Andric }; 207fe6060f1SDimitry Andric 208fe6060f1SDimitry Andric if (!needsThunks()) { 209fe6060f1SDimitry Andric for (ConcatInputSection *isec : inputs) 210fe6060f1SDimitry Andric finalizeOne(isec); 211fe6060f1SDimitry Andric size = isecAddr - addr; 212fe6060f1SDimitry Andric fileSize = isecFileOff - fileOff; 213fe6060f1SDimitry Andric return; 214fe6060f1SDimitry Andric } 215fe6060f1SDimitry Andric 216*349cc55cSDimitry Andric uint64_t forwardBranchRange = target->forwardBranchRange; 217*349cc55cSDimitry Andric uint64_t backwardBranchRange = target->backwardBranchRange; 218fe6060f1SDimitry Andric uint64_t stubsInRangeVA = TargetInfo::outOfRangeVA; 219fe6060f1SDimitry Andric size_t thunkSize = target->thunkSize; 220fe6060f1SDimitry Andric size_t relocCount = 0; 221fe6060f1SDimitry Andric size_t callSiteCount = 0; 222fe6060f1SDimitry Andric size_t thunkCallCount = 0; 223fe6060f1SDimitry Andric size_t thunkCount = 0; 224fe6060f1SDimitry Andric 225*349cc55cSDimitry Andric // Walk all sections in order. Finalize all sections that are less than 226*349cc55cSDimitry Andric // forwardBranchRange in front of it. 227*349cc55cSDimitry Andric // isecVA is the address of the current section. 228*349cc55cSDimitry Andric // isecAddr is the start address of the first non-finalized section. 229*349cc55cSDimitry Andric 230fe6060f1SDimitry Andric // inputs[finalIdx] is for finalization (address-assignment) 231fe6060f1SDimitry Andric size_t finalIdx = 0; 232fe6060f1SDimitry Andric // Kick-off by ensuring that the first input section has an address 233fe6060f1SDimitry Andric for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx; 234fe6060f1SDimitry Andric ++callIdx) { 235fe6060f1SDimitry Andric if (finalIdx == callIdx) 236fe6060f1SDimitry Andric finalizeOne(inputs[finalIdx++]); 237fe6060f1SDimitry Andric ConcatInputSection *isec = inputs[callIdx]; 238fe6060f1SDimitry Andric assert(isec->isFinal); 239fe6060f1SDimitry Andric uint64_t isecVA = isec->getVA(); 240*349cc55cSDimitry Andric 241*349cc55cSDimitry Andric // Assign addresses up-to the forward branch-range limit. 242*349cc55cSDimitry Andric // Every call instruction needs a small number of bytes (on Arm64: 4), 243*349cc55cSDimitry Andric // and each inserted thunk needs a slightly larger number of bytes 244*349cc55cSDimitry Andric // (on Arm64: 12). If a section starts with a branch instruction and 245*349cc55cSDimitry Andric // contains several branch instructions in succession, then the distance 246*349cc55cSDimitry Andric // from the current position to the position where the thunks are inserted 247*349cc55cSDimitry Andric // grows. So leave room for a bunch of thunks. 248*349cc55cSDimitry Andric unsigned slop = 100 * thunkSize; 249*349cc55cSDimitry Andric while (finalIdx < endIdx && isecAddr + inputs[finalIdx]->getSize() < 250*349cc55cSDimitry Andric isecVA + forwardBranchRange - slop) 251fe6060f1SDimitry Andric finalizeOne(inputs[finalIdx++]); 252*349cc55cSDimitry Andric 253fe6060f1SDimitry Andric if (isec->callSiteCount == 0) 254fe6060f1SDimitry Andric continue; 255*349cc55cSDimitry Andric 256fe6060f1SDimitry Andric if (finalIdx == endIdx && stubsInRangeVA == TargetInfo::outOfRangeVA) { 257fe6060f1SDimitry Andric // When we have finalized all input sections, __stubs (destined 258fe6060f1SDimitry Andric // to follow __text) comes within range of forward branches and 259fe6060f1SDimitry Andric // we can estimate the threshold address after which we can 260fe6060f1SDimitry Andric // reach any stub with a forward branch. Note that although it 261fe6060f1SDimitry Andric // sits in the middle of a loop, this code executes only once. 262fe6060f1SDimitry Andric // It is in the loop because we need to call it at the proper 263fe6060f1SDimitry Andric // time: the earliest call site from which the end of __text 264fe6060f1SDimitry Andric // (and start of __stubs) comes within range of a forward branch. 265fe6060f1SDimitry Andric stubsInRangeVA = estimateStubsInRangeVA(callIdx); 266fe6060f1SDimitry Andric } 267fe6060f1SDimitry Andric // Process relocs by ascending address, i.e., ascending offset within isec 268fe6060f1SDimitry Andric std::vector<Reloc> &relocs = isec->relocs; 269fe6060f1SDimitry Andric // FIXME: This property does not hold for object files produced by ld64's 270fe6060f1SDimitry Andric // `-r` mode. 271fe6060f1SDimitry Andric assert(is_sorted(relocs, 272fe6060f1SDimitry Andric [](Reloc &a, Reloc &b) { return a.offset > b.offset; })); 273fe6060f1SDimitry Andric for (Reloc &r : reverse(relocs)) { 274fe6060f1SDimitry Andric ++relocCount; 275fe6060f1SDimitry Andric if (!target->hasAttr(r.type, RelocAttrBits::BRANCH)) 276fe6060f1SDimitry Andric continue; 277fe6060f1SDimitry Andric ++callSiteCount; 278fe6060f1SDimitry Andric // Calculate branch reachability boundaries 279fe6060f1SDimitry Andric uint64_t callVA = isecVA + r.offset; 280*349cc55cSDimitry Andric uint64_t lowVA = 281*349cc55cSDimitry Andric backwardBranchRange < callVA ? callVA - backwardBranchRange : 0; 282*349cc55cSDimitry Andric uint64_t highVA = callVA + forwardBranchRange; 283fe6060f1SDimitry Andric // Calculate our call referent address 284fe6060f1SDimitry Andric auto *funcSym = r.referent.get<Symbol *>(); 285fe6060f1SDimitry Andric ThunkInfo &thunkInfo = thunkMap[funcSym]; 286fe6060f1SDimitry Andric // The referent is not reachable, so we need to use a thunk ... 287fe6060f1SDimitry Andric if (funcSym->isInStubs() && callVA >= stubsInRangeVA) { 288*349cc55cSDimitry Andric assert(callVA != TargetInfo::outOfRangeVA); 289fe6060f1SDimitry Andric // ... Oh, wait! We are close enough to the end that __stubs 290fe6060f1SDimitry Andric // are now within range of a simple forward branch. 291fe6060f1SDimitry Andric continue; 292fe6060f1SDimitry Andric } 293fe6060f1SDimitry Andric uint64_t funcVA = funcSym->resolveBranchVA(); 294fe6060f1SDimitry Andric ++thunkInfo.callSitesUsed; 295*349cc55cSDimitry Andric if (lowVA <= funcVA && funcVA <= highVA) { 296fe6060f1SDimitry Andric // The referent is reachable with a simple call instruction. 297fe6060f1SDimitry Andric continue; 298fe6060f1SDimitry Andric } 299fe6060f1SDimitry Andric ++thunkInfo.thunkCallCount; 300fe6060f1SDimitry Andric ++thunkCallCount; 301fe6060f1SDimitry Andric // If an existing thunk is reachable, use it ... 302fe6060f1SDimitry Andric if (thunkInfo.sym) { 303fe6060f1SDimitry Andric uint64_t thunkVA = thunkInfo.isec->getVA(); 304*349cc55cSDimitry Andric if (lowVA <= thunkVA && thunkVA <= highVA) { 305fe6060f1SDimitry Andric r.referent = thunkInfo.sym; 306fe6060f1SDimitry Andric continue; 307fe6060f1SDimitry Andric } 308fe6060f1SDimitry Andric } 309*349cc55cSDimitry Andric // ... otherwise, create a new thunk. 310fe6060f1SDimitry Andric if (isecAddr > highVA) { 311*349cc55cSDimitry Andric // There were too many consecutive branch instructions for `slop` 312*349cc55cSDimitry Andric // above. If you hit this: For the current algorithm, just bumping up 313*349cc55cSDimitry Andric // slop above and trying again is probably simplest. (See also PR51578 314*349cc55cSDimitry Andric // comment 5). 315fe6060f1SDimitry Andric fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun"); 316fe6060f1SDimitry Andric } 317fe6060f1SDimitry Andric thunkInfo.isec = 318fe6060f1SDimitry Andric make<ConcatInputSection>(isec->getSegName(), isec->getName()); 319fe6060f1SDimitry Andric thunkInfo.isec->parent = this; 320*349cc55cSDimitry Andric 321*349cc55cSDimitry Andric // This code runs after dead code removal. Need to set the `live` bit 322*349cc55cSDimitry Andric // on the thunk isec so that asserts that check that only live sections 323*349cc55cSDimitry Andric // get written are happy. 324*349cc55cSDimitry Andric thunkInfo.isec->live = true; 325*349cc55cSDimitry Andric 326fe6060f1SDimitry Andric StringRef thunkName = saver.save(funcSym->getName() + ".thunk." + 327fe6060f1SDimitry Andric std::to_string(thunkInfo.sequence++)); 328fe6060f1SDimitry Andric r.referent = thunkInfo.sym = symtab->addDefined( 329fe6060f1SDimitry Andric thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, 330fe6060f1SDimitry Andric /*size=*/thunkSize, /*isWeakDef=*/false, /*isPrivateExtern=*/true, 331fe6060f1SDimitry Andric /*isThumb=*/false, /*isReferencedDynamically=*/false, 332*349cc55cSDimitry Andric /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false); 333*349cc55cSDimitry Andric thunkInfo.sym->used = true; 334fe6060f1SDimitry Andric target->populateThunk(thunkInfo.isec, funcSym); 335fe6060f1SDimitry Andric finalizeOne(thunkInfo.isec); 336fe6060f1SDimitry Andric thunks.push_back(thunkInfo.isec); 337fe6060f1SDimitry Andric ++thunkCount; 338fe6060f1SDimitry Andric } 339fe6060f1SDimitry Andric } 340fe6060f1SDimitry Andric size = isecAddr - addr; 341fe6060f1SDimitry Andric fileSize = isecFileOff - fileOff; 342fe6060f1SDimitry Andric 343fe6060f1SDimitry Andric log("thunks for " + parent->name + "," + name + 344fe6060f1SDimitry Andric ": funcs = " + std::to_string(thunkMap.size()) + 345fe6060f1SDimitry Andric ", relocs = " + std::to_string(relocCount) + 346fe6060f1SDimitry Andric ", all calls = " + std::to_string(callSiteCount) + 347fe6060f1SDimitry Andric ", thunk calls = " + std::to_string(thunkCallCount) + 348fe6060f1SDimitry Andric ", thunks = " + std::to_string(thunkCount)); 349fe6060f1SDimitry Andric } 350fe6060f1SDimitry Andric 351fe6060f1SDimitry Andric void ConcatOutputSection::writeTo(uint8_t *buf) const { 352fe6060f1SDimitry Andric // Merge input sections from thunk & ordinary vectors 353fe6060f1SDimitry Andric size_t i = 0, ie = inputs.size(); 354fe6060f1SDimitry Andric size_t t = 0, te = thunks.size(); 355fe6060f1SDimitry Andric while (i < ie || t < te) { 356*349cc55cSDimitry Andric while (i < ie && (t == te || inputs[i]->empty() || 357fe6060f1SDimitry Andric inputs[i]->outSecOff < thunks[t]->outSecOff)) { 358fe6060f1SDimitry Andric inputs[i]->writeTo(buf + inputs[i]->outSecOff); 359fe6060f1SDimitry Andric ++i; 360fe6060f1SDimitry Andric } 361fe6060f1SDimitry Andric while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) { 362fe6060f1SDimitry Andric thunks[t]->writeTo(buf + thunks[t]->outSecOff); 363fe6060f1SDimitry Andric ++t; 364fe6060f1SDimitry Andric } 365fe6060f1SDimitry Andric } 366fe6060f1SDimitry Andric } 367fe6060f1SDimitry Andric 368fe6060f1SDimitry Andric void ConcatOutputSection::finalizeFlags(InputSection *input) { 369fe6060f1SDimitry Andric switch (sectionType(input->getFlags())) { 370fe6060f1SDimitry Andric default /*type-unspec'ed*/: 371fe6060f1SDimitry Andric // FIXME: Add additional logic here when supporting emitting obj files. 372fe6060f1SDimitry Andric break; 373fe6060f1SDimitry Andric case S_4BYTE_LITERALS: 374fe6060f1SDimitry Andric case S_8BYTE_LITERALS: 375fe6060f1SDimitry Andric case S_16BYTE_LITERALS: 376fe6060f1SDimitry Andric case S_CSTRING_LITERALS: 377fe6060f1SDimitry Andric case S_ZEROFILL: 378fe6060f1SDimitry Andric case S_LAZY_SYMBOL_POINTERS: 379fe6060f1SDimitry Andric case S_MOD_TERM_FUNC_POINTERS: 380fe6060f1SDimitry Andric case S_THREAD_LOCAL_REGULAR: 381fe6060f1SDimitry Andric case S_THREAD_LOCAL_ZEROFILL: 382fe6060f1SDimitry Andric case S_THREAD_LOCAL_VARIABLES: 383fe6060f1SDimitry Andric case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: 384fe6060f1SDimitry Andric case S_THREAD_LOCAL_VARIABLE_POINTERS: 385fe6060f1SDimitry Andric case S_NON_LAZY_SYMBOL_POINTERS: 386fe6060f1SDimitry Andric case S_SYMBOL_STUBS: 387fe6060f1SDimitry Andric flags |= input->getFlags(); 388fe6060f1SDimitry Andric break; 389fe6060f1SDimitry Andric } 390fe6060f1SDimitry Andric } 391fe6060f1SDimitry Andric 392fe6060f1SDimitry Andric ConcatOutputSection * 393fe6060f1SDimitry Andric ConcatOutputSection::getOrCreateForInput(const InputSection *isec) { 394fe6060f1SDimitry Andric NamePair names = maybeRenameSection({isec->getSegName(), isec->getName()}); 395fe6060f1SDimitry Andric ConcatOutputSection *&osec = concatOutputSections[names]; 396fe6060f1SDimitry Andric if (!osec) 397fe6060f1SDimitry Andric osec = make<ConcatOutputSection>(names.second); 398fe6060f1SDimitry Andric return osec; 399fe6060f1SDimitry Andric } 400fe6060f1SDimitry Andric 401fe6060f1SDimitry Andric NamePair macho::maybeRenameSection(NamePair key) { 402fe6060f1SDimitry Andric auto newNames = config->sectionRenameMap.find(key); 403fe6060f1SDimitry Andric if (newNames != config->sectionRenameMap.end()) 404fe6060f1SDimitry Andric return newNames->second; 405fe6060f1SDimitry Andric return key; 406fe6060f1SDimitry Andric } 407