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