xref: /llvm-project/lld/MachO/ConcatOutputSection.h (revision 162814a7ec00e2c89693f96568b72956d1bf2f0f)
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::Kind kind = ConcatKind)
30       : OutputSection(kind, 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 || sec->kind() == TextKind;
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)
71       : ConcatOutputSection(name, TextKind) {}
72   void finalizeContents() override {}
73   void finalize() override;
74   bool needsThunks() const;
75   ArrayRef<ConcatInputSection *> getThunks() const { return thunks; }
76   void writeTo(uint8_t *buf) const override;
77 
78   static bool classof(const OutputSection *sec) {
79     return sec->kind() == TextKind;
80   }
81 
82 private:
83   uint64_t estimateStubsInRangeVA(size_t callIdx) const;
84 
85   std::vector<ConcatInputSection *> thunks;
86 };
87 
88 // We maintain one ThunkInfo per real function.
89 //
90 // The "active thunk" is represented by the sym/isec pair that
91 // turns-over during finalize(): as the call-site address advances,
92 // the active thunk goes out of branch-range, and we create a new
93 // thunk to take its place.
94 //
95 // The remaining members -- bools and counters -- apply to the
96 // collection of thunks associated with the real function.
97 
98 struct ThunkInfo {
99   // These denote the active thunk:
100   Defined *sym = nullptr;             // private-extern symbol for active thunk
101   ConcatInputSection *isec = nullptr; // input section for active thunk
102 
103   // The following values are cumulative across all thunks on this function
104   uint32_t callSiteCount = 0;  // how many calls to the real function?
105   uint32_t callSitesUsed = 0;  // how many call sites processed so-far?
106   uint32_t thunkCallCount = 0; // how many call sites went to thunk?
107   uint8_t sequence = 0;        // how many thunks created so-far?
108 };
109 
110 NamePair maybeRenameSection(NamePair key);
111 
112 // Output sections are added to output segments in iteration order
113 // of ConcatOutputSection, so must have deterministic iteration order.
114 extern llvm::MapVector<NamePair, ConcatOutputSection *> concatOutputSections;
115 
116 extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap;
117 
118 } // namespace lld::macho
119 
120 #endif
121