1 //===- Relocations.cpp ----------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains platform-independent functions to process relocations.
11 // I'll describe the overview of this file here.
12 //
13 // Simple relocations are easy to handle for the linker. For example,
14 // for R_X86_64_PC64 relocs, the linker just has to fix up locations
15 // with the relative offsets to the target symbols. It would just be
16 // reading records from relocation sections and applying them to output.
17 //
18 // But not all relocations are that easy to handle. For example, for
19 // R_386_GOTOFF relocs, the linker has to create new GOT entries for
20 // symbols if they don't exist, and fix up locations with GOT entry
21 // offsets from the beginning of GOT section. So there is more than
22 // fixing addresses in relocation processing.
23 //
24 // ELF defines a large number of complex relocations.
25 //
26 // The functions in this file analyze relocations and do whatever needs
27 // to be done. It includes, but not limited to, the following.
28 //
29 //  - create GOT/PLT entries
30 //  - create new relocations in .dynsym to let the dynamic linker resolve
31 //    them at runtime (since ELF supports dynamic linking, not all
32 //    relocations can be resolved at link-time)
33 //  - create COPY relocs and reserve space in .bss
34 //  - replace expensive relocs (in terms of runtime cost) with cheap ones
35 //  - error out infeasible combinations such as PIC and non-relative relocs
36 //
37 // Note that the functions in this file don't actually apply relocations
38 // because it doesn't know about the output file nor the output file buffer.
39 // It instead stores Relocation objects to InputSection's Relocations
40 // vector to let it apply later in InputSection::writeTo.
41 //
42 //===----------------------------------------------------------------------===//
43 
44 #include "Relocations.h"
45 #include "Config.h"
46 #include "LinkerScript.h"
47 #include "OutputSections.h"
48 #include "Strings.h"
49 #include "SymbolTable.h"
50 #include "SyntheticSections.h"
51 #include "Target.h"
52 #include "Thunks.h"
53 #include "lld/Common/Memory.h"
54 
55 #include "llvm/Support/Endian.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include <algorithm>
58 
59 using namespace llvm;
60 using namespace llvm::ELF;
61 using namespace llvm::object;
62 using namespace llvm::support::endian;
63 
64 using namespace lld;
65 using namespace lld::elf;
66 
67 // Construct a message in the following format.
68 //
69 // >>> defined in /home/alice/src/foo.o
70 // >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
71 // >>>               /home/alice/src/bar.o:(.text+0x1)
72 template <class ELFT>
73 static std::string getLocation(InputSectionBase &S, const Symbol &Sym,
74                                uint64_t Off) {
75   std::string Msg =
76       "\n>>> defined in " + toString(Sym.File) + "\n>>> referenced by ";
77   std::string Src = S.getSrcMsg<ELFT>(Sym, Off);
78   if (!Src.empty())
79     Msg += Src + "\n>>>               ";
80   return Msg + S.getObjMsg(Off);
81 }
82 
83 // This is a MIPS-specific rule.
84 //
85 // In case of MIPS GP-relative relocations always resolve to a definition
86 // in a regular input file, ignoring the one-definition rule. So we,
87 // for example, should not attempt to create a dynamic relocation even
88 // if the target symbol is preemptible. There are two two MIPS GP-relative
89 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
90 // can be against a preemptible symbol.
91 //
92 // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
93 // relocation types occupy eight bit. In case of N64 ABI we extract first
94 // relocation from 3-in-1 packet because only the first relocation can
95 // be against a real symbol.
96 static bool isMipsGprel(RelType Type) {
97   if (Config->EMachine != EM_MIPS)
98     return false;
99   Type &= 0xff;
100   return Type == R_MIPS_GPREL16 || Type == R_MICROMIPS_GPREL16 ||
101          Type == R_MICROMIPS_GPREL7_S2;
102 }
103 
104 // This function is similar to the `handleTlsRelocation`. MIPS does not
105 // support any relaxations for TLS relocations so by factoring out MIPS
106 // handling in to the separate function we can simplify the code and do not
107 // pollute other `handleTlsRelocation` by MIPS `ifs` statements.
108 // Mips has a custom MipsGotSection that handles the writing of GOT entries
109 // without dynamic relocations.
110 template <class ELFT>
111 static unsigned handleMipsTlsRelocation(RelType Type, Symbol &Sym,
112                                         InputSectionBase &C, uint64_t Offset,
113                                         int64_t Addend, RelExpr Expr) {
114   if (Expr == R_MIPS_TLSLD) {
115     if (InX::MipsGot->addTlsIndex() && Config->Pic)
116       In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::MipsGot,
117                                    InX::MipsGot->getTlsIndexOff(), false,
118                                    nullptr, 0});
119     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
120     return 1;
121   }
122 
123   if (Expr == R_MIPS_TLSGD) {
124     if (InX::MipsGot->addDynTlsEntry(Sym) && Sym.IsPreemptible) {
125       uint64_t Off = InX::MipsGot->getGlobalDynOffset(Sym);
126       In<ELFT>::RelaDyn->addReloc(
127           {Target->TlsModuleIndexRel, InX::MipsGot, Off, false, &Sym, 0});
128       if (Sym.IsPreemptible)
129         In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, InX::MipsGot,
130                                      Off + Config->Wordsize, false, &Sym, 0});
131     }
132     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
133     return 1;
134   }
135   return 0;
136 }
137 
138 // This function is similar to the `handleMipsTlsRelocation`. ARM also does not
139 // support any relaxations for TLS relocations. ARM is logically similar to Mips
140 // in how it handles TLS, but Mips uses its own custom GOT which handles some
141 // of the cases that ARM uses GOT relocations for.
142 //
143 // We look for TLS global dynamic and local dynamic relocations, these may
144 // require the generation of a pair of GOT entries that have associated
145 // dynamic relocations. When the results of the dynamic relocations can be
146 // resolved at static link time we do so. This is necessary for static linking
147 // as there will be no dynamic loader to resolve them at load-time.
148 //
149 // The pair of GOT entries created are of the form
150 // GOT[e0] Module Index (Used to find pointer to TLS block at run-time)
151 // GOT[e1] Offset of symbol in TLS block
152 template <class ELFT>
153 static unsigned handleARMTlsRelocation(RelType Type, Symbol &Sym,
154                                        InputSectionBase &C, uint64_t Offset,
155                                        int64_t Addend, RelExpr Expr) {
156   // The Dynamic TLS Module Index Relocation for a symbol defined in an
157   // executable is always 1. If the target Symbol is not preemptible then
158   // we know the offset into the TLS block at static link time.
159   bool NeedDynId = Sym.IsPreemptible || Config->Shared;
160   bool NeedDynOff = Sym.IsPreemptible;
161 
162   auto AddTlsReloc = [&](uint64_t Off, RelType Type, Symbol *Dest, bool Dyn) {
163     if (Dyn)
164       In<ELFT>::RelaDyn->addReloc({Type, InX::Got, Off, false, Dest, 0});
165     else
166       InX::Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
167   };
168 
169   // Local Dynamic is for access to module local TLS variables, while still
170   // being suitable for being dynamically loaded via dlopen.
171   // GOT[e0] is the module index, with a special value of 0 for the current
172   // module. GOT[e1] is unused. There only needs to be one module index entry.
173   if (Expr == R_TLSLD_PC && InX::Got->addTlsIndex()) {
174     AddTlsReloc(InX::Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
175                 NeedDynId ? nullptr : &Sym, NeedDynId);
176     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
177     return 1;
178   }
179 
180   // Global Dynamic is the most general purpose access model. When we know
181   // the module index and offset of symbol in TLS block we can fill these in
182   // using static GOT relocations.
183   if (Expr == R_TLSGD_PC) {
184     if (InX::Got->addDynTlsEntry(Sym)) {
185       uint64_t Off = InX::Got->getGlobalDynOffset(Sym);
186       AddTlsReloc(Off, Target->TlsModuleIndexRel, &Sym, NeedDynId);
187       AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Sym,
188                   NeedDynOff);
189     }
190     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
191     return 1;
192   }
193   return 0;
194 }
195 
196 // Returns the number of relocations processed.
197 template <class ELFT>
198 static unsigned
199 handleTlsRelocation(RelType Type, Symbol &Sym, InputSectionBase &C,
200                     typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201   if (!(C.Flags & SHF_ALLOC))
202     return 0;
203 
204   if (!Sym.isTls())
205     return 0;
206 
207   if (Config->EMachine == EM_ARM)
208     return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
209   if (Config->EMachine == EM_MIPS)
210     return handleMipsTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
211 
212   if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
213       Config->Shared) {
214     if (InX::Got->addDynTlsEntry(Sym)) {
215       uint64_t Off = InX::Got->getGlobalDynOffset(Sym);
216       In<ELFT>::RelaDyn->addReloc(
217           {Target->TlsDescRel, InX::Got, Off, !Sym.IsPreemptible, &Sym, 0});
218     }
219     if (Expr != R_TLSDESC_CALL)
220       C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
221     return 1;
222   }
223 
224   if (isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)) {
225     // Local-Dynamic relocs can be relaxed to Local-Exec.
226     if (!Config->Shared) {
227       C.Relocations.push_back(
228           {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
229       return 2;
230     }
231     if (InX::Got->addTlsIndex())
232       In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::Got,
233                                    InX::Got->getTlsIndexOff(), false, nullptr,
234                                    0});
235     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
236     return 1;
237   }
238 
239   // Local-Dynamic relocs can be relaxed to Local-Exec.
240   if (isRelExprOneOf<R_ABS, R_TLSLD, R_TLSLD_PC>(Expr) && !Config->Shared) {
241     C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
242     return 1;
243   }
244 
245   if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD,
246                      R_TLSGD_PC>(Expr)) {
247     if (Config->Shared) {
248       if (InX::Got->addDynTlsEntry(Sym)) {
249         uint64_t Off = InX::Got->getGlobalDynOffset(Sym);
250         In<ELFT>::RelaDyn->addReloc(
251             {Target->TlsModuleIndexRel, InX::Got, Off, false, &Sym, 0});
252 
253         // If the symbol is preemptible we need the dynamic linker to write
254         // the offset too.
255         uint64_t OffsetOff = Off + Config->Wordsize;
256         if (Sym.IsPreemptible)
257           In<ELFT>::RelaDyn->addReloc(
258               {Target->TlsOffsetRel, InX::Got, OffsetOff, false, &Sym, 0});
259         else
260           InX::Got->Relocations.push_back(
261               {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
262       }
263       C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
264       return 1;
265     }
266 
267     // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
268     // depending on the symbol being locally defined or not.
269     if (Sym.IsPreemptible) {
270       C.Relocations.push_back(
271           {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
272            Offset, Addend, &Sym});
273       if (!Sym.isInGot()) {
274         InX::Got->addEntry(Sym);
275         In<ELFT>::RelaDyn->addReloc(
276             {Target->TlsGotRel, InX::Got, Sym.getGotOffset(), false, &Sym, 0});
277       }
278     } else {
279       C.Relocations.push_back(
280           {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
281            Offset, Addend, &Sym});
282     }
283     return Target->TlsGdRelaxSkip;
284   }
285 
286   // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
287   // defined.
288   if (isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_GOT_PAGE_PC>(Expr) &&
289       !Config->Shared && !Sym.IsPreemptible) {
290     C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
291     return 1;
292   }
293 
294   if (Expr == R_TLSDESC_CALL)
295     return 1;
296   return 0;
297 }
298 
299 static RelType getMipsPairType(RelType Type, bool IsLocal) {
300   switch (Type) {
301   case R_MIPS_HI16:
302     return R_MIPS_LO16;
303   case R_MIPS_GOT16:
304     // In case of global symbol, the R_MIPS_GOT16 relocation does not
305     // have a pair. Each global symbol has a unique entry in the GOT
306     // and a corresponding instruction with help of the R_MIPS_GOT16
307     // relocation loads an address of the symbol. In case of local
308     // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
309     // the high 16 bits of the symbol's value. A paired R_MIPS_LO16
310     // relocations handle low 16 bits of the address. That allows
311     // to allocate only one GOT entry for every 64 KBytes of local data.
312     return IsLocal ? R_MIPS_LO16 : R_MIPS_NONE;
313   case R_MICROMIPS_GOT16:
314     return IsLocal ? R_MICROMIPS_LO16 : R_MIPS_NONE;
315   case R_MIPS_PCHI16:
316     return R_MIPS_PCLO16;
317   case R_MICROMIPS_HI16:
318     return R_MICROMIPS_LO16;
319   default:
320     return R_MIPS_NONE;
321   }
322 }
323 
324 // True if non-preemptable symbol always has the same value regardless of where
325 // the DSO is loaded.
326 static bool isAbsolute(const Symbol &Sym) {
327   if (Sym.isUndefWeak())
328     return true;
329   if (const auto *DR = dyn_cast<Defined>(&Sym))
330     return DR->Section == nullptr; // Absolute symbol.
331   return false;
332 }
333 
334 static bool isAbsoluteValue(const Symbol &Sym) {
335   return isAbsolute(Sym) || Sym.isTls();
336 }
337 
338 // Returns true if Expr refers a PLT entry.
339 static bool needsPlt(RelExpr Expr) {
340   return isRelExprOneOf<R_PLT_PC, R_PPC_PLT_OPD, R_PLT, R_PLT_PAGE_PC>(Expr);
341 }
342 
343 // Returns true if Expr refers a GOT entry. Note that this function
344 // returns false for TLS variables even though they need GOT, because
345 // TLS variables uses GOT differently than the regular variables.
346 static bool needsGot(RelExpr Expr) {
347   return isRelExprOneOf<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
348                         R_MIPS_GOT_OFF32, R_GOT_PAGE_PC, R_GOT_PC,
349                         R_GOT_FROM_END>(Expr);
350 }
351 
352 // True if this expression is of the form Sym - X, where X is a position in the
353 // file (PC, or GOT for example).
354 static bool isRelExpr(RelExpr Expr) {
355   return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL,
356                         R_PAGE_PC, R_RELAX_GOT_PC>(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 template <class ELFT>
369 static bool isStaticLinkTimeConstant(RelExpr E, RelType Type, const Symbol &Sym,
370                                      InputSectionBase &S, uint64_t RelOff) {
371   // These expressions always compute a constant
372   if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
373                      R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
374                      R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
375                      R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
376                      R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
377     return true;
378 
379   // These never do, except if the entire file is position dependent or if
380   // only the low bits are used.
381   if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
382     return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
383 
384   if (Sym.IsPreemptible)
385     return false;
386   if (!Config->Pic)
387     return true;
388 
389   // For the target and the relocation, we want to know if they are
390   // absolute or relative.
391   bool AbsVal = isAbsoluteValue(Sym);
392   bool RelE = isRelExpr(E);
393   if (AbsVal && !RelE)
394     return true;
395   if (!AbsVal && RelE)
396     return true;
397   if (!AbsVal && !RelE)
398     return Target->usesOnlyLowPageBits(Type);
399 
400   // Relative relocation to an absolute value. This is normally unrepresentable,
401   // but if the relocation refers to a weak undefined symbol, we allow it to
402   // resolve to the image base. This is a little strange, but it allows us to
403   // link function calls to such symbols. Normally such a call will be guarded
404   // with a comparison, which will load a zero from the GOT.
405   // Another special case is MIPS _gp_disp symbol which represents offset
406   // between start of a function and '_gp' value and defined as absolute just
407   // to simplify the code.
408   assert(AbsVal && RelE);
409   if (Sym.isUndefWeak())
410     return true;
411 
412   error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
413         toString(Sym) + getLocation<ELFT>(S, Sym, RelOff));
414   return true;
415 }
416 
417 static RelExpr toPlt(RelExpr Expr) {
418   if (Expr == R_PPC_OPD)
419     return R_PPC_PLT_OPD;
420   if (Expr == R_PC)
421     return R_PLT_PC;
422   if (Expr == R_PAGE_PC)
423     return R_PLT_PAGE_PC;
424   if (Expr == R_ABS)
425     return R_PLT;
426   return Expr;
427 }
428 
429 static RelExpr fromPlt(RelExpr Expr) {
430   // We decided not to use a plt. Optimize a reference to the plt to a
431   // reference to the symbol itself.
432   if (Expr == R_PLT_PC)
433     return R_PC;
434   if (Expr == R_PPC_PLT_OPD)
435     return R_PPC_OPD;
436   if (Expr == R_PLT)
437     return R_ABS;
438   return Expr;
439 }
440 
441 // Returns true if a given shared symbol is in a read-only segment in a DSO.
442 template <class ELFT> static bool isReadOnly(SharedSymbol *SS) {
443   typedef typename ELFT::Phdr Elf_Phdr;
444 
445   // Determine if the symbol is read-only by scanning the DSO's program headers.
446   const SharedFile<ELFT> *File = SS->getFile<ELFT>();
447   for (const Elf_Phdr &Phdr : check(File->getObj().program_headers()))
448     if ((Phdr.p_type == ELF::PT_LOAD || Phdr.p_type == ELF::PT_GNU_RELRO) &&
449         !(Phdr.p_flags & ELF::PF_W) && SS->Value >= Phdr.p_vaddr &&
450         SS->Value < Phdr.p_vaddr + Phdr.p_memsz)
451       return true;
452   return false;
453 }
454 
455 // Returns symbols at the same offset as a given symbol, including SS itself.
456 //
457 // If two or more symbols are at the same offset, and at least one of
458 // them are copied by a copy relocation, all of them need to be copied.
459 // Otherwise, they would refer different places at runtime.
460 template <class ELFT>
461 static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) {
462   typedef typename ELFT::Sym Elf_Sym;
463 
464   SharedFile<ELFT> *File = SS->getFile<ELFT>();
465 
466   std::vector<SharedSymbol *> Ret;
467   for (const Elf_Sym &S : File->getGlobalELFSyms()) {
468     if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
469         S.st_value != SS->Value)
470       continue;
471     StringRef Name = check(S.getName(File->getStringTable()));
472     Symbol *Sym = Symtab->find(Name);
473     if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
474       Ret.push_back(Alias);
475   }
476   return Ret;
477 }
478 
479 // Reserve space in .bss or .bss.rel.ro for copy relocation.
480 //
481 // The copy relocation is pretty much a hack. If you use a copy relocation
482 // in your program, not only the symbol name but the symbol's size, RW/RO
483 // bit and alignment become part of the ABI. In addition to that, if the
484 // symbol has aliases, the aliases become part of the ABI. That's subtle,
485 // but if you violate that implicit ABI, that can cause very counter-
486 // intuitive consequences.
487 //
488 // So, what is the copy relocation? It's for linking non-position
489 // independent code to DSOs. In an ideal world, all references to data
490 // exported by DSOs should go indirectly through GOT. But if object files
491 // are compiled as non-PIC, all data references are direct. There is no
492 // way for the linker to transform the code to use GOT, as machine
493 // instructions are already set in stone in object files. This is where
494 // the copy relocation takes a role.
495 //
496 // A copy relocation instructs the dynamic linker to copy data from a DSO
497 // to a specified address (which is usually in .bss) at load-time. If the
498 // static linker (that's us) finds a direct data reference to a DSO
499 // symbol, it creates a copy relocation, so that the symbol can be
500 // resolved as if it were in .bss rather than in a DSO.
501 //
502 // As you can see in this function, we create a copy relocation for the
503 // dynamic linker, and the relocation contains not only symbol name but
504 // various other informtion about the symbol. So, such attributes become a
505 // part of the ABI.
506 //
507 // Note for application developers: I can give you a piece of advice if
508 // you are writing a shared library. You probably should export only
509 // functions from your library. You shouldn't export variables.
510 //
511 // As an example what can happen when you export variables without knowing
512 // the semantics of copy relocations, assume that you have an exported
513 // variable of type T. It is an ABI-breaking change to add new members at
514 // end of T even though doing that doesn't change the layout of the
515 // existing members. That's because the space for the new members are not
516 // reserved in .bss unless you recompile the main program. That means they
517 // are likely to overlap with other data that happens to be laid out next
518 // to the variable in .bss. This kind of issue is sometimes very hard to
519 // debug. What's a solution? Instead of exporting a varaible V from a DSO,
520 // define an accessor getV().
521 template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) {
522   // Copy relocation against zero-sized symbol doesn't make sense.
523   uint64_t SymSize = SS->getSize();
524   if (SymSize == 0)
525     fatal("cannot create a copy relocation for symbol " + toString(*SS));
526 
527   // See if this symbol is in a read-only segment. If so, preserve the symbol's
528   // memory protection by reserving space in the .bss.rel.ro section.
529   bool IsReadOnly = isReadOnly<ELFT>(SS);
530   BssSection *Sec = make<BssSection>(IsReadOnly ? ".bss.rel.ro" : ".bss",
531                                      SymSize, SS->Alignment);
532   if (IsReadOnly)
533     InX::BssRelRo->getParent()->addSection(Sec);
534   else
535     InX::Bss->getParent()->addSection(Sec);
536 
537   // Look through the DSO's dynamic symbol table for aliases and create a
538   // dynamic symbol for each one. This causes the copy relocation to correctly
539   // interpose any aliases.
540   for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) {
541     Sym->CopyRelSec = Sec;
542     Sym->IsPreemptible = false;
543     Sym->IsUsedInRegularObj = true;
544     Sym->Used = true;
545   }
546 
547   In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, 0, false, SS, 0});
548 }
549 
550 static void errorOrWarn(const Twine &Msg) {
551   if (!Config->NoinhibitExec)
552     error(Msg);
553   else
554     warn(Msg);
555 }
556 
557 template <class ELFT>
558 static RelExpr adjustExpr(Symbol &Sym, RelExpr Expr, RelType Type,
559                           InputSectionBase &S, uint64_t RelOff) {
560   // We can create any dynamic relocation if a section is simply writable.
561   if (S.Flags & SHF_WRITE)
562     return Expr;
563 
564   // Or, if we are allowed to create dynamic relocations against
565   // read-only sections (i.e. unless "-z notext" is given),
566   // we can create a dynamic relocation as we want, too.
567   if (!Config->ZText)
568     return Expr;
569 
570   // If a relocation can be applied at link-time, we don't need to
571   // create a dynamic relocation in the first place.
572   if (isStaticLinkTimeConstant<ELFT>(Expr, Type, Sym, S, RelOff))
573     return Expr;
574 
575   // If we got here we know that this relocation would require the dynamic
576   // linker to write a value to read only memory.
577 
578   // If the relocation is to a weak undef, give up on it and produce a
579   // non preemptible 0.
580   if (Sym.isUndefWeak()) {
581     Sym.IsPreemptible = false;
582     return Expr;
583   }
584 
585   // We can hack around it if we are producing an executable and
586   // the refered symbol can be preemepted to refer to the executable.
587   if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
588     error(
589         "can't create dynamic relocation " + toString(Type) + " against " +
590         (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
591         " in readonly segment; recompile object files with -fPIC" +
592         getLocation<ELFT>(S, Sym, RelOff));
593     return Expr;
594   }
595 
596   if (Sym.getVisibility() != STV_DEFAULT) {
597     error("cannot preempt symbol: " + toString(Sym) +
598           getLocation<ELFT>(S, Sym, RelOff));
599     return Expr;
600   }
601 
602   if (Sym.isObject()) {
603     // Produce a copy relocation.
604     auto *B = cast<SharedSymbol>(&Sym);
605     if (!B->CopyRelSec) {
606       if (Config->ZNocopyreloc)
607         error("unresolvable relocation " + toString(Type) +
608               " against symbol '" + toString(*B) +
609               "'; recompile with -fPIC or remove '-z nocopyreloc'" +
610               getLocation<ELFT>(S, Sym, RelOff));
611 
612       addCopyRelSymbol<ELFT>(B);
613     }
614     return Expr;
615   }
616 
617   if (Sym.isFunc()) {
618     // This handles a non PIC program call to function in a shared library. In
619     // an ideal world, we could just report an error saying the relocation can
620     // overflow at runtime. In the real world with glibc, crt1.o has a
621     // R_X86_64_PC32 pointing to libc.so.
622     //
623     // The general idea on how to handle such cases is to create a PLT entry and
624     // use that as the function value.
625     //
626     // For the static linking part, we just return a plt expr and everything
627     // else will use the the PLT entry as the address.
628     //
629     // The remaining problem is making sure pointer equality still works. We
630     // need the help of the dynamic linker for that. We let it know that we have
631     // a direct reference to a so symbol by creating an undefined symbol with a
632     // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
633     // the value of the symbol we created. This is true even for got entries, so
634     // pointer equality is maintained. To avoid an infinite loop, the only entry
635     // that points to the real function is a dedicated got entry used by the
636     // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
637     // R_386_JMP_SLOT, etc).
638     Sym.NeedsPltAddr = true;
639     Sym.IsPreemptible = false;
640     return toPlt(Expr);
641   }
642 
643   errorOrWarn("symbol '" + toString(Sym) + "' defined in " +
644               toString(Sym.File) + " has no type");
645   return Expr;
646 }
647 
648 // MIPS has an odd notion of "paired" relocations to calculate addends.
649 // For example, if a relocation is of R_MIPS_HI16, there must be a
650 // R_MIPS_LO16 relocation after that, and an addend is calculated using
651 // the two relocations.
652 template <class ELFT, class RelTy>
653 static int64_t computeMipsAddend(const RelTy &Rel, const RelTy *End,
654                                  InputSectionBase &Sec, RelExpr Expr,
655                                  bool IsLocal) {
656   if (Expr == R_MIPS_GOTREL && IsLocal)
657     return Sec.getFile<ELFT>()->MipsGp0;
658 
659   // The ABI says that the paired relocation is used only for REL.
660   // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
661   if (RelTy::IsRela)
662     return 0;
663 
664   RelType Type = Rel.getType(Config->IsMips64EL);
665   uint32_t PairTy = getMipsPairType(Type, IsLocal);
666   if (PairTy == R_MIPS_NONE)
667     return 0;
668 
669   const uint8_t *Buf = Sec.Data.data();
670   uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
671 
672   // To make things worse, paired relocations might not be contiguous in
673   // the relocation table, so we need to do linear search. *sigh*
674   for (const RelTy *RI = &Rel; RI != End; ++RI)
675     if (RI->getType(Config->IsMips64EL) == PairTy &&
676         RI->getSymbol(Config->IsMips64EL) == SymIndex)
677       return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
678 
679   warn("can't find matching " + toString(PairTy) + " relocation for " +
680        toString(Type));
681   return 0;
682 }
683 
684 // Returns an addend of a given relocation. If it is RELA, an addend
685 // is in a relocation itself. If it is REL, we need to read it from an
686 // input section.
687 template <class ELFT, class RelTy>
688 static int64_t computeAddend(const RelTy &Rel, const RelTy *End,
689                              InputSectionBase &Sec, RelExpr Expr,
690                              bool IsLocal) {
691   int64_t Addend;
692   RelType Type = Rel.getType(Config->IsMips64EL);
693 
694   if (RelTy::IsRela) {
695     Addend = getAddend<ELFT>(Rel);
696   } else {
697     const uint8_t *Buf = Sec.Data.data();
698     Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
699   }
700 
701   if (Config->EMachine == EM_PPC64 && Config->Pic && Type == R_PPC64_TOC)
702     Addend += getPPC64TocBase();
703   if (Config->EMachine == EM_MIPS)
704     Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
705 
706   return Addend;
707 }
708 
709 // Report an undefined symbol if necessary.
710 // Returns true if this function printed out an error message.
711 template <class ELFT>
712 static bool maybeReportUndefined(Symbol &Sym, InputSectionBase &Sec,
713                                  uint64_t Offset) {
714   if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
715     return false;
716 
717   if (Sym.isLocal() || !Sym.isUndefined() || Sym.isWeak())
718     return false;
719 
720   bool CanBeExternal =
721       Sym.computeBinding() != STB_LOCAL && Sym.getVisibility() == STV_DEFAULT;
722   if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && CanBeExternal)
723     return false;
724 
725   std::string Msg =
726       "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
727 
728   std::string Src = Sec.getSrcMsg<ELFT>(Sym, Offset);
729   if (!Src.empty())
730     Msg += Src + "\n>>>               ";
731   Msg += Sec.getObjMsg(Offset);
732 
733   if ((Config->UnresolvedSymbols == UnresolvedPolicy::Warn && CanBeExternal) ||
734       Config->NoinhibitExec) {
735     warn(Msg);
736     return false;
737   }
738 
739   error(Msg);
740   return true;
741 }
742 
743 // MIPS N32 ABI treats series of successive relocations with the same offset
744 // as a single relocation. The similar approach used by N64 ABI, but this ABI
745 // packs all relocations into the single relocation record. Here we emulate
746 // this for the N32 ABI. Iterate over relocation with the same offset and put
747 // theirs types into the single bit-set.
748 template <class RelTy> static RelType getMipsN32RelType(RelTy *&Rel, RelTy *End) {
749   RelType Type = Rel->getType(Config->IsMips64EL);
750   uint64_t Offset = Rel->r_offset;
751 
752   int N = 0;
753   while (Rel + 1 != End && (Rel + 1)->r_offset == Offset)
754     Type |= (++Rel)->getType(Config->IsMips64EL) << (8 * ++N);
755   return Type;
756 }
757 
758 // .eh_frame sections are mergeable input sections, so their input
759 // offsets are not linearly mapped to output section. For each input
760 // offset, we need to find a section piece containing the offset and
761 // add the piece's base address to the input offset to compute the
762 // output offset. That isn't cheap.
763 //
764 // This class is to speed up the offset computation. When we process
765 // relocations, we access offsets in the monotonically increasing
766 // order. So we can optimize for that access pattern.
767 //
768 // For sections other than .eh_frame, this class doesn't do anything.
769 namespace {
770 class OffsetGetter {
771 public:
772   explicit OffsetGetter(InputSectionBase &Sec) {
773     if (auto *Eh = dyn_cast<EhInputSection>(&Sec))
774       Pieces = Eh->Pieces;
775   }
776 
777   // Translates offsets in input sections to offsets in output sections.
778   // Given offset must increase monotonically. We assume that Piece is
779   // sorted by InputOff.
780   uint64_t get(uint64_t Off) {
781     if (Pieces.empty())
782       return Off;
783 
784     while (I != Pieces.size() && Pieces[I].InputOff + Pieces[I].Size <= Off)
785       ++I;
786     if (I == Pieces.size())
787       return Off;
788 
789     // Pieces must be contiguous, so there must be no holes in between.
790     assert(Pieces[I].InputOff <= Off && "Relocation not in any piece");
791 
792     // Offset -1 means that the piece is dead (i.e. garbage collected).
793     if (Pieces[I].OutputOff == -1)
794       return -1;
795     return Pieces[I].OutputOff + Off - Pieces[I].InputOff;
796   }
797 
798 private:
799   ArrayRef<EhSectionPiece> Pieces;
800   size_t I = 0;
801 };
802 } // namespace
803 
804 template <class ELFT, class GotPltSection>
805 static void addPltEntry(PltSection *Plt, GotPltSection *GotPlt,
806                         RelocationSection<ELFT> *Rel, RelType Type, Symbol &Sym,
807                         bool UseSymVA) {
808   Plt->addEntry<ELFT>(Sym);
809   GotPlt->addEntry(Sym);
810   Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
811 }
812 
813 template <class ELFT> static void addGotEntry(Symbol &Sym, bool Preemptible) {
814   InX::Got->addEntry(Sym);
815 
816   RelExpr Expr = Sym.isTls() ? R_TLS : R_ABS;
817   uint64_t Off = Sym.getGotOffset();
818 
819   // If a GOT slot value can be calculated at link-time, which is now,
820   // we can just fill that out.
821   //
822   // (We don't actually write a value to a GOT slot right now, but we
823   // add a static relocation to a Relocations vector so that
824   // InputSection::relocate will do the work for us. We may be able
825   // to just write a value now, but it is a TODO.)
826   bool IsLinkTimeConstant = !Preemptible && (!Config->Pic || isAbsolute(Sym));
827   if (IsLinkTimeConstant) {
828     InX::Got->Relocations.push_back({Expr, Target->GotRel, Off, 0, &Sym});
829     return;
830   }
831 
832   // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
833   // the GOT slot will be fixed at load-time.
834   RelType Type;
835   if (Sym.isTls())
836     Type = Target->TlsGotRel;
837   else if (!Preemptible && Config->Pic && !isAbsolute(Sym))
838     Type = Target->RelativeRel;
839   else
840     Type = Target->GotRel;
841   In<ELFT>::RelaDyn->addReloc({Type, InX::Got, Off, !Preemptible, &Sym, 0});
842 
843   // REL type relocations don't have addend fields unlike RELAs, and
844   // their addends are stored to the section to which they are applied.
845   // So, store addends if we need to.
846   //
847   // This is ugly -- the difference between REL and RELA should be
848   // handled in a better way. It's a TODO.
849   if (!Config->IsRela)
850     InX::Got->Relocations.push_back({R_ABS, Target->GotRel, Off, 0, &Sym});
851 }
852 
853 // The reason we have to do this early scan is as follows
854 // * To mmap the output file, we need to know the size
855 // * For that, we need to know how many dynamic relocs we will have.
856 // It might be possible to avoid this by outputting the file with write:
857 // * Write the allocated output sections, computing addresses.
858 // * Apply relocations, recording which ones require a dynamic reloc.
859 // * Write the dynamic relocations.
860 // * Write the rest of the file.
861 // This would have some drawbacks. For example, we would only know if .rela.dyn
862 // is needed after applying relocations. If it is, it will go after rw and rx
863 // sections. Given that it is ro, we will need an extra PT_LOAD. This
864 // complicates things for the dynamic linker and means we would have to reserve
865 // space for the extra PT_LOAD even if we end up not using it.
866 template <class ELFT, class RelTy>
867 static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
868   OffsetGetter GetOffset(Sec);
869 
870   for (auto I = Rels.begin(), End = Rels.end(); I != End; ++I) {
871     const RelTy &Rel = *I;
872     Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
873     RelType Type = Rel.getType(Config->IsMips64EL);
874 
875     // Deal with MIPS oddity.
876     if (Config->MipsN32Abi)
877       Type = getMipsN32RelType(I, End);
878 
879     // Get an offset in an output section this relocation is applied to.
880     uint64_t Offset = GetOffset.get(Rel.r_offset);
881     if (Offset == uint64_t(-1))
882       continue;
883 
884     // Skip if the target symbol is an erroneous undefined symbol.
885     if (maybeReportUndefined<ELFT>(Sym, Sec, Rel.r_offset))
886       continue;
887 
888     RelExpr Expr =
889         Target->getRelExpr(Type, Sym, Sec.Data.begin() + Rel.r_offset);
890 
891     // Ignore "hint" relocations because they are only markers for relaxation.
892     if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
893       continue;
894 
895     // Handle yet another MIPS-ness.
896     if (isMipsGprel(Type)) {
897       int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
898       Sec.Relocations.push_back({R_MIPS_GOTREL, Type, Offset, Addend, &Sym});
899       continue;
900     }
901 
902     bool Preemptible = Sym.IsPreemptible;
903 
904     // Strenghten or relax a PLT access.
905     //
906     // GNU ifunc symbols must be accessed via PLT because their addresses
907     // are determined by runtime.
908     //
909     // On the other hand, if we know that a PLT entry will be resolved within
910     // the same ELF module, we can skip PLT access and directly jump to the
911     // destination function. For example, if we are linking a main exectuable,
912     // all dynamic symbols that can be resolved within the executable will
913     // actually be resolved that way at runtime, because the main exectuable
914     // is always at the beginning of a search list. We can leverage that fact.
915     if (Sym.isGnuIFunc())
916       Expr = toPlt(Expr);
917     else if (!Preemptible && Expr == R_GOT_PC && !isAbsoluteValue(Sym))
918       Expr =
919           Target->adjustRelaxExpr(Type, Sec.Data.data() + Rel.r_offset, Expr);
920     else if (!Preemptible)
921       Expr = fromPlt(Expr);
922 
923     Expr = adjustExpr<ELFT>(Sym, Expr, Type, Sec, Rel.r_offset);
924     if (errorCount())
925       continue;
926 
927     // This relocation does not require got entry, but it is relative to got and
928     // needs it to be created. Here we request for that.
929     if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
930                        R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
931       InX::Got->HasGotOffRel = true;
932 
933     // Read an addend.
934     int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
935 
936     // Process some TLS relocations, including relaxing TLS relocations.
937     // Note that this function does not handle all TLS relocations.
938     if (unsigned Processed =
939             handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
940       I += (Processed - 1);
941       continue;
942     }
943 
944     // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
945     if (needsPlt(Expr) && !Sym.isInPlt()) {
946       if (Sym.isGnuIFunc() && !Preemptible)
947         addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
948                     Target->IRelativeRel, Sym, true);
949       else
950         addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
951                     Sym, !Preemptible);
952     }
953 
954     // Create a GOT slot if a relocation needs GOT.
955     if (needsGot(Expr)) {
956       if (Config->EMachine == EM_MIPS) {
957         // MIPS ABI has special rules to process GOT entries and doesn't
958         // require relocation entries for them. A special case is TLS
959         // relocations. In that case dynamic loader applies dynamic
960         // relocations to initialize TLS GOT entries.
961         // See "Global Offset Table" in Chapter 5 in the following document
962         // for detailed description:
963         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
964         InX::MipsGot->addEntry(Sym, Addend, Expr);
965         if (Sym.isTls() && Sym.IsPreemptible)
966           In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
967                                        Sym.getGotOffset(), false, &Sym, 0});
968       } else if (!Sym.isInGot()) {
969         addGotEntry<ELFT>(Sym, Preemptible);
970       }
971     }
972 
973     if (!needsPlt(Expr) && !needsGot(Expr) && Sym.IsPreemptible) {
974       // We don't know anything about the finaly symbol. Just ask the dynamic
975       // linker to handle the relocation for us.
976       if (!Target->isPicRel(Type))
977         errorOrWarn(
978             "relocation " + toString(Type) +
979             " cannot be used against shared object; recompile with -fPIC" +
980             getLocation<ELFT>(Sec, Sym, Offset));
981 
982       In<ELFT>::RelaDyn->addReloc(
983           {Target->getDynRel(Type), &Sec, Offset, false, &Sym, Addend});
984 
985       // MIPS ABI turns using of GOT and dynamic relocations inside out.
986       // While regular ABI uses dynamic relocations to fill up GOT entries
987       // MIPS ABI requires dynamic linker to fills up GOT entries using
988       // specially sorted dynamic symbol table. This affects even dynamic
989       // relocations against symbols which do not require GOT entries
990       // creation explicitly, i.e. do not have any GOT-relocations. So if
991       // a preemptible symbol has a dynamic relocation we anyway have
992       // to create a GOT entry for it.
993       // If a non-preemptible symbol has a dynamic relocation against it,
994       // dynamic linker takes it st_value, adds offset and writes down
995       // result of the dynamic relocation. In case of preemptible symbol
996       // dynamic linker performs symbol resolution, writes the symbol value
997       // to the GOT entry and reads the GOT entry when it needs to perform
998       // a dynamic relocation.
999       // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
1000       if (Config->EMachine == EM_MIPS)
1001         InX::MipsGot->addEntry(Sym, Addend, Expr);
1002       continue;
1003     }
1004 
1005     // If the relocation points to something in the file, we can process it.
1006     bool IsConstant =
1007         isStaticLinkTimeConstant<ELFT>(Expr, Type, Sym, Sec, Rel.r_offset);
1008 
1009     // The size is not going to change, so we fold it in here.
1010     if (Expr == R_SIZE)
1011       Addend += Sym.getSize();
1012 
1013     // If the produced value is a constant, we just remember to write it
1014     // when outputting this section. We also have to do it if the format
1015     // uses Elf_Rel, since in that case the written value is the addend.
1016     if (IsConstant) {
1017       Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
1018       continue;
1019     }
1020 
1021     // If the output being produced is position independent, the final value
1022     // is still not known. In that case we still need some help from the
1023     // dynamic linker. We can however do better than just copying the incoming
1024     // relocation. We can process some of it and and just ask the dynamic
1025     // linker to add the load address.
1026     if (Config->IsRela) {
1027       In<ELFT>::RelaDyn->addReloc(
1028           {Target->RelativeRel, &Sec, Offset, true, &Sym, Addend});
1029     } else {
1030       // In REL, addends are stored to the target section.
1031       In<ELFT>::RelaDyn->addReloc(
1032           {Target->RelativeRel, &Sec, Offset, true, &Sym, 0});
1033       Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
1034     }
1035   }
1036 }
1037 
1038 template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
1039   if (S.AreRelocsRela)
1040     scanRelocs<ELFT>(S, S.relas<ELFT>());
1041   else
1042     scanRelocs<ELFT>(S, S.rels<ELFT>());
1043 }
1044 
1045 // Thunk Implementation
1046 //
1047 // Thunks (sometimes called stubs, veneers or branch islands) are small pieces
1048 // of code that the linker inserts inbetween a caller and a callee. The thunks
1049 // are added at link time rather than compile time as the decision on whether
1050 // a thunk is needed, such as the caller and callee being out of range, can only
1051 // be made at link time.
1052 //
1053 // It is straightforward to tell given the current state of the program when a
1054 // thunk is needed for a particular call. The more difficult part is that
1055 // the thunk needs to be placed in the program such that the caller can reach
1056 // the thunk and the thunk can reach the callee; furthermore, adding thunks to
1057 // the program alters addresses, which can mean more thunks etc.
1058 //
1059 // In lld we have a synthetic ThunkSection that can hold many Thunks.
1060 // The decision to have a ThunkSection act as a container means that we can
1061 // more easily handle the most common case of a single block of contiguous
1062 // Thunks by inserting just a single ThunkSection.
1063 //
1064 // The implementation of Thunks in lld is split across these areas
1065 // Relocations.cpp : Framework for creating and placing thunks
1066 // Thunks.cpp : The code generated for each supported thunk
1067 // Target.cpp : Target specific hooks that the framework uses to decide when
1068 //              a thunk is used
1069 // Synthetic.cpp : Implementation of ThunkSection
1070 // Writer.cpp : Iteratively call framework until no more Thunks added
1071 //
1072 // Thunk placement requirements:
1073 // Mips LA25 thunks. These must be placed immediately before the callee section
1074 // We can assume that the caller is in range of the Thunk. These are modelled
1075 // by Thunks that return the section they must precede with
1076 // getTargetInputSection().
1077 //
1078 // ARM interworking and range extension thunks. These thunks must be placed
1079 // within range of the caller. All implemented ARM thunks can always reach the
1080 // callee as they use an indirect jump via a register that has no range
1081 // restrictions.
1082 //
1083 // Thunk placement algorithm:
1084 // For Mips LA25 ThunkSections; the placement is explicit, it has to be before
1085 // getTargetInputSection().
1086 //
1087 // For thunks that must be placed within range of the caller there are many
1088 // possible choices given that the maximum range from the caller is usually
1089 // much larger than the average InputSection size. Desirable properties include:
1090 // - Maximize reuse of thunks by multiple callers
1091 // - Minimize number of ThunkSections to simplify insertion
1092 // - Handle impact of already added Thunks on addresses
1093 // - Simple to understand and implement
1094 //
1095 // In lld for the first pass, we pre-create one or more ThunkSections per
1096 // InputSectionDescription at Target specific intervals. A ThunkSection is
1097 // placed so that the estimated end of the ThunkSection is within range of the
1098 // start of the InputSectionDescription or the previous ThunkSection. For
1099 // example:
1100 // InputSectionDescription
1101 // Section 0
1102 // ...
1103 // Section N
1104 // ThunkSection 0
1105 // Section N + 1
1106 // ...
1107 // Section N + K
1108 // Thunk Section 1
1109 //
1110 // The intention is that we can add a Thunk to a ThunkSection that is well
1111 // spaced enough to service a number of callers without having to do a lot
1112 // of work. An important principle is that it is not an error if a Thunk cannot
1113 // be placed in a pre-created ThunkSection; when this happens we create a new
1114 // ThunkSection placed next to the caller. This allows us to handle the vast
1115 // majority of thunks simply, but also handle rare cases where the branch range
1116 // is smaller than the target specific spacing.
1117 //
1118 // The algorithm is expected to create all the thunks that are needed in a
1119 // single pass, with a small number of programs needing a second pass due to
1120 // the insertion of thunks in the first pass increasing the offset between
1121 // callers and callees that were only just in range.
1122 //
1123 // A consequence of allowing new ThunkSections to be created outside of the
1124 // pre-created ThunkSections is that in rare cases calls to Thunks that were in
1125 // range in pass K, are out of range in some pass > K due to the insertion of
1126 // more Thunks in between the caller and callee. When this happens we retarget
1127 // the relocation back to the original target and create another Thunk.
1128 
1129 // Remove ThunkSections that are empty, this should only be the initial set
1130 // precreated on pass 0.
1131 
1132 // Insert the Thunks for OutputSection OS into their designated place
1133 // in the Sections vector, and recalculate the InputSection output section
1134 // offsets.
1135 // This may invalidate any output section offsets stored outside of InputSection
1136 void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> OutputSections) {
1137   forEachInputSectionDescription(
1138       OutputSections, [&](OutputSection *OS, InputSectionDescription *ISD) {
1139         if (ISD->ThunkSections.empty())
1140           return;
1141 
1142         // Remove any zero sized precreated Thunks.
1143         llvm::erase_if(ISD->ThunkSections,
1144                        [](const std::pair<ThunkSection *, uint32_t> &TS) {
1145                          return TS.first->getSize() == 0;
1146                        });
1147         // ISD->ThunkSections contains all created ThunkSections, including
1148         // those inserted in previous passes. Extract the Thunks created this
1149         // pass and order them in ascending OutSecOff.
1150         std::vector<ThunkSection *> NewThunks;
1151         for (const std::pair<ThunkSection *, uint32_t> TS : ISD->ThunkSections)
1152           if (TS.second == Pass)
1153             NewThunks.push_back(TS.first);
1154         std::stable_sort(NewThunks.begin(), NewThunks.end(),
1155                          [](const ThunkSection *A, const ThunkSection *B) {
1156                            return A->OutSecOff < B->OutSecOff;
1157                          });
1158 
1159         // Merge sorted vectors of Thunks and InputSections by OutSecOff
1160         std::vector<InputSection *> Tmp;
1161         Tmp.reserve(ISD->Sections.size() + NewThunks.size());
1162         auto MergeCmp = [](const InputSection *A, const InputSection *B) {
1163           // std::merge requires a strict weak ordering.
1164           if (A->OutSecOff < B->OutSecOff)
1165             return true;
1166           if (A->OutSecOff == B->OutSecOff) {
1167             auto *TA = dyn_cast<ThunkSection>(A);
1168             auto *TB = dyn_cast<ThunkSection>(B);
1169             // Check if Thunk is immediately before any specific Target
1170             // InputSection for example Mips LA25 Thunks.
1171             if (TA && TA->getTargetInputSection() == B)
1172               return true;
1173             if (TA && !TB && !TA->getTargetInputSection())
1174               // Place Thunk Sections without specific targets before
1175               // non-Thunk Sections.
1176               return true;
1177           }
1178           return false;
1179         };
1180         std::merge(ISD->Sections.begin(), ISD->Sections.end(),
1181                    NewThunks.begin(), NewThunks.end(), std::back_inserter(Tmp),
1182                    MergeCmp);
1183         ISD->Sections = std::move(Tmp);
1184       });
1185 }
1186 
1187 // Find or create a ThunkSection within the InputSectionDescription (ISD) that
1188 // is in range of Src. An ISD maps to a range of InputSections described by a
1189 // linker script section pattern such as { .text .text.* }.
1190 ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *OS, InputSection *IS,
1191                                            InputSectionDescription *ISD,
1192                                            uint32_t Type, uint64_t Src) {
1193   for (std::pair<ThunkSection *, uint32_t> TP : ISD->ThunkSections) {
1194     ThunkSection *TS = TP.first;
1195     uint64_t TSBase = OS->Addr + TS->OutSecOff;
1196     uint64_t TSLimit = TSBase + TS->getSize();
1197     if (Target->inBranchRange(Type, Src, (Src > TSLimit) ? TSBase : TSLimit))
1198       return TS;
1199   }
1200 
1201   // No suitable ThunkSection exists. This can happen when there is a branch
1202   // with lower range than the ThunkSection spacing or when there are too
1203   // many Thunks. Create a new ThunkSection as close to the InputSection as
1204   // possible. Error if InputSection is so large we cannot place ThunkSection
1205   // anywhere in Range.
1206   uint64_t ThunkSecOff = IS->OutSecOff;
1207   if (!Target->inBranchRange(Type, Src, OS->Addr + ThunkSecOff)) {
1208     ThunkSecOff = IS->OutSecOff + IS->getSize();
1209     if (!Target->inBranchRange(Type, Src, OS->Addr + ThunkSecOff))
1210       fatal("InputSection too large for range extension thunk " +
1211             IS->getObjMsg(Src - (OS->Addr + IS->OutSecOff)));
1212   }
1213   return addThunkSection(OS, ISD, ThunkSecOff);
1214 }
1215 
1216 // Add a Thunk that needs to be placed in a ThunkSection that immediately
1217 // precedes its Target.
1218 ThunkSection *ThunkCreator::getISThunkSec(InputSection *IS) {
1219   ThunkSection *TS = ThunkedSections.lookup(IS);
1220   if (TS)
1221     return TS;
1222 
1223   // Find InputSectionRange within Target Output Section (TOS) that the
1224   // InputSection (IS) that we need to precede is in.
1225   OutputSection *TOS = IS->getParent();
1226   for (BaseCommand *BC : TOS->SectionCommands)
1227     if (auto *ISD = dyn_cast<InputSectionDescription>(BC)) {
1228       if (ISD->Sections.empty())
1229         continue;
1230       InputSection *first = ISD->Sections.front();
1231       InputSection *last = ISD->Sections.back();
1232       if (IS->OutSecOff >= first->OutSecOff &&
1233           IS->OutSecOff <= last->OutSecOff) {
1234         TS = addThunkSection(TOS, ISD, IS->OutSecOff);
1235         ThunkedSections[IS] = TS;
1236         break;
1237       }
1238     }
1239   return TS;
1240 }
1241 
1242 // Create one or more ThunkSections per OS that can be used to place Thunks.
1243 // We attempt to place the ThunkSections using the following desirable
1244 // properties:
1245 // - Within range of the maximum number of callers
1246 // - Minimise the number of ThunkSections
1247 //
1248 // We follow a simple but conservative heuristic to place ThunkSections at
1249 // offsets that are multiples of a Target specific branch range.
1250 // For an InputSectionRange that is smaller than the range, a single
1251 // ThunkSection at the end of the range will do.
1252 void ThunkCreator::createInitialThunkSections(
1253     ArrayRef<OutputSection *> OutputSections) {
1254   forEachInputSectionDescription(
1255       OutputSections, [&](OutputSection *OS, InputSectionDescription *ISD) {
1256         if (ISD->Sections.empty())
1257           return;
1258         uint32_t ISLimit;
1259         uint32_t PrevISLimit = ISD->Sections.front()->OutSecOff;
1260         uint32_t ThunkUpperBound = PrevISLimit + Target->ThunkSectionSpacing;
1261 
1262         for (const InputSection *IS : ISD->Sections) {
1263           ISLimit = IS->OutSecOff + IS->getSize();
1264           if (ISLimit > ThunkUpperBound) {
1265             addThunkSection(OS, ISD, PrevISLimit);
1266             ThunkUpperBound = PrevISLimit + Target->ThunkSectionSpacing;
1267           }
1268           PrevISLimit = ISLimit;
1269         }
1270         addThunkSection(OS, ISD, ISLimit);
1271       });
1272 }
1273 
1274 ThunkSection *ThunkCreator::addThunkSection(OutputSection *OS,
1275                                             InputSectionDescription *ISD,
1276                                             uint64_t Off) {
1277   auto *TS = make<ThunkSection>(OS, Off);
1278   ISD->ThunkSections.push_back(std::make_pair(TS, Pass));
1279   return TS;
1280 }
1281 
1282 std::pair<Thunk *, bool> ThunkCreator::getThunk(Symbol &Sym, RelType Type,
1283                                                 uint64_t Src) {
1284   auto Res = ThunkedSymbols.insert({&Sym, std::vector<Thunk *>()});
1285   if (!Res.second) {
1286     // Check existing Thunks for Sym to see if they can be reused
1287     for (Thunk *ET : Res.first->second)
1288       if (ET->isCompatibleWith(Type) &&
1289           Target->inBranchRange(Type, Src, ET->ThunkSym->getVA()))
1290         return std::make_pair(ET, false);
1291   }
1292   // No existing compatible Thunk in range, create a new one
1293   Thunk *T = addThunk(Type, Sym);
1294   Res.first->second.push_back(T);
1295   return std::make_pair(T, true);
1296 }
1297 
1298 // Call Fn on every executable InputSection accessed via the linker script
1299 // InputSectionDescription::Sections.
1300 void ThunkCreator::forEachInputSectionDescription(
1301     ArrayRef<OutputSection *> OutputSections,
1302     std::function<void(OutputSection *, InputSectionDescription *)> Fn) {
1303   for (OutputSection *OS : OutputSections) {
1304     if (!(OS->Flags & SHF_ALLOC) || !(OS->Flags & SHF_EXECINSTR))
1305       continue;
1306     for (BaseCommand *BC : OS->SectionCommands)
1307       if (auto *ISD = dyn_cast<InputSectionDescription>(BC))
1308         Fn(OS, ISD);
1309   }
1310 }
1311 
1312 // Return true if the relocation target is an in range Thunk.
1313 // Return false if the relocation is not to a Thunk. If the relocation target
1314 // was originally to a Thunk, but is no longer in range we revert the
1315 // relocation back to its original non-Thunk target.
1316 bool ThunkCreator::normalizeExistingThunk(Relocation &Rel, uint64_t Src) {
1317   if (Thunk *ET = Thunks.lookup(Rel.Sym)) {
1318     if (Target->inBranchRange(Rel.Type, Src, Rel.Sym->getVA()))
1319       return true;
1320     Rel.Sym = &ET->Destination;
1321     if (Rel.Sym->isInPlt())
1322       Rel.Expr = toPlt(Rel.Expr);
1323   }
1324   return false;
1325 }
1326 
1327 // Process all relocations from the InputSections that have been assigned
1328 // to InputSectionDescriptions and redirect through Thunks if needed. The
1329 // function should be called iteratively until it returns false.
1330 //
1331 // PreConditions:
1332 // All InputSections that may need a Thunk are reachable from
1333 // OutputSectionCommands.
1334 //
1335 // All OutputSections have an address and all InputSections have an offset
1336 // within the OutputSection.
1337 //
1338 // The offsets between caller (relocation place) and callee
1339 // (relocation target) will not be modified outside of createThunks().
1340 //
1341 // PostConditions:
1342 // If return value is true then ThunkSections have been inserted into
1343 // OutputSections. All relocations that needed a Thunk based on the information
1344 // available to createThunks() on entry have been redirected to a Thunk. Note
1345 // that adding Thunks changes offsets between caller and callee so more Thunks
1346 // may be required.
1347 //
1348 // If return value is false then no more Thunks are needed, and createThunks has
1349 // made no changes. If the target requires range extension thunks, currently
1350 // ARM, then any future change in offset between caller and callee risks a
1351 // relocation out of range error.
1352 bool ThunkCreator::createThunks(ArrayRef<OutputSection *> OutputSections) {
1353   bool AddressesChanged = false;
1354   if (Pass == 0 && Target->ThunkSectionSpacing)
1355     createInitialThunkSections(OutputSections);
1356   else if (Pass == 10)
1357     // With Thunk Size much smaller than branch range we expect to
1358     // converge quickly; if we get to 10 something has gone wrong.
1359     fatal("thunk creation not converged");
1360 
1361   // Create all the Thunks and insert them into synthetic ThunkSections. The
1362   // ThunkSections are later inserted back into InputSectionDescriptions.
1363   // We separate the creation of ThunkSections from the insertion of the
1364   // ThunkSections as ThunkSections are not always inserted into the same
1365   // InputSectionDescription as the caller.
1366   forEachInputSectionDescription(
1367       OutputSections, [&](OutputSection *OS, InputSectionDescription *ISD) {
1368         for (InputSection *IS : ISD->Sections)
1369           for (Relocation &Rel : IS->Relocations) {
1370             uint64_t Src = OS->Addr + IS->OutSecOff + Rel.Offset;
1371 
1372             // If we are a relocation to an existing Thunk, check if it is
1373             // still in range. If not then Rel will be altered to point to its
1374             // original target so another Thunk can be generated.
1375             if (Pass > 0 && normalizeExistingThunk(Rel, Src))
1376               continue;
1377 
1378             if (!Target->needsThunk(Rel.Expr, Rel.Type, IS->File, Src,
1379                                     *Rel.Sym))
1380               continue;
1381             Thunk *T;
1382             bool IsNew;
1383             std::tie(T, IsNew) = getThunk(*Rel.Sym, Rel.Type, Src);
1384             if (IsNew) {
1385               AddressesChanged = true;
1386               // Find or create a ThunkSection for the new Thunk
1387               ThunkSection *TS;
1388               if (auto *TIS = T->getTargetInputSection())
1389                 TS = getISThunkSec(TIS);
1390               else
1391                 TS = getISDThunkSec(OS, IS, ISD, Rel.Type, Src);
1392               TS->addThunk(T);
1393               Thunks[T->ThunkSym] = T;
1394             }
1395             // Redirect relocation to Thunk, we never go via the PLT to a Thunk
1396             Rel.Sym = T->ThunkSym;
1397             Rel.Expr = fromPlt(Rel.Expr);
1398           }
1399       });
1400   // Merge all created synthetic ThunkSections back into OutputSection
1401   mergeThunks(OutputSections);
1402   ++Pass;
1403   return AddressesChanged;
1404 }
1405 
1406 template void elf::scanRelocations<ELF32LE>(InputSectionBase &);
1407 template void elf::scanRelocations<ELF32BE>(InputSectionBase &);
1408 template void elf::scanRelocations<ELF64LE>(InputSectionBase &);
1409 template void elf::scanRelocations<ELF64BE>(InputSectionBase &);
1410