annotate lld/MachO/ConcatOutputSection.cpp @ 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.cpp --------------------------------------------===//
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 #include "ConcatOutputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "Config.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "OutputSegment.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "SymbolTable.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "Symbols.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "SyntheticSections.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "lld/Common/ErrorHandler.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "lld/Common/Memory.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "llvm/BinaryFormat/MachO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/Support/ScopedPrinter.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include <algorithm>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 using namespace llvm::MachO;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 void ConcatOutputSection::addInput(InputSection *input) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 if (inputs.empty()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 align = input->align;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 flags = input->flags;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 align = std::max(align, input->align);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 mergeFlags(input);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 inputs.push_back(input);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 input->parent = this;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 // Branch-range extension can be implemented in two ways, either through ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 // (1) Branch islands: Single branch instructions (also of limited range),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 // that might be chained in multiple hops to reach the desired
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 // destination. On ARM64, as 16 branch islands are needed to hop between
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 // opposite ends of a 2 GiB program. LD64 uses branch islands exclusively,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 // even when it needs excessive hops.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 // (2) Thunks: Instruction(s) to load the destination address into a scratch
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 // register, followed by a register-indirect branch. Thunks are
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 // constructed to reach any arbitrary address, so need not be
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 // chained. Although thunks need not be chained, a program might need
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 // multiple thunks to the same destination distributed throughout a large
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 // program so that all call sites can have one within range.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 // The optimal approach is to mix islands for distinations within two hops,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 // and use thunks for destinations at greater distance. For now, we only
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 // implement thunks. TODO: Adding support for branch islands!
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 // Internally -- as expressed in LLD's data structures -- a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 // branch-range-extension thunk comprises ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 // (1) new Defined privateExtern symbol for the thunk named
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 // <FUNCTION>.thunk.<SEQUENCE>, which references ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 // (2) new InputSection, which contains ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 // (3.1) new data for the instructions to load & branch to the far address +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 // (3.2) new Relocs on instructions to load the far address, which reference ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 // (4.1) existing Defined extern symbol for the real function in __text, or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 // (4.2) existing DylibSymbol for the real function in a dylib
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 // Nearly-optimal thunk-placement algorithm features:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 // * Single pass: O(n) on the number of call sites.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 // * Accounts for the exact space overhead of thunks - no heuristics
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 // * Exploits the full range of call instructions - forward & backward
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 // Data:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 // * DenseMap<Symbol *, ThunkInfo> thunkMap: Maps the function symbol
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 // to its thunk bookkeeper.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 // * struct ThunkInfo (bookkeeper): Call instructions have limited range, and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 // distant call sites might be unable to reach the same thunk, so multiple
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 // thunks are necessary to serve all call sites in a very large program. A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 // thunkInfo stores state for all thunks associated with a particular
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 // function: (a) thunk symbol, (b) input section containing stub code, and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 // (c) sequence number for the active thunk incarnation. When an old thunk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 // goes out of range, we increment the sequence number and create a new
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 // thunk named <FUNCTION>.thunk.<SEQUENCE>.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 // * A thunk incarnation comprises (a) private-extern Defined symbol pointing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 // to (b) an InputSection holding machine instructions (similar to a MachO
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 // stub), and (c) Reloc(s) that reference the real function for fixing-up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 // the stub code.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 // * std::vector<InputSection *> MergedInputSection::thunks: A vector parallel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 // to the inputs vector. We store new thunks via cheap vector append, rather
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 // than costly insertion into the inputs vector.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 // Control Flow:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 // * During address assignment, MergedInputSection::finalize() examines call
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 // sites by ascending address and creates thunks. When a function is beyond
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 // the range of a call site, we need a thunk. Place it at the largest
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 // available forward address from the call site. Call sites increase
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 // monotonically and thunks are always placed as far forward as possible;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 // thus, we place thunks at monotonically increasing addresses. Once a thunk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 // is placed, it and all previous input-section addresses are final.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 // * MergedInputSection::finalize() and MergedInputSection::writeTo() merge
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 // the inputs and thunks vectors (both ordered by ascending address), which
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 // is simple and cheap.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 DenseMap<Symbol *, ThunkInfo> lld::macho::thunkMap;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 // Determine whether we need thunks, which depends on the target arch -- RISC
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 // (i.e., ARM) generally does because it has limited-range branch/call
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 // instructions, whereas CISC (i.e., x86) generally doesn't. RISC only needs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 // thunks for programs so large that branch source & destination addresses
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 // might differ more than the range of branch instruction(s).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 bool ConcatOutputSection::needsThunks() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 if (!target->usesThunks())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 uint64_t isecAddr = addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 for (InputSection *isec : inputs)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 isecAddr = alignTo(isecAddr, isec->align) + isec->getSize();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 if (isecAddr - addr + in.stubs->getSize() <= target->branchRange)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 // Yes, this program is large enough to need thunks.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 for (InputSection *isec : inputs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 for (Reloc &r : isec->relocs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 auto *sym = r.referent.get<Symbol *>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 // Pre-populate the thunkMap and memoize call site counts for every
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 // InputSection and ThunkInfo. We do this for the benefit of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 // ConcatOutputSection::estimateStubsInRangeVA()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 ThunkInfo &thunkInfo = thunkMap[sym];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 // Knowing ThunkInfo call site count will help us know whether or not we
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 // might need to create more for this referent at the time we are
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 // estimating distance to __stubs in .
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 ++thunkInfo.callSiteCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 // Knowing InputSection call site count will help us avoid work on those
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 // that have no BRANCH relocs.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 ++isec->callSiteCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 return true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 // Since __stubs is placed after __text, we must estimate the address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 // beyond which stubs are within range of a simple forward branch.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 uint64_t ConcatOutputSection::estimateStubsInRangeVA(size_t callIdx) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 uint64_t branchRange = target->branchRange;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 size_t endIdx = inputs.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 InputSection *isec = inputs[callIdx];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 uint64_t isecVA = isec->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 // Tally the non-stub functions which still have call sites
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 // remaining to process, which yields the maximum number
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 // of thunks we might yet place.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 size_t maxPotentialThunks = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 for (auto &tp : thunkMap) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 ThunkInfo &ti = tp.second;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 maxPotentialThunks +=
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 !tp.first->isInStubs() && ti.callSitesUsed < ti.callSiteCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 // Tally the total size of input sections remaining to process.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 uint64_t isecEnd = isec->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 for (size_t i = callIdx; i < endIdx; i++) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 InputSection *isec = inputs[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 isecEnd = alignTo(isecEnd, isec->align) + isec->getSize();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 // Estimate the address after which call sites can safely call stubs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 // directly rather than through intermediary thunks.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 uint64_t stubsInRangeVA = isecEnd + maxPotentialThunks * target->thunkSize +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 in.stubs->getSize() - branchRange;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 log("thunks = " + std::to_string(thunkMap.size()) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 ", potential = " + std::to_string(maxPotentialThunks) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 ", stubs = " + std::to_string(in.stubs->getSize()) + ", isecVA = " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 to_hexString(isecVA) + ", threshold = " + to_hexString(stubsInRangeVA) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 ", isecEnd = " + to_hexString(isecEnd) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 ", tail = " + to_hexString(isecEnd - isecVA) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 ", slop = " + to_hexString(branchRange - (isecEnd - isecVA)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 return stubsInRangeVA;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 void ConcatOutputSection::finalize() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 uint64_t isecAddr = addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 uint64_t isecFileOff = fileOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 auto finalizeOne = [&](InputSection *isec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 isecAddr = alignTo(isecAddr, isec->align);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 isecFileOff = alignTo(isecFileOff, isec->align);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 isec->outSecOff = isecAddr - addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 isec->outSecFileOff = isecFileOff - fileOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 isec->isFinal = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 isecAddr += isec->getSize();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 isecFileOff += isec->getFileSize();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 if (!needsThunks()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 for (InputSection *isec : inputs)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 finalizeOne(isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 size = isecAddr - addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 fileSize = isecFileOff - fileOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 uint64_t branchRange = target->branchRange;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 uint64_t stubsInRangeVA = TargetInfo::outOfRangeVA;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 size_t thunkSize = target->thunkSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 size_t relocCount = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 size_t callSiteCount = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 size_t thunkCallCount = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 size_t thunkCount = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 // inputs[finalIdx] is for finalization (address-assignment)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 size_t finalIdx = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 // Kick-off by ensuring that the first input section has an address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 for (size_t callIdx = 0, endIdx = inputs.size(); callIdx < endIdx;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 ++callIdx) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 if (finalIdx == callIdx)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 finalizeOne(inputs[finalIdx++]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 InputSection *isec = inputs[callIdx];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 assert(isec->isFinal);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 uint64_t isecVA = isec->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 // Assign addresses up-to the forward branch-range limit
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 while (finalIdx < endIdx &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 isecAddr + inputs[finalIdx]->getSize() < isecVA + branchRange)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 finalizeOne(inputs[finalIdx++]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 if (isec->callSiteCount == 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 if (finalIdx == endIdx && stubsInRangeVA == TargetInfo::outOfRangeVA) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 // When we have finalized all input sections, __stubs (destined
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 // to follow __text) comes within range of forward branches and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 // we can estimate the threshold address after which we can
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 // reach any stub with a forward branch. Note that although it
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 // sits in the middle of a loop, this code executes only once.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 // It is in the loop because we need to call it at the proper
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 // time: the earliest call site from which the end of __text
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 // (and start of __stubs) comes within range of a forward branch.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 stubsInRangeVA = estimateStubsInRangeVA(callIdx);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 // Process relocs by ascending address, i.e., ascending offset within isec
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 std::vector<Reloc> &relocs = isec->relocs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 assert(is_sorted(relocs,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 [](Reloc &a, Reloc &b) { return a.offset > b.offset; }));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 for (Reloc &r : reverse(relocs)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 ++relocCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 ++callSiteCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 // Calculate branch reachability boundaries
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 uint64_t callVA = isecVA + r.offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 uint64_t lowVA = branchRange < callVA ? callVA - branchRange : 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 uint64_t highVA = callVA + branchRange;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 // Calculate our call referent address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 auto *funcSym = r.referent.get<Symbol *>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 ThunkInfo &thunkInfo = thunkMap[funcSym];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 // The referent is not reachable, so we need to use a thunk ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 if (funcSym->isInStubs() && callVA >= stubsInRangeVA) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 // ... Oh, wait! We are close enough to the end that __stubs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 // are now within range of a simple forward branch.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 uint64_t funcVA = funcSym->resolveBranchVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 ++thunkInfo.callSitesUsed;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 if (lowVA < funcVA && funcVA < highVA) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 // The referent is reachable with a simple call instruction.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 ++thunkInfo.thunkCallCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 ++thunkCallCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 // If an existing thunk is reachable, use it ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 if (thunkInfo.sym) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 uint64_t thunkVA = thunkInfo.isec->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 if (lowVA < thunkVA && thunkVA < highVA) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 r.referent = thunkInfo.sym;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 // ... otherwise, create a new thunk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 if (isecAddr > highVA) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 // When there is small-to-no margin between highVA and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 // isecAddr and the distance between subsequent call sites is
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 // smaller than thunkSize, then a new thunk can go out of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 // range. Fix by unfinalizing inputs[finalIdx] to reduce the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 // distance between callVA and highVA, then shift some thunks
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 // to occupy address-space formerly occupied by the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 // unfinalized inputs[finalIdx].
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 thunkInfo.isec = make<InputSection>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 thunkInfo.isec->name = isec->name;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 thunkInfo.isec->segname = isec->segname;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 thunkInfo.isec->parent = this;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 StringRef thunkName = saver.save(funcSym->getName() + ".thunk." +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 std::to_string(thunkInfo.sequence++));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 r.referent = thunkInfo.sym = symtab->addDefined(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 thunkName, /*file=*/nullptr, thunkInfo.isec, /*value=*/0,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 /*size=*/thunkSize, /*isWeakDef=*/false, /*isPrivateExtern=*/true,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 /*isThumb=*/false, /*isReferencedDynamically=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 /*noDeadStrip=*/false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 target->populateThunk(thunkInfo.isec, funcSym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 finalizeOne(thunkInfo.isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 thunks.push_back(thunkInfo.isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 ++thunkCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 size = isecAddr - addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 fileSize = isecFileOff - fileOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 log("thunks for " + parent->name + "," + name +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 ": funcs = " + std::to_string(thunkMap.size()) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 ", relocs = " + std::to_string(relocCount) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 ", all calls = " + std::to_string(callSiteCount) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 ", thunk calls = " + std::to_string(thunkCallCount) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 ", thunks = " + std::to_string(thunkCount));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 void ConcatOutputSection::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 // Merge input sections from thunk & ordinary vectors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 size_t i = 0, ie = inputs.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 size_t t = 0, te = thunks.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 while (i < ie || t < te) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 while (i < ie && (t == te || inputs[i]->getSize() == 0 ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 inputs[i]->outSecOff < thunks[t]->outSecOff)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 inputs[i]->writeTo(buf + inputs[i]->outSecFileOff);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 ++i;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 while (t < te && (i == ie || thunks[t]->outSecOff < inputs[i]->outSecOff)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 thunks[t]->writeTo(buf + thunks[t]->outSecFileOff);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 ++t;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 // TODO: this is most likely wrong; reconsider how section flags
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 // are actually merged. The logic presented here was written without
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 // any form of informed research.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 void ConcatOutputSection::mergeFlags(InputSection *input) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 uint8_t baseType = flags & SECTION_TYPE;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 uint8_t inputType = input->flags & SECTION_TYPE;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 if (baseType != inputType)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 error("Cannot merge section " + input->name + " (type=0x" +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 to_hexString(inputType) + ") into " + name + " (type=0x" +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 to_hexString(baseType) + "): inconsistent types");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 constexpr uint32_t strictFlags = S_ATTR_DEBUG | S_ATTR_STRIP_STATIC_SYMS |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 S_ATTR_NO_DEAD_STRIP | S_ATTR_LIVE_SUPPORT;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 if ((input->flags ^ flags) & strictFlags)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 error("Cannot merge section " + input->name + " (flags=0x" +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 to_hexString(input->flags) + ") into " + name + " (flags=0x" +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 to_hexString(flags) + "): strict flags differ");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 // Negate pure instruction presence if any section isn't pure.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 uint32_t pureMask = ~S_ATTR_PURE_INSTRUCTIONS | (input->flags & flags);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 // Merge the rest
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 flags |= input->flags;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 flags &= pureMask;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 }