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