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 // 56349cc55cSDimitry 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 // 112349cc55cSDimitry 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; 127*0eae32dcSDimitry Andric for (ConcatInputSection *isec : inputs) 128fe6060f1SDimitry Andric isecAddr = alignTo(isecAddr, isec->align) + isec->getSize(); 129349cc55cSDimitry Andric if (isecAddr - addr + in.stubs->getSize() <= 130349cc55cSDimitry Andric std::min(target->backwardBranchRange, target->forwardBranchRange)) 131fe6060f1SDimitry Andric return false; 132fe6060f1SDimitry Andric // Yes, this program is large enough to need thunks. 133*0eae32dcSDimitry Andric for (ConcatInputSection *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 144349cc55cSDimitry Andric // estimating distance to __stubs in estimateStubsInRangeVA(). 145fe6060f1SDimitry Andric ++thunkInfo.callSiteCount; 146*0eae32dcSDimitry Andric // We can avoid work on InputSections that have no BRANCH relocs. 147*0eae32dcSDimitry Andric isec->hasCallSites = true; 148fe6060f1SDimitry Andric } 149fe6060f1SDimitry Andric } 150fe6060f1SDimitry Andric return true; 151fe6060f1SDimitry Andric } 152fe6060f1SDimitry Andric 153fe6060f1SDimitry Andric // Since __stubs is placed after __text, we must estimate the address 154fe6060f1SDimitry Andric // beyond which stubs are within range of a simple forward branch. 155349cc55cSDimitry Andric // This is called exactly once, when the last input section has been finalized. 156fe6060f1SDimitry Andric uint64_t ConcatOutputSection::estimateStubsInRangeVA(size_t callIdx) const { 157349cc55cSDimitry Andric // Tally the functions which still have call sites remaining to process, 158349cc55cSDimitry Andric // which yields the maximum number of thunks we might yet place. 159fe6060f1SDimitry Andric size_t maxPotentialThunks = 0; 160fe6060f1SDimitry Andric for (auto &tp : thunkMap) { 161fe6060f1SDimitry Andric ThunkInfo &ti = tp.second; 162349cc55cSDimitry Andric // This overcounts: Only sections that are in forward jump range from the 163349cc55cSDimitry Andric // currently-active section get finalized, and all input sections are 164349cc55cSDimitry Andric // finalized when estimateStubsInRangeVA() is called. So only backward 165349cc55cSDimitry Andric // jumps will need thunks, but we count all jumps. 166349cc55cSDimitry Andric if (ti.callSitesUsed < ti.callSiteCount) 167349cc55cSDimitry Andric maxPotentialThunks += 1; 168fe6060f1SDimitry Andric } 169fe6060f1SDimitry Andric // Tally the total size of input sections remaining to process. 170349cc55cSDimitry Andric uint64_t isecVA = inputs[callIdx]->getVA(); 171349cc55cSDimitry Andric uint64_t isecEnd = isecVA; 172349cc55cSDimitry Andric for (size_t i = callIdx; i < inputs.size(); i++) { 173fe6060f1SDimitry Andric InputSection *isec = inputs[i]; 174fe6060f1SDimitry Andric isecEnd = alignTo(isecEnd, isec->align) + isec->getSize(); 175fe6060f1SDimitry Andric } 176fe6060f1SDimitry Andric // Estimate the address after which call sites can safely call stubs 177fe6060f1SDimitry Andric // directly rather than through intermediary thunks. 178349cc55cSDimitry Andric uint64_t forwardBranchRange = target->forwardBranchRange; 179349cc55cSDimitry Andric assert(isecEnd > forwardBranchRange && 180349cc55cSDimitry Andric "should not run thunk insertion if all code fits in jump range"); 181349cc55cSDimitry Andric assert(isecEnd - isecVA <= forwardBranchRange && 182349cc55cSDimitry Andric "should only finalize sections in jump range"); 183fe6060f1SDimitry Andric uint64_t stubsInRangeVA = isecEnd + maxPotentialThunks * target->thunkSize + 184349cc55cSDimitry Andric in.stubs->getSize() - forwardBranchRange; 185fe6060f1SDimitry Andric log("thunks = " + std::to_string(thunkMap.size()) + 186fe6060f1SDimitry Andric ", potential = " + std::to_string(maxPotentialThunks) + 187fe6060f1SDimitry Andric ", stubs = " + std::to_string(in.stubs->getSize()) + ", isecVA = " + 188fe6060f1SDimitry Andric to_hexString(isecVA) + ", threshold = " + to_hexString(stubsInRangeVA) + 189fe6060f1SDimitry Andric ", isecEnd = " + to_hexString(isecEnd) + 190fe6060f1SDimitry Andric ", tail = " + to_hexString(isecEnd - isecVA) + 191349cc55cSDimitry Andric ", slop = " + to_hexString(forwardBranchRange - (isecEnd - isecVA))); 192fe6060f1SDimitry Andric return stubsInRangeVA; 193fe6060f1SDimitry Andric } 194fe6060f1SDimitry Andric 195fe6060f1SDimitry Andric void ConcatOutputSection::finalize() { 196fe6060f1SDimitry Andric uint64_t isecAddr = addr; 197fe6060f1SDimitry Andric uint64_t isecFileOff = fileOff; 198fe6060f1SDimitry Andric auto finalizeOne = [&](ConcatInputSection *isec) { 199fe6060f1SDimitry Andric isecAddr = alignTo(isecAddr, isec->align); 200fe6060f1SDimitry Andric isecFileOff = alignTo(isecFileOff, isec->align); 201fe6060f1SDimitry Andric isec->outSecOff = isecAddr - addr; 202fe6060f1SDimitry Andric isec->isFinal = true; 203fe6060f1SDimitry Andric isecAddr += isec->getSize(); 204fe6060f1SDimitry Andric isecFileOff += isec->getFileSize(); 205fe6060f1SDimitry Andric }; 206fe6060f1SDimitry Andric 207fe6060f1SDimitry Andric if (!needsThunks()) { 208fe6060f1SDimitry Andric for (ConcatInputSection *isec : inputs) 209fe6060f1SDimitry Andric finalizeOne(isec); 210fe6060f1SDimitry Andric size = isecAddr - addr; 211fe6060f1SDimitry Andric fileSize = isecFileOff - fileOff; 212fe6060f1SDimitry Andric return; 213fe6060f1SDimitry Andric } 214fe6060f1SDimitry Andric 215349cc55cSDimitry Andric uint64_t forwardBranchRange = target->forwardBranchRange; 216349cc55cSDimitry Andric uint64_t backwardBranchRange = target->backwardBranchRange; 217fe6060f1SDimitry Andric uint64_t stubsInRangeVA = TargetInfo::outOfRangeVA; 218fe6060f1SDimitry Andric size_t thunkSize = target->thunkSize; 219fe6060f1SDimitry Andric size_t relocCount = 0; 220fe6060f1SDimitry Andric size_t callSiteCount = 0; 221fe6060f1SDimitry Andric size_t thunkCallCount = 0; 222fe6060f1SDimitry Andric size_t thunkCount = 0; 223fe6060f1SDimitry Andric 224349cc55cSDimitry Andric // Walk all sections in order. Finalize all sections that are less than 225349cc55cSDimitry Andric // forwardBranchRange in front of it. 226349cc55cSDimitry Andric // isecVA is the address of the current section. 227349cc55cSDimitry Andric // isecAddr is the start address of the first non-finalized section. 228349cc55cSDimitry Andric 229fe6060f1SDimitry Andric // inputs[finalIdx] is for finalization (address-assignment) 230fe6060f1SDimitry Andric size_t finalIdx = 0; 231fe6060f1SDimitry Andric // Kick-off by ensuring that the first input section has an address 232fe6060f1SDimitry Andric for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx; 233fe6060f1SDimitry Andric ++callIdx) { 234fe6060f1SDimitry Andric if (finalIdx == callIdx) 235fe6060f1SDimitry Andric finalizeOne(inputs[finalIdx++]); 236fe6060f1SDimitry Andric ConcatInputSection *isec = inputs[callIdx]; 237fe6060f1SDimitry Andric assert(isec->isFinal); 238fe6060f1SDimitry Andric uint64_t isecVA = isec->getVA(); 239349cc55cSDimitry Andric 240349cc55cSDimitry Andric // Assign addresses up-to the forward branch-range limit. 241349cc55cSDimitry Andric // Every call instruction needs a small number of bytes (on Arm64: 4), 242349cc55cSDimitry Andric // and each inserted thunk needs a slightly larger number of bytes 243349cc55cSDimitry Andric // (on Arm64: 12). If a section starts with a branch instruction and 244349cc55cSDimitry Andric // contains several branch instructions in succession, then the distance 245349cc55cSDimitry Andric // from the current position to the position where the thunks are inserted 246349cc55cSDimitry Andric // grows. So leave room for a bunch of thunks. 247349cc55cSDimitry Andric unsigned slop = 100 * thunkSize; 248349cc55cSDimitry Andric while (finalIdx < endIdx && isecAddr + inputs[finalIdx]->getSize() < 249349cc55cSDimitry Andric isecVA + forwardBranchRange - slop) 250fe6060f1SDimitry Andric finalizeOne(inputs[finalIdx++]); 251349cc55cSDimitry Andric 252*0eae32dcSDimitry Andric if (!isec->hasCallSites) 253fe6060f1SDimitry Andric continue; 254349cc55cSDimitry Andric 255fe6060f1SDimitry Andric if (finalIdx == endIdx && stubsInRangeVA == TargetInfo::outOfRangeVA) { 256fe6060f1SDimitry Andric // When we have finalized all input sections, __stubs (destined 257fe6060f1SDimitry Andric // to follow __text) comes within range of forward branches and 258fe6060f1SDimitry Andric // we can estimate the threshold address after which we can 259fe6060f1SDimitry Andric // reach any stub with a forward branch. Note that although it 260fe6060f1SDimitry Andric // sits in the middle of a loop, this code executes only once. 261fe6060f1SDimitry Andric // It is in the loop because we need to call it at the proper 262fe6060f1SDimitry Andric // time: the earliest call site from which the end of __text 263fe6060f1SDimitry Andric // (and start of __stubs) comes within range of a forward branch. 264fe6060f1SDimitry Andric stubsInRangeVA = estimateStubsInRangeVA(callIdx); 265fe6060f1SDimitry Andric } 266fe6060f1SDimitry Andric // Process relocs by ascending address, i.e., ascending offset within isec 267fe6060f1SDimitry Andric std::vector<Reloc> &relocs = isec->relocs; 268fe6060f1SDimitry Andric // FIXME: This property does not hold for object files produced by ld64's 269fe6060f1SDimitry Andric // `-r` mode. 270fe6060f1SDimitry Andric assert(is_sorted(relocs, 271fe6060f1SDimitry Andric [](Reloc &a, Reloc &b) { return a.offset > b.offset; })); 272fe6060f1SDimitry Andric for (Reloc &r : reverse(relocs)) { 273fe6060f1SDimitry Andric ++relocCount; 274fe6060f1SDimitry Andric if (!target->hasAttr(r.type, RelocAttrBits::BRANCH)) 275fe6060f1SDimitry Andric continue; 276fe6060f1SDimitry Andric ++callSiteCount; 277fe6060f1SDimitry Andric // Calculate branch reachability boundaries 278fe6060f1SDimitry Andric uint64_t callVA = isecVA + r.offset; 279349cc55cSDimitry Andric uint64_t lowVA = 280349cc55cSDimitry Andric backwardBranchRange < callVA ? callVA - backwardBranchRange : 0; 281349cc55cSDimitry Andric uint64_t highVA = callVA + forwardBranchRange; 282fe6060f1SDimitry Andric // Calculate our call referent address 283fe6060f1SDimitry Andric auto *funcSym = r.referent.get<Symbol *>(); 284fe6060f1SDimitry Andric ThunkInfo &thunkInfo = thunkMap[funcSym]; 285fe6060f1SDimitry Andric // The referent is not reachable, so we need to use a thunk ... 286fe6060f1SDimitry Andric if (funcSym->isInStubs() && callVA >= stubsInRangeVA) { 287349cc55cSDimitry Andric assert(callVA != TargetInfo::outOfRangeVA); 288fe6060f1SDimitry Andric // ... Oh, wait! We are close enough to the end that __stubs 289fe6060f1SDimitry Andric // are now within range of a simple forward branch. 290fe6060f1SDimitry Andric continue; 291fe6060f1SDimitry Andric } 292fe6060f1SDimitry Andric uint64_t funcVA = funcSym->resolveBranchVA(); 293fe6060f1SDimitry Andric ++thunkInfo.callSitesUsed; 294349cc55cSDimitry Andric if (lowVA <= funcVA && funcVA <= highVA) { 295fe6060f1SDimitry Andric // The referent is reachable with a simple call instruction. 296fe6060f1SDimitry Andric continue; 297fe6060f1SDimitry Andric } 298fe6060f1SDimitry Andric ++thunkInfo.thunkCallCount; 299fe6060f1SDimitry Andric ++thunkCallCount; 300fe6060f1SDimitry Andric // If an existing thunk is reachable, use it ... 301fe6060f1SDimitry Andric if (thunkInfo.sym) { 302fe6060f1SDimitry Andric uint64_t thunkVA = thunkInfo.isec->getVA(); 303349cc55cSDimitry Andric if (lowVA <= thunkVA && thunkVA <= highVA) { 304fe6060f1SDimitry Andric r.referent = thunkInfo.sym; 305fe6060f1SDimitry Andric continue; 306fe6060f1SDimitry Andric } 307fe6060f1SDimitry Andric } 308349cc55cSDimitry Andric // ... otherwise, create a new thunk. 309fe6060f1SDimitry Andric if (isecAddr > highVA) { 310349cc55cSDimitry Andric // There were too many consecutive branch instructions for `slop` 311349cc55cSDimitry Andric // above. If you hit this: For the current algorithm, just bumping up 312349cc55cSDimitry Andric // slop above and trying again is probably simplest. (See also PR51578 313349cc55cSDimitry Andric // comment 5). 314fe6060f1SDimitry Andric fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun"); 315fe6060f1SDimitry Andric } 316fe6060f1SDimitry Andric thunkInfo.isec = 317fe6060f1SDimitry Andric make<ConcatInputSection>(isec->getSegName(), isec->getName()); 318fe6060f1SDimitry Andric thunkInfo.isec->parent = this; 319349cc55cSDimitry Andric 320349cc55cSDimitry Andric // This code runs after dead code removal. Need to set the `live` bit 321349cc55cSDimitry Andric // on the thunk isec so that asserts that check that only live sections 322349cc55cSDimitry Andric // get written are happy. 323349cc55cSDimitry Andric thunkInfo.isec->live = true; 324349cc55cSDimitry Andric 325fe6060f1SDimitry Andric StringRef thunkName = saver.save(funcSym->getName() + ".thunk." + 326fe6060f1SDimitry Andric std::to_string(thunkInfo.sequence++)); 327fe6060f1SDimitry Andric r.referent = thunkInfo.sym = symtab->addDefined( 328fe6060f1SDimitry Andric thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0, 329fe6060f1SDimitry Andric /*size=*/thunkSize, /*isWeakDef=*/false, /*isPrivateExtern=*/true, 330fe6060f1SDimitry Andric /*isThumb=*/false, /*isReferencedDynamically=*/false, 331349cc55cSDimitry Andric /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false); 332349cc55cSDimitry Andric thunkInfo.sym->used = true; 333fe6060f1SDimitry Andric target->populateThunk(thunkInfo.isec, funcSym); 334fe6060f1SDimitry Andric finalizeOne(thunkInfo.isec); 335fe6060f1SDimitry Andric thunks.push_back(thunkInfo.isec); 336fe6060f1SDimitry Andric ++thunkCount; 337fe6060f1SDimitry Andric } 338fe6060f1SDimitry Andric } 339fe6060f1SDimitry Andric size = isecAddr - addr; 340fe6060f1SDimitry Andric fileSize = isecFileOff - fileOff; 341fe6060f1SDimitry Andric 342fe6060f1SDimitry Andric log("thunks for " + parent->name + "," + name + 343fe6060f1SDimitry Andric ": funcs = " + std::to_string(thunkMap.size()) + 344fe6060f1SDimitry Andric ", relocs = " + std::to_string(relocCount) + 345fe6060f1SDimitry Andric ", all calls = " + std::to_string(callSiteCount) + 346fe6060f1SDimitry Andric ", thunk calls = " + std::to_string(thunkCallCount) + 347fe6060f1SDimitry Andric ", thunks = " + std::to_string(thunkCount)); 348fe6060f1SDimitry Andric } 349fe6060f1SDimitry Andric 350fe6060f1SDimitry Andric void ConcatOutputSection::writeTo(uint8_t *buf) const { 351fe6060f1SDimitry Andric // Merge input sections from thunk & ordinary vectors 352fe6060f1SDimitry Andric size_t i = 0, ie = inputs.size(); 353fe6060f1SDimitry Andric size_t t = 0, te = thunks.size(); 354fe6060f1SDimitry Andric while (i < ie || t < te) { 355349cc55cSDimitry Andric while (i < ie && (t == te || inputs[i]->empty() || 356fe6060f1SDimitry Andric inputs[i]->outSecOff < thunks[t]->outSecOff)) { 357fe6060f1SDimitry Andric inputs[i]->writeTo(buf + inputs[i]->outSecOff); 358fe6060f1SDimitry Andric ++i; 359fe6060f1SDimitry Andric } 360fe6060f1SDimitry Andric while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) { 361fe6060f1SDimitry Andric thunks[t]->writeTo(buf + thunks[t]->outSecOff); 362fe6060f1SDimitry Andric ++t; 363fe6060f1SDimitry Andric } 364fe6060f1SDimitry Andric } 365fe6060f1SDimitry Andric } 366fe6060f1SDimitry Andric 367fe6060f1SDimitry Andric void ConcatOutputSection::finalizeFlags(InputSection *input) { 368fe6060f1SDimitry Andric switch (sectionType(input->getFlags())) { 369fe6060f1SDimitry Andric default /*type-unspec'ed*/: 370fe6060f1SDimitry Andric // FIXME: Add additional logic here when supporting emitting obj files. 371fe6060f1SDimitry Andric break; 372fe6060f1SDimitry Andric case S_4BYTE_LITERALS: 373fe6060f1SDimitry Andric case S_8BYTE_LITERALS: 374fe6060f1SDimitry Andric case S_16BYTE_LITERALS: 375fe6060f1SDimitry Andric case S_CSTRING_LITERALS: 376fe6060f1SDimitry Andric case S_ZEROFILL: 377fe6060f1SDimitry Andric case S_LAZY_SYMBOL_POINTERS: 378fe6060f1SDimitry Andric case S_MOD_TERM_FUNC_POINTERS: 379fe6060f1SDimitry Andric case S_THREAD_LOCAL_REGULAR: 380fe6060f1SDimitry Andric case S_THREAD_LOCAL_ZEROFILL: 381fe6060f1SDimitry Andric case S_THREAD_LOCAL_VARIABLES: 382fe6060f1SDimitry Andric case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: 383fe6060f1SDimitry Andric case S_THREAD_LOCAL_VARIABLE_POINTERS: 384fe6060f1SDimitry Andric case S_NON_LAZY_SYMBOL_POINTERS: 385fe6060f1SDimitry Andric case S_SYMBOL_STUBS: 386fe6060f1SDimitry Andric flags |= input->getFlags(); 387fe6060f1SDimitry Andric break; 388fe6060f1SDimitry Andric } 389fe6060f1SDimitry Andric } 390fe6060f1SDimitry Andric 391fe6060f1SDimitry Andric ConcatOutputSection * 392fe6060f1SDimitry Andric ConcatOutputSection::getOrCreateForInput(const InputSection *isec) { 393fe6060f1SDimitry Andric NamePair names = maybeRenameSection({isec->getSegName(), isec->getName()}); 394fe6060f1SDimitry Andric ConcatOutputSection *&osec = concatOutputSections[names]; 395fe6060f1SDimitry Andric if (!osec) 396fe6060f1SDimitry Andric osec = make<ConcatOutputSection>(names.second); 397fe6060f1SDimitry Andric return osec; 398fe6060f1SDimitry Andric } 399fe6060f1SDimitry Andric 400fe6060f1SDimitry Andric NamePair macho::maybeRenameSection(NamePair key) { 401fe6060f1SDimitry Andric auto newNames = config->sectionRenameMap.find(key); 402fe6060f1SDimitry Andric if (newNames != config->sectionRenameMap.end()) 403fe6060f1SDimitry Andric return newNames->second; 404fe6060f1SDimitry Andric return key; 405fe6060f1SDimitry Andric } 406