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