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 | // |
George Rimar | 95912d0 | 2016-06-08 12:29:29 +0000 | [diff] [blame] | 10 | // This file contains platform-independent functions to process relocations. |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 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" |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 49 | #include "Thunks.h" |
Eugene Leviant | 8983759 | 2016-10-06 09:45:04 +0000 | [diff] [blame] | 50 | #include "Strings.h" |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 51 | |
| 52 | #include "llvm/Support/Endian.h" |
| 53 | #include "llvm/Support/raw_ostream.h" |
| 54 | |
| 55 | using namespace llvm; |
| 56 | using namespace llvm::ELF; |
| 57 | using namespace llvm::object; |
| 58 | using namespace llvm::support::endian; |
| 59 | |
| 60 | namespace lld { |
| 61 | namespace elf { |
| 62 | |
| 63 | static bool refersToGotEntry(RelExpr Expr) { |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 64 | return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT_LOCAL_PAGE || |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 65 | Expr == R_MIPS_GOT_OFF || Expr == R_MIPS_GOT_OFF32 || |
| 66 | Expr == R_MIPS_TLSGD || Expr == R_MIPS_TLSLD || |
| 67 | Expr == R_GOT_PAGE_PC || Expr == R_GOT_PC || Expr == R_GOT_FROM_END || |
| 68 | Expr == R_TLSGD || Expr == R_TLSGD_PC || Expr == R_TLSDESC || |
| 69 | Expr == R_TLSDESC_PAGE; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 72 | static bool isPreemptible(const SymbolBody &Body, uint32_t Type) { |
| 73 | // In case of MIPS GP-relative relocations always resolve to a definition |
| 74 | // in a regular input file, ignoring the one-definition rule. So we, |
| 75 | // for example, should not attempt to create a dynamic relocation even |
| 76 | // if the target symbol is preemptible. There are two two MIPS GP-relative |
| 77 | // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16 |
| 78 | // can be against a preemptible symbol. |
Simon Atanasyan | a26a157 | 2016-06-10 12:26:09 +0000 | [diff] [blame] | 79 | // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 80 | // relocation types occupy eight bit. In case of N64 ABI we extract first |
| 81 | // relocation from 3-in-1 packet because only the first relocation can |
| 82 | // be against a real symbol. |
Simon Atanasyan | a26a157 | 2016-06-10 12:26:09 +0000 | [diff] [blame] | 83 | if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16) |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 84 | return false; |
| 85 | return Body.isPreemptible(); |
| 86 | } |
| 87 | |
Peter Smith | fde6213 | 2016-09-23 13:54:48 +0000 | [diff] [blame] | 88 | // This function is similar to the `handleTlsRelocation`. ARM and MIPS do not |
| 89 | // support any relaxations for TLS relocations so by factoring out ARM and MIPS |
| 90 | // handling in to the separate function we can simplify the code and do not |
| 91 | // pollute `handleTlsRelocation` by ARM and MIPS `ifs` statements. |
| 92 | // FIXME: The ARM implementation always adds the module index dynamic |
| 93 | // relocation even for non-preemptible symbols in applications. For static |
| 94 | // linking support we must either resolve the module index relocation at static |
| 95 | // link time, or hard code the module index (1) for the application in the GOT. |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 96 | template <class ELFT> |
Peter Smith | fde6213 | 2016-09-23 13:54:48 +0000 | [diff] [blame] | 97 | static unsigned handleNoRelaxTlsRelocation(uint32_t Type, SymbolBody &Body, |
| 98 | InputSectionBase<ELFT> &C, |
| 99 | typename ELFT::uint Offset, |
| 100 | typename ELFT::uint Addend, |
| 101 | RelExpr Expr) { |
| 102 | if (Expr == R_MIPS_TLSLD || Expr == R_TLSLD_PC) { |
| 103 | if (Out<ELFT>::Got->addTlsIndex() && |
| 104 | (Config->Pic || Config->EMachine == EM_ARM)) |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 105 | Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got, |
| 106 | Out<ELFT>::Got->getTlsIndexOff(), false, |
| 107 | nullptr, 0}); |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 108 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 109 | return 1; |
| 110 | } |
Peter Smith | fde6213 | 2016-09-23 13:54:48 +0000 | [diff] [blame] | 111 | typedef typename ELFT::uint uintX_t; |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 112 | if (Target->isTlsGlobalDynamicRel(Type)) { |
Peter Smith | fde6213 | 2016-09-23 13:54:48 +0000 | [diff] [blame] | 113 | if (Out<ELFT>::Got->addDynTlsEntry(Body) && |
| 114 | (Body.isPreemptible() || Config->EMachine == EM_ARM)) { |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 115 | uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body); |
| 116 | Out<ELFT>::RelaDyn->addReloc( |
| 117 | {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0}); |
Peter Smith | fde6213 | 2016-09-23 13:54:48 +0000 | [diff] [blame] | 118 | if (Body.isPreemptible()) |
| 119 | Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got, |
| 120 | Off + (uintX_t)sizeof(uintX_t), false, |
| 121 | &Body, 0}); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 122 | } |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 123 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 124 | return 1; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 129 | // Returns the number of relocations processed. |
| 130 | template <class ELFT> |
| 131 | static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body, |
| 132 | InputSectionBase<ELFT> &C, |
| 133 | typename ELFT::uint Offset, |
| 134 | typename ELFT::uint Addend, RelExpr Expr) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 135 | if (!(C.Flags & SHF_ALLOC)) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | |
| 138 | if (!Body.isTls()) |
| 139 | return 0; |
| 140 | |
| 141 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | e37d13b | 2016-06-02 19:49:53 +0000 | [diff] [blame] | 142 | |
Peter Smith | fde6213 | 2016-09-23 13:54:48 +0000 | [diff] [blame] | 143 | if (Config->EMachine == EM_MIPS || Config->EMachine == EM_ARM) |
| 144 | return handleNoRelaxTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, |
| 145 | Expr); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 146 | |
Peter Smith | d648603 | 2016-10-20 09:59:26 +0000 | [diff] [blame] | 147 | if ((Expr == R_TLSDESC || Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC_CALL) && |
Rafael Espindola | e37d13b | 2016-06-02 19:49:53 +0000 | [diff] [blame] | 148 | Config->Shared) { |
| 149 | if (Out<ELFT>::Got->addDynTlsEntry(Body)) { |
| 150 | uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body); |
| 151 | Out<ELFT>::RelaDyn->addReloc( |
| 152 | {Target->TlsDescRel, Out<ELFT>::Got, Off, false, &Body, 0}); |
| 153 | } |
Peter Smith | d648603 | 2016-10-20 09:59:26 +0000 | [diff] [blame] | 154 | if (Expr != R_TLSDESC_CALL) |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 155 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
Rafael Espindola | e37d13b | 2016-06-02 19:49:53 +0000 | [diff] [blame] | 156 | return 1; |
| 157 | } |
| 158 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 159 | if (Expr == R_TLSLD_PC || Expr == R_TLSLD) { |
| 160 | // Local-Dynamic relocs can be relaxed to Local-Exec. |
| 161 | if (!Config->Shared) { |
| 162 | C.Relocations.push_back( |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 163 | {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 164 | return 2; |
| 165 | } |
| 166 | if (Out<ELFT>::Got->addTlsIndex()) |
| 167 | Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got, |
| 168 | Out<ELFT>::Got->getTlsIndexOff(), false, |
| 169 | nullptr, 0}); |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 170 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | // Local-Dynamic relocs can be relaxed to Local-Exec. |
| 175 | if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) { |
| 176 | C.Relocations.push_back( |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 177 | {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 178 | return 1; |
| 179 | } |
| 180 | |
Peter Smith | d648603 | 2016-10-20 09:59:26 +0000 | [diff] [blame] | 181 | if (Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC || Expr == R_TLSDESC_CALL || |
Rafael Espindola | e37d13b | 2016-06-02 19:49:53 +0000 | [diff] [blame] | 182 | Target->isTlsGlobalDynamicRel(Type)) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 183 | if (Config->Shared) { |
| 184 | if (Out<ELFT>::Got->addDynTlsEntry(Body)) { |
| 185 | uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body); |
| 186 | Out<ELFT>::RelaDyn->addReloc( |
| 187 | {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0}); |
Rafael Espindola | a8777c2 | 2016-06-08 21:31:59 +0000 | [diff] [blame] | 188 | |
| 189 | // If the symbol is preemptible we need the dynamic linker to write |
| 190 | // the offset too. |
Simon Atanasyan | 9b86118 | 2016-06-10 12:26:39 +0000 | [diff] [blame] | 191 | if (isPreemptible(Body, Type)) |
Rafael Espindola | a8777c2 | 2016-06-08 21:31:59 +0000 | [diff] [blame] | 192 | Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got, |
| 193 | Off + (uintX_t)sizeof(uintX_t), false, |
| 194 | &Body, 0}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 195 | } |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 196 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec |
| 201 | // depending on the symbol being locally defined or not. |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 202 | if (isPreemptible(Body, Type)) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 203 | C.Relocations.push_back( |
Rafael Espindola | 69f5402 | 2016-06-04 23:22:34 +0000 | [diff] [blame] | 204 | {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type, |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 205 | Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 206 | if (!Body.isInGot()) { |
| 207 | Out<ELFT>::Got->addEntry(Body); |
| 208 | Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got, |
| 209 | Body.getGotOffset<ELFT>(), false, &Body, |
| 210 | 0}); |
| 211 | } |
Rafael Espindola | e1979ae | 2016-06-04 23:33:31 +0000 | [diff] [blame] | 212 | return Target->TlsGdRelaxSkip; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 213 | } |
| 214 | C.Relocations.push_back( |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 215 | {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type, |
Rafael Espindola | 69f5402 | 2016-06-04 23:22:34 +0000 | [diff] [blame] | 216 | Offset, Addend, &Body}); |
Rafael Espindola | f807d47 | 2016-06-04 23:04:39 +0000 | [diff] [blame] | 217 | return Target->TlsGdRelaxSkip; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally |
| 221 | // defined. |
| 222 | if (Target->isTlsInitialExecRel(Type) && !Config->Shared && |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 223 | !isPreemptible(Body, Type)) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 224 | C.Relocations.push_back( |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 225 | {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 226 | return 1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 231 | template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) { |
| 232 | return read32<E>(Loc) & 0xffff; |
| 233 | } |
| 234 | |
| 235 | template <class RelTy> |
| 236 | static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) { |
| 237 | switch (Rel->getType(Config->Mips64EL)) { |
| 238 | case R_MIPS_HI16: |
| 239 | return R_MIPS_LO16; |
| 240 | case R_MIPS_GOT16: |
| 241 | return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE; |
| 242 | case R_MIPS_PCHI16: |
| 243 | return R_MIPS_PCLO16; |
| 244 | case R_MICROMIPS_HI16: |
| 245 | return R_MICROMIPS_LO16; |
| 246 | default: |
| 247 | return R_MIPS_NONE; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | template <class ELFT, class RelTy> |
| 252 | static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc, |
| 253 | SymbolBody &Sym, const RelTy *Rel, |
| 254 | const RelTy *End) { |
| 255 | uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL); |
| 256 | uint32_t Type = getMipsPairType(Rel, Sym); |
| 257 | |
| 258 | // Some MIPS relocations use addend calculated from addend of the relocation |
| 259 | // itself and addend of paired relocation. ABI requires to compute such |
| 260 | // combined addend in case of REL relocation record format only. |
| 261 | // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 262 | if (RelTy::IsRela || Type == R_MIPS_NONE) |
| 263 | return 0; |
| 264 | |
| 265 | for (const RelTy *RI = Rel; RI != End; ++RI) { |
| 266 | if (RI->getType(Config->Mips64EL) != Type) |
| 267 | continue; |
| 268 | if (RI->getSymbol(Config->Mips64EL) != SymIndex) |
| 269 | continue; |
| 270 | const endianness E = ELFT::TargetEndianness; |
| 271 | return ((read32<E>(BufLoc) & 0xffff) << 16) + |
| 272 | readSignedLo16<E>(Buf + RI->r_offset); |
| 273 | } |
Rui Ueyama | d31e13f | 2016-09-29 21:00:23 +0000 | [diff] [blame] | 274 | warn("can't find matching " + getRelName(Type) + " relocation for " + |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 275 | getRelName(Rel->getType(Config->Mips64EL))); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | // True if non-preemptable symbol always has the same value regardless of where |
| 280 | // the DSO is loaded. |
| 281 | template <class ELFT> static bool isAbsolute(const SymbolBody &Body) { |
| 282 | if (Body.isUndefined()) |
| 283 | return !Body.isLocal() && Body.symbol()->isWeak(); |
| 284 | if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body)) |
| 285 | return DR->Section == nullptr; // Absolute symbol. |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | static bool needsPlt(RelExpr Expr) { |
Rafael Espindola | 12dc446 | 2016-06-04 19:11:14 +0000 | [diff] [blame] | 290 | return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT || |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 291 | Expr == R_PLT_PAGE_PC || Expr == R_THUNK_PLT_PC; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // True if this expression is of the form Sym - X, where X is a position in the |
| 295 | // file (PC, or GOT for example). |
| 296 | static bool isRelExpr(RelExpr Expr) { |
Rafael Espindola | 719f55d | 2016-09-06 13:57:15 +0000 | [diff] [blame] | 297 | return Expr == R_PC || Expr == R_GOTREL || Expr == R_GOTREL_FROM_END || |
| 298 | Expr == R_PAGE_PC || Expr == R_RELAX_GOT_PC || Expr == R_THUNK_PC || |
| 299 | Expr == R_THUNK_PLT_PC; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | template <class ELFT> |
| 303 | static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type, |
| 304 | const SymbolBody &Body) { |
| 305 | // These expressions always compute a constant |
| 306 | if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF || |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 307 | E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF || |
| 308 | E == R_MIPS_GOT_OFF32 || E == R_MIPS_TLSGD || E == R_GOT_PAGE_PC || |
| 309 | E == R_GOT_PC || E == R_PLT_PC || E == R_TLSGD_PC || E == R_TLSGD || |
| 310 | E == R_PPC_PLT_OPD || E == R_TLSDESC_CALL || E == R_TLSDESC_PAGE || |
| 311 | E == R_HINT || E == R_THUNK_PC || E == R_THUNK_PLT_PC) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 312 | return true; |
| 313 | |
| 314 | // These never do, except if the entire file is position dependent or if |
| 315 | // only the low bits are used. |
Rafael Espindola | e37d13b | 2016-06-02 19:49:53 +0000 | [diff] [blame] | 316 | if (E == R_GOT || E == R_PLT || E == R_TLSDESC) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 317 | return Target->usesOnlyLowPageBits(Type) || !Config->Pic; |
| 318 | |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 319 | if (isPreemptible(Body, Type)) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 320 | return false; |
| 321 | |
| 322 | if (!Config->Pic) |
| 323 | return true; |
| 324 | |
| 325 | bool AbsVal = isAbsolute<ELFT>(Body) || Body.isTls(); |
| 326 | bool RelE = isRelExpr(E); |
| 327 | if (AbsVal && !RelE) |
| 328 | return true; |
| 329 | if (!AbsVal && RelE) |
| 330 | return true; |
| 331 | |
| 332 | // Relative relocation to an absolute value. This is normally unrepresentable, |
| 333 | // but if the relocation refers to a weak undefined symbol, we allow it to |
| 334 | // resolve to the image base. This is a little strange, but it allows us to |
| 335 | // link function calls to such symbols. Normally such a call will be guarded |
| 336 | // with a comparison, which will load a zero from the GOT. |
| 337 | if (AbsVal && RelE) { |
| 338 | if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) |
| 339 | return true; |
George Rimar | e6389d1 | 2016-06-08 12:22:26 +0000 | [diff] [blame] | 340 | error("relocation " + getRelName(Type) + |
| 341 | " cannot refer to absolute symbol " + Body.getName()); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | |
| 345 | return Target->usesOnlyLowPageBits(Type); |
| 346 | } |
| 347 | |
| 348 | static RelExpr toPlt(RelExpr Expr) { |
| 349 | if (Expr == R_PPC_OPD) |
| 350 | return R_PPC_PLT_OPD; |
| 351 | if (Expr == R_PC) |
| 352 | return R_PLT_PC; |
Rafael Espindola | 12dc446 | 2016-06-04 19:11:14 +0000 | [diff] [blame] | 353 | if (Expr == R_PAGE_PC) |
| 354 | return R_PLT_PAGE_PC; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 355 | if (Expr == R_ABS) |
| 356 | return R_PLT; |
| 357 | return Expr; |
| 358 | } |
| 359 | |
| 360 | static RelExpr fromPlt(RelExpr Expr) { |
| 361 | // We decided not to use a plt. Optimize a reference to the plt to a |
| 362 | // reference to the symbol itself. |
| 363 | if (Expr == R_PLT_PC) |
| 364 | return R_PC; |
| 365 | if (Expr == R_PPC_PLT_OPD) |
| 366 | return R_PPC_OPD; |
| 367 | if (Expr == R_PLT) |
| 368 | return R_ABS; |
| 369 | return Expr; |
| 370 | } |
| 371 | |
| 372 | template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) { |
| 373 | typedef typename ELFT::uint uintX_t; |
| 374 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 375 | uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 376 | uintX_t SymValue = SS->Sym.st_value; |
| 377 | int TrailingZeros = |
| 378 | std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue)); |
| 379 | return 1 << TrailingZeros; |
| 380 | } |
| 381 | |
| 382 | // Reserve space in .bss for copy relocation. |
| 383 | template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) { |
| 384 | typedef typename ELFT::uint uintX_t; |
| 385 | typedef typename ELFT::Sym Elf_Sym; |
| 386 | |
| 387 | // Copy relocation against zero-sized symbol doesn't make sense. |
| 388 | uintX_t SymSize = SS->template getSize<ELFT>(); |
| 389 | if (SymSize == 0) |
Petr Hosek | 4071b1b | 2016-08-18 21:55:23 +0000 | [diff] [blame] | 390 | fatal("cannot create a copy relocation for symbol " + SS->getName()); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 391 | |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 392 | uintX_t Alignment = getAlignment(SS); |
| 393 | uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Alignment); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 394 | Out<ELFT>::Bss->setSize(Off + SymSize); |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 395 | Out<ELFT>::Bss->updateAlignment(Alignment); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 396 | uintX_t Shndx = SS->Sym.st_shndx; |
| 397 | uintX_t Value = SS->Sym.st_value; |
| 398 | // Look through the DSO's dynamic symbol table for aliases and create a |
| 399 | // dynamic symbol for each one. This causes the copy relocation to correctly |
| 400 | // interpose any aliases. |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 401 | for (const Elf_Sym &S : SS->file()->getElfSymbols(true)) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 402 | if (S.st_shndx != Shndx || S.st_value != Value) |
| 403 | continue; |
| 404 | auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>( |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 405 | Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable())))); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 406 | if (!Alias) |
| 407 | continue; |
| 408 | Alias->OffsetInBss = Off; |
| 409 | Alias->NeedsCopyOrPltAddr = true; |
| 410 | Alias->symbol()->IsUsedInRegularObj = true; |
| 411 | } |
| 412 | Out<ELFT>::RelaDyn->addReloc( |
| 413 | {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0}); |
| 414 | } |
| 415 | |
| 416 | template <class ELFT> |
Petr Hosek | 5b4f6c6 | 2016-08-22 19:01:53 +0000 | [diff] [blame] | 417 | static StringRef getSymbolName(const elf::ObjectFile<ELFT> &File, |
| 418 | SymbolBody &Body) { |
| 419 | if (Body.isLocal() && Body.getNameOffset()) |
| 420 | return File.getStringTable().data() + Body.getNameOffset(); |
| 421 | if (!Body.isLocal()) |
| 422 | return Body.getName(); |
| 423 | return ""; |
Petr Hosek | 4071b1b | 2016-08-18 21:55:23 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | template <class ELFT> |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 427 | static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body, |
George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 428 | bool IsWrite, RelExpr Expr, uint32_t Type, |
Rafael Espindola | f2956a3 | 2016-06-17 15:01:50 +0000 | [diff] [blame] | 429 | const uint8_t *Data) { |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 430 | bool Preemptible = isPreemptible(Body, Type); |
George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 431 | if (Body.isGnuIFunc()) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 432 | Expr = toPlt(Expr); |
George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 433 | } else if (!Preemptible) { |
| 434 | if (needsPlt(Expr)) |
| 435 | Expr = fromPlt(Expr); |
George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 436 | if (Expr == R_GOT_PC) |
Rafael Espindola | f2956a3 | 2016-06-17 15:01:50 +0000 | [diff] [blame] | 437 | Expr = Target->adjustRelaxExpr(Type, Data, Expr); |
George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 438 | } |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 439 | Expr = Target->getThunkExpr(Expr, Type, File, Body); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 440 | |
| 441 | if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body)) |
| 442 | return Expr; |
| 443 | |
| 444 | // This relocation would require the dynamic linker to write a value to read |
| 445 | // only memory. We can hack around it if we are producing an executable and |
| 446 | // the refered symbol can be preemepted to refer to the executable. |
| 447 | if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) { |
Petr Hosek | 5b4f6c6 | 2016-08-22 19:01:53 +0000 | [diff] [blame] | 448 | StringRef Name = getSymbolName(File, Body); |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 449 | error("can't create dynamic relocation " + getRelName(Type) + " against " + |
| 450 | (Name.empty() ? "readonly segment" : "symbol " + Name)); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 451 | return Expr; |
| 452 | } |
| 453 | if (Body.getVisibility() != STV_DEFAULT) { |
Petr Hosek | 4071b1b | 2016-08-18 21:55:23 +0000 | [diff] [blame] | 454 | error("cannot preempt symbol " + Body.getName()); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 455 | return Expr; |
| 456 | } |
| 457 | if (Body.isObject()) { |
| 458 | // Produce a copy relocation. |
| 459 | auto *B = cast<SharedSymbol<ELFT>>(&Body); |
| 460 | if (!B->needsCopy()) |
| 461 | addCopyRelSymbol(B); |
| 462 | return Expr; |
| 463 | } |
| 464 | if (Body.isFunc()) { |
| 465 | // This handles a non PIC program call to function in a shared library. In |
| 466 | // an ideal world, we could just report an error saying the relocation can |
| 467 | // overflow at runtime. In the real world with glibc, crt1.o has a |
| 468 | // R_X86_64_PC32 pointing to libc.so. |
| 469 | // |
| 470 | // The general idea on how to handle such cases is to create a PLT entry and |
| 471 | // use that as the function value. |
| 472 | // |
| 473 | // For the static linking part, we just return a plt expr and everything |
| 474 | // else will use the the PLT entry as the address. |
| 475 | // |
| 476 | // The remaining problem is making sure pointer equality still works. We |
| 477 | // need the help of the dynamic linker for that. We let it know that we have |
| 478 | // a direct reference to a so symbol by creating an undefined symbol with a |
| 479 | // non zero st_value. Seeing that, the dynamic linker resolves the symbol to |
| 480 | // the value of the symbol we created. This is true even for got entries, so |
| 481 | // pointer equality is maintained. To avoid an infinite loop, the only entry |
| 482 | // that points to the real function is a dedicated got entry used by the |
| 483 | // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 484 | // R_386_JMP_SLOT, etc). |
| 485 | Body.NeedsCopyOrPltAddr = true; |
| 486 | return toPlt(Expr); |
| 487 | } |
Petr Hosek | 4071b1b | 2016-08-18 21:55:23 +0000 | [diff] [blame] | 488 | error("symbol " + Body.getName() + " is missing type"); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 489 | |
| 490 | return Expr; |
| 491 | } |
| 492 | |
| 493 | template <class ELFT, class RelTy> |
| 494 | static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File, |
| 495 | const uint8_t *SectionData, |
| 496 | const RelTy *End, const RelTy &RI, |
| 497 | RelExpr Expr, SymbolBody &Body) { |
| 498 | typedef typename ELFT::uint uintX_t; |
| 499 | |
| 500 | uint32_t Type = RI.getType(Config->Mips64EL); |
| 501 | uintX_t Addend = getAddend<ELFT>(RI); |
| 502 | const uint8_t *BufLoc = SectionData + RI.r_offset; |
| 503 | if (!RelTy::IsRela) |
| 504 | Addend += Target->getImplicitAddend(BufLoc, Type); |
| 505 | if (Config->EMachine == EM_MIPS) { |
| 506 | Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End); |
| 507 | if (Type == R_MIPS_LO16 && Expr == R_PC) |
| 508 | // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp |
| 509 | // symbol. In that case we should use the following formula for |
| 510 | // calculation "AHL + GP - P + 4". Let's add 4 right here. |
| 511 | // For details see p. 4-19 at |
| 512 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 513 | Addend += 4; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 514 | if (Expr == R_GOTREL) { |
| 515 | Addend -= MipsGPOffset; |
| 516 | if (Body.isLocal()) |
| 517 | Addend += File.getMipsGp0(); |
| 518 | } |
| 519 | } |
| 520 | if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC) |
| 521 | Addend += getPPC64TocBase(); |
| 522 | return Addend; |
| 523 | } |
| 524 | |
Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 525 | // Find symbol that encloses given offset. Used for error reporting. |
| 526 | template <class ELFT> |
| 527 | static DefinedRegular<ELFT> *getSymbolAt(InputSectionBase<ELFT> *S, |
| 528 | typename ELFT::uint Offset) { |
| 529 | for (SymbolBody *B : S->getFile()->getSymbols()) |
| 530 | if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B)) |
| 531 | if (D->Value <= Offset && D->Value + D->Size > Offset && D->Section == S) |
| 532 | return D; |
| 533 | |
| 534 | return nullptr; |
| 535 | } |
| 536 | |
| 537 | template <class ELFT> |
| 538 | static std::string getLocation(SymbolBody &Sym, InputSectionBase<ELFT> &S, |
| 539 | typename ELFT::uint Offset) { |
| 540 | ObjectFile<ELFT> *File = S.getFile(); |
| 541 | |
| 542 | // First check if we can get desired values from debugging information. |
| 543 | std::string LineInfo = File->getDIHelper()->getLineInfo(Offset); |
| 544 | if (!LineInfo.empty()) |
| 545 | return LineInfo; |
| 546 | |
| 547 | // If don't have STT_FILE typed symbol in object file then |
| 548 | // use object file name. |
| 549 | std::string SrcFile = File->SourceFile; |
| 550 | if (SrcFile.empty()) |
| 551 | SrcFile = Sym.File ? getFilename(Sym.File) : getFilename(File); |
| 552 | |
Rui Ueyama | 5f7e6e6 | 2016-10-26 20:26:29 +0000 | [diff] [blame^] | 553 | // Find a symbol at a given location. |
| 554 | DefinedRegular<ELFT> *Sym = getSymbolAt(&S, Offset); |
| 555 | if (Sym && Sym->Type == STT_FUNC) { |
| 556 | StringRef Func = getSymbolName(*File, *Sym); |
Rui Ueyama | 5ce977c | 2016-10-26 18:28:06 +0000 | [diff] [blame] | 557 | return SrcFile + " (function " + maybeDemangle(Func) + ")"; |
Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Rui Ueyama | 5f7e6e6 | 2016-10-26 20:26:29 +0000 | [diff] [blame^] | 560 | // If there's no symbol, print out the offset instead of a symbol name. |
Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 561 | return (SrcFile + " (" + S.Name + "+0x" + Twine::utohexstr(Offset) + ")") |
| 562 | .str(); |
| 563 | } |
| 564 | |
| 565 | template <class ELFT> |
| 566 | static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &S, |
| 567 | typename ELFT::uint Offset) { |
Eugene Leviant | 8983759 | 2016-10-06 09:45:04 +0000 | [diff] [blame] | 568 | if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore) |
| 569 | return; |
| 570 | |
| 571 | if (Config->Shared && Sym.symbol()->Visibility == STV_DEFAULT && |
| 572 | Config->UnresolvedSymbols != UnresolvedPolicy::NoUndef) |
| 573 | return; |
| 574 | |
Rui Ueyama | 5ce977c | 2016-10-26 18:28:06 +0000 | [diff] [blame] | 575 | std::string Msg = getLocation(Sym, S, Offset) + ": undefined symbol '" + |
| 576 | maybeDemangle(Sym.getName()) + "'"; |
Eugene Leviant | 8983759 | 2016-10-06 09:45:04 +0000 | [diff] [blame] | 577 | |
Eugene Leviant | 8983759 | 2016-10-06 09:45:04 +0000 | [diff] [blame] | 578 | if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn) |
| 579 | warn(Msg); |
| 580 | else |
| 581 | error(Msg); |
| 582 | } |
| 583 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 584 | // The reason we have to do this early scan is as follows |
| 585 | // * To mmap the output file, we need to know the size |
| 586 | // * For that, we need to know how many dynamic relocs we will have. |
| 587 | // It might be possible to avoid this by outputting the file with write: |
| 588 | // * Write the allocated output sections, computing addresses. |
| 589 | // * Apply relocations, recording which ones require a dynamic reloc. |
| 590 | // * Write the dynamic relocations. |
| 591 | // * Write the rest of the file. |
| 592 | // This would have some drawbacks. For example, we would only know if .rela.dyn |
| 593 | // is needed after applying relocations. If it is, it will go after rw and rx |
| 594 | // sections. Given that it is ro, we will need an extra PT_LOAD. This |
| 595 | // complicates things for the dynamic linker and means we would have to reserve |
| 596 | // space for the extra PT_LOAD even if we end up not using it. |
| 597 | template <class ELFT, class RelTy> |
Rui Ueyama | 2487f19 | 2016-05-25 03:40:02 +0000 | [diff] [blame] | 598 | static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 599 | typedef typename ELFT::uint uintX_t; |
| 600 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 601 | bool IsWrite = C.Flags & SHF_WRITE; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 602 | |
| 603 | auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) { |
| 604 | Out<ELFT>::RelaDyn->addReloc(Reloc); |
| 605 | }; |
| 606 | |
| 607 | const elf::ObjectFile<ELFT> &File = *C.getFile(); |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 608 | ArrayRef<uint8_t> SectionData = C.Data; |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 609 | const uint8_t *Buf = SectionData.begin(); |
Rafael Espindola | 5b7a79f | 2016-07-20 11:47:50 +0000 | [diff] [blame] | 610 | |
Rafael Espindola | 3abe3aa | 2016-07-21 21:15:32 +0000 | [diff] [blame] | 611 | ArrayRef<EhSectionPiece> Pieces; |
| 612 | if (auto *Eh = dyn_cast<EhInputSection<ELFT>>(&C)) |
| 613 | Pieces = Eh->Pieces; |
| 614 | |
| 615 | ArrayRef<EhSectionPiece>::iterator PieceI = Pieces.begin(); |
| 616 | ArrayRef<EhSectionPiece>::iterator PieceE = Pieces.end(); |
Rafael Espindola | 5b7a79f | 2016-07-20 11:47:50 +0000 | [diff] [blame] | 617 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 618 | for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) { |
| 619 | const RelTy &RI = *I; |
| 620 | SymbolBody &Body = File.getRelocTargetSym(RI); |
| 621 | uint32_t Type = RI.getType(Config->Mips64EL); |
| 622 | |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 623 | // We only report undefined symbols if they are referenced somewhere in the |
| 624 | // code. |
Eugene Leviant | 8983759 | 2016-10-06 09:45:04 +0000 | [diff] [blame] | 625 | if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak()) |
Eugene Leviant | b380b24 | 2016-10-26 11:07:09 +0000 | [diff] [blame] | 626 | reportUndefined(Body, C, RI.r_offset); |
Eugene Leviant | 8983759 | 2016-10-06 09:45:04 +0000 | [diff] [blame] | 627 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 628 | RelExpr Expr = Target->getRelExpr(Type, Body); |
Rafael Espindola | 678844e | 2016-06-17 15:42:36 +0000 | [diff] [blame] | 629 | bool Preemptible = isPreemptible(Body, Type); |
| 630 | Expr = adjustExpr(File, Body, IsWrite, Expr, Type, Buf + RI.r_offset); |
| 631 | if (HasError) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 632 | continue; |
| 633 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 634 | // Skip a relocation that points to a dead piece |
Rafael Espindola | 5b7a79f | 2016-07-20 11:47:50 +0000 | [diff] [blame] | 635 | // in a eh_frame section. |
| 636 | while (PieceI != PieceE && |
| 637 | (PieceI->InputOff + PieceI->size() <= RI.r_offset)) |
| 638 | ++PieceI; |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 639 | |
| 640 | // Compute the offset of this section in the output section. We do it here |
| 641 | // to try to compute it only once. |
| 642 | uintX_t Offset; |
| 643 | if (PieceI != PieceE) { |
| 644 | assert(PieceI->InputOff <= RI.r_offset && "Relocation not in any piece"); |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 645 | if (PieceI->OutputOff == -1) |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 646 | continue; |
| 647 | Offset = PieceI->OutputOff + RI.r_offset - PieceI->InputOff; |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 648 | } else { |
George Rimar | 3e6833b | 2016-08-19 15:46:28 +0000 | [diff] [blame] | 649 | Offset = RI.r_offset; |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 650 | } |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 651 | |
| 652 | // This relocation does not require got entry, but it is relative to got and |
| 653 | // needs it to be created. Here we request for that. |
Rafael Espindola | 79202c3 | 2016-08-31 23:24:11 +0000 | [diff] [blame] | 654 | if (Expr == R_GOTONLY_PC || Expr == R_GOTONLY_PC_FROM_END || |
| 655 | Expr == R_GOTREL || Expr == R_GOTREL_FROM_END || Expr == R_PPC_TOC) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 656 | Out<ELFT>::Got->HasGotOffRel = true; |
| 657 | |
| 658 | uintX_t Addend = computeAddend(File, Buf, E, RI, Expr, Body); |
| 659 | |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 660 | if (unsigned Processed = |
| 661 | handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 662 | I += (Processed - 1); |
| 663 | continue; |
| 664 | } |
| 665 | |
Peter Smith | d648603 | 2016-10-20 09:59:26 +0000 | [diff] [blame] | 666 | // Ignore "hint" and TLS Descriptor call relocation because they are |
| 667 | // only markers for relaxation. |
| 668 | if (Expr == R_HINT || Expr == R_TLSDESC_CALL) |
Rafael Espindola | e37d13b | 2016-06-02 19:49:53 +0000 | [diff] [blame] | 669 | continue; |
| 670 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 671 | if (needsPlt(Expr) || Expr == R_THUNK_ABS || Expr == R_THUNK_PC || |
| 672 | Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) || |
Simon Atanasyan | 9a9a316 | 2016-05-28 04:49:57 +0000 | [diff] [blame] | 673 | !isPreemptible(Body, Type)) { |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 674 | // If the relocation points to something in the file, we can process it. |
| 675 | bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body); |
| 676 | |
| 677 | // If the output being produced is position independent, the final value |
| 678 | // is still not known. In that case we still need some help from the |
| 679 | // dynamic linker. We can however do better than just copying the incoming |
| 680 | // relocation. We can process some of it and and just ask the dynamic |
| 681 | // linker to add the load address. |
| 682 | if (!Constant) |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 683 | AddDyn({Target->RelativeRel, &C, Offset, true, &Body, Addend}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 684 | |
| 685 | // If the produced value is a constant, we just remember to write it |
| 686 | // when outputting this section. We also have to do it if the format |
| 687 | // uses Elf_Rel, since in that case the written value is the addend. |
| 688 | if (Constant || !RelTy::IsRela) |
Rafael Espindola | 664c652 | 2016-09-07 20:37:34 +0000 | [diff] [blame] | 689 | C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 690 | } else { |
| 691 | // We don't know anything about the finaly symbol. Just ask the dynamic |
| 692 | // linker to handle the relocation for us. |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 693 | AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend}); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 694 | // MIPS ABI turns using of GOT and dynamic relocations inside out. |
| 695 | // While regular ABI uses dynamic relocations to fill up GOT entries |
| 696 | // MIPS ABI requires dynamic linker to fills up GOT entries using |
| 697 | // specially sorted dynamic symbol table. This affects even dynamic |
| 698 | // relocations against symbols which do not require GOT entries |
| 699 | // creation explicitly, i.e. do not have any GOT-relocations. So if |
| 700 | // a preemptible symbol has a dynamic relocation we anyway have |
| 701 | // to create a GOT entry for it. |
| 702 | // If a non-preemptible symbol has a dynamic relocation against it, |
| 703 | // dynamic linker takes it st_value, adds offset and writes down |
| 704 | // result of the dynamic relocation. In case of preemptible symbol |
| 705 | // dynamic linker performs symbol resolution, writes the symbol value |
| 706 | // to the GOT entry and reads the GOT entry when it needs to perform |
| 707 | // a dynamic relocation. |
| 708 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 709 | if (Config->EMachine == EM_MIPS) |
| 710 | Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 711 | continue; |
| 712 | } |
| 713 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 714 | // At this point we are done with the relocated position. Some relocations |
| 715 | // also require us to create a got or plt entry. |
| 716 | |
| 717 | // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol. |
| 718 | if (needsPlt(Expr)) { |
| 719 | if (Body.isInPlt()) |
| 720 | continue; |
| 721 | Out<ELFT>::Plt->addEntry(Body); |
| 722 | |
| 723 | uint32_t Rel; |
| 724 | if (Body.isGnuIFunc() && !Preemptible) |
| 725 | Rel = Target->IRelativeRel; |
| 726 | else |
| 727 | Rel = Target->PltRel; |
| 728 | |
| 729 | Out<ELFT>::GotPlt->addEntry(Body); |
| 730 | Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt, |
| 731 | Body.getGotPltOffset<ELFT>(), !Preemptible, |
| 732 | &Body, 0}); |
| 733 | continue; |
| 734 | } |
| 735 | |
| 736 | if (refersToGotEntry(Expr)) { |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 737 | if (Config->EMachine == EM_MIPS) { |
Simon Atanasyan | af52f6a | 2016-09-08 09:07:12 +0000 | [diff] [blame] | 738 | // MIPS ABI has special rules to process GOT entries and doesn't |
| 739 | // require relocation entries for them. A special case is TLS |
| 740 | // relocations. In that case dynamic loader applies dynamic |
| 741 | // relocations to initialize TLS GOT entries. |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 742 | // See "Global Offset Table" in Chapter 5 in the following document |
| 743 | // for detailed description: |
| 744 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 745 | Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 746 | if (Body.isTls() && Body.isPreemptible()) |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 747 | AddDyn({Target->TlsGotRel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 748 | false, &Body, 0}); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 749 | continue; |
| 750 | } |
| 751 | |
| 752 | if (Body.isInGot()) |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 753 | continue; |
| 754 | |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 755 | Out<ELFT>::Got->addEntry(Body); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 756 | if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) { |
| 757 | uint32_t DynType; |
| 758 | if (Body.isTls()) |
| 759 | DynType = Target->TlsGotRel; |
| 760 | else if (Preemptible) |
| 761 | DynType = Target->GotRel; |
| 762 | else |
| 763 | DynType = Target->RelativeRel; |
| 764 | AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(), |
| 765 | !Preemptible, &Body, 0}); |
| 766 | } |
| 767 | continue; |
| 768 | } |
| 769 | } |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 772 | template <class ELFT> |
| 773 | void scanRelocations(InputSectionBase<ELFT> &S, |
| 774 | const typename ELFT::Shdr &RelSec) { |
| 775 | ELFFile<ELFT> &EObj = S.getFile()->getObj(); |
| 776 | if (RelSec.sh_type == SHT_RELA) |
| 777 | scanRelocs(S, EObj.relas(&RelSec)); |
| 778 | else |
| 779 | scanRelocs(S, EObj.rels(&RelSec)); |
| 780 | } |
| 781 | |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 782 | template <class ELFT, class RelTy> |
| 783 | static void createThunks(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) { |
| 784 | const elf::ObjectFile<ELFT> &File = *C.getFile(); |
| 785 | for (const RelTy &Rel : Rels) { |
| 786 | SymbolBody &Body = File.getRelocTargetSym(Rel); |
| 787 | uint32_t Type = Rel.getType(Config->Mips64EL); |
| 788 | RelExpr Expr = Target->getRelExpr(Type, Body); |
| 789 | if (!isPreemptible(Body, Type) && needsPlt(Expr)) |
| 790 | Expr = fromPlt(Expr); |
| 791 | Expr = Target->getThunkExpr(Expr, Type, File, Body); |
| 792 | // Some targets might require creation of thunks for relocations. |
| 793 | // Now we support only MIPS which requires LA25 thunk to call PIC |
| 794 | // code from non-PIC one, and ARM which requires interworking. |
| 795 | if (Expr == R_THUNK_ABS || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC) { |
| 796 | auto *Sec = cast<InputSection<ELFT>>(&C); |
| 797 | addThunk<ELFT>(Type, Body, *Sec); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | template <class ELFT> |
| 803 | void createThunks(InputSectionBase<ELFT> &S, |
| 804 | const typename ELFT::Shdr &RelSec) { |
| 805 | ELFFile<ELFT> &EObj = S.getFile()->getObj(); |
| 806 | if (RelSec.sh_type == SHT_RELA) |
| 807 | createThunks(S, EObj.relas(&RelSec)); |
| 808 | else |
| 809 | createThunks(S, EObj.rels(&RelSec)); |
| 810 | } |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 811 | |
| 812 | template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &, |
| 813 | const ELF32LE::Shdr &); |
| 814 | template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &, |
| 815 | const ELF32BE::Shdr &); |
| 816 | template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &, |
| 817 | const ELF64LE::Shdr &); |
| 818 | template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &, |
| 819 | const ELF64BE::Shdr &); |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 820 | |
| 821 | template void createThunks<ELF32LE>(InputSectionBase<ELF32LE> &, |
| 822 | const ELF32LE::Shdr &); |
| 823 | template void createThunks<ELF32BE>(InputSectionBase<ELF32BE> &, |
| 824 | const ELF32BE::Shdr &); |
| 825 | template void createThunks<ELF64LE>(InputSectionBase<ELF64LE> &, |
| 826 | const ELF64LE::Shdr &); |
| 827 | template void createThunks<ELF64BE>(InputSectionBase<ELF64BE> &, |
| 828 | const ELF64BE::Shdr &); |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 829 | } |
| 830 | } |