annotate lld/MachO/ConcatOutputSection.h @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- ConcatOutputSection.h ------------------------------------*- C++ -*-===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #ifndef LLD_MACHO_MERGED_OUTPUT_SECTION_H
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #define LLD_MACHO_MERGED_OUTPUT_SECTION_H
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "InputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "OutputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "lld/Common/LLVM.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "llvm/ADT/DenseMap.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "llvm/ADT/MapVector.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 namespace lld {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 namespace macho {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 class Defined;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 // Linking multiple files will inevitably mean resolving sections in different
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 // files that are labeled with the same segment and section name. This class
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 // contains all such sections and writes the data from each section sequentially
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 // in the final binary.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 class ConcatOutputSection : public OutputSection {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 public:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 explicit ConcatOutputSection(StringRef name)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 : OutputSection(ConcatKind, name) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 const InputSection *firstSection() const { return inputs.front(); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 const InputSection *lastSection() const { return inputs.back(); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 // These accessors will only be valid after finalizing the section
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 uint64_t getSize() const override { return size; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 uint64_t getFileSize() const override { return fileSize; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 void addInput(InputSection *input);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 void finalize() override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 bool needsThunks() const;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 uint64_t estimateStubsInRangeVA(size_t callIdx) const;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 void writeTo(uint8_t *buf) const override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 std::vector<InputSection *> inputs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 std::vector<InputSection *> thunks;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 static bool classof(const OutputSection *sec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 return sec->kind() == ConcatKind;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 private:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 void mergeFlags(InputSection *input);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 size_t size = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 uint64_t fileSize = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 // We maintain one ThunkInfo per real function.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 // The "active thunk" is represented by the sym/isec pair that
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 // turns-over during finalize(): as the call-site address advances,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 // the active thunk goes out of branch-range, and we create a new
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 // thunk to take its place.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 // The remaining members -- bools and counters -- apply to the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 // collection of thunks associated with the real function.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 struct ThunkInfo {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 // These denote the active thunk:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 Defined *sym = nullptr; // private-extern symbol for active thunk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 InputSection *isec = nullptr; // input section for active thunk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 // The following values are cumulative across all thunks on this function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 uint32_t callSiteCount = 0; // how many calls to the real function?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 uint32_t callSitesUsed = 0; // how many call sites processed so-far?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 uint32_t thunkCallCount = 0; // how many call sites went to thunk?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 uint8_t sequence = 0; // how many thunks created so-far?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 } // namespace macho
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 } // namespace lld
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 #endif