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