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