1*fe6060f1SDimitry Andric //===- ConcatOutputSection.h ------------------------------------*- C++ -*-===// 2*fe6060f1SDimitry Andric // 3*fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*fe6060f1SDimitry Andric // 7*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8*fe6060f1SDimitry Andric 9*fe6060f1SDimitry Andric #ifndef LLD_MACHO_MERGED_OUTPUT_SECTION_H 10*fe6060f1SDimitry Andric #define LLD_MACHO_MERGED_OUTPUT_SECTION_H 11*fe6060f1SDimitry Andric 12*fe6060f1SDimitry Andric #include "InputSection.h" 13*fe6060f1SDimitry Andric #include "OutputSection.h" 14*fe6060f1SDimitry Andric #include "lld/Common/LLVM.h" 15*fe6060f1SDimitry Andric #include "llvm/ADT/DenseMap.h" 16*fe6060f1SDimitry Andric #include "llvm/ADT/MapVector.h" 17*fe6060f1SDimitry Andric 18*fe6060f1SDimitry Andric namespace lld { 19*fe6060f1SDimitry Andric namespace macho { 20*fe6060f1SDimitry Andric 21*fe6060f1SDimitry Andric class Defined; 22*fe6060f1SDimitry Andric 23*fe6060f1SDimitry Andric // Linking multiple files will inevitably mean resolving sections in different 24*fe6060f1SDimitry Andric // files that are labeled with the same segment and section name. This class 25*fe6060f1SDimitry Andric // contains all such sections and writes the data from each section sequentially 26*fe6060f1SDimitry Andric // in the final binary. 27*fe6060f1SDimitry Andric class ConcatOutputSection final : public OutputSection { 28*fe6060f1SDimitry Andric public: 29*fe6060f1SDimitry Andric explicit ConcatOutputSection(StringRef name) 30*fe6060f1SDimitry Andric : OutputSection(ConcatKind, name) {} 31*fe6060f1SDimitry Andric 32*fe6060f1SDimitry Andric const ConcatInputSection *firstSection() const { return inputs.front(); } 33*fe6060f1SDimitry Andric const ConcatInputSection *lastSection() const { return inputs.back(); } 34*fe6060f1SDimitry Andric bool isNeeded() const override { return !inputs.empty(); } 35*fe6060f1SDimitry Andric 36*fe6060f1SDimitry Andric // These accessors will only be valid after finalizing the section 37*fe6060f1SDimitry Andric uint64_t getSize() const override { return size; } 38*fe6060f1SDimitry Andric uint64_t getFileSize() const override { return fileSize; } 39*fe6060f1SDimitry Andric 40*fe6060f1SDimitry Andric void addInput(ConcatInputSection *input); 41*fe6060f1SDimitry Andric void finalize() override; 42*fe6060f1SDimitry Andric bool needsThunks() const; 43*fe6060f1SDimitry Andric uint64_t estimateStubsInRangeVA(size_t callIdx) const; 44*fe6060f1SDimitry Andric 45*fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override; 46*fe6060f1SDimitry Andric 47*fe6060f1SDimitry Andric std::vector<ConcatInputSection *> inputs; 48*fe6060f1SDimitry Andric std::vector<ConcatInputSection *> thunks; 49*fe6060f1SDimitry Andric 50*fe6060f1SDimitry Andric static bool classof(const OutputSection *sec) { 51*fe6060f1SDimitry Andric return sec->kind() == ConcatKind; 52*fe6060f1SDimitry Andric } 53*fe6060f1SDimitry Andric 54*fe6060f1SDimitry Andric static ConcatOutputSection *getOrCreateForInput(const InputSection *); 55*fe6060f1SDimitry Andric 56*fe6060f1SDimitry Andric private: 57*fe6060f1SDimitry Andric void finalizeFlags(InputSection *input); 58*fe6060f1SDimitry Andric 59*fe6060f1SDimitry Andric size_t size = 0; 60*fe6060f1SDimitry Andric uint64_t fileSize = 0; 61*fe6060f1SDimitry Andric }; 62*fe6060f1SDimitry Andric 63*fe6060f1SDimitry Andric // We maintain one ThunkInfo per real function. 64*fe6060f1SDimitry Andric // 65*fe6060f1SDimitry Andric // The "active thunk" is represented by the sym/isec pair that 66*fe6060f1SDimitry Andric // turns-over during finalize(): as the call-site address advances, 67*fe6060f1SDimitry Andric // the active thunk goes out of branch-range, and we create a new 68*fe6060f1SDimitry Andric // thunk to take its place. 69*fe6060f1SDimitry Andric // 70*fe6060f1SDimitry Andric // The remaining members -- bools and counters -- apply to the 71*fe6060f1SDimitry Andric // collection of thunks associated with the real function. 72*fe6060f1SDimitry Andric 73*fe6060f1SDimitry Andric struct ThunkInfo { 74*fe6060f1SDimitry Andric // These denote the active thunk: 75*fe6060f1SDimitry Andric Defined *sym = nullptr; // private-extern symbol for active thunk 76*fe6060f1SDimitry Andric ConcatInputSection *isec = nullptr; // input section for active thunk 77*fe6060f1SDimitry Andric 78*fe6060f1SDimitry Andric // The following values are cumulative across all thunks on this function 79*fe6060f1SDimitry Andric uint32_t callSiteCount = 0; // how many calls to the real function? 80*fe6060f1SDimitry Andric uint32_t callSitesUsed = 0; // how many call sites processed so-far? 81*fe6060f1SDimitry Andric uint32_t thunkCallCount = 0; // how many call sites went to thunk? 82*fe6060f1SDimitry Andric uint8_t sequence = 0; // how many thunks created so-far? 83*fe6060f1SDimitry Andric }; 84*fe6060f1SDimitry Andric 85*fe6060f1SDimitry Andric NamePair maybeRenameSection(NamePair key); 86*fe6060f1SDimitry Andric 87*fe6060f1SDimitry Andric // Output sections are added to output segments in iteration order 88*fe6060f1SDimitry Andric // of ConcatOutputSection, so must have deterministic iteration order. 89*fe6060f1SDimitry Andric extern llvm::MapVector<NamePair, ConcatOutputSection *> concatOutputSections; 90*fe6060f1SDimitry Andric 91*fe6060f1SDimitry Andric extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap; 92*fe6060f1SDimitry Andric 93*fe6060f1SDimitry Andric } // namespace macho 94*fe6060f1SDimitry Andric } // namespace lld 95*fe6060f1SDimitry Andric 96*fe6060f1SDimitry Andric #endif 97