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 processe 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 "OutputSections.h" 47 #include "SymbolTable.h" 48 #include "Target.h" 49 50 #include "llvm/Support/Endian.h" 51 #include "llvm/Support/raw_ostream.h" 52 53 using namespace llvm; 54 using namespace llvm::ELF; 55 using namespace llvm::object; 56 using namespace llvm::support::endian; 57 58 namespace lld { 59 namespace elf { 60 61 static bool refersToGotEntry(RelExpr Expr) { 62 return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT_LOCAL || 63 Expr == R_MIPS_GOT_LOCAL_PAGE || Expr == R_GOT_PAGE_PC || 64 Expr == R_GOT_PC || Expr == R_GOT_FROM_END || Expr == R_TLSGD || 65 Expr == R_TLSGD_PC; 66 } 67 68 static bool isPreemptible(const SymbolBody &Body, uint32_t Type) { 69 // In case of MIPS GP-relative relocations always resolve to a definition 70 // in a regular input file, ignoring the one-definition rule. So we, 71 // for example, should not attempt to create a dynamic relocation even 72 // if the target symbol is preemptible. There are two two MIPS GP-relative 73 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16 74 // can be against a preemptible symbol. 75 // To get MIPS relocation type we apply 0xf mask. In case of O32 ABI all 76 // relocation types occupy eight bit. In case of N64 ABI we extract first 77 // relocation from 3-in-1 packet because only the first relocation can 78 // be against a real symbol. 79 if (Config->EMachine == EM_MIPS && (Type & 0xf) == R_MIPS_GPREL16) 80 return false; 81 return Body.isPreemptible(); 82 } 83 84 // Returns the number of relocations processed. 85 template <class ELFT> 86 static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body, 87 InputSectionBase<ELFT> &C, 88 typename ELFT::uint Offset, 89 typename ELFT::uint Addend, RelExpr Expr) { 90 if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC)) 91 return 0; 92 93 if (!Body.isTls()) 94 return 0; 95 96 typedef typename ELFT::uint uintX_t; 97 if (Expr == R_TLSLD_PC || Expr == R_TLSLD) { 98 // Local-Dynamic relocs can be relaxed to Local-Exec. 99 if (!Config->Shared) { 100 C.Relocations.push_back( 101 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); 102 return 2; 103 } 104 if (Out<ELFT>::Got->addTlsIndex()) 105 Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got, 106 Out<ELFT>::Got->getTlsIndexOff(), false, 107 nullptr, 0}); 108 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 109 return 1; 110 } 111 112 // Local-Dynamic relocs can be relaxed to Local-Exec. 113 if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) { 114 C.Relocations.push_back( 115 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); 116 return 1; 117 } 118 119 if (Target->isTlsGlobalDynamicRel(Type)) { 120 if (Config->Shared) { 121 if (Out<ELFT>::Got->addDynTlsEntry(Body)) { 122 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body); 123 Out<ELFT>::RelaDyn->addReloc( 124 {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0}); 125 Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::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 133 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec 134 // depending on the symbol being locally defined or not. 135 if (isPreemptible(Body, Type)) { 136 C.Relocations.push_back( 137 {R_RELAX_TLS_GD_TO_IE, Type, Offset, Addend, &Body}); 138 if (!Body.isInGot()) { 139 Out<ELFT>::Got->addEntry(Body); 140 Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got, 141 Body.getGotOffset<ELFT>(), false, &Body, 142 0}); 143 } 144 return 2; 145 } 146 C.Relocations.push_back( 147 {R_RELAX_TLS_GD_TO_LE, Type, Offset, Addend, &Body}); 148 return Target->TlsGdToLeSkip; 149 } 150 151 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally 152 // defined. 153 if (Target->isTlsInitialExecRel(Type) && !Config->Shared && 154 !isPreemptible(Body, Type)) { 155 C.Relocations.push_back( 156 {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body}); 157 return 1; 158 } 159 return 0; 160 } 161 162 // Some targets might require creation of thunks for relocations. Now we 163 // support only MIPS which requires LA25 thunk to call PIC code from non-PIC 164 // one. Scan relocations to find each one requires thunk. 165 template <class ELFT, class RelTy> 166 static void scanRelocsForThunks(const elf::ObjectFile<ELFT> &File, 167 ArrayRef<RelTy> Rels) { 168 for (const RelTy &RI : Rels) { 169 uint32_t Type = RI.getType(Config->Mips64EL); 170 SymbolBody &Body = File.getRelocTargetSym(RI); 171 if (Body.hasThunk() || !Target->needsThunk(Type, File, Body)) 172 continue; 173 auto *D = cast<DefinedRegular<ELFT>>(&Body); 174 auto *S = cast<InputSection<ELFT>>(D->Section); 175 S->addThunk(Body); 176 } 177 } 178 179 template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) { 180 return read32<E>(Loc) & 0xffff; 181 } 182 183 template <class RelTy> 184 static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) { 185 switch (Rel->getType(Config->Mips64EL)) { 186 case R_MIPS_HI16: 187 return R_MIPS_LO16; 188 case R_MIPS_GOT16: 189 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE; 190 case R_MIPS_PCHI16: 191 return R_MIPS_PCLO16; 192 case R_MICROMIPS_HI16: 193 return R_MICROMIPS_LO16; 194 default: 195 return R_MIPS_NONE; 196 } 197 } 198 199 template <class ELFT, class RelTy> 200 static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc, 201 SymbolBody &Sym, const RelTy *Rel, 202 const RelTy *End) { 203 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL); 204 uint32_t Type = getMipsPairType(Rel, Sym); 205 206 // Some MIPS relocations use addend calculated from addend of the relocation 207 // itself and addend of paired relocation. ABI requires to compute such 208 // combined addend in case of REL relocation record format only. 209 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 210 if (RelTy::IsRela || Type == R_MIPS_NONE) 211 return 0; 212 213 for (const RelTy *RI = Rel; RI != End; ++RI) { 214 if (RI->getType(Config->Mips64EL) != Type) 215 continue; 216 if (RI->getSymbol(Config->Mips64EL) != SymIndex) 217 continue; 218 const endianness E = ELFT::TargetEndianness; 219 return ((read32<E>(BufLoc) & 0xffff) << 16) + 220 readSignedLo16<E>(Buf + RI->r_offset); 221 } 222 unsigned OldType = Rel->getType(Config->Mips64EL); 223 StringRef OldName = getELFRelocationTypeName(Config->EMachine, OldType); 224 StringRef NewName = getELFRelocationTypeName(Config->EMachine, Type); 225 warning("can't find matching " + NewName + " relocation for " + OldName); 226 return 0; 227 } 228 229 // True if non-preemptable symbol always has the same value regardless of where 230 // the DSO is loaded. 231 template <class ELFT> static bool isAbsolute(const SymbolBody &Body) { 232 if (Body.isUndefined()) 233 return !Body.isLocal() && Body.symbol()->isWeak(); 234 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body)) 235 return DR->Section == nullptr; // Absolute symbol. 236 return false; 237 } 238 239 static bool needsPlt(RelExpr Expr) { 240 return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT; 241 } 242 243 // True if this expression is of the form Sym - X, where X is a position in the 244 // file (PC, or GOT for example). 245 static bool isRelExpr(RelExpr Expr) { 246 return Expr == R_PC || Expr == R_GOTREL || Expr == R_PAGE_PC || 247 Expr == R_RELAX_GOT_PC; 248 } 249 250 template <class ELFT> 251 static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type, 252 const SymbolBody &Body) { 253 // These expressions always compute a constant 254 if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF || 255 E == R_MIPS_GOT_LOCAL || E == R_MIPS_GOT_LOCAL_PAGE || 256 E == R_GOT_PAGE_PC || E == R_GOT_PC || E == R_PLT_PC || E == R_TLSGD_PC || 257 E == R_TLSGD || E == R_PPC_PLT_OPD) 258 return true; 259 260 // These never do, except if the entire file is position dependent or if 261 // only the low bits are used. 262 if (E == R_GOT || E == R_PLT) 263 return Target->usesOnlyLowPageBits(Type) || !Config->Pic; 264 265 if (isPreemptible(Body, Type)) 266 return false; 267 268 if (!Config->Pic) 269 return true; 270 271 bool AbsVal = isAbsolute<ELFT>(Body) || Body.isTls(); 272 bool RelE = isRelExpr(E); 273 if (AbsVal && !RelE) 274 return true; 275 if (!AbsVal && RelE) 276 return true; 277 278 // Relative relocation to an absolute value. This is normally unrepresentable, 279 // but if the relocation refers to a weak undefined symbol, we allow it to 280 // resolve to the image base. This is a little strange, but it allows us to 281 // link function calls to such symbols. Normally such a call will be guarded 282 // with a comparison, which will load a zero from the GOT. 283 if (AbsVal && RelE) { 284 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) 285 return true; 286 StringRef S = getELFRelocationTypeName(Config->EMachine, Type); 287 error("relocation " + S + " cannot refer to absolute symbol " + 288 Body.getName()); 289 return true; 290 } 291 292 return Target->usesOnlyLowPageBits(Type); 293 } 294 295 static RelExpr toPlt(RelExpr Expr) { 296 if (Expr == R_PPC_OPD) 297 return R_PPC_PLT_OPD; 298 if (Expr == R_PC) 299 return R_PLT_PC; 300 if (Expr == R_ABS) 301 return R_PLT; 302 return Expr; 303 } 304 305 static RelExpr fromPlt(RelExpr Expr) { 306 // We decided not to use a plt. Optimize a reference to the plt to a 307 // reference to the symbol itself. 308 if (Expr == R_PLT_PC) 309 return R_PC; 310 if (Expr == R_PPC_PLT_OPD) 311 return R_PPC_OPD; 312 if (Expr == R_PLT) 313 return R_ABS; 314 return Expr; 315 } 316 317 template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) { 318 typedef typename ELFT::uint uintX_t; 319 320 uintX_t SecAlign = SS->File->getSection(SS->Sym)->sh_addralign; 321 uintX_t SymValue = SS->Sym.st_value; 322 int TrailingZeros = 323 std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue)); 324 return 1 << TrailingZeros; 325 } 326 327 // Reserve space in .bss for copy relocation. 328 template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) { 329 typedef typename ELFT::uint uintX_t; 330 typedef typename ELFT::Sym Elf_Sym; 331 332 // Copy relocation against zero-sized symbol doesn't make sense. 333 uintX_t SymSize = SS->template getSize<ELFT>(); 334 if (SymSize == 0) 335 fatal("cannot create a copy relocation for " + SS->getName()); 336 337 uintX_t Align = getAlignment(SS); 338 uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Align); 339 Out<ELFT>::Bss->setSize(Off + SymSize); 340 Out<ELFT>::Bss->updateAlign(Align); 341 uintX_t Shndx = SS->Sym.st_shndx; 342 uintX_t Value = SS->Sym.st_value; 343 // Look through the DSO's dynamic symbol table for aliases and create a 344 // dynamic symbol for each one. This causes the copy relocation to correctly 345 // interpose any aliases. 346 for (const Elf_Sym &S : SS->File->getElfSymbols(true)) { 347 if (S.st_shndx != Shndx || S.st_value != Value) 348 continue; 349 auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>( 350 Symtab<ELFT>::X->find(check(S.getName(SS->File->getStringTable())))); 351 if (!Alias) 352 continue; 353 Alias->OffsetInBss = Off; 354 Alias->NeedsCopyOrPltAddr = true; 355 Alias->symbol()->IsUsedInRegularObj = true; 356 } 357 Out<ELFT>::RelaDyn->addReloc( 358 {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0}); 359 } 360 361 template <class ELFT> 362 static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body, 363 bool IsWrite, RelExpr Expr, uint32_t Type, 364 const uint8_t *Data, typename ELFT::uint Offset) { 365 if (Target->needsThunk(Type, File, Body)) 366 return R_THUNK; 367 bool Preemptible = isPreemptible(Body, Type); 368 if (Body.isGnuIFunc()) { 369 Expr = toPlt(Expr); 370 } else if (!Preemptible) { 371 if (needsPlt(Expr)) 372 Expr = fromPlt(Expr); 373 if (Expr == R_GOT_PC) 374 Expr = Target->adjustRelaxGotExpr(Type, Data + Offset, Expr); 375 } 376 377 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body)) 378 return Expr; 379 380 // This relocation would require the dynamic linker to write a value to read 381 // only memory. We can hack around it if we are producing an executable and 382 // the refered symbol can be preemepted to refer to the executable. 383 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) { 384 StringRef S = getELFRelocationTypeName(Config->EMachine, Type); 385 error("relocation " + S + " cannot be used when making a shared " 386 "object; recompile with -fPIC."); 387 return Expr; 388 } 389 if (Body.getVisibility() != STV_DEFAULT) { 390 error("Cannot preempt symbol"); 391 return Expr; 392 } 393 if (Body.isObject()) { 394 // Produce a copy relocation. 395 auto *B = cast<SharedSymbol<ELFT>>(&Body); 396 if (!B->needsCopy()) 397 addCopyRelSymbol(B); 398 return Expr; 399 } 400 if (Body.isFunc()) { 401 // This handles a non PIC program call to function in a shared library. In 402 // an ideal world, we could just report an error saying the relocation can 403 // overflow at runtime. In the real world with glibc, crt1.o has a 404 // R_X86_64_PC32 pointing to libc.so. 405 // 406 // The general idea on how to handle such cases is to create a PLT entry and 407 // use that as the function value. 408 // 409 // For the static linking part, we just return a plt expr and everything 410 // else will use the the PLT entry as the address. 411 // 412 // The remaining problem is making sure pointer equality still works. We 413 // need the help of the dynamic linker for that. We let it know that we have 414 // a direct reference to a so symbol by creating an undefined symbol with a 415 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to 416 // the value of the symbol we created. This is true even for got entries, so 417 // pointer equality is maintained. To avoid an infinite loop, the only entry 418 // that points to the real function is a dedicated got entry used by the 419 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, 420 // R_386_JMP_SLOT, etc). 421 Body.NeedsCopyOrPltAddr = true; 422 return toPlt(Expr); 423 } 424 error("Symbol is missing type"); 425 426 return Expr; 427 } 428 429 template <class ELFT, class RelTy> 430 static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File, 431 const uint8_t *SectionData, 432 const RelTy *End, const RelTy &RI, 433 RelExpr Expr, SymbolBody &Body) { 434 typedef typename ELFT::uint uintX_t; 435 436 uint32_t Type = RI.getType(Config->Mips64EL); 437 uintX_t Addend = getAddend<ELFT>(RI); 438 const uint8_t *BufLoc = SectionData + RI.r_offset; 439 if (!RelTy::IsRela) 440 Addend += Target->getImplicitAddend(BufLoc, Type); 441 if (Config->EMachine == EM_MIPS) { 442 Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End); 443 if (Type == R_MIPS_LO16 && Expr == R_PC) 444 // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp 445 // symbol. In that case we should use the following formula for 446 // calculation "AHL + GP - P + 4". Let's add 4 right here. 447 // For details see p. 4-19 at 448 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 449 Addend += 4; 450 if (Expr == R_GOT_OFF) 451 Addend -= MipsGPOffset; 452 if (Expr == R_GOTREL) { 453 Addend -= MipsGPOffset; 454 if (Body.isLocal()) 455 Addend += File.getMipsGp0(); 456 } 457 } 458 if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC) 459 Addend += getPPC64TocBase(); 460 return Addend; 461 } 462 463 // The reason we have to do this early scan is as follows 464 // * To mmap the output file, we need to know the size 465 // * For that, we need to know how many dynamic relocs we will have. 466 // It might be possible to avoid this by outputting the file with write: 467 // * Write the allocated output sections, computing addresses. 468 // * Apply relocations, recording which ones require a dynamic reloc. 469 // * Write the dynamic relocations. 470 // * Write the rest of the file. 471 // This would have some drawbacks. For example, we would only know if .rela.dyn 472 // is needed after applying relocations. If it is, it will go after rw and rx 473 // sections. Given that it is ro, we will need an extra PT_LOAD. This 474 // complicates things for the dynamic linker and means we would have to reserve 475 // space for the extra PT_LOAD even if we end up not using it. 476 template <class ELFT, class RelTy> 477 static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) { 478 typedef typename ELFT::uint uintX_t; 479 480 uintX_t Flags = C.getSectionHdr()->sh_flags; 481 bool IsWrite = Flags & SHF_WRITE; 482 483 auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) { 484 Out<ELFT>::RelaDyn->addReloc(Reloc); 485 }; 486 487 const elf::ObjectFile<ELFT> &File = *C.getFile(); 488 ArrayRef<uint8_t> SectionData = C.getSectionData(); 489 const uint8_t *Buf = SectionData.begin(); 490 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) { 491 const RelTy &RI = *I; 492 SymbolBody &Body = File.getRelocTargetSym(RI); 493 uint32_t Type = RI.getType(Config->Mips64EL); 494 495 RelExpr Expr = Target->getRelExpr(Type, Body); 496 // Ignore "hint" relocation because it is for optional code optimization. 497 if (Expr == R_HINT) 498 continue; 499 500 uintX_t Offset = C.getOffset(RI.r_offset); 501 if (Offset == (uintX_t)-1) 502 continue; 503 504 bool Preemptible = isPreemptible(Body, Type); 505 Expr = adjustExpr(File, Body, IsWrite, Expr, Type, Buf, Offset); 506 if (HasError) 507 continue; 508 509 // This relocation does not require got entry, but it is relative to got and 510 // needs it to be created. Here we request for that. 511 if (Expr == R_GOTONLY_PC || Expr == R_GOTREL || Expr == R_PPC_TOC) 512 Out<ELFT>::Got->HasGotOffRel = true; 513 514 uintX_t Addend = computeAddend(File, Buf, E, RI, Expr, Body); 515 516 if (unsigned Processed = 517 handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) { 518 I += (Processed - 1); 519 continue; 520 } 521 522 if (needsPlt(Expr) || Expr == R_THUNK || refersToGotEntry(Expr) || 523 !isPreemptible(Body, Type)) { 524 // If the relocation points to something in the file, we can process it. 525 bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body); 526 527 // If the output being produced is position independent, the final value 528 // is still not known. In that case we still need some help from the 529 // dynamic linker. We can however do better than just copying the incoming 530 // relocation. We can process some of it and and just ask the dynamic 531 // linker to add the load address. 532 if (!Constant) 533 AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body, Addend}); 534 535 // If the produced value is a constant, we just remember to write it 536 // when outputting this section. We also have to do it if the format 537 // uses Elf_Rel, since in that case the written value is the addend. 538 if (Constant || !RelTy::IsRela) 539 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); 540 } else { 541 // We don't know anything about the finaly symbol. Just ask the dynamic 542 // linker to handle the relocation for us. 543 AddDyn({Target->getDynRel(Type), C.OutSec, Offset, false, &Body, Addend}); 544 // MIPS ABI turns using of GOT and dynamic relocations inside out. 545 // While regular ABI uses dynamic relocations to fill up GOT entries 546 // MIPS ABI requires dynamic linker to fills up GOT entries using 547 // specially sorted dynamic symbol table. This affects even dynamic 548 // relocations against symbols which do not require GOT entries 549 // creation explicitly, i.e. do not have any GOT-relocations. So if 550 // a preemptible symbol has a dynamic relocation we anyway have 551 // to create a GOT entry for it. 552 // If a non-preemptible symbol has a dynamic relocation against it, 553 // dynamic linker takes it st_value, adds offset and writes down 554 // result of the dynamic relocation. In case of preemptible symbol 555 // dynamic linker performs symbol resolution, writes the symbol value 556 // to the GOT entry and reads the GOT entry when it needs to perform 557 // a dynamic relocation. 558 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 559 if (Config->EMachine == EM_MIPS && !Body.isInGot()) 560 Out<ELFT>::Got->addEntry(Body); 561 continue; 562 } 563 564 if (Expr == R_THUNK) 565 continue; 566 567 // At this point we are done with the relocated position. Some relocations 568 // also require us to create a got or plt entry. 569 570 // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol. 571 if (needsPlt(Expr)) { 572 if (Body.isInPlt()) 573 continue; 574 Out<ELFT>::Plt->addEntry(Body); 575 576 uint32_t Rel; 577 if (Body.isGnuIFunc() && !Preemptible) 578 Rel = Target->IRelativeRel; 579 else 580 Rel = Target->PltRel; 581 582 Out<ELFT>::GotPlt->addEntry(Body); 583 Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt, 584 Body.getGotPltOffset<ELFT>(), !Preemptible, 585 &Body, 0}); 586 continue; 587 } 588 589 if (refersToGotEntry(Expr)) { 590 if (Body.isInGot()) 591 continue; 592 Out<ELFT>::Got->addEntry(Body); 593 594 if (Config->EMachine == EM_MIPS) 595 // MIPS ABI has special rules to process GOT entries 596 // and doesn't require relocation entries for them. 597 // See "Global Offset Table" in Chapter 5 in the following document 598 // for detailed description: 599 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 600 continue; 601 602 if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) { 603 uint32_t DynType; 604 if (Body.isTls()) 605 DynType = Target->TlsGotRel; 606 else if (Preemptible) 607 DynType = Target->GotRel; 608 else 609 DynType = Target->RelativeRel; 610 AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), 611 !Preemptible, &Body, 0}); 612 } 613 continue; 614 } 615 } 616 617 // Scan relocations for necessary thunks. 618 if (Config->EMachine == EM_MIPS) 619 scanRelocsForThunks<ELFT>(File, Rels); 620 } 621 622 template <class ELFT> void scanRelocations(InputSection<ELFT> &C) { 623 typedef typename ELFT::Shdr Elf_Shdr; 624 625 // Scan all relocations. Each relocation goes through a series 626 // of tests to determine if it needs special treatment, such as 627 // creating GOT, PLT, copy relocations, etc. 628 // Note that relocations for non-alloc sections are directly 629 // processed by InputSection::relocateNative. 630 if (C.getSectionHdr()->sh_flags & SHF_ALLOC) 631 for (const Elf_Shdr *RelSec : C.RelocSections) 632 scanRelocations(C, *RelSec); 633 } 634 635 template <class ELFT> 636 void scanRelocations(InputSectionBase<ELFT> &S, 637 const typename ELFT::Shdr &RelSec) { 638 ELFFile<ELFT> &EObj = S.getFile()->getObj(); 639 if (RelSec.sh_type == SHT_RELA) 640 scanRelocs(S, EObj.relas(&RelSec)); 641 else 642 scanRelocs(S, EObj.rels(&RelSec)); 643 } 644 645 template void scanRelocations<ELF32LE>(InputSection<ELF32LE> &); 646 template void scanRelocations<ELF32BE>(InputSection<ELF32BE> &); 647 template void scanRelocations<ELF64LE>(InputSection<ELF64LE> &); 648 template void scanRelocations<ELF64BE>(InputSection<ELF64BE> &); 649 650 template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &, 651 const ELF32LE::Shdr &); 652 template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &, 653 const ELF32BE::Shdr &); 654 template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &, 655 const ELF64LE::Shdr &); 656 template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &, 657 const ELF64BE::Shdr &); 658 } 659 } 660