Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/lld/ELF/Relocations.cpp
Warning:line 2050, column 9
3rd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name Relocations.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D LLD_VENDOR="Debian" -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/lld/ELF -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/lld/ELF -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/lld/include -I tools/lld/include -I include -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-10-03-140002-15933-1 -x c++ /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/lld/ELF/Relocations.cpp
1//===- Relocations.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains platform-independent functions to process relocations.
10// I'll describe the overview of this file here.
11//
12// Simple relocations are easy to handle for the linker. For example,
13// for R_X86_64_PC64 relocs, the linker just has to fix up locations
14// with the relative offsets to the target symbols. It would just be
15// reading records from relocation sections and applying them to output.
16//
17// But not all relocations are that easy to handle. For example, for
18// R_386_GOTOFF relocs, the linker has to create new GOT entries for
19// symbols if they don't exist, and fix up locations with GOT entry
20// offsets from the beginning of GOT section. So there is more than
21// fixing addresses in relocation processing.
22//
23// ELF defines a large number of complex relocations.
24//
25// The functions in this file analyze relocations and do whatever needs
26// to be done. It includes, but not limited to, the following.
27//
28// - create GOT/PLT entries
29// - create new relocations in .dynsym to let the dynamic linker resolve
30// them at runtime (since ELF supports dynamic linking, not all
31// relocations can be resolved at link-time)
32// - create COPY relocs and reserve space in .bss
33// - replace expensive relocs (in terms of runtime cost) with cheap ones
34// - error out infeasible combinations such as PIC and non-relative relocs
35//
36// Note that the functions in this file don't actually apply relocations
37// because it doesn't know about the output file nor the output file buffer.
38// It instead stores Relocation objects to InputSection's Relocations
39// vector to let it apply later in InputSection::writeTo.
40//
41//===----------------------------------------------------------------------===//
42
43#include "Relocations.h"
44#include "Config.h"
45#include "InputFiles.h"
46#include "LinkerScript.h"
47#include "OutputSections.h"
48#include "SymbolTable.h"
49#include "Symbols.h"
50#include "SyntheticSections.h"
51#include "Target.h"
52#include "Thunks.h"
53#include "lld/Common/ErrorHandler.h"
54#include "lld/Common/Memory.h"
55#include "llvm/ADT/SmallSet.h"
56#include "llvm/Demangle/Demangle.h"
57#include "llvm/Support/Endian.h"
58#include <algorithm>
59
60using namespace llvm;
61using namespace llvm::ELF;
62using namespace llvm::object;
63using namespace llvm::support::endian;
64using namespace lld;
65using namespace lld::elf;
66
67static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
68 for (SectionCommand *cmd : script->sectionCommands)
69 if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
70 if (assign->sym == &sym)
71 return assign->location;
72 return None;
73}
74
75static std::string getDefinedLocation(const Symbol &sym) {
76 const char msg[] = "\n>>> defined in ";
77 if (sym.file)
78 return msg + toString(sym.file);
79 if (Optional<std::string> loc = getLinkerScriptLocation(sym))
80 return msg + *loc;
81 return "";
82}
83
84// Construct a message in the following format.
85//
86// >>> defined in /home/alice/src/foo.o
87// >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
88// >>> /home/alice/src/bar.o:(.text+0x1)
89static std::string getLocation(InputSectionBase &s, const Symbol &sym,
90 uint64_t off) {
91 std::string msg = getDefinedLocation(sym) + "\n>>> referenced by ";
92 std::string src = s.getSrcMsg(sym, off);
93 if (!src.empty())
94 msg += src + "\n>>> ";
95 return msg + s.getObjMsg(off);
96}
97
98void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
99 int64_t min, uint64_t max) {
100 ErrorPlace errPlace = getErrorPlace(loc);
101 std::string hint;
102 if (rel.sym && !rel.sym->isSection())
103 hint = "; references " + lld::toString(*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);
108
109 if (errPlace.isec && errPlace.isec->name.startswith(".debug"))
110 hint += "; consider recompiling with -fdebug-types-section to reduce size "
111 "of debug sections";
112
113 errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) +
114 " out of range: " + v.str() + " is not in [" + Twine(min).str() +
115 ", " + Twine(max).str() + "]" + hint);
116}
117
118void elf::reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym,
119 const Twine &msg) {
120 ErrorPlace errPlace = getErrorPlace(loc);
121 std::string hint;
122 if (!sym.getName().empty())
123 hint = "; references " + lld::toString(sym) + getDefinedLocation(sym);
124 errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) +
125 " is not in [" + Twine(llvm::minIntN(n)) + ", " +
126 Twine(llvm::maxIntN(n)) + "]" + hint);
127}
128
129// Build a bitmask with one bit set for each 64 subset of RelExpr.
130static constexpr uint64_t buildMask() { return 0; }
131
132template <typename... Tails>
133static constexpr uint64_t buildMask(int head, Tails... tails) {
134 return (0 <= head && head < 64 ? uint64_t(1) << head : 0) |
135 buildMask(tails...);
136}
137
138// Return true if `Expr` is one of `Exprs`.
139// There are more than 64 but less than 128 RelExprs, so we divide the set of
140// exprs into [0, 64) and [64, 128) and represent each range as a constant
141// 64-bit mask. Then we decide which mask to test depending on the value of
142// expr and use a simple shift and bitwise-and to test for membership.
143template <RelExpr... Exprs> static bool oneof(RelExpr expr) {
144 assert(0 <= expr && (int)expr < 128 &&(static_cast <bool> (0 <= expr && (int)expr <
128 && "RelExpr is too large for 128-bit mask!") ? void
(0) : __assert_fail ("0 <= expr && (int)expr < 128 && \"RelExpr is too large for 128-bit mask!\""
, "lld/ELF/Relocations.cpp", 145, __extension__ __PRETTY_FUNCTION__
))
145 "RelExpr is too large for 128-bit mask!")(static_cast <bool> (0 <= expr && (int)expr <
128 && "RelExpr is too large for 128-bit mask!") ? void
(0) : __assert_fail ("0 <= expr && (int)expr < 128 && \"RelExpr is too large for 128-bit mask!\""
, "lld/ELF/Relocations.cpp", 145, __extension__ __PRETTY_FUNCTION__
))
;
146
147 if (expr >= 64)
148 return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...);
149 return (uint64_t(1) << expr) & buildMask(Exprs...);
150}
151
152static RelType getMipsPairType(RelType type, bool isLocal) {
153 switch (type) {
154 case R_MIPS_HI16:
155 return R_MIPS_LO16;
156 case R_MIPS_GOT16:
157 // In case of global symbol, the R_MIPS_GOT16 relocation does not
158 // have a pair. Each global symbol has a unique entry in the GOT
159 // and a corresponding instruction with help of the R_MIPS_GOT16
160 // relocation loads an address of the symbol. In case of local
161 // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
162 // the high 16 bits of the symbol's value. A paired R_MIPS_LO16
163 // relocations handle low 16 bits of the address. That allows
164 // to allocate only one GOT entry for every 64 KBytes of local data.
165 return isLocal ? R_MIPS_LO16 : R_MIPS_NONE;
166 case R_MICROMIPS_GOT16:
167 return isLocal ? R_MICROMIPS_LO16 : R_MIPS_NONE;
168 case R_MIPS_PCHI16:
169 return R_MIPS_PCLO16;
170 case R_MICROMIPS_HI16:
171 return R_MICROMIPS_LO16;
172 default:
173 return R_MIPS_NONE;
174 }
175}
176
177// True if non-preemptable symbol always has the same value regardless of where
178// the DSO is loaded.
179static bool isAbsolute(const Symbol &sym) {
180 if (sym.isUndefWeak())
181 return true;
182 if (const auto *dr = dyn_cast<Defined>(&sym))
183 return dr->section == nullptr; // Absolute symbol.
184 return false;
185}
186
187static bool isAbsoluteValue(const Symbol &sym) {
188 return isAbsolute(sym) || sym.isTls();
189}
190
191// Returns true if Expr refers a PLT entry.
192static bool needsPlt(RelExpr expr) {
193 return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT>(
194 expr);
195}
196
197// Returns true if Expr refers a GOT entry. Note that this function
198// returns false for TLS variables even though they need GOT, because
199// TLS variables uses GOT differently than the regular variables.
200static bool needsGot(RelExpr expr) {
201 return oneof<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
202 R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT,
203 R_AARCH64_GOT_PAGE>(expr);
204}
205
206// True if this expression is of the form Sym - X, where X is a position in the
207// file (PC, or GOT for example).
208static bool isRelExpr(RelExpr expr) {
209 return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL,
210 R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC,
211 R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr);
212}
213
214
215static RelExpr toPlt(RelExpr expr) {
216 switch (expr) {
217 case R_PPC64_CALL:
218 return R_PPC64_CALL_PLT;
219 case R_PC:
220 return R_PLT_PC;
221 case R_ABS:
222 return R_PLT;
223 default:
224 return expr;
225 }
226}
227
228static RelExpr fromPlt(RelExpr expr) {
229 // We decided not to use a plt. Optimize a reference to the plt to a
230 // reference to the symbol itself.
231 switch (expr) {
232 case R_PLT_PC:
233 case R_PPC32_PLTREL:
234 return R_PC;
235 case R_PPC64_CALL_PLT:
236 return R_PPC64_CALL;
237 case R_PLT:
238 return R_ABS;
239 case R_PLT_GOTPLT:
240 return R_GOTPLTREL;
241 default:
242 return expr;
243 }
244}
245
246// Returns true if a given shared symbol is in a read-only segment in a DSO.
247template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
248 using Elf_Phdr = typename ELFT::Phdr;
249
250 // Determine if the symbol is read-only by scanning the DSO's program headers.
251 const auto &file = cast<SharedFile>(*ss.file);
252 for (const Elf_Phdr &phdr :
253 check(file.template getObj<ELFT>().program_headers()))
254 if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) &&
255 !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr &&
256 ss.value < phdr.p_vaddr + phdr.p_memsz)
257 return true;
258 return false;
259}
260
261// Returns symbols at the same offset as a given symbol, including SS itself.
262//
263// If two or more symbols are at the same offset, and at least one of
264// them are copied by a copy relocation, all of them need to be copied.
265// Otherwise, they would refer to different places at runtime.
266template <class ELFT>
267static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
268 using Elf_Sym = typename ELFT::Sym;
269
270 const auto &file = cast<SharedFile>(*ss.file);
271
272 SmallSet<SharedSymbol *, 4> ret;
273 for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
274 if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
275 s.getType() == STT_TLS || s.st_value != ss.value)
276 continue;
277 StringRef name = check(s.getName(file.getStringTable()));
278 Symbol *sym = symtab.find(name);
279 if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
280 ret.insert(alias);
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);
289 return ret;
290}
291
292// When a symbol is copy relocated or we create a canonical plt entry, it is
293// effectively a defined symbol. In the case of copy relocation the symbol is
294// in .bss and in the case of a canonical plt entry it is in .plt. This function
295// replaces the existing symbol with a Defined pointing to the appropriate
296// location.
297static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value,
298 uint64_t size) {
299 Symbol old = sym;
300 Defined(sym.file, StringRef(), sym.binding, sym.stOther, sym.type, value,
301 size, &sec)
302 .overwrite(sym);
303
304 sym.verdefIndex = old.verdefIndex;
305 sym.exportDynamic = 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);
310}
311
312// Reserve space in .bss or .bss.rel.ro for copy relocation.
313//
314// The copy relocation is pretty much a hack. If you use a copy relocation
315// in your program, not only the symbol name but the symbol's size, RW/RO
316// bit and alignment become part of the ABI. In addition to that, if the
317// symbol has aliases, the aliases become part of the ABI. That's subtle,
318// but if you violate that implicit ABI, that can cause very counter-
319// intuitive consequences.
320//
321// So, what is the copy relocation? It's for linking non-position
322// independent code to DSOs. In an ideal world, all references to data
323// exported by DSOs should go indirectly through GOT. But if object files
324// are compiled as non-PIC, all data references are direct. There is no
325// way for the linker to transform the code to use GOT, as machine
326// instructions are already set in stone in object files. This is where
327// the copy relocation takes a role.
328//
329// A copy relocation instructs the dynamic linker to copy data from a DSO
330// to a specified address (which is usually in .bss) at load-time. If the
331// static linker (that's us) finds a direct data reference to a DSO
332// symbol, it creates a copy relocation, so that the symbol can be
333// resolved as if it were in .bss rather than in a DSO.
334//
335// As you can see in this function, we create a copy relocation for the
336// dynamic linker, and the relocation contains not only symbol name but
337// various other information about the symbol. So, such attributes become a
338// part of the ABI.
339//
340// Note for application developers: I can give you a piece of advice if
341// you are writing a shared library. You probably should export only
342// functions from your library. You shouldn't export variables.
343//
344// As an example what can happen when you export variables without knowing
345// the semantics of copy relocations, assume that you have an exported
346// variable of type T. It is an ABI-breaking change to add new members at
347// end of T even though doing that doesn't change the layout of the
348// existing members. That's because the space for the new members are not
349// reserved in .bss unless you recompile the main program. That means they
350// are likely to overlap with other data that happens to be laid out next
351// to the variable in .bss. This kind of issue is sometimes very hard to
352// debug. What's a solution? Instead of exporting a variable V from a DSO,
353// define an accessor getV().
354template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
355 // Copy relocation against zero-sized symbol doesn't make sense.
356 uint64_t symSize = ss.getSize();
357 if (symSize == 0 || ss.alignment == 0)
358 fatal("cannot create a copy relocation for symbol " + toString(ss));
359
360 // See if this symbol is in a read-only segment. If so, preserve the symbol's
361 // memory protection by reserving space in the .bss.rel.ro section.
362 bool isRO = isReadOnly<ELFT>(ss);
363 BssSection *sec =
364 make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment);
365 OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent();
366
367 // At this point, sectionBases has been migrated to sections. Append sec to
368 // sections.
369 if (osec->commands.empty() ||
370 !isa<InputSectionDescription>(osec->commands.back()))
371 osec->commands.push_back(make<InputSectionDescription>(""));
372 auto *isd = cast<InputSectionDescription>(osec->commands.back());
373 isd->sections.push_back(sec);
374 osec->commitSection(sec);
375
376 // Look through the DSO's dynamic symbol table for aliases and create a
377 // dynamic symbol for each one. This causes the copy relocation to correctly
378 // interpose any aliases.
379 for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
380 replaceWithDefined(*sym, *sec, 0, sym->size);
381
382 mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss);
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.
396namespace {
397class OffsetGetter {
398public:
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
433private:
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.
440class RelocationScanner {
441public:
442 template <class ELFT> void scanSection(InputSectionBase &s);
443
444private:
445 InputSectionBase *sec;
446 OffsetGetter getter;
447 const TargetInfo &target = *elf::target;
448
449 // End of relocations, used by Mips/PPC64.
450 const void *end = nullptr;
451
452 template <class RelTy> RelType getMipsN32RelType(RelTy *&rel) const;
453 template <class ELFT, class RelTy>
454 int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const;
455 template <class ELFT, class RelTy>
456 int64_t computeAddend(const RelTy &rel, RelExpr expr, bool isLocal) const;
457 bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
458 uint64_t relOff) const;
459 void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
460 int64_t addend) const;
461 template <class ELFT, class RelTy> void scanOne(RelTy *&i);
462 template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels);
463};
464} // namespace
465
466// MIPS has an odd notion of "paired" relocations to calculate addends.
467// For example, if a relocation is of R_MIPS_HI16, there must be a
468// R_MIPS_LO16 relocation after that, and an addend is calculated using
469// the two relocations.
470template <class ELFT, class RelTy>
471int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
472 bool isLocal) const {
473 if (expr == R_MIPS_GOTREL && isLocal)
474 return sec->getFile<ELFT>()->mipsGp0;
475
476 // The ABI says that the paired relocation is used only for REL.
477 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
478 if (RelTy::IsRela)
479 return 0;
480
481 RelType type = rel.getType(config->isMips64EL);
482 uint32_t pairTy = getMipsPairType(type, isLocal);
483 if (pairTy == R_MIPS_NONE)
484 return 0;
485
486 const uint8_t *buf = sec->rawData.data();
487 uint32_t symIndex = rel.getSymbol(config->isMips64EL);
488
489 // To make things worse, paired relocations might not be contiguous in
490 // the relocation table, so we need to do linear search. *sigh*
491 for (const RelTy *ri = &rel; ri != static_cast<const RelTy *>(end); ++ri)
492 if (ri->getType(config->isMips64EL) == pairTy &&
493 ri->getSymbol(config->isMips64EL) == symIndex)
494 return target.getImplicitAddend(buf + ri->r_offset, pairTy);
495
496 warn("can't find matching " + toString(pairTy) + " relocation for " +
497 toString(type));
498 return 0;
499}
500
501// Returns an addend of a given relocation. If it is RELA, an addend
502// is in a relocation itself. If it is REL, we need to read it from an
503// input section.
504template <class ELFT, class RelTy>
505int64_t RelocationScanner::computeAddend(const RelTy &rel, RelExpr expr,
506 bool isLocal) const {
507 int64_t addend;
508 RelType type = rel.getType(config->isMips64EL);
509
510 if (RelTy::IsRela) {
511 addend = getAddend<ELFT>(rel);
512 } else {
513 const uint8_t *buf = sec->rawData.data();
514 addend = target.getImplicitAddend(buf + rel.r_offset, type);
515 }
516
517 if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC)
518 addend += getPPC64TocBase();
519 if (config->emachine == EM_MIPS)
520 addend += computeMipsAddend<ELFT>(rel, expr, isLocal);
521
522 return addend;
523}
524
525// Custom error message if Sym is defined in a discarded section.
526template <class ELFT>
527static std::string maybeReportDiscarded(Undefined &sym) {
528 auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
529 if (!file || !sym.discardedSecIdx ||
530 file->getSections()[sym.discardedSecIdx] != &InputSection::discarded)
531 return "";
532 ArrayRef<typename ELFT::Shdr> objSections =
533 file->template getELFShdrs<ELFT>();
534
535 std::string msg;
536 if (sym.type == ELF::STT_SECTION) {
537 msg = "relocation refers to a discarded section: ";
538 msg += CHECK(check2((file->getObj().getSectionName(objSections[sym.discardedSecIdx
])), [&] { return toString(file); })
539 file->getObj().getSectionName(objSections[sym.discardedSecIdx]), file)check2((file->getObj().getSectionName(objSections[sym.discardedSecIdx
])), [&] { return toString(file); })
;
540 } else {
541 msg = "relocation refers to a symbol in a discarded section: " +
542 toString(sym);
543 }
544 msg += "\n>>> defined in " + toString(file);
545
546 Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];
547 if (elfSec.sh_type != SHT_GROUP)
548 return msg;
549
550 // If the discarded section is a COMDAT.
551 StringRef signature = file->getShtGroupSignature(objSections, elfSec);
552 if (const InputFile *prevailing =
553 symtab.comdatGroups.lookup(CachedHashStringRef(signature))) {
554 msg += "\n>>> section group signature: " + signature.str() +
555 "\n>>> prevailing definition is in " + toString(prevailing);
556 if (sym.nonPrevailing) {
557 msg += "\n>>> or the symbol in the prevailing group had STB_WEAK "
558 "binding and the symbol in a non-prevailing group had STB_GLOBAL "
559 "binding. Mixing groups with STB_WEAK and STB_GLOBAL binding "
560 "signature is not supported";
561 }
562 }
563 return msg;
564}
565
566namespace {
567// Undefined diagnostics are collected in a vector and emitted once all of
568// them are known, so that some postprocessing on the list of undefined symbols
569// can happen before lld emits diagnostics.
570struct UndefinedDiag {
571 Undefined *sym;
572 struct Loc {
573 InputSectionBase *sec;
574 uint64_t offset;
575 };
576 std::vector<Loc> locs;
577 bool isWarning;
578};
579
580std::vector<UndefinedDiag> undefs;
581std::mutex relocMutex;
582}
583
584// Check whether the definition name def is a mangled function name that matches
585// the reference name ref.
586static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {
587 llvm::ItaniumPartialDemangler d;
588 std::string name = def.str();
589 if (d.partialDemangle(name.c_str()))
590 return false;
591 char *buf = d.getFunctionName(nullptr, nullptr);
592 if (!buf)
593 return false;
594 bool ret = ref == buf;
595 free(buf);
596 return ret;
597}
598
599// Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns
600// the suggested symbol, which is either in the symbol table, or in the same
601// file of sym.
602static const Symbol *getAlternativeSpelling(const Undefined &sym,
603 std::string &pre_hint,
604 std::string &post_hint) {
605 DenseMap<StringRef, const Symbol *> map;
606 if (sym.file && sym.file->kind() == InputFile::ObjKind) {
607 auto *file = cast<ELFFileBase>(sym.file);
608 // If sym is a symbol defined in a discarded section, maybeReportDiscarded()
609 // will give an error. Don't suggest an alternative spelling.
610 if (file && sym.discardedSecIdx != 0 &&
611 file->getSections()[sym.discardedSecIdx] == &InputSection::discarded)
612 return nullptr;
613
614 // Build a map of local defined symbols.
615 for (const Symbol *s : sym.file->getSymbols())
616 if (s->isLocal() && s->isDefined() && !s->getName().empty())
617 map.try_emplace(s->getName(), s);
618 }
619
620 auto suggest = [&](StringRef newName) -> const Symbol * {
621 // If defined locally.
622 if (const Symbol *s = map.lookup(newName))
623 return s;
624
625 // If in the symbol table and not undefined.
626 if (const Symbol *s = symtab.find(newName))
627 if (!s->isUndefined())
628 return s;
629
630 return nullptr;
631 };
632
633 // This loop enumerates all strings of Levenshtein distance 1 as typo
634 // correction candidates and suggests the one that exists as a non-undefined
635 // symbol.
636 StringRef name = sym.getName();
637 for (size_t i = 0, e = name.size(); i != e + 1; ++i) {
638 // Insert a character before name[i].
639 std::string newName = (name.substr(0, i) + "0" + name.substr(i)).str();
640 for (char c = '0'; c <= 'z'; ++c) {
641 newName[i] = c;
642 if (const Symbol *s = suggest(newName))
643 return s;
644 }
645 if (i == e)
646 break;
647
648 // Substitute name[i].
649 newName = std::string(name);
650 for (char c = '0'; c <= 'z'; ++c) {
651 newName[i] = c;
652 if (const Symbol *s = suggest(newName))
653 return s;
654 }
655
656 // Transpose name[i] and name[i+1]. This is of edit distance 2 but it is
657 // common.
658 if (i + 1 < e) {
659 newName[i] = name[i + 1];
660 newName[i + 1] = name[i];
661 if (const Symbol *s = suggest(newName))
662 return s;
663 }
664
665 // Delete name[i].
666 newName = (name.substr(0, i) + name.substr(i + 1)).str();
667 if (const Symbol *s = suggest(newName))
668 return s;
669 }
670
671 // Case mismatch, e.g. Foo vs FOO.
672 for (auto &it : map)
673 if (name.equals_insensitive(it.first))
674 return it.second;
675 for (Symbol *sym : symtab.getSymbols())
676 if (!sym->isUndefined() && name.equals_insensitive(sym->getName()))
677 return sym;
678
679 // The reference may be a mangled name while the definition is not. Suggest a
680 // missing extern "C".
681 if (name.startswith("_Z")) {
682 std::string buf = name.str();
683 llvm::ItaniumPartialDemangler d;
684 if (!d.partialDemangle(buf.c_str()))
685 if (char *buf = d.getFunctionName(nullptr, nullptr)) {
686 const Symbol *s = suggest(buf);
687 free(buf);
688 if (s) {
689 pre_hint = ": extern \"C\" ";
690 return s;
691 }
692 }
693 } else {
694 const Symbol *s = nullptr;
695 for (auto &it : map)
696 if (canSuggestExternCForCXX(name, it.first)) {
697 s = it.second;
698 break;
699 }
700 if (!s)
701 for (Symbol *sym : symtab.getSymbols())
702 if (canSuggestExternCForCXX(name, sym->getName())) {
703 s = sym;
704 break;
705 }
706 if (s) {
707 pre_hint = " to declare ";
708 post_hint = " as extern \"C\"?";
709 return s;
710 }
711 }
712
713 return nullptr;
714}
715
716static void reportUndefinedSymbol(const UndefinedDiag &undef,
717 bool correctSpelling) {
718 Undefined &sym = *undef.sym;
719
720 auto visibility = [&]() -> std::string {
721 switch (sym.visibility()) {
722 case STV_INTERNAL:
723 return "internal ";
724 case STV_HIDDEN:
725 return "hidden ";
726 case STV_PROTECTED:
727 return "protected ";
728 default:
729 return "";
730 }
731 };
732
733 std::string msg;
734 switch (config->ekind) {
735 case ELF32LEKind:
736 msg = maybeReportDiscarded<ELF32LE>(sym);
737 break;
738 case ELF32BEKind:
739 msg = maybeReportDiscarded<ELF32BE>(sym);
740 break;
741 case ELF64LEKind:
742 msg = maybeReportDiscarded<ELF64LE>(sym);
743 break;
744 case ELF64BEKind:
745 msg = maybeReportDiscarded<ELF64BE>(sym);
746 break;
747 default:
748 llvm_unreachable("")::llvm::llvm_unreachable_internal("", "lld/ELF/Relocations.cpp"
, 748)
;
749 }
750 if (msg.empty())
751 msg = "undefined " + visibility() + "symbol: " + toString(sym);
752
753 const size_t maxUndefReferences = 3;
754 size_t i = 0;
755 for (UndefinedDiag::Loc l : undef.locs) {
756 if (i >= maxUndefReferences)
757 break;
758 InputSectionBase &sec = *l.sec;
759 uint64_t offset = l.offset;
760
761 msg += "\n>>> referenced by ";
762 std::string src = sec.getSrcMsg(sym, offset);
763 if (!src.empty())
764 msg += src + "\n>>> ";
765 msg += sec.getObjMsg(offset);
766 i++;
767 }
768
769 if (i < undef.locs.size())
770 msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times")
771 .str();
772
773 if (correctSpelling) {
774 std::string pre_hint = ": ", post_hint;
775 if (const Symbol *corrected =
776 getAlternativeSpelling(sym, pre_hint, post_hint)) {
777 msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint;
778 if (corrected->file)
779 msg += "\n>>> defined in: " + toString(corrected->file);
780 }
781 }
782
783 if (sym.getName().startswith("_ZTV"))
784 msg +=
785 "\n>>> the vtable symbol may be undefined because the class is missing "
786 "its key function (see https://lld.llvm.org/missingkeyfunction)";
787 if (config->gcSections && config->zStartStopGC &&
788 sym.getName().startswith("__start_")) {
789 msg += "\n>>> the encapsulation symbol needs to be retained under "
790 "--gc-sections properly; consider -z nostart-stop-gc "
791 "(see https://lld.llvm.org/ELF/start-stop-gc)";
792 }
793
794 if (undef.isWarning)
795 warn(msg);
796 else
797 error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
798}
799
800void elf::reportUndefinedSymbols() {
801 // Find the first "undefined symbol" diagnostic for each diagnostic, and
802 // collect all "referenced from" lines at the first diagnostic.
803 DenseMap<Symbol *, UndefinedDiag *> firstRef;
804 for (UndefinedDiag &undef : undefs) {
805 assert(undef.locs.size() == 1)(static_cast <bool> (undef.locs.size() == 1) ? void (0)
: __assert_fail ("undef.locs.size() == 1", "lld/ELF/Relocations.cpp"
, 805, __extension__ __PRETTY_FUNCTION__))
;
806 if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
807 canon->locs.push_back(undef.locs[0]);
808 undef.locs.clear();
809 } else
810 firstRef[undef.sym] = &undef;
811 }
812
813 // Enable spell corrector for the first 2 diagnostics.
814 for (const auto &[i, undef] : llvm::enumerate(undefs))
815 if (!undef.locs.empty())
816 reportUndefinedSymbol(undef, i < 2);
817 undefs.clear();
818}
819
820// Report an undefined symbol if necessary.
821// Returns true if the undefined symbol will produce an error message.
822static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec,
823 uint64_t offset) {
824 std::lock_guard<std::mutex> lock(relocMutex);
825 // If versioned, issue an error (even if the symbol is weak) because we don't
826 // know the defining filename which is required to construct a Verneed entry.
827 if (sym.hasVersionSuffix) {
828 undefs.push_back({&sym, {{&sec, offset}}, false});
829 return true;
830 }
831 if (sym.isWeak())
832 return false;
833
834 bool canBeExternal = !sym.isLocal() && sym.visibility() == STV_DEFAULT;
835 if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)
836 return false;
837
838 // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
839 // which references a switch table in a discarded .rodata/.text section. The
840 // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
841 // spec says references from outside the group to a STB_LOCAL symbol are not
842 // allowed. Work around the bug.
843 //
844 // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible
845 // because .LC0-.LTOC is not representable if the two labels are in different
846 // .got2
847 if (sym.discardedSecIdx != 0 && (sec.name == ".got2" || sec.name == ".toc"))
848 return false;
849
850 bool isWarning =
851 (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
852 config->noinhibitExec;
853 undefs.push_back({&sym, {{&sec, offset}}, isWarning});
854 return !isWarning;
855}
856
857// MIPS N32 ABI treats series of successive relocations with the same offset
858// as a single relocation. The similar approach used by N64 ABI, but this ABI
859// packs all relocations into the single relocation record. Here we emulate
860// this for the N32 ABI. Iterate over relocation with the same offset and put
861// theirs types into the single bit-set.
862template <class RelTy>
863RelType RelocationScanner::getMipsN32RelType(RelTy *&rel) const {
864 RelType type = 0;
865 uint64_t offset = rel->r_offset;
866
867 int n = 0;
868 while (rel != static_cast<const RelTy *>(end) && rel->r_offset == offset)
869 type |= (rel++)->getType(config->isMips64EL) << (8 * n++);
870 return type;
871}
872
873template <bool shard = false>
874static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec,
875 Symbol &sym, int64_t addend, RelExpr expr,
876 RelType type) {
877 Partition &part = isec.getPartition();
878
879 // Add a relative relocation. If relrDyn section is enabled, and the
880 // relocation offset is guaranteed to be even, add the relocation to
881 // the relrDyn section, otherwise add it to the relaDyn section.
882 // relrDyn sections don't support odd offsets. Also, relrDyn sections
883 // don't store the addend values, so we must write it to the relocated
884 // address.
885 if (part.relrDyn && isec.alignment >= 2 && offsetInSec % 2 == 0) {
886 isec.relocations.push_back({expr, type, offsetInSec, addend, &sym});
887 if (shard)
888 part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back(
889 {&isec, offsetInSec});
890 else
891 part.relrDyn->relocs.push_back({&isec, offsetInSec});
892 return;
893 }
894 part.relaDyn->addRelativeReloc<shard>(target->relativeRel, isec, offsetInSec,
895 sym, addend, type, expr);
896}
897
898template <class PltSection, class GotPltSection>
899static void addPltEntry(PltSection &plt, GotPltSection &gotPlt,
900 RelocationBaseSection &rel, RelType type, Symbol &sym) {
901 plt.addEntry(sym);
902 gotPlt.addEntry(sym);
903 rel.addReloc({type, &gotPlt, sym.getGotPltOffset(),
904 sym.isPreemptible ? DynamicReloc::AgainstSymbol
905 : DynamicReloc::AddendOnlyWithTargetVA,
906 sym, 0, R_ABS});
907}
908
909static void addGotEntry(Symbol &sym) {
910 in.got->addEntry(sym);
911 uint64_t off = sym.getGotOffset();
912
913 // If preemptible, emit a GLOB_DAT relocation.
914 if (sym.isPreemptible) {
915 mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off,
916 DynamicReloc::AgainstSymbol, sym, 0, R_ABS});
917 return;
918 }
919
920 // Otherwise, the value is either a link-time constant or the load base
921 // plus a constant.
922 if (!config->isPic || isAbsolute(sym))
923 in.got->relocations.push_back({R_ABS, target->symbolicRel, off, 0, &sym});
924 else
925 addRelativeReloc(*in.got, off, sym, 0, R_ABS, target->symbolicRel);
926}
927
928static void addTpOffsetGotEntry(Symbol &sym) {
929 in.got->addEntry(sym);
930 uint64_t off = sym.getGotOffset();
931 if (!sym.isPreemptible && !config->isPic) {
932 in.got->relocations.push_back({R_TPREL, target->symbolicRel, off, 0, &sym});
933 return;
934 }
935 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
936 target->tlsGotRel, *in.got, off, sym, target->symbolicRel);
937}
938
939// Return true if we can define a symbol in the executable that
940// contains the value/function of a symbol defined in a shared
941// library.
942static bool canDefineSymbolInExecutable(Symbol &sym) {
943 // If the symbol has default visibility the symbol defined in the
944 // executable will preempt it.
945 // Note that we want the visibility of the shared symbol itself, not
946 // the visibility of the symbol in the output file we are producing.
947 if (!sym.dsoProtected)
948 return true;
949
950 // If we are allowed to break address equality of functions, defining
951 // a plt entry will allow the program to call the function in the
952 // .so, but the .so and the executable will no agree on the address
953 // of the function. Similar logic for objects.
954 return ((sym.isFunc() && config->ignoreFunctionAddressEquality) ||
955 (sym.isObject() && config->ignoreDataAddressEquality));
956}
957
958// Returns true if a given relocation can be computed at link-time.
959// This only handles relocation types expected in processAux.
960//
961// For instance, we know the offset from a relocation to its target at
962// link-time if the relocation is PC-relative and refers a
963// non-interposable function in the same executable. This function
964// will return true for such relocation.
965//
966// If this function returns false, that means we need to emit a
967// dynamic relocation so that the relocation will be fixed at load-time.
968bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
969 const Symbol &sym,
970 uint64_t relOff) const {
971 // These expressions always compute a constant
972 if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, R_MIPS_GOT_LOCAL_PAGE,
973 R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
974 R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
975 R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT,
976 R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE>(e))
977 return true;
978
979 // These never do, except if the entire file is position dependent or if
980 // only the low bits are used.
981 if (e == R_GOT || e == R_PLT)
982 return target.usesOnlyLowPageBits(type) || !config->isPic;
983
984 if (sym.isPreemptible)
985 return false;
986 if (!config->isPic)
987 return true;
988
989 // The size of a non preemptible symbol is a constant.
990 if (e == R_SIZE)
991 return true;
992
993 // For the target and the relocation, we want to know if they are
994 // absolute or relative.
995 bool absVal = isAbsoluteValue(sym);
996 bool relE = isRelExpr(e);
997 if (absVal && !relE)
998 return true;
999 if (!absVal && relE)
1000 return true;
1001 if (!absVal && !relE)
1002 return target.usesOnlyLowPageBits(type);
1003
1004 assert(absVal && relE)(static_cast <bool> (absVal && relE) ? void (0)
: __assert_fail ("absVal && relE", "lld/ELF/Relocations.cpp"
, 1004, __extension__ __PRETTY_FUNCTION__))
;
1005
1006 // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol
1007 // in PIC mode. This is a little strange, but it allows us to link function
1008 // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers).
1009 // Normally such a call will be guarded with a comparison, which will load a
1010 // zero from the GOT.
1011 if (sym.isUndefWeak())
1012 return true;
1013
1014 // We set the final symbols values for linker script defined symbols later.
1015 // They always can be computed as a link time constant.
1016 if (sym.scriptDefined)
1017 return true;
1018
1019 error("relocation " + toString(type) + " cannot refer to absolute symbol: " +
1020 toString(sym) + getLocation(*sec, sym, relOff));
1021 return true;
1022}
1023
1024// The reason we have to do this early scan is as follows
1025// * To mmap the output file, we need to know the size
1026// * For that, we need to know how many dynamic relocs we will have.
1027// It might be possible to avoid this by outputting the file with write:
1028// * Write the allocated output sections, computing addresses.
1029// * Apply relocations, recording which ones require a dynamic reloc.
1030// * Write the dynamic relocations.
1031// * Write the rest of the file.
1032// This would have some drawbacks. For example, we would only know if .rela.dyn
1033// is needed after applying relocations. If it is, it will go after rw and rx
1034// sections. Given that it is ro, we will need an extra PT_LOAD. This
1035// complicates things for the dynamic linker and means we would have to reserve
1036// space for the extra PT_LOAD even if we end up not using it.
1037void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
1038 Symbol &sym, int64_t addend) const {
1039 // If the relocation is known to be a link-time constant, we know no dynamic
1040 // relocation will be created, pass the control to relocateAlloc() or
1041 // relocateNonAlloc() to resolve it.
1042 //
1043 // The behavior of an undefined weak reference is implementation defined. For
1044 // non-link-time constants, we resolve relocations statically (let
1045 // relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic
1046 // relocations for -pie and -shared.
1047 //
1048 // The general expectation of -no-pie static linking is that there is no
1049 // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for
1050 // -shared matches the spirit of its -z undefs default. -pie has freedom on
1051 // choices, and we choose dynamic relocations to be consistent with the
1052 // handling of GOT-generating relocations.
1053 if (isStaticLinkTimeConstant(expr, type, sym, offset) ||
1054 (!config->isPic && sym.isUndefWeak())) {
1055 sec->relocations.push_back({expr, type, offset, addend, &sym});
1056 return;
1057 }
1058
1059 bool canWrite = (sec->flags & SHF_WRITE) || !config->zText;
1060 if (canWrite) {
1061 RelType rel = target.getDynRel(type);
1062 if (expr == R_GOT || (rel == target.symbolicRel && !sym.isPreemptible)) {
1063 addRelativeReloc<true>(*sec, offset, sym, addend, expr, type);
1064 return;
1065 } else if (rel != 0) {
1066 if (config->emachine == EM_MIPS && rel == target.symbolicRel)
1067 rel = target.relativeRel;
1068 std::lock_guard<std::mutex> lock(relocMutex);
1069 sec->getPartition().relaDyn->addSymbolReloc(rel, *sec, offset, sym,
1070 addend, type);
1071
1072 // MIPS ABI turns using of GOT and dynamic relocations inside out.
1073 // While regular ABI uses dynamic relocations to fill up GOT entries
1074 // MIPS ABI requires dynamic linker to fills up GOT entries using
1075 // specially sorted dynamic symbol table. This affects even dynamic
1076 // relocations against symbols which do not require GOT entries
1077 // creation explicitly, i.e. do not have any GOT-relocations. So if
1078 // a preemptible symbol has a dynamic relocation we anyway have
1079 // to create a GOT entry for it.
1080 // If a non-preemptible symbol has a dynamic relocation against it,
1081 // dynamic linker takes it st_value, adds offset and writes down
1082 // result of the dynamic relocation. In case of preemptible symbol
1083 // dynamic linker performs symbol resolution, writes the symbol value
1084 // to the GOT entry and reads the GOT entry when it needs to perform
1085 // a dynamic relocation.
1086 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
1087 if (config->emachine == EM_MIPS)
1088 in.mipsGot->addEntry(*sec->file, sym, addend, expr);
1089 return;
1090 }
1091 }
1092
1093 // When producing an executable, we can perform copy relocations (for
1094 // STT_OBJECT) and canonical PLT (for STT_FUNC).
1095 if (!config->shared) {
1096 if (!canDefineSymbolInExecutable(sym)) {
1097 errorOrWarn("cannot preempt symbol: " + toString(sym) +
1098 getLocation(*sec, sym, offset));
1099 return;
1100 }
1101
1102 if (sym.isObject()) {
1103 // Produce a copy relocation.
1104 if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1105 if (!config->zCopyreloc)
1106 error("unresolvable relocation " + toString(type) +
1107 " against symbol '" + toString(*ss) +
1108 "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1109 getLocation(*sec, sym, offset));
1110 sym.setFlags(NEEDS_COPY);
1111 }
1112 sec->relocations.push_back({expr, type, offset, addend, &sym});
1113 return;
1114 }
1115
1116 // This handles a non PIC program call to function in a shared library. In
1117 // an ideal world, we could just report an error saying the relocation can
1118 // overflow at runtime. In the real world with glibc, crt1.o has a
1119 // R_X86_64_PC32 pointing to libc.so.
1120 //
1121 // The general idea on how to handle such cases is to create a PLT entry and
1122 // use that as the function value.
1123 //
1124 // For the static linking part, we just return a plt expr and everything
1125 // else will use the PLT entry as the address.
1126 //
1127 // The remaining problem is making sure pointer equality still works. We
1128 // need the help of the dynamic linker for that. We let it know that we have
1129 // a direct reference to a so symbol by creating an undefined symbol with a
1130 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1131 // the value of the symbol we created. This is true even for got entries, so
1132 // pointer equality is maintained. To avoid an infinite loop, the only entry
1133 // that points to the real function is a dedicated got entry used by the
1134 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1135 // R_386_JMP_SLOT, etc).
1136
1137 // For position independent executable on i386, the plt entry requires ebx
1138 // to be set. This causes two problems:
1139 // * If some code has a direct reference to a function, it was probably
1140 // compiled without -fPIE/-fPIC and doesn't maintain ebx.
1141 // * If a library definition gets preempted to the executable, it will have
1142 // the wrong ebx value.
1143 if (sym.isFunc()) {
1144 if (config->pie && config->emachine == EM_386)
1145 errorOrWarn("symbol '" + toString(sym) +
1146 "' cannot be preempted; recompile with -fPIE" +
1147 getLocation(*sec, sym, offset));
1148 sym.setFlags(NEEDS_COPY | NEEDS_PLT);
1149 sec->relocations.push_back({expr, type, offset, addend, &sym});
1150 return;
1151 }
1152 }
1153
1154 errorOrWarn("relocation " + toString(type) + " cannot be used against " +
1155 (sym.getName().empty() ? "local symbol"
1156 : "symbol '" + toString(sym) + "'") +
1157 "; recompile with -fPIC" + getLocation(*sec, sym, offset));
1158}
1159
1160// This function is similar to the `handleTlsRelocation`. MIPS does not
1161// support any relaxations for TLS relocations so by factoring out MIPS
1162// handling in to the separate function we can simplify the code and do not
1163// pollute other `handleTlsRelocation` by MIPS `ifs` statements.
1164// Mips has a custom MipsGotSection that handles the writing of GOT entries
1165// without dynamic relocations.
1166static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym,
1167 InputSectionBase &c, uint64_t offset,
1168 int64_t addend, RelExpr expr) {
1169 if (expr == R_MIPS_TLSLD) {
1170 in.mipsGot->addTlsIndex(*c.file);
1171 c.relocations.push_back({expr, type, offset, addend, &sym});
1172 return 1;
1173 }
1174 if (expr == R_MIPS_TLSGD) {
1175 in.mipsGot->addDynTlsEntry(*c.file, sym);
1176 c.relocations.push_back({expr, type, offset, addend, &sym});
1177 return 1;
1178 }
1179 return 0;
1180}
1181
1182// Notes about General Dynamic and Local Dynamic TLS models below. They may
1183// require the generation of a pair of GOT entries that have associated dynamic
1184// relocations. The pair of GOT entries created are of the form GOT[e0] Module
1185// Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of
1186// symbol in TLS block.
1187//
1188// Returns the number of relocations processed.
1189static unsigned handleTlsRelocation(RelType type, Symbol &sym,
1190 InputSectionBase &c, uint64_t offset,
1191 int64_t addend, RelExpr expr) {
1192 if (!sym.isTls())
1193 return 0;
1194
1195 if (config->emachine == EM_MIPS)
1196 return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
1197
1198 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
1199 R_TLSDESC_GOTPLT>(expr) &&
1200 config->shared) {
1201 if (expr != R_TLSDESC_CALL) {
1202 sym.setFlags(NEEDS_TLSDESC);
1203 c.relocations.push_back({expr, type, offset, addend, &sym});
1204 }
1205 return 1;
1206 }
1207
1208 // ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For
1209 // PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable
1210 // relaxation as well.
1211 bool toExecRelax = !config->shared && config->emachine != EM_ARM &&
1212 config->emachine != EM_HEXAGON &&
1213 config->emachine != EM_RISCV &&
1214 !c.file->ppc64DisableTLSRelax;
1215
1216 // If we are producing an executable and the symbol is non-preemptable, it
1217 // must be defined and the code sequence can be relaxed to use Local-Exec.
1218 //
1219 // ARM and RISC-V do not support any relaxations for TLS relocations, however,
1220 // we can omit the DTPMOD dynamic relocations and resolve them at link time
1221 // because them are always 1. This may be necessary for static linking as
1222 // DTPMOD may not be expected at load time.
1223 bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
1224
1225 // Local Dynamic is for access to module local TLS variables, while still
1226 // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
1227 // module index, with a special value of 0 for the current module. GOT[e1] is
1228 // unused. There only needs to be one module index entry.
1229 if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
1230 expr)) {
1231 // Local-Dynamic relocs can be relaxed to Local-Exec.
1232 if (toExecRelax) {
1233 c.relocations.push_back(
1234 {target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, offset,
1235 addend, &sym});
1236 return target->getTlsGdRelaxSkip(type);
1237 }
1238 if (expr == R_TLSLD_HINT)
1239 return 1;
1240 ctx.needsTlsLd.store(true, std::memory_order_relaxed);
1241 c.relocations.push_back({expr, type, offset, addend, &sym});
1242 return 1;
1243 }
1244
1245 // Local-Dynamic relocs can be relaxed to Local-Exec.
1246 if (expr == R_DTPREL) {
1247 if (toExecRelax)
1248 expr = target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE);
1249 c.relocations.push_back({expr, type, offset, addend, &sym});
1250 return 1;
1251 }
1252
1253 // Local-Dynamic sequence where offset of tls variable relative to dynamic
1254 // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
1255 if (expr == R_TLSLD_GOT_OFF) {
1256 sym.setFlags(NEEDS_GOT_DTPREL);
1257 c.relocations.push_back({expr, type, offset, addend, &sym});
1258 return 1;
1259 }
1260
1261 if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
1262 R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
1263 if (!toExecRelax) {
1264 sym.setFlags(NEEDS_TLSGD);
1265 c.relocations.push_back({expr, type, offset, addend, &sym});
1266 return 1;
1267 }
1268
1269 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
1270 // depending on the symbol being locally defined or not.
1271 if (sym.isPreemptible) {
1272 sym.setFlags(NEEDS_TLSGD_TO_IE);
1273 c.relocations.push_back(
1274 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, offset,
1275 addend, &sym});
1276 } else {
1277 c.relocations.push_back(
1278 {target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, offset,
1279 addend, &sym});
1280 }
1281 return target->getTlsGdRelaxSkip(type);
1282 }
1283
1284 if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
1285 R_TLSIE_HINT>(expr)) {
1286 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
1287 // defined.
1288 if (toExecRelax && isLocalInExecutable) {
1289 c.relocations.push_back(
1290 {R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
1291 } else if (expr != R_TLSIE_HINT) {
1292 sym.setFlags(NEEDS_TLSIE);
1293 // R_GOT needs a relative relocation for PIC on i386 and Hexagon.
1294 if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type))
1295 addRelativeReloc<true>(c, offset, sym, addend, expr, type);
1296 else
1297 c.relocations.push_back({expr, type, offset, addend, &sym});
1298 }
1299 return 1;
1300 }
1301
1302 return 0;
1303}
1304
1305template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
1306 const RelTy &rel = *i;
1307 uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1308 Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex);
1309 RelType type;
1310
1311 // Deal with MIPS oddity.
1312 if (config->mipsN32Abi) {
1313 type = getMipsN32RelType(i);
1314 } else {
1315 type = rel.getType(config->isMips64EL);
1316 ++i;
1317 }
1318
1319 // Get an offset in an output section this relocation is applied to.
1320 uint64_t offset = getter.get(rel.r_offset);
1321 if (offset == uint64_t(-1))
1322 return;
1323
1324 // Error if the target symbol is undefined. Symbol index 0 may be used by
1325 // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
1326 if (sym.isUndefined() && symIndex != 0 &&
1327 maybeReportUndefined(cast<Undefined>(sym), *sec, offset))
1328 return;
1329
1330 const uint8_t *relocatedAddr = sec->rawData.begin() + offset;
1331 RelExpr expr = target.getRelExpr(type, sym, relocatedAddr);
1332
1333 // Ignore R_*_NONE and other marker relocations.
1334 if (expr == R_NONE)
1335 return;
1336
1337 // Read an addend.
1338 int64_t addend = computeAddend<ELFT>(rel, expr, sym.isLocal());
1339
1340 if (config->emachine == EM_PPC64) {
1341 // We can separate the small code model relocations into 2 categories:
1342 // 1) Those that access the compiler generated .toc sections.
1343 // 2) Those that access the linker allocated got entries.
1344 // lld allocates got entries to symbols on demand. Since we don't try to
1345 // sort the got entries in any way, we don't have to track which objects
1346 // have got-based small code model relocs. The .toc sections get placed
1347 // after the end of the linker allocated .got section and we do sort those
1348 // so sections addressed with small code model relocations come first.
1349 if (type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS)
1350 sec->file->ppc64SmallCodeModelTocRelocs = true;
1351
1352 // Record the TOC entry (.toc + addend) as not relaxable. See the comment in
1353 // InputSectionBase::relocateAlloc().
1354 if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) &&
1355 cast<Defined>(sym).section->name == ".toc")
1356 ppc64noTocRelax.insert({&sym, addend});
1357
1358 if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) ||
1359 (type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) {
1360 if (i == end) {
1361 errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last "
1362 "relocation" +
1363 getLocation(*sec, sym, offset));
1364 return;
1365 }
1366
1367 // Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case,
1368 // so we can discern it later from the toc-case.
1369 if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC)
1370 ++offset;
1371 }
1372 }
1373
1374 // If the relocation does not emit a GOT or GOTPLT entry but its computation
1375 // uses their addresses, we need GOT or GOTPLT to be created.
1376 //
1377 // The 5 types that relative GOTPLT are all x86 and x86-64 specific.
1378 if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT,
1379 R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
1380 in.gotPlt->hasGotPltOffRel.store(true, std::memory_order_relaxed);
1381 } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE,
1382 R_PPC64_RELAX_TOC>(expr)) {
1383 in.got->hasGotOffRel.store(true, std::memory_order_relaxed);
1384 }
1385
1386 // Process TLS relocations, including relaxing TLS relocations. Note that
1387 // R_TPREL and R_TPREL_NEG relocations are resolved in processAux.
1388 if (expr == R_TPREL || expr == R_TPREL_NEG) {
1389 if (config->shared) {
1390 errorOrWarn("relocation " + toString(type) + " against " + toString(sym) +
1391 " cannot be used with -shared" +
1392 getLocation(*sec, sym, offset));
1393 return;
1394 }
1395 } else if (unsigned processed =
1396 handleTlsRelocation(type, sym, *sec, offset, addend, expr)) {
1397 i += (processed - 1);
1398 return;
1399 }
1400
1401 // Relax relocations.
1402 //
1403 // If we know that a PLT entry will be resolved within the same ELF module, we
1404 // can skip PLT access and directly jump to the destination function. For
1405 // example, if we are linking a main executable, all dynamic symbols that can
1406 // be resolved within the executable will actually be resolved that way at
1407 // runtime, because the main executable is always at the beginning of a search
1408 // list. We can leverage that fact.
1409 const bool isIfunc = sym.isGnuIFunc();
1410 if (!sym.isPreemptible && (!isIfunc || config->zIfuncNoplt)) {
1411 if (expr != R_GOT_PC) {
1412 // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call
1413 // stub type. It should be ignored if optimized to R_PC.
1414 if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL)
1415 addend &= ~0x8000;
1416 // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into
1417 // call __tls_get_addr even if the symbol is non-preemptible.
1418 if (!(config->emachine == EM_HEXAGON &&
1419 (type == R_HEX_GD_PLT_B22_PCREL ||
1420 type == R_HEX_GD_PLT_B22_PCREL_X ||
1421 type == R_HEX_GD_PLT_B32_PCREL_X)))
1422 expr = fromPlt(expr);
1423 } else if (!isAbsoluteValue(sym)) {
1424 expr = target.adjustGotPcExpr(type, addend, relocatedAddr);
1425 }
1426 }
1427
1428 // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1429 // direct relocation on through.
1430 if (LLVM_UNLIKELY(isIfunc)__builtin_expect((bool)(isIfunc), false) && config->zIfuncNoplt) {
1431 std::lock_guard<std::mutex> lock(relocMutex);
1432 sym.exportDynamic = true;
1433 mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);
1434 return;
1435 }
1436
1437 if (needsGot(expr)) {
1438 if (config->emachine == EM_MIPS) {
1439 // MIPS ABI has special rules to process GOT entries and doesn't
1440 // require relocation entries for them. A special case is TLS
1441 // relocations. In that case dynamic loader applies dynamic
1442 // relocations to initialize TLS GOT entries.
1443 // See "Global Offset Table" in Chapter 5 in the following document
1444 // for detailed description:
1445 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1446 in.mipsGot->addEntry(*sec->file, sym, addend, expr);
1447 } else {
1448 sym.setFlags(NEEDS_GOT);
1449 }
1450 } else if (needsPlt(expr)) {
1451 sym.setFlags(NEEDS_PLT);
1452 } else if (LLVM_UNLIKELY(isIfunc)__builtin_expect((bool)(isIfunc), false)) {
1453 sym.setFlags(HAS_DIRECT_RELOC);
1454 }
1455
1456 processAux(expr, type, offset, sym, addend);
1457}
1458
1459// R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for
1460// General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is
1461// found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the
1462// instructions are generated by very old IBM XL compilers. Work around the
1463// issue by disabling GD/LD to IE/LE relaxation.
1464template <class RelTy>
1465static void checkPPC64TLSRelax(InputSectionBase &sec, ArrayRef<RelTy> rels) {
1466 // Skip if sec is synthetic (sec.file is null) or if sec has been marked.
1467 if (!sec.file || sec.file->ppc64DisableTLSRelax)
1468 return;
1469 bool hasGDLD = false;
1470 for (const RelTy &rel : rels) {
1471 RelType type = rel.getType(false);
1472 switch (type) {
1473 case R_PPC64_TLSGD:
1474 case R_PPC64_TLSLD:
1475 return; // Found a marker
1476 case R_PPC64_GOT_TLSGD16:
1477 case R_PPC64_GOT_TLSGD16_HA:
1478 case R_PPC64_GOT_TLSGD16_HI:
1479 case R_PPC64_GOT_TLSGD16_LO:
1480 case R_PPC64_GOT_TLSLD16:
1481 case R_PPC64_GOT_TLSLD16_HA:
1482 case R_PPC64_GOT_TLSLD16_HI:
1483 case R_PPC64_GOT_TLSLD16_LO:
1484 hasGDLD = true;
1485 break;
1486 }
1487 }
1488 if (hasGDLD) {
1489 sec.file->ppc64DisableTLSRelax = true;
1490 warn(toString(sec.file) +
1491 ": disable TLS relaxation due to R_PPC64_GOT_TLS* relocations without "
1492 "R_PPC64_TLSGD/R_PPC64_TLSLD relocations");
1493 }
1494}
1495
1496template <class ELFT, class RelTy>
1497void RelocationScanner::scan(ArrayRef<RelTy> rels) {
1498 // Not all relocations end up in Sec->Relocations, but a lot do.
1499 sec->relocations.reserve(rels.size());
1500
1501 if (config->emachine == EM_PPC64)
1502 checkPPC64TLSRelax<RelTy>(*sec, rels);
1503
1504 // For EhInputSection, OffsetGetter expects the relocations to be sorted by
1505 // r_offset. In rare cases (.eh_frame pieces are reordered by a linker
1506 // script), the relocations may be unordered.
1507 SmallVector<RelTy, 0> storage;
1508 if (isa<EhInputSection>(sec))
1509 rels = sortRels(rels, storage);
1510
1511 end = static_cast<const void *>(rels.end());
1512 for (auto i = rels.begin(); i != end;)
1513 scanOne<ELFT>(i);
1514
1515 // Sort relocations by offset for more efficient searching for
1516 // R_RISCV_PCREL_HI20 and R_PPC64_ADDR64.
1517 if (config->emachine == EM_RISCV ||
1518 (config->emachine == EM_PPC64 && sec->name == ".toc"))
1519 llvm::stable_sort(sec->relocations,
1520 [](const Relocation &lhs, const Relocation &rhs) {
1521 return lhs.offset < rhs.offset;
1522 });
1523}
1524
1525template <class ELFT> void RelocationScanner::scanSection(InputSectionBase &s) {
1526 sec = &s;
1527 getter = OffsetGetter(s);
1528 const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>();
1529 if (rels.areRelocsRel())
1530 scan<ELFT>(rels.rels);
1531 else
1532 scan<ELFT>(rels.relas);
1533}
1534
1535template <class ELFT> void elf::scanRelocations() {
1536 // Scan all relocations. Each relocation goes through a series of tests to
1537 // determine if it needs special treatment, such as creating GOT, PLT,
1538 // copy relocations, etc. Note that relocations for non-alloc sections are
1539 // directly processed by InputSection::relocateNonAlloc.
1540
1541 // Deterministic parallellism needs sorting relocations which is unsuitable
1542 // for -z nocombreloc. MIPS and PPC64 use global states which are not suitable
1543 // for parallelism.
1544 bool serial = !config->zCombreloc || config->emachine == EM_MIPS ||
1545 config->emachine == EM_PPC64;
1546 parallel::TaskGroup tg;
1547 for (ELFFileBase *f : ctx.objectFiles) {
1548 auto fn = [f]() {
1549 RelocationScanner scanner;
1550 for (InputSectionBase *s : f->getSections()) {
1551 if (s && s->kind() == SectionBase::Regular && s->isLive() &&
1552 (s->flags & SHF_ALLOC) &&
1553 !(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM))
1554 scanner.template scanSection<ELFT>(*s);
1555 }
1556 };
1557 if (serial)
1558 fn();
1559 else
1560 tg.execute(fn);
1561 }
1562
1563 // Both the main thread and thread pool index 0 use getThreadIndex()==0. Be
1564 // careful that they don't concurrently run scanSections. When serial is
1565 // true, fn() has finished at this point, so running execute is safe.
1566 tg.execute([] {
1567 RelocationScanner scanner;
1568 for (Partition &part : partitions) {
1569 for (EhInputSection *sec : part.ehFrame->sections)
1570 scanner.template scanSection<ELFT>(*sec);
1571 if (part.armExidx && part.armExidx->isLive())
1572 for (InputSection *sec : part.armExidx->exidxSections)
1573 scanner.template scanSection<ELFT>(*sec);
1574 }
1575 });
1576}
1577
1578static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {
1579 // Handle a reference to a non-preemptible ifunc. These are special in a
1580 // few ways:
1581 //
1582 // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1583 // a fixed value. But assuming that all references to the ifunc are
1584 // GOT-generating or PLT-generating, the handling of an ifunc is
1585 // relatively straightforward. We create a PLT entry in Iplt, which is
1586 // usually at the end of .plt, which makes an indirect call using a
1587 // matching GOT entry in igotPlt, which is usually at the end of .got.plt.
1588 // The GOT entry is relocated using an IRELATIVE relocation in relaIplt,
1589 // which is usually at the end of .rela.plt. Unlike most relocations in
1590 // .rela.plt, which may be evaluated lazily without -z now, dynamic
1591 // loaders evaluate IRELATIVE relocs eagerly, which means that for
1592 // IRELATIVE relocs only, GOT-generating relocations can point directly to
1593 // .got.plt without requiring a separate GOT entry.
1594 //
1595 // - Despite the fact that an ifunc does not have a fixed value, compilers
1596 // that are not passed -fPIC will assume that they do, and will emit
1597 // direct (non-GOT-generating, non-PLT-generating) relocations to the
1598 // symbol. This means that if a direct relocation to the symbol is
1599 // seen, the linker must set a value for the symbol, and this value must
1600 // be consistent no matter what type of reference is made to the symbol.
1601 // This can be done by creating a PLT entry for the symbol in the way
1602 // described above and making it canonical, that is, making all references
1603 // point to the PLT entry instead of the resolver. In lld we also store
1604 // the address of the PLT entry in the dynamic symbol table, which means
1605 // that the symbol will also have the same value in other modules.
1606 // Because the value loaded from the GOT needs to be consistent with
1607 // the value computed using a direct relocation, a non-preemptible ifunc
1608 // may end up with two GOT entries, one in .got.plt that points to the
1609 // address returned by the resolver and is used only by the PLT entry,
1610 // and another in .got that points to the PLT entry and is used by
1611 // GOT-generating relocations.
1612 //
1613 // - The fact that these symbols do not have a fixed value makes them an
1614 // exception to the general rule that a statically linked executable does
1615 // not require any form of dynamic relocation. To handle these relocations
1616 // correctly, the IRELATIVE relocations are stored in an array which a
1617 // statically linked executable's startup code must enumerate using the
1618 // linker-defined symbols __rela?_iplt_{start,end}.
1619 if (!sym.isGnuIFunc() || sym.isPreemptible || config->zIfuncNoplt)
1620 return false;
1621 // Skip unreferenced non-preemptible ifunc.
1622 if (!(flags & (NEEDS_GOT | NEEDS_PLT | HAS_DIRECT_RELOC)))
1623 return true;
1624
1625 sym.isInIplt = true;
1626
1627 // Create an Iplt and the associated IRELATIVE relocation pointing to the
1628 // original section/value pairs. For non-GOT non-PLT relocation case below, we
1629 // may alter section/value, so create a copy of the symbol to make
1630 // section/value fixed.
1631 auto *directSym = makeDefined(cast<Defined>(sym));
1632 directSym->allocateAux();
1633 addPltEntry(*in.iplt, *in.igotPlt, *in.relaIplt, target->iRelativeRel,
1634 *directSym);
1635 sym.allocateAux();
1636 symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx;
1637
1638 if (flags & HAS_DIRECT_RELOC) {
1639 // Change the value to the IPLT and redirect all references to it.
1640 auto &d = cast<Defined>(sym);
1641 d.section = in.iplt.get();
1642 d.value = d.getPltIdx() * target->ipltEntrySize;
1643 d.size = 0;
1644 // It's important to set the symbol type here so that dynamic loaders
1645 // don't try to call the PLT as if it were an ifunc resolver.
1646 d.type = STT_FUNC;
1647
1648 if (flags & NEEDS_GOT)
1649 addGotEntry(sym);
1650 } else if (flags & NEEDS_GOT) {
1651 // Redirect GOT accesses to point to the Igot.
1652 sym.gotInIgot = true;
1653 }
1654 return true;
1655}
1656
1657void elf::postScanRelocations() {
1658 auto fn = [](Symbol &sym) {
1659 auto flags = sym.flags.load(std::memory_order_relaxed);
1660 if (handleNonPreemptibleIfunc(sym, flags))
1661 return;
1662 if (!sym.needsDynReloc())
1663 return;
1664 sym.allocateAux();
1665
1666 if (flags & NEEDS_GOT)
1667 addGotEntry(sym);
1668 if (flags & NEEDS_PLT)
1669 addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym);
1670 if (flags & NEEDS_COPY) {
1671 if (sym.isObject()) {
1672 invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym))switch (config->ekind) { case ELF32LEKind: addCopyRelSymbol
<ELF32LE>(cast<SharedSymbol>(sym)); break; case ELF32BEKind
: addCopyRelSymbol<ELF32BE>(cast<SharedSymbol>(sym
)); break; case ELF64LEKind: addCopyRelSymbol<ELF64LE>(
cast<SharedSymbol>(sym)); break; case ELF64BEKind: addCopyRelSymbol
<ELF64BE>(cast<SharedSymbol>(sym)); break; default
: ::llvm::llvm_unreachable_internal("unknown config->ekind"
, "lld/ELF/Relocations.cpp", 1672); }
;
1673 // NEEDS_COPY is cleared for sym and its aliases so that in
1674 // later iterations aliases won't cause redundant copies.
1675 assert(!sym.hasFlag(NEEDS_COPY))(static_cast <bool> (!sym.hasFlag(NEEDS_COPY)) ? void (
0) : __assert_fail ("!sym.hasFlag(NEEDS_COPY)", "lld/ELF/Relocations.cpp"
, 1675, __extension__ __PRETTY_FUNCTION__))
;
1676 } else {
1677 assert(sym.isFunc() && sym.hasFlag(NEEDS_PLT))(static_cast <bool> (sym.isFunc() && sym.hasFlag
(NEEDS_PLT)) ? void (0) : __assert_fail ("sym.isFunc() && sym.hasFlag(NEEDS_PLT)"
, "lld/ELF/Relocations.cpp", 1677, __extension__ __PRETTY_FUNCTION__
))
;
1678 if (!sym.isDefined()) {
1679 replaceWithDefined(sym, *in.plt,
1680 target->pltHeaderSize +
1681 target->pltEntrySize * sym.getPltIdx(),
1682 0);
1683 sym.setFlags(NEEDS_COPY);
1684 if (config->emachine == EM_PPC) {
1685 // PPC32 canonical PLT entries are at the beginning of .glink
1686 cast<Defined>(sym).value = in.plt->headerSize;
1687 in.plt->headerSize += 16;
1688 cast<PPC32GlinkSection>(*in.plt).canonical_plts.push_back(&sym);
1689 }
1690 }
1691 }
1692 }
1693
1694 if (!sym.isTls())
1695 return;
1696 bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
1697
1698 if (flags & NEEDS_TLSDESC) {
1699 in.got->addTlsDescEntry(sym);
1700 mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
1701 target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym,
1702 target->tlsDescRel);
1703 }
1704 if (flags & NEEDS_TLSGD) {
1705 in.got->addDynTlsEntry(sym);
1706 uint64_t off = in.got->getGlobalDynOffset(sym);
1707 if (isLocalInExecutable)
1708 // Write one to the GOT slot.
1709 in.got->relocations.push_back(
1710 {R_ADDEND, target->symbolicRel, off, 1, &sym});
1711 else
1712 mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *in.got,
1713 off, sym);
1714
1715 // If the symbol is preemptible we need the dynamic linker to write
1716 // the offset too.
1717 uint64_t offsetOff = off + config->wordsize;
1718 if (sym.isPreemptible)
1719 mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *in.got,
1720 offsetOff, sym);
1721 else
1722 in.got->relocations.push_back(
1723 {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
1724 }
1725 if (flags & NEEDS_TLSGD_TO_IE) {
1726 in.got->addEntry(sym);
1727 mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *in.got,
1728 sym.getGotOffset(), sym);
1729 }
1730 if (flags & NEEDS_GOT_DTPREL) {
1731 in.got->addEntry(sym);
1732 in.got->relocations.push_back(
1733 {R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym});
1734 }
1735
1736 if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE))
1737 addTpOffsetGotEntry(sym);
1738 };
1739
1740 if (ctx.needsTlsLd.load(std::memory_order_relaxed) && in.got->addTlsIndex()) {
1741 static Undefined dummy(nullptr, "", STB_LOCAL, 0, 0);
1742 if (config->shared)
1743 mainPart->relaDyn->addReloc(
1744 {target->tlsModuleIndexRel, in.got.get(), in.got->getTlsIndexOff()});
1745 else
1746 in.got->relocations.push_back(
1747 {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &dummy});
1748 }
1749
1750 assert(symAux.size() == 1)(static_cast <bool> (symAux.size() == 1) ? void (0) : __assert_fail
("symAux.size() == 1", "lld/ELF/Relocations.cpp", 1750, __extension__
__PRETTY_FUNCTION__))
;
1751 for (Symbol *sym : symtab.getSymbols())
1752 fn(*sym);
1753
1754 // Local symbols may need the aforementioned non-preemptible ifunc and GOT
1755 // handling. They don't need regular PLT.
1756 for (ELFFileBase *file : ctx.objectFiles)
1757 for (Symbol *sym : file->getLocalSymbols())
1758 fn(*sym);
1759}
1760
1761static bool mergeCmp(const InputSection *a, const InputSection *b) {
1762 // std::merge requires a strict weak ordering.
1763 if (a->outSecOff < b->outSecOff)
1764 return true;
1765
1766 // FIXME dyn_cast<ThunkSection> is non-null for any SyntheticSection.
1767 if (a->outSecOff == b->outSecOff && a != b) {
1768 auto *ta = dyn_cast<ThunkSection>(a);
1769 auto *tb = dyn_cast<ThunkSection>(b);
1770
1771 // Check if Thunk is immediately before any specific Target
1772 // InputSection for example Mips LA25 Thunks.
1773 if (ta && ta->getTargetInputSection() == b)
1774 return true;
1775
1776 // Place Thunk Sections without specific targets before
1777 // non-Thunk Sections.
1778 if (ta && !tb && !ta->getTargetInputSection())
1779 return true;
1780 }
1781
1782 return false;
1783}
1784
1785// Call Fn on every executable InputSection accessed via the linker script
1786// InputSectionDescription::Sections.
1787static void forEachInputSectionDescription(
1788 ArrayRef<OutputSection *> outputSections,
1789 llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) {
1790 for (OutputSection *os : outputSections) {
1791 if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))
1792 continue;
1793 for (SectionCommand *bc : os->commands)
1794 if (auto *isd = dyn_cast<InputSectionDescription>(bc))
1795 fn(os, isd);
1796 }
1797}
1798
1799// Thunk Implementation
1800//
1801// Thunks (sometimes called stubs, veneers or branch islands) are small pieces
1802// of code that the linker inserts inbetween a caller and a callee. The thunks
1803// are added at link time rather than compile time as the decision on whether
1804// a thunk is needed, such as the caller and callee being out of range, can only
1805// be made at link time.
1806//
1807// It is straightforward to tell given the current state of the program when a
1808// thunk is needed for a particular call. The more difficult part is that
1809// the thunk needs to be placed in the program such that the caller can reach
1810// the thunk and the thunk can reach the callee; furthermore, adding thunks to
1811// the program alters addresses, which can mean more thunks etc.
1812//
1813// In lld we have a synthetic ThunkSection that can hold many Thunks.
1814// The decision to have a ThunkSection act as a container means that we can
1815// more easily handle the most common case of a single block of contiguous
1816// Thunks by inserting just a single ThunkSection.
1817//
1818// The implementation of Thunks in lld is split across these areas
1819// Relocations.cpp : Framework for creating and placing thunks
1820// Thunks.cpp : The code generated for each supported thunk
1821// Target.cpp : Target specific hooks that the framework uses to decide when
1822// a thunk is used
1823// Synthetic.cpp : Implementation of ThunkSection
1824// Writer.cpp : Iteratively call framework until no more Thunks added
1825//
1826// Thunk placement requirements:
1827// Mips LA25 thunks. These must be placed immediately before the callee section
1828// We can assume that the caller is in range of the Thunk. These are modelled
1829// by Thunks that return the section they must precede with
1830// getTargetInputSection().
1831//
1832// ARM interworking and range extension thunks. These thunks must be placed
1833// within range of the caller. All implemented ARM thunks can always reach the
1834// callee as they use an indirect jump via a register that has no range
1835// restrictions.
1836//
1837// Thunk placement algorithm:
1838// For Mips LA25 ThunkSections; the placement is explicit, it has to be before
1839// getTargetInputSection().
1840//
1841// For thunks that must be placed within range of the caller there are many
1842// possible choices given that the maximum range from the caller is usually
1843// much larger than the average InputSection size. Desirable properties include:
1844// - Maximize reuse of thunks by multiple callers
1845// - Minimize number of ThunkSections to simplify insertion
1846// - Handle impact of already added Thunks on addresses
1847// - Simple to understand and implement
1848//
1849// In lld for the first pass, we pre-create one or more ThunkSections per
1850// InputSectionDescription at Target specific intervals. A ThunkSection is
1851// placed so that the estimated end of the ThunkSection is within range of the
1852// start of the InputSectionDescription or the previous ThunkSection. For
1853// example:
1854// InputSectionDescription
1855// Section 0
1856// ...
1857// Section N
1858// ThunkSection 0
1859// Section N + 1
1860// ...
1861// Section N + K
1862// Thunk Section 1
1863//
1864// The intention is that we can add a Thunk to a ThunkSection that is well
1865// spaced enough to service a number of callers without having to do a lot
1866// of work. An important principle is that it is not an error if a Thunk cannot
1867// be placed in a pre-created ThunkSection; when this happens we create a new
1868// ThunkSection placed next to the caller. This allows us to handle the vast
1869// majority of thunks simply, but also handle rare cases where the branch range
1870// is smaller than the target specific spacing.
1871//
1872// The algorithm is expected to create all the thunks that are needed in a
1873// single pass, with a small number of programs needing a second pass due to
1874// the insertion of thunks in the first pass increasing the offset between
1875// callers and callees that were only just in range.
1876//
1877// A consequence of allowing new ThunkSections to be created outside of the
1878// pre-created ThunkSections is that in rare cases calls to Thunks that were in
1879// range in pass K, are out of range in some pass > K due to the insertion of
1880// more Thunks in between the caller and callee. When this happens we retarget
1881// the relocation back to the original target and create another Thunk.
1882
1883// Remove ThunkSections that are empty, this should only be the initial set
1884// precreated on pass 0.
1885
1886// Insert the Thunks for OutputSection OS into their designated place
1887// in the Sections vector, and recalculate the InputSection output section
1888// offsets.
1889// This may invalidate any output section offsets stored outside of InputSection
1890void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {
1891 forEachInputSectionDescription(
1892 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
1893 if (isd->thunkSections.empty())
1894 return;
1895
1896 // Remove any zero sized precreated Thunks.
1897 llvm::erase_if(isd->thunkSections,
1898 [](const std::pair<ThunkSection *, uint32_t> &ts) {
1899 return ts.first->getSize() == 0;
1900 });
1901
1902 // ISD->ThunkSections contains all created ThunkSections, including
1903 // those inserted in previous passes. Extract the Thunks created this
1904 // pass and order them in ascending outSecOff.
1905 std::vector<ThunkSection *> newThunks;
1906 for (std::pair<ThunkSection *, uint32_t> ts : isd->thunkSections)
1907 if (ts.second == pass)
1908 newThunks.push_back(ts.first);
1909 llvm::stable_sort(newThunks,
1910 [](const ThunkSection *a, const ThunkSection *b) {
1911 return a->outSecOff < b->outSecOff;
1912 });
1913
1914 // Merge sorted vectors of Thunks and InputSections by outSecOff
1915 SmallVector<InputSection *, 0> tmp;
1916 tmp.reserve(isd->sections.size() + newThunks.size());
1917
1918 std::merge(isd->sections.begin(), isd->sections.end(),
1919 newThunks.begin(), newThunks.end(), std::back_inserter(tmp),
1920 mergeCmp);
1921
1922 isd->sections = std::move(tmp);
1923 });
1924}
1925
1926static int64_t getPCBias(RelType type) {
1927 if (config->emachine != EM_ARM)
1928 return 0;
1929 switch (type) {
1930 case R_ARM_THM_JUMP19:
1931 case R_ARM_THM_JUMP24:
1932 case R_ARM_THM_CALL:
1933 return 4;
1934 default:
1935 return 8;
1936 }
1937}
1938
1939// Find or create a ThunkSection within the InputSectionDescription (ISD) that
1940// is in range of Src. An ISD maps to a range of InputSections described by a
1941// linker script section pattern such as { .text .text.* }.
1942ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
1943 InputSection *isec,
1944 InputSectionDescription *isd,
1945 const Relocation &rel,
1946 uint64_t src) {
1947 // See the comment in getThunk for -pcBias below.
1948 const int64_t pcBias = getPCBias(rel.type);
1949 for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {
1950 ThunkSection *ts = tp.first;
1951 uint64_t tsBase = os->addr + ts->outSecOff - pcBias;
1952 uint64_t tsLimit = tsBase + ts->getSize();
1953 if (target->inBranchRange(rel.type, src,
1954 (src > tsLimit) ? tsBase : tsLimit))
1955 return ts;
1956 }
1957
1958 // No suitable ThunkSection exists. This can happen when there is a branch
1959 // with lower range than the ThunkSection spacing or when there are too
1960 // many Thunks. Create a new ThunkSection as close to the InputSection as
1961 // possible. Error if InputSection is so large we cannot place ThunkSection
1962 // anywhere in Range.
1963 uint64_t thunkSecOff = isec->outSecOff;
1964 if (!target->inBranchRange(rel.type, src,
1965 os->addr + thunkSecOff + rel.addend)) {
1966 thunkSecOff = isec->outSecOff + isec->getSize();
1967 if (!target->inBranchRange(rel.type, src,
1968 os->addr + thunkSecOff + rel.addend))
1969 fatal("InputSection too large for range extension thunk " +
1970 isec->getObjMsg(src - (os->addr + isec->outSecOff)));
1971 }
1972 return addThunkSection(os, isd, thunkSecOff);
1973}
1974
1975// Add a Thunk that needs to be placed in a ThunkSection that immediately
1976// precedes its Target.
1977ThunkSection *ThunkCreator::getISThunkSec(InputSection *isec) {
1978 ThunkSection *ts = thunkedSections.lookup(isec);
1979 if (ts)
1980 return ts;
1981
1982 // Find InputSectionRange within Target Output Section (TOS) that the
1983 // InputSection (IS) that we need to precede is in.
1984 OutputSection *tos = isec->getParent();
1985 for (SectionCommand *bc : tos->commands) {
1986 auto *isd = dyn_cast<InputSectionDescription>(bc);
1987 if (!isd || isd->sections.empty())
1988 continue;
1989
1990 InputSection *first = isd->sections.front();
1991 InputSection *last = isd->sections.back();
1992
1993 if (isec->outSecOff < first->outSecOff || last->outSecOff < isec->outSecOff)
1994 continue;
1995
1996 ts = addThunkSection(tos, isd, isec->outSecOff);
1997 thunkedSections[isec] = ts;
1998 return ts;
1999 }
2000
2001 return nullptr;
2002}
2003
2004// Create one or more ThunkSections per OS that can be used to place Thunks.
2005// We attempt to place the ThunkSections using the following desirable
2006// properties:
2007// - Within range of the maximum number of callers
2008// - Minimise the number of ThunkSections
2009//
2010// We follow a simple but conservative heuristic to place ThunkSections at
2011// offsets that are multiples of a Target specific branch range.
2012// For an InputSectionDescription that is smaller than the range, a single
2013// ThunkSection at the end of the range will do.
2014//
2015// For an InputSectionDescription that is more than twice the size of the range,
2016// we place the last ThunkSection at range bytes from the end of the
2017// InputSectionDescription in order to increase the likelihood that the
2018// distance from a thunk to its target will be sufficiently small to
2019// allow for the creation of a short thunk.
2020void ThunkCreator::createInitialThunkSections(
2021 ArrayRef<OutputSection *> outputSections) {
2022 uint32_t thunkSectionSpacing = target->getThunkSectionSpacing();
2023
2024 forEachInputSectionDescription(
2025 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
2026 if (isd->sections.empty())
1
Taking false branch
2027 return;
2028
2029 uint32_t isdBegin = isd->sections.front()->outSecOff;
2030 uint32_t isdEnd =
2031 isd->sections.back()->outSecOff + isd->sections.back()->getSize();
2032 uint32_t lastThunkLowerBound = -1;
2033 if (isdEnd - isdBegin > thunkSectionSpacing * 2)
2
Assuming the condition is false
3
Taking false branch
2034 lastThunkLowerBound = isdEnd - thunkSectionSpacing;
2035
2036 uint32_t isecLimit;
4
'isecLimit' declared without an initial value
2037 uint32_t prevIsecLimit = isdBegin;
2038 uint32_t thunkUpperBound = isdBegin + thunkSectionSpacing;
2039
2040 for (const InputSection *isec : isd->sections) {
5
Assuming '__begin1' is equal to '__end1'
2041 isecLimit = isec->outSecOff + isec->getSize();
2042 if (isecLimit > thunkUpperBound) {
2043 addThunkSection(os, isd, prevIsecLimit);
2044 thunkUpperBound = prevIsecLimit + thunkSectionSpacing;
2045 }
2046 if (isecLimit > lastThunkLowerBound)
2047 break;
2048 prevIsecLimit = isecLimit;
2049 }
2050 addThunkSection(os, isd, isecLimit);
6
3rd function call argument is an uninitialized value
2051 });
2052}
2053
2054ThunkSection *ThunkCreator::addThunkSection(OutputSection *os,
2055 InputSectionDescription *isd,
2056 uint64_t off) {
2057 auto *ts = make<ThunkSection>(os, off);
2058 ts->partition = os->partition;
2059 if ((config->fixCortexA53Errata843419 || config->fixCortexA8) &&
2060 !isd->sections.empty()) {
2061 // The errata fixes are sensitive to addresses modulo 4 KiB. When we add
2062 // thunks we disturb the base addresses of sections placed after the thunks
2063 // this makes patches we have generated redundant, and may cause us to
2064 // generate more patches as different instructions are now in sensitive
2065 // locations. When we generate more patches we may force more branches to
2066 // go out of range, causing more thunks to be generated. In pathological
2067 // cases this can cause the address dependent content pass not to converge.
2068 // We fix this by rounding up the size of the ThunkSection to 4KiB, this
2069 // limits the insertion of a ThunkSection on the addresses modulo 4 KiB,
2070 // which means that adding Thunks to the section does not invalidate
2071 // errata patches for following code.
2072 // Rounding up the size to 4KiB has consequences for code-size and can
2073 // trip up linker script defined assertions. For example the linux kernel
2074 // has an assertion that what LLD represents as an InputSectionDescription
2075 // does not exceed 4 KiB even if the overall OutputSection is > 128 Mib.
2076 // We use the heuristic of rounding up the size when both of the following
2077 // conditions are true:
2078 // 1.) The OutputSection is larger than the ThunkSectionSpacing. This
2079 // accounts for the case where no single InputSectionDescription is
2080 // larger than the OutputSection size. This is conservative but simple.
2081 // 2.) The InputSectionDescription is larger than 4 KiB. This will prevent
2082 // any assertion failures that an InputSectionDescription is < 4 KiB
2083 // in size.
2084 uint64_t isdSize = isd->sections.back()->outSecOff +
2085 isd->sections.back()->getSize() -
2086 isd->sections.front()->outSecOff;
2087 if (os->size > target->getThunkSectionSpacing() && isdSize > 4096)
2088 ts->roundUpSizeForErrata = true;
2089 }
2090 isd->thunkSections.push_back({ts, pass});
2091 return ts;
2092}
2093
2094static bool isThunkSectionCompatible(InputSection *source,
2095 SectionBase *target) {
2096 // We can't reuse thunks in different loadable partitions because they might
2097 // not be loaded. But partition 1 (the main partition) will always be loaded.
2098 if (source->partition != target->partition)
2099 return target->partition == 1;
2100 return true;
2101}
2102
2103std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec,
2104 Relocation &rel, uint64_t src) {
2105 std::vector<Thunk *> *thunkVec = nullptr;
2106 // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled
2107 // out in the relocation addend. We compensate for the PC bias so that
2108 // an Arm and Thumb relocation to the same destination get the same keyAddend,
2109 // which is usually 0.
2110 const int64_t pcBias = getPCBias(rel.type);
2111 const int64_t keyAddend = rel.addend + pcBias;
2112
2113 // We use a ((section, offset), addend) pair to find the thunk position if
2114 // possible so that we create only one thunk for aliased symbols or ICFed
2115 // sections. There may be multiple relocations sharing the same (section,
2116 // offset + addend) pair. We may revert the relocation back to its original
2117 // non-Thunk target, so we cannot fold offset + addend.
2118 if (auto *d = dyn_cast<Defined>(rel.sym))
2119 if (!d->isInPlt() && d->section)
2120 thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value},
2121 keyAddend}];
2122 if (!thunkVec)
2123 thunkVec = &thunkedSymbols[{rel.sym, keyAddend}];
2124
2125 // Check existing Thunks for Sym to see if they can be reused
2126 for (Thunk *t : *thunkVec)
2127 if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) &&
2128 t->isCompatibleWith(*isec, rel) &&
2129 target->inBranchRange(rel.type, src,
2130 t->getThunkTargetSym()->getVA(-pcBias)))
2131 return std::make_pair(t, false);
2132
2133 // No existing compatible Thunk in range, create a new one
2134 Thunk *t = addThunk(*isec, rel);
2135 thunkVec->push_back(t);
2136 return std::make_pair(t, true);
2137}
2138
2139// Return true if the relocation target is an in range Thunk.
2140// Return false if the relocation is not to a Thunk. If the relocation target
2141// was originally to a Thunk, but is no longer in range we revert the
2142// relocation back to its original non-Thunk target.
2143bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) {
2144 if (Thunk *t = thunks.lookup(rel.sym)) {
2145 if (target->inBranchRange(rel.type, src, rel.sym->getVA(rel.addend)))
2146 return true;
2147 rel.sym = &t->destination;
2148 rel.addend = t->addend;
2149 if (rel.sym->isInPlt())
2150 rel.expr = toPlt(rel.expr);
2151 }
2152 return false;
2153}
2154
2155// Process all relocations from the InputSections that have been assigned
2156// to InputSectionDescriptions and redirect through Thunks if needed. The
2157// function should be called iteratively until it returns false.
2158//
2159// PreConditions:
2160// All InputSections that may need a Thunk are reachable from
2161// OutputSectionCommands.
2162//
2163// All OutputSections have an address and all InputSections have an offset
2164// within the OutputSection.
2165//
2166// The offsets between caller (relocation place) and callee
2167// (relocation target) will not be modified outside of createThunks().
2168//
2169// PostConditions:
2170// If return value is true then ThunkSections have been inserted into
2171// OutputSections. All relocations that needed a Thunk based on the information
2172// available to createThunks() on entry have been redirected to a Thunk. Note
2173// that adding Thunks changes offsets between caller and callee so more Thunks
2174// may be required.
2175//
2176// If return value is false then no more Thunks are needed, and createThunks has
2177// made no changes. If the target requires range extension thunks, currently
2178// ARM, then any future change in offset between caller and callee risks a
2179// relocation out of range error.
2180bool ThunkCreator::createThunks(uint32_t pass,
2181 ArrayRef<OutputSection *> outputSections) {
2182 this->pass = pass;
2183 bool addressesChanged = false;
2184
2185 if (pass == 0 && target->getThunkSectionSpacing())
2186 createInitialThunkSections(outputSections);
2187
2188 // Create all the Thunks and insert them into synthetic ThunkSections. The
2189 // ThunkSections are later inserted back into InputSectionDescriptions.
2190 // We separate the creation of ThunkSections from the insertion of the
2191 // ThunkSections as ThunkSections are not always inserted into the same
2192 // InputSectionDescription as the caller.
2193 forEachInputSectionDescription(
2194 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
2195 for (InputSection *isec : isd->sections)
2196 for (Relocation &rel : isec->relocations) {
2197 uint64_t src = isec->getVA(rel.offset);
2198
2199 // If we are a relocation to an existing Thunk, check if it is
2200 // still in range. If not then Rel will be altered to point to its
2201 // original target so another Thunk can be generated.
2202 if (pass > 0 && normalizeExistingThunk(rel, src))
2203 continue;
2204
2205 if (!target->needsThunk(rel.expr, rel.type, isec->file, src,
2206 *rel.sym, rel.addend))
2207 continue;
2208
2209 Thunk *t;
2210 bool isNew;
2211 std::tie(t, isNew) = getThunk(isec, rel, src);
2212
2213 if (isNew) {
2214 // Find or create a ThunkSection for the new Thunk
2215 ThunkSection *ts;
2216 if (auto *tis = t->getTargetInputSection())
2217 ts = getISThunkSec(tis);
2218 else
2219 ts = getISDThunkSec(os, isec, isd, rel, src);
2220 ts->addThunk(t);
2221 thunks[t->getThunkTargetSym()] = t;
2222 }
2223
2224 // Redirect relocation to Thunk, we never go via the PLT to a Thunk
2225 rel.sym = t->getThunkTargetSym();
2226 rel.expr = fromPlt(rel.expr);
2227
2228 // On AArch64 and PPC, a jump/call relocation may be encoded as
2229 // STT_SECTION + non-zero addend, clear the addend after
2230 // redirection.
2231 if (config->emachine != EM_MIPS)
2232 rel.addend = -getPCBias(rel.type);
2233 }
2234
2235 for (auto &p : isd->thunkSections)
2236 addressesChanged |= p.first->assignOffsets();
2237 });
2238
2239 for (auto &p : thunkedSections)
2240 addressesChanged |= p.second->assignOffsets();
2241
2242 // Merge all created synthetic ThunkSections back into OutputSection
2243 mergeThunks(outputSections);
2244 return addressesChanged;
2245}
2246
2247// The following aid in the conversion of call x@GDPLT to call __tls_get_addr
2248// hexagonNeedsTLSSymbol scans for relocations would require a call to
2249// __tls_get_addr.
2250// hexagonTLSSymbolUpdate rebinds the relocation to __tls_get_addr.
2251bool elf::hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections) {
2252 bool needTlsSymbol = false;
2253 forEachInputSectionDescription(
2254 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
2255 for (InputSection *isec : isd->sections)
2256 for (Relocation &rel : isec->relocations)
2257 if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
2258 needTlsSymbol = true;
2259 return;
2260 }
2261 });
2262 return needTlsSymbol;
2263}
2264
2265void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) {
2266 Symbol *sym = symtab.find("__tls_get_addr");
2267 if (!sym)
2268 return;
2269 bool needEntry = true;
2270 forEachInputSectionDescription(
2271 outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
2272 for (InputSection *isec : isd->sections)
2273 for (Relocation &rel : isec->relocations)
2274 if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
2275 if (needEntry) {
2276 sym->allocateAux();
2277 addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel,
2278 *sym);
2279 needEntry = false;
2280 }
2281 rel.sym = sym;
2282 }
2283 });
2284}
2285
2286template void elf::scanRelocations<ELF32LE>();
2287template void elf::scanRelocations<ELF32BE>();
2288template void elf::scanRelocations<ELF64LE>();
2289template void elf::scanRelocations<ELF64BE>();