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 "Memory.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 54 #include "llvm/Support/Endian.h" 55 #include "llvm/Support/raw_ostream.h" 56 #include <algorithm> 57 58 using namespace llvm; 59 using namespace llvm::ELF; 60 using namespace llvm::object; 61 using namespace llvm::support::endian; 62 63 using namespace lld; 64 using namespace lld::elf; 65 66 // Construct a message in the following format. 67 // 68 // >>> defined in /home/alice/src/foo.o 69 // >>> referenced by bar.c:12 (/home/alice/src/bar.c:12) 70 // >>> /home/alice/src/bar.o:(.text+0x1) 71 template <class ELFT> 72 static std::string getLocation(InputSectionBase &S, const SymbolBody &Sym, 73 uint64_t Off) { 74 std::string Msg = 75 "\n>>> defined in " + toString(Sym.File) + "\n>>> referenced by "; 76 std::string Src = S.getSrcMsg<ELFT>(Off); 77 if (!Src.empty()) 78 Msg += Src + "\n>>> "; 79 return Msg + S.getObjMsg<ELFT>(Off); 80 } 81 82 static bool isPreemptible(const SymbolBody &Body, uint32_t Type) { 83 // In case of MIPS GP-relative relocations always resolve to a definition 84 // in a regular input file, ignoring the one-definition rule. So we, 85 // for example, should not attempt to create a dynamic relocation even 86 // if the target symbol is preemptible. There are two two MIPS GP-relative 87 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16 88 // can be against a preemptible symbol. 89 // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all 90 // relocation types occupy eight bit. In case of N64 ABI we extract first 91 // relocation from 3-in-1 packet because only the first relocation can 92 // be against a real symbol. 93 if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16) 94 return false; 95 return Body.isPreemptible(); 96 } 97 98 // This function is similar to the `handleTlsRelocation`. MIPS does not 99 // support any relaxations for TLS relocations so by factoring out MIPS 100 // handling in to the separate function we can simplify the code and do not 101 // pollute other `handleTlsRelocation` by MIPS `ifs` statements. 102 // Mips has a custom MipsGotSection that handles the writing of GOT entries 103 // without dynamic relocations. 104 template <class ELFT> 105 static unsigned handleMipsTlsRelocation(uint32_t Type, SymbolBody &Body, 106 InputSectionBase &C, uint64_t Offset, 107 int64_t Addend, RelExpr Expr) { 108 if (Expr == R_MIPS_TLSLD) { 109 if (In<ELFT>::MipsGot->addTlsIndex() && Config->Pic) 110 In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, In<ELFT>::MipsGot, 111 In<ELFT>::MipsGot->getTlsIndexOff(), false, 112 nullptr, 0}); 113 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 114 return 1; 115 } 116 117 if (Expr == R_MIPS_TLSGD) { 118 if (In<ELFT>::MipsGot->addDynTlsEntry(Body) && Body.isPreemptible()) { 119 uint64_t Off = In<ELFT>::MipsGot->getGlobalDynOffset(Body); 120 In<ELFT>::RelaDyn->addReloc( 121 {Target->TlsModuleIndexRel, In<ELFT>::MipsGot, Off, false, &Body, 0}); 122 if (Body.isPreemptible()) 123 In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, In<ELFT>::MipsGot, 124 Off + Config->Wordsize, false, &Body, 0}); 125 } 126 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 127 return 1; 128 } 129 return 0; 130 } 131 132 // This function is similar to the `handleMipsTlsRelocation`. ARM also does not 133 // support any relaxations for TLS relocations. ARM is logically similar to Mips 134 // in how it handles TLS, but Mips uses its own custom GOT which handles some 135 // of the cases that ARM uses GOT relocations for. 136 // 137 // We look for TLS global dynamic and local dynamic relocations, these may 138 // require the generation of a pair of GOT entries that have associated 139 // dynamic relocations. When the results of the dynamic relocations can be 140 // resolved at static link time we do so. This is necessary for static linking 141 // as there will be no dynamic loader to resolve them at load-time. 142 // 143 // The pair of GOT entries created are of the form 144 // GOT[e0] Module Index (Used to find pointer to TLS block at run-time) 145 // GOT[e1] Offset of symbol in TLS block 146 template <class ELFT> 147 static unsigned handleARMTlsRelocation(uint32_t Type, SymbolBody &Body, 148 InputSectionBase &C, uint64_t Offset, 149 int64_t Addend, RelExpr Expr) { 150 // The Dynamic TLS Module Index Relocation for a symbol defined in an 151 // executable is always 1. If the target Symbol is not preemtible then 152 // we know the offset into the TLS block at static link time. 153 bool NeedDynId = Body.isPreemptible() || Config->Shared; 154 bool NeedDynOff = Body.isPreemptible(); 155 156 auto AddTlsReloc = [&](uint64_t Off, uint32_t Type, SymbolBody *Dest, 157 bool Dyn) { 158 if (Dyn) 159 In<ELFT>::RelaDyn->addReloc({Type, In<ELFT>::Got, Off, false, Dest, 0}); 160 else 161 In<ELFT>::Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest}); 162 }; 163 164 // Local Dynamic is for access to module local TLS variables, while still 165 // being suitable for being dynamically loaded via dlopen. 166 // GOT[e0] is the module index, with a special value of 0 for the current 167 // module. GOT[e1] is unused. There only needs to be one module index entry. 168 if (Expr == R_TLSLD_PC && In<ELFT>::Got->addTlsIndex()) { 169 AddTlsReloc(In<ELFT>::Got->getTlsIndexOff(), Target->TlsModuleIndexRel, 170 NeedDynId ? nullptr : &Body, NeedDynId); 171 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 172 return 1; 173 } 174 175 // Global Dynamic is the most general purpose access model. When we know 176 // the module index and offset of symbol in TLS block we can fill these in 177 // using static GOT relocations. 178 if (Expr == R_TLSGD_PC) { 179 if (In<ELFT>::Got->addDynTlsEntry(Body)) { 180 uint64_t Off = In<ELFT>::Got->getGlobalDynOffset(Body); 181 AddTlsReloc(Off, Target->TlsModuleIndexRel, &Body, NeedDynId); 182 AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Body, 183 NeedDynOff); 184 } 185 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 186 return 1; 187 } 188 return 0; 189 } 190 191 // Returns the number of relocations processed. 192 template <class ELFT> 193 static unsigned 194 handleTlsRelocation(uint32_t Type, SymbolBody &Body, InputSectionBase &C, 195 typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) { 196 if (!(C.Flags & SHF_ALLOC)) 197 return 0; 198 199 if (!Body.isTls()) 200 return 0; 201 202 if (Config->EMachine == EM_ARM) 203 return handleARMTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr); 204 if (Config->EMachine == EM_MIPS) 205 return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr); 206 207 bool IsPreemptible = isPreemptible(Body, Type); 208 if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) && 209 Config->Shared) { 210 if (In<ELFT>::Got->addDynTlsEntry(Body)) { 211 uint64_t Off = In<ELFT>::Got->getGlobalDynOffset(Body); 212 In<ELFT>::RelaDyn->addReloc({Target->TlsDescRel, In<ELFT>::Got, Off, 213 !IsPreemptible, &Body, 0}); 214 } 215 if (Expr != R_TLSDESC_CALL) 216 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 217 return 1; 218 } 219 220 if (isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)) { 221 // Local-Dynamic relocs can be relaxed to Local-Exec. 222 if (!Config->Shared) { 223 C.Relocations.push_back( 224 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); 225 return 2; 226 } 227 if (In<ELFT>::Got->addTlsIndex()) 228 In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, In<ELFT>::Got, 229 In<ELFT>::Got->getTlsIndexOff(), false, 230 nullptr, 0}); 231 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 232 return 1; 233 } 234 235 // Local-Dynamic relocs can be relaxed to Local-Exec. 236 if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) { 237 C.Relocations.push_back( 238 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); 239 return 1; 240 } 241 242 if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD, 243 R_TLSGD_PC>(Expr)) { 244 if (Config->Shared) { 245 if (In<ELFT>::Got->addDynTlsEntry(Body)) { 246 uint64_t Off = In<ELFT>::Got->getGlobalDynOffset(Body); 247 In<ELFT>::RelaDyn->addReloc( 248 {Target->TlsModuleIndexRel, In<ELFT>::Got, Off, false, &Body, 0}); 249 250 // If the symbol is preemptible we need the dynamic linker to write 251 // the offset too. 252 uint64_t OffsetOff = Off + Config->Wordsize; 253 if (IsPreemptible) 254 In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, In<ELFT>::Got, 255 OffsetOff, false, &Body, 0}); 256 else 257 In<ELFT>::Got->Relocations.push_back( 258 {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body}); 259 } 260 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 261 return 1; 262 } 263 264 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec 265 // depending on the symbol being locally defined or not. 266 if (IsPreemptible) { 267 C.Relocations.push_back( 268 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type, 269 Offset, Addend, &Body}); 270 if (!Body.isInGot()) { 271 In<ELFT>::Got->addEntry(Body); 272 In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, In<ELFT>::Got, 273 Body.getGotOffset(), false, &Body, 0}); 274 } 275 } else { 276 C.Relocations.push_back( 277 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type, 278 Offset, Addend, &Body}); 279 } 280 return Target->TlsGdRelaxSkip; 281 } 282 283 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally 284 // defined. 285 if (Target->isTlsInitialExecRel(Type) && !Config->Shared && !IsPreemptible) { 286 C.Relocations.push_back( 287 {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body}); 288 return 1; 289 } 290 291 if (Expr == R_TLSDESC_CALL) 292 return 1; 293 return 0; 294 } 295 296 static uint32_t getMipsPairType(uint32_t Type, const SymbolBody &Sym) { 297 switch (Type) { 298 case R_MIPS_HI16: 299 return R_MIPS_LO16; 300 case R_MIPS_GOT16: 301 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE; 302 case R_MIPS_PCHI16: 303 return R_MIPS_PCLO16; 304 case R_MICROMIPS_HI16: 305 return R_MICROMIPS_LO16; 306 default: 307 return R_MIPS_NONE; 308 } 309 } 310 311 // True if non-preemptable symbol always has the same value regardless of where 312 // the DSO is loaded. 313 static bool isAbsolute(const SymbolBody &Body) { 314 if (Body.isUndefined()) 315 return !Body.isLocal() && Body.symbol()->isWeak(); 316 if (const auto *DR = dyn_cast<DefinedRegular>(&Body)) 317 return DR->Section == nullptr; // Absolute symbol. 318 return false; 319 } 320 321 static bool isAbsoluteValue(const SymbolBody &Body) { 322 return isAbsolute(Body) || Body.isTls(); 323 } 324 325 // Returns true if Expr refers a PLT entry. 326 static bool needsPlt(RelExpr Expr) { 327 return isRelExprOneOf<R_PLT_PC, R_PPC_PLT_OPD, R_PLT, R_PLT_PAGE_PC>(Expr); 328 } 329 330 // Returns true if Expr refers a GOT entry. Note that this function 331 // returns false for TLS variables even though they need GOT, because 332 // TLS variables uses GOT differently than the regular variables. 333 static bool needsGot(RelExpr Expr) { 334 return isRelExprOneOf<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF, 335 R_MIPS_GOT_OFF32, R_GOT_PAGE_PC, R_GOT_PC, 336 R_GOT_FROM_END>(Expr); 337 } 338 339 // True if this expression is of the form Sym - X, where X is a position in the 340 // file (PC, or GOT for example). 341 static bool isRelExpr(RelExpr Expr) { 342 return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL, 343 R_PAGE_PC, R_RELAX_GOT_PC>(Expr); 344 } 345 346 // Returns true if a given relocation can be computed at link-time. 347 // 348 // For instance, we know the offset from a relocation to its target at 349 // link-time if the relocation is PC-relative and refers a 350 // non-interposable function in the same executable. This function 351 // will return true for such relocation. 352 // 353 // If this function returns false, that means we need to emit a 354 // dynamic relocation so that the relocation will be fixed at load-time. 355 template <class ELFT> 356 static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type, 357 const SymbolBody &Body, 358 InputSectionBase &S, uint64_t RelOff) { 359 // These expressions always compute a constant 360 if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, 361 R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, 362 R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_PLT_PC, 363 R_TLSGD_PC, R_TLSGD, R_PPC_PLT_OPD, R_TLSDESC_CALL, 364 R_TLSDESC_PAGE, R_HINT>(E)) 365 return true; 366 367 // These never do, except if the entire file is position dependent or if 368 // only the low bits are used. 369 if (E == R_GOT || E == R_PLT || E == R_TLSDESC) 370 return Target->usesOnlyLowPageBits(Type) || !Config->Pic; 371 372 if (isPreemptible(Body, Type)) 373 return false; 374 if (!Config->Pic) 375 return true; 376 377 // For the target and the relocation, we want to know if they are 378 // absolute or relative. 379 bool AbsVal = isAbsoluteValue(Body); 380 bool RelE = isRelExpr(E); 381 if (AbsVal && !RelE) 382 return true; 383 if (!AbsVal && RelE) 384 return true; 385 if (!AbsVal && !RelE) 386 return Target->usesOnlyLowPageBits(Type); 387 388 // Relative relocation to an absolute value. This is normally unrepresentable, 389 // but if the relocation refers to a weak undefined symbol, we allow it to 390 // resolve to the image base. This is a little strange, but it allows us to 391 // link function calls to such symbols. Normally such a call will be guarded 392 // with a comparison, which will load a zero from the GOT. 393 // Another special case is MIPS _gp_disp symbol which represents offset 394 // between start of a function and '_gp' value and defined as absolute just 395 // to simplify the code. 396 assert(AbsVal && RelE); 397 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) 398 return true; 399 400 error("relocation " + toString(Type) + " cannot refer to absolute symbol: " + 401 toString(Body) + getLocation<ELFT>(S, Body, RelOff)); 402 return true; 403 } 404 405 static RelExpr toPlt(RelExpr Expr) { 406 if (Expr == R_PPC_OPD) 407 return R_PPC_PLT_OPD; 408 if (Expr == R_PC) 409 return R_PLT_PC; 410 if (Expr == R_PAGE_PC) 411 return R_PLT_PAGE_PC; 412 if (Expr == R_ABS) 413 return R_PLT; 414 return Expr; 415 } 416 417 static RelExpr fromPlt(RelExpr Expr) { 418 // We decided not to use a plt. Optimize a reference to the plt to a 419 // reference to the symbol itself. 420 if (Expr == R_PLT_PC) 421 return R_PC; 422 if (Expr == R_PPC_PLT_OPD) 423 return R_PPC_OPD; 424 if (Expr == R_PLT) 425 return R_ABS; 426 return Expr; 427 } 428 429 // Returns true if a given shared symbol is in a read-only segment in a DSO. 430 template <class ELFT> static bool isReadOnly(SharedSymbol *SS) { 431 typedef typename ELFT::Phdr Elf_Phdr; 432 uint64_t Value = SS->getValue<ELFT>(); 433 434 // Determine if the symbol is read-only by scanning the DSO's program headers. 435 auto *File = cast<SharedFile<ELFT>>(SS->File); 436 for (const Elf_Phdr &Phdr : check(File->getObj().program_headers())) 437 if ((Phdr.p_type == ELF::PT_LOAD || Phdr.p_type == ELF::PT_GNU_RELRO) && 438 !(Phdr.p_flags & ELF::PF_W) && Value >= Phdr.p_vaddr && 439 Value < Phdr.p_vaddr + Phdr.p_memsz) 440 return true; 441 return false; 442 } 443 444 // Returns symbols at the same offset as a given symbol, including SS itself. 445 // 446 // If two or more symbols are at the same offset, and at least one of 447 // them are copied by a copy relocation, all of them need to be copied. 448 // Otherwise, they would refer different places at runtime. 449 template <class ELFT> 450 static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) { 451 typedef typename ELFT::Sym Elf_Sym; 452 453 auto *File = cast<SharedFile<ELFT>>(SS->File); 454 uint64_t Shndx = SS->getShndx<ELFT>(); 455 uint64_t Value = SS->getValue<ELFT>(); 456 457 std::vector<SharedSymbol *> Ret; 458 for (const Elf_Sym &S : File->getGlobalSymbols()) { 459 if (S.st_shndx != Shndx || S.st_value != Value) 460 continue; 461 StringRef Name = check(S.getName(File->getStringTable())); 462 SymbolBody *Sym = Symtab<ELFT>::X->find(Name); 463 if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym)) 464 Ret.push_back(Alias); 465 } 466 return Ret; 467 } 468 469 // Reserve space in .bss or .bss.rel.ro for copy relocation. 470 // 471 // The copy relocation is pretty much a hack. If you use a copy relocation 472 // in your program, not only the symbol name but the symbol's size, RW/RO 473 // bit and alignment become part of the ABI. In addition to that, if the 474 // symbol has aliases, the aliases become part of the ABI. That's subtle, 475 // but if you violate that implicit ABI, that can cause very counter- 476 // intuitive consequences. 477 // 478 // So, what is the copy relocation? It's for linking non-position 479 // independent code to DSOs. In an ideal world, all references to data 480 // exported by DSOs should go indirectly through GOT. But if object files 481 // are compiled as non-PIC, all data references are direct. There is no 482 // way for the linker to transform the code to use GOT, as machine 483 // instructions are already set in stone in object files. This is where 484 // the copy relocation takes a role. 485 // 486 // A copy relocation instructs the dynamic linker to copy data from a DSO 487 // to a specified address (which is usually in .bss) at load-time. If the 488 // static linker (that's us) finds a direct data reference to a DSO 489 // symbol, it creates a copy relocation, so that the symbol can be 490 // resolved as if it were in .bss rather than in a DSO. 491 // 492 // As you can see in this function, we create a copy relocation for the 493 // dynamic linker, and the relocation contains not only symbol name but 494 // various other informtion about the symbol. So, such attributes become a 495 // part of the ABI. 496 // 497 // Note for application developers: I can give you a piece of advice if 498 // you are writing a shared library. You probably should export only 499 // functions from your library. You shouldn't export variables. 500 // 501 // As an example what can happen when you export variables without knowing 502 // the semantics of copy relocations, assume that you have an exported 503 // variable of type T. It is an ABI-breaking change to add new members at 504 // end of T even though doing that doesn't change the layout of the 505 // existing members. That's because the space for the new members are not 506 // reserved in .bss unless you recompile the main program. That means they 507 // are likely to overlap with other data that happens to be laid out next 508 // to the variable in .bss. This kind of issue is sometimes very hard to 509 // debug. What's a solution? Instead of exporting a varaible V from a DSO, 510 // define an accessor getV(). 511 template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) { 512 // Copy relocation against zero-sized symbol doesn't make sense. 513 uint64_t SymSize = SS->template getSize<ELFT>(); 514 if (SymSize == 0) 515 fatal("cannot create a copy relocation for symbol " + toString(*SS)); 516 517 // See if this symbol is in a read-only segment. If so, preserve the symbol's 518 // memory protection by reserving space in the .bss.rel.ro section. 519 bool IsReadOnly = isReadOnly<ELFT>(SS); 520 BssSection *Sec = IsReadOnly ? In<ELFT>::BssRelRo : In<ELFT>::Bss; 521 uint64_t Off = Sec->reserveSpace(SymSize, SS->getAlignment<ELFT>()); 522 523 // Look through the DSO's dynamic symbol table for aliases and create a 524 // dynamic symbol for each one. This causes the copy relocation to correctly 525 // interpose any aliases. 526 for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) { 527 Sym->NeedsCopy = true; 528 Sym->CopyRelSec = Sec; 529 Sym->CopyRelSecOff = Off; 530 Sym->symbol()->IsUsedInRegularObj = true; 531 } 532 533 In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, Off, false, SS, 0}); 534 } 535 536 template <class ELFT> 537 static RelExpr adjustExpr(SymbolBody &Body, RelExpr Expr, uint32_t Type, 538 const uint8_t *Data, InputSectionBase &S, 539 typename ELFT::uint RelOff) { 540 if (Body.isGnuIFunc()) { 541 Expr = toPlt(Expr); 542 } else if (!isPreemptible(Body, Type)) { 543 if (needsPlt(Expr)) 544 Expr = fromPlt(Expr); 545 if (Expr == R_GOT_PC && !isAbsoluteValue(Body)) 546 Expr = Target->adjustRelaxExpr(Type, Data, Expr); 547 } 548 549 bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE); 550 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)) 551 return Expr; 552 553 // This relocation would require the dynamic linker to write a value to read 554 // only memory. We can hack around it if we are producing an executable and 555 // the refered symbol can be preemepted to refer to the executable. 556 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) { 557 error("can't create dynamic relocation " + toString(Type) + " against " + 558 (Body.getName().empty() ? "local symbol in readonly segment" 559 : "symbol: " + toString(Body)) + 560 getLocation<ELFT>(S, Body, RelOff)); 561 return Expr; 562 } 563 564 if (Body.getVisibility() != STV_DEFAULT) { 565 error("cannot preempt symbol: " + toString(Body) + 566 getLocation<ELFT>(S, Body, RelOff)); 567 return Expr; 568 } 569 570 if (Body.isObject()) { 571 // Produce a copy relocation. 572 auto *B = cast<SharedSymbol>(&Body); 573 if (!B->NeedsCopy) { 574 if (Config->ZNocopyreloc) 575 error("unresolvable relocation " + toString(Type) + 576 " against symbol '" + toString(*B) + 577 "'; recompile with -fPIC or remove '-z nocopyreloc'" + 578 getLocation<ELFT>(S, Body, RelOff)); 579 580 addCopyRelSymbol<ELFT>(B); 581 } 582 return Expr; 583 } 584 585 if (Body.isFunc()) { 586 // This handles a non PIC program call to function in a shared library. In 587 // an ideal world, we could just report an error saying the relocation can 588 // overflow at runtime. In the real world with glibc, crt1.o has a 589 // R_X86_64_PC32 pointing to libc.so. 590 // 591 // The general idea on how to handle such cases is to create a PLT entry and 592 // use that as the function value. 593 // 594 // For the static linking part, we just return a plt expr and everything 595 // else will use the the PLT entry as the address. 596 // 597 // The remaining problem is making sure pointer equality still works. We 598 // need the help of the dynamic linker for that. We let it know that we have 599 // a direct reference to a so symbol by creating an undefined symbol with a 600 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to 601 // the value of the symbol we created. This is true even for got entries, so 602 // pointer equality is maintained. To avoid an infinite loop, the only entry 603 // that points to the real function is a dedicated got entry used by the 604 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, 605 // R_386_JMP_SLOT, etc). 606 Body.NeedsPltAddr = true; 607 return toPlt(Expr); 608 } 609 610 error("symbol '" + toString(Body) + "' defined in " + toString(Body.File) + 611 " has no type"); 612 return Expr; 613 } 614 615 // Returns an addend of a given relocation. If it is RELA, an addend 616 // is in a relocation itself. If it is REL, we need to read it from an 617 // input section. 618 template <class ELFT, class RelTy> 619 static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) { 620 uint32_t Type = Rel.getType(Config->IsMips64EL); 621 int64_t A = RelTy::IsRela 622 ? getAddend<ELFT>(Rel) 623 : Target->getImplicitAddend(Buf + Rel.r_offset, Type); 624 625 if (Config->EMachine == EM_PPC64 && Config->Pic && Type == R_PPC64_TOC) 626 A += getPPC64TocBase(); 627 return A; 628 } 629 630 // MIPS has an odd notion of "paired" relocations to calculate addends. 631 // For example, if a relocation is of R_MIPS_HI16, there must be a 632 // R_MIPS_LO16 relocation after that, and an addend is calculated using 633 // the two relocations. 634 template <class ELFT, class RelTy> 635 static int64_t computeMipsAddend(const RelTy &Rel, InputSectionBase &Sec, 636 RelExpr Expr, SymbolBody &Body, 637 const RelTy *End) { 638 if (Expr == R_MIPS_GOTREL && Body.isLocal()) 639 return Sec.getFile<ELFT>()->MipsGp0; 640 641 // The ABI says that the paired relocation is used only for REL. 642 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 643 if (RelTy::IsRela) 644 return 0; 645 646 uint32_t Type = Rel.getType(Config->IsMips64EL); 647 uint32_t PairTy = getMipsPairType(Type, Body); 648 if (PairTy == R_MIPS_NONE) 649 return 0; 650 651 const uint8_t *Buf = Sec.Data.data(); 652 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL); 653 654 // To make things worse, paired relocations might not be contiguous in 655 // the relocation table, so we need to do linear search. *sigh* 656 for (const RelTy *RI = &Rel; RI != End; ++RI) { 657 if (RI->getType(Config->IsMips64EL) != PairTy) 658 continue; 659 if (RI->getSymbol(Config->IsMips64EL) != SymIndex) 660 continue; 661 662 endianness E = Config->Endianness; 663 int32_t Hi = (read32(Buf + Rel.r_offset, E) & 0xffff) << 16; 664 int32_t Lo = SignExtend32<16>(read32(Buf + RI->r_offset, E)); 665 return Hi + Lo; 666 } 667 668 warn("can't find matching " + toString(PairTy) + " relocation for " + 669 toString(Type)); 670 return 0; 671 } 672 673 template <class ELFT> 674 static void reportUndefined(SymbolBody &Sym, InputSectionBase &S, 675 uint64_t Offset) { 676 if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll) 677 return; 678 679 bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL && 680 Sym.getVisibility() == STV_DEFAULT; 681 if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && CanBeExternal) 682 return; 683 684 std::string Msg = 685 "undefined symbol: " + toString(Sym) + "\n>>> referenced by "; 686 687 std::string Src = S.getSrcMsg<ELFT>(Offset); 688 if (!Src.empty()) 689 Msg += Src + "\n>>> "; 690 Msg += S.getObjMsg<ELFT>(Offset); 691 692 if (Config->UnresolvedSymbols == UnresolvedPolicy::WarnAll || 693 (Config->UnresolvedSymbols == UnresolvedPolicy::Warn && CanBeExternal)) { 694 warn(Msg); 695 } else { 696 error(Msg); 697 698 if (Config->ArchiveWithoutSymbolsSeen) { 699 message("At least one archive listed no symbols in its index." 700 " This can happen when creating archives with a version" 701 " of ar that does not understand the object files in" 702 " the archive. For example, if you are using LLVM" 703 " bitcode objects (such as created by -flto), you may" 704 " need to use llvm-ar or GNU ar with a plugin."); 705 // Reset to false so that we print the message only once. 706 Config->ArchiveWithoutSymbolsSeen = false; 707 } 708 } 709 } 710 711 template <class RelTy> 712 static std::pair<uint32_t, uint32_t> 713 mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) { 714 // MIPS N32 ABI treats series of successive relocations with the same offset 715 // as a single relocation. The similar approach used by N64 ABI, but this ABI 716 // packs all relocations into the single relocation record. Here we emulate 717 // this for the N32 ABI. Iterate over relocation with the same offset and put 718 // theirs types into the single bit-set. 719 uint32_t Processed = 0; 720 for (; I != E && Offset == I->r_offset; ++I) { 721 ++Processed; 722 Type |= I->getType(Config->IsMips64EL) << (8 * Processed); 723 } 724 return std::make_pair(Type, Processed); 725 } 726 727 // .eh_frame sections are mergeable input sections, so their input 728 // offsets are not linearly mapped to output section. For each input 729 // offset, we need to find a section piece containing the offset and 730 // add the piece's base address to the input offset to compute the 731 // output offset. That isn't cheap. 732 // 733 // This class is to speed up the offset computation. When we process 734 // relocations, we access offsets in the monotonically increasing 735 // order. So we can optimize for that access pattern. 736 // 737 // For sections other than .eh_frame, this class doesn't do anything. 738 namespace { 739 class OffsetGetter { 740 public: 741 explicit OffsetGetter(InputSectionBase &Sec) { 742 if (auto *Eh = dyn_cast<EhInputSection>(&Sec)) { 743 P = Eh->Pieces; 744 Size = Eh->Pieces.size(); 745 } 746 } 747 748 // Translates offsets in input sections to offsets in output sections. 749 // Given offset must increase monotonically. We assume that P is 750 // sorted by InputOff. 751 uint64_t get(uint64_t Off) { 752 if (P.empty()) 753 return Off; 754 755 while (I != Size && P[I].InputOff + P[I].size() <= Off) 756 ++I; 757 if (I == Size) 758 return Off; 759 760 // P must be contiguous, so there must be no holes in between. 761 assert(P[I].InputOff <= Off && "Relocation not in any piece"); 762 763 // Offset -1 means that the piece is dead (i.e. garbage collected). 764 if (P[I].OutputOff == -1) 765 return -1; 766 return P[I].OutputOff + Off - P[I].InputOff; 767 } 768 769 private: 770 ArrayRef<EhSectionPiece> P; 771 size_t I = 0; 772 size_t Size; 773 }; 774 } // namespace 775 776 template <class ELFT, class GotPltSection> 777 static void addPltEntry(PltSection *Plt, GotPltSection *GotPlt, 778 RelocationSection<ELFT> *Rel, uint32_t Type, 779 SymbolBody &Sym, bool UseSymVA) { 780 Plt->addEntry<ELFT>(Sym); 781 GotPlt->addEntry(Sym); 782 Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0}); 783 } 784 785 template <class ELFT> 786 static void addGotEntry(SymbolBody &Sym, bool Preemptible) { 787 In<ELFT>::Got->addEntry(Sym); 788 789 uint64_t Off = Sym.getGotOffset(); 790 uint32_t DynType; 791 RelExpr Expr = R_ABS; 792 793 if (Sym.isTls()) { 794 DynType = Target->TlsGotRel; 795 Expr = R_TLS; 796 } else if (!Preemptible && Config->Pic && !isAbsolute(Sym)) { 797 DynType = Target->RelativeRel; 798 } else { 799 DynType = Target->GotRel; 800 } 801 802 bool Constant = !Preemptible && !(Config->Pic && !isAbsolute(Sym)); 803 if (!Constant) 804 In<ELFT>::RelaDyn->addReloc( 805 {DynType, In<ELFT>::Got, Off, !Preemptible, &Sym, 0}); 806 807 if (Constant || (!Config->IsRela && !Preemptible)) 808 In<ELFT>::Got->Relocations.push_back({Expr, DynType, Off, 0, &Sym}); 809 } 810 811 // The reason we have to do this early scan is as follows 812 // * To mmap the output file, we need to know the size 813 // * For that, we need to know how many dynamic relocs we will have. 814 // It might be possible to avoid this by outputting the file with write: 815 // * Write the allocated output sections, computing addresses. 816 // * Apply relocations, recording which ones require a dynamic reloc. 817 // * Write the dynamic relocations. 818 // * Write the rest of the file. 819 // This would have some drawbacks. For example, we would only know if .rela.dyn 820 // is needed after applying relocations. If it is, it will go after rw and rx 821 // sections. Given that it is ro, we will need an extra PT_LOAD. This 822 // complicates things for the dynamic linker and means we would have to reserve 823 // space for the extra PT_LOAD even if we end up not using it. 824 template <class ELFT, class RelTy> 825 static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) { 826 OffsetGetter GetOffset(Sec); 827 828 for (auto I = Rels.begin(), End = Rels.end(); I != End; ++I) { 829 const RelTy &Rel = *I; 830 SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel); 831 uint32_t Type = Rel.getType(Config->IsMips64EL); 832 833 if (Config->MipsN32Abi) { 834 uint32_t Processed; 835 std::tie(Type, Processed) = 836 mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End); 837 I += Processed; 838 } 839 840 // Compute the offset of this section in the output section. 841 uint64_t Offset = GetOffset.get(Rel.r_offset); 842 if (Offset == uint64_t(-1)) 843 continue; 844 845 // Report undefined symbols. The fact that we report undefined 846 // symbols here means that we report undefined symbols only when 847 // they have relocations pointing to them. We don't care about 848 // undefined symbols that are in dead-stripped sections. 849 if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak()) 850 reportUndefined<ELFT>(Body, Sec, Rel.r_offset); 851 852 RelExpr Expr = 853 Target->getRelExpr(Type, Body, Sec.Data.begin() + Rel.r_offset); 854 855 // Ignore "hint" relocations because they are only markers for relaxation. 856 if (isRelExprOneOf<R_HINT, R_NONE>(Expr)) 857 continue; 858 859 bool Preemptible = isPreemptible(Body, Type); 860 Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset, 861 Sec, Rel.r_offset); 862 if (ErrorCount) 863 continue; 864 865 // This relocation does not require got entry, but it is relative to got and 866 // needs it to be created. Here we request for that. 867 if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL, 868 R_GOTREL_FROM_END, R_PPC_TOC>(Expr)) 869 In<ELFT>::Got->HasGotOffRel = true; 870 871 // Read an addend. 872 int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data()); 873 if (Config->EMachine == EM_MIPS) 874 Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End); 875 876 // Process some TLS relocations, including relaxing TLS relocations. 877 // Note that this function does not handle all TLS relocations. 878 if (unsigned Processed = 879 handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) { 880 I += (Processed - 1); 881 continue; 882 } 883 884 // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol. 885 if (needsPlt(Expr) && !Body.isInPlt()) { 886 if (Body.isGnuIFunc() && !Preemptible) 887 addPltEntry(InX::Iplt, In<ELFT>::IgotPlt, In<ELFT>::RelaIplt, 888 Target->IRelativeRel, Body, true); 889 else 890 addPltEntry(InX::Plt, In<ELFT>::GotPlt, In<ELFT>::RelaPlt, 891 Target->PltRel, Body, !Preemptible); 892 } 893 894 // Create a GOT slot if a relocation needs GOT. 895 if (needsGot(Expr)) { 896 if (Config->EMachine == EM_MIPS) { 897 // MIPS ABI has special rules to process GOT entries and doesn't 898 // require relocation entries for them. A special case is TLS 899 // relocations. In that case dynamic loader applies dynamic 900 // relocations to initialize TLS GOT entries. 901 // See "Global Offset Table" in Chapter 5 in the following document 902 // for detailed description: 903 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 904 In<ELFT>::MipsGot->addEntry(Body, Addend, Expr); 905 if (Body.isTls() && Body.isPreemptible()) 906 In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, In<ELFT>::MipsGot, 907 Body.getGotOffset(), false, &Body, 0}); 908 } else if (!Body.isInGot()) { 909 addGotEntry<ELFT>(Body, Preemptible); 910 } 911 } 912 913 if (!needsPlt(Expr) && !needsGot(Expr) && isPreemptible(Body, Type)) { 914 // We don't know anything about the finaly symbol. Just ask the dynamic 915 // linker to handle the relocation for us. 916 if (!Target->isPicRel(Type)) 917 error("relocation " + toString(Type) + 918 " cannot be used against shared object; recompile with -fPIC" + 919 getLocation<ELFT>(Sec, Body, Offset)); 920 921 In<ELFT>::RelaDyn->addReloc( 922 {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend}); 923 924 // MIPS ABI turns using of GOT and dynamic relocations inside out. 925 // While regular ABI uses dynamic relocations to fill up GOT entries 926 // MIPS ABI requires dynamic linker to fills up GOT entries using 927 // specially sorted dynamic symbol table. This affects even dynamic 928 // relocations against symbols which do not require GOT entries 929 // creation explicitly, i.e. do not have any GOT-relocations. So if 930 // a preemptible symbol has a dynamic relocation we anyway have 931 // to create a GOT entry for it. 932 // If a non-preemptible symbol has a dynamic relocation against it, 933 // dynamic linker takes it st_value, adds offset and writes down 934 // result of the dynamic relocation. In case of preemptible symbol 935 // dynamic linker performs symbol resolution, writes the symbol value 936 // to the GOT entry and reads the GOT entry when it needs to perform 937 // a dynamic relocation. 938 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 939 if (Config->EMachine == EM_MIPS) 940 In<ELFT>::MipsGot->addEntry(Body, Addend, Expr); 941 continue; 942 } 943 944 // If the relocation points to something in the file, we can process it. 945 bool IsConstant = 946 isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset); 947 948 // If the output being produced is position independent, the final value 949 // is still not known. In that case we still need some help from the 950 // dynamic linker. We can however do better than just copying the incoming 951 // relocation. We can process some of it and and just ask the dynamic 952 // linker to add the load address. 953 if (!IsConstant) 954 In<ELFT>::RelaDyn->addReloc( 955 {Target->RelativeRel, &Sec, Offset, true, &Body, Addend}); 956 957 // If the produced value is a constant, we just remember to write it 958 // when outputting this section. We also have to do it if the format 959 // uses Elf_Rel, since in that case the written value is the addend. 960 if (IsConstant || !RelTy::IsRela) 961 Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 962 } 963 } 964 965 template <class ELFT> void elf::scanRelocations(InputSectionBase &S) { 966 if (S.AreRelocsRela) 967 scanRelocs<ELFT>(S, S.relas<ELFT>()); 968 else 969 scanRelocs<ELFT>(S, S.rels<ELFT>()); 970 } 971 972 // Insert the Thunks for OutputSection OS into their designated place 973 // in the Sections vector, and recalculate the InputSection output section 974 // offsets. 975 // This may invalidate any output section offsets stored outside of InputSection 976 template <class ELFT> 977 void ThunkCreator<ELFT>::mergeThunks(OutputSection *OS, 978 std::vector<ThunkSection *> &Thunks) { 979 // Order Thunks in ascending OutSecOff 980 auto ThunkCmp = [](const ThunkSection *A, const ThunkSection *B) { 981 return A->OutSecOff < B->OutSecOff; 982 }; 983 std::stable_sort(Thunks.begin(), Thunks.end(), ThunkCmp); 984 985 // Merge sorted vectors of Thunks and InputSections by OutSecOff 986 std::vector<InputSection *> Tmp; 987 Tmp.reserve(OS->Sections.size() + Thunks.size()); 988 auto MergeCmp = [](const InputSection *A, const InputSection *B) { 989 // std::merge requires a strict weak ordering. 990 if (A->OutSecOff < B->OutSecOff) 991 return true; 992 if (A->OutSecOff == B->OutSecOff) 993 // Check if Thunk is immediately before any specific Target InputSection 994 // for example Mips LA25 Thunks. 995 if (auto *TA = dyn_cast<ThunkSection>(A)) 996 if (TA && TA->getTargetInputSection() == B) 997 return true; 998 return false; 999 }; 1000 std::merge(OS->Sections.begin(), OS->Sections.end(), Thunks.begin(), 1001 Thunks.end(), std::back_inserter(Tmp), MergeCmp); 1002 OS->Sections = std::move(Tmp); 1003 OS->assignOffsets(); 1004 } 1005 1006 template <class ELFT> 1007 ThunkSection *ThunkCreator<ELFT>::getOSThunkSec(ThunkSection *&TS, 1008 OutputSection *OS) { 1009 if (TS == nullptr) { 1010 uint32_t Off = 0; 1011 for (auto *IS : OS->Sections) { 1012 Off = IS->OutSecOff + IS->getSize(); 1013 if ((IS->Flags & SHF_EXECINSTR) == 0) 1014 break; 1015 } 1016 TS = make<ThunkSection>(OS, Off); 1017 ThunkSections[OS].push_back(TS); 1018 } 1019 return TS; 1020 } 1021 1022 template <class ELFT> 1023 ThunkSection *ThunkCreator<ELFT>::getISThunkSec(InputSection *IS, 1024 OutputSection *OS) { 1025 ThunkSection *TS = ThunkedSections.lookup(IS); 1026 if (TS) 1027 return TS; 1028 auto *TOS = cast<OutputSection>(IS->OutSec); 1029 TS = make<ThunkSection>(TOS, IS->OutSecOff); 1030 ThunkSections[TOS].push_back(TS); 1031 ThunkedSections[IS] = TS; 1032 return TS; 1033 } 1034 1035 template <class ELFT> 1036 std::pair<Thunk *, bool> ThunkCreator<ELFT>::getThunk(SymbolBody &Body, 1037 uint32_t Type) { 1038 auto res = ThunkedSymbols.insert({&Body, nullptr}); 1039 if (res.second) 1040 res.first->second = addThunk<ELFT>(Type, Body); 1041 return std::make_pair(res.first->second, res.second); 1042 } 1043 1044 // Process all relocations from the InputSections that have been assigned 1045 // to OutputSections and redirect through Thunks if needed. 1046 // 1047 // createThunks must be called after scanRelocs has created the Relocations for 1048 // each InputSection. It must be called before the static symbol table is 1049 // finalized. If any Thunks are added to an OutputSection the output section 1050 // offsets of the InputSections will change. 1051 // 1052 // FIXME: All Thunks are assumed to be in range of the relocation. Range 1053 // extension Thunks are not yet supported. 1054 template <class ELFT> 1055 bool ThunkCreator<ELFT>::createThunks( 1056 ArrayRef<OutputSection *> OutputSections) { 1057 // Create all the Thunks and insert them into synthetic ThunkSections. The 1058 // ThunkSections are later inserted back into the OutputSection. 1059 1060 // We separate the creation of ThunkSections from the insertion of the 1061 // ThunkSections back into the OutputSection as ThunkSections are not always 1062 // inserted into the same OutputSection as the caller. 1063 for (OutputSection *OS : OutputSections) { 1064 ThunkSection *OSTS = nullptr; 1065 for (InputSection *IS : OS->Sections) { 1066 for (Relocation &Rel : IS->Relocations) { 1067 SymbolBody &Body = *Rel.Sym; 1068 if (!Target->needsThunk(Rel.Expr, Rel.Type, IS->File, Body)) 1069 continue; 1070 Thunk *T; 1071 bool IsNew; 1072 std::tie(T, IsNew) = getThunk(Body, Rel.Type); 1073 if (IsNew) { 1074 // Find or create a ThunkSection for the new Thunk 1075 ThunkSection *TS; 1076 if (auto *TIS = T->getTargetInputSection()) 1077 TS = getISThunkSec(TIS, OS); 1078 else 1079 TS = getOSThunkSec(OSTS, OS); 1080 TS->addThunk(T); 1081 } 1082 // Redirect relocation to Thunk, we never go via the PLT to a Thunk 1083 Rel.Sym = T->ThunkSym; 1084 Rel.Expr = fromPlt(Rel.Expr); 1085 } 1086 } 1087 } 1088 1089 // Merge all created synthetic ThunkSections back into OutputSection 1090 for (auto &KV : ThunkSections) 1091 mergeThunks(KV.first, KV.second); 1092 return !ThunkSections.empty(); 1093 } 1094 1095 template void elf::scanRelocations<ELF32LE>(InputSectionBase &); 1096 template void elf::scanRelocations<ELF32BE>(InputSectionBase &); 1097 template void elf::scanRelocations<ELF64LE>(InputSectionBase &); 1098 template void elf::scanRelocations<ELF64BE>(InputSectionBase &); 1099 1100 template class elf::ThunkCreator<ELF32LE>; 1101 template class elf::ThunkCreator<ELF32BE>; 1102 template class elf::ThunkCreator<ELF64LE>; 1103 template class elf::ThunkCreator<ELF64BE>; 1104