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