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