1 //===- Relocations.cpp ----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains platform-independent functions to process relocations.
11 // I'll describe the overview of this file here.
13 // Simple relocations are easy to handle for the linker. For example,
14 // for R_X86_64_PC64 relocs, the linker just has to fix up locations
15 // with the relative offsets to the target symbols. It would just be
16 // reading records from relocation sections and applying them to output.
18 // But not all relocations are that easy to handle. For example, for
19 // R_386_GOTOFF relocs, the linker has to create new GOT entries for
20 // symbols if they don't exist, and fix up locations with GOT entry
21 // offsets from the beginning of GOT section. So there is more than
22 // fixing addresses in relocation processing.
24 // ELF defines a large number of complex relocations.
26 // The functions in this file analyze relocations and do whatever needs
27 // to be done. It includes, but not limited to, the following.
29 // - create GOT/PLT entries
30 // - create new relocations in .dynsym to let the dynamic linker resolve
31 // them at runtime (since ELF supports dynamic linking, not all
32 // relocations can be resolved at link-time)
33 // - create COPY relocs and reserve space in .bss
34 // - replace expensive relocs (in terms of runtime cost) with cheap ones
35 // - error out infeasible combinations such as PIC and non-relative relocs
37 // Note that the functions in this file don't actually apply relocations
38 // because it doesn't know about the output file nor the output file buffer.
39 // It instead stores Relocation objects to InputSection's Relocations
40 // vector to let it apply later in InputSection::writeTo.
42 //===----------------------------------------------------------------------===//
44 #include "Relocations.h"
46 #include "OutputSections.h"
47 #include "SymbolTable.h"
51 #include "llvm/Support/Endian.h"
52 #include "llvm/Support/raw_ostream.h"
55 using namespace llvm::ELF;
56 using namespace llvm::object;
57 using namespace llvm::support::endian;
62 static bool refersToGotEntry(RelExpr Expr) {
63 return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT_LOCAL_PAGE ||
64 Expr == R_MIPS_GOT_OFF || Expr == R_MIPS_TLSGD ||
65 Expr == R_MIPS_TLSLD || Expr == R_GOT_PAGE_PC || Expr == R_GOT_PC ||
66 Expr == R_GOT_FROM_END || Expr == R_TLSGD || Expr == R_TLSGD_PC ||
67 Expr == R_TLSDESC || Expr == R_TLSDESC_PAGE;
70 static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
71 // In case of MIPS GP-relative relocations always resolve to a definition
72 // in a regular input file, ignoring the one-definition rule. So we,
73 // for example, should not attempt to create a dynamic relocation even
74 // if the target symbol is preemptible. There are two two MIPS GP-relative
75 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
76 // can be against a preemptible symbol.
77 // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
78 // relocation types occupy eight bit. In case of N64 ABI we extract first
79 // relocation from 3-in-1 packet because only the first relocation can
80 // be against a real symbol.
81 if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16)
83 return Body.isPreemptible();
86 // This function is similar to the `handleTlsRelocation`. MIPS does not support
87 // any relaxations for TLS relocations so by factoring out MIPS handling into
88 // the separate function we can simplify the code and does not pollute
89 // `handleTlsRelocation` by MIPS `ifs` statements.
92 handleMipsTlsRelocation(uint32_t Type, SymbolBody &Body,
93 InputSectionBase<ELFT> &C, typename ELFT::uint Offset,
94 typename ELFT::uint Addend, RelExpr Expr) {
95 if (Expr == R_MIPS_TLSLD) {
96 if (Out<ELFT>::Got->addTlsIndex())
97 Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got,
98 Out<ELFT>::Got->getTlsIndexOff(), false,
100 C.Relocations.push_back({Expr, Type, &C, Offset, Addend, &Body});
103 if (Target->isTlsGlobalDynamicRel(Type)) {
104 if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
105 typedef typename ELFT::uint uintX_t;
106 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
107 Out<ELFT>::RelaDyn->addReloc(
108 {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0});
109 Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got,
110 Off + (uintX_t)sizeof(uintX_t), false,
113 C.Relocations.push_back({Expr, Type, &C, Offset, Addend, &Body});
119 // Returns the number of relocations processed.
120 template <class ELFT>
121 static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body,
122 InputSectionBase<ELFT> &C,
123 typename ELFT::uint Offset,
124 typename ELFT::uint Addend, RelExpr Expr) {
125 if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC))
131 typedef typename ELFT::uint uintX_t;
133 if (Config->EMachine == EM_MIPS)
134 return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
136 if ((Expr == R_TLSDESC || Expr == R_TLSDESC_PAGE || Expr == R_HINT) &&
138 if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
139 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
140 Out<ELFT>::RelaDyn->addReloc(
141 {Target->TlsDescRel, Out<ELFT>::Got, Off, false, &Body, 0});
144 C.Relocations.push_back({Expr, Type, &C, Offset, Addend, &Body});
148 if (Expr == R_TLSLD_PC || Expr == R_TLSLD) {
149 // Local-Dynamic relocs can be relaxed to Local-Exec.
150 if (!Config->Shared) {
151 C.Relocations.push_back(
152 {R_RELAX_TLS_LD_TO_LE, Type, &C, Offset, Addend, &Body});
155 if (Out<ELFT>::Got->addTlsIndex())
156 Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got,
157 Out<ELFT>::Got->getTlsIndexOff(), false,
159 C.Relocations.push_back({Expr, Type, &C, Offset, Addend, &Body});
163 // Local-Dynamic relocs can be relaxed to Local-Exec.
164 if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) {
165 C.Relocations.push_back(
166 {R_RELAX_TLS_LD_TO_LE, Type, &C, Offset, Addend, &Body});
170 if (Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC || Expr == R_HINT ||
171 Target->isTlsGlobalDynamicRel(Type)) {
172 if (Config->Shared) {
173 if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
174 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
175 Out<ELFT>::RelaDyn->addReloc(
176 {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0});
178 // If the symbol is preemptible we need the dynamic linker to write
180 if (isPreemptible(Body, Type))
181 Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got,
182 Off + (uintX_t)sizeof(uintX_t), false,
185 C.Relocations.push_back({Expr, Type, &C, Offset, Addend, &Body});
189 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
190 // depending on the symbol being locally defined or not.
191 if (isPreemptible(Body, Type)) {
192 C.Relocations.push_back(
193 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
194 &C, Offset, Addend, &Body});
195 if (!Body.isInGot()) {
196 Out<ELFT>::Got->addEntry(Body);
197 Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got,
198 Body.getGotOffset<ELFT>(), false, &Body,
201 return Target->TlsGdRelaxSkip;
203 C.Relocations.push_back(
204 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type, &C,
205 Offset, Addend, &Body});
206 return Target->TlsGdRelaxSkip;
209 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
211 if (Target->isTlsInitialExecRel(Type) && !Config->Shared &&
212 !isPreemptible(Body, Type)) {
213 C.Relocations.push_back(
214 {R_RELAX_TLS_IE_TO_LE, Type, &C, Offset, Addend, &Body});
220 template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
221 return read32<E>(Loc) & 0xffff;
224 template <class RelTy>
225 static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
226 switch (Rel->getType(Config->Mips64EL)) {
230 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
232 return R_MIPS_PCLO16;
233 case R_MICROMIPS_HI16:
234 return R_MICROMIPS_LO16;
240 template <class ELFT, class RelTy>
241 static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc,
242 SymbolBody &Sym, const RelTy *Rel,
244 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
245 uint32_t Type = getMipsPairType(Rel, Sym);
247 // Some MIPS relocations use addend calculated from addend of the relocation
248 // itself and addend of paired relocation. ABI requires to compute such
249 // combined addend in case of REL relocation record format only.
250 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
251 if (RelTy::IsRela || Type == R_MIPS_NONE)
254 for (const RelTy *RI = Rel; RI != End; ++RI) {
255 if (RI->getType(Config->Mips64EL) != Type)
257 if (RI->getSymbol(Config->Mips64EL) != SymIndex)
259 const endianness E = ELFT::TargetEndianness;
260 return ((read32<E>(BufLoc) & 0xffff) << 16) +
261 readSignedLo16<E>(Buf + RI->r_offset);
263 warning("can't find matching " + getRelName(Type) + " relocation for " +
264 getRelName(Rel->getType(Config->Mips64EL)));
268 // True if non-preemptable symbol always has the same value regardless of where
269 // the DSO is loaded.
270 template <class ELFT> static bool isAbsolute(const SymbolBody &Body) {
271 if (Body.isUndefined())
272 return !Body.isLocal() && Body.symbol()->isWeak();
273 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body))
274 return DR->Section == nullptr; // Absolute symbol.
278 static bool needsPlt(RelExpr Expr) {
279 return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT ||
280 Expr == R_PLT_PAGE_PC || Expr == R_THUNK_PLT_PC;
283 // True if this expression is of the form Sym - X, where X is a position in the
284 // file (PC, or GOT for example).
285 static bool isRelExpr(RelExpr Expr) {
286 return Expr == R_PC || Expr == R_GOTREL || Expr == R_PAGE_PC ||
287 Expr == R_RELAX_GOT_PC || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC;
290 template <class ELFT>
291 static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
292 const SymbolBody &Body) {
293 // These expressions always compute a constant
294 if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF ||
295 E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF || E == R_MIPS_TLSGD ||
296 E == R_GOT_PAGE_PC || E == R_GOT_PC || E == R_PLT_PC || E == R_TLSGD_PC ||
297 E == R_TLSGD || E == R_PPC_PLT_OPD || E == R_TLSDESC_PAGE ||
298 E == R_HINT || E == R_THUNK_PC || E == R_THUNK_PLT_PC)
301 // These never do, except if the entire file is position dependent or if
302 // only the low bits are used.
303 if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
304 return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
306 if (isPreemptible(Body, Type))
312 bool AbsVal = isAbsolute<ELFT>(Body) || Body.isTls();
313 bool RelE = isRelExpr(E);
319 // Relative relocation to an absolute value. This is normally unrepresentable,
320 // but if the relocation refers to a weak undefined symbol, we allow it to
321 // resolve to the image base. This is a little strange, but it allows us to
322 // link function calls to such symbols. Normally such a call will be guarded
323 // with a comparison, which will load a zero from the GOT.
324 if (AbsVal && RelE) {
325 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
327 error("relocation " + getRelName(Type) +
328 " cannot refer to absolute symbol " + Body.getName());
332 return Target->usesOnlyLowPageBits(Type);
335 static RelExpr toPlt(RelExpr Expr) {
336 if (Expr == R_PPC_OPD)
337 return R_PPC_PLT_OPD;
340 if (Expr == R_PAGE_PC)
341 return R_PLT_PAGE_PC;
347 static RelExpr fromPlt(RelExpr Expr) {
348 // We decided not to use a plt. Optimize a reference to the plt to a
349 // reference to the symbol itself.
350 if (Expr == R_PLT_PC)
352 if (Expr == R_PPC_PLT_OPD)
359 template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
360 typedef typename ELFT::uint uintX_t;
362 uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign;
363 uintX_t SymValue = SS->Sym.st_value;
365 std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
366 return 1 << TrailingZeros;
369 // Reserve space in .bss for copy relocation.
370 template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
371 typedef typename ELFT::uint uintX_t;
372 typedef typename ELFT::Sym Elf_Sym;
374 // Copy relocation against zero-sized symbol doesn't make sense.
375 uintX_t SymSize = SS->template getSize<ELFT>();
377 fatal("cannot create a copy relocation for " + SS->getName());
379 uintX_t Alignment = getAlignment(SS);
380 uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Alignment);
381 Out<ELFT>::Bss->setSize(Off + SymSize);
382 Out<ELFT>::Bss->updateAlignment(Alignment);
383 uintX_t Shndx = SS->Sym.st_shndx;
384 uintX_t Value = SS->Sym.st_value;
385 // Look through the DSO's dynamic symbol table for aliases and create a
386 // dynamic symbol for each one. This causes the copy relocation to correctly
387 // interpose any aliases.
388 for (const Elf_Sym &S : SS->file()->getElfSymbols(true)) {
389 if (S.st_shndx != Shndx || S.st_value != Value)
391 auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>(
392 Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable()))));
395 Alias->OffsetInBss = Off;
396 Alias->NeedsCopyOrPltAddr = true;
397 Alias->symbol()->IsUsedInRegularObj = true;
399 Out<ELFT>::RelaDyn->addReloc(
400 {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
403 template <class ELFT>
404 static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
405 bool IsWrite, RelExpr Expr, uint32_t Type,
406 const uint8_t *Data) {
407 bool Preemptible = isPreemptible(Body, Type);
408 if (Body.isGnuIFunc()) {
410 } else if (!Preemptible) {
412 Expr = fromPlt(Expr);
413 if (Expr == R_GOT_PC)
414 Expr = Target->adjustRelaxExpr(Type, Data, Expr);
416 Expr = Target->getThunkExpr(Expr, Type, File, Body);
418 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body))
421 // This relocation would require the dynamic linker to write a value to read
422 // only memory. We can hack around it if we are producing an executable and
423 // the refered symbol can be preemepted to refer to the executable.
424 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
425 error("can't create dynamic relocation " + getRelName(Type) +
426 " against readonly segment");
429 if (Body.getVisibility() != STV_DEFAULT) {
430 error("cannot preempt symbol");
433 if (Body.isObject()) {
434 // Produce a copy relocation.
435 auto *B = cast<SharedSymbol<ELFT>>(&Body);
441 // This handles a non PIC program call to function in a shared library. In
442 // an ideal world, we could just report an error saying the relocation can
443 // overflow at runtime. In the real world with glibc, crt1.o has a
444 // R_X86_64_PC32 pointing to libc.so.
446 // The general idea on how to handle such cases is to create a PLT entry and
447 // use that as the function value.
449 // For the static linking part, we just return a plt expr and everything
450 // else will use the the PLT entry as the address.
452 // The remaining problem is making sure pointer equality still works. We
453 // need the help of the dynamic linker for that. We let it know that we have
454 // a direct reference to a so symbol by creating an undefined symbol with a
455 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
456 // the value of the symbol we created. This is true even for got entries, so
457 // pointer equality is maintained. To avoid an infinite loop, the only entry
458 // that points to the real function is a dedicated got entry used by the
459 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
460 // R_386_JMP_SLOT, etc).
461 Body.NeedsCopyOrPltAddr = true;
464 error("symbol is missing type");
469 template <class ELFT, class RelTy>
470 static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File,
471 const uint8_t *SectionData,
472 const RelTy *End, const RelTy &RI,
473 RelExpr Expr, SymbolBody &Body) {
474 typedef typename ELFT::uint uintX_t;
476 uint32_t Type = RI.getType(Config->Mips64EL);
477 uintX_t Addend = getAddend<ELFT>(RI);
478 const uint8_t *BufLoc = SectionData + RI.r_offset;
480 Addend += Target->getImplicitAddend(BufLoc, Type);
481 if (Config->EMachine == EM_MIPS) {
482 Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End);
483 if (Type == R_MIPS_LO16 && Expr == R_PC)
484 // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
485 // symbol. In that case we should use the following formula for
486 // calculation "AHL + GP - P + 4". Let's add 4 right here.
487 // For details see p. 4-19 at
488 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
490 if (Expr == R_GOTREL) {
491 Addend -= MipsGPOffset;
493 Addend += File.getMipsGp0();
496 if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
497 Addend += getPPC64TocBase();
501 // The reason we have to do this early scan is as follows
502 // * To mmap the output file, we need to know the size
503 // * For that, we need to know how many dynamic relocs we will have.
504 // It might be possible to avoid this by outputting the file with write:
505 // * Write the allocated output sections, computing addresses.
506 // * Apply relocations, recording which ones require a dynamic reloc.
507 // * Write the dynamic relocations.
508 // * Write the rest of the file.
509 // This would have some drawbacks. For example, we would only know if .rela.dyn
510 // is needed after applying relocations. If it is, it will go after rw and rx
511 // sections. Given that it is ro, we will need an extra PT_LOAD. This
512 // complicates things for the dynamic linker and means we would have to reserve
513 // space for the extra PT_LOAD even if we end up not using it.
514 template <class ELFT, class RelTy>
515 static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
516 typedef typename ELFT::uint uintX_t;
518 bool IsWrite = C.getSectionHdr()->sh_flags & SHF_WRITE;
520 auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
521 Out<ELFT>::RelaDyn->addReloc(Reloc);
524 const elf::ObjectFile<ELFT> &File = *C.getFile();
525 ArrayRef<uint8_t> SectionData = C.getSectionData();
526 const uint8_t *Buf = SectionData.begin();
527 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
528 const RelTy &RI = *I;
529 SymbolBody &Body = File.getRelocTargetSym(RI);
530 uint32_t Type = RI.getType(Config->Mips64EL);
532 RelExpr Expr = Target->getRelExpr(Type, Body);
533 bool Preemptible = isPreemptible(Body, Type);
534 Expr = adjustExpr(File, Body, IsWrite, Expr, Type, Buf + RI.r_offset);
538 // Skip a relocation that points to a dead piece
539 // in a mergeable section.
540 if (C.getOffset(RI.r_offset) == (uintX_t)-1)
543 // This relocation does not require got entry, but it is relative to got and
544 // needs it to be created. Here we request for that.
545 if (Expr == R_GOTONLY_PC || Expr == R_GOTREL || Expr == R_PPC_TOC)
546 Out<ELFT>::Got->HasGotOffRel = true;
548 uintX_t Addend = computeAddend(File, Buf, E, RI, Expr, Body);
550 if (unsigned Processed = handleTlsRelocation<ELFT>(
551 Type, Body, C, RI.r_offset, Addend, Expr)) {
552 I += (Processed - 1);
556 // Ignore "hint" relocation because it is for optional code optimization.
560 if (needsPlt(Expr) || Expr == R_THUNK_ABS || Expr == R_THUNK_PC ||
561 Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) ||
562 !isPreemptible(Body, Type)) {
563 // If the relocation points to something in the file, we can process it.
564 bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body);
566 // If the output being produced is position independent, the final value
567 // is still not known. In that case we still need some help from the
568 // dynamic linker. We can however do better than just copying the incoming
569 // relocation. We can process some of it and and just ask the dynamic
570 // linker to add the load address.
572 AddDyn({Target->RelativeRel, &C, RI.r_offset, true, &Body, Addend});
574 // If the produced value is a constant, we just remember to write it
575 // when outputting this section. We also have to do it if the format
576 // uses Elf_Rel, since in that case the written value is the addend.
577 if (Constant || !RelTy::IsRela)
578 C.Relocations.push_back({Expr, Type, &C, RI.r_offset, Addend, &Body});
580 // We don't know anything about the finaly symbol. Just ask the dynamic
581 // linker to handle the relocation for us.
582 AddDyn({Target->getDynRel(Type), &C, RI.r_offset, false, &Body, Addend});
583 // MIPS ABI turns using of GOT and dynamic relocations inside out.
584 // While regular ABI uses dynamic relocations to fill up GOT entries
585 // MIPS ABI requires dynamic linker to fills up GOT entries using
586 // specially sorted dynamic symbol table. This affects even dynamic
587 // relocations against symbols which do not require GOT entries
588 // creation explicitly, i.e. do not have any GOT-relocations. So if
589 // a preemptible symbol has a dynamic relocation we anyway have
590 // to create a GOT entry for it.
591 // If a non-preemptible symbol has a dynamic relocation against it,
592 // dynamic linker takes it st_value, adds offset and writes down
593 // result of the dynamic relocation. In case of preemptible symbol
594 // dynamic linker performs symbol resolution, writes the symbol value
595 // to the GOT entry and reads the GOT entry when it needs to perform
596 // a dynamic relocation.
597 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
598 if (Config->EMachine == EM_MIPS)
599 Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr);
603 // Some targets might require creation of thunks for relocations.
604 // Now we support only MIPS which requires LA25 thunk to call PIC
605 // code from non-PIC one, and ARM which requires interworking.
606 if (Expr == R_THUNK_ABS || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC) {
607 auto *Sec = cast<InputSection<ELFT>>(&C);
608 addThunk<ELFT>(Type, Body, *Sec);
611 // At this point we are done with the relocated position. Some relocations
612 // also require us to create a got or plt entry.
614 // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol.
615 if (needsPlt(Expr)) {
618 Out<ELFT>::Plt->addEntry(Body);
621 if (Body.isGnuIFunc() && !Preemptible)
622 Rel = Target->IRelativeRel;
624 Rel = Target->PltRel;
626 Out<ELFT>::GotPlt->addEntry(Body);
627 Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt,
628 Body.getGotPltOffset<ELFT>(), !Preemptible,
633 if (refersToGotEntry(Expr)) {
634 if (Config->EMachine == EM_MIPS) {
635 // MIPS ABI has special rules to process GOT entries
636 // and doesn't require relocation entries for them.
637 // See "Global Offset Table" in Chapter 5 in the following document
638 // for detailed description:
639 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
640 Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr);
642 AddDyn({Target->TlsGotRel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
643 !Preemptible, &Body, 0});
650 Out<ELFT>::Got->addEntry(Body);
651 if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) {
654 DynType = Target->TlsGotRel;
655 else if (Preemptible)
656 DynType = Target->GotRel;
658 DynType = Target->RelativeRel;
659 AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
660 !Preemptible, &Body, 0});
667 template <class ELFT> void scanRelocations(InputSection<ELFT> &C) {
668 typedef typename ELFT::Shdr Elf_Shdr;
670 // Scan all relocations. Each relocation goes through a series
671 // of tests to determine if it needs special treatment, such as
672 // creating GOT, PLT, copy relocations, etc.
673 // Note that relocations for non-alloc sections are directly
674 // processed by InputSection::relocateNonAlloc.
675 if (C.getSectionHdr()->sh_flags & SHF_ALLOC)
676 for (const Elf_Shdr *RelSec : C.RelocSections)
677 scanRelocations(C, *RelSec);
680 template <class ELFT>
681 void scanRelocations(InputSectionBase<ELFT> &S,
682 const typename ELFT::Shdr &RelSec) {
683 ELFFile<ELFT> &EObj = S.getFile()->getObj();
684 if (RelSec.sh_type == SHT_RELA)
685 scanRelocs(S, EObj.relas(&RelSec));
687 scanRelocs(S, EObj.rels(&RelSec));
690 template void scanRelocations<ELF32LE>(InputSection<ELF32LE> &);
691 template void scanRelocations<ELF32BE>(InputSection<ELF32BE> &);
692 template void scanRelocations<ELF64LE>(InputSection<ELF64LE> &);
693 template void scanRelocations<ELF64BE>(InputSection<ELF64BE> &);
695 template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &,
696 const ELF32LE::Shdr &);
697 template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &,
698 const ELF32BE::Shdr &);
699 template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &,
700 const ELF64LE::Shdr &);
701 template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &,
702 const ELF64BE::Shdr &);