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