annotate lld/ELF/Relocations.cpp @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 5f17cb93ff66
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- Relocations.cpp ----------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file contains platform-independent functions to process relocations.
anatofuz
parents:
diff changeset
10 // I'll describe the overview of this file here.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 // Simple relocations are easy to handle for the linker. For example,
anatofuz
parents:
diff changeset
13 // for R_X86_64_PC64 relocs, the linker just has to fix up locations
anatofuz
parents:
diff changeset
14 // with the relative offsets to the target symbols. It would just be
anatofuz
parents:
diff changeset
15 // reading records from relocation sections and applying them to output.
anatofuz
parents:
diff changeset
16 //
anatofuz
parents:
diff changeset
17 // But not all relocations are that easy to handle. For example, for
anatofuz
parents:
diff changeset
18 // R_386_GOTOFF relocs, the linker has to create new GOT entries for
anatofuz
parents:
diff changeset
19 // symbols if they don't exist, and fix up locations with GOT entry
anatofuz
parents:
diff changeset
20 // offsets from the beginning of GOT section. So there is more than
anatofuz
parents:
diff changeset
21 // fixing addresses in relocation processing.
anatofuz
parents:
diff changeset
22 //
anatofuz
parents:
diff changeset
23 // ELF defines a large number of complex relocations.
anatofuz
parents:
diff changeset
24 //
anatofuz
parents:
diff changeset
25 // The functions in this file analyze relocations and do whatever needs
anatofuz
parents:
diff changeset
26 // to be done. It includes, but not limited to, the following.
anatofuz
parents:
diff changeset
27 //
anatofuz
parents:
diff changeset
28 // - create GOT/PLT entries
anatofuz
parents:
diff changeset
29 // - create new relocations in .dynsym to let the dynamic linker resolve
anatofuz
parents:
diff changeset
30 // them at runtime (since ELF supports dynamic linking, not all
anatofuz
parents:
diff changeset
31 // relocations can be resolved at link-time)
anatofuz
parents:
diff changeset
32 // - create COPY relocs and reserve space in .bss
anatofuz
parents:
diff changeset
33 // - replace expensive relocs (in terms of runtime cost) with cheap ones
anatofuz
parents:
diff changeset
34 // - error out infeasible combinations such as PIC and non-relative relocs
anatofuz
parents:
diff changeset
35 //
anatofuz
parents:
diff changeset
36 // Note that the functions in this file don't actually apply relocations
anatofuz
parents:
diff changeset
37 // because it doesn't know about the output file nor the output file buffer.
anatofuz
parents:
diff changeset
38 // It instead stores Relocation objects to InputSection's Relocations
anatofuz
parents:
diff changeset
39 // vector to let it apply later in InputSection::writeTo.
anatofuz
parents:
diff changeset
40 //
anatofuz
parents:
diff changeset
41 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 #include "Relocations.h"
anatofuz
parents:
diff changeset
44 #include "Config.h"
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
45 #include "InputFiles.h"
150
anatofuz
parents:
diff changeset
46 #include "LinkerScript.h"
anatofuz
parents:
diff changeset
47 #include "OutputSections.h"
anatofuz
parents:
diff changeset
48 #include "SymbolTable.h"
anatofuz
parents:
diff changeset
49 #include "Symbols.h"
anatofuz
parents:
diff changeset
50 #include "SyntheticSections.h"
anatofuz
parents:
diff changeset
51 #include "Target.h"
anatofuz
parents:
diff changeset
52 #include "Thunks.h"
anatofuz
parents:
diff changeset
53 #include "lld/Common/ErrorHandler.h"
anatofuz
parents:
diff changeset
54 #include "lld/Common/Memory.h"
anatofuz
parents:
diff changeset
55 #include "llvm/ADT/SmallSet.h"
anatofuz
parents:
diff changeset
56 #include "llvm/Demangle/Demangle.h"
anatofuz
parents:
diff changeset
57 #include "llvm/Support/Endian.h"
anatofuz
parents:
diff changeset
58 #include <algorithm>
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 using namespace llvm;
anatofuz
parents:
diff changeset
61 using namespace llvm::ELF;
anatofuz
parents:
diff changeset
62 using namespace llvm::object;
anatofuz
parents:
diff changeset
63 using namespace llvm::support::endian;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
64 using namespace lld;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
65 using namespace lld::elf;
150
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
68 for (SectionCommand *cmd : script->sectionCommands)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
69 if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
70 if (assign->sym == &sym)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
71 return assign->location;
150
anatofuz
parents:
diff changeset
72 return None;
anatofuz
parents:
diff changeset
73 }
anatofuz
parents:
diff changeset
74
anatofuz
parents:
diff changeset
75 static std::string getDefinedLocation(const Symbol &sym) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
76 const char msg[] = "\n>>> defined in ";
150
anatofuz
parents:
diff changeset
77 if (sym.file)
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
78 return msg + toString(sym.file);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
79 if (Optional<std::string> loc = getLinkerScriptLocation(sym))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
80 return msg + *loc;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
81 return "";
150
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 // Construct a message in the following format.
anatofuz
parents:
diff changeset
85 //
anatofuz
parents:
diff changeset
86 // >>> defined in /home/alice/src/foo.o
anatofuz
parents:
diff changeset
87 // >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
anatofuz
parents:
diff changeset
88 // >>> /home/alice/src/bar.o:(.text+0x1)
anatofuz
parents:
diff changeset
89 static std::string getLocation(InputSectionBase &s, const Symbol &sym,
anatofuz
parents:
diff changeset
90 uint64_t off) {
anatofuz
parents:
diff changeset
91 std::string msg = getDefinedLocation(sym) + "\n>>> referenced by ";
anatofuz
parents:
diff changeset
92 std::string src = s.getSrcMsg(sym, off);
anatofuz
parents:
diff changeset
93 if (!src.empty())
anatofuz
parents:
diff changeset
94 msg += src + "\n>>> ";
anatofuz
parents:
diff changeset
95 return msg + s.getObjMsg(off);
anatofuz
parents:
diff changeset
96 }
anatofuz
parents:
diff changeset
97
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
98 void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
99 int64_t min, uint64_t max) {
150
anatofuz
parents:
diff changeset
100 ErrorPlace errPlace = getErrorPlace(loc);
anatofuz
parents:
diff changeset
101 std::string hint;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
102 if (rel.sym && !rel.sym->isSection())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
103 hint = "; references " + lld::toString(*rel.sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
104 if (!errPlace.srcLoc.empty())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
105 hint += "\n>>> referenced by " + errPlace.srcLoc;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
106 if (rel.sym && !rel.sym->isSection())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
107 hint += getDefinedLocation(*rel.sym);
150
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 if (errPlace.isec && errPlace.isec->name.startswith(".debug"))
anatofuz
parents:
diff changeset
110 hint += "; consider recompiling with -fdebug-types-section to reduce size "
anatofuz
parents:
diff changeset
111 "of debug sections";
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) +
anatofuz
parents:
diff changeset
114 " out of range: " + v.str() + " is not in [" + Twine(min).str() +
anatofuz
parents:
diff changeset
115 ", " + Twine(max).str() + "]" + hint);
anatofuz
parents:
diff changeset
116 }
anatofuz
parents:
diff changeset
117
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
118 void elf::reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
119 const Twine &msg) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
120 ErrorPlace errPlace = getErrorPlace(loc);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
121 std::string hint;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
122 if (!sym.getName().empty())
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
123 hint = "; references " + lld::toString(sym) + getDefinedLocation(sym);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
124 errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) +
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
125 " is not in [" + Twine(llvm::minIntN(n)) + ", " +
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
126 Twine(llvm::maxIntN(n)) + "]" + hint);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
127 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
128
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
129 // Build a bitmask with one bit set for each 64 subset of RelExpr.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
130 static constexpr uint64_t buildMask() { return 0; }
150
anatofuz
parents:
diff changeset
131
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
132 template <typename... Tails>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
133 static constexpr uint64_t buildMask(int head, Tails... tails) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
134 return (0 <= head && head < 64 ? uint64_t(1) << head : 0) |
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
135 buildMask(tails...);
150
anatofuz
parents:
diff changeset
136 }
anatofuz
parents:
diff changeset
137
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
138 // Return true if `Expr` is one of `Exprs`.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
139 // There are more than 64 but less than 128 RelExprs, so we divide the set of
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
140 // exprs into [0, 64) and [64, 128) and represent each range as a constant
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
141 // 64-bit mask. Then we decide which mask to test depending on the value of
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
142 // expr and use a simple shift and bitwise-and to test for membership.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
143 template <RelExpr... Exprs> static bool oneof(RelExpr expr) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
144 assert(0 <= expr && (int)expr < 128 &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
145 "RelExpr is too large for 128-bit mask!");
150
anatofuz
parents:
diff changeset
146
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
147 if (expr >= 64)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
148 return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
149 return (uint64_t(1) << expr) & buildMask(Exprs...);
150
anatofuz
parents:
diff changeset
150 }
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 static RelType getMipsPairType(RelType type, bool isLocal) {
anatofuz
parents:
diff changeset
153 switch (type) {
anatofuz
parents:
diff changeset
154 case R_MIPS_HI16:
anatofuz
parents:
diff changeset
155 return R_MIPS_LO16;
anatofuz
parents:
diff changeset
156 case R_MIPS_GOT16:
anatofuz
parents:
diff changeset
157 // In case of global symbol, the R_MIPS_GOT16 relocation does not
anatofuz
parents:
diff changeset
158 // have a pair. Each global symbol has a unique entry in the GOT
anatofuz
parents:
diff changeset
159 // and a corresponding instruction with help of the R_MIPS_GOT16
anatofuz
parents:
diff changeset
160 // relocation loads an address of the symbol. In case of local
anatofuz
parents:
diff changeset
161 // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
anatofuz
parents:
diff changeset
162 // the high 16 bits of the symbol's value. A paired R_MIPS_LO16
anatofuz
parents:
diff changeset
163 // relocations handle low 16 bits of the address. That allows
anatofuz
parents:
diff changeset
164 // to allocate only one GOT entry for every 64 KBytes of local data.
anatofuz
parents:
diff changeset
165 return isLocal ? R_MIPS_LO16 : R_MIPS_NONE;
anatofuz
parents:
diff changeset
166 case R_MICROMIPS_GOT16:
anatofuz
parents:
diff changeset
167 return isLocal ? R_MICROMIPS_LO16 : R_MIPS_NONE;
anatofuz
parents:
diff changeset
168 case R_MIPS_PCHI16:
anatofuz
parents:
diff changeset
169 return R_MIPS_PCLO16;
anatofuz
parents:
diff changeset
170 case R_MICROMIPS_HI16:
anatofuz
parents:
diff changeset
171 return R_MICROMIPS_LO16;
anatofuz
parents:
diff changeset
172 default:
anatofuz
parents:
diff changeset
173 return R_MIPS_NONE;
anatofuz
parents:
diff changeset
174 }
anatofuz
parents:
diff changeset
175 }
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 // True if non-preemptable symbol always has the same value regardless of where
anatofuz
parents:
diff changeset
178 // the DSO is loaded.
anatofuz
parents:
diff changeset
179 static bool isAbsolute(const Symbol &sym) {
anatofuz
parents:
diff changeset
180 if (sym.isUndefWeak())
anatofuz
parents:
diff changeset
181 return true;
anatofuz
parents:
diff changeset
182 if (const auto *dr = dyn_cast<Defined>(&sym))
anatofuz
parents:
diff changeset
183 return dr->section == nullptr; // Absolute symbol.
anatofuz
parents:
diff changeset
184 return false;
anatofuz
parents:
diff changeset
185 }
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 static bool isAbsoluteValue(const Symbol &sym) {
anatofuz
parents:
diff changeset
188 return isAbsolute(sym) || sym.isTls();
anatofuz
parents:
diff changeset
189 }
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 // Returns true if Expr refers a PLT entry.
anatofuz
parents:
diff changeset
192 static bool needsPlt(RelExpr expr) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
193 return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT>(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
194 expr);
150
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 // Returns true if Expr refers a GOT entry. Note that this function
anatofuz
parents:
diff changeset
198 // returns false for TLS variables even though they need GOT, because
anatofuz
parents:
diff changeset
199 // TLS variables uses GOT differently than the regular variables.
anatofuz
parents:
diff changeset
200 static bool needsGot(RelExpr expr) {
anatofuz
parents:
diff changeset
201 return oneof<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
202 R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
203 R_AARCH64_GOT_PAGE>(expr);
150
anatofuz
parents:
diff changeset
204 }
anatofuz
parents:
diff changeset
205
anatofuz
parents:
diff changeset
206 // True if this expression is of the form Sym - X, where X is a position in the
anatofuz
parents:
diff changeset
207 // file (PC, or GOT for example).
anatofuz
parents:
diff changeset
208 static bool isRelExpr(RelExpr expr) {
anatofuz
parents:
diff changeset
209 return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL,
anatofuz
parents:
diff changeset
210 R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
211 R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr);
150
anatofuz
parents:
diff changeset
212 }
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 static RelExpr toPlt(RelExpr expr) {
anatofuz
parents:
diff changeset
216 switch (expr) {
anatofuz
parents:
diff changeset
217 case R_PPC64_CALL:
anatofuz
parents:
diff changeset
218 return R_PPC64_CALL_PLT;
anatofuz
parents:
diff changeset
219 case R_PC:
anatofuz
parents:
diff changeset
220 return R_PLT_PC;
anatofuz
parents:
diff changeset
221 case R_ABS:
anatofuz
parents:
diff changeset
222 return R_PLT;
anatofuz
parents:
diff changeset
223 default:
anatofuz
parents:
diff changeset
224 return expr;
anatofuz
parents:
diff changeset
225 }
anatofuz
parents:
diff changeset
226 }
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 static RelExpr fromPlt(RelExpr expr) {
anatofuz
parents:
diff changeset
229 // We decided not to use a plt. Optimize a reference to the plt to a
anatofuz
parents:
diff changeset
230 // reference to the symbol itself.
anatofuz
parents:
diff changeset
231 switch (expr) {
anatofuz
parents:
diff changeset
232 case R_PLT_PC:
anatofuz
parents:
diff changeset
233 case R_PPC32_PLTREL:
anatofuz
parents:
diff changeset
234 return R_PC;
anatofuz
parents:
diff changeset
235 case R_PPC64_CALL_PLT:
anatofuz
parents:
diff changeset
236 return R_PPC64_CALL;
anatofuz
parents:
diff changeset
237 case R_PLT:
anatofuz
parents:
diff changeset
238 return R_ABS;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
239 case R_PLT_GOTPLT:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
240 return R_GOTPLTREL;
150
anatofuz
parents:
diff changeset
241 default:
anatofuz
parents:
diff changeset
242 return expr;
anatofuz
parents:
diff changeset
243 }
anatofuz
parents:
diff changeset
244 }
anatofuz
parents:
diff changeset
245
anatofuz
parents:
diff changeset
246 // Returns true if a given shared symbol is in a read-only segment in a DSO.
anatofuz
parents:
diff changeset
247 template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
anatofuz
parents:
diff changeset
248 using Elf_Phdr = typename ELFT::Phdr;
anatofuz
parents:
diff changeset
249
anatofuz
parents:
diff changeset
250 // Determine if the symbol is read-only by scanning the DSO's program headers.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
251 const auto &file = cast<SharedFile>(*ss.file);
150
anatofuz
parents:
diff changeset
252 for (const Elf_Phdr &phdr :
anatofuz
parents:
diff changeset
253 check(file.template getObj<ELFT>().program_headers()))
anatofuz
parents:
diff changeset
254 if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) &&
anatofuz
parents:
diff changeset
255 !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr &&
anatofuz
parents:
diff changeset
256 ss.value < phdr.p_vaddr + phdr.p_memsz)
anatofuz
parents:
diff changeset
257 return true;
anatofuz
parents:
diff changeset
258 return false;
anatofuz
parents:
diff changeset
259 }
anatofuz
parents:
diff changeset
260
anatofuz
parents:
diff changeset
261 // Returns symbols at the same offset as a given symbol, including SS itself.
anatofuz
parents:
diff changeset
262 //
anatofuz
parents:
diff changeset
263 // If two or more symbols are at the same offset, and at least one of
anatofuz
parents:
diff changeset
264 // them are copied by a copy relocation, all of them need to be copied.
anatofuz
parents:
diff changeset
265 // Otherwise, they would refer to different places at runtime.
anatofuz
parents:
diff changeset
266 template <class ELFT>
anatofuz
parents:
diff changeset
267 static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
anatofuz
parents:
diff changeset
268 using Elf_Sym = typename ELFT::Sym;
anatofuz
parents:
diff changeset
269
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
270 const auto &file = cast<SharedFile>(*ss.file);
150
anatofuz
parents:
diff changeset
271
anatofuz
parents:
diff changeset
272 SmallSet<SharedSymbol *, 4> ret;
anatofuz
parents:
diff changeset
273 for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
anatofuz
parents:
diff changeset
274 if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
anatofuz
parents:
diff changeset
275 s.getType() == STT_TLS || s.st_value != ss.value)
anatofuz
parents:
diff changeset
276 continue;
anatofuz
parents:
diff changeset
277 StringRef name = check(s.getName(file.getStringTable()));
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
278 Symbol *sym = symtab.find(name);
150
anatofuz
parents:
diff changeset
279 if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
anatofuz
parents:
diff changeset
280 ret.insert(alias);
anatofuz
parents:
diff changeset
281 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
282
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
283 // The loop does not check SHT_GNU_verneed, so ret does not contain
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
284 // non-default version symbols. If ss has a non-default version, ret won't
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
285 // contain ss. Just add ss unconditionally. If a non-default version alias is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
286 // separately copy relocated, it and ss will have different addresses.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
287 // Fortunately this case is impractical and fails with GNU ld as well.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
288 ret.insert(&ss);
150
anatofuz
parents:
diff changeset
289 return ret;
anatofuz
parents:
diff changeset
290 }
anatofuz
parents:
diff changeset
291
anatofuz
parents:
diff changeset
292 // When a symbol is copy relocated or we create a canonical plt entry, it is
anatofuz
parents:
diff changeset
293 // effectively a defined symbol. In the case of copy relocation the symbol is
anatofuz
parents:
diff changeset
294 // in .bss and in the case of a canonical plt entry it is in .plt. This function
anatofuz
parents:
diff changeset
295 // replaces the existing symbol with a Defined pointing to the appropriate
anatofuz
parents:
diff changeset
296 // location.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
297 static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value,
150
anatofuz
parents:
diff changeset
298 uint64_t size) {
anatofuz
parents:
diff changeset
299 Symbol old = sym;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
300 Defined(sym.file, StringRef(), sym.binding, sym.stOther, sym.type, value,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
301 size, &sec)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
302 .overwrite(sym);
150
anatofuz
parents:
diff changeset
303
anatofuz
parents:
diff changeset
304 sym.verdefIndex = old.verdefIndex;
anatofuz
parents:
diff changeset
305 sym.exportDynamic = true;
anatofuz
parents:
diff changeset
306 sym.isUsedInRegularObj = true;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
307 // A copy relocated alias may need a GOT entry.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
308 sym.flags.store(old.flags.load(std::memory_order_relaxed) & NEEDS_GOT,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
309 std::memory_order_relaxed);
150
anatofuz
parents:
diff changeset
310 }
anatofuz
parents:
diff changeset
311
anatofuz
parents:
diff changeset
312 // Reserve space in .bss or .bss.rel.ro for copy relocation.
anatofuz
parents:
diff changeset
313 //
anatofuz
parents:
diff changeset
314 // The copy relocation is pretty much a hack. If you use a copy relocation
anatofuz
parents:
diff changeset
315 // in your program, not only the symbol name but the symbol's size, RW/RO
anatofuz
parents:
diff changeset
316 // bit and alignment become part of the ABI. In addition to that, if the
anatofuz
parents:
diff changeset
317 // symbol has aliases, the aliases become part of the ABI. That's subtle,
anatofuz
parents:
diff changeset
318 // but if you violate that implicit ABI, that can cause very counter-
anatofuz
parents:
diff changeset
319 // intuitive consequences.
anatofuz
parents:
diff changeset
320 //
anatofuz
parents:
diff changeset
321 // So, what is the copy relocation? It's for linking non-position
anatofuz
parents:
diff changeset
322 // independent code to DSOs. In an ideal world, all references to data
anatofuz
parents:
diff changeset
323 // exported by DSOs should go indirectly through GOT. But if object files
anatofuz
parents:
diff changeset
324 // are compiled as non-PIC, all data references are direct. There is no
anatofuz
parents:
diff changeset
325 // way for the linker to transform the code to use GOT, as machine
anatofuz
parents:
diff changeset
326 // instructions are already set in stone in object files. This is where
anatofuz
parents:
diff changeset
327 // the copy relocation takes a role.
anatofuz
parents:
diff changeset
328 //
anatofuz
parents:
diff changeset
329 // A copy relocation instructs the dynamic linker to copy data from a DSO
anatofuz
parents:
diff changeset
330 // to a specified address (which is usually in .bss) at load-time. If the
anatofuz
parents:
diff changeset
331 // static linker (that's us) finds a direct data reference to a DSO
anatofuz
parents:
diff changeset
332 // symbol, it creates a copy relocation, so that the symbol can be
anatofuz
parents:
diff changeset
333 // resolved as if it were in .bss rather than in a DSO.
anatofuz
parents:
diff changeset
334 //
anatofuz
parents:
diff changeset
335 // As you can see in this function, we create a copy relocation for the
anatofuz
parents:
diff changeset
336 // dynamic linker, and the relocation contains not only symbol name but
anatofuz
parents:
diff changeset
337 // various other information about the symbol. So, such attributes become a
anatofuz
parents:
diff changeset
338 // part of the ABI.
anatofuz
parents:
diff changeset
339 //
anatofuz
parents:
diff changeset
340 // Note for application developers: I can give you a piece of advice if
anatofuz
parents:
diff changeset
341 // you are writing a shared library. You probably should export only
anatofuz
parents:
diff changeset
342 // functions from your library. You shouldn't export variables.
anatofuz
parents:
diff changeset
343 //
anatofuz
parents:
diff changeset
344 // As an example what can happen when you export variables without knowing
anatofuz
parents:
diff changeset
345 // the semantics of copy relocations, assume that you have an exported
anatofuz
parents:
diff changeset
346 // variable of type T. It is an ABI-breaking change to add new members at
anatofuz
parents:
diff changeset
347 // end of T even though doing that doesn't change the layout of the
anatofuz
parents:
diff changeset
348 // existing members. That's because the space for the new members are not
anatofuz
parents:
diff changeset
349 // reserved in .bss unless you recompile the main program. That means they
anatofuz
parents:
diff changeset
350 // are likely to overlap with other data that happens to be laid out next
anatofuz
parents:
diff changeset
351 // to the variable in .bss. This kind of issue is sometimes very hard to
anatofuz
parents:
diff changeset
352 // debug. What's a solution? Instead of exporting a variable V from a DSO,
anatofuz
parents:
diff changeset
353 // define an accessor getV().
anatofuz
parents:
diff changeset
354 template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
anatofuz
parents:
diff changeset
355 // Copy relocation against zero-sized symbol doesn't make sense.
anatofuz
parents:
diff changeset
356 uint64_t symSize = ss.getSize();
anatofuz
parents:
diff changeset
357 if (symSize == 0 || ss.alignment == 0)
anatofuz
parents:
diff changeset
358 fatal("cannot create a copy relocation for symbol " + toString(ss));
anatofuz
parents:
diff changeset
359
anatofuz
parents:
diff changeset
360 // See if this symbol is in a read-only segment. If so, preserve the symbol's
anatofuz
parents:
diff changeset
361 // memory protection by reserving space in the .bss.rel.ro section.
anatofuz
parents:
diff changeset
362 bool isRO = isReadOnly<ELFT>(ss);
anatofuz
parents:
diff changeset
363 BssSection *sec =
anatofuz
parents:
diff changeset
364 make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment);
anatofuz
parents:
diff changeset
365 OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent();
anatofuz
parents:
diff changeset
366
anatofuz
parents:
diff changeset
367 // At this point, sectionBases has been migrated to sections. Append sec to
anatofuz
parents:
diff changeset
368 // sections.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
369 if (osec->commands.empty() ||
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
370 !isa<InputSectionDescription>(osec->commands.back()))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
371 osec->commands.push_back(make<InputSectionDescription>(""));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
372 auto *isd = cast<InputSectionDescription>(osec->commands.back());
150
anatofuz
parents:
diff changeset
373 isd->sections.push_back(sec);
anatofuz
parents:
diff changeset
374 osec->commitSection(sec);
anatofuz
parents:
diff changeset
375
anatofuz
parents:
diff changeset
376 // Look through the DSO's dynamic symbol table for aliases and create a
anatofuz
parents:
diff changeset
377 // dynamic symbol for each one. This causes the copy relocation to correctly
anatofuz
parents:
diff changeset
378 // interpose any aliases.
anatofuz
parents:
diff changeset
379 for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
380 replaceWithDefined(*sym, *sec, 0, sym->size);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
381
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
382 mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
383 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
384
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
385 // .eh_frame sections are mergeable input sections, so their input
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
386 // offsets are not linearly mapped to output section. For each input
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
387 // offset, we need to find a section piece containing the offset and
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
388 // add the piece's base address to the input offset to compute the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
389 // output offset. That isn't cheap.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
390 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
391 // This class is to speed up the offset computation. When we process
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
392 // relocations, we access offsets in the monotonically increasing
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
393 // order. So we can optimize for that access pattern.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
394 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
395 // For sections other than .eh_frame, this class doesn't do anything.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
396 namespace {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
397 class OffsetGetter {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
398 public:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
399 OffsetGetter() = default;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
400 explicit OffsetGetter(InputSectionBase &sec) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
401 if (auto *eh = dyn_cast<EhInputSection>(&sec)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
402 cies = eh->cies;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
403 fdes = eh->fdes;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
404 i = cies.begin();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
405 j = fdes.begin();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
406 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
407 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
408
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
409 // Translates offsets in input sections to offsets in output sections.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
410 // Given offset must increase monotonically. We assume that Piece is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
411 // sorted by inputOff.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
412 uint64_t get(uint64_t off) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
413 if (cies.empty())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
414 return off;
150
anatofuz
parents:
diff changeset
415
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
416 while (j != fdes.end() && j->inputOff <= off)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
417 ++j;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
418 auto it = j;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
419 if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
420 while (i != cies.end() && i->inputOff <= off)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
421 ++i;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
422 if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
423 fatal(".eh_frame: relocation is not in any piece");
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
424 it = i;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
425 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
426
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
427 // Offset -1 means that the piece is dead (i.e. garbage collected).
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
428 if (it[-1].outputOff == -1)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
429 return -1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
430 return it[-1].outputOff + (off - it[-1].inputOff);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
431 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
432
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
433 private:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
434 ArrayRef<EhSectionPiece> cies, fdes;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
435 ArrayRef<EhSectionPiece>::iterator i, j;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
436 };
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
437
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
438 // This class encapsulates states needed to scan relocations for one
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
439 // InputSectionBase.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
440 class RelocationScanner {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
441 public:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
442 template <class ELFT> void scanSection(InputSectionBase &s);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
443
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
444 private:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
445 InputSectionBase *sec;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
446 OffsetGetter getter;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
447
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
448 // End of relocations, used by Mips/PPC64.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
449 const void *end = nullptr;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
450
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
451 template <class RelTy> RelType getMipsN32RelType(RelTy *&rel) const;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
452 template <class ELFT, class RelTy>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
453 int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
454 bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
455 uint64_t relOff) const;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
456 void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
457 int64_t addend) const;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
458 template <class ELFT, class RelTy> void scanOne(RelTy *&i);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
459 template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
460 };
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
461 } // namespace
150
anatofuz
parents:
diff changeset
462
anatofuz
parents:
diff changeset
463 // MIPS has an odd notion of "paired" relocations to calculate addends.
anatofuz
parents:
diff changeset
464 // For example, if a relocation is of R_MIPS_HI16, there must be a
anatofuz
parents:
diff changeset
465 // R_MIPS_LO16 relocation after that, and an addend is calculated using
anatofuz
parents:
diff changeset
466 // the two relocations.
anatofuz
parents:
diff changeset
467 template <class ELFT, class RelTy>
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
468 int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
469 bool isLocal) const {
150
anatofuz
parents:
diff changeset
470 if (expr == R_MIPS_GOTREL && isLocal)
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
471 return sec->getFile<ELFT>()->mipsGp0;
150
anatofuz
parents:
diff changeset
472
anatofuz
parents:
diff changeset
473 // The ABI says that the paired relocation is used only for REL.
anatofuz
parents:
diff changeset
474 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
anatofuz
parents:
diff changeset
475 if (RelTy::IsRela)
anatofuz
parents:
diff changeset
476 return 0;
anatofuz
parents:
diff changeset
477
anatofuz
parents:
diff changeset
478 RelType type = rel.getType(config->isMips64EL);
anatofuz
parents:
diff changeset
479 uint32_t pairTy = getMipsPairType(type, isLocal);
anatofuz
parents:
diff changeset
480 if (pairTy == R_MIPS_NONE)
anatofuz
parents:
diff changeset
481 return 0;
anatofuz
parents:
diff changeset
482
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
483 const uint8_t *buf = sec->rawData.data();
150
anatofuz
parents:
diff changeset
484 uint32_t symIndex = rel.getSymbol(config->isMips64EL);
anatofuz
parents:
diff changeset
485
anatofuz
parents:
diff changeset
486 // To make things worse, paired relocations might not be contiguous in
anatofuz
parents:
diff changeset
487 // the relocation table, so we need to do linear search. *sigh*
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
488 for (const RelTy *ri = &rel; ri != static_cast<const RelTy *>(end); ++ri)
150
anatofuz
parents:
diff changeset
489 if (ri->getType(config->isMips64EL) == pairTy &&
anatofuz
parents:
diff changeset
490 ri->getSymbol(config->isMips64EL) == symIndex)
anatofuz
parents:
diff changeset
491 return target->getImplicitAddend(buf + ri->r_offset, pairTy);
anatofuz
parents:
diff changeset
492
anatofuz
parents:
diff changeset
493 warn("can't find matching " + toString(pairTy) + " relocation for " +
anatofuz
parents:
diff changeset
494 toString(type));
anatofuz
parents:
diff changeset
495 return 0;
anatofuz
parents:
diff changeset
496 }
anatofuz
parents:
diff changeset
497
anatofuz
parents:
diff changeset
498 // Custom error message if Sym is defined in a discarded section.
anatofuz
parents:
diff changeset
499 template <class ELFT>
anatofuz
parents:
diff changeset
500 static std::string maybeReportDiscarded(Undefined &sym) {
anatofuz
parents:
diff changeset
501 auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
anatofuz
parents:
diff changeset
502 if (!file || !sym.discardedSecIdx ||
anatofuz
parents:
diff changeset
503 file->getSections()[sym.discardedSecIdx] != &InputSection::discarded)
anatofuz
parents:
diff changeset
504 return "";
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
505 ArrayRef<typename ELFT::Shdr> objSections =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
506 file->template getELFShdrs<ELFT>();
150
anatofuz
parents:
diff changeset
507
anatofuz
parents:
diff changeset
508 std::string msg;
anatofuz
parents:
diff changeset
509 if (sym.type == ELF::STT_SECTION) {
anatofuz
parents:
diff changeset
510 msg = "relocation refers to a discarded section: ";
anatofuz
parents:
diff changeset
511 msg += CHECK(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
512 file->getObj().getSectionName(objSections[sym.discardedSecIdx]), file);
150
anatofuz
parents:
diff changeset
513 } else {
anatofuz
parents:
diff changeset
514 msg = "relocation refers to a symbol in a discarded section: " +
anatofuz
parents:
diff changeset
515 toString(sym);
anatofuz
parents:
diff changeset
516 }
anatofuz
parents:
diff changeset
517 msg += "\n>>> defined in " + toString(file);
anatofuz
parents:
diff changeset
518
anatofuz
parents:
diff changeset
519 Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];
anatofuz
parents:
diff changeset
520 if (elfSec.sh_type != SHT_GROUP)
anatofuz
parents:
diff changeset
521 return msg;
anatofuz
parents:
diff changeset
522
anatofuz
parents:
diff changeset
523 // If the discarded section is a COMDAT.
anatofuz
parents:
diff changeset
524 StringRef signature = file->getShtGroupSignature(objSections, elfSec);
anatofuz
parents:
diff changeset
525 if (const InputFile *prevailing =
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
526 symtab.comdatGroups.lookup(CachedHashStringRef(signature))) {
150
anatofuz
parents:
diff changeset
527 msg += "\n>>> section group signature: " + signature.str() +
anatofuz
parents:
diff changeset
528 "\n>>> prevailing definition is in " + toString(prevailing);
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
529 if (sym.nonPrevailing) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
530 msg += "\n>>> or the symbol in the prevailing group had STB_WEAK "
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
531 "binding and the symbol in a non-prevailing group had STB_GLOBAL "
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
532 "binding. Mixing groups with STB_WEAK and STB_GLOBAL binding "
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
533 "signature is not supported";
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
534 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
535 }
150
anatofuz
parents:
diff changeset
536 return msg;
anatofuz
parents:
diff changeset
537 }
anatofuz
parents:
diff changeset
538
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
539 namespace {
150
anatofuz
parents:
diff changeset
540 // Undefined diagnostics are collected in a vector and emitted once all of
anatofuz
parents:
diff changeset
541 // them are known, so that some postprocessing on the list of undefined symbols
anatofuz
parents:
diff changeset
542 // can happen before lld emits diagnostics.
anatofuz
parents:
diff changeset
543 struct UndefinedDiag {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
544 Undefined *sym;
150
anatofuz
parents:
diff changeset
545 struct Loc {
anatofuz
parents:
diff changeset
546 InputSectionBase *sec;
anatofuz
parents:
diff changeset
547 uint64_t offset;
anatofuz
parents:
diff changeset
548 };
anatofuz
parents:
diff changeset
549 std::vector<Loc> locs;
anatofuz
parents:
diff changeset
550 bool isWarning;
anatofuz
parents:
diff changeset
551 };
anatofuz
parents:
diff changeset
552
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
553 std::vector<UndefinedDiag> undefs;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
554 std::mutex relocMutex;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
555 }
150
anatofuz
parents:
diff changeset
556
anatofuz
parents:
diff changeset
557 // Check whether the definition name def is a mangled function name that matches
anatofuz
parents:
diff changeset
558 // the reference name ref.
anatofuz
parents:
diff changeset
559 static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {
anatofuz
parents:
diff changeset
560 llvm::ItaniumPartialDemangler d;
anatofuz
parents:
diff changeset
561 std::string name = def.str();
anatofuz
parents:
diff changeset
562 if (d.partialDemangle(name.c_str()))
anatofuz
parents:
diff changeset
563 return false;
anatofuz
parents:
diff changeset
564 char *buf = d.getFunctionName(nullptr, nullptr);
anatofuz
parents:
diff changeset
565 if (!buf)
anatofuz
parents:
diff changeset
566 return false;
anatofuz
parents:
diff changeset
567 bool ret = ref == buf;
anatofuz
parents:
diff changeset
568 free(buf);
anatofuz
parents:
diff changeset
569 return ret;
anatofuz
parents:
diff changeset
570 }
anatofuz
parents:
diff changeset
571
anatofuz
parents:
diff changeset
572 // Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns
anatofuz
parents:
diff changeset
573 // the suggested symbol, which is either in the symbol table, or in the same
anatofuz
parents:
diff changeset
574 // file of sym.
anatofuz
parents:
diff changeset
575 static const Symbol *getAlternativeSpelling(const Undefined &sym,
anatofuz
parents:
diff changeset
576 std::string &pre_hint,
anatofuz
parents:
diff changeset
577 std::string &post_hint) {
anatofuz
parents:
diff changeset
578 DenseMap<StringRef, const Symbol *> map;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
579 if (sym.file && sym.file->kind() == InputFile::ObjKind) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
580 auto *file = cast<ELFFileBase>(sym.file);
150
anatofuz
parents:
diff changeset
581 // If sym is a symbol defined in a discarded section, maybeReportDiscarded()
anatofuz
parents:
diff changeset
582 // will give an error. Don't suggest an alternative spelling.
anatofuz
parents:
diff changeset
583 if (file && sym.discardedSecIdx != 0 &&
anatofuz
parents:
diff changeset
584 file->getSections()[sym.discardedSecIdx] == &InputSection::discarded)
anatofuz
parents:
diff changeset
585 return nullptr;
anatofuz
parents:
diff changeset
586
anatofuz
parents:
diff changeset
587 // Build a map of local defined symbols.
anatofuz
parents:
diff changeset
588 for (const Symbol *s : sym.file->getSymbols())
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
589 if (s->isLocal() && s->isDefined() && !s->getName().empty())
150
anatofuz
parents:
diff changeset
590 map.try_emplace(s->getName(), s);
anatofuz
parents:
diff changeset
591 }
anatofuz
parents:
diff changeset
592
anatofuz
parents:
diff changeset
593 auto suggest = [&](StringRef newName) -> const Symbol * {
anatofuz
parents:
diff changeset
594 // If defined locally.
anatofuz
parents:
diff changeset
595 if (const Symbol *s = map.lookup(newName))
anatofuz
parents:
diff changeset
596 return s;
anatofuz
parents:
diff changeset
597
anatofuz
parents:
diff changeset
598 // If in the symbol table and not undefined.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
599 if (const Symbol *s = symtab.find(newName))
150
anatofuz
parents:
diff changeset
600 if (!s->isUndefined())
anatofuz
parents:
diff changeset
601 return s;
anatofuz
parents:
diff changeset
602
anatofuz
parents:
diff changeset
603 return nullptr;
anatofuz
parents:
diff changeset
604 };
anatofuz
parents:
diff changeset
605
anatofuz
parents:
diff changeset
606 // This loop enumerates all strings of Levenshtein distance 1 as typo
anatofuz
parents:
diff changeset
607 // correction candidates and suggests the one that exists as a non-undefined
anatofuz
parents:
diff changeset
608 // symbol.
anatofuz
parents:
diff changeset
609 StringRef name = sym.getName();
anatofuz
parents:
diff changeset
610 for (size_t i = 0, e = name.size(); i != e + 1; ++i) {
anatofuz
parents:
diff changeset
611 // Insert a character before name[i].
anatofuz
parents:
diff changeset
612 std::string newName = (name.substr(0, i) + "0" + name.substr(i)).str();
anatofuz
parents:
diff changeset
613 for (char c = '0'; c <= 'z'; ++c) {
anatofuz
parents:
diff changeset
614 newName[i] = c;
anatofuz
parents:
diff changeset
615 if (const Symbol *s = suggest(newName))
anatofuz
parents:
diff changeset
616 return s;
anatofuz
parents:
diff changeset
617 }
anatofuz
parents:
diff changeset
618 if (i == e)
anatofuz
parents:
diff changeset
619 break;
anatofuz
parents:
diff changeset
620
anatofuz
parents:
diff changeset
621 // Substitute name[i].
anatofuz
parents:
diff changeset
622 newName = std::string(name);
anatofuz
parents:
diff changeset
623 for (char c = '0'; c <= 'z'; ++c) {
anatofuz
parents:
diff changeset
624 newName[i] = c;
anatofuz
parents:
diff changeset
625 if (const Symbol *s = suggest(newName))
anatofuz
parents:
diff changeset
626 return s;
anatofuz
parents:
diff changeset
627 }
anatofuz
parents:
diff changeset
628
anatofuz
parents:
diff changeset
629 // Transpose name[i] and name[i+1]. This is of edit distance 2 but it is
anatofuz
parents:
diff changeset
630 // common.
anatofuz
parents:
diff changeset
631 if (i + 1 < e) {
anatofuz
parents:
diff changeset
632 newName[i] = name[i + 1];
anatofuz
parents:
diff changeset
633 newName[i + 1] = name[i];
anatofuz
parents:
diff changeset
634 if (const Symbol *s = suggest(newName))
anatofuz
parents:
diff changeset
635 return s;
anatofuz
parents:
diff changeset
636 }
anatofuz
parents:
diff changeset
637
anatofuz
parents:
diff changeset
638 // Delete name[i].
anatofuz
parents:
diff changeset
639 newName = (name.substr(0, i) + name.substr(i + 1)).str();
anatofuz
parents:
diff changeset
640 if (const Symbol *s = suggest(newName))
anatofuz
parents:
diff changeset
641 return s;
anatofuz
parents:
diff changeset
642 }
anatofuz
parents:
diff changeset
643
anatofuz
parents:
diff changeset
644 // Case mismatch, e.g. Foo vs FOO.
anatofuz
parents:
diff changeset
645 for (auto &it : map)
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
646 if (name.equals_insensitive(it.first))
150
anatofuz
parents:
diff changeset
647 return it.second;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
648 for (Symbol *sym : symtab.getSymbols())
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
649 if (!sym->isUndefined() && name.equals_insensitive(sym->getName()))
150
anatofuz
parents:
diff changeset
650 return sym;
anatofuz
parents:
diff changeset
651
anatofuz
parents:
diff changeset
652 // The reference may be a mangled name while the definition is not. Suggest a
anatofuz
parents:
diff changeset
653 // missing extern "C".
anatofuz
parents:
diff changeset
654 if (name.startswith("_Z")) {
anatofuz
parents:
diff changeset
655 std::string buf = name.str();
anatofuz
parents:
diff changeset
656 llvm::ItaniumPartialDemangler d;
anatofuz
parents:
diff changeset
657 if (!d.partialDemangle(buf.c_str()))
anatofuz
parents:
diff changeset
658 if (char *buf = d.getFunctionName(nullptr, nullptr)) {
anatofuz
parents:
diff changeset
659 const Symbol *s = suggest(buf);
anatofuz
parents:
diff changeset
660 free(buf);
anatofuz
parents:
diff changeset
661 if (s) {
anatofuz
parents:
diff changeset
662 pre_hint = ": extern \"C\" ";
anatofuz
parents:
diff changeset
663 return s;
anatofuz
parents:
diff changeset
664 }
anatofuz
parents:
diff changeset
665 }
anatofuz
parents:
diff changeset
666 } else {
anatofuz
parents:
diff changeset
667 const Symbol *s = nullptr;
anatofuz
parents:
diff changeset
668 for (auto &it : map)
anatofuz
parents:
diff changeset
669 if (canSuggestExternCForCXX(name, it.first)) {
anatofuz
parents:
diff changeset
670 s = it.second;
anatofuz
parents:
diff changeset
671 break;
anatofuz
parents:
diff changeset
672 }
anatofuz
parents:
diff changeset
673 if (!s)
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
674 for (Symbol *sym : symtab.getSymbols())
150
anatofuz
parents:
diff changeset
675 if (canSuggestExternCForCXX(name, sym->getName())) {
anatofuz
parents:
diff changeset
676 s = sym;
anatofuz
parents:
diff changeset
677 break;
anatofuz
parents:
diff changeset
678 }
anatofuz
parents:
diff changeset
679 if (s) {
anatofuz
parents:
diff changeset
680 pre_hint = " to declare ";
anatofuz
parents:
diff changeset
681 post_hint = " as extern \"C\"?";
anatofuz
parents:
diff changeset
682 return s;
anatofuz
parents:
diff changeset
683 }
anatofuz
parents:
diff changeset
684 }
anatofuz
parents:
diff changeset
685
anatofuz
parents:
diff changeset
686 return nullptr;
anatofuz
parents:
diff changeset
687 }
anatofuz
parents:
diff changeset
688
anatofuz
parents:
diff changeset
689 static void reportUndefinedSymbol(const UndefinedDiag &undef,
anatofuz
parents:
diff changeset
690 bool correctSpelling) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
691 Undefined &sym = *undef.sym;
150
anatofuz
parents:
diff changeset
692
anatofuz
parents:
diff changeset
693 auto visibility = [&]() -> std::string {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
694 switch (sym.visibility()) {
150
anatofuz
parents:
diff changeset
695 case STV_INTERNAL:
anatofuz
parents:
diff changeset
696 return "internal ";
anatofuz
parents:
diff changeset
697 case STV_HIDDEN:
anatofuz
parents:
diff changeset
698 return "hidden ";
anatofuz
parents:
diff changeset
699 case STV_PROTECTED:
anatofuz
parents:
diff changeset
700 return "protected ";
anatofuz
parents:
diff changeset
701 default:
anatofuz
parents:
diff changeset
702 return "";
anatofuz
parents:
diff changeset
703 }
anatofuz
parents:
diff changeset
704 };
anatofuz
parents:
diff changeset
705
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
706 std::string msg;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
707 switch (config->ekind) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
708 case ELF32LEKind:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
709 msg = maybeReportDiscarded<ELF32LE>(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
710 break;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
711 case ELF32BEKind:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
712 msg = maybeReportDiscarded<ELF32BE>(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
713 break;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
714 case ELF64LEKind:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
715 msg = maybeReportDiscarded<ELF64LE>(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
716 break;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
717 case ELF64BEKind:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
718 msg = maybeReportDiscarded<ELF64BE>(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
719 break;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
720 default:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
721 llvm_unreachable("");
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
722 }
150
anatofuz
parents:
diff changeset
723 if (msg.empty())
anatofuz
parents:
diff changeset
724 msg = "undefined " + visibility() + "symbol: " + toString(sym);
anatofuz
parents:
diff changeset
725
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
726 const size_t maxUndefReferences = 3;
150
anatofuz
parents:
diff changeset
727 size_t i = 0;
anatofuz
parents:
diff changeset
728 for (UndefinedDiag::Loc l : undef.locs) {
anatofuz
parents:
diff changeset
729 if (i >= maxUndefReferences)
anatofuz
parents:
diff changeset
730 break;
anatofuz
parents:
diff changeset
731 InputSectionBase &sec = *l.sec;
anatofuz
parents:
diff changeset
732 uint64_t offset = l.offset;
anatofuz
parents:
diff changeset
733
anatofuz
parents:
diff changeset
734 msg += "\n>>> referenced by ";
anatofuz
parents:
diff changeset
735 std::string src = sec.getSrcMsg(sym, offset);
anatofuz
parents:
diff changeset
736 if (!src.empty())
anatofuz
parents:
diff changeset
737 msg += src + "\n>>> ";
anatofuz
parents:
diff changeset
738 msg += sec.getObjMsg(offset);
anatofuz
parents:
diff changeset
739 i++;
anatofuz
parents:
diff changeset
740 }
anatofuz
parents:
diff changeset
741
anatofuz
parents:
diff changeset
742 if (i < undef.locs.size())
anatofuz
parents:
diff changeset
743 msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times")
anatofuz
parents:
diff changeset
744 .str();
anatofuz
parents:
diff changeset
745
anatofuz
parents:
diff changeset
746 if (correctSpelling) {
anatofuz
parents:
diff changeset
747 std::string pre_hint = ": ", post_hint;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
748 if (const Symbol *corrected =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
749 getAlternativeSpelling(sym, pre_hint, post_hint)) {
150
anatofuz
parents:
diff changeset
750 msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint;
anatofuz
parents:
diff changeset
751 if (corrected->file)
anatofuz
parents:
diff changeset
752 msg += "\n>>> defined in: " + toString(corrected->file);
anatofuz
parents:
diff changeset
753 }
anatofuz
parents:
diff changeset
754 }
anatofuz
parents:
diff changeset
755
anatofuz
parents:
diff changeset
756 if (sym.getName().startswith("_ZTV"))
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
757 msg +=
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
758 "\n>>> the vtable symbol may be undefined because the class is missing "
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
759 "its key function (see https://lld.llvm.org/missingkeyfunction)";
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
760 if (config->gcSections && config->zStartStopGC &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
761 sym.getName().startswith("__start_")) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
762 msg += "\n>>> the encapsulation symbol needs to be retained under "
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
763 "--gc-sections properly; consider -z nostart-stop-gc "
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
764 "(see https://lld.llvm.org/ELF/start-stop-gc)";
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
765 }
150
anatofuz
parents:
diff changeset
766
anatofuz
parents:
diff changeset
767 if (undef.isWarning)
anatofuz
parents:
diff changeset
768 warn(msg);
anatofuz
parents:
diff changeset
769 else
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
770 error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
150
anatofuz
parents:
diff changeset
771 }
anatofuz
parents:
diff changeset
772
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
773 void elf::reportUndefinedSymbols() {
150
anatofuz
parents:
diff changeset
774 // Find the first "undefined symbol" diagnostic for each diagnostic, and
anatofuz
parents:
diff changeset
775 // collect all "referenced from" lines at the first diagnostic.
anatofuz
parents:
diff changeset
776 DenseMap<Symbol *, UndefinedDiag *> firstRef;
anatofuz
parents:
diff changeset
777 for (UndefinedDiag &undef : undefs) {
anatofuz
parents:
diff changeset
778 assert(undef.locs.size() == 1);
anatofuz
parents:
diff changeset
779 if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
anatofuz
parents:
diff changeset
780 canon->locs.push_back(undef.locs[0]);
anatofuz
parents:
diff changeset
781 undef.locs.clear();
anatofuz
parents:
diff changeset
782 } else
anatofuz
parents:
diff changeset
783 firstRef[undef.sym] = &undef;
anatofuz
parents:
diff changeset
784 }
anatofuz
parents:
diff changeset
785
anatofuz
parents:
diff changeset
786 // Enable spell corrector for the first 2 diagnostics.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
787 for (const auto &[i, undef] : llvm::enumerate(undefs))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
788 if (!undef.locs.empty())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
789 reportUndefinedSymbol(undef, i < 2);
150
anatofuz
parents:
diff changeset
790 undefs.clear();
anatofuz
parents:
diff changeset
791 }
anatofuz
parents:
diff changeset
792
anatofuz
parents:
diff changeset
793 // Report an undefined symbol if necessary.
anatofuz
parents:
diff changeset
794 // Returns true if the undefined symbol will produce an error message.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
795 static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec,
150
anatofuz
parents:
diff changeset
796 uint64_t offset) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
797 std::lock_guard<std::mutex> lock(relocMutex);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
798 // If versioned, issue an error (even if the symbol is weak) because we don't
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
799 // know the defining filename which is required to construct a Verneed entry.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
800 if (sym.hasVersionSuffix) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
801 undefs.push_back({&sym, {{&sec, offset}}, false});
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
802 return true;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
803 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
804 if (sym.isWeak())
150
anatofuz
parents:
diff changeset
805 return false;
anatofuz
parents:
diff changeset
806
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
807 bool canBeExternal = !sym.isLocal() && sym.visibility() == STV_DEFAULT;
150
anatofuz
parents:
diff changeset
808 if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)
anatofuz
parents:
diff changeset
809 return false;
anatofuz
parents:
diff changeset
810
anatofuz
parents:
diff changeset
811 // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
anatofuz
parents:
diff changeset
812 // which references a switch table in a discarded .rodata/.text section. The
anatofuz
parents:
diff changeset
813 // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
anatofuz
parents:
diff changeset
814 // spec says references from outside the group to a STB_LOCAL symbol are not
anatofuz
parents:
diff changeset
815 // allowed. Work around the bug.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
816 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
817 // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
818 // because .LC0-.LTOC is not representable if the two labels are in different
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
819 // .got2
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
820 if (sym.discardedSecIdx != 0 && (sec.name == ".got2" || sec.name == ".toc"))
150
anatofuz
parents:
diff changeset
821 return false;
anatofuz
parents:
diff changeset
822
anatofuz
parents:
diff changeset
823 bool isWarning =
anatofuz
parents:
diff changeset
824 (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
anatofuz
parents:
diff changeset
825 config->noinhibitExec;
anatofuz
parents:
diff changeset
826 undefs.push_back({&sym, {{&sec, offset}}, isWarning});
anatofuz
parents:
diff changeset
827 return !isWarning;
anatofuz
parents:
diff changeset
828 }
anatofuz
parents:
diff changeset
829
anatofuz
parents:
diff changeset
830 // MIPS N32 ABI treats series of successive relocations with the same offset
anatofuz
parents:
diff changeset
831 // as a single relocation. The similar approach used by N64 ABI, but this ABI
anatofuz
parents:
diff changeset
832 // packs all relocations into the single relocation record. Here we emulate
anatofuz
parents:
diff changeset
833 // this for the N32 ABI. Iterate over relocation with the same offset and put
anatofuz
parents:
diff changeset
834 // theirs types into the single bit-set.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
835 template <class RelTy>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
836 RelType RelocationScanner::getMipsN32RelType(RelTy *&rel) const {
150
anatofuz
parents:
diff changeset
837 RelType type = 0;
anatofuz
parents:
diff changeset
838 uint64_t offset = rel->r_offset;
anatofuz
parents:
diff changeset
839
anatofuz
parents:
diff changeset
840 int n = 0;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
841 while (rel != static_cast<const RelTy *>(end) && rel->r_offset == offset)
150
anatofuz
parents:
diff changeset
842 type |= (rel++)->getType(config->isMips64EL) << (8 * n++);
anatofuz
parents:
diff changeset
843 return type;
anatofuz
parents:
diff changeset
844 }
anatofuz
parents:
diff changeset
845
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
846 template <bool shard = false>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
847 static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec,
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
848 Symbol &sym, int64_t addend, RelExpr expr,
150
anatofuz
parents:
diff changeset
849 RelType type) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
850 Partition &part = isec.getPartition();
150
anatofuz
parents:
diff changeset
851
anatofuz
parents:
diff changeset
852 // Add a relative relocation. If relrDyn section is enabled, and the
anatofuz
parents:
diff changeset
853 // relocation offset is guaranteed to be even, add the relocation to
anatofuz
parents:
diff changeset
854 // the relrDyn section, otherwise add it to the relaDyn section.
anatofuz
parents:
diff changeset
855 // relrDyn sections don't support odd offsets. Also, relrDyn sections
anatofuz
parents:
diff changeset
856 // don't store the addend values, so we must write it to the relocated
anatofuz
parents:
diff changeset
857 // address.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
858 if (part.relrDyn && isec.alignment >= 2 && offsetInSec % 2 == 0) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
859 isec.relocations.push_back({expr, type, offsetInSec, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
860 if (shard)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
861 part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
862 {&isec, offsetInSec});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
863 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
864 part.relrDyn->relocs.push_back({&isec, offsetInSec});
150
anatofuz
parents:
diff changeset
865 return;
anatofuz
parents:
diff changeset
866 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
867 part.relaDyn->addRelativeReloc<shard>(target->relativeRel, isec, offsetInSec,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
868 sym, addend, type, expr);
150
anatofuz
parents:
diff changeset
869 }
anatofuz
parents:
diff changeset
870
anatofuz
parents:
diff changeset
871 template <class PltSection, class GotPltSection>
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
872 static void addPltEntry(PltSection &plt, GotPltSection &gotPlt,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
873 RelocationBaseSection &rel, RelType type, Symbol &sym) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
874 plt.addEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
875 gotPlt.addEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
876 rel.addReloc({type, &gotPlt, sym.getGotPltOffset(),
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
877 sym.isPreemptible ? DynamicReloc::AgainstSymbol
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
878 : DynamicReloc::AddendOnlyWithTargetVA,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
879 sym, 0, R_ABS});
150
anatofuz
parents:
diff changeset
880 }
anatofuz
parents:
diff changeset
881
anatofuz
parents:
diff changeset
882 static void addGotEntry(Symbol &sym) {
anatofuz
parents:
diff changeset
883 in.got->addEntry(sym);
anatofuz
parents:
diff changeset
884 uint64_t off = sym.getGotOffset();
anatofuz
parents:
diff changeset
885
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
886 // If preemptible, emit a GLOB_DAT relocation.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
887 if (sym.isPreemptible) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
888 mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
889 DynamicReloc::AgainstSymbol, sym, 0, R_ABS});
150
anatofuz
parents:
diff changeset
890 return;
anatofuz
parents:
diff changeset
891 }
anatofuz
parents:
diff changeset
892
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
893 // Otherwise, the value is either a link-time constant or the load base
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
894 // plus a constant.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
895 if (!config->isPic || isAbsolute(sym))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
896 in.got->relocations.push_back({R_ABS, target->symbolicRel, off, 0, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
897 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
898 addRelativeReloc(*in.got, off, sym, 0, R_ABS, target->symbolicRel);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
899 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
900
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
901 static void addTpOffsetGotEntry(Symbol &sym) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
902 in.got->addEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
903 uint64_t off = sym.getGotOffset();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
904 if (!sym.isPreemptible && !config->isPic) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
905 in.got->relocations.push_back({R_TPREL, target->symbolicRel, off, 0, &sym});
150
anatofuz
parents:
diff changeset
906 return;
anatofuz
parents:
diff changeset
907 }
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
908 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
909 target->tlsGotRel, *in.got, off, sym, target->symbolicRel);
150
anatofuz
parents:
diff changeset
910 }
anatofuz
parents:
diff changeset
911
anatofuz
parents:
diff changeset
912 // Return true if we can define a symbol in the executable that
anatofuz
parents:
diff changeset
913 // contains the value/function of a symbol defined in a shared
anatofuz
parents:
diff changeset
914 // library.
anatofuz
parents:
diff changeset
915 static bool canDefineSymbolInExecutable(Symbol &sym) {
anatofuz
parents:
diff changeset
916 // If the symbol has default visibility the symbol defined in the
anatofuz
parents:
diff changeset
917 // executable will preempt it.
anatofuz
parents:
diff changeset
918 // Note that we want the visibility of the shared symbol itself, not
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
919 // the visibility of the symbol in the output file we are producing.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
920 if (!sym.dsoProtected)
150
anatofuz
parents:
diff changeset
921 return true;
anatofuz
parents:
diff changeset
922
anatofuz
parents:
diff changeset
923 // If we are allowed to break address equality of functions, defining
anatofuz
parents:
diff changeset
924 // a plt entry will allow the program to call the function in the
anatofuz
parents:
diff changeset
925 // .so, but the .so and the executable will no agree on the address
anatofuz
parents:
diff changeset
926 // of the function. Similar logic for objects.
anatofuz
parents:
diff changeset
927 return ((sym.isFunc() && config->ignoreFunctionAddressEquality) ||
anatofuz
parents:
diff changeset
928 (sym.isObject() && config->ignoreDataAddressEquality));
anatofuz
parents:
diff changeset
929 }
anatofuz
parents:
diff changeset
930
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
931 // Returns true if a given relocation can be computed at link-time.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
932 // This only handles relocation types expected in processAux.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
933 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
934 // For instance, we know the offset from a relocation to its target at
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
935 // link-time if the relocation is PC-relative and refers a
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
936 // non-interposable function in the same executable. This function
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
937 // will return true for such relocation.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
938 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
939 // If this function returns false, that means we need to emit a
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
940 // dynamic relocation so that the relocation will be fixed at load-time.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
941 bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
942 const Symbol &sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
943 uint64_t relOff) const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
944 // These expressions always compute a constant
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
945 if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, R_MIPS_GOT_LOCAL_PAGE,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
946 R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
947 R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
948 R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
949 R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE>(e))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
950 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
951
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
952 // These never do, except if the entire file is position dependent or if
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
953 // only the low bits are used.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
954 if (e == R_GOT || e == R_PLT)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
955 return target->usesOnlyLowPageBits(type) || !config->isPic;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
956
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
957 if (sym.isPreemptible)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
958 return false;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
959 if (!config->isPic)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
960 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
961
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
962 // The size of a non preemptible symbol is a constant.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
963 if (e == R_SIZE)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
964 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
965
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
966 // For the target and the relocation, we want to know if they are
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
967 // absolute or relative.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
968 bool absVal = isAbsoluteValue(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
969 bool relE = isRelExpr(e);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
970 if (absVal && !relE)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
971 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
972 if (!absVal && relE)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
973 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
974 if (!absVal && !relE)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
975 return target->usesOnlyLowPageBits(type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
976
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
977 assert(absVal && relE);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
978
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
979 // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
980 // in PIC mode. This is a little strange, but it allows us to link function
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
981 // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers).
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
982 // Normally such a call will be guarded with a comparison, which will load a
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
983 // zero from the GOT.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
984 if (sym.isUndefWeak())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
985 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
986
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
987 // We set the final symbols values for linker script defined symbols later.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
988 // They always can be computed as a link time constant.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
989 if (sym.scriptDefined)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
990 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
991
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
992 error("relocation " + toString(type) + " cannot refer to absolute symbol: " +
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
993 toString(sym) + getLocation(*sec, sym, relOff));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
994 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
995 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
996
150
anatofuz
parents:
diff changeset
997 // The reason we have to do this early scan is as follows
anatofuz
parents:
diff changeset
998 // * To mmap the output file, we need to know the size
anatofuz
parents:
diff changeset
999 // * For that, we need to know how many dynamic relocs we will have.
anatofuz
parents:
diff changeset
1000 // It might be possible to avoid this by outputting the file with write:
anatofuz
parents:
diff changeset
1001 // * Write the allocated output sections, computing addresses.
anatofuz
parents:
diff changeset
1002 // * Apply relocations, recording which ones require a dynamic reloc.
anatofuz
parents:
diff changeset
1003 // * Write the dynamic relocations.
anatofuz
parents:
diff changeset
1004 // * Write the rest of the file.
anatofuz
parents:
diff changeset
1005 // This would have some drawbacks. For example, we would only know if .rela.dyn
anatofuz
parents:
diff changeset
1006 // is needed after applying relocations. If it is, it will go after rw and rx
anatofuz
parents:
diff changeset
1007 // sections. Given that it is ro, we will need an extra PT_LOAD. This
anatofuz
parents:
diff changeset
1008 // complicates things for the dynamic linker and means we would have to reserve
anatofuz
parents:
diff changeset
1009 // space for the extra PT_LOAD even if we end up not using it.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1010 void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1011 Symbol &sym, int64_t addend) const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1012 // If non-ifunc non-preemptible, change PLT to direct call and optimize GOT
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1013 // indirection.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1014 const bool isIfunc = sym.isGnuIFunc();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1015 if (!sym.isPreemptible && (!isIfunc || config->zIfuncNoplt)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1016 if (expr != R_GOT_PC) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1017 // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1018 // stub type. It should be ignored if optimized to R_PC.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1019 if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1020 addend &= ~0x8000;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1021 // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1022 // call __tls_get_addr even if the symbol is non-preemptible.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1023 if (!(config->emachine == EM_HEXAGON &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1024 (type == R_HEX_GD_PLT_B22_PCREL ||
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1025 type == R_HEX_GD_PLT_B22_PCREL_X ||
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1026 type == R_HEX_GD_PLT_B32_PCREL_X)))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1027 expr = fromPlt(expr);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1028 } else if (!isAbsoluteValue(sym)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1029 expr =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1030 target->adjustGotPcExpr(type, addend, sec->rawData.data() + offset);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1031 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1032 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1033
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1034 // We were asked not to generate PLT entries for ifuncs. Instead, pass the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1035 // direct relocation on through.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1036 if (LLVM_UNLIKELY(isIfunc) && config->zIfuncNoplt) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1037 std::lock_guard<std::mutex> lock(relocMutex);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1038 sym.exportDynamic = true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1039 mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1040 return;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1041 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1042
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1043 if (needsGot(expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1044 if (config->emachine == EM_MIPS) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1045 // MIPS ABI has special rules to process GOT entries and doesn't
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1046 // require relocation entries for them. A special case is TLS
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1047 // relocations. In that case dynamic loader applies dynamic
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1048 // relocations to initialize TLS GOT entries.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1049 // See "Global Offset Table" in Chapter 5 in the following document
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1050 // for detailed description:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1051 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1052 in.mipsGot->addEntry(*sec->file, sym, addend, expr);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1053 } else {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1054 sym.setFlags(NEEDS_GOT);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1055 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1056 } else if (needsPlt(expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1057 sym.setFlags(NEEDS_PLT);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1058 } else if (LLVM_UNLIKELY(isIfunc)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1059 sym.setFlags(HAS_DIRECT_RELOC);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1060 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1061
150
anatofuz
parents:
diff changeset
1062 // If the relocation is known to be a link-time constant, we know no dynamic
anatofuz
parents:
diff changeset
1063 // relocation will be created, pass the control to relocateAlloc() or
anatofuz
parents:
diff changeset
1064 // relocateNonAlloc() to resolve it.
anatofuz
parents:
diff changeset
1065 //
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1066 // The behavior of an undefined weak reference is implementation defined. For
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1067 // non-link-time constants, we resolve relocations statically (let
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1068 // relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1069 // relocations for -pie and -shared.
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1070 //
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1071 // The general expectation of -no-pie static linking is that there is no
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1072 // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1073 // -shared matches the spirit of its -z undefs default. -pie has freedom on
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1074 // choices, and we choose dynamic relocations to be consistent with the
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1075 // handling of GOT-generating relocations.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1076 if (isStaticLinkTimeConstant(expr, type, sym, offset) ||
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
1077 (!config->isPic && sym.isUndefWeak())) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1078 sec->relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1079 return;
150
anatofuz
parents:
diff changeset
1080 }
anatofuz
parents:
diff changeset
1081
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1082 bool canWrite = (sec->flags & SHF_WRITE) || !config->zText;
150
anatofuz
parents:
diff changeset
1083 if (canWrite) {
anatofuz
parents:
diff changeset
1084 RelType rel = target->getDynRel(type);
anatofuz
parents:
diff changeset
1085 if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1086 addRelativeReloc<true>(*sec, offset, sym, addend, expr, type);
150
anatofuz
parents:
diff changeset
1087 return;
anatofuz
parents:
diff changeset
1088 } else if (rel != 0) {
anatofuz
parents:
diff changeset
1089 if (config->emachine == EM_MIPS && rel == target->symbolicRel)
anatofuz
parents:
diff changeset
1090 rel = target->relativeRel;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1091 std::lock_guard<std::mutex> lock(relocMutex);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1092 sec->getPartition().relaDyn->addSymbolReloc(rel, *sec, offset, sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1093 addend, type);
150
anatofuz
parents:
diff changeset
1094
anatofuz
parents:
diff changeset
1095 // MIPS ABI turns using of GOT and dynamic relocations inside out.
anatofuz
parents:
diff changeset
1096 // While regular ABI uses dynamic relocations to fill up GOT entries
anatofuz
parents:
diff changeset
1097 // MIPS ABI requires dynamic linker to fills up GOT entries using
anatofuz
parents:
diff changeset
1098 // specially sorted dynamic symbol table. This affects even dynamic
anatofuz
parents:
diff changeset
1099 // relocations against symbols which do not require GOT entries
anatofuz
parents:
diff changeset
1100 // creation explicitly, i.e. do not have any GOT-relocations. So if
anatofuz
parents:
diff changeset
1101 // a preemptible symbol has a dynamic relocation we anyway have
anatofuz
parents:
diff changeset
1102 // to create a GOT entry for it.
anatofuz
parents:
diff changeset
1103 // If a non-preemptible symbol has a dynamic relocation against it,
anatofuz
parents:
diff changeset
1104 // dynamic linker takes it st_value, adds offset and writes down
anatofuz
parents:
diff changeset
1105 // result of the dynamic relocation. In case of preemptible symbol
anatofuz
parents:
diff changeset
1106 // dynamic linker performs symbol resolution, writes the symbol value
anatofuz
parents:
diff changeset
1107 // to the GOT entry and reads the GOT entry when it needs to perform
anatofuz
parents:
diff changeset
1108 // a dynamic relocation.
anatofuz
parents:
diff changeset
1109 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
anatofuz
parents:
diff changeset
1110 if (config->emachine == EM_MIPS)
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1111 in.mipsGot->addEntry(*sec->file, sym, addend, expr);
150
anatofuz
parents:
diff changeset
1112 return;
anatofuz
parents:
diff changeset
1113 }
anatofuz
parents:
diff changeset
1114 }
anatofuz
parents:
diff changeset
1115
anatofuz
parents:
diff changeset
1116 // When producing an executable, we can perform copy relocations (for
anatofuz
parents:
diff changeset
1117 // STT_OBJECT) and canonical PLT (for STT_FUNC).
anatofuz
parents:
diff changeset
1118 if (!config->shared) {
anatofuz
parents:
diff changeset
1119 if (!canDefineSymbolInExecutable(sym)) {
anatofuz
parents:
diff changeset
1120 errorOrWarn("cannot preempt symbol: " + toString(sym) +
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1121 getLocation(*sec, sym, offset));
150
anatofuz
parents:
diff changeset
1122 return;
anatofuz
parents:
diff changeset
1123 }
anatofuz
parents:
diff changeset
1124
anatofuz
parents:
diff changeset
1125 if (sym.isObject()) {
anatofuz
parents:
diff changeset
1126 // Produce a copy relocation.
anatofuz
parents:
diff changeset
1127 if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
anatofuz
parents:
diff changeset
1128 if (!config->zCopyreloc)
anatofuz
parents:
diff changeset
1129 error("unresolvable relocation " + toString(type) +
anatofuz
parents:
diff changeset
1130 " against symbol '" + toString(*ss) +
anatofuz
parents:
diff changeset
1131 "'; recompile with -fPIC or remove '-z nocopyreloc'" +
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1132 getLocation(*sec, sym, offset));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1133 sym.setFlags(NEEDS_COPY);
150
anatofuz
parents:
diff changeset
1134 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1135 sec->relocations.push_back({expr, type, offset, addend, &sym});
150
anatofuz
parents:
diff changeset
1136 return;
anatofuz
parents:
diff changeset
1137 }
anatofuz
parents:
diff changeset
1138
anatofuz
parents:
diff changeset
1139 // This handles a non PIC program call to function in a shared library. In
anatofuz
parents:
diff changeset
1140 // an ideal world, we could just report an error saying the relocation can
anatofuz
parents:
diff changeset
1141 // overflow at runtime. In the real world with glibc, crt1.o has a
anatofuz
parents:
diff changeset
1142 // R_X86_64_PC32 pointing to libc.so.
anatofuz
parents:
diff changeset
1143 //
anatofuz
parents:
diff changeset
1144 // The general idea on how to handle such cases is to create a PLT entry and
anatofuz
parents:
diff changeset
1145 // use that as the function value.
anatofuz
parents:
diff changeset
1146 //
anatofuz
parents:
diff changeset
1147 // For the static linking part, we just return a plt expr and everything
anatofuz
parents:
diff changeset
1148 // else will use the PLT entry as the address.
anatofuz
parents:
diff changeset
1149 //
anatofuz
parents:
diff changeset
1150 // The remaining problem is making sure pointer equality still works. We
anatofuz
parents:
diff changeset
1151 // need the help of the dynamic linker for that. We let it know that we have
anatofuz
parents:
diff changeset
1152 // a direct reference to a so symbol by creating an undefined symbol with a
anatofuz
parents:
diff changeset
1153 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
anatofuz
parents:
diff changeset
1154 // the value of the symbol we created. This is true even for got entries, so
anatofuz
parents:
diff changeset
1155 // pointer equality is maintained. To avoid an infinite loop, the only entry
anatofuz
parents:
diff changeset
1156 // that points to the real function is a dedicated got entry used by the
anatofuz
parents:
diff changeset
1157 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
anatofuz
parents:
diff changeset
1158 // R_386_JMP_SLOT, etc).
anatofuz
parents:
diff changeset
1159
anatofuz
parents:
diff changeset
1160 // For position independent executable on i386, the plt entry requires ebx
anatofuz
parents:
diff changeset
1161 // to be set. This causes two problems:
anatofuz
parents:
diff changeset
1162 // * If some code has a direct reference to a function, it was probably
anatofuz
parents:
diff changeset
1163 // compiled without -fPIE/-fPIC and doesn't maintain ebx.
anatofuz
parents:
diff changeset
1164 // * If a library definition gets preempted to the executable, it will have
anatofuz
parents:
diff changeset
1165 // the wrong ebx value.
anatofuz
parents:
diff changeset
1166 if (sym.isFunc()) {
anatofuz
parents:
diff changeset
1167 if (config->pie && config->emachine == EM_386)
anatofuz
parents:
diff changeset
1168 errorOrWarn("symbol '" + toString(sym) +
anatofuz
parents:
diff changeset
1169 "' cannot be preempted; recompile with -fPIE" +
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1170 getLocation(*sec, sym, offset));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1171 sym.setFlags(NEEDS_COPY | NEEDS_PLT);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1172 sec->relocations.push_back({expr, type, offset, addend, &sym});
150
anatofuz
parents:
diff changeset
1173 return;
anatofuz
parents:
diff changeset
1174 }
anatofuz
parents:
diff changeset
1175 }
anatofuz
parents:
diff changeset
1176
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1177 errorOrWarn("relocation " + toString(type) + " cannot be used against " +
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1178 (sym.getName().empty() ? "local symbol"
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1179 : "symbol '" + toString(sym) + "'") +
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1180 "; recompile with -fPIC" + getLocation(*sec, sym, offset));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1181 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1182
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1183 // This function is similar to the `handleTlsRelocation`. MIPS does not
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1184 // support any relaxations for TLS relocations so by factoring out MIPS
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1185 // handling in to the separate function we can simplify the code and do not
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1186 // pollute other `handleTlsRelocation` by MIPS `ifs` statements.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1187 // Mips has a custom MipsGotSection that handles the writing of GOT entries
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1188 // without dynamic relocations.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1189 static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1190 InputSectionBase &c, uint64_t offset,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1191 int64_t addend, RelExpr expr) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1192 if (expr == R_MIPS_TLSLD) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1193 in.mipsGot->addTlsIndex(*c.file);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1194 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1195 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1196 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1197 if (expr == R_MIPS_TLSGD) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1198 in.mipsGot->addDynTlsEntry(*c.file, sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1199 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1200 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1201 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1202 return 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1203 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1204
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1205 // Notes about General Dynamic and Local Dynamic TLS models below. They may
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1206 // require the generation of a pair of GOT entries that have associated dynamic
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1207 // relocations. The pair of GOT entries created are of the form GOT[e0] Module
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1208 // Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1209 // symbol in TLS block.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1210 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1211 // Returns the number of relocations processed.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1212 static unsigned handleTlsRelocation(RelType type, Symbol &sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1213 InputSectionBase &c, uint64_t offset,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1214 int64_t addend, RelExpr expr) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1215 if (expr == R_TPREL || expr == R_TPREL_NEG) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1216 if (config->shared) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1217 errorOrWarn("relocation " + toString(type) + " against " + toString(sym) +
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1218 " cannot be used with -shared" + getLocation(c, sym, offset));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1219 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1220 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1221 return 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1222 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1223
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1224 if (config->emachine == EM_MIPS)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1225 return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1226
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1227 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1228 R_TLSDESC_GOTPLT>(expr) &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1229 config->shared) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1230 if (expr != R_TLSDESC_CALL) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1231 sym.setFlags(NEEDS_TLSDESC);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1232 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1233 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1234 return 1;
150
anatofuz
parents:
diff changeset
1235 }
anatofuz
parents:
diff changeset
1236
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1237 // ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1238 // PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1239 // relaxation as well.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1240 bool toExecRelax = !config->shared && config->emachine != EM_ARM &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1241 config->emachine != EM_HEXAGON &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1242 config->emachine != EM_RISCV &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1243 !c.file->ppc64DisableTLSRelax;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1244
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1245 // If we are producing an executable and the symbol is non-preemptable, it
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1246 // must be defined and the code sequence can be relaxed to use Local-Exec.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1247 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1248 // ARM and RISC-V do not support any relaxations for TLS relocations, however,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1249 // we can omit the DTPMOD dynamic relocations and resolve them at link time
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1250 // because them are always 1. This may be necessary for static linking as
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1251 // DTPMOD may not be expected at load time.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1252 bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1253
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1254 // Local Dynamic is for access to module local TLS variables, while still
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1255 // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1256 // module index, with a special value of 0 for the current module. GOT[e1] is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1257 // unused. There only needs to be one module index entry.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1258 if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1259 expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1260 // Local-Dynamic relocs can be relaxed to Local-Exec.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1261 if (toExecRelax) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1262 c.relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1263 {target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, offset,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1264 addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1265 return target->getTlsGdRelaxSkip(type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1266 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1267 if (expr == R_TLSLD_HINT)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1268 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1269 ctx.needsTlsLd.store(true, std::memory_order_relaxed);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1270 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1271 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1272 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1273
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1274 // Local-Dynamic relocs can be relaxed to Local-Exec.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1275 if (expr == R_DTPREL) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1276 if (toExecRelax)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1277 expr = target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1278 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1279 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1280 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1281
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1282 // Local-Dynamic sequence where offset of tls variable relative to dynamic
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1283 // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1284 if (expr == R_TLSLD_GOT_OFF) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1285 sym.setFlags(NEEDS_GOT_DTPREL);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1286 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1287 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1288 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1289
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1290 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1291 R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1292 if (!toExecRelax) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1293 sym.setFlags(NEEDS_TLSGD);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1294 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1295 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1296 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1297
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1298 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1299 // depending on the symbol being locally defined or not.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1300 if (sym.isPreemptible) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1301 sym.setFlags(NEEDS_TLSGD_TO_IE);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1302 c.relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1303 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, offset,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1304 addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1305 } else {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1306 c.relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1307 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, offset,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1308 addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1309 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1310 return target->getTlsGdRelaxSkip(type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1311 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1312
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1313 if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1314 R_TLSIE_HINT>(expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1315 ctx.hasTlsIe.store(true, std::memory_order_relaxed);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1316 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1317 // defined.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1318 if (toExecRelax && isLocalInExecutable) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1319 c.relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1320 {R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1321 } else if (expr != R_TLSIE_HINT) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1322 sym.setFlags(NEEDS_TLSIE);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1323 // R_GOT needs a relative relocation for PIC on i386 and Hexagon.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1324 if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1325 addRelativeReloc<true>(c, offset, sym, addend, expr, type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1326 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1327 c.relocations.push_back({expr, type, offset, addend, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1328 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1329 return 1;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1330 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1331
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1332 return 0;
150
anatofuz
parents:
diff changeset
1333 }
anatofuz
parents:
diff changeset
1334
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1335 template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
150
anatofuz
parents:
diff changeset
1336 const RelTy &rel = *i;
anatofuz
parents:
diff changeset
1337 uint32_t symIndex = rel.getSymbol(config->isMips64EL);
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1338 Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex);
150
anatofuz
parents:
diff changeset
1339 RelType type;
anatofuz
parents:
diff changeset
1340 if (config->mipsN32Abi) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1341 type = getMipsN32RelType(i);
150
anatofuz
parents:
diff changeset
1342 } else {
anatofuz
parents:
diff changeset
1343 type = rel.getType(config->isMips64EL);
anatofuz
parents:
diff changeset
1344 ++i;
anatofuz
parents:
diff changeset
1345 }
anatofuz
parents:
diff changeset
1346 // Get an offset in an output section this relocation is applied to.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1347 uint64_t offset = getter.get(rel.r_offset);
150
anatofuz
parents:
diff changeset
1348 if (offset == uint64_t(-1))
anatofuz
parents:
diff changeset
1349 return;
anatofuz
parents:
diff changeset
1350
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1351 RelExpr expr = target->getRelExpr(type, sym, sec->rawData.data() + offset);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1352 int64_t addend =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1353 RelTy::IsRela
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1354 ? getAddend<ELFT>(rel)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1355 : target->getImplicitAddend(sec->rawData.data() + rel.r_offset, type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1356 if (LLVM_UNLIKELY(config->emachine == EM_MIPS))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1357 addend += computeMipsAddend<ELFT>(rel, expr, sym.isLocal());
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1358 else if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1359 addend += getPPC64TocBase();
150
anatofuz
parents:
diff changeset
1360
anatofuz
parents:
diff changeset
1361 // Ignore R_*_NONE and other marker relocations.
anatofuz
parents:
diff changeset
1362 if (expr == R_NONE)
anatofuz
parents:
diff changeset
1363 return;
anatofuz
parents:
diff changeset
1364
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1365 // Error if the target symbol is undefined. Symbol index 0 may be used by
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1366 // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1367 if (sym.isUndefined() && symIndex != 0 &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1368 maybeReportUndefined(cast<Undefined>(sym), *sec, offset))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1369 return;
150
anatofuz
parents:
diff changeset
1370
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1371 if (config->emachine == EM_PPC64) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1372 // We can separate the small code model relocations into 2 categories:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1373 // 1) Those that access the compiler generated .toc sections.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1374 // 2) Those that access the linker allocated got entries.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1375 // lld allocates got entries to symbols on demand. Since we don't try to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1376 // sort the got entries in any way, we don't have to track which objects
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1377 // have got-based small code model relocs. The .toc sections get placed
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1378 // after the end of the linker allocated .got section and we do sort those
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1379 // so sections addressed with small code model relocations come first.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1380 if (type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1381 sec->file->ppc64SmallCodeModelTocRelocs = true;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1382
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1383 // Record the TOC entry (.toc + addend) as not relaxable. See the comment in
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1384 // InputSectionBase::relocateAlloc().
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1385 if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) &&
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1386 cast<Defined>(sym).section->name == ".toc")
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1387 ppc64noTocRelax.insert({&sym, addend});
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1388
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1389 if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) ||
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1390 (type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1391 if (i == end) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1392 errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last "
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1393 "relocation" +
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1394 getLocation(*sec, sym, offset));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1395 return;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1396 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1397
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1398 // Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1399 // so we can discern it later from the toc-case.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1400 if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1401 ++offset;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1402 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1403 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1404
150
anatofuz
parents:
diff changeset
1405 // If the relocation does not emit a GOT or GOTPLT entry but its computation
anatofuz
parents:
diff changeset
1406 // uses their addresses, we need GOT or GOTPLT to be created.
anatofuz
parents:
diff changeset
1407 //
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1408 // The 5 types that relative GOTPLT are all x86 and x86-64 specific.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1409 if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1410 R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1411 in.gotPlt->hasGotPltOffRel.store(true, std::memory_order_relaxed);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1412 } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1413 R_PPC64_RELAX_TOC>(expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1414 in.got->hasGotOffRel.store(true, std::memory_order_relaxed);
150
anatofuz
parents:
diff changeset
1415 }
anatofuz
parents:
diff changeset
1416
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1417 // Process TLS relocations, including relaxing TLS relocations. Note that
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1418 // R_TPREL and R_TPREL_NEG relocations are resolved in processAux.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1419 if (sym.isTls()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1420 if (unsigned processed =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1421 handleTlsRelocation(type, sym, *sec, offset, addend, expr)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1422 i += processed - 1;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1423 return;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1424 }
150
anatofuz
parents:
diff changeset
1425 }
anatofuz
parents:
diff changeset
1426
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1427 processAux(expr, type, offset, sym, addend);
150
anatofuz
parents:
diff changeset
1428 }
anatofuz
parents:
diff changeset
1429
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1430 // R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1431 // General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1432 // found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1433 // instructions are generated by very old IBM XL compilers. Work around the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1434 // issue by disabling GD/LD to IE/LE relaxation.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1435 template <class RelTy>
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1436 static void checkPPC64TLSRelax(InputSectionBase &sec, ArrayRef<RelTy> rels) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1437 // Skip if sec is synthetic (sec.file is null) or if sec has been marked.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1438 if (!sec.file || sec.file->ppc64DisableTLSRelax)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1439 return;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1440 bool hasGDLD = false;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1441 for (const RelTy &rel : rels) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1442 RelType type = rel.getType(false);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1443 switch (type) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1444 case R_PPC64_TLSGD:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1445 case R_PPC64_TLSLD:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1446 return; // Found a marker
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1447 case R_PPC64_GOT_TLSGD16:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1448 case R_PPC64_GOT_TLSGD16_HA:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1449 case R_PPC64_GOT_TLSGD16_HI:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1450 case R_PPC64_GOT_TLSGD16_LO:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1451 case R_PPC64_GOT_TLSLD16:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1452 case R_PPC64_GOT_TLSLD16_HA:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1453 case R_PPC64_GOT_TLSLD16_HI:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1454 case R_PPC64_GOT_TLSLD16_LO:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1455 hasGDLD = true;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1456 break;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1457 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1458 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1459 if (hasGDLD) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1460 sec.file->ppc64DisableTLSRelax = true;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1461 warn(toString(sec.file) +
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1462 ": disable TLS relaxation due to R_PPC64_GOT_TLS* relocations without "
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1463 "R_PPC64_TLSGD/R_PPC64_TLSLD relocations");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1464 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1465 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1466
150
anatofuz
parents:
diff changeset
1467 template <class ELFT, class RelTy>
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1468 void RelocationScanner::scan(ArrayRef<RelTy> rels) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1469 // Not all relocations end up in Sec->Relocations, but a lot do.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1470 sec->relocations.reserve(rels.size());
150
anatofuz
parents:
diff changeset
1471
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1472 if (config->emachine == EM_PPC64)
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1473 checkPPC64TLSRelax<RelTy>(*sec, rels);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1474
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1475 // For EhInputSection, OffsetGetter expects the relocations to be sorted by
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1476 // r_offset. In rare cases (.eh_frame pieces are reordered by a linker
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1477 // script), the relocations may be unordered.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1478 SmallVector<RelTy, 0> storage;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1479 if (isa<EhInputSection>(sec))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1480 rels = sortRels(rels, storage);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1481
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1482 end = static_cast<const void *>(rels.end());
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1483 for (auto i = rels.begin(); i != end;)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1484 scanOne<ELFT>(i);
150
anatofuz
parents:
diff changeset
1485
anatofuz
parents:
diff changeset
1486 // Sort relocations by offset for more efficient searching for
anatofuz
parents:
diff changeset
1487 // R_RISCV_PCREL_HI20 and R_PPC64_ADDR64.
anatofuz
parents:
diff changeset
1488 if (config->emachine == EM_RISCV ||
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1489 (config->emachine == EM_PPC64 && sec->name == ".toc"))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1490 llvm::stable_sort(sec->relocations,
150
anatofuz
parents:
diff changeset
1491 [](const Relocation &lhs, const Relocation &rhs) {
anatofuz
parents:
diff changeset
1492 return lhs.offset < rhs.offset;
anatofuz
parents:
diff changeset
1493 });
anatofuz
parents:
diff changeset
1494 }
anatofuz
parents:
diff changeset
1495
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1496 template <class ELFT> void RelocationScanner::scanSection(InputSectionBase &s) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1497 sec = &s;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1498 getter = OffsetGetter(s);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1499 const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1500 if (rels.areRelocsRel())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1501 scan<ELFT>(rels.rels);
150
anatofuz
parents:
diff changeset
1502 else
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1503 scan<ELFT>(rels.relas);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1504 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1505
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1506 template <class ELFT> void elf::scanRelocations() {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1507 // Scan all relocations. Each relocation goes through a series of tests to
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1508 // determine if it needs special treatment, such as creating GOT, PLT,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1509 // copy relocations, etc. Note that relocations for non-alloc sections are
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1510 // directly processed by InputSection::relocateNonAlloc.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1511
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1512 // Deterministic parallellism needs sorting relocations which is unsuitable
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1513 // for -z nocombreloc. MIPS and PPC64 use global states which are not suitable
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1514 // for parallelism.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1515 bool serial = !config->zCombreloc || config->emachine == EM_MIPS ||
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1516 config->emachine == EM_PPC64;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1517 parallel::TaskGroup tg;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1518 for (ELFFileBase *f : ctx.objectFiles) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1519 auto fn = [f]() {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1520 RelocationScanner scanner;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1521 for (InputSectionBase *s : f->getSections()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1522 if (s && s->kind() == SectionBase::Regular && s->isLive() &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1523 (s->flags & SHF_ALLOC) &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1524 !(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1525 scanner.template scanSection<ELFT>(*s);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1526 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1527 };
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1528 if (serial)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1529 fn();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1530 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1531 tg.execute(fn);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1532 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1533
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1534 // Both the main thread and thread pool index 0 use getThreadIndex()==0. Be
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1535 // careful that they don't concurrently run scanSections. When serial is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1536 // true, fn() has finished at this point, so running execute is safe.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1537 tg.execute([] {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1538 RelocationScanner scanner;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1539 for (Partition &part : partitions) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1540 for (EhInputSection *sec : part.ehFrame->sections)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1541 scanner.template scanSection<ELFT>(*sec);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1542 if (part.armExidx && part.armExidx->isLive())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1543 for (InputSection *sec : part.armExidx->exidxSections)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1544 scanner.template scanSection<ELFT>(*sec);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1545 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1546 });
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1547 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1548
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1549 static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1550 // Handle a reference to a non-preemptible ifunc. These are special in a
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1551 // few ways:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1552 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1553 // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1554 // a fixed value. But assuming that all references to the ifunc are
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1555 // GOT-generating or PLT-generating, the handling of an ifunc is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1556 // relatively straightforward. We create a PLT entry in Iplt, which is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1557 // usually at the end of .plt, which makes an indirect call using a
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1558 // matching GOT entry in igotPlt, which is usually at the end of .got.plt.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1559 // The GOT entry is relocated using an IRELATIVE relocation in relaIplt,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1560 // which is usually at the end of .rela.plt. Unlike most relocations in
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1561 // .rela.plt, which may be evaluated lazily without -z now, dynamic
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1562 // loaders evaluate IRELATIVE relocs eagerly, which means that for
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1563 // IRELATIVE relocs only, GOT-generating relocations can point directly to
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1564 // .got.plt without requiring a separate GOT entry.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1565 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1566 // - Despite the fact that an ifunc does not have a fixed value, compilers
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1567 // that are not passed -fPIC will assume that they do, and will emit
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1568 // direct (non-GOT-generating, non-PLT-generating) relocations to the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1569 // symbol. This means that if a direct relocation to the symbol is
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1570 // seen, the linker must set a value for the symbol, and this value must
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1571 // be consistent no matter what type of reference is made to the symbol.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1572 // This can be done by creating a PLT entry for the symbol in the way
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1573 // described above and making it canonical, that is, making all references
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1574 // point to the PLT entry instead of the resolver. In lld we also store
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1575 // the address of the PLT entry in the dynamic symbol table, which means
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1576 // that the symbol will also have the same value in other modules.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1577 // Because the value loaded from the GOT needs to be consistent with
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1578 // the value computed using a direct relocation, a non-preemptible ifunc
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1579 // may end up with two GOT entries, one in .got.plt that points to the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1580 // address returned by the resolver and is used only by the PLT entry,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1581 // and another in .got that points to the PLT entry and is used by
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1582 // GOT-generating relocations.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1583 //
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1584 // - The fact that these symbols do not have a fixed value makes them an
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1585 // exception to the general rule that a statically linked executable does
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1586 // not require any form of dynamic relocation. To handle these relocations
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1587 // correctly, the IRELATIVE relocations are stored in an array which a
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1588 // statically linked executable's startup code must enumerate using the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1589 // linker-defined symbols __rela?_iplt_{start,end}.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1590 if (!sym.isGnuIFunc() || sym.isPreemptible || config->zIfuncNoplt)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1591 return false;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1592 // Skip unreferenced non-preemptible ifunc.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1593 if (!(flags & (NEEDS_GOT | NEEDS_PLT | HAS_DIRECT_RELOC)))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1594 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1595
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1596 sym.isInIplt = true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1597
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1598 // Create an Iplt and the associated IRELATIVE relocation pointing to the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1599 // original section/value pairs. For non-GOT non-PLT relocation case below, we
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1600 // may alter section/value, so create a copy of the symbol to make
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1601 // section/value fixed.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1602 auto *directSym = makeDefined(cast<Defined>(sym));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1603 directSym->allocateAux();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1604 addPltEntry(*in.iplt, *in.igotPlt, *in.relaIplt, target->iRelativeRel,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1605 *directSym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1606 sym.allocateAux();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1607 symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1608
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1609 if (flags & HAS_DIRECT_RELOC) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1610 // Change the value to the IPLT and redirect all references to it.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1611 auto &d = cast<Defined>(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1612 d.section = in.iplt.get();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1613 d.value = d.getPltIdx() * target->ipltEntrySize;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1614 d.size = 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1615 // It's important to set the symbol type here so that dynamic loaders
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1616 // don't try to call the PLT as if it were an ifunc resolver.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1617 d.type = STT_FUNC;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1618
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1619 if (flags & NEEDS_GOT)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1620 addGotEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1621 } else if (flags & NEEDS_GOT) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1622 // Redirect GOT accesses to point to the Igot.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1623 sym.gotInIgot = true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1624 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1625 return true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1626 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1627
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1628 void elf::postScanRelocations() {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1629 auto fn = [](Symbol &sym) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1630 auto flags = sym.flags.load(std::memory_order_relaxed);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1631 if (handleNonPreemptibleIfunc(sym, flags))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1632 return;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1633 if (!sym.needsDynReloc())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1634 return;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1635 sym.allocateAux();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1636
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1637 if (flags & NEEDS_GOT)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1638 addGotEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1639 if (flags & NEEDS_PLT)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1640 addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1641 if (flags & NEEDS_COPY) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1642 if (sym.isObject()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1643 invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1644 // NEEDS_COPY is cleared for sym and its aliases so that in
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1645 // later iterations aliases won't cause redundant copies.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1646 assert(!sym.hasFlag(NEEDS_COPY));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1647 } else {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1648 assert(sym.isFunc() && sym.hasFlag(NEEDS_PLT));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1649 if (!sym.isDefined()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1650 replaceWithDefined(sym, *in.plt,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1651 target->pltHeaderSize +
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1652 target->pltEntrySize * sym.getPltIdx(),
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1653 0);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1654 sym.setFlags(NEEDS_COPY);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1655 if (config->emachine == EM_PPC) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1656 // PPC32 canonical PLT entries are at the beginning of .glink
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1657 cast<Defined>(sym).value = in.plt->headerSize;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1658 in.plt->headerSize += 16;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1659 cast<PPC32GlinkSection>(*in.plt).canonical_plts.push_back(&sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1660 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1661 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1662 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1663 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1664
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1665 if (!sym.isTls())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1666 return;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1667 bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1668
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1669 if (flags & NEEDS_TLSDESC) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1670 in.got->addTlsDescEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1671 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1672 target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1673 target->tlsDescRel);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1674 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1675 if (flags & NEEDS_TLSGD) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1676 in.got->addDynTlsEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1677 uint64_t off = in.got->getGlobalDynOffset(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1678 if (isLocalInExecutable)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1679 // Write one to the GOT slot.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1680 in.got->relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1681 {R_ADDEND, target->symbolicRel, off, 1, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1682 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1683 mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *in.got,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1684 off, sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1685
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1686 // If the symbol is preemptible we need the dynamic linker to write
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1687 // the offset too.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1688 uint64_t offsetOff = off + config->wordsize;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1689 if (sym.isPreemptible)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1690 mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *in.got,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1691 offsetOff, sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1692 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1693 in.got->relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1694 {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1695 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1696 if (flags & NEEDS_TLSGD_TO_IE) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1697 in.got->addEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1698 mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *in.got,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1699 sym.getGotOffset(), sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1700 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1701 if (flags & NEEDS_GOT_DTPREL) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1702 in.got->addEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1703 in.got->relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1704 {R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1705 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1706
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1707 if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1708 addTpOffsetGotEntry(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1709 };
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1710
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1711 if (ctx.needsTlsLd.load(std::memory_order_relaxed) && in.got->addTlsIndex()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1712 static Undefined dummy(nullptr, "", STB_LOCAL, 0, 0);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1713 if (config->shared)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1714 mainPart->relaDyn->addReloc(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1715 {target->tlsModuleIndexRel, in.got.get(), in.got->getTlsIndexOff()});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1716 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1717 in.got->relocations.push_back(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1718 {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &dummy});
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1719 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1720
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1721 assert(symAux.size() == 1);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1722 for (Symbol *sym : symtab.getSymbols())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1723 fn(*sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1724
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1725 // Local symbols may need the aforementioned non-preemptible ifunc and GOT
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1726 // handling. They don't need regular PLT.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1727 for (ELFFileBase *file : ctx.objectFiles)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1728 for (Symbol *sym : file->getLocalSymbols())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1729 fn(*sym);
150
anatofuz
parents:
diff changeset
1730 }
anatofuz
parents:
diff changeset
1731
anatofuz
parents:
diff changeset
1732 static bool mergeCmp(const InputSection *a, const InputSection *b) {
anatofuz
parents:
diff changeset
1733 // std::merge requires a strict weak ordering.
anatofuz
parents:
diff changeset
1734 if (a->outSecOff < b->outSecOff)
anatofuz
parents:
diff changeset
1735 return true;
anatofuz
parents:
diff changeset
1736
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1737 // FIXME dyn_cast<ThunkSection> is non-null for any SyntheticSection.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1738 if (a->outSecOff == b->outSecOff && a != b) {
150
anatofuz
parents:
diff changeset
1739 auto *ta = dyn_cast<ThunkSection>(a);
anatofuz
parents:
diff changeset
1740 auto *tb = dyn_cast<ThunkSection>(b);
anatofuz
parents:
diff changeset
1741
anatofuz
parents:
diff changeset
1742 // Check if Thunk is immediately before any specific Target
anatofuz
parents:
diff changeset
1743 // InputSection for example Mips LA25 Thunks.
anatofuz
parents:
diff changeset
1744 if (ta && ta->getTargetInputSection() == b)
anatofuz
parents:
diff changeset
1745 return true;
anatofuz
parents:
diff changeset
1746
anatofuz
parents:
diff changeset
1747 // Place Thunk Sections without specific targets before
anatofuz
parents:
diff changeset
1748 // non-Thunk Sections.
anatofuz
parents:
diff changeset
1749 if (ta && !tb && !ta->getTargetInputSection())
anatofuz
parents:
diff changeset
1750 return true;
anatofuz
parents:
diff changeset
1751 }
anatofuz
parents:
diff changeset
1752
anatofuz
parents:
diff changeset
1753 return false;
anatofuz
parents:
diff changeset
1754 }
anatofuz
parents:
diff changeset
1755
anatofuz
parents:
diff changeset
1756 // Call Fn on every executable InputSection accessed via the linker script
anatofuz
parents:
diff changeset
1757 // InputSectionDescription::Sections.
anatofuz
parents:
diff changeset
1758 static void forEachInputSectionDescription(
anatofuz
parents:
diff changeset
1759 ArrayRef<OutputSection *> outputSections,
anatofuz
parents:
diff changeset
1760 llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) {
anatofuz
parents:
diff changeset
1761 for (OutputSection *os : outputSections) {
anatofuz
parents:
diff changeset
1762 if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))
anatofuz
parents:
diff changeset
1763 continue;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1764 for (SectionCommand *bc : os->commands)
150
anatofuz
parents:
diff changeset
1765 if (auto *isd = dyn_cast<InputSectionDescription>(bc))
anatofuz
parents:
diff changeset
1766 fn(os, isd);
anatofuz
parents:
diff changeset
1767 }
anatofuz
parents:
diff changeset
1768 }
anatofuz
parents:
diff changeset
1769
anatofuz
parents:
diff changeset
1770 // Thunk Implementation
anatofuz
parents:
diff changeset
1771 //
anatofuz
parents:
diff changeset
1772 // Thunks (sometimes called stubs, veneers or branch islands) are small pieces
anatofuz
parents:
diff changeset
1773 // of code that the linker inserts inbetween a caller and a callee. The thunks
anatofuz
parents:
diff changeset
1774 // are added at link time rather than compile time as the decision on whether
anatofuz
parents:
diff changeset
1775 // a thunk is needed, such as the caller and callee being out of range, can only
anatofuz
parents:
diff changeset
1776 // be made at link time.
anatofuz
parents:
diff changeset
1777 //
anatofuz
parents:
diff changeset
1778 // It is straightforward to tell given the current state of the program when a
anatofuz
parents:
diff changeset
1779 // thunk is needed for a particular call. The more difficult part is that
anatofuz
parents:
diff changeset
1780 // the thunk needs to be placed in the program such that the caller can reach
anatofuz
parents:
diff changeset
1781 // the thunk and the thunk can reach the callee; furthermore, adding thunks to
anatofuz
parents:
diff changeset
1782 // the program alters addresses, which can mean more thunks etc.
anatofuz
parents:
diff changeset
1783 //
anatofuz
parents:
diff changeset
1784 // In lld we have a synthetic ThunkSection that can hold many Thunks.
anatofuz
parents:
diff changeset
1785 // The decision to have a ThunkSection act as a container means that we can
anatofuz
parents:
diff changeset
1786 // more easily handle the most common case of a single block of contiguous
anatofuz
parents:
diff changeset
1787 // Thunks by inserting just a single ThunkSection.
anatofuz
parents:
diff changeset
1788 //
anatofuz
parents:
diff changeset
1789 // The implementation of Thunks in lld is split across these areas
anatofuz
parents:
diff changeset
1790 // Relocations.cpp : Framework for creating and placing thunks
anatofuz
parents:
diff changeset
1791 // Thunks.cpp : The code generated for each supported thunk
anatofuz
parents:
diff changeset
1792 // Target.cpp : Target specific hooks that the framework uses to decide when
anatofuz
parents:
diff changeset
1793 // a thunk is used
anatofuz
parents:
diff changeset
1794 // Synthetic.cpp : Implementation of ThunkSection
anatofuz
parents:
diff changeset
1795 // Writer.cpp : Iteratively call framework until no more Thunks added
anatofuz
parents:
diff changeset
1796 //
anatofuz
parents:
diff changeset
1797 // Thunk placement requirements:
anatofuz
parents:
diff changeset
1798 // Mips LA25 thunks. These must be placed immediately before the callee section
anatofuz
parents:
diff changeset
1799 // We can assume that the caller is in range of the Thunk. These are modelled
anatofuz
parents:
diff changeset
1800 // by Thunks that return the section they must precede with
anatofuz
parents:
diff changeset
1801 // getTargetInputSection().
anatofuz
parents:
diff changeset
1802 //
anatofuz
parents:
diff changeset
1803 // ARM interworking and range extension thunks. These thunks must be placed
anatofuz
parents:
diff changeset
1804 // within range of the caller. All implemented ARM thunks can always reach the
anatofuz
parents:
diff changeset
1805 // callee as they use an indirect jump via a register that has no range
anatofuz
parents:
diff changeset
1806 // restrictions.
anatofuz
parents:
diff changeset
1807 //
anatofuz
parents:
diff changeset
1808 // Thunk placement algorithm:
anatofuz
parents:
diff changeset
1809 // For Mips LA25 ThunkSections; the placement is explicit, it has to be before
anatofuz
parents:
diff changeset
1810 // getTargetInputSection().
anatofuz
parents:
diff changeset
1811 //
anatofuz
parents:
diff changeset
1812 // For thunks that must be placed within range of the caller there are many
anatofuz
parents:
diff changeset
1813 // possible choices given that the maximum range from the caller is usually
anatofuz
parents:
diff changeset
1814 // much larger than the average InputSection size. Desirable properties include:
anatofuz
parents:
diff changeset
1815 // - Maximize reuse of thunks by multiple callers
anatofuz
parents:
diff changeset
1816 // - Minimize number of ThunkSections to simplify insertion
anatofuz
parents:
diff changeset
1817 // - Handle impact of already added Thunks on addresses
anatofuz
parents:
diff changeset
1818 // - Simple to understand and implement
anatofuz
parents:
diff changeset
1819 //
anatofuz
parents:
diff changeset
1820 // In lld for the first pass, we pre-create one or more ThunkSections per
anatofuz
parents:
diff changeset
1821 // InputSectionDescription at Target specific intervals. A ThunkSection is
anatofuz
parents:
diff changeset
1822 // placed so that the estimated end of the ThunkSection is within range of the
anatofuz
parents:
diff changeset
1823 // start of the InputSectionDescription or the previous ThunkSection. For
anatofuz
parents:
diff changeset
1824 // example:
anatofuz
parents:
diff changeset
1825 // InputSectionDescription
anatofuz
parents:
diff changeset
1826 // Section 0
anatofuz
parents:
diff changeset
1827 // ...
anatofuz
parents:
diff changeset
1828 // Section N
anatofuz
parents:
diff changeset
1829 // ThunkSection 0
anatofuz
parents:
diff changeset
1830 // Section N + 1
anatofuz
parents:
diff changeset
1831 // ...
anatofuz
parents:
diff changeset
1832 // Section N + K
anatofuz
parents:
diff changeset
1833 // Thunk Section 1
anatofuz
parents:
diff changeset
1834 //
anatofuz
parents:
diff changeset
1835 // The intention is that we can add a Thunk to a ThunkSection that is well
anatofuz
parents:
diff changeset
1836 // spaced enough to service a number of callers without having to do a lot
anatofuz
parents:
diff changeset
1837 // of work. An important principle is that it is not an error if a Thunk cannot
anatofuz
parents:
diff changeset
1838 // be placed in a pre-created ThunkSection; when this happens we create a new
anatofuz
parents:
diff changeset
1839 // ThunkSection placed next to the caller. This allows us to handle the vast
anatofuz
parents:
diff changeset
1840 // majority of thunks simply, but also handle rare cases where the branch range
anatofuz
parents:
diff changeset
1841 // is smaller than the target specific spacing.
anatofuz
parents:
diff changeset
1842 //
anatofuz
parents:
diff changeset
1843 // The algorithm is expected to create all the thunks that are needed in a
anatofuz
parents:
diff changeset
1844 // single pass, with a small number of programs needing a second pass due to
anatofuz
parents:
diff changeset
1845 // the insertion of thunks in the first pass increasing the offset between
anatofuz
parents:
diff changeset
1846 // callers and callees that were only just in range.
anatofuz
parents:
diff changeset
1847 //
anatofuz
parents:
diff changeset
1848 // A consequence of allowing new ThunkSections to be created outside of the
anatofuz
parents:
diff changeset
1849 // pre-created ThunkSections is that in rare cases calls to Thunks that were in
anatofuz
parents:
diff changeset
1850 // range in pass K, are out of range in some pass > K due to the insertion of
anatofuz
parents:
diff changeset
1851 // more Thunks in between the caller and callee. When this happens we retarget
anatofuz
parents:
diff changeset
1852 // the relocation back to the original target and create another Thunk.
anatofuz
parents:
diff changeset
1853
anatofuz
parents:
diff changeset
1854 // Remove ThunkSections that are empty, this should only be the initial set
anatofuz
parents:
diff changeset
1855 // precreated on pass 0.
anatofuz
parents:
diff changeset
1856
anatofuz
parents:
diff changeset
1857 // Insert the Thunks for OutputSection OS into their designated place
anatofuz
parents:
diff changeset
1858 // in the Sections vector, and recalculate the InputSection output section
anatofuz
parents:
diff changeset
1859 // offsets.
anatofuz
parents:
diff changeset
1860 // This may invalidate any output section offsets stored outside of InputSection
anatofuz
parents:
diff changeset
1861 void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {
anatofuz
parents:
diff changeset
1862 forEachInputSectionDescription(
anatofuz
parents:
diff changeset
1863 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
anatofuz
parents:
diff changeset
1864 if (isd->thunkSections.empty())
anatofuz
parents:
diff changeset
1865 return;
anatofuz
parents:
diff changeset
1866
anatofuz
parents:
diff changeset
1867 // Remove any zero sized precreated Thunks.
anatofuz
parents:
diff changeset
1868 llvm::erase_if(isd->thunkSections,
anatofuz
parents:
diff changeset
1869 [](const std::pair<ThunkSection *, uint32_t> &ts) {
anatofuz
parents:
diff changeset
1870 return ts.first->getSize() == 0;
anatofuz
parents:
diff changeset
1871 });
anatofuz
parents:
diff changeset
1872
anatofuz
parents:
diff changeset
1873 // ISD->ThunkSections contains all created ThunkSections, including
anatofuz
parents:
diff changeset
1874 // those inserted in previous passes. Extract the Thunks created this
anatofuz
parents:
diff changeset
1875 // pass and order them in ascending outSecOff.
anatofuz
parents:
diff changeset
1876 std::vector<ThunkSection *> newThunks;
anatofuz
parents:
diff changeset
1877 for (std::pair<ThunkSection *, uint32_t> ts : isd->thunkSections)
anatofuz
parents:
diff changeset
1878 if (ts.second == pass)
anatofuz
parents:
diff changeset
1879 newThunks.push_back(ts.first);
anatofuz
parents:
diff changeset
1880 llvm::stable_sort(newThunks,
anatofuz
parents:
diff changeset
1881 [](const ThunkSection *a, const ThunkSection *b) {
anatofuz
parents:
diff changeset
1882 return a->outSecOff < b->outSecOff;
anatofuz
parents:
diff changeset
1883 });
anatofuz
parents:
diff changeset
1884
anatofuz
parents:
diff changeset
1885 // Merge sorted vectors of Thunks and InputSections by outSecOff
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1886 SmallVector<InputSection *, 0> tmp;
150
anatofuz
parents:
diff changeset
1887 tmp.reserve(isd->sections.size() + newThunks.size());
anatofuz
parents:
diff changeset
1888
anatofuz
parents:
diff changeset
1889 std::merge(isd->sections.begin(), isd->sections.end(),
anatofuz
parents:
diff changeset
1890 newThunks.begin(), newThunks.end(), std::back_inserter(tmp),
anatofuz
parents:
diff changeset
1891 mergeCmp);
anatofuz
parents:
diff changeset
1892
anatofuz
parents:
diff changeset
1893 isd->sections = std::move(tmp);
anatofuz
parents:
diff changeset
1894 });
anatofuz
parents:
diff changeset
1895 }
anatofuz
parents:
diff changeset
1896
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1897 static int64_t getPCBias(RelType type) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1898 if (config->emachine != EM_ARM)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1899 return 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1900 switch (type) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1901 case R_ARM_THM_JUMP19:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1902 case R_ARM_THM_JUMP24:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1903 case R_ARM_THM_CALL:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1904 return 4;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1905 default:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1906 return 8;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1907 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1908 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1909
150
anatofuz
parents:
diff changeset
1910 // Find or create a ThunkSection within the InputSectionDescription (ISD) that
anatofuz
parents:
diff changeset
1911 // is in range of Src. An ISD maps to a range of InputSections described by a
anatofuz
parents:
diff changeset
1912 // linker script section pattern such as { .text .text.* }.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1913 ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1914 InputSection *isec,
150
anatofuz
parents:
diff changeset
1915 InputSectionDescription *isd,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1916 const Relocation &rel,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1917 uint64_t src) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1918 // See the comment in getThunk for -pcBias below.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1919 const int64_t pcBias = getPCBias(rel.type);
150
anatofuz
parents:
diff changeset
1920 for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {
anatofuz
parents:
diff changeset
1921 ThunkSection *ts = tp.first;
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1922 uint64_t tsBase = os->addr + ts->outSecOff - pcBias;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1923 uint64_t tsLimit = tsBase + ts->getSize();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1924 if (target->inBranchRange(rel.type, src,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1925 (src > tsLimit) ? tsBase : tsLimit))
150
anatofuz
parents:
diff changeset
1926 return ts;
anatofuz
parents:
diff changeset
1927 }
anatofuz
parents:
diff changeset
1928
anatofuz
parents:
diff changeset
1929 // No suitable ThunkSection exists. This can happen when there is a branch
anatofuz
parents:
diff changeset
1930 // with lower range than the ThunkSection spacing or when there are too
anatofuz
parents:
diff changeset
1931 // many Thunks. Create a new ThunkSection as close to the InputSection as
anatofuz
parents:
diff changeset
1932 // possible. Error if InputSection is so large we cannot place ThunkSection
anatofuz
parents:
diff changeset
1933 // anywhere in Range.
anatofuz
parents:
diff changeset
1934 uint64_t thunkSecOff = isec->outSecOff;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1935 if (!target->inBranchRange(rel.type, src,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1936 os->addr + thunkSecOff + rel.addend)) {
150
anatofuz
parents:
diff changeset
1937 thunkSecOff = isec->outSecOff + isec->getSize();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1938 if (!target->inBranchRange(rel.type, src,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1939 os->addr + thunkSecOff + rel.addend))
150
anatofuz
parents:
diff changeset
1940 fatal("InputSection too large for range extension thunk " +
anatofuz
parents:
diff changeset
1941 isec->getObjMsg(src - (os->addr + isec->outSecOff)));
anatofuz
parents:
diff changeset
1942 }
anatofuz
parents:
diff changeset
1943 return addThunkSection(os, isd, thunkSecOff);
anatofuz
parents:
diff changeset
1944 }
anatofuz
parents:
diff changeset
1945
anatofuz
parents:
diff changeset
1946 // Add a Thunk that needs to be placed in a ThunkSection that immediately
anatofuz
parents:
diff changeset
1947 // precedes its Target.
anatofuz
parents:
diff changeset
1948 ThunkSection *ThunkCreator::getISThunkSec(InputSection *isec) {
anatofuz
parents:
diff changeset
1949 ThunkSection *ts = thunkedSections.lookup(isec);
anatofuz
parents:
diff changeset
1950 if (ts)
anatofuz
parents:
diff changeset
1951 return ts;
anatofuz
parents:
diff changeset
1952
anatofuz
parents:
diff changeset
1953 // Find InputSectionRange within Target Output Section (TOS) that the
anatofuz
parents:
diff changeset
1954 // InputSection (IS) that we need to precede is in.
anatofuz
parents:
diff changeset
1955 OutputSection *tos = isec->getParent();
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
1956 for (SectionCommand *bc : tos->commands) {
150
anatofuz
parents:
diff changeset
1957 auto *isd = dyn_cast<InputSectionDescription>(bc);
anatofuz
parents:
diff changeset
1958 if (!isd || isd->sections.empty())
anatofuz
parents:
diff changeset
1959 continue;
anatofuz
parents:
diff changeset
1960
anatofuz
parents:
diff changeset
1961 InputSection *first = isd->sections.front();
anatofuz
parents:
diff changeset
1962 InputSection *last = isd->sections.back();
anatofuz
parents:
diff changeset
1963
anatofuz
parents:
diff changeset
1964 if (isec->outSecOff < first->outSecOff || last->outSecOff < isec->outSecOff)
anatofuz
parents:
diff changeset
1965 continue;
anatofuz
parents:
diff changeset
1966
anatofuz
parents:
diff changeset
1967 ts = addThunkSection(tos, isd, isec->outSecOff);
anatofuz
parents:
diff changeset
1968 thunkedSections[isec] = ts;
anatofuz
parents:
diff changeset
1969 return ts;
anatofuz
parents:
diff changeset
1970 }
anatofuz
parents:
diff changeset
1971
anatofuz
parents:
diff changeset
1972 return nullptr;
anatofuz
parents:
diff changeset
1973 }
anatofuz
parents:
diff changeset
1974
anatofuz
parents:
diff changeset
1975 // Create one or more ThunkSections per OS that can be used to place Thunks.
anatofuz
parents:
diff changeset
1976 // We attempt to place the ThunkSections using the following desirable
anatofuz
parents:
diff changeset
1977 // properties:
anatofuz
parents:
diff changeset
1978 // - Within range of the maximum number of callers
anatofuz
parents:
diff changeset
1979 // - Minimise the number of ThunkSections
anatofuz
parents:
diff changeset
1980 //
anatofuz
parents:
diff changeset
1981 // We follow a simple but conservative heuristic to place ThunkSections at
anatofuz
parents:
diff changeset
1982 // offsets that are multiples of a Target specific branch range.
anatofuz
parents:
diff changeset
1983 // For an InputSectionDescription that is smaller than the range, a single
anatofuz
parents:
diff changeset
1984 // ThunkSection at the end of the range will do.
anatofuz
parents:
diff changeset
1985 //
anatofuz
parents:
diff changeset
1986 // For an InputSectionDescription that is more than twice the size of the range,
anatofuz
parents:
diff changeset
1987 // we place the last ThunkSection at range bytes from the end of the
anatofuz
parents:
diff changeset
1988 // InputSectionDescription in order to increase the likelihood that the
anatofuz
parents:
diff changeset
1989 // distance from a thunk to its target will be sufficiently small to
anatofuz
parents:
diff changeset
1990 // allow for the creation of a short thunk.
anatofuz
parents:
diff changeset
1991 void ThunkCreator::createInitialThunkSections(
anatofuz
parents:
diff changeset
1992 ArrayRef<OutputSection *> outputSections) {
anatofuz
parents:
diff changeset
1993 uint32_t thunkSectionSpacing = target->getThunkSectionSpacing();
anatofuz
parents:
diff changeset
1994
anatofuz
parents:
diff changeset
1995 forEachInputSectionDescription(
anatofuz
parents:
diff changeset
1996 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
anatofuz
parents:
diff changeset
1997 if (isd->sections.empty())
anatofuz
parents:
diff changeset
1998 return;
anatofuz
parents:
diff changeset
1999
anatofuz
parents:
diff changeset
2000 uint32_t isdBegin = isd->sections.front()->outSecOff;
anatofuz
parents:
diff changeset
2001 uint32_t isdEnd =
anatofuz
parents:
diff changeset
2002 isd->sections.back()->outSecOff + isd->sections.back()->getSize();
anatofuz
parents:
diff changeset
2003 uint32_t lastThunkLowerBound = -1;
anatofuz
parents:
diff changeset
2004 if (isdEnd - isdBegin > thunkSectionSpacing * 2)
anatofuz
parents:
diff changeset
2005 lastThunkLowerBound = isdEnd - thunkSectionSpacing;
anatofuz
parents:
diff changeset
2006
anatofuz
parents:
diff changeset
2007 uint32_t isecLimit;
anatofuz
parents:
diff changeset
2008 uint32_t prevIsecLimit = isdBegin;
anatofuz
parents:
diff changeset
2009 uint32_t thunkUpperBound = isdBegin + thunkSectionSpacing;
anatofuz
parents:
diff changeset
2010
anatofuz
parents:
diff changeset
2011 for (const InputSection *isec : isd->sections) {
anatofuz
parents:
diff changeset
2012 isecLimit = isec->outSecOff + isec->getSize();
anatofuz
parents:
diff changeset
2013 if (isecLimit > thunkUpperBound) {
anatofuz
parents:
diff changeset
2014 addThunkSection(os, isd, prevIsecLimit);
anatofuz
parents:
diff changeset
2015 thunkUpperBound = prevIsecLimit + thunkSectionSpacing;
anatofuz
parents:
diff changeset
2016 }
anatofuz
parents:
diff changeset
2017 if (isecLimit > lastThunkLowerBound)
anatofuz
parents:
diff changeset
2018 break;
anatofuz
parents:
diff changeset
2019 prevIsecLimit = isecLimit;
anatofuz
parents:
diff changeset
2020 }
anatofuz
parents:
diff changeset
2021 addThunkSection(os, isd, isecLimit);
anatofuz
parents:
diff changeset
2022 });
anatofuz
parents:
diff changeset
2023 }
anatofuz
parents:
diff changeset
2024
anatofuz
parents:
diff changeset
2025 ThunkSection *ThunkCreator::addThunkSection(OutputSection *os,
anatofuz
parents:
diff changeset
2026 InputSectionDescription *isd,
anatofuz
parents:
diff changeset
2027 uint64_t off) {
anatofuz
parents:
diff changeset
2028 auto *ts = make<ThunkSection>(os, off);
anatofuz
parents:
diff changeset
2029 ts->partition = os->partition;
anatofuz
parents:
diff changeset
2030 if ((config->fixCortexA53Errata843419 || config->fixCortexA8) &&
anatofuz
parents:
diff changeset
2031 !isd->sections.empty()) {
anatofuz
parents:
diff changeset
2032 // The errata fixes are sensitive to addresses modulo 4 KiB. When we add
anatofuz
parents:
diff changeset
2033 // thunks we disturb the base addresses of sections placed after the thunks
anatofuz
parents:
diff changeset
2034 // this makes patches we have generated redundant, and may cause us to
anatofuz
parents:
diff changeset
2035 // generate more patches as different instructions are now in sensitive
anatofuz
parents:
diff changeset
2036 // locations. When we generate more patches we may force more branches to
anatofuz
parents:
diff changeset
2037 // go out of range, causing more thunks to be generated. In pathological
anatofuz
parents:
diff changeset
2038 // cases this can cause the address dependent content pass not to converge.
anatofuz
parents:
diff changeset
2039 // We fix this by rounding up the size of the ThunkSection to 4KiB, this
anatofuz
parents:
diff changeset
2040 // limits the insertion of a ThunkSection on the addresses modulo 4 KiB,
anatofuz
parents:
diff changeset
2041 // which means that adding Thunks to the section does not invalidate
anatofuz
parents:
diff changeset
2042 // errata patches for following code.
anatofuz
parents:
diff changeset
2043 // Rounding up the size to 4KiB has consequences for code-size and can
anatofuz
parents:
diff changeset
2044 // trip up linker script defined assertions. For example the linux kernel
anatofuz
parents:
diff changeset
2045 // has an assertion that what LLD represents as an InputSectionDescription
anatofuz
parents:
diff changeset
2046 // does not exceed 4 KiB even if the overall OutputSection is > 128 Mib.
anatofuz
parents:
diff changeset
2047 // We use the heuristic of rounding up the size when both of the following
anatofuz
parents:
diff changeset
2048 // conditions are true:
anatofuz
parents:
diff changeset
2049 // 1.) The OutputSection is larger than the ThunkSectionSpacing. This
anatofuz
parents:
diff changeset
2050 // accounts for the case where no single InputSectionDescription is
anatofuz
parents:
diff changeset
2051 // larger than the OutputSection size. This is conservative but simple.
anatofuz
parents:
diff changeset
2052 // 2.) The InputSectionDescription is larger than 4 KiB. This will prevent
anatofuz
parents:
diff changeset
2053 // any assertion failures that an InputSectionDescription is < 4 KiB
anatofuz
parents:
diff changeset
2054 // in size.
anatofuz
parents:
diff changeset
2055 uint64_t isdSize = isd->sections.back()->outSecOff +
anatofuz
parents:
diff changeset
2056 isd->sections.back()->getSize() -
anatofuz
parents:
diff changeset
2057 isd->sections.front()->outSecOff;
anatofuz
parents:
diff changeset
2058 if (os->size > target->getThunkSectionSpacing() && isdSize > 4096)
anatofuz
parents:
diff changeset
2059 ts->roundUpSizeForErrata = true;
anatofuz
parents:
diff changeset
2060 }
anatofuz
parents:
diff changeset
2061 isd->thunkSections.push_back({ts, pass});
anatofuz
parents:
diff changeset
2062 return ts;
anatofuz
parents:
diff changeset
2063 }
anatofuz
parents:
diff changeset
2064
anatofuz
parents:
diff changeset
2065 static bool isThunkSectionCompatible(InputSection *source,
anatofuz
parents:
diff changeset
2066 SectionBase *target) {
anatofuz
parents:
diff changeset
2067 // We can't reuse thunks in different loadable partitions because they might
anatofuz
parents:
diff changeset
2068 // not be loaded. But partition 1 (the main partition) will always be loaded.
anatofuz
parents:
diff changeset
2069 if (source->partition != target->partition)
anatofuz
parents:
diff changeset
2070 return target->partition == 1;
anatofuz
parents:
diff changeset
2071 return true;
anatofuz
parents:
diff changeset
2072 }
anatofuz
parents:
diff changeset
2073
anatofuz
parents:
diff changeset
2074 std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec,
anatofuz
parents:
diff changeset
2075 Relocation &rel, uint64_t src) {
anatofuz
parents:
diff changeset
2076 std::vector<Thunk *> *thunkVec = nullptr;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2077 // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2078 // out in the relocation addend. We compensate for the PC bias so that
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2079 // an Arm and Thumb relocation to the same destination get the same keyAddend,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2080 // which is usually 0.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2081 const int64_t pcBias = getPCBias(rel.type);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2082 const int64_t keyAddend = rel.addend + pcBias;
150
anatofuz
parents:
diff changeset
2083
anatofuz
parents:
diff changeset
2084 // We use a ((section, offset), addend) pair to find the thunk position if
anatofuz
parents:
diff changeset
2085 // possible so that we create only one thunk for aliased symbols or ICFed
anatofuz
parents:
diff changeset
2086 // sections. There may be multiple relocations sharing the same (section,
anatofuz
parents:
diff changeset
2087 // offset + addend) pair. We may revert the relocation back to its original
anatofuz
parents:
diff changeset
2088 // non-Thunk target, so we cannot fold offset + addend.
anatofuz
parents:
diff changeset
2089 if (auto *d = dyn_cast<Defined>(rel.sym))
anatofuz
parents:
diff changeset
2090 if (!d->isInPlt() && d->section)
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2091 thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value},
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2092 keyAddend}];
150
anatofuz
parents:
diff changeset
2093 if (!thunkVec)
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2094 thunkVec = &thunkedSymbols[{rel.sym, keyAddend}];
150
anatofuz
parents:
diff changeset
2095
anatofuz
parents:
diff changeset
2096 // Check existing Thunks for Sym to see if they can be reused
anatofuz
parents:
diff changeset
2097 for (Thunk *t : *thunkVec)
anatofuz
parents:
diff changeset
2098 if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) &&
anatofuz
parents:
diff changeset
2099 t->isCompatibleWith(*isec, rel) &&
anatofuz
parents:
diff changeset
2100 target->inBranchRange(rel.type, src,
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2101 t->getThunkTargetSym()->getVA(-pcBias)))
150
anatofuz
parents:
diff changeset
2102 return std::make_pair(t, false);
anatofuz
parents:
diff changeset
2103
anatofuz
parents:
diff changeset
2104 // No existing compatible Thunk in range, create a new one
anatofuz
parents:
diff changeset
2105 Thunk *t = addThunk(*isec, rel);
anatofuz
parents:
diff changeset
2106 thunkVec->push_back(t);
anatofuz
parents:
diff changeset
2107 return std::make_pair(t, true);
anatofuz
parents:
diff changeset
2108 }
anatofuz
parents:
diff changeset
2109
anatofuz
parents:
diff changeset
2110 // Return true if the relocation target is an in range Thunk.
anatofuz
parents:
diff changeset
2111 // Return false if the relocation is not to a Thunk. If the relocation target
anatofuz
parents:
diff changeset
2112 // was originally to a Thunk, but is no longer in range we revert the
anatofuz
parents:
diff changeset
2113 // relocation back to its original non-Thunk target.
anatofuz
parents:
diff changeset
2114 bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) {
anatofuz
parents:
diff changeset
2115 if (Thunk *t = thunks.lookup(rel.sym)) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2116 if (target->inBranchRange(rel.type, src, rel.sym->getVA(rel.addend)))
150
anatofuz
parents:
diff changeset
2117 return true;
anatofuz
parents:
diff changeset
2118 rel.sym = &t->destination;
anatofuz
parents:
diff changeset
2119 rel.addend = t->addend;
anatofuz
parents:
diff changeset
2120 if (rel.sym->isInPlt())
anatofuz
parents:
diff changeset
2121 rel.expr = toPlt(rel.expr);
anatofuz
parents:
diff changeset
2122 }
anatofuz
parents:
diff changeset
2123 return false;
anatofuz
parents:
diff changeset
2124 }
anatofuz
parents:
diff changeset
2125
anatofuz
parents:
diff changeset
2126 // Process all relocations from the InputSections that have been assigned
anatofuz
parents:
diff changeset
2127 // to InputSectionDescriptions and redirect through Thunks if needed. The
anatofuz
parents:
diff changeset
2128 // function should be called iteratively until it returns false.
anatofuz
parents:
diff changeset
2129 //
anatofuz
parents:
diff changeset
2130 // PreConditions:
anatofuz
parents:
diff changeset
2131 // All InputSections that may need a Thunk are reachable from
anatofuz
parents:
diff changeset
2132 // OutputSectionCommands.
anatofuz
parents:
diff changeset
2133 //
anatofuz
parents:
diff changeset
2134 // All OutputSections have an address and all InputSections have an offset
anatofuz
parents:
diff changeset
2135 // within the OutputSection.
anatofuz
parents:
diff changeset
2136 //
anatofuz
parents:
diff changeset
2137 // The offsets between caller (relocation place) and callee
anatofuz
parents:
diff changeset
2138 // (relocation target) will not be modified outside of createThunks().
anatofuz
parents:
diff changeset
2139 //
anatofuz
parents:
diff changeset
2140 // PostConditions:
anatofuz
parents:
diff changeset
2141 // If return value is true then ThunkSections have been inserted into
anatofuz
parents:
diff changeset
2142 // OutputSections. All relocations that needed a Thunk based on the information
anatofuz
parents:
diff changeset
2143 // available to createThunks() on entry have been redirected to a Thunk. Note
anatofuz
parents:
diff changeset
2144 // that adding Thunks changes offsets between caller and callee so more Thunks
anatofuz
parents:
diff changeset
2145 // may be required.
anatofuz
parents:
diff changeset
2146 //
anatofuz
parents:
diff changeset
2147 // If return value is false then no more Thunks are needed, and createThunks has
anatofuz
parents:
diff changeset
2148 // made no changes. If the target requires range extension thunks, currently
anatofuz
parents:
diff changeset
2149 // ARM, then any future change in offset between caller and callee risks a
anatofuz
parents:
diff changeset
2150 // relocation out of range error.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2151 bool ThunkCreator::createThunks(uint32_t pass,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2152 ArrayRef<OutputSection *> outputSections) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2153 this->pass = pass;
150
anatofuz
parents:
diff changeset
2154 bool addressesChanged = false;
anatofuz
parents:
diff changeset
2155
anatofuz
parents:
diff changeset
2156 if (pass == 0 && target->getThunkSectionSpacing())
anatofuz
parents:
diff changeset
2157 createInitialThunkSections(outputSections);
anatofuz
parents:
diff changeset
2158
anatofuz
parents:
diff changeset
2159 // Create all the Thunks and insert them into synthetic ThunkSections. The
anatofuz
parents:
diff changeset
2160 // ThunkSections are later inserted back into InputSectionDescriptions.
anatofuz
parents:
diff changeset
2161 // We separate the creation of ThunkSections from the insertion of the
anatofuz
parents:
diff changeset
2162 // ThunkSections as ThunkSections are not always inserted into the same
anatofuz
parents:
diff changeset
2163 // InputSectionDescription as the caller.
anatofuz
parents:
diff changeset
2164 forEachInputSectionDescription(
anatofuz
parents:
diff changeset
2165 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
anatofuz
parents:
diff changeset
2166 for (InputSection *isec : isd->sections)
anatofuz
parents:
diff changeset
2167 for (Relocation &rel : isec->relocations) {
anatofuz
parents:
diff changeset
2168 uint64_t src = isec->getVA(rel.offset);
anatofuz
parents:
diff changeset
2169
anatofuz
parents:
diff changeset
2170 // If we are a relocation to an existing Thunk, check if it is
anatofuz
parents:
diff changeset
2171 // still in range. If not then Rel will be altered to point to its
anatofuz
parents:
diff changeset
2172 // original target so another Thunk can be generated.
anatofuz
parents:
diff changeset
2173 if (pass > 0 && normalizeExistingThunk(rel, src))
anatofuz
parents:
diff changeset
2174 continue;
anatofuz
parents:
diff changeset
2175
anatofuz
parents:
diff changeset
2176 if (!target->needsThunk(rel.expr, rel.type, isec->file, src,
anatofuz
parents:
diff changeset
2177 *rel.sym, rel.addend))
anatofuz
parents:
diff changeset
2178 continue;
anatofuz
parents:
diff changeset
2179
anatofuz
parents:
diff changeset
2180 Thunk *t;
anatofuz
parents:
diff changeset
2181 bool isNew;
anatofuz
parents:
diff changeset
2182 std::tie(t, isNew) = getThunk(isec, rel, src);
anatofuz
parents:
diff changeset
2183
anatofuz
parents:
diff changeset
2184 if (isNew) {
anatofuz
parents:
diff changeset
2185 // Find or create a ThunkSection for the new Thunk
anatofuz
parents:
diff changeset
2186 ThunkSection *ts;
anatofuz
parents:
diff changeset
2187 if (auto *tis = t->getTargetInputSection())
anatofuz
parents:
diff changeset
2188 ts = getISThunkSec(tis);
anatofuz
parents:
diff changeset
2189 else
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
2190 ts = getISDThunkSec(os, isec, isd, rel, src);
150
anatofuz
parents:
diff changeset
2191 ts->addThunk(t);
anatofuz
parents:
diff changeset
2192 thunks[t->getThunkTargetSym()] = t;
anatofuz
parents:
diff changeset
2193 }
anatofuz
parents:
diff changeset
2194
anatofuz
parents:
diff changeset
2195 // Redirect relocation to Thunk, we never go via the PLT to a Thunk
anatofuz
parents:
diff changeset
2196 rel.sym = t->getThunkTargetSym();
anatofuz
parents:
diff changeset
2197 rel.expr = fromPlt(rel.expr);
anatofuz
parents:
diff changeset
2198
anatofuz
parents:
diff changeset
2199 // On AArch64 and PPC, a jump/call relocation may be encoded as
anatofuz
parents:
diff changeset
2200 // STT_SECTION + non-zero addend, clear the addend after
anatofuz
parents:
diff changeset
2201 // redirection.
anatofuz
parents:
diff changeset
2202 if (config->emachine != EM_MIPS)
anatofuz
parents:
diff changeset
2203 rel.addend = -getPCBias(rel.type);
anatofuz
parents:
diff changeset
2204 }
anatofuz
parents:
diff changeset
2205
anatofuz
parents:
diff changeset
2206 for (auto &p : isd->thunkSections)
anatofuz
parents:
diff changeset
2207 addressesChanged |= p.first->assignOffsets();
anatofuz
parents:
diff changeset
2208 });
anatofuz
parents:
diff changeset
2209
anatofuz
parents:
diff changeset
2210 for (auto &p : thunkedSections)
anatofuz
parents:
diff changeset
2211 addressesChanged |= p.second->assignOffsets();
anatofuz
parents:
diff changeset
2212
anatofuz
parents:
diff changeset
2213 // Merge all created synthetic ThunkSections back into OutputSection
anatofuz
parents:
diff changeset
2214 mergeThunks(outputSections);
anatofuz
parents:
diff changeset
2215 return addressesChanged;
anatofuz
parents:
diff changeset
2216 }
anatofuz
parents:
diff changeset
2217
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2218 // The following aid in the conversion of call x@GDPLT to call __tls_get_addr
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2219 // hexagonNeedsTLSSymbol scans for relocations would require a call to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2220 // __tls_get_addr.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2221 // hexagonTLSSymbolUpdate rebinds the relocation to __tls_get_addr.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2222 bool elf::hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2223 bool needTlsSymbol = false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2224 forEachInputSectionDescription(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2225 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2226 for (InputSection *isec : isd->sections)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2227 for (Relocation &rel : isec->relocations)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2228 if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2229 needTlsSymbol = true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2230 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2231 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2232 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2233 return needTlsSymbol;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2234 }
150
anatofuz
parents:
diff changeset
2235
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2236 void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2237 Symbol *sym = symtab.find("__tls_get_addr");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2238 if (!sym)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2239 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2240 bool needEntry = true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2241 forEachInputSectionDescription(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2242 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2243 for (InputSection *isec : isd->sections)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2244 for (Relocation &rel : isec->relocations)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2245 if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2246 if (needEntry) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2247 sym->allocateAux();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2248 addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2249 *sym);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2250 needEntry = false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2251 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2252 rel.sym = sym;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2253 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2254 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2255 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
2256
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2257 template void elf::scanRelocations<ELF32LE>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2258 template void elf::scanRelocations<ELF32BE>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2259 template void elf::scanRelocations<ELF64LE>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
2260 template void elf::scanRelocations<ELF64BE>();