207
|
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 : public OutputSection {
|
|
28 public:
|
|
29 explicit ConcatOutputSection(StringRef name)
|
|
30 : OutputSection(ConcatKind, name) {}
|
|
31
|
|
32 const InputSection *firstSection() const { return inputs.front(); }
|
|
33 const InputSection *lastSection() const { return inputs.back(); }
|
|
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 void addInput(InputSection *input);
|
|
40 void finalize() override;
|
|
41 bool needsThunks() const;
|
|
42 uint64_t estimateStubsInRangeVA(size_t callIdx) const;
|
|
43
|
|
44 void writeTo(uint8_t *buf) const override;
|
|
45
|
|
46 std::vector<InputSection *> inputs;
|
|
47 std::vector<InputSection *> thunks;
|
|
48
|
|
49 static bool classof(const OutputSection *sec) {
|
|
50 return sec->kind() == ConcatKind;
|
|
51 }
|
|
52
|
|
53 private:
|
|
54 void mergeFlags(InputSection *input);
|
|
55
|
|
56 size_t size = 0;
|
|
57 uint64_t fileSize = 0;
|
|
58 };
|
|
59
|
|
60 // We maintain one ThunkInfo per real function.
|
|
61 //
|
|
62 // The "active thunk" is represented by the sym/isec pair that
|
|
63 // turns-over during finalize(): as the call-site address advances,
|
|
64 // the active thunk goes out of branch-range, and we create a new
|
|
65 // thunk to take its place.
|
|
66 //
|
|
67 // The remaining members -- bools and counters -- apply to the
|
|
68 // collection of thunks associated with the real function.
|
|
69
|
|
70 struct ThunkInfo {
|
|
71 // These denote the active thunk:
|
|
72 Defined *sym = nullptr; // private-extern symbol for active thunk
|
|
73 InputSection *isec = nullptr; // input section for active thunk
|
|
74
|
|
75 // The following values are cumulative across all thunks on this function
|
|
76 uint32_t callSiteCount = 0; // how many calls to the real function?
|
|
77 uint32_t callSitesUsed = 0; // how many call sites processed so-far?
|
|
78 uint32_t thunkCallCount = 0; // how many call sites went to thunk?
|
|
79 uint8_t sequence = 0; // how many thunks created so-far?
|
|
80 };
|
|
81
|
|
82 extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap;
|
|
83
|
|
84 } // namespace macho
|
|
85 } // namespace lld
|
|
86
|
|
87 #endif
|