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