1fe6060f1SDimitry Andric //===- ConcatOutputSection.h ------------------------------------*- C++ -*-===// 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 981ad6265SDimitry Andric #ifndef LLD_MACHO_CONCAT_OUTPUT_SECTION_H 1081ad6265SDimitry Andric #define LLD_MACHO_CONCAT_OUTPUT_SECTION_H 11fe6060f1SDimitry Andric 12fe6060f1SDimitry Andric #include "InputSection.h" 13fe6060f1SDimitry Andric #include "OutputSection.h" 14fe6060f1SDimitry Andric #include "lld/Common/LLVM.h" 15fe6060f1SDimitry Andric #include "llvm/ADT/DenseMap.h" 16fe6060f1SDimitry Andric #include "llvm/ADT/MapVector.h" 17fe6060f1SDimitry Andric 18*bdd1243dSDimitry Andric namespace lld::macho { 19fe6060f1SDimitry Andric 20fe6060f1SDimitry Andric class Defined; 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric // Linking multiple files will inevitably mean resolving sections in different 23fe6060f1SDimitry Andric // files that are labeled with the same segment and section name. This class 24fe6060f1SDimitry Andric // contains all such sections and writes the data from each section sequentially 25fe6060f1SDimitry Andric // in the final binary. 2681ad6265SDimitry Andric class ConcatOutputSection : public OutputSection { 27fe6060f1SDimitry Andric public: ConcatOutputSection(StringRef name)28fe6060f1SDimitry Andric explicit ConcatOutputSection(StringRef name) 29fe6060f1SDimitry Andric : OutputSection(ConcatKind, name) {} 30fe6060f1SDimitry Andric firstSection()31fe6060f1SDimitry Andric const ConcatInputSection *firstSection() const { return inputs.front(); } lastSection()32fe6060f1SDimitry Andric const ConcatInputSection *lastSection() const { return inputs.back(); } isNeeded()33fe6060f1SDimitry Andric bool isNeeded() const override { return !inputs.empty(); } 34fe6060f1SDimitry Andric 35fe6060f1SDimitry Andric // These accessors will only be valid after finalizing the section getSize()36fe6060f1SDimitry Andric uint64_t getSize() const override { return size; } getFileSize()37fe6060f1SDimitry Andric uint64_t getFileSize() const override { return fileSize; } 38fe6060f1SDimitry Andric 3981ad6265SDimitry Andric // Assign values to InputSection::outSecOff. In contrast to TextOutputSection, 4081ad6265SDimitry Andric // which does this in its implementation of `finalize()`, we can do this 4181ad6265SDimitry Andric // without `finalize()`'s sequential guarantees detailed in the block comment 4281ad6265SDimitry Andric // of `OutputSection::finalize()`. 4381ad6265SDimitry Andric virtual void finalizeContents(); 4481ad6265SDimitry Andric 45fe6060f1SDimitry Andric void addInput(ConcatInputSection *input); 46fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override; 47fe6060f1SDimitry Andric classof(const OutputSection * sec)48fe6060f1SDimitry Andric static bool classof(const OutputSection *sec) { 49fe6060f1SDimitry Andric return sec->kind() == ConcatKind; 50fe6060f1SDimitry Andric } 51fe6060f1SDimitry Andric 52fe6060f1SDimitry Andric static ConcatOutputSection *getOrCreateForInput(const InputSection *); 53fe6060f1SDimitry Andric 5481ad6265SDimitry Andric std::vector<ConcatInputSection *> inputs; 55fe6060f1SDimitry Andric 5681ad6265SDimitry Andric protected: 57fe6060f1SDimitry Andric size_t size = 0; 58fe6060f1SDimitry Andric uint64_t fileSize = 0; 5981ad6265SDimitry Andric void finalizeOne(ConcatInputSection *); 6081ad6265SDimitry Andric 6181ad6265SDimitry Andric private: 6281ad6265SDimitry Andric void finalizeFlags(InputSection *input); 6381ad6265SDimitry Andric }; 6481ad6265SDimitry Andric 6581ad6265SDimitry Andric // ConcatOutputSections that contain code (text) require special handling to 6681ad6265SDimitry Andric // support thunk insertion. 6781ad6265SDimitry Andric class TextOutputSection : public ConcatOutputSection { 6881ad6265SDimitry Andric public: TextOutputSection(StringRef name)6981ad6265SDimitry Andric explicit TextOutputSection(StringRef name) : ConcatOutputSection(name) {} finalizeContents()7081ad6265SDimitry Andric void finalizeContents() override {} 7181ad6265SDimitry Andric void finalize() override; 7281ad6265SDimitry Andric bool needsThunks() const; 7381ad6265SDimitry Andric void writeTo(uint8_t *buf) const override; 7481ad6265SDimitry Andric 7581ad6265SDimitry Andric private: 7681ad6265SDimitry Andric uint64_t estimateStubsInRangeVA(size_t callIdx) const; 7781ad6265SDimitry Andric 7881ad6265SDimitry Andric std::vector<ConcatInputSection *> thunks; 79fe6060f1SDimitry Andric }; 80fe6060f1SDimitry Andric 81fe6060f1SDimitry Andric // We maintain one ThunkInfo per real function. 82fe6060f1SDimitry Andric // 83fe6060f1SDimitry Andric // The "active thunk" is represented by the sym/isec pair that 84fe6060f1SDimitry Andric // turns-over during finalize(): as the call-site address advances, 85fe6060f1SDimitry Andric // the active thunk goes out of branch-range, and we create a new 86fe6060f1SDimitry Andric // thunk to take its place. 87fe6060f1SDimitry Andric // 88fe6060f1SDimitry Andric // The remaining members -- bools and counters -- apply to the 89fe6060f1SDimitry Andric // collection of thunks associated with the real function. 90fe6060f1SDimitry Andric 91fe6060f1SDimitry Andric struct ThunkInfo { 92fe6060f1SDimitry Andric // These denote the active thunk: 93fe6060f1SDimitry Andric Defined *sym = nullptr; // private-extern symbol for active thunk 94fe6060f1SDimitry Andric ConcatInputSection *isec = nullptr; // input section for active thunk 95fe6060f1SDimitry Andric 96fe6060f1SDimitry Andric // The following values are cumulative across all thunks on this function 97fe6060f1SDimitry Andric uint32_t callSiteCount = 0; // how many calls to the real function? 98fe6060f1SDimitry Andric uint32_t callSitesUsed = 0; // how many call sites processed so-far? 99fe6060f1SDimitry Andric uint32_t thunkCallCount = 0; // how many call sites went to thunk? 100fe6060f1SDimitry Andric uint8_t sequence = 0; // how many thunks created so-far? 101fe6060f1SDimitry Andric }; 102fe6060f1SDimitry Andric 103fe6060f1SDimitry Andric NamePair maybeRenameSection(NamePair key); 104fe6060f1SDimitry Andric 105fe6060f1SDimitry Andric // Output sections are added to output segments in iteration order 106fe6060f1SDimitry Andric // of ConcatOutputSection, so must have deterministic iteration order. 107fe6060f1SDimitry Andric extern llvm::MapVector<NamePair, ConcatOutputSection *> concatOutputSections; 108fe6060f1SDimitry Andric 109fe6060f1SDimitry Andric extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap; 110fe6060f1SDimitry Andric 111*bdd1243dSDimitry Andric } // namespace lld::macho 112fe6060f1SDimitry Andric 113fe6060f1SDimitry Andric #endif 114