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