xref: /freebsd/contrib/llvm-project/lld/MachO/ConcatOutputSection.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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_MERGED_OUTPUT_SECTION_H
10 #define LLD_MACHO_MERGED_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 final : 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   void addInput(ConcatInputSection *input);
41   void finalize() override;
42   bool needsThunks() const;
43   uint64_t estimateStubsInRangeVA(size_t callIdx) const;
44 
45   void writeTo(uint8_t *buf) const override;
46 
47   std::vector<ConcatInputSection *> inputs;
48   std::vector<ConcatInputSection *> thunks;
49 
50   static bool classof(const OutputSection *sec) {
51     return sec->kind() == ConcatKind;
52   }
53 
54   static ConcatOutputSection *getOrCreateForInput(const InputSection *);
55 
56 private:
57   void finalizeFlags(InputSection *input);
58 
59   size_t size = 0;
60   uint64_t fileSize = 0;
61 };
62 
63 // We maintain one ThunkInfo per real function.
64 //
65 // The "active thunk" is represented by the sym/isec pair that
66 // turns-over during finalize(): as the call-site address advances,
67 // the active thunk goes out of branch-range, and we create a new
68 // thunk to take its place.
69 //
70 // The remaining members -- bools and counters -- apply to the
71 // collection of thunks associated with the real function.
72 
73 struct ThunkInfo {
74   // These denote the active thunk:
75   Defined *sym = nullptr;             // private-extern symbol for active thunk
76   ConcatInputSection *isec = nullptr; // input section for active thunk
77 
78   // The following values are cumulative across all thunks on this function
79   uint32_t callSiteCount = 0;  // how many calls to the real function?
80   uint32_t callSitesUsed = 0;  // how many call sites processed so-far?
81   uint32_t thunkCallCount = 0; // how many call sites went to thunk?
82   uint8_t sequence = 0;        // how many thunks created so-far?
83 };
84 
85 NamePair maybeRenameSection(NamePair key);
86 
87 // Output sections are added to output segments in iteration order
88 // of ConcatOutputSection, so must have deterministic iteration order.
89 extern llvm::MapVector<NamePair, ConcatOutputSection *> concatOutputSections;
90 
91 extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap;
92 
93 } // namespace macho
94 } // namespace lld
95 
96 #endif
97