Mercurial > hg > CbC > CbC_llvm
comparison lld/ELF/Relocations.cpp @ 236:c4bab56944e8 llvm-original
LLVM 16
author | kono |
---|---|
date | Wed, 09 Nov 2022 17:45:10 +0900 |
parents | 5f17cb93ff66 |
children | 1f2b6ac9f198 |
comparison
equal
deleted
inserted
replaced
232:70dce7da266c | 236:c4bab56944e8 |
---|---|
40 // | 40 // |
41 //===----------------------------------------------------------------------===// | 41 //===----------------------------------------------------------------------===// |
42 | 42 |
43 #include "Relocations.h" | 43 #include "Relocations.h" |
44 #include "Config.h" | 44 #include "Config.h" |
45 #include "InputFiles.h" | |
45 #include "LinkerScript.h" | 46 #include "LinkerScript.h" |
46 #include "OutputSections.h" | 47 #include "OutputSections.h" |
47 #include "SymbolTable.h" | 48 #include "SymbolTable.h" |
48 #include "Symbols.h" | 49 #include "Symbols.h" |
49 #include "SyntheticSections.h" | 50 #include "SyntheticSections.h" |
50 #include "Target.h" | 51 #include "Target.h" |
51 #include "Thunks.h" | 52 #include "Thunks.h" |
52 #include "lld/Common/ErrorHandler.h" | 53 #include "lld/Common/ErrorHandler.h" |
53 #include "lld/Common/Memory.h" | 54 #include "lld/Common/Memory.h" |
54 #include "lld/Common/Strings.h" | |
55 #include "llvm/ADT/SmallSet.h" | 55 #include "llvm/ADT/SmallSet.h" |
56 #include "llvm/Demangle/Demangle.h" | 56 #include "llvm/Demangle/Demangle.h" |
57 #include "llvm/Support/Endian.h" | 57 #include "llvm/Support/Endian.h" |
58 #include "llvm/Support/raw_ostream.h" | |
59 #include <algorithm> | 58 #include <algorithm> |
60 | 59 |
61 using namespace llvm; | 60 using namespace llvm; |
62 using namespace llvm::ELF; | 61 using namespace llvm::ELF; |
63 using namespace llvm::object; | 62 using namespace llvm::object; |
64 using namespace llvm::support::endian; | 63 using namespace llvm::support::endian; |
65 using namespace lld; | 64 using namespace lld; |
66 using namespace lld::elf; | 65 using namespace lld::elf; |
67 | 66 |
68 static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) { | 67 static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) { |
69 for (BaseCommand *base : script->sectionCommands) | 68 for (SectionCommand *cmd : script->sectionCommands) |
70 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) | 69 if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) |
71 if (cmd->sym == &sym) | 70 if (assign->sym == &sym) |
72 return cmd->location; | 71 return assign->location; |
73 return None; | 72 return None; |
74 } | 73 } |
75 | 74 |
76 static std::string getDefinedLocation(const Symbol &sym) { | 75 static std::string getDefinedLocation(const Symbol &sym) { |
77 const char msg[] = "\n>>> defined in "; | 76 const char msg[] = "\n>>> defined in "; |
98 | 97 |
99 void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, | 98 void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, |
100 int64_t min, uint64_t max) { | 99 int64_t min, uint64_t max) { |
101 ErrorPlace errPlace = getErrorPlace(loc); | 100 ErrorPlace errPlace = getErrorPlace(loc); |
102 std::string hint; | 101 std::string hint; |
103 if (rel.sym && !rel.sym->isLocal()) | 102 if (rel.sym && !rel.sym->isSection()) |
104 hint = "; references " + lld::toString(*rel.sym) + | 103 hint = "; references " + lld::toString(*rel.sym); |
105 getDefinedLocation(*rel.sym); | 104 if (!errPlace.srcLoc.empty()) |
105 hint += "\n>>> referenced by " + errPlace.srcLoc; | |
106 if (rel.sym && !rel.sym->isSection()) | |
107 hint += getDefinedLocation(*rel.sym); | |
106 | 108 |
107 if (errPlace.isec && errPlace.isec->name.startswith(".debug")) | 109 if (errPlace.isec && errPlace.isec->name.startswith(".debug")) |
108 hint += "; consider recompiling with -fdebug-types-section to reduce size " | 110 hint += "; consider recompiling with -fdebug-types-section to reduce size " |
109 "of debug sections"; | 111 "of debug sections"; |
110 | 112 |
122 errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) + | 124 errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) + |
123 " is not in [" + Twine(llvm::minIntN(n)) + ", " + | 125 " is not in [" + Twine(llvm::minIntN(n)) + ", " + |
124 Twine(llvm::maxIntN(n)) + "]" + hint); | 126 Twine(llvm::maxIntN(n)) + "]" + hint); |
125 } | 127 } |
126 | 128 |
127 namespace { | 129 // Build a bitmask with one bit set for each 64 subset of RelExpr. |
128 // Build a bitmask with one bit set for each RelExpr. | 130 static constexpr uint64_t buildMask() { return 0; } |
129 // | 131 |
130 // Constexpr function arguments can't be used in static asserts, so we | 132 template <typename... Tails> |
131 // use template arguments to build the mask. | 133 static constexpr uint64_t buildMask(int head, Tails... tails) { |
132 // But function template partial specializations don't exist (needed | 134 return (0 <= head && head < 64 ? uint64_t(1) << head : 0) | |
133 // for base case of the recursion), so we need a dummy struct. | 135 buildMask(tails...); |
134 template <RelExpr... Exprs> struct RelExprMaskBuilder { | 136 } |
135 static inline uint64_t build() { return 0; } | |
136 }; | |
137 | |
138 // Specialization for recursive case. | |
139 template <RelExpr Head, RelExpr... Tail> | |
140 struct RelExprMaskBuilder<Head, Tail...> { | |
141 static inline uint64_t build() { | |
142 static_assert(0 <= Head && Head < 64, | |
143 "RelExpr is too large for 64-bit mask!"); | |
144 return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build(); | |
145 } | |
146 }; | |
147 } // namespace | |
148 | 137 |
149 // Return true if `Expr` is one of `Exprs`. | 138 // Return true if `Expr` is one of `Exprs`. |
150 // There are fewer than 64 RelExpr's, so we can represent any set of | 139 // There are more than 64 but less than 128 RelExprs, so we divide the set of |
151 // RelExpr's as a constant bit mask and test for membership with a | 140 // exprs into [0, 64) and [64, 128) and represent each range as a constant |
152 // couple cheap bitwise operations. | 141 // 64-bit mask. Then we decide which mask to test depending on the value of |
153 template <RelExpr... Exprs> bool oneof(RelExpr expr) { | 142 // expr and use a simple shift and bitwise-and to test for membership. |
154 assert(0 <= expr && (int)expr < 64 && | 143 template <RelExpr... Exprs> static bool oneof(RelExpr expr) { |
155 "RelExpr is too large for 64-bit mask!"); | 144 assert(0 <= expr && (int)expr < 128 && |
156 return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build(); | 145 "RelExpr is too large for 128-bit mask!"); |
157 } | 146 |
158 | 147 if (expr >= 64) |
159 // This function is similar to the `handleTlsRelocation`. MIPS does not | 148 return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...); |
160 // support any relaxations for TLS relocations so by factoring out MIPS | 149 return (uint64_t(1) << expr) & buildMask(Exprs...); |
161 // handling in to the separate function we can simplify the code and do not | |
162 // pollute other `handleTlsRelocation` by MIPS `ifs` statements. | |
163 // Mips has a custom MipsGotSection that handles the writing of GOT entries | |
164 // without dynamic relocations. | |
165 static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym, | |
166 InputSectionBase &c, uint64_t offset, | |
167 int64_t addend, RelExpr expr) { | |
168 if (expr == R_MIPS_TLSLD) { | |
169 in.mipsGot->addTlsIndex(*c.file); | |
170 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
171 return 1; | |
172 } | |
173 if (expr == R_MIPS_TLSGD) { | |
174 in.mipsGot->addDynTlsEntry(*c.file, sym); | |
175 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
176 return 1; | |
177 } | |
178 return 0; | |
179 } | |
180 | |
181 // Notes about General Dynamic and Local Dynamic TLS models below. They may | |
182 // require the generation of a pair of GOT entries that have associated dynamic | |
183 // relocations. The pair of GOT entries created are of the form GOT[e0] Module | |
184 // Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of | |
185 // symbol in TLS block. | |
186 // | |
187 // Returns the number of relocations processed. | |
188 template <class ELFT> | |
189 static unsigned | |
190 handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c, | |
191 typename ELFT::uint offset, int64_t addend, RelExpr expr) { | |
192 if (!sym.isTls()) | |
193 return 0; | |
194 | |
195 if (config->emachine == EM_MIPS) | |
196 return handleMipsTlsRelocation(type, sym, c, offset, addend, expr); | |
197 | |
198 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC>( | |
199 expr) && | |
200 config->shared) { | |
201 if (in.got->addDynTlsEntry(sym)) { | |
202 uint64_t off = in.got->getGlobalDynOffset(sym); | |
203 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( | |
204 target->tlsDescRel, in.got, off, sym, target->tlsDescRel); | |
205 } | |
206 if (expr != R_TLSDESC_CALL) | |
207 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
208 return 1; | |
209 } | |
210 | |
211 // ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For | |
212 // PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable | |
213 // relaxation as well. | |
214 bool toExecRelax = !config->shared && config->emachine != EM_ARM && | |
215 config->emachine != EM_HEXAGON && | |
216 config->emachine != EM_RISCV && | |
217 !c.file->ppc64DisableTLSRelax; | |
218 | |
219 // If we are producing an executable and the symbol is non-preemptable, it | |
220 // must be defined and the code sequence can be relaxed to use Local-Exec. | |
221 // | |
222 // ARM and RISC-V do not support any relaxations for TLS relocations, however, | |
223 // we can omit the DTPMOD dynamic relocations and resolve them at link time | |
224 // because them are always 1. This may be necessary for static linking as | |
225 // DTPMOD may not be expected at load time. | |
226 bool isLocalInExecutable = !sym.isPreemptible && !config->shared; | |
227 | |
228 // Local Dynamic is for access to module local TLS variables, while still | |
229 // being suitable for being dynamically loaded via dlopen. GOT[e0] is the | |
230 // module index, with a special value of 0 for the current module. GOT[e1] is | |
231 // unused. There only needs to be one module index entry. | |
232 if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>( | |
233 expr)) { | |
234 // Local-Dynamic relocs can be relaxed to Local-Exec. | |
235 if (toExecRelax) { | |
236 c.relocations.push_back( | |
237 {target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, offset, | |
238 addend, &sym}); | |
239 return target->getTlsGdRelaxSkip(type); | |
240 } | |
241 if (expr == R_TLSLD_HINT) | |
242 return 1; | |
243 if (in.got->addTlsIndex()) { | |
244 if (isLocalInExecutable) | |
245 in.got->relocations.push_back( | |
246 {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym}); | |
247 else | |
248 mainPart->relaDyn->addReloc( | |
249 {target->tlsModuleIndexRel, in.got, in.got->getTlsIndexOff()}); | |
250 } | |
251 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
252 return 1; | |
253 } | |
254 | |
255 // Local-Dynamic relocs can be relaxed to Local-Exec. | |
256 if (expr == R_DTPREL && toExecRelax) { | |
257 c.relocations.push_back({target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), | |
258 type, offset, addend, &sym}); | |
259 return 1; | |
260 } | |
261 | |
262 // Local-Dynamic sequence where offset of tls variable relative to dynamic | |
263 // thread pointer is stored in the got. This cannot be relaxed to Local-Exec. | |
264 if (expr == R_TLSLD_GOT_OFF) { | |
265 if (!sym.isInGot()) { | |
266 in.got->addEntry(sym); | |
267 uint64_t off = sym.getGotOffset(); | |
268 in.got->relocations.push_back( | |
269 {R_ABS, target->tlsOffsetRel, off, 0, &sym}); | |
270 } | |
271 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
272 return 1; | |
273 } | |
274 | |
275 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, | |
276 R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) { | |
277 if (!toExecRelax) { | |
278 if (in.got->addDynTlsEntry(sym)) { | |
279 uint64_t off = in.got->getGlobalDynOffset(sym); | |
280 | |
281 if (isLocalInExecutable) | |
282 // Write one to the GOT slot. | |
283 in.got->relocations.push_back( | |
284 {R_ADDEND, target->symbolicRel, off, 1, &sym}); | |
285 else | |
286 mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, in.got, | |
287 off, sym); | |
288 | |
289 // If the symbol is preemptible we need the dynamic linker to write | |
290 // the offset too. | |
291 uint64_t offsetOff = off + config->wordsize; | |
292 if (sym.isPreemptible) | |
293 mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, in.got, | |
294 offsetOff, sym); | |
295 else | |
296 in.got->relocations.push_back( | |
297 {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym}); | |
298 } | |
299 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
300 return 1; | |
301 } | |
302 | |
303 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec | |
304 // depending on the symbol being locally defined or not. | |
305 if (sym.isPreemptible) { | |
306 c.relocations.push_back( | |
307 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, offset, | |
308 addend, &sym}); | |
309 if (!sym.isInGot()) { | |
310 in.got->addEntry(sym); | |
311 mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, in.got, | |
312 sym.getGotOffset(), sym); | |
313 } | |
314 } else { | |
315 c.relocations.push_back( | |
316 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, offset, | |
317 addend, &sym}); | |
318 } | |
319 return target->getTlsGdRelaxSkip(type); | |
320 } | |
321 | |
322 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally | |
323 // defined. | |
324 if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF, | |
325 R_TLSIE_HINT>(expr) && | |
326 toExecRelax && isLocalInExecutable) { | |
327 c.relocations.push_back({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym}); | |
328 return 1; | |
329 } | |
330 | |
331 if (expr == R_TLSIE_HINT) | |
332 return 1; | |
333 return 0; | |
334 } | 150 } |
335 | 151 |
336 static RelType getMipsPairType(RelType type, bool isLocal) { | 152 static RelType getMipsPairType(RelType type, bool isLocal) { |
337 switch (type) { | 153 switch (type) { |
338 case R_MIPS_HI16: | 154 case R_MIPS_HI16: |
372 return isAbsolute(sym) || sym.isTls(); | 188 return isAbsolute(sym) || sym.isTls(); |
373 } | 189 } |
374 | 190 |
375 // Returns true if Expr refers a PLT entry. | 191 // Returns true if Expr refers a PLT entry. |
376 static bool needsPlt(RelExpr expr) { | 192 static bool needsPlt(RelExpr expr) { |
377 return oneof<R_PLT_PC, R_PPC32_PLTREL, R_PPC64_CALL_PLT, R_PLT>(expr); | 193 return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT>( |
194 expr); | |
378 } | 195 } |
379 | 196 |
380 // Returns true if Expr refers a GOT entry. Note that this function | 197 // Returns true if Expr refers a GOT entry. Note that this function |
381 // returns false for TLS variables even though they need GOT, because | 198 // returns false for TLS variables even though they need GOT, because |
382 // TLS variables uses GOT differently than the regular variables. | 199 // TLS variables uses GOT differently than the regular variables. |
392 return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL, | 209 return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL, |
393 R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC, | 210 R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC, |
394 R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr); | 211 R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr); |
395 } | 212 } |
396 | 213 |
397 // Returns true if a given relocation can be computed at link-time. | |
398 // | |
399 // For instance, we know the offset from a relocation to its target at | |
400 // link-time if the relocation is PC-relative and refers a | |
401 // non-interposable function in the same executable. This function | |
402 // will return true for such relocation. | |
403 // | |
404 // If this function returns false, that means we need to emit a | |
405 // dynamic relocation so that the relocation will be fixed at load-time. | |
406 static bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym, | |
407 InputSectionBase &s, uint64_t relOff) { | |
408 // These expressions always compute a constant | |
409 if (oneof<R_DTPREL, R_GOTPLT, R_GOT_OFF, R_TLSLD_GOT_OFF, | |
410 R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOTREL, R_MIPS_GOT_OFF, | |
411 R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, R_MIPS_TLSGD, | |
412 R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC, | |
413 R_PLT_PC, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC, R_PPC32_PLTREL, | |
414 R_PPC64_CALL_PLT, R_PPC64_RELAX_TOC, R_RISCV_ADD, R_TLSDESC_CALL, | |
415 R_TLSDESC_PC, R_AARCH64_TLSDESC_PAGE, R_TLSLD_HINT, R_TLSIE_HINT, | |
416 R_AARCH64_GOT_PAGE>( | |
417 e)) | |
418 return true; | |
419 | |
420 // These never do, except if the entire file is position dependent or if | |
421 // only the low bits are used. | |
422 if (e == R_GOT || e == R_PLT || e == R_TLSDESC) | |
423 return target->usesOnlyLowPageBits(type) || !config->isPic; | |
424 | |
425 if (sym.isPreemptible) | |
426 return false; | |
427 if (!config->isPic) | |
428 return true; | |
429 | |
430 // The size of a non preemptible symbol is a constant. | |
431 if (e == R_SIZE) | |
432 return true; | |
433 | |
434 // For the target and the relocation, we want to know if they are | |
435 // absolute or relative. | |
436 bool absVal = isAbsoluteValue(sym); | |
437 bool relE = isRelExpr(e); | |
438 if (absVal && !relE) | |
439 return true; | |
440 if (!absVal && relE) | |
441 return true; | |
442 if (!absVal && !relE) | |
443 return target->usesOnlyLowPageBits(type); | |
444 | |
445 assert(absVal && relE); | |
446 | |
447 // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol | |
448 // in PIC mode. This is a little strange, but it allows us to link function | |
449 // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers). | |
450 // Normally such a call will be guarded with a comparison, which will load a | |
451 // zero from the GOT. | |
452 if (sym.isUndefWeak()) | |
453 return true; | |
454 | |
455 // We set the final symbols values for linker script defined symbols later. | |
456 // They always can be computed as a link time constant. | |
457 if (sym.scriptDefined) | |
458 return true; | |
459 | |
460 error("relocation " + toString(type) + " cannot refer to absolute symbol: " + | |
461 toString(sym) + getLocation(s, sym, relOff)); | |
462 return true; | |
463 } | |
464 | 214 |
465 static RelExpr toPlt(RelExpr expr) { | 215 static RelExpr toPlt(RelExpr expr) { |
466 switch (expr) { | 216 switch (expr) { |
467 case R_PPC64_CALL: | 217 case R_PPC64_CALL: |
468 return R_PPC64_CALL_PLT; | 218 return R_PPC64_CALL_PLT; |
484 return R_PC; | 234 return R_PC; |
485 case R_PPC64_CALL_PLT: | 235 case R_PPC64_CALL_PLT: |
486 return R_PPC64_CALL; | 236 return R_PPC64_CALL; |
487 case R_PLT: | 237 case R_PLT: |
488 return R_ABS; | 238 return R_ABS; |
239 case R_PLT_GOTPLT: | |
240 return R_GOTPLTREL; | |
489 default: | 241 default: |
490 return expr; | 242 return expr; |
491 } | 243 } |
492 } | 244 } |
493 | 245 |
494 // Returns true if a given shared symbol is in a read-only segment in a DSO. | 246 // Returns true if a given shared symbol is in a read-only segment in a DSO. |
495 template <class ELFT> static bool isReadOnly(SharedSymbol &ss) { | 247 template <class ELFT> static bool isReadOnly(SharedSymbol &ss) { |
496 using Elf_Phdr = typename ELFT::Phdr; | 248 using Elf_Phdr = typename ELFT::Phdr; |
497 | 249 |
498 // Determine if the symbol is read-only by scanning the DSO's program headers. | 250 // Determine if the symbol is read-only by scanning the DSO's program headers. |
499 const SharedFile &file = ss.getFile(); | 251 const auto &file = cast<SharedFile>(*ss.file); |
500 for (const Elf_Phdr &phdr : | 252 for (const Elf_Phdr &phdr : |
501 check(file.template getObj<ELFT>().program_headers())) | 253 check(file.template getObj<ELFT>().program_headers())) |
502 if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) && | 254 if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) && |
503 !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr && | 255 !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr && |
504 ss.value < phdr.p_vaddr + phdr.p_memsz) | 256 ss.value < phdr.p_vaddr + phdr.p_memsz) |
513 // Otherwise, they would refer to different places at runtime. | 265 // Otherwise, they would refer to different places at runtime. |
514 template <class ELFT> | 266 template <class ELFT> |
515 static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) { | 267 static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) { |
516 using Elf_Sym = typename ELFT::Sym; | 268 using Elf_Sym = typename ELFT::Sym; |
517 | 269 |
518 SharedFile &file = ss.getFile(); | 270 const auto &file = cast<SharedFile>(*ss.file); |
519 | 271 |
520 SmallSet<SharedSymbol *, 4> ret; | 272 SmallSet<SharedSymbol *, 4> ret; |
521 for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) { | 273 for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) { |
522 if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS || | 274 if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS || |
523 s.getType() == STT_TLS || s.st_value != ss.value) | 275 s.getType() == STT_TLS || s.st_value != ss.value) |
524 continue; | 276 continue; |
525 StringRef name = check(s.getName(file.getStringTable())); | 277 StringRef name = check(s.getName(file.getStringTable())); |
526 Symbol *sym = symtab->find(name); | 278 Symbol *sym = symtab.find(name); |
527 if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym)) | 279 if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym)) |
528 ret.insert(alias); | 280 ret.insert(alias); |
529 } | 281 } |
282 | |
283 // The loop does not check SHT_GNU_verneed, so ret does not contain | |
284 // non-default version symbols. If ss has a non-default version, ret won't | |
285 // contain ss. Just add ss unconditionally. If a non-default version alias is | |
286 // separately copy relocated, it and ss will have different addresses. | |
287 // Fortunately this case is impractical and fails with GNU ld as well. | |
288 ret.insert(&ss); | |
530 return ret; | 289 return ret; |
531 } | 290 } |
532 | 291 |
533 // When a symbol is copy relocated or we create a canonical plt entry, it is | 292 // When a symbol is copy relocated or we create a canonical plt entry, it is |
534 // effectively a defined symbol. In the case of copy relocation the symbol is | 293 // effectively a defined symbol. In the case of copy relocation the symbol is |
535 // in .bss and in the case of a canonical plt entry it is in .plt. This function | 294 // in .bss and in the case of a canonical plt entry it is in .plt. This function |
536 // replaces the existing symbol with a Defined pointing to the appropriate | 295 // replaces the existing symbol with a Defined pointing to the appropriate |
537 // location. | 296 // location. |
538 static void replaceWithDefined(Symbol &sym, SectionBase *sec, uint64_t value, | 297 static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value, |
539 uint64_t size) { | 298 uint64_t size) { |
540 Symbol old = sym; | 299 Symbol old = sym; |
541 | 300 Defined(sym.file, StringRef(), sym.binding, sym.stOther, sym.type, value, |
542 sym.replace(Defined{sym.file, sym.getName(), sym.binding, sym.stOther, | 301 size, &sec) |
543 sym.type, value, size, sec}); | 302 .overwrite(sym); |
544 | 303 |
545 sym.pltIndex = old.pltIndex; | |
546 sym.gotIndex = old.gotIndex; | |
547 sym.verdefIndex = old.verdefIndex; | 304 sym.verdefIndex = old.verdefIndex; |
548 sym.exportDynamic = true; | 305 sym.exportDynamic = true; |
549 sym.isUsedInRegularObj = true; | 306 sym.isUsedInRegularObj = true; |
307 // A copy relocated alias may need a GOT entry. | |
308 sym.flags.store(old.flags.load(std::memory_order_relaxed) & NEEDS_GOT, | |
309 std::memory_order_relaxed); | |
550 } | 310 } |
551 | 311 |
552 // Reserve space in .bss or .bss.rel.ro for copy relocation. | 312 // Reserve space in .bss or .bss.rel.ro for copy relocation. |
553 // | 313 // |
554 // The copy relocation is pretty much a hack. If you use a copy relocation | 314 // The copy relocation is pretty much a hack. If you use a copy relocation |
604 make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment); | 364 make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment); |
605 OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent(); | 365 OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent(); |
606 | 366 |
607 // At this point, sectionBases has been migrated to sections. Append sec to | 367 // At this point, sectionBases has been migrated to sections. Append sec to |
608 // sections. | 368 // sections. |
609 if (osec->sectionCommands.empty() || | 369 if (osec->commands.empty() || |
610 !isa<InputSectionDescription>(osec->sectionCommands.back())) | 370 !isa<InputSectionDescription>(osec->commands.back())) |
611 osec->sectionCommands.push_back(make<InputSectionDescription>("")); | 371 osec->commands.push_back(make<InputSectionDescription>("")); |
612 auto *isd = cast<InputSectionDescription>(osec->sectionCommands.back()); | 372 auto *isd = cast<InputSectionDescription>(osec->commands.back()); |
613 isd->sections.push_back(sec); | 373 isd->sections.push_back(sec); |
614 osec->commitSection(sec); | 374 osec->commitSection(sec); |
615 | 375 |
616 // Look through the DSO's dynamic symbol table for aliases and create a | 376 // Look through the DSO's dynamic symbol table for aliases and create a |
617 // dynamic symbol for each one. This causes the copy relocation to correctly | 377 // dynamic symbol for each one. This causes the copy relocation to correctly |
618 // interpose any aliases. | 378 // interpose any aliases. |
619 for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss)) | 379 for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss)) |
620 replaceWithDefined(*sym, sec, 0, sym->size); | 380 replaceWithDefined(*sym, *sec, 0, sym->size); |
621 | 381 |
622 mainPart->relaDyn->addSymbolReloc(target->copyRel, sec, 0, ss); | 382 mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss); |
623 } | 383 } |
384 | |
385 // .eh_frame sections are mergeable input sections, so their input | |
386 // offsets are not linearly mapped to output section. For each input | |
387 // offset, we need to find a section piece containing the offset and | |
388 // add the piece's base address to the input offset to compute the | |
389 // output offset. That isn't cheap. | |
390 // | |
391 // This class is to speed up the offset computation. When we process | |
392 // relocations, we access offsets in the monotonically increasing | |
393 // order. So we can optimize for that access pattern. | |
394 // | |
395 // For sections other than .eh_frame, this class doesn't do anything. | |
396 namespace { | |
397 class OffsetGetter { | |
398 public: | |
399 OffsetGetter() = default; | |
400 explicit OffsetGetter(InputSectionBase &sec) { | |
401 if (auto *eh = dyn_cast<EhInputSection>(&sec)) { | |
402 cies = eh->cies; | |
403 fdes = eh->fdes; | |
404 i = cies.begin(); | |
405 j = fdes.begin(); | |
406 } | |
407 } | |
408 | |
409 // Translates offsets in input sections to offsets in output sections. | |
410 // Given offset must increase monotonically. We assume that Piece is | |
411 // sorted by inputOff. | |
412 uint64_t get(uint64_t off) { | |
413 if (cies.empty()) | |
414 return off; | |
415 | |
416 while (j != fdes.end() && j->inputOff <= off) | |
417 ++j; | |
418 auto it = j; | |
419 if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) { | |
420 while (i != cies.end() && i->inputOff <= off) | |
421 ++i; | |
422 if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) | |
423 fatal(".eh_frame: relocation is not in any piece"); | |
424 it = i; | |
425 } | |
426 | |
427 // Offset -1 means that the piece is dead (i.e. garbage collected). | |
428 if (it[-1].outputOff == -1) | |
429 return -1; | |
430 return it[-1].outputOff + (off - it[-1].inputOff); | |
431 } | |
432 | |
433 private: | |
434 ArrayRef<EhSectionPiece> cies, fdes; | |
435 ArrayRef<EhSectionPiece>::iterator i, j; | |
436 }; | |
437 | |
438 // This class encapsulates states needed to scan relocations for one | |
439 // InputSectionBase. | |
440 class RelocationScanner { | |
441 public: | |
442 template <class ELFT> void scanSection(InputSectionBase &s); | |
443 | |
444 private: | |
445 InputSectionBase *sec; | |
446 OffsetGetter getter; | |
447 | |
448 // End of relocations, used by Mips/PPC64. | |
449 const void *end = nullptr; | |
450 | |
451 template <class RelTy> RelType getMipsN32RelType(RelTy *&rel) const; | |
452 template <class ELFT, class RelTy> | |
453 int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const; | |
454 bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym, | |
455 uint64_t relOff) const; | |
456 void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, | |
457 int64_t addend) const; | |
458 template <class ELFT, class RelTy> void scanOne(RelTy *&i); | |
459 template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels); | |
460 }; | |
461 } // namespace | |
624 | 462 |
625 // MIPS has an odd notion of "paired" relocations to calculate addends. | 463 // MIPS has an odd notion of "paired" relocations to calculate addends. |
626 // For example, if a relocation is of R_MIPS_HI16, there must be a | 464 // For example, if a relocation is of R_MIPS_HI16, there must be a |
627 // R_MIPS_LO16 relocation after that, and an addend is calculated using | 465 // R_MIPS_LO16 relocation after that, and an addend is calculated using |
628 // the two relocations. | 466 // the two relocations. |
629 template <class ELFT, class RelTy> | 467 template <class ELFT, class RelTy> |
630 static int64_t computeMipsAddend(const RelTy &rel, const RelTy *end, | 468 int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr, |
631 InputSectionBase &sec, RelExpr expr, | 469 bool isLocal) const { |
632 bool isLocal) { | |
633 if (expr == R_MIPS_GOTREL && isLocal) | 470 if (expr == R_MIPS_GOTREL && isLocal) |
634 return sec.getFile<ELFT>()->mipsGp0; | 471 return sec->getFile<ELFT>()->mipsGp0; |
635 | 472 |
636 // The ABI says that the paired relocation is used only for REL. | 473 // The ABI says that the paired relocation is used only for REL. |
637 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf | 474 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
638 if (RelTy::IsRela) | 475 if (RelTy::IsRela) |
639 return 0; | 476 return 0; |
641 RelType type = rel.getType(config->isMips64EL); | 478 RelType type = rel.getType(config->isMips64EL); |
642 uint32_t pairTy = getMipsPairType(type, isLocal); | 479 uint32_t pairTy = getMipsPairType(type, isLocal); |
643 if (pairTy == R_MIPS_NONE) | 480 if (pairTy == R_MIPS_NONE) |
644 return 0; | 481 return 0; |
645 | 482 |
646 const uint8_t *buf = sec.data().data(); | 483 const uint8_t *buf = sec->rawData.data(); |
647 uint32_t symIndex = rel.getSymbol(config->isMips64EL); | 484 uint32_t symIndex = rel.getSymbol(config->isMips64EL); |
648 | 485 |
649 // To make things worse, paired relocations might not be contiguous in | 486 // To make things worse, paired relocations might not be contiguous in |
650 // the relocation table, so we need to do linear search. *sigh* | 487 // the relocation table, so we need to do linear search. *sigh* |
651 for (const RelTy *ri = &rel; ri != end; ++ri) | 488 for (const RelTy *ri = &rel; ri != static_cast<const RelTy *>(end); ++ri) |
652 if (ri->getType(config->isMips64EL) == pairTy && | 489 if (ri->getType(config->isMips64EL) == pairTy && |
653 ri->getSymbol(config->isMips64EL) == symIndex) | 490 ri->getSymbol(config->isMips64EL) == symIndex) |
654 return target->getImplicitAddend(buf + ri->r_offset, pairTy); | 491 return target->getImplicitAddend(buf + ri->r_offset, pairTy); |
655 | 492 |
656 warn("can't find matching " + toString(pairTy) + " relocation for " + | 493 warn("can't find matching " + toString(pairTy) + " relocation for " + |
657 toString(type)); | 494 toString(type)); |
658 return 0; | 495 return 0; |
659 } | |
660 | |
661 // Returns an addend of a given relocation. If it is RELA, an addend | |
662 // is in a relocation itself. If it is REL, we need to read it from an | |
663 // input section. | |
664 template <class ELFT, class RelTy> | |
665 static int64_t computeAddend(const RelTy &rel, const RelTy *end, | |
666 InputSectionBase &sec, RelExpr expr, | |
667 bool isLocal) { | |
668 int64_t addend; | |
669 RelType type = rel.getType(config->isMips64EL); | |
670 | |
671 if (RelTy::IsRela) { | |
672 addend = getAddend<ELFT>(rel); | |
673 } else { | |
674 const uint8_t *buf = sec.data().data(); | |
675 addend = target->getImplicitAddend(buf + rel.r_offset, type); | |
676 } | |
677 | |
678 if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC) | |
679 addend += getPPC64TocBase(); | |
680 if (config->emachine == EM_MIPS) | |
681 addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal); | |
682 | |
683 return addend; | |
684 } | 496 } |
685 | 497 |
686 // Custom error message if Sym is defined in a discarded section. | 498 // Custom error message if Sym is defined in a discarded section. |
687 template <class ELFT> | 499 template <class ELFT> |
688 static std::string maybeReportDiscarded(Undefined &sym) { | 500 static std::string maybeReportDiscarded(Undefined &sym) { |
689 auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file); | 501 auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file); |
690 if (!file || !sym.discardedSecIdx || | 502 if (!file || !sym.discardedSecIdx || |
691 file->getSections()[sym.discardedSecIdx] != &InputSection::discarded) | 503 file->getSections()[sym.discardedSecIdx] != &InputSection::discarded) |
692 return ""; | 504 return ""; |
693 ArrayRef<Elf_Shdr_Impl<ELFT>> objSections = | 505 ArrayRef<typename ELFT::Shdr> objSections = |
694 CHECK(file->getObj().sections(), file); | 506 file->template getELFShdrs<ELFT>(); |
695 | 507 |
696 std::string msg; | 508 std::string msg; |
697 if (sym.type == ELF::STT_SECTION) { | 509 if (sym.type == ELF::STT_SECTION) { |
698 msg = "relocation refers to a discarded section: "; | 510 msg = "relocation refers to a discarded section: "; |
699 msg += CHECK( | 511 msg += CHECK( |
709 return msg; | 521 return msg; |
710 | 522 |
711 // If the discarded section is a COMDAT. | 523 // If the discarded section is a COMDAT. |
712 StringRef signature = file->getShtGroupSignature(objSections, elfSec); | 524 StringRef signature = file->getShtGroupSignature(objSections, elfSec); |
713 if (const InputFile *prevailing = | 525 if (const InputFile *prevailing = |
714 symtab->comdatGroups.lookup(CachedHashStringRef(signature))) | 526 symtab.comdatGroups.lookup(CachedHashStringRef(signature))) { |
715 msg += "\n>>> section group signature: " + signature.str() + | 527 msg += "\n>>> section group signature: " + signature.str() + |
716 "\n>>> prevailing definition is in " + toString(prevailing); | 528 "\n>>> prevailing definition is in " + toString(prevailing); |
529 if (sym.nonPrevailing) { | |
530 msg += "\n>>> or the symbol in the prevailing group had STB_WEAK " | |
531 "binding and the symbol in a non-prevailing group had STB_GLOBAL " | |
532 "binding. Mixing groups with STB_WEAK and STB_GLOBAL binding " | |
533 "signature is not supported"; | |
534 } | |
535 } | |
717 return msg; | 536 return msg; |
718 } | 537 } |
719 | 538 |
539 namespace { | |
720 // Undefined diagnostics are collected in a vector and emitted once all of | 540 // Undefined diagnostics are collected in a vector and emitted once all of |
721 // them are known, so that some postprocessing on the list of undefined symbols | 541 // them are known, so that some postprocessing on the list of undefined symbols |
722 // can happen before lld emits diagnostics. | 542 // can happen before lld emits diagnostics. |
723 struct UndefinedDiag { | 543 struct UndefinedDiag { |
724 Symbol *sym; | 544 Undefined *sym; |
725 struct Loc { | 545 struct Loc { |
726 InputSectionBase *sec; | 546 InputSectionBase *sec; |
727 uint64_t offset; | 547 uint64_t offset; |
728 }; | 548 }; |
729 std::vector<Loc> locs; | 549 std::vector<Loc> locs; |
730 bool isWarning; | 550 bool isWarning; |
731 }; | 551 }; |
732 | 552 |
733 static std::vector<UndefinedDiag> undefs; | 553 std::vector<UndefinedDiag> undefs; |
554 std::mutex relocMutex; | |
555 } | |
734 | 556 |
735 // Check whether the definition name def is a mangled function name that matches | 557 // Check whether the definition name def is a mangled function name that matches |
736 // the reference name ref. | 558 // the reference name ref. |
737 static bool canSuggestExternCForCXX(StringRef ref, StringRef def) { | 559 static bool canSuggestExternCForCXX(StringRef ref, StringRef def) { |
738 llvm::ItaniumPartialDemangler d; | 560 llvm::ItaniumPartialDemangler d; |
748 } | 570 } |
749 | 571 |
750 // Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns | 572 // Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns |
751 // the suggested symbol, which is either in the symbol table, or in the same | 573 // the suggested symbol, which is either in the symbol table, or in the same |
752 // file of sym. | 574 // file of sym. |
753 template <class ELFT> | |
754 static const Symbol *getAlternativeSpelling(const Undefined &sym, | 575 static const Symbol *getAlternativeSpelling(const Undefined &sym, |
755 std::string &pre_hint, | 576 std::string &pre_hint, |
756 std::string &post_hint) { | 577 std::string &post_hint) { |
757 DenseMap<StringRef, const Symbol *> map; | 578 DenseMap<StringRef, const Symbol *> map; |
758 if (auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file)) { | 579 if (sym.file && sym.file->kind() == InputFile::ObjKind) { |
580 auto *file = cast<ELFFileBase>(sym.file); | |
759 // If sym is a symbol defined in a discarded section, maybeReportDiscarded() | 581 // If sym is a symbol defined in a discarded section, maybeReportDiscarded() |
760 // will give an error. Don't suggest an alternative spelling. | 582 // will give an error. Don't suggest an alternative spelling. |
761 if (file && sym.discardedSecIdx != 0 && | 583 if (file && sym.discardedSecIdx != 0 && |
762 file->getSections()[sym.discardedSecIdx] == &InputSection::discarded) | 584 file->getSections()[sym.discardedSecIdx] == &InputSection::discarded) |
763 return nullptr; | 585 return nullptr; |
772 // If defined locally. | 594 // If defined locally. |
773 if (const Symbol *s = map.lookup(newName)) | 595 if (const Symbol *s = map.lookup(newName)) |
774 return s; | 596 return s; |
775 | 597 |
776 // If in the symbol table and not undefined. | 598 // If in the symbol table and not undefined. |
777 if (const Symbol *s = symtab->find(newName)) | 599 if (const Symbol *s = symtab.find(newName)) |
778 if (!s->isUndefined()) | 600 if (!s->isUndefined()) |
779 return s; | 601 return s; |
780 | 602 |
781 return nullptr; | 603 return nullptr; |
782 }; | 604 }; |
821 | 643 |
822 // Case mismatch, e.g. Foo vs FOO. | 644 // Case mismatch, e.g. Foo vs FOO. |
823 for (auto &it : map) | 645 for (auto &it : map) |
824 if (name.equals_insensitive(it.first)) | 646 if (name.equals_insensitive(it.first)) |
825 return it.second; | 647 return it.second; |
826 for (Symbol *sym : symtab->symbols()) | 648 for (Symbol *sym : symtab.getSymbols()) |
827 if (!sym->isUndefined() && name.equals_insensitive(sym->getName())) | 649 if (!sym->isUndefined() && name.equals_insensitive(sym->getName())) |
828 return sym; | 650 return sym; |
829 | 651 |
830 // The reference may be a mangled name while the definition is not. Suggest a | 652 // The reference may be a mangled name while the definition is not. Suggest a |
831 // missing extern "C". | 653 // missing extern "C". |
847 if (canSuggestExternCForCXX(name, it.first)) { | 669 if (canSuggestExternCForCXX(name, it.first)) { |
848 s = it.second; | 670 s = it.second; |
849 break; | 671 break; |
850 } | 672 } |
851 if (!s) | 673 if (!s) |
852 for (Symbol *sym : symtab->symbols()) | 674 for (Symbol *sym : symtab.getSymbols()) |
853 if (canSuggestExternCForCXX(name, sym->getName())) { | 675 if (canSuggestExternCForCXX(name, sym->getName())) { |
854 s = sym; | 676 s = sym; |
855 break; | 677 break; |
856 } | 678 } |
857 if (s) { | 679 if (s) { |
862 } | 684 } |
863 | 685 |
864 return nullptr; | 686 return nullptr; |
865 } | 687 } |
866 | 688 |
867 template <class ELFT> | |
868 static void reportUndefinedSymbol(const UndefinedDiag &undef, | 689 static void reportUndefinedSymbol(const UndefinedDiag &undef, |
869 bool correctSpelling) { | 690 bool correctSpelling) { |
870 Symbol &sym = *undef.sym; | 691 Undefined &sym = *undef.sym; |
871 | 692 |
872 auto visibility = [&]() -> std::string { | 693 auto visibility = [&]() -> std::string { |
873 switch (sym.visibility) { | 694 switch (sym.visibility()) { |
874 case STV_INTERNAL: | 695 case STV_INTERNAL: |
875 return "internal "; | 696 return "internal "; |
876 case STV_HIDDEN: | 697 case STV_HIDDEN: |
877 return "hidden "; | 698 return "hidden "; |
878 case STV_PROTECTED: | 699 case STV_PROTECTED: |
880 default: | 701 default: |
881 return ""; | 702 return ""; |
882 } | 703 } |
883 }; | 704 }; |
884 | 705 |
885 std::string msg = maybeReportDiscarded<ELFT>(cast<Undefined>(sym)); | 706 std::string msg; |
707 switch (config->ekind) { | |
708 case ELF32LEKind: | |
709 msg = maybeReportDiscarded<ELF32LE>(sym); | |
710 break; | |
711 case ELF32BEKind: | |
712 msg = maybeReportDiscarded<ELF32BE>(sym); | |
713 break; | |
714 case ELF64LEKind: | |
715 msg = maybeReportDiscarded<ELF64LE>(sym); | |
716 break; | |
717 case ELF64BEKind: | |
718 msg = maybeReportDiscarded<ELF64BE>(sym); | |
719 break; | |
720 default: | |
721 llvm_unreachable(""); | |
722 } | |
886 if (msg.empty()) | 723 if (msg.empty()) |
887 msg = "undefined " + visibility() + "symbol: " + toString(sym); | 724 msg = "undefined " + visibility() + "symbol: " + toString(sym); |
888 | 725 |
889 const size_t maxUndefReferences = 3; | 726 const size_t maxUndefReferences = 3; |
890 size_t i = 0; | 727 size_t i = 0; |
906 msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times") | 743 msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times") |
907 .str(); | 744 .str(); |
908 | 745 |
909 if (correctSpelling) { | 746 if (correctSpelling) { |
910 std::string pre_hint = ": ", post_hint; | 747 std::string pre_hint = ": ", post_hint; |
911 if (const Symbol *corrected = getAlternativeSpelling<ELFT>( | 748 if (const Symbol *corrected = |
912 cast<Undefined>(sym), pre_hint, post_hint)) { | 749 getAlternativeSpelling(sym, pre_hint, post_hint)) { |
913 msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint; | 750 msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint; |
914 if (corrected->file) | 751 if (corrected->file) |
915 msg += "\n>>> defined in: " + toString(corrected->file); | 752 msg += "\n>>> defined in: " + toString(corrected->file); |
916 } | 753 } |
917 } | 754 } |
918 | 755 |
919 if (sym.getName().startswith("_ZTV")) | 756 if (sym.getName().startswith("_ZTV")) |
920 msg += | 757 msg += |
921 "\n>>> the vtable symbol may be undefined because the class is missing " | 758 "\n>>> the vtable symbol may be undefined because the class is missing " |
922 "its key function (see https://lld.llvm.org/missingkeyfunction)"; | 759 "its key function (see https://lld.llvm.org/missingkeyfunction)"; |
760 if (config->gcSections && config->zStartStopGC && | |
761 sym.getName().startswith("__start_")) { | |
762 msg += "\n>>> the encapsulation symbol needs to be retained under " | |
763 "--gc-sections properly; consider -z nostart-stop-gc " | |
764 "(see https://lld.llvm.org/ELF/start-stop-gc)"; | |
765 } | |
923 | 766 |
924 if (undef.isWarning) | 767 if (undef.isWarning) |
925 warn(msg); | 768 warn(msg); |
926 else | 769 else |
927 error(msg, ErrorTag::SymbolNotFound, {sym.getName()}); | 770 error(msg, ErrorTag::SymbolNotFound, {sym.getName()}); |
928 } | 771 } |
929 | 772 |
930 template <class ELFT> void elf::reportUndefinedSymbols() { | 773 void elf::reportUndefinedSymbols() { |
931 // Find the first "undefined symbol" diagnostic for each diagnostic, and | 774 // Find the first "undefined symbol" diagnostic for each diagnostic, and |
932 // collect all "referenced from" lines at the first diagnostic. | 775 // collect all "referenced from" lines at the first diagnostic. |
933 DenseMap<Symbol *, UndefinedDiag *> firstRef; | 776 DenseMap<Symbol *, UndefinedDiag *> firstRef; |
934 for (UndefinedDiag &undef : undefs) { | 777 for (UndefinedDiag &undef : undefs) { |
935 assert(undef.locs.size() == 1); | 778 assert(undef.locs.size() == 1); |
939 } else | 782 } else |
940 firstRef[undef.sym] = &undef; | 783 firstRef[undef.sym] = &undef; |
941 } | 784 } |
942 | 785 |
943 // Enable spell corrector for the first 2 diagnostics. | 786 // Enable spell corrector for the first 2 diagnostics. |
944 for (auto it : enumerate(undefs)) | 787 for (const auto &[i, undef] : llvm::enumerate(undefs)) |
945 if (!it.value().locs.empty()) | 788 if (!undef.locs.empty()) |
946 reportUndefinedSymbol<ELFT>(it.value(), it.index() < 2); | 789 reportUndefinedSymbol(undef, i < 2); |
947 undefs.clear(); | 790 undefs.clear(); |
948 } | 791 } |
949 | 792 |
950 // Report an undefined symbol if necessary. | 793 // Report an undefined symbol if necessary. |
951 // Returns true if the undefined symbol will produce an error message. | 794 // Returns true if the undefined symbol will produce an error message. |
952 static bool maybeReportUndefined(Symbol &sym, InputSectionBase &sec, | 795 static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec, |
953 uint64_t offset) { | 796 uint64_t offset) { |
954 if (!sym.isUndefined()) | 797 std::lock_guard<std::mutex> lock(relocMutex); |
955 return false; | |
956 // If versioned, issue an error (even if the symbol is weak) because we don't | 798 // If versioned, issue an error (even if the symbol is weak) because we don't |
957 // know the defining filename which is required to construct a Verneed entry. | 799 // know the defining filename which is required to construct a Verneed entry. |
958 if (*sym.getVersionSuffix() == '@') { | 800 if (sym.hasVersionSuffix) { |
959 undefs.push_back({&sym, {{&sec, offset}}, false}); | 801 undefs.push_back({&sym, {{&sec, offset}}, false}); |
960 return true; | 802 return true; |
961 } | 803 } |
962 if (sym.isWeak()) | 804 if (sym.isWeak()) |
963 return false; | 805 return false; |
964 | 806 |
965 bool canBeExternal = !sym.isLocal() && sym.visibility == STV_DEFAULT; | 807 bool canBeExternal = !sym.isLocal() && sym.visibility() == STV_DEFAULT; |
966 if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal) | 808 if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal) |
967 return false; | 809 return false; |
968 | 810 |
969 // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc | 811 // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc |
970 // which references a switch table in a discarded .rodata/.text section. The | 812 // which references a switch table in a discarded .rodata/.text section. The |
973 // allowed. Work around the bug. | 815 // allowed. Work around the bug. |
974 // | 816 // |
975 // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible | 817 // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible |
976 // because .LC0-.LTOC is not representable if the two labels are in different | 818 // because .LC0-.LTOC is not representable if the two labels are in different |
977 // .got2 | 819 // .got2 |
978 if (cast<Undefined>(sym).discardedSecIdx != 0 && | 820 if (sym.discardedSecIdx != 0 && (sec.name == ".got2" || sec.name == ".toc")) |
979 (sec.name == ".got2" || sec.name == ".toc")) | |
980 return false; | 821 return false; |
981 | 822 |
982 bool isWarning = | 823 bool isWarning = |
983 (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) || | 824 (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) || |
984 config->noinhibitExec; | 825 config->noinhibitExec; |
989 // MIPS N32 ABI treats series of successive relocations with the same offset | 830 // MIPS N32 ABI treats series of successive relocations with the same offset |
990 // as a single relocation. The similar approach used by N64 ABI, but this ABI | 831 // as a single relocation. The similar approach used by N64 ABI, but this ABI |
991 // packs all relocations into the single relocation record. Here we emulate | 832 // packs all relocations into the single relocation record. Here we emulate |
992 // this for the N32 ABI. Iterate over relocation with the same offset and put | 833 // this for the N32 ABI. Iterate over relocation with the same offset and put |
993 // theirs types into the single bit-set. | 834 // theirs types into the single bit-set. |
994 template <class RelTy> static RelType getMipsN32RelType(RelTy *&rel, RelTy *end) { | 835 template <class RelTy> |
836 RelType RelocationScanner::getMipsN32RelType(RelTy *&rel) const { | |
995 RelType type = 0; | 837 RelType type = 0; |
996 uint64_t offset = rel->r_offset; | 838 uint64_t offset = rel->r_offset; |
997 | 839 |
998 int n = 0; | 840 int n = 0; |
999 while (rel != end && rel->r_offset == offset) | 841 while (rel != static_cast<const RelTy *>(end) && rel->r_offset == offset) |
1000 type |= (rel++)->getType(config->isMips64EL) << (8 * n++); | 842 type |= (rel++)->getType(config->isMips64EL) << (8 * n++); |
1001 return type; | 843 return type; |
1002 } | 844 } |
1003 | 845 |
1004 // .eh_frame sections are mergeable input sections, so their input | 846 template <bool shard = false> |
1005 // offsets are not linearly mapped to output section. For each input | 847 static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec, |
1006 // offset, we need to find a section piece containing the offset and | |
1007 // add the piece's base address to the input offset to compute the | |
1008 // output offset. That isn't cheap. | |
1009 // | |
1010 // This class is to speed up the offset computation. When we process | |
1011 // relocations, we access offsets in the monotonically increasing | |
1012 // order. So we can optimize for that access pattern. | |
1013 // | |
1014 // For sections other than .eh_frame, this class doesn't do anything. | |
1015 namespace { | |
1016 class OffsetGetter { | |
1017 public: | |
1018 explicit OffsetGetter(InputSectionBase &sec) { | |
1019 if (auto *eh = dyn_cast<EhInputSection>(&sec)) | |
1020 pieces = eh->pieces; | |
1021 } | |
1022 | |
1023 // Translates offsets in input sections to offsets in output sections. | |
1024 // Given offset must increase monotonically. We assume that Piece is | |
1025 // sorted by inputOff. | |
1026 uint64_t get(uint64_t off) { | |
1027 if (pieces.empty()) | |
1028 return off; | |
1029 | |
1030 while (i != pieces.size() && pieces[i].inputOff + pieces[i].size <= off) | |
1031 ++i; | |
1032 if (i == pieces.size()) | |
1033 fatal(".eh_frame: relocation is not in any piece"); | |
1034 | |
1035 // Pieces must be contiguous, so there must be no holes in between. | |
1036 assert(pieces[i].inputOff <= off && "Relocation not in any piece"); | |
1037 | |
1038 // Offset -1 means that the piece is dead (i.e. garbage collected). | |
1039 if (pieces[i].outputOff == -1) | |
1040 return -1; | |
1041 return pieces[i].outputOff + off - pieces[i].inputOff; | |
1042 } | |
1043 | |
1044 private: | |
1045 ArrayRef<EhSectionPiece> pieces; | |
1046 size_t i = 0; | |
1047 }; | |
1048 } // namespace | |
1049 | |
1050 static void addRelativeReloc(InputSectionBase *isec, uint64_t offsetInSec, | |
1051 Symbol &sym, int64_t addend, RelExpr expr, | 848 Symbol &sym, int64_t addend, RelExpr expr, |
1052 RelType type) { | 849 RelType type) { |
1053 Partition &part = isec->getPartition(); | 850 Partition &part = isec.getPartition(); |
1054 | 851 |
1055 // Add a relative relocation. If relrDyn section is enabled, and the | 852 // Add a relative relocation. If relrDyn section is enabled, and the |
1056 // relocation offset is guaranteed to be even, add the relocation to | 853 // relocation offset is guaranteed to be even, add the relocation to |
1057 // the relrDyn section, otherwise add it to the relaDyn section. | 854 // the relrDyn section, otherwise add it to the relaDyn section. |
1058 // relrDyn sections don't support odd offsets. Also, relrDyn sections | 855 // relrDyn sections don't support odd offsets. Also, relrDyn sections |
1059 // don't store the addend values, so we must write it to the relocated | 856 // don't store the addend values, so we must write it to the relocated |
1060 // address. | 857 // address. |
1061 if (part.relrDyn && isec->alignment >= 2 && offsetInSec % 2 == 0) { | 858 if (part.relrDyn && isec.alignment >= 2 && offsetInSec % 2 == 0) { |
1062 isec->relocations.push_back({expr, type, offsetInSec, addend, &sym}); | 859 isec.relocations.push_back({expr, type, offsetInSec, addend, &sym}); |
1063 part.relrDyn->relocs.push_back({isec, offsetInSec}); | 860 if (shard) |
861 part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back( | |
862 {&isec, offsetInSec}); | |
863 else | |
864 part.relrDyn->relocs.push_back({&isec, offsetInSec}); | |
1064 return; | 865 return; |
1065 } | 866 } |
1066 part.relaDyn->addRelativeReloc(target->relativeRel, isec, offsetInSec, sym, | 867 part.relaDyn->addRelativeReloc<shard>(target->relativeRel, isec, offsetInSec, |
1067 addend, type, expr); | 868 sym, addend, type, expr); |
1068 } | 869 } |
1069 | 870 |
1070 template <class PltSection, class GotPltSection> | 871 template <class PltSection, class GotPltSection> |
1071 static void addPltEntry(PltSection *plt, GotPltSection *gotPlt, | 872 static void addPltEntry(PltSection &plt, GotPltSection &gotPlt, |
1072 RelocationBaseSection *rel, RelType type, Symbol &sym) { | 873 RelocationBaseSection &rel, RelType type, Symbol &sym) { |
1073 plt->addEntry(sym); | 874 plt.addEntry(sym); |
1074 gotPlt->addEntry(sym); | 875 gotPlt.addEntry(sym); |
1075 rel->addReloc({type, gotPlt, sym.getGotPltOffset(), | 876 rel.addReloc({type, &gotPlt, sym.getGotPltOffset(), |
1076 sym.isPreemptible ? DynamicReloc::AgainstSymbol | 877 sym.isPreemptible ? DynamicReloc::AgainstSymbol |
1077 : DynamicReloc::AddendOnlyWithTargetVA, | 878 : DynamicReloc::AddendOnlyWithTargetVA, |
1078 sym, 0, R_ABS}); | 879 sym, 0, R_ABS}); |
1079 } | 880 } |
1080 | 881 |
1081 static void addGotEntry(Symbol &sym) { | 882 static void addGotEntry(Symbol &sym) { |
1082 in.got->addEntry(sym); | 883 in.got->addEntry(sym); |
1083 | |
1084 RelExpr expr = sym.isTls() ? R_TPREL : R_ABS; | |
1085 uint64_t off = sym.getGotOffset(); | 884 uint64_t off = sym.getGotOffset(); |
1086 | 885 |
1087 // If a GOT slot value can be calculated at link-time, which is now, | 886 // If preemptible, emit a GLOB_DAT relocation. |
1088 // we can just fill that out. | 887 if (sym.isPreemptible) { |
1089 // | 888 mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off, |
1090 // (We don't actually write a value to a GOT slot right now, but we | 889 DynamicReloc::AgainstSymbol, sym, 0, R_ABS}); |
1091 // add a static relocation to a Relocations vector so that | |
1092 // InputSection::relocate will do the work for us. We may be able | |
1093 // to just write a value now, but it is a TODO.) | |
1094 bool isLinkTimeConstant = | |
1095 !sym.isPreemptible && (!config->isPic || isAbsolute(sym)); | |
1096 if (isLinkTimeConstant) { | |
1097 in.got->relocations.push_back({expr, target->symbolicRel, off, 0, &sym}); | |
1098 return; | 890 return; |
1099 } | 891 } |
1100 | 892 |
1101 // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that | 893 // Otherwise, the value is either a link-time constant or the load base |
1102 // the GOT slot will be fixed at load-time. | 894 // plus a constant. |
1103 if (!sym.isTls() && !sym.isPreemptible && config->isPic) { | 895 if (!config->isPic || isAbsolute(sym)) |
1104 addRelativeReloc(in.got, off, sym, 0, R_ABS, target->symbolicRel); | 896 in.got->relocations.push_back({R_ABS, target->symbolicRel, off, 0, &sym}); |
897 else | |
898 addRelativeReloc(*in.got, off, sym, 0, R_ABS, target->symbolicRel); | |
899 } | |
900 | |
901 static void addTpOffsetGotEntry(Symbol &sym) { | |
902 in.got->addEntry(sym); | |
903 uint64_t off = sym.getGotOffset(); | |
904 if (!sym.isPreemptible && !config->isPic) { | |
905 in.got->relocations.push_back({R_TPREL, target->symbolicRel, off, 0, &sym}); | |
1105 return; | 906 return; |
1106 } | 907 } |
1107 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( | 908 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( |
1108 sym.isTls() ? target->tlsGotRel : target->gotRel, in.got, off, sym, | 909 target->tlsGotRel, *in.got, off, sym, target->symbolicRel); |
1109 target->symbolicRel); | |
1110 } | 910 } |
1111 | 911 |
1112 // Return true if we can define a symbol in the executable that | 912 // Return true if we can define a symbol in the executable that |
1113 // contains the value/function of a symbol defined in a shared | 913 // contains the value/function of a symbol defined in a shared |
1114 // library. | 914 // library. |
1115 static bool canDefineSymbolInExecutable(Symbol &sym) { | 915 static bool canDefineSymbolInExecutable(Symbol &sym) { |
1116 // If the symbol has default visibility the symbol defined in the | 916 // If the symbol has default visibility the symbol defined in the |
1117 // executable will preempt it. | 917 // executable will preempt it. |
1118 // Note that we want the visibility of the shared symbol itself, not | 918 // Note that we want the visibility of the shared symbol itself, not |
1119 // the visibility of the symbol in the output file we are producing. That is | 919 // the visibility of the symbol in the output file we are producing. |
1120 // why we use Sym.stOther. | 920 if (!sym.dsoProtected) |
1121 if ((sym.stOther & 0x3) == STV_DEFAULT) | |
1122 return true; | 921 return true; |
1123 | 922 |
1124 // If we are allowed to break address equality of functions, defining | 923 // If we are allowed to break address equality of functions, defining |
1125 // a plt entry will allow the program to call the function in the | 924 // a plt entry will allow the program to call the function in the |
1126 // .so, but the .so and the executable will no agree on the address | 925 // .so, but the .so and the executable will no agree on the address |
1127 // of the function. Similar logic for objects. | 926 // of the function. Similar logic for objects. |
1128 return ((sym.isFunc() && config->ignoreFunctionAddressEquality) || | 927 return ((sym.isFunc() && config->ignoreFunctionAddressEquality) || |
1129 (sym.isObject() && config->ignoreDataAddressEquality)); | 928 (sym.isObject() && config->ignoreDataAddressEquality)); |
929 } | |
930 | |
931 // Returns true if a given relocation can be computed at link-time. | |
932 // This only handles relocation types expected in processAux. | |
933 // | |
934 // For instance, we know the offset from a relocation to its target at | |
935 // link-time if the relocation is PC-relative and refers a | |
936 // non-interposable function in the same executable. This function | |
937 // will return true for such relocation. | |
938 // | |
939 // If this function returns false, that means we need to emit a | |
940 // dynamic relocation so that the relocation will be fixed at load-time. | |
941 bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type, | |
942 const Symbol &sym, | |
943 uint64_t relOff) const { | |
944 // These expressions always compute a constant | |
945 if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, R_MIPS_GOT_LOCAL_PAGE, | |
946 R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, | |
947 R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC, | |
948 R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT, | |
949 R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE>(e)) | |
950 return true; | |
951 | |
952 // These never do, except if the entire file is position dependent or if | |
953 // only the low bits are used. | |
954 if (e == R_GOT || e == R_PLT) | |
955 return target->usesOnlyLowPageBits(type) || !config->isPic; | |
956 | |
957 if (sym.isPreemptible) | |
958 return false; | |
959 if (!config->isPic) | |
960 return true; | |
961 | |
962 // The size of a non preemptible symbol is a constant. | |
963 if (e == R_SIZE) | |
964 return true; | |
965 | |
966 // For the target and the relocation, we want to know if they are | |
967 // absolute or relative. | |
968 bool absVal = isAbsoluteValue(sym); | |
969 bool relE = isRelExpr(e); | |
970 if (absVal && !relE) | |
971 return true; | |
972 if (!absVal && relE) | |
973 return true; | |
974 if (!absVal && !relE) | |
975 return target->usesOnlyLowPageBits(type); | |
976 | |
977 assert(absVal && relE); | |
978 | |
979 // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol | |
980 // in PIC mode. This is a little strange, but it allows us to link function | |
981 // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers). | |
982 // Normally such a call will be guarded with a comparison, which will load a | |
983 // zero from the GOT. | |
984 if (sym.isUndefWeak()) | |
985 return true; | |
986 | |
987 // We set the final symbols values for linker script defined symbols later. | |
988 // They always can be computed as a link time constant. | |
989 if (sym.scriptDefined) | |
990 return true; | |
991 | |
992 error("relocation " + toString(type) + " cannot refer to absolute symbol: " + | |
993 toString(sym) + getLocation(*sec, sym, relOff)); | |
994 return true; | |
1130 } | 995 } |
1131 | 996 |
1132 // The reason we have to do this early scan is as follows | 997 // The reason we have to do this early scan is as follows |
1133 // * To mmap the output file, we need to know the size | 998 // * To mmap the output file, we need to know the size |
1134 // * For that, we need to know how many dynamic relocs we will have. | 999 // * For that, we need to know how many dynamic relocs we will have. |
1140 // This would have some drawbacks. For example, we would only know if .rela.dyn | 1005 // This would have some drawbacks. For example, we would only know if .rela.dyn |
1141 // is needed after applying relocations. If it is, it will go after rw and rx | 1006 // is needed after applying relocations. If it is, it will go after rw and rx |
1142 // sections. Given that it is ro, we will need an extra PT_LOAD. This | 1007 // sections. Given that it is ro, we will need an extra PT_LOAD. This |
1143 // complicates things for the dynamic linker and means we would have to reserve | 1008 // complicates things for the dynamic linker and means we would have to reserve |
1144 // space for the extra PT_LOAD even if we end up not using it. | 1009 // space for the extra PT_LOAD even if we end up not using it. |
1145 template <class ELFT, class RelTy> | 1010 void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset, |
1146 static void processRelocAux(InputSectionBase &sec, RelExpr expr, RelType type, | 1011 Symbol &sym, int64_t addend) const { |
1147 uint64_t offset, Symbol &sym, const RelTy &rel, | 1012 // If non-ifunc non-preemptible, change PLT to direct call and optimize GOT |
1148 int64_t addend) { | 1013 // indirection. |
1014 const bool isIfunc = sym.isGnuIFunc(); | |
1015 if (!sym.isPreemptible && (!isIfunc || config->zIfuncNoplt)) { | |
1016 if (expr != R_GOT_PC) { | |
1017 // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call | |
1018 // stub type. It should be ignored if optimized to R_PC. | |
1019 if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL) | |
1020 addend &= ~0x8000; | |
1021 // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into | |
1022 // call __tls_get_addr even if the symbol is non-preemptible. | |
1023 if (!(config->emachine == EM_HEXAGON && | |
1024 (type == R_HEX_GD_PLT_B22_PCREL || | |
1025 type == R_HEX_GD_PLT_B22_PCREL_X || | |
1026 type == R_HEX_GD_PLT_B32_PCREL_X))) | |
1027 expr = fromPlt(expr); | |
1028 } else if (!isAbsoluteValue(sym)) { | |
1029 expr = | |
1030 target->adjustGotPcExpr(type, addend, sec->rawData.data() + offset); | |
1031 } | |
1032 } | |
1033 | |
1034 // We were asked not to generate PLT entries for ifuncs. Instead, pass the | |
1035 // direct relocation on through. | |
1036 if (LLVM_UNLIKELY(isIfunc) && config->zIfuncNoplt) { | |
1037 std::lock_guard<std::mutex> lock(relocMutex); | |
1038 sym.exportDynamic = true; | |
1039 mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type); | |
1040 return; | |
1041 } | |
1042 | |
1043 if (needsGot(expr)) { | |
1044 if (config->emachine == EM_MIPS) { | |
1045 // MIPS ABI has special rules to process GOT entries and doesn't | |
1046 // require relocation entries for them. A special case is TLS | |
1047 // relocations. In that case dynamic loader applies dynamic | |
1048 // relocations to initialize TLS GOT entries. | |
1049 // See "Global Offset Table" in Chapter 5 in the following document | |
1050 // for detailed description: | |
1051 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf | |
1052 in.mipsGot->addEntry(*sec->file, sym, addend, expr); | |
1053 } else { | |
1054 sym.setFlags(NEEDS_GOT); | |
1055 } | |
1056 } else if (needsPlt(expr)) { | |
1057 sym.setFlags(NEEDS_PLT); | |
1058 } else if (LLVM_UNLIKELY(isIfunc)) { | |
1059 sym.setFlags(HAS_DIRECT_RELOC); | |
1060 } | |
1061 | |
1149 // If the relocation is known to be a link-time constant, we know no dynamic | 1062 // If the relocation is known to be a link-time constant, we know no dynamic |
1150 // relocation will be created, pass the control to relocateAlloc() or | 1063 // relocation will be created, pass the control to relocateAlloc() or |
1151 // relocateNonAlloc() to resolve it. | 1064 // relocateNonAlloc() to resolve it. |
1152 // | 1065 // |
1153 // The behavior of an undefined weak reference is implementation defined. For | 1066 // The behavior of an undefined weak reference is implementation defined. For |
1158 // The general expectation of -no-pie static linking is that there is no | 1071 // The general expectation of -no-pie static linking is that there is no |
1159 // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for | 1072 // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for |
1160 // -shared matches the spirit of its -z undefs default. -pie has freedom on | 1073 // -shared matches the spirit of its -z undefs default. -pie has freedom on |
1161 // choices, and we choose dynamic relocations to be consistent with the | 1074 // choices, and we choose dynamic relocations to be consistent with the |
1162 // handling of GOT-generating relocations. | 1075 // handling of GOT-generating relocations. |
1163 if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) || | 1076 if (isStaticLinkTimeConstant(expr, type, sym, offset) || |
1164 (!config->isPic && sym.isUndefWeak())) { | 1077 (!config->isPic && sym.isUndefWeak())) { |
1165 sec.relocations.push_back({expr, type, offset, addend, &sym}); | 1078 sec->relocations.push_back({expr, type, offset, addend, &sym}); |
1166 return; | 1079 return; |
1167 } | 1080 } |
1168 | 1081 |
1169 bool canWrite = (sec.flags & SHF_WRITE) || !config->zText; | 1082 bool canWrite = (sec->flags & SHF_WRITE) || !config->zText; |
1170 if (canWrite) { | 1083 if (canWrite) { |
1171 RelType rel = target->getDynRel(type); | 1084 RelType rel = target->getDynRel(type); |
1172 if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) { | 1085 if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) { |
1173 addRelativeReloc(&sec, offset, sym, addend, expr, type); | 1086 addRelativeReloc<true>(*sec, offset, sym, addend, expr, type); |
1174 return; | 1087 return; |
1175 } else if (rel != 0) { | 1088 } else if (rel != 0) { |
1176 if (config->emachine == EM_MIPS && rel == target->symbolicRel) | 1089 if (config->emachine == EM_MIPS && rel == target->symbolicRel) |
1177 rel = target->relativeRel; | 1090 rel = target->relativeRel; |
1178 sec.getPartition().relaDyn->addSymbolReloc(rel, &sec, offset, sym, addend, | 1091 std::lock_guard<std::mutex> lock(relocMutex); |
1179 type); | 1092 sec->getPartition().relaDyn->addSymbolReloc(rel, *sec, offset, sym, |
1093 addend, type); | |
1180 | 1094 |
1181 // MIPS ABI turns using of GOT and dynamic relocations inside out. | 1095 // MIPS ABI turns using of GOT and dynamic relocations inside out. |
1182 // While regular ABI uses dynamic relocations to fill up GOT entries | 1096 // While regular ABI uses dynamic relocations to fill up GOT entries |
1183 // MIPS ABI requires dynamic linker to fills up GOT entries using | 1097 // MIPS ABI requires dynamic linker to fills up GOT entries using |
1184 // specially sorted dynamic symbol table. This affects even dynamic | 1098 // specially sorted dynamic symbol table. This affects even dynamic |
1192 // dynamic linker performs symbol resolution, writes the symbol value | 1106 // dynamic linker performs symbol resolution, writes the symbol value |
1193 // to the GOT entry and reads the GOT entry when it needs to perform | 1107 // to the GOT entry and reads the GOT entry when it needs to perform |
1194 // a dynamic relocation. | 1108 // a dynamic relocation. |
1195 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 | 1109 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 |
1196 if (config->emachine == EM_MIPS) | 1110 if (config->emachine == EM_MIPS) |
1197 in.mipsGot->addEntry(*sec.file, sym, addend, expr); | 1111 in.mipsGot->addEntry(*sec->file, sym, addend, expr); |
1198 return; | 1112 return; |
1199 } | 1113 } |
1200 } | 1114 } |
1201 | 1115 |
1202 // When producing an executable, we can perform copy relocations (for | 1116 // When producing an executable, we can perform copy relocations (for |
1203 // STT_OBJECT) and canonical PLT (for STT_FUNC). | 1117 // STT_OBJECT) and canonical PLT (for STT_FUNC). |
1204 if (!config->shared) { | 1118 if (!config->shared) { |
1205 if (!canDefineSymbolInExecutable(sym)) { | 1119 if (!canDefineSymbolInExecutable(sym)) { |
1206 errorOrWarn("cannot preempt symbol: " + toString(sym) + | 1120 errorOrWarn("cannot preempt symbol: " + toString(sym) + |
1207 getLocation(sec, sym, offset)); | 1121 getLocation(*sec, sym, offset)); |
1208 return; | 1122 return; |
1209 } | 1123 } |
1210 | 1124 |
1211 if (sym.isObject()) { | 1125 if (sym.isObject()) { |
1212 // Produce a copy relocation. | 1126 // Produce a copy relocation. |
1213 if (auto *ss = dyn_cast<SharedSymbol>(&sym)) { | 1127 if (auto *ss = dyn_cast<SharedSymbol>(&sym)) { |
1214 if (!config->zCopyreloc) | 1128 if (!config->zCopyreloc) |
1215 error("unresolvable relocation " + toString(type) + | 1129 error("unresolvable relocation " + toString(type) + |
1216 " against symbol '" + toString(*ss) + | 1130 " against symbol '" + toString(*ss) + |
1217 "'; recompile with -fPIC or remove '-z nocopyreloc'" + | 1131 "'; recompile with -fPIC or remove '-z nocopyreloc'" + |
1218 getLocation(sec, sym, offset)); | 1132 getLocation(*sec, sym, offset)); |
1219 addCopyRelSymbol<ELFT>(*ss); | 1133 sym.setFlags(NEEDS_COPY); |
1220 } | 1134 } |
1221 sec.relocations.push_back({expr, type, offset, addend, &sym}); | 1135 sec->relocations.push_back({expr, type, offset, addend, &sym}); |
1222 return; | 1136 return; |
1223 } | 1137 } |
1224 | 1138 |
1225 // This handles a non PIC program call to function in a shared library. In | 1139 // This handles a non PIC program call to function in a shared library. In |
1226 // an ideal world, we could just report an error saying the relocation can | 1140 // an ideal world, we could just report an error saying the relocation can |
1251 // the wrong ebx value. | 1165 // the wrong ebx value. |
1252 if (sym.isFunc()) { | 1166 if (sym.isFunc()) { |
1253 if (config->pie && config->emachine == EM_386) | 1167 if (config->pie && config->emachine == EM_386) |
1254 errorOrWarn("symbol '" + toString(sym) + | 1168 errorOrWarn("symbol '" + toString(sym) + |
1255 "' cannot be preempted; recompile with -fPIE" + | 1169 "' cannot be preempted; recompile with -fPIE" + |
1256 getLocation(sec, sym, offset)); | 1170 getLocation(*sec, sym, offset)); |
1257 if (!sym.isInPlt()) | 1171 sym.setFlags(NEEDS_COPY | NEEDS_PLT); |
1258 addPltEntry(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym); | 1172 sec->relocations.push_back({expr, type, offset, addend, &sym}); |
1259 if (!sym.isDefined()) { | |
1260 replaceWithDefined( | |
1261 sym, in.plt, | |
1262 target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0); | |
1263 if (config->emachine == EM_PPC) { | |
1264 // PPC32 canonical PLT entries are at the beginning of .glink | |
1265 cast<Defined>(sym).value = in.plt->headerSize; | |
1266 in.plt->headerSize += 16; | |
1267 cast<PPC32GlinkSection>(in.plt)->canonical_plts.push_back(&sym); | |
1268 } | |
1269 } | |
1270 sym.needsPltAddr = true; | |
1271 sec.relocations.push_back({expr, type, offset, addend, &sym}); | |
1272 return; | 1173 return; |
1273 } | 1174 } |
1274 } | 1175 } |
1275 | 1176 |
1276 if (config->isPic) { | 1177 errorOrWarn("relocation " + toString(type) + " cannot be used against " + |
1277 if (!canWrite && !isRelExpr(expr)) | 1178 (sym.getName().empty() ? "local symbol" |
1278 errorOrWarn( | 1179 : "symbol '" + toString(sym) + "'") + |
1279 "can't create dynamic relocation " + toString(type) + " against " + | 1180 "; recompile with -fPIC" + getLocation(*sec, sym, offset)); |
1280 (sym.getName().empty() ? "local symbol" | 1181 } |
1281 : "symbol: " + toString(sym)) + | 1182 |
1282 " in readonly segment; recompile object files with -fPIC " | 1183 // This function is similar to the `handleTlsRelocation`. MIPS does not |
1283 "or pass '-Wl,-z,notext' to allow text relocations in the output" + | 1184 // support any relaxations for TLS relocations so by factoring out MIPS |
1284 getLocation(sec, sym, offset)); | 1185 // handling in to the separate function we can simplify the code and do not |
1285 else | 1186 // pollute other `handleTlsRelocation` by MIPS `ifs` statements. |
1286 errorOrWarn( | 1187 // Mips has a custom MipsGotSection that handles the writing of GOT entries |
1287 "relocation " + toString(type) + " cannot be used against " + | 1188 // without dynamic relocations. |
1288 (sym.getName().empty() ? "local symbol" : "symbol " + toString(sym)) + | 1189 static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym, |
1289 "; recompile with -fPIC" + getLocation(sec, sym, offset)); | 1190 InputSectionBase &c, uint64_t offset, |
1290 return; | 1191 int64_t addend, RelExpr expr) { |
1291 } | 1192 if (expr == R_MIPS_TLSLD) { |
1292 | 1193 in.mipsGot->addTlsIndex(*c.file); |
1293 errorOrWarn("symbol '" + toString(sym) + "' has no type" + | 1194 c.relocations.push_back({expr, type, offset, addend, &sym}); |
1294 getLocation(sec, sym, offset)); | 1195 return 1; |
1295 } | 1196 } |
1296 | 1197 if (expr == R_MIPS_TLSGD) { |
1297 template <class ELFT, class RelTy> | 1198 in.mipsGot->addDynTlsEntry(*c.file, sym); |
1298 static void scanReloc(InputSectionBase &sec, OffsetGetter &getOffset, RelTy *&i, | 1199 c.relocations.push_back({expr, type, offset, addend, &sym}); |
1299 RelTy *start, RelTy *end) { | 1200 return 1; |
1201 } | |
1202 return 0; | |
1203 } | |
1204 | |
1205 // Notes about General Dynamic and Local Dynamic TLS models below. They may | |
1206 // require the generation of a pair of GOT entries that have associated dynamic | |
1207 // relocations. The pair of GOT entries created are of the form GOT[e0] Module | |
1208 // Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of | |
1209 // symbol in TLS block. | |
1210 // | |
1211 // Returns the number of relocations processed. | |
1212 static unsigned handleTlsRelocation(RelType type, Symbol &sym, | |
1213 InputSectionBase &c, uint64_t offset, | |
1214 int64_t addend, RelExpr expr) { | |
1215 if (expr == R_TPREL || expr == R_TPREL_NEG) { | |
1216 if (config->shared) { | |
1217 errorOrWarn("relocation " + toString(type) + " against " + toString(sym) + | |
1218 " cannot be used with -shared" + getLocation(c, sym, offset)); | |
1219 return 1; | |
1220 } | |
1221 return 0; | |
1222 } | |
1223 | |
1224 if (config->emachine == EM_MIPS) | |
1225 return handleMipsTlsRelocation(type, sym, c, offset, addend, expr); | |
1226 | |
1227 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, | |
1228 R_TLSDESC_GOTPLT>(expr) && | |
1229 config->shared) { | |
1230 if (expr != R_TLSDESC_CALL) { | |
1231 sym.setFlags(NEEDS_TLSDESC); | |
1232 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
1233 } | |
1234 return 1; | |
1235 } | |
1236 | |
1237 // ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For | |
1238 // PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable | |
1239 // relaxation as well. | |
1240 bool toExecRelax = !config->shared && config->emachine != EM_ARM && | |
1241 config->emachine != EM_HEXAGON && | |
1242 config->emachine != EM_RISCV && | |
1243 !c.file->ppc64DisableTLSRelax; | |
1244 | |
1245 // If we are producing an executable and the symbol is non-preemptable, it | |
1246 // must be defined and the code sequence can be relaxed to use Local-Exec. | |
1247 // | |
1248 // ARM and RISC-V do not support any relaxations for TLS relocations, however, | |
1249 // we can omit the DTPMOD dynamic relocations and resolve them at link time | |
1250 // because them are always 1. This may be necessary for static linking as | |
1251 // DTPMOD may not be expected at load time. | |
1252 bool isLocalInExecutable = !sym.isPreemptible && !config->shared; | |
1253 | |
1254 // Local Dynamic is for access to module local TLS variables, while still | |
1255 // being suitable for being dynamically loaded via dlopen. GOT[e0] is the | |
1256 // module index, with a special value of 0 for the current module. GOT[e1] is | |
1257 // unused. There only needs to be one module index entry. | |
1258 if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>( | |
1259 expr)) { | |
1260 // Local-Dynamic relocs can be relaxed to Local-Exec. | |
1261 if (toExecRelax) { | |
1262 c.relocations.push_back( | |
1263 {target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, offset, | |
1264 addend, &sym}); | |
1265 return target->getTlsGdRelaxSkip(type); | |
1266 } | |
1267 if (expr == R_TLSLD_HINT) | |
1268 return 1; | |
1269 ctx.needsTlsLd.store(true, std::memory_order_relaxed); | |
1270 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
1271 return 1; | |
1272 } | |
1273 | |
1274 // Local-Dynamic relocs can be relaxed to Local-Exec. | |
1275 if (expr == R_DTPREL) { | |
1276 if (toExecRelax) | |
1277 expr = target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE); | |
1278 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
1279 return 1; | |
1280 } | |
1281 | |
1282 // Local-Dynamic sequence where offset of tls variable relative to dynamic | |
1283 // thread pointer is stored in the got. This cannot be relaxed to Local-Exec. | |
1284 if (expr == R_TLSLD_GOT_OFF) { | |
1285 sym.setFlags(NEEDS_GOT_DTPREL); | |
1286 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
1287 return 1; | |
1288 } | |
1289 | |
1290 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, | |
1291 R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) { | |
1292 if (!toExecRelax) { | |
1293 sym.setFlags(NEEDS_TLSGD); | |
1294 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
1295 return 1; | |
1296 } | |
1297 | |
1298 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec | |
1299 // depending on the symbol being locally defined or not. | |
1300 if (sym.isPreemptible) { | |
1301 sym.setFlags(NEEDS_TLSGD_TO_IE); | |
1302 c.relocations.push_back( | |
1303 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, offset, | |
1304 addend, &sym}); | |
1305 } else { | |
1306 c.relocations.push_back( | |
1307 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, offset, | |
1308 addend, &sym}); | |
1309 } | |
1310 return target->getTlsGdRelaxSkip(type); | |
1311 } | |
1312 | |
1313 if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF, | |
1314 R_TLSIE_HINT>(expr)) { | |
1315 ctx.hasTlsIe.store(true, std::memory_order_relaxed); | |
1316 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally | |
1317 // defined. | |
1318 if (toExecRelax && isLocalInExecutable) { | |
1319 c.relocations.push_back( | |
1320 {R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym}); | |
1321 } else if (expr != R_TLSIE_HINT) { | |
1322 sym.setFlags(NEEDS_TLSIE); | |
1323 // R_GOT needs a relative relocation for PIC on i386 and Hexagon. | |
1324 if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type)) | |
1325 addRelativeReloc<true>(c, offset, sym, addend, expr, type); | |
1326 else | |
1327 c.relocations.push_back({expr, type, offset, addend, &sym}); | |
1328 } | |
1329 return 1; | |
1330 } | |
1331 | |
1332 return 0; | |
1333 } | |
1334 | |
1335 template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) { | |
1300 const RelTy &rel = *i; | 1336 const RelTy &rel = *i; |
1301 uint32_t symIndex = rel.getSymbol(config->isMips64EL); | 1337 uint32_t symIndex = rel.getSymbol(config->isMips64EL); |
1302 Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIndex); | 1338 Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex); |
1303 RelType type; | 1339 RelType type; |
1304 | |
1305 // Deal with MIPS oddity. | |
1306 if (config->mipsN32Abi) { | 1340 if (config->mipsN32Abi) { |
1307 type = getMipsN32RelType(i, end); | 1341 type = getMipsN32RelType(i); |
1308 } else { | 1342 } else { |
1309 type = rel.getType(config->isMips64EL); | 1343 type = rel.getType(config->isMips64EL); |
1310 ++i; | 1344 ++i; |
1311 } | 1345 } |
1312 | |
1313 // Get an offset in an output section this relocation is applied to. | 1346 // Get an offset in an output section this relocation is applied to. |
1314 uint64_t offset = getOffset.get(rel.r_offset); | 1347 uint64_t offset = getter.get(rel.r_offset); |
1315 if (offset == uint64_t(-1)) | 1348 if (offset == uint64_t(-1)) |
1316 return; | 1349 return; |
1317 | 1350 |
1318 // Error if the target symbol is undefined. Symbol index 0 may be used by | 1351 RelExpr expr = target->getRelExpr(type, sym, sec->rawData.data() + offset); |
1319 // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them. | 1352 int64_t addend = |
1320 if (symIndex != 0 && maybeReportUndefined(sym, sec, rel.r_offset)) | 1353 RelTy::IsRela |
1321 return; | 1354 ? getAddend<ELFT>(rel) |
1322 | 1355 : target->getImplicitAddend(sec->rawData.data() + rel.r_offset, type); |
1323 const uint8_t *relocatedAddr = sec.data().begin() + rel.r_offset; | 1356 if (LLVM_UNLIKELY(config->emachine == EM_MIPS)) |
1324 RelExpr expr = target->getRelExpr(type, sym, relocatedAddr); | 1357 addend += computeMipsAddend<ELFT>(rel, expr, sym.isLocal()); |
1358 else if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC) | |
1359 addend += getPPC64TocBase(); | |
1325 | 1360 |
1326 // Ignore R_*_NONE and other marker relocations. | 1361 // Ignore R_*_NONE and other marker relocations. |
1327 if (expr == R_NONE) | 1362 if (expr == R_NONE) |
1328 return; | 1363 return; |
1329 | 1364 |
1330 // Read an addend. | 1365 // Error if the target symbol is undefined. Symbol index 0 may be used by |
1331 int64_t addend = computeAddend<ELFT>(rel, end, sec, expr, sym.isLocal()); | 1366 // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them. |
1367 if (sym.isUndefined() && symIndex != 0 && | |
1368 maybeReportUndefined(cast<Undefined>(sym), *sec, offset)) | |
1369 return; | |
1332 | 1370 |
1333 if (config->emachine == EM_PPC64) { | 1371 if (config->emachine == EM_PPC64) { |
1334 // We can separate the small code model relocations into 2 categories: | 1372 // We can separate the small code model relocations into 2 categories: |
1335 // 1) Those that access the compiler generated .toc sections. | 1373 // 1) Those that access the compiler generated .toc sections. |
1336 // 2) Those that access the linker allocated got entries. | 1374 // 2) Those that access the linker allocated got entries. |
1337 // lld allocates got entries to symbols on demand. Since we don't try to | 1375 // lld allocates got entries to symbols on demand. Since we don't try to |
1338 // sort the got entries in any way, we don't have to track which objects | 1376 // sort the got entries in any way, we don't have to track which objects |
1339 // have got-based small code model relocs. The .toc sections get placed | 1377 // have got-based small code model relocs. The .toc sections get placed |
1340 // after the end of the linker allocated .got section and we do sort those | 1378 // after the end of the linker allocated .got section and we do sort those |
1341 // so sections addressed with small code model relocations come first. | 1379 // so sections addressed with small code model relocations come first. |
1342 if (isPPC64SmallCodeModelTocReloc(type)) | 1380 if (type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS) |
1343 sec.file->ppc64SmallCodeModelTocRelocs = true; | 1381 sec->file->ppc64SmallCodeModelTocRelocs = true; |
1344 | 1382 |
1345 // Record the TOC entry (.toc + addend) as not relaxable. See the comment in | 1383 // Record the TOC entry (.toc + addend) as not relaxable. See the comment in |
1346 // InputSectionBase::relocateAlloc(). | 1384 // InputSectionBase::relocateAlloc(). |
1347 if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) && | 1385 if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) && |
1348 cast<Defined>(sym).section->name == ".toc") | 1386 cast<Defined>(sym).section->name == ".toc") |
1351 if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) || | 1389 if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) || |
1352 (type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) { | 1390 (type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) { |
1353 if (i == end) { | 1391 if (i == end) { |
1354 errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last " | 1392 errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last " |
1355 "relocation" + | 1393 "relocation" + |
1356 getLocation(sec, sym, offset)); | 1394 getLocation(*sec, sym, offset)); |
1357 return; | 1395 return; |
1358 } | 1396 } |
1359 | 1397 |
1360 // Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case, | 1398 // Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case, |
1361 // so we can discern it later from the toc-case. | 1399 // so we can discern it later from the toc-case. |
1362 if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC) | 1400 if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC) |
1363 ++offset; | 1401 ++offset; |
1364 } | 1402 } |
1365 } | 1403 } |
1366 | 1404 |
1367 // Relax relocations. | |
1368 // | |
1369 // If we know that a PLT entry will be resolved within the same ELF module, we | |
1370 // can skip PLT access and directly jump to the destination function. For | |
1371 // example, if we are linking a main executable, all dynamic symbols that can | |
1372 // be resolved within the executable will actually be resolved that way at | |
1373 // runtime, because the main executable is always at the beginning of a search | |
1374 // list. We can leverage that fact. | |
1375 if (!sym.isPreemptible && (!sym.isGnuIFunc() || config->zIfuncNoplt)) { | |
1376 if (expr != R_GOT_PC) { | |
1377 // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call | |
1378 // stub type. It should be ignored if optimized to R_PC. | |
1379 if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL) | |
1380 addend &= ~0x8000; | |
1381 // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into | |
1382 // call __tls_get_addr even if the symbol is non-preemptible. | |
1383 if (!(config->emachine == EM_HEXAGON && | |
1384 (type == R_HEX_GD_PLT_B22_PCREL || | |
1385 type == R_HEX_GD_PLT_B22_PCREL_X || | |
1386 type == R_HEX_GD_PLT_B32_PCREL_X))) | |
1387 expr = fromPlt(expr); | |
1388 } else if (!isAbsoluteValue(sym)) { | |
1389 expr = target->adjustGotPcExpr(type, addend, relocatedAddr); | |
1390 } | |
1391 } | |
1392 | |
1393 // If the relocation does not emit a GOT or GOTPLT entry but its computation | 1405 // If the relocation does not emit a GOT or GOTPLT entry but its computation |
1394 // uses their addresses, we need GOT or GOTPLT to be created. | 1406 // uses their addresses, we need GOT or GOTPLT to be created. |
1395 // | 1407 // |
1396 // The 4 types that relative GOTPLT are all x86 and x86-64 specific. | 1408 // The 5 types that relative GOTPLT are all x86 and x86-64 specific. |
1397 if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(expr)) { | 1409 if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT, |
1398 in.gotPlt->hasGotPltOffRel = true; | 1410 R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) { |
1399 } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC64_TOCBASE, R_PPC64_RELAX_TOC>( | 1411 in.gotPlt->hasGotPltOffRel.store(true, std::memory_order_relaxed); |
1400 expr)) { | 1412 } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE, |
1401 in.got->hasGotOffRel = true; | 1413 R_PPC64_RELAX_TOC>(expr)) { |
1414 in.got->hasGotOffRel.store(true, std::memory_order_relaxed); | |
1402 } | 1415 } |
1403 | 1416 |
1404 // Process TLS relocations, including relaxing TLS relocations. Note that | 1417 // Process TLS relocations, including relaxing TLS relocations. Note that |
1405 // R_TPREL and R_TPREL_NEG relocations are resolved in processRelocAux. | 1418 // R_TPREL and R_TPREL_NEG relocations are resolved in processAux. |
1406 if (expr == R_TPREL || expr == R_TPREL_NEG) { | 1419 if (sym.isTls()) { |
1407 if (config->shared) { | 1420 if (unsigned processed = |
1408 errorOrWarn("relocation " + toString(type) + " against " + toString(sym) + | 1421 handleTlsRelocation(type, sym, *sec, offset, addend, expr)) { |
1409 " cannot be used with -shared" + | 1422 i += processed - 1; |
1410 getLocation(sec, sym, offset)); | |
1411 return; | 1423 return; |
1412 } | 1424 } |
1413 } else if (unsigned processed = handleTlsRelocation<ELFT>( | 1425 } |
1414 type, sym, sec, offset, addend, expr)) { | 1426 |
1415 i += (processed - 1); | 1427 processAux(expr, type, offset, sym, addend); |
1416 return; | |
1417 } | |
1418 | |
1419 // We were asked not to generate PLT entries for ifuncs. Instead, pass the | |
1420 // direct relocation on through. | |
1421 if (sym.isGnuIFunc() && config->zIfuncNoplt) { | |
1422 sym.exportDynamic = true; | |
1423 mainPart->relaDyn->addSymbolReloc(type, &sec, offset, sym, addend, type); | |
1424 return; | |
1425 } | |
1426 | |
1427 // Non-preemptible ifuncs require special handling. First, handle the usual | |
1428 // case where the symbol isn't one of these. | |
1429 if (!sym.isGnuIFunc() || sym.isPreemptible) { | |
1430 // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol. | |
1431 if (needsPlt(expr) && !sym.isInPlt()) | |
1432 addPltEntry(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym); | |
1433 | |
1434 // Create a GOT slot if a relocation needs GOT. | |
1435 if (needsGot(expr)) { | |
1436 if (config->emachine == EM_MIPS) { | |
1437 // MIPS ABI has special rules to process GOT entries and doesn't | |
1438 // require relocation entries for them. A special case is TLS | |
1439 // relocations. In that case dynamic loader applies dynamic | |
1440 // relocations to initialize TLS GOT entries. | |
1441 // See "Global Offset Table" in Chapter 5 in the following document | |
1442 // for detailed description: | |
1443 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf | |
1444 in.mipsGot->addEntry(*sec.file, sym, addend, expr); | |
1445 } else if (!sym.isInGot()) { | |
1446 addGotEntry(sym); | |
1447 } | |
1448 } | |
1449 } else { | |
1450 // Handle a reference to a non-preemptible ifunc. These are special in a | |
1451 // few ways: | |
1452 // | |
1453 // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have | |
1454 // a fixed value. But assuming that all references to the ifunc are | |
1455 // GOT-generating or PLT-generating, the handling of an ifunc is | |
1456 // relatively straightforward. We create a PLT entry in Iplt, which is | |
1457 // usually at the end of .plt, which makes an indirect call using a | |
1458 // matching GOT entry in igotPlt, which is usually at the end of .got.plt. | |
1459 // The GOT entry is relocated using an IRELATIVE relocation in relaIplt, | |
1460 // which is usually at the end of .rela.plt. Unlike most relocations in | |
1461 // .rela.plt, which may be evaluated lazily without -z now, dynamic | |
1462 // loaders evaluate IRELATIVE relocs eagerly, which means that for | |
1463 // IRELATIVE relocs only, GOT-generating relocations can point directly to | |
1464 // .got.plt without requiring a separate GOT entry. | |
1465 // | |
1466 // - Despite the fact that an ifunc does not have a fixed value, compilers | |
1467 // that are not passed -fPIC will assume that they do, and will emit | |
1468 // direct (non-GOT-generating, non-PLT-generating) relocations to the | |
1469 // symbol. This means that if a direct relocation to the symbol is | |
1470 // seen, the linker must set a value for the symbol, and this value must | |
1471 // be consistent no matter what type of reference is made to the symbol. | |
1472 // This can be done by creating a PLT entry for the symbol in the way | |
1473 // described above and making it canonical, that is, making all references | |
1474 // point to the PLT entry instead of the resolver. In lld we also store | |
1475 // the address of the PLT entry in the dynamic symbol table, which means | |
1476 // that the symbol will also have the same value in other modules. | |
1477 // Because the value loaded from the GOT needs to be consistent with | |
1478 // the value computed using a direct relocation, a non-preemptible ifunc | |
1479 // may end up with two GOT entries, one in .got.plt that points to the | |
1480 // address returned by the resolver and is used only by the PLT entry, | |
1481 // and another in .got that points to the PLT entry and is used by | |
1482 // GOT-generating relocations. | |
1483 // | |
1484 // - The fact that these symbols do not have a fixed value makes them an | |
1485 // exception to the general rule that a statically linked executable does | |
1486 // not require any form of dynamic relocation. To handle these relocations | |
1487 // correctly, the IRELATIVE relocations are stored in an array which a | |
1488 // statically linked executable's startup code must enumerate using the | |
1489 // linker-defined symbols __rela?_iplt_{start,end}. | |
1490 if (!sym.isInPlt()) { | |
1491 // Create PLT and GOTPLT slots for the symbol. | |
1492 sym.isInIplt = true; | |
1493 | |
1494 // Create a copy of the symbol to use as the target of the IRELATIVE | |
1495 // relocation in the igotPlt. This is in case we make the PLT canonical | |
1496 // later, which would overwrite the original symbol. | |
1497 // | |
1498 // FIXME: Creating a copy of the symbol here is a bit of a hack. All | |
1499 // that's really needed to create the IRELATIVE is the section and value, | |
1500 // so ideally we should just need to copy those. | |
1501 auto *directSym = make<Defined>(cast<Defined>(sym)); | |
1502 addPltEntry(in.iplt, in.igotPlt, in.relaIplt, target->iRelativeRel, | |
1503 *directSym); | |
1504 sym.pltIndex = directSym->pltIndex; | |
1505 } | |
1506 if (needsGot(expr)) { | |
1507 // Redirect GOT accesses to point to the Igot. | |
1508 // | |
1509 // This field is also used to keep track of whether we ever needed a GOT | |
1510 // entry. If we did and we make the PLT canonical later, we'll need to | |
1511 // create a GOT entry pointing to the PLT entry for Sym. | |
1512 sym.gotInIgot = true; | |
1513 } else if (!needsPlt(expr)) { | |
1514 // Make the ifunc's PLT entry canonical by changing the value of its | |
1515 // symbol to redirect all references to point to it. | |
1516 auto &d = cast<Defined>(sym); | |
1517 d.section = in.iplt; | |
1518 d.value = sym.pltIndex * target->ipltEntrySize; | |
1519 d.size = 0; | |
1520 // It's important to set the symbol type here so that dynamic loaders | |
1521 // don't try to call the PLT as if it were an ifunc resolver. | |
1522 d.type = STT_FUNC; | |
1523 | |
1524 if (sym.gotInIgot) { | |
1525 // We previously encountered a GOT generating reference that we | |
1526 // redirected to the Igot. Now that the PLT entry is canonical we must | |
1527 // clear the redirection to the Igot and add a GOT entry. As we've | |
1528 // changed the symbol type to STT_FUNC future GOT generating references | |
1529 // will naturally use this GOT entry. | |
1530 // | |
1531 // We don't need to worry about creating a MIPS GOT here because ifuncs | |
1532 // aren't a thing on MIPS. | |
1533 sym.gotInIgot = false; | |
1534 addGotEntry(sym); | |
1535 } | |
1536 } | |
1537 } | |
1538 | |
1539 processRelocAux<ELFT>(sec, expr, type, offset, sym, rel, addend); | |
1540 } | 1428 } |
1541 | 1429 |
1542 // R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for | 1430 // R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for |
1543 // General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is | 1431 // General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is |
1544 // found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the | 1432 // found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the |
1575 "R_PPC64_TLSGD/R_PPC64_TLSLD relocations"); | 1463 "R_PPC64_TLSGD/R_PPC64_TLSLD relocations"); |
1576 } | 1464 } |
1577 } | 1465 } |
1578 | 1466 |
1579 template <class ELFT, class RelTy> | 1467 template <class ELFT, class RelTy> |
1580 static void scanRelocs(InputSectionBase &sec, ArrayRef<RelTy> rels) { | 1468 void RelocationScanner::scan(ArrayRef<RelTy> rels) { |
1581 OffsetGetter getOffset(sec); | 1469 // Not all relocations end up in Sec->Relocations, but a lot do. |
1582 | 1470 sec->relocations.reserve(rels.size()); |
1583 // Not all relocations end up in Sec.Relocations, but a lot do. | |
1584 sec.relocations.reserve(rels.size()); | |
1585 | 1471 |
1586 if (config->emachine == EM_PPC64) | 1472 if (config->emachine == EM_PPC64) |
1587 checkPPC64TLSRelax<RelTy>(sec, rels); | 1473 checkPPC64TLSRelax<RelTy>(*sec, rels); |
1588 | 1474 |
1589 // For EhInputSection, OffsetGetter expects the relocations to be sorted by | 1475 // For EhInputSection, OffsetGetter expects the relocations to be sorted by |
1590 // r_offset. In rare cases (.eh_frame pieces are reordered by a linker | 1476 // r_offset. In rare cases (.eh_frame pieces are reordered by a linker |
1591 // script), the relocations may be unordered. | 1477 // script), the relocations may be unordered. |
1592 SmallVector<RelTy, 0> storage; | 1478 SmallVector<RelTy, 0> storage; |
1593 if (isa<EhInputSection>(sec)) | 1479 if (isa<EhInputSection>(sec)) |
1594 rels = sortRels(rels, storage); | 1480 rels = sortRels(rels, storage); |
1595 | 1481 |
1596 for (auto i = rels.begin(), end = rels.end(); i != end;) | 1482 end = static_cast<const void *>(rels.end()); |
1597 scanReloc<ELFT>(sec, getOffset, i, rels.begin(), end); | 1483 for (auto i = rels.begin(); i != end;) |
1484 scanOne<ELFT>(i); | |
1598 | 1485 |
1599 // Sort relocations by offset for more efficient searching for | 1486 // Sort relocations by offset for more efficient searching for |
1600 // R_RISCV_PCREL_HI20 and R_PPC64_ADDR64. | 1487 // R_RISCV_PCREL_HI20 and R_PPC64_ADDR64. |
1601 if (config->emachine == EM_RISCV || | 1488 if (config->emachine == EM_RISCV || |
1602 (config->emachine == EM_PPC64 && sec.name == ".toc")) | 1489 (config->emachine == EM_PPC64 && sec->name == ".toc")) |
1603 llvm::stable_sort(sec.relocations, | 1490 llvm::stable_sort(sec->relocations, |
1604 [](const Relocation &lhs, const Relocation &rhs) { | 1491 [](const Relocation &lhs, const Relocation &rhs) { |
1605 return lhs.offset < rhs.offset; | 1492 return lhs.offset < rhs.offset; |
1606 }); | 1493 }); |
1607 } | 1494 } |
1608 | 1495 |
1609 template <class ELFT> void elf::scanRelocations(InputSectionBase &s) { | 1496 template <class ELFT> void RelocationScanner::scanSection(InputSectionBase &s) { |
1610 if (s.areRelocsRela) | 1497 sec = &s; |
1611 scanRelocs<ELFT>(s, s.relas<ELFT>()); | 1498 getter = OffsetGetter(s); |
1499 const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>(); | |
1500 if (rels.areRelocsRel()) | |
1501 scan<ELFT>(rels.rels); | |
1612 else | 1502 else |
1613 scanRelocs<ELFT>(s, s.rels<ELFT>()); | 1503 scan<ELFT>(rels.relas); |
1504 } | |
1505 | |
1506 template <class ELFT> void elf::scanRelocations() { | |
1507 // Scan all relocations. Each relocation goes through a series of tests to | |
1508 // determine if it needs special treatment, such as creating GOT, PLT, | |
1509 // copy relocations, etc. Note that relocations for non-alloc sections are | |
1510 // directly processed by InputSection::relocateNonAlloc. | |
1511 | |
1512 // Deterministic parallellism needs sorting relocations which is unsuitable | |
1513 // for -z nocombreloc. MIPS and PPC64 use global states which are not suitable | |
1514 // for parallelism. | |
1515 bool serial = !config->zCombreloc || config->emachine == EM_MIPS || | |
1516 config->emachine == EM_PPC64; | |
1517 parallel::TaskGroup tg; | |
1518 for (ELFFileBase *f : ctx.objectFiles) { | |
1519 auto fn = [f]() { | |
1520 RelocationScanner scanner; | |
1521 for (InputSectionBase *s : f->getSections()) { | |
1522 if (s && s->kind() == SectionBase::Regular && s->isLive() && | |
1523 (s->flags & SHF_ALLOC) && | |
1524 !(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM)) | |
1525 scanner.template scanSection<ELFT>(*s); | |
1526 } | |
1527 }; | |
1528 if (serial) | |
1529 fn(); | |
1530 else | |
1531 tg.execute(fn); | |
1532 } | |
1533 | |
1534 // Both the main thread and thread pool index 0 use getThreadIndex()==0. Be | |
1535 // careful that they don't concurrently run scanSections. When serial is | |
1536 // true, fn() has finished at this point, so running execute is safe. | |
1537 tg.execute([] { | |
1538 RelocationScanner scanner; | |
1539 for (Partition &part : partitions) { | |
1540 for (EhInputSection *sec : part.ehFrame->sections) | |
1541 scanner.template scanSection<ELFT>(*sec); | |
1542 if (part.armExidx && part.armExidx->isLive()) | |
1543 for (InputSection *sec : part.armExidx->exidxSections) | |
1544 scanner.template scanSection<ELFT>(*sec); | |
1545 } | |
1546 }); | |
1547 } | |
1548 | |
1549 static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) { | |
1550 // Handle a reference to a non-preemptible ifunc. These are special in a | |
1551 // few ways: | |
1552 // | |
1553 // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have | |
1554 // a fixed value. But assuming that all references to the ifunc are | |
1555 // GOT-generating or PLT-generating, the handling of an ifunc is | |
1556 // relatively straightforward. We create a PLT entry in Iplt, which is | |
1557 // usually at the end of .plt, which makes an indirect call using a | |
1558 // matching GOT entry in igotPlt, which is usually at the end of .got.plt. | |
1559 // The GOT entry is relocated using an IRELATIVE relocation in relaIplt, | |
1560 // which is usually at the end of .rela.plt. Unlike most relocations in | |
1561 // .rela.plt, which may be evaluated lazily without -z now, dynamic | |
1562 // loaders evaluate IRELATIVE relocs eagerly, which means that for | |
1563 // IRELATIVE relocs only, GOT-generating relocations can point directly to | |
1564 // .got.plt without requiring a separate GOT entry. | |
1565 // | |
1566 // - Despite the fact that an ifunc does not have a fixed value, compilers | |
1567 // that are not passed -fPIC will assume that they do, and will emit | |
1568 // direct (non-GOT-generating, non-PLT-generating) relocations to the | |
1569 // symbol. This means that if a direct relocation to the symbol is | |
1570 // seen, the linker must set a value for the symbol, and this value must | |
1571 // be consistent no matter what type of reference is made to the symbol. | |
1572 // This can be done by creating a PLT entry for the symbol in the way | |
1573 // described above and making it canonical, that is, making all references | |
1574 // point to the PLT entry instead of the resolver. In lld we also store | |
1575 // the address of the PLT entry in the dynamic symbol table, which means | |
1576 // that the symbol will also have the same value in other modules. | |
1577 // Because the value loaded from the GOT needs to be consistent with | |
1578 // the value computed using a direct relocation, a non-preemptible ifunc | |
1579 // may end up with two GOT entries, one in .got.plt that points to the | |
1580 // address returned by the resolver and is used only by the PLT entry, | |
1581 // and another in .got that points to the PLT entry and is used by | |
1582 // GOT-generating relocations. | |
1583 // | |
1584 // - The fact that these symbols do not have a fixed value makes them an | |
1585 // exception to the general rule that a statically linked executable does | |
1586 // not require any form of dynamic relocation. To handle these relocations | |
1587 // correctly, the IRELATIVE relocations are stored in an array which a | |
1588 // statically linked executable's startup code must enumerate using the | |
1589 // linker-defined symbols __rela?_iplt_{start,end}. | |
1590 if (!sym.isGnuIFunc() || sym.isPreemptible || config->zIfuncNoplt) | |
1591 return false; | |
1592 // Skip unreferenced non-preemptible ifunc. | |
1593 if (!(flags & (NEEDS_GOT | NEEDS_PLT | HAS_DIRECT_RELOC))) | |
1594 return true; | |
1595 | |
1596 sym.isInIplt = true; | |
1597 | |
1598 // Create an Iplt and the associated IRELATIVE relocation pointing to the | |
1599 // original section/value pairs. For non-GOT non-PLT relocation case below, we | |
1600 // may alter section/value, so create a copy of the symbol to make | |
1601 // section/value fixed. | |
1602 auto *directSym = makeDefined(cast<Defined>(sym)); | |
1603 directSym->allocateAux(); | |
1604 addPltEntry(*in.iplt, *in.igotPlt, *in.relaIplt, target->iRelativeRel, | |
1605 *directSym); | |
1606 sym.allocateAux(); | |
1607 symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx; | |
1608 | |
1609 if (flags & HAS_DIRECT_RELOC) { | |
1610 // Change the value to the IPLT and redirect all references to it. | |
1611 auto &d = cast<Defined>(sym); | |
1612 d.section = in.iplt.get(); | |
1613 d.value = d.getPltIdx() * target->ipltEntrySize; | |
1614 d.size = 0; | |
1615 // It's important to set the symbol type here so that dynamic loaders | |
1616 // don't try to call the PLT as if it were an ifunc resolver. | |
1617 d.type = STT_FUNC; | |
1618 | |
1619 if (flags & NEEDS_GOT) | |
1620 addGotEntry(sym); | |
1621 } else if (flags & NEEDS_GOT) { | |
1622 // Redirect GOT accesses to point to the Igot. | |
1623 sym.gotInIgot = true; | |
1624 } | |
1625 return true; | |
1626 } | |
1627 | |
1628 void elf::postScanRelocations() { | |
1629 auto fn = [](Symbol &sym) { | |
1630 auto flags = sym.flags.load(std::memory_order_relaxed); | |
1631 if (handleNonPreemptibleIfunc(sym, flags)) | |
1632 return; | |
1633 if (!sym.needsDynReloc()) | |
1634 return; | |
1635 sym.allocateAux(); | |
1636 | |
1637 if (flags & NEEDS_GOT) | |
1638 addGotEntry(sym); | |
1639 if (flags & NEEDS_PLT) | |
1640 addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym); | |
1641 if (flags & NEEDS_COPY) { | |
1642 if (sym.isObject()) { | |
1643 invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym)); | |
1644 // NEEDS_COPY is cleared for sym and its aliases so that in | |
1645 // later iterations aliases won't cause redundant copies. | |
1646 assert(!sym.hasFlag(NEEDS_COPY)); | |
1647 } else { | |
1648 assert(sym.isFunc() && sym.hasFlag(NEEDS_PLT)); | |
1649 if (!sym.isDefined()) { | |
1650 replaceWithDefined(sym, *in.plt, | |
1651 target->pltHeaderSize + | |
1652 target->pltEntrySize * sym.getPltIdx(), | |
1653 0); | |
1654 sym.setFlags(NEEDS_COPY); | |
1655 if (config->emachine == EM_PPC) { | |
1656 // PPC32 canonical PLT entries are at the beginning of .glink | |
1657 cast<Defined>(sym).value = in.plt->headerSize; | |
1658 in.plt->headerSize += 16; | |
1659 cast<PPC32GlinkSection>(*in.plt).canonical_plts.push_back(&sym); | |
1660 } | |
1661 } | |
1662 } | |
1663 } | |
1664 | |
1665 if (!sym.isTls()) | |
1666 return; | |
1667 bool isLocalInExecutable = !sym.isPreemptible && !config->shared; | |
1668 | |
1669 if (flags & NEEDS_TLSDESC) { | |
1670 in.got->addTlsDescEntry(sym); | |
1671 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( | |
1672 target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym, | |
1673 target->tlsDescRel); | |
1674 } | |
1675 if (flags & NEEDS_TLSGD) { | |
1676 in.got->addDynTlsEntry(sym); | |
1677 uint64_t off = in.got->getGlobalDynOffset(sym); | |
1678 if (isLocalInExecutable) | |
1679 // Write one to the GOT slot. | |
1680 in.got->relocations.push_back( | |
1681 {R_ADDEND, target->symbolicRel, off, 1, &sym}); | |
1682 else | |
1683 mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *in.got, | |
1684 off, sym); | |
1685 | |
1686 // If the symbol is preemptible we need the dynamic linker to write | |
1687 // the offset too. | |
1688 uint64_t offsetOff = off + config->wordsize; | |
1689 if (sym.isPreemptible) | |
1690 mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *in.got, | |
1691 offsetOff, sym); | |
1692 else | |
1693 in.got->relocations.push_back( | |
1694 {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym}); | |
1695 } | |
1696 if (flags & NEEDS_TLSGD_TO_IE) { | |
1697 in.got->addEntry(sym); | |
1698 mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *in.got, | |
1699 sym.getGotOffset(), sym); | |
1700 } | |
1701 if (flags & NEEDS_GOT_DTPREL) { | |
1702 in.got->addEntry(sym); | |
1703 in.got->relocations.push_back( | |
1704 {R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym}); | |
1705 } | |
1706 | |
1707 if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE)) | |
1708 addTpOffsetGotEntry(sym); | |
1709 }; | |
1710 | |
1711 if (ctx.needsTlsLd.load(std::memory_order_relaxed) && in.got->addTlsIndex()) { | |
1712 static Undefined dummy(nullptr, "", STB_LOCAL, 0, 0); | |
1713 if (config->shared) | |
1714 mainPart->relaDyn->addReloc( | |
1715 {target->tlsModuleIndexRel, in.got.get(), in.got->getTlsIndexOff()}); | |
1716 else | |
1717 in.got->relocations.push_back( | |
1718 {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &dummy}); | |
1719 } | |
1720 | |
1721 assert(symAux.size() == 1); | |
1722 for (Symbol *sym : symtab.getSymbols()) | |
1723 fn(*sym); | |
1724 | |
1725 // Local symbols may need the aforementioned non-preemptible ifunc and GOT | |
1726 // handling. They don't need regular PLT. | |
1727 for (ELFFileBase *file : ctx.objectFiles) | |
1728 for (Symbol *sym : file->getLocalSymbols()) | |
1729 fn(*sym); | |
1614 } | 1730 } |
1615 | 1731 |
1616 static bool mergeCmp(const InputSection *a, const InputSection *b) { | 1732 static bool mergeCmp(const InputSection *a, const InputSection *b) { |
1617 // std::merge requires a strict weak ordering. | 1733 // std::merge requires a strict weak ordering. |
1618 if (a->outSecOff < b->outSecOff) | 1734 if (a->outSecOff < b->outSecOff) |
1619 return true; | 1735 return true; |
1620 | 1736 |
1621 if (a->outSecOff == b->outSecOff) { | 1737 // FIXME dyn_cast<ThunkSection> is non-null for any SyntheticSection. |
1738 if (a->outSecOff == b->outSecOff && a != b) { | |
1622 auto *ta = dyn_cast<ThunkSection>(a); | 1739 auto *ta = dyn_cast<ThunkSection>(a); |
1623 auto *tb = dyn_cast<ThunkSection>(b); | 1740 auto *tb = dyn_cast<ThunkSection>(b); |
1624 | 1741 |
1625 // Check if Thunk is immediately before any specific Target | 1742 // Check if Thunk is immediately before any specific Target |
1626 // InputSection for example Mips LA25 Thunks. | 1743 // InputSection for example Mips LA25 Thunks. |
1642 ArrayRef<OutputSection *> outputSections, | 1759 ArrayRef<OutputSection *> outputSections, |
1643 llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) { | 1760 llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) { |
1644 for (OutputSection *os : outputSections) { | 1761 for (OutputSection *os : outputSections) { |
1645 if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR)) | 1762 if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR)) |
1646 continue; | 1763 continue; |
1647 for (BaseCommand *bc : os->sectionCommands) | 1764 for (SectionCommand *bc : os->commands) |
1648 if (auto *isd = dyn_cast<InputSectionDescription>(bc)) | 1765 if (auto *isd = dyn_cast<InputSectionDescription>(bc)) |
1649 fn(os, isd); | 1766 fn(os, isd); |
1650 } | 1767 } |
1651 } | 1768 } |
1652 | 1769 |
1764 [](const ThunkSection *a, const ThunkSection *b) { | 1881 [](const ThunkSection *a, const ThunkSection *b) { |
1765 return a->outSecOff < b->outSecOff; | 1882 return a->outSecOff < b->outSecOff; |
1766 }); | 1883 }); |
1767 | 1884 |
1768 // Merge sorted vectors of Thunks and InputSections by outSecOff | 1885 // Merge sorted vectors of Thunks and InputSections by outSecOff |
1769 std::vector<InputSection *> tmp; | 1886 SmallVector<InputSection *, 0> tmp; |
1770 tmp.reserve(isd->sections.size() + newThunks.size()); | 1887 tmp.reserve(isd->sections.size() + newThunks.size()); |
1771 | 1888 |
1772 std::merge(isd->sections.begin(), isd->sections.end(), | 1889 std::merge(isd->sections.begin(), isd->sections.end(), |
1773 newThunks.begin(), newThunks.end(), std::back_inserter(tmp), | 1890 newThunks.begin(), newThunks.end(), std::back_inserter(tmp), |
1774 mergeCmp); | 1891 mergeCmp); |
1775 | 1892 |
1776 isd->sections = std::move(tmp); | 1893 isd->sections = std::move(tmp); |
1777 }); | 1894 }); |
1895 } | |
1896 | |
1897 static int64_t getPCBias(RelType type) { | |
1898 if (config->emachine != EM_ARM) | |
1899 return 0; | |
1900 switch (type) { | |
1901 case R_ARM_THM_JUMP19: | |
1902 case R_ARM_THM_JUMP24: | |
1903 case R_ARM_THM_CALL: | |
1904 return 4; | |
1905 default: | |
1906 return 8; | |
1907 } | |
1778 } | 1908 } |
1779 | 1909 |
1780 // Find or create a ThunkSection within the InputSectionDescription (ISD) that | 1910 // Find or create a ThunkSection within the InputSectionDescription (ISD) that |
1781 // is in range of Src. An ISD maps to a range of InputSections described by a | 1911 // is in range of Src. An ISD maps to a range of InputSections described by a |
1782 // linker script section pattern such as { .text .text.* }. | 1912 // linker script section pattern such as { .text .text.* }. |
1783 ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os, | 1913 ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os, |
1784 InputSection *isec, | 1914 InputSection *isec, |
1785 InputSectionDescription *isd, | 1915 InputSectionDescription *isd, |
1786 const Relocation &rel, | 1916 const Relocation &rel, |
1787 uint64_t src) { | 1917 uint64_t src) { |
1918 // See the comment in getThunk for -pcBias below. | |
1919 const int64_t pcBias = getPCBias(rel.type); | |
1788 for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) { | 1920 for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) { |
1789 ThunkSection *ts = tp.first; | 1921 ThunkSection *ts = tp.first; |
1790 uint64_t tsBase = os->addr + ts->outSecOff + rel.addend; | 1922 uint64_t tsBase = os->addr + ts->outSecOff - pcBias; |
1791 uint64_t tsLimit = tsBase + ts->getSize() + rel.addend; | 1923 uint64_t tsLimit = tsBase + ts->getSize(); |
1792 if (target->inBranchRange(rel.type, src, | 1924 if (target->inBranchRange(rel.type, src, |
1793 (src > tsLimit) ? tsBase : tsLimit)) | 1925 (src > tsLimit) ? tsBase : tsLimit)) |
1794 return ts; | 1926 return ts; |
1795 } | 1927 } |
1796 | 1928 |
1819 return ts; | 1951 return ts; |
1820 | 1952 |
1821 // Find InputSectionRange within Target Output Section (TOS) that the | 1953 // Find InputSectionRange within Target Output Section (TOS) that the |
1822 // InputSection (IS) that we need to precede is in. | 1954 // InputSection (IS) that we need to precede is in. |
1823 OutputSection *tos = isec->getParent(); | 1955 OutputSection *tos = isec->getParent(); |
1824 for (BaseCommand *bc : tos->sectionCommands) { | 1956 for (SectionCommand *bc : tos->commands) { |
1825 auto *isd = dyn_cast<InputSectionDescription>(bc); | 1957 auto *isd = dyn_cast<InputSectionDescription>(bc); |
1826 if (!isd || isd->sections.empty()) | 1958 if (!isd || isd->sections.empty()) |
1827 continue; | 1959 continue; |
1828 | 1960 |
1829 InputSection *first = isd->sections.front(); | 1961 InputSection *first = isd->sections.front(); |
1937 if (source->partition != target->partition) | 2069 if (source->partition != target->partition) |
1938 return target->partition == 1; | 2070 return target->partition == 1; |
1939 return true; | 2071 return true; |
1940 } | 2072 } |
1941 | 2073 |
1942 static int64_t getPCBias(RelType type) { | |
1943 if (config->emachine != EM_ARM) | |
1944 return 0; | |
1945 switch (type) { | |
1946 case R_ARM_THM_JUMP19: | |
1947 case R_ARM_THM_JUMP24: | |
1948 case R_ARM_THM_CALL: | |
1949 return 4; | |
1950 default: | |
1951 return 8; | |
1952 } | |
1953 } | |
1954 | |
1955 std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec, | 2074 std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec, |
1956 Relocation &rel, uint64_t src) { | 2075 Relocation &rel, uint64_t src) { |
1957 std::vector<Thunk *> *thunkVec = nullptr; | 2076 std::vector<Thunk *> *thunkVec = nullptr; |
1958 // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled | 2077 // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled |
1959 // out in the relocation addend. We compensate for the PC bias so that | 2078 // out in the relocation addend. We compensate for the PC bias so that |
1960 // an Arm and Thumb relocation to the same destination get the same keyAddend, | 2079 // an Arm and Thumb relocation to the same destination get the same keyAddend, |
1961 // which is usually 0. | 2080 // which is usually 0. |
1962 int64_t keyAddend = rel.addend + getPCBias(rel.type); | 2081 const int64_t pcBias = getPCBias(rel.type); |
2082 const int64_t keyAddend = rel.addend + pcBias; | |
1963 | 2083 |
1964 // We use a ((section, offset), addend) pair to find the thunk position if | 2084 // We use a ((section, offset), addend) pair to find the thunk position if |
1965 // possible so that we create only one thunk for aliased symbols or ICFed | 2085 // possible so that we create only one thunk for aliased symbols or ICFed |
1966 // sections. There may be multiple relocations sharing the same (section, | 2086 // sections. There may be multiple relocations sharing the same (section, |
1967 // offset + addend) pair. We may revert the relocation back to its original | 2087 // offset + addend) pair. We may revert the relocation back to its original |
1968 // non-Thunk target, so we cannot fold offset + addend. | 2088 // non-Thunk target, so we cannot fold offset + addend. |
1969 if (auto *d = dyn_cast<Defined>(rel.sym)) | 2089 if (auto *d = dyn_cast<Defined>(rel.sym)) |
1970 if (!d->isInPlt() && d->section) | 2090 if (!d->isInPlt() && d->section) |
1971 thunkVec = &thunkedSymbolsBySectionAndAddend[{ | 2091 thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value}, |
1972 {d->section->repl, d->value}, keyAddend}]; | 2092 keyAddend}]; |
1973 if (!thunkVec) | 2093 if (!thunkVec) |
1974 thunkVec = &thunkedSymbols[{rel.sym, keyAddend}]; | 2094 thunkVec = &thunkedSymbols[{rel.sym, keyAddend}]; |
1975 | 2095 |
1976 // Check existing Thunks for Sym to see if they can be reused | 2096 // Check existing Thunks for Sym to see if they can be reused |
1977 for (Thunk *t : *thunkVec) | 2097 for (Thunk *t : *thunkVec) |
1978 if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) && | 2098 if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) && |
1979 t->isCompatibleWith(*isec, rel) && | 2099 t->isCompatibleWith(*isec, rel) && |
1980 target->inBranchRange(rel.type, src, | 2100 target->inBranchRange(rel.type, src, |
1981 t->getThunkTargetSym()->getVA(rel.addend))) | 2101 t->getThunkTargetSym()->getVA(-pcBias))) |
1982 return std::make_pair(t, false); | 2102 return std::make_pair(t, false); |
1983 | 2103 |
1984 // No existing compatible Thunk in range, create a new one | 2104 // No existing compatible Thunk in range, create a new one |
1985 Thunk *t = addThunk(*isec, rel); | 2105 Thunk *t = addThunk(*isec, rel); |
1986 thunkVec->push_back(t); | 2106 thunkVec->push_back(t); |
2026 // | 2146 // |
2027 // If return value is false then no more Thunks are needed, and createThunks has | 2147 // If return value is false then no more Thunks are needed, and createThunks has |
2028 // made no changes. If the target requires range extension thunks, currently | 2148 // made no changes. If the target requires range extension thunks, currently |
2029 // ARM, then any future change in offset between caller and callee risks a | 2149 // ARM, then any future change in offset between caller and callee risks a |
2030 // relocation out of range error. | 2150 // relocation out of range error. |
2031 bool ThunkCreator::createThunks(ArrayRef<OutputSection *> outputSections) { | 2151 bool ThunkCreator::createThunks(uint32_t pass, |
2152 ArrayRef<OutputSection *> outputSections) { | |
2153 this->pass = pass; | |
2032 bool addressesChanged = false; | 2154 bool addressesChanged = false; |
2033 | 2155 |
2034 if (pass == 0 && target->getThunkSectionSpacing()) | 2156 if (pass == 0 && target->getThunkSectionSpacing()) |
2035 createInitialThunkSections(outputSections); | 2157 createInitialThunkSections(outputSections); |
2036 | 2158 |
2088 for (auto &p : thunkedSections) | 2210 for (auto &p : thunkedSections) |
2089 addressesChanged |= p.second->assignOffsets(); | 2211 addressesChanged |= p.second->assignOffsets(); |
2090 | 2212 |
2091 // Merge all created synthetic ThunkSections back into OutputSection | 2213 // Merge all created synthetic ThunkSections back into OutputSection |
2092 mergeThunks(outputSections); | 2214 mergeThunks(outputSections); |
2093 ++pass; | |
2094 return addressesChanged; | 2215 return addressesChanged; |
2095 } | 2216 } |
2096 | 2217 |
2097 // The following aid in the conversion of call x@GDPLT to call __tls_get_addr | 2218 // The following aid in the conversion of call x@GDPLT to call __tls_get_addr |
2098 // hexagonNeedsTLSSymbol scans for relocations would require a call to | 2219 // hexagonNeedsTLSSymbol scans for relocations would require a call to |
2111 }); | 2232 }); |
2112 return needTlsSymbol; | 2233 return needTlsSymbol; |
2113 } | 2234 } |
2114 | 2235 |
2115 void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) { | 2236 void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) { |
2116 Symbol *sym = symtab->find("__tls_get_addr"); | 2237 Symbol *sym = symtab.find("__tls_get_addr"); |
2117 if (!sym) | 2238 if (!sym) |
2118 return; | 2239 return; |
2119 bool needEntry = true; | 2240 bool needEntry = true; |
2120 forEachInputSectionDescription( | 2241 forEachInputSectionDescription( |
2121 outputSections, [&](OutputSection *os, InputSectionDescription *isd) { | 2242 outputSections, [&](OutputSection *os, InputSectionDescription *isd) { |
2122 for (InputSection *isec : isd->sections) | 2243 for (InputSection *isec : isd->sections) |
2123 for (Relocation &rel : isec->relocations) | 2244 for (Relocation &rel : isec->relocations) |
2124 if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) { | 2245 if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) { |
2125 if (needEntry) { | 2246 if (needEntry) { |
2126 addPltEntry(in.plt, in.gotPlt, in.relaPlt, target->pltRel, | 2247 sym->allocateAux(); |
2248 addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, | |
2127 *sym); | 2249 *sym); |
2128 needEntry = false; | 2250 needEntry = false; |
2129 } | 2251 } |
2130 rel.sym = sym; | 2252 rel.sym = sym; |
2131 } | 2253 } |
2132 }); | 2254 }); |
2133 } | 2255 } |
2134 | 2256 |
2135 template void elf::scanRelocations<ELF32LE>(InputSectionBase &); | 2257 template void elf::scanRelocations<ELF32LE>(); |
2136 template void elf::scanRelocations<ELF32BE>(InputSectionBase &); | 2258 template void elf::scanRelocations<ELF32BE>(); |
2137 template void elf::scanRelocations<ELF64LE>(InputSectionBase &); | 2259 template void elf::scanRelocations<ELF64LE>(); |
2138 template void elf::scanRelocations<ELF64BE>(InputSectionBase &); | 2260 template void elf::scanRelocations<ELF64BE>(); |
2139 template void elf::reportUndefinedSymbols<ELF32LE>(); | |
2140 template void elf::reportUndefinedSymbols<ELF32BE>(); | |
2141 template void elf::reportUndefinedSymbols<ELF64LE>(); | |
2142 template void elf::reportUndefinedSymbols<ELF64BE>(); |