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