blob: 1d15b600a5a9c10862525f8bf259d8299b1fe8bf [file] [log] [blame]
Rui Ueyama0fcdc732016-05-24 20:24:43 +00001//===- Relocations.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
George Rimar95912d02016-06-08 12:29:29 +000010// This file contains platform-independent functions to process relocations.
Rui Ueyama0fcdc732016-05-24 20:24:43 +000011// I'll describe the overview of this file here.
12//
13// Simple relocations are easy to handle for the linker. For example,
14// for R_X86_64_PC64 relocs, the linker just has to fix up locations
15// with the relative offsets to the target symbols. It would just be
16// reading records from relocation sections and applying them to output.
17//
18// But not all relocations are that easy to handle. For example, for
19// R_386_GOTOFF relocs, the linker has to create new GOT entries for
20// symbols if they don't exist, and fix up locations with GOT entry
21// offsets from the beginning of GOT section. So there is more than
22// fixing addresses in relocation processing.
23//
24// ELF defines a large number of complex relocations.
25//
26// The functions in this file analyze relocations and do whatever needs
27// to be done. It includes, but not limited to, the following.
28//
29// - create GOT/PLT entries
30// - create new relocations in .dynsym to let the dynamic linker resolve
31// them at runtime (since ELF supports dynamic linking, not all
32// relocations can be resolved at link-time)
33// - create COPY relocs and reserve space in .bss
34// - replace expensive relocs (in terms of runtime cost) with cheap ones
35// - error out infeasible combinations such as PIC and non-relative relocs
36//
37// Note that the functions in this file don't actually apply relocations
38// because it doesn't know about the output file nor the output file buffer.
39// It instead stores Relocation objects to InputSection's Relocations
40// vector to let it apply later in InputSection::writeTo.
41//
42//===----------------------------------------------------------------------===//
43
44#include "Relocations.h"
45#include "Config.h"
46#include "OutputSections.h"
47#include "SymbolTable.h"
48#include "Target.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000049#include "Thunks.h"
Eugene Leviant89837592016-10-06 09:45:04 +000050#include "Strings.h"
Rui Ueyama0fcdc732016-05-24 20:24:43 +000051
52#include "llvm/Support/Endian.h"
53#include "llvm/Support/raw_ostream.h"
54
55using namespace llvm;
56using namespace llvm::ELF;
57using namespace llvm::object;
58using namespace llvm::support::endian;
59
60namespace lld {
61namespace elf {
62
63static bool refersToGotEntry(RelExpr Expr) {
Simon Atanasyan41325112016-06-19 21:39:37 +000064 return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT_LOCAL_PAGE ||
Simon Atanasyanbed04bf2016-10-21 07:22:30 +000065 Expr == R_MIPS_GOT_OFF || Expr == R_MIPS_GOT_OFF32 ||
66 Expr == R_MIPS_TLSGD || Expr == R_MIPS_TLSLD ||
67 Expr == R_GOT_PAGE_PC || Expr == R_GOT_PC || Expr == R_GOT_FROM_END ||
68 Expr == R_TLSGD || Expr == R_TLSGD_PC || Expr == R_TLSDESC ||
69 Expr == R_TLSDESC_PAGE;
Rui Ueyama0fcdc732016-05-24 20:24:43 +000070}
71
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000072static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
73 // In case of MIPS GP-relative relocations always resolve to a definition
74 // in a regular input file, ignoring the one-definition rule. So we,
75 // for example, should not attempt to create a dynamic relocation even
76 // if the target symbol is preemptible. There are two two MIPS GP-relative
77 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
78 // can be against a preemptible symbol.
Simon Atanasyana26a1572016-06-10 12:26:09 +000079 // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000080 // relocation types occupy eight bit. In case of N64 ABI we extract first
81 // relocation from 3-in-1 packet because only the first relocation can
82 // be against a real symbol.
Simon Atanasyana26a1572016-06-10 12:26:09 +000083 if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16)
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000084 return false;
85 return Body.isPreemptible();
86}
87
Peter Smithfde62132016-09-23 13:54:48 +000088// This function is similar to the `handleTlsRelocation`. ARM and MIPS do not
89// support any relaxations for TLS relocations so by factoring out ARM and MIPS
90// handling in to the separate function we can simplify the code and do not
91// pollute `handleTlsRelocation` by ARM and MIPS `ifs` statements.
92// FIXME: The ARM implementation always adds the module index dynamic
93// relocation even for non-preemptible symbols in applications. For static
94// linking support we must either resolve the module index relocation at static
95// link time, or hard code the module index (1) for the application in the GOT.
Simon Atanasyan002e2442016-06-23 15:26:31 +000096template <class ELFT>
Peter Smithfde62132016-09-23 13:54:48 +000097static unsigned handleNoRelaxTlsRelocation(uint32_t Type, SymbolBody &Body,
98 InputSectionBase<ELFT> &C,
99 typename ELFT::uint Offset,
100 typename ELFT::uint Addend,
101 RelExpr Expr) {
102 if (Expr == R_MIPS_TLSLD || Expr == R_TLSLD_PC) {
103 if (Out<ELFT>::Got->addTlsIndex() &&
104 (Config->Pic || Config->EMachine == EM_ARM))
Simon Atanasyan002e2442016-06-23 15:26:31 +0000105 Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got,
106 Out<ELFT>::Got->getTlsIndexOff(), false,
107 nullptr, 0});
Rafael Espindola664c6522016-09-07 20:37:34 +0000108 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000109 return 1;
110 }
Peter Smithfde62132016-09-23 13:54:48 +0000111 typedef typename ELFT::uint uintX_t;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000112 if (Target->isTlsGlobalDynamicRel(Type)) {
Peter Smithfde62132016-09-23 13:54:48 +0000113 if (Out<ELFT>::Got->addDynTlsEntry(Body) &&
114 (Body.isPreemptible() || Config->EMachine == EM_ARM)) {
Simon Atanasyan002e2442016-06-23 15:26:31 +0000115 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
116 Out<ELFT>::RelaDyn->addReloc(
117 {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0});
Peter Smithfde62132016-09-23 13:54:48 +0000118 if (Body.isPreemptible())
119 Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got,
120 Off + (uintX_t)sizeof(uintX_t), false,
121 &Body, 0});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000122 }
Rafael Espindola664c6522016-09-07 20:37:34 +0000123 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000124 return 1;
125 }
126 return 0;
127}
128
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000129// Returns the number of relocations processed.
130template <class ELFT>
131static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body,
132 InputSectionBase<ELFT> &C,
133 typename ELFT::uint Offset,
134 typename ELFT::uint Addend, RelExpr Expr) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000135 if (!(C.Flags & SHF_ALLOC))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000136 return 0;
137
138 if (!Body.isTls())
139 return 0;
140
141 typedef typename ELFT::uint uintX_t;
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000142
Peter Smithfde62132016-09-23 13:54:48 +0000143 if (Config->EMachine == EM_MIPS || Config->EMachine == EM_ARM)
144 return handleNoRelaxTlsRelocation<ELFT>(Type, Body, C, Offset, Addend,
145 Expr);
Simon Atanasyan002e2442016-06-23 15:26:31 +0000146
Peter Smithd6486032016-10-20 09:59:26 +0000147 if ((Expr == R_TLSDESC || Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC_CALL) &&
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000148 Config->Shared) {
149 if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
150 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
151 Out<ELFT>::RelaDyn->addReloc(
152 {Target->TlsDescRel, Out<ELFT>::Got, Off, false, &Body, 0});
153 }
Peter Smithd6486032016-10-20 09:59:26 +0000154 if (Expr != R_TLSDESC_CALL)
Rafael Espindola664c6522016-09-07 20:37:34 +0000155 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000156 return 1;
157 }
158
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000159 if (Expr == R_TLSLD_PC || Expr == R_TLSLD) {
160 // Local-Dynamic relocs can be relaxed to Local-Exec.
161 if (!Config->Shared) {
162 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000163 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000164 return 2;
165 }
166 if (Out<ELFT>::Got->addTlsIndex())
167 Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Out<ELFT>::Got,
168 Out<ELFT>::Got->getTlsIndexOff(), false,
169 nullptr, 0});
Rafael Espindola664c6522016-09-07 20:37:34 +0000170 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000171 return 1;
172 }
173
174 // Local-Dynamic relocs can be relaxed to Local-Exec.
175 if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) {
176 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000177 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000178 return 1;
179 }
180
Peter Smithd6486032016-10-20 09:59:26 +0000181 if (Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC || Expr == R_TLSDESC_CALL ||
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000182 Target->isTlsGlobalDynamicRel(Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000183 if (Config->Shared) {
184 if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
185 uintX_t Off = Out<ELFT>::Got->getGlobalDynOffset(Body);
186 Out<ELFT>::RelaDyn->addReloc(
187 {Target->TlsModuleIndexRel, Out<ELFT>::Got, Off, false, &Body, 0});
Rafael Espindolaa8777c22016-06-08 21:31:59 +0000188
189 // If the symbol is preemptible we need the dynamic linker to write
190 // the offset too.
Simon Atanasyan9b861182016-06-10 12:26:39 +0000191 if (isPreemptible(Body, Type))
Rafael Espindolaa8777c22016-06-08 21:31:59 +0000192 Out<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Out<ELFT>::Got,
193 Off + (uintX_t)sizeof(uintX_t), false,
194 &Body, 0});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000195 }
Rafael Espindola664c6522016-09-07 20:37:34 +0000196 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000197 return 1;
198 }
199
200 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
201 // depending on the symbol being locally defined or not.
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000202 if (isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000203 C.Relocations.push_back(
Rafael Espindola69f54022016-06-04 23:22:34 +0000204 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
Rafael Espindola664c6522016-09-07 20:37:34 +0000205 Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000206 if (!Body.isInGot()) {
207 Out<ELFT>::Got->addEntry(Body);
208 Out<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, Out<ELFT>::Got,
209 Body.getGotOffset<ELFT>(), false, &Body,
210 0});
211 }
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000212 return Target->TlsGdRelaxSkip;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000213 }
214 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000215 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
Rafael Espindola69f54022016-06-04 23:22:34 +0000216 Offset, Addend, &Body});
Rafael Espindolaf807d472016-06-04 23:04:39 +0000217 return Target->TlsGdRelaxSkip;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000218 }
219
220 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
221 // defined.
222 if (Target->isTlsInitialExecRel(Type) && !Config->Shared &&
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000223 !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000224 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000225 {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000226 return 1;
227 }
228 return 0;
229}
230
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000231template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
232 return read32<E>(Loc) & 0xffff;
233}
234
235template <class RelTy>
236static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
237 switch (Rel->getType(Config->Mips64EL)) {
238 case R_MIPS_HI16:
239 return R_MIPS_LO16;
240 case R_MIPS_GOT16:
241 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
242 case R_MIPS_PCHI16:
243 return R_MIPS_PCLO16;
244 case R_MICROMIPS_HI16:
245 return R_MICROMIPS_LO16;
246 default:
247 return R_MIPS_NONE;
248 }
249}
250
251template <class ELFT, class RelTy>
252static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc,
253 SymbolBody &Sym, const RelTy *Rel,
254 const RelTy *End) {
255 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
256 uint32_t Type = getMipsPairType(Rel, Sym);
257
258 // Some MIPS relocations use addend calculated from addend of the relocation
259 // itself and addend of paired relocation. ABI requires to compute such
260 // combined addend in case of REL relocation record format only.
261 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
262 if (RelTy::IsRela || Type == R_MIPS_NONE)
263 return 0;
264
265 for (const RelTy *RI = Rel; RI != End; ++RI) {
266 if (RI->getType(Config->Mips64EL) != Type)
267 continue;
268 if (RI->getSymbol(Config->Mips64EL) != SymIndex)
269 continue;
270 const endianness E = ELFT::TargetEndianness;
271 return ((read32<E>(BufLoc) & 0xffff) << 16) +
272 readSignedLo16<E>(Buf + RI->r_offset);
273 }
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000274 warn("can't find matching " + getRelName(Type) + " relocation for " +
George Rimara4c7e742016-10-20 08:36:42 +0000275 getRelName(Rel->getType(Config->Mips64EL)));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000276 return 0;
277}
278
279// True if non-preemptable symbol always has the same value regardless of where
280// the DSO is loaded.
281template <class ELFT> static bool isAbsolute(const SymbolBody &Body) {
282 if (Body.isUndefined())
283 return !Body.isLocal() && Body.symbol()->isWeak();
284 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body))
285 return DR->Section == nullptr; // Absolute symbol.
286 return false;
287}
288
289static bool needsPlt(RelExpr Expr) {
Rafael Espindola12dc4462016-06-04 19:11:14 +0000290 return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT ||
Peter Smithfb05cd92016-07-08 16:10:27 +0000291 Expr == R_PLT_PAGE_PC || Expr == R_THUNK_PLT_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000292}
293
294// True if this expression is of the form Sym - X, where X is a position in the
295// file (PC, or GOT for example).
296static bool isRelExpr(RelExpr Expr) {
Rafael Espindola719f55d2016-09-06 13:57:15 +0000297 return Expr == R_PC || Expr == R_GOTREL || Expr == R_GOTREL_FROM_END ||
298 Expr == R_PAGE_PC || Expr == R_RELAX_GOT_PC || Expr == R_THUNK_PC ||
299 Expr == R_THUNK_PLT_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000300}
301
302template <class ELFT>
303static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
304 const SymbolBody &Body) {
305 // These expressions always compute a constant
306 if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF ||
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000307 E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF ||
308 E == R_MIPS_GOT_OFF32 || E == R_MIPS_TLSGD || E == R_GOT_PAGE_PC ||
309 E == R_GOT_PC || E == R_PLT_PC || E == R_TLSGD_PC || E == R_TLSGD ||
310 E == R_PPC_PLT_OPD || E == R_TLSDESC_CALL || E == R_TLSDESC_PAGE ||
311 E == R_HINT || E == R_THUNK_PC || E == R_THUNK_PLT_PC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000312 return true;
313
314 // These never do, except if the entire file is position dependent or if
315 // only the low bits are used.
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000316 if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000317 return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
318
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000319 if (isPreemptible(Body, Type))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000320 return false;
321
322 if (!Config->Pic)
323 return true;
324
325 bool AbsVal = isAbsolute<ELFT>(Body) || Body.isTls();
326 bool RelE = isRelExpr(E);
327 if (AbsVal && !RelE)
328 return true;
329 if (!AbsVal && RelE)
330 return true;
331
332 // Relative relocation to an absolute value. This is normally unrepresentable,
333 // but if the relocation refers to a weak undefined symbol, we allow it to
334 // resolve to the image base. This is a little strange, but it allows us to
335 // link function calls to such symbols. Normally such a call will be guarded
336 // with a comparison, which will load a zero from the GOT.
337 if (AbsVal && RelE) {
338 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
339 return true;
George Rimare6389d12016-06-08 12:22:26 +0000340 error("relocation " + getRelName(Type) +
341 " cannot refer to absolute symbol " + Body.getName());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000342 return true;
343 }
344
345 return Target->usesOnlyLowPageBits(Type);
346}
347
348static RelExpr toPlt(RelExpr Expr) {
349 if (Expr == R_PPC_OPD)
350 return R_PPC_PLT_OPD;
351 if (Expr == R_PC)
352 return R_PLT_PC;
Rafael Espindola12dc4462016-06-04 19:11:14 +0000353 if (Expr == R_PAGE_PC)
354 return R_PLT_PAGE_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000355 if (Expr == R_ABS)
356 return R_PLT;
357 return Expr;
358}
359
360static RelExpr fromPlt(RelExpr Expr) {
361 // We decided not to use a plt. Optimize a reference to the plt to a
362 // reference to the symbol itself.
363 if (Expr == R_PLT_PC)
364 return R_PC;
365 if (Expr == R_PPC_PLT_OPD)
366 return R_PPC_OPD;
367 if (Expr == R_PLT)
368 return R_ABS;
369 return Expr;
370}
371
372template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
373 typedef typename ELFT::uint uintX_t;
374
Rui Ueyama434b5612016-07-17 03:11:46 +0000375 uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000376 uintX_t SymValue = SS->Sym.st_value;
377 int TrailingZeros =
378 std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
379 return 1 << TrailingZeros;
380}
381
382// Reserve space in .bss for copy relocation.
383template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
384 typedef typename ELFT::uint uintX_t;
385 typedef typename ELFT::Sym Elf_Sym;
386
387 // Copy relocation against zero-sized symbol doesn't make sense.
388 uintX_t SymSize = SS->template getSize<ELFT>();
389 if (SymSize == 0)
Petr Hosek4071b1b2016-08-18 21:55:23 +0000390 fatal("cannot create a copy relocation for symbol " + SS->getName());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000391
Rui Ueyama424b4082016-06-17 01:18:46 +0000392 uintX_t Alignment = getAlignment(SS);
393 uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Alignment);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000394 Out<ELFT>::Bss->setSize(Off + SymSize);
Rui Ueyama424b4082016-06-17 01:18:46 +0000395 Out<ELFT>::Bss->updateAlignment(Alignment);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000396 uintX_t Shndx = SS->Sym.st_shndx;
397 uintX_t Value = SS->Sym.st_value;
398 // Look through the DSO's dynamic symbol table for aliases and create a
399 // dynamic symbol for each one. This causes the copy relocation to correctly
400 // interpose any aliases.
Rui Ueyama434b5612016-07-17 03:11:46 +0000401 for (const Elf_Sym &S : SS->file()->getElfSymbols(true)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000402 if (S.st_shndx != Shndx || S.st_value != Value)
403 continue;
404 auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>(
Rui Ueyama434b5612016-07-17 03:11:46 +0000405 Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable()))));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000406 if (!Alias)
407 continue;
408 Alias->OffsetInBss = Off;
409 Alias->NeedsCopyOrPltAddr = true;
410 Alias->symbol()->IsUsedInRegularObj = true;
411 }
412 Out<ELFT>::RelaDyn->addReloc(
413 {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
414}
415
416template <class ELFT>
Petr Hosek5b4f6c62016-08-22 19:01:53 +0000417static StringRef getSymbolName(const elf::ObjectFile<ELFT> &File,
418 SymbolBody &Body) {
419 if (Body.isLocal() && Body.getNameOffset())
420 return File.getStringTable().data() + Body.getNameOffset();
421 if (!Body.isLocal())
422 return Body.getName();
423 return "";
Petr Hosek4071b1b2016-08-18 21:55:23 +0000424}
425
426template <class ELFT>
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000427static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
George Rimar5c33b912016-05-25 14:31:37 +0000428 bool IsWrite, RelExpr Expr, uint32_t Type,
Rafael Espindolaf2956a32016-06-17 15:01:50 +0000429 const uint8_t *Data) {
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000430 bool Preemptible = isPreemptible(Body, Type);
George Rimar5c33b912016-05-25 14:31:37 +0000431 if (Body.isGnuIFunc()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000432 Expr = toPlt(Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000433 } else if (!Preemptible) {
434 if (needsPlt(Expr))
435 Expr = fromPlt(Expr);
George Rimarf10c8292016-06-01 16:45:30 +0000436 if (Expr == R_GOT_PC)
Rafael Espindolaf2956a32016-06-17 15:01:50 +0000437 Expr = Target->adjustRelaxExpr(Type, Data, Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000438 }
Peter Smithfb05cd92016-07-08 16:10:27 +0000439 Expr = Target->getThunkExpr(Expr, Type, File, Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000440
441 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body))
442 return Expr;
443
444 // This relocation would require the dynamic linker to write a value to read
445 // only memory. We can hack around it if we are producing an executable and
446 // the refered symbol can be preemepted to refer to the executable.
447 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
Petr Hosek5b4f6c62016-08-22 19:01:53 +0000448 StringRef Name = getSymbolName(File, Body);
George Rimara4c7e742016-10-20 08:36:42 +0000449 error("can't create dynamic relocation " + getRelName(Type) + " against " +
450 (Name.empty() ? "readonly segment" : "symbol " + Name));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000451 return Expr;
452 }
453 if (Body.getVisibility() != STV_DEFAULT) {
Petr Hosek4071b1b2016-08-18 21:55:23 +0000454 error("cannot preempt symbol " + Body.getName());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000455 return Expr;
456 }
457 if (Body.isObject()) {
458 // Produce a copy relocation.
459 auto *B = cast<SharedSymbol<ELFT>>(&Body);
460 if (!B->needsCopy())
461 addCopyRelSymbol(B);
462 return Expr;
463 }
464 if (Body.isFunc()) {
465 // This handles a non PIC program call to function in a shared library. In
466 // an ideal world, we could just report an error saying the relocation can
467 // overflow at runtime. In the real world with glibc, crt1.o has a
468 // R_X86_64_PC32 pointing to libc.so.
469 //
470 // The general idea on how to handle such cases is to create a PLT entry and
471 // use that as the function value.
472 //
473 // For the static linking part, we just return a plt expr and everything
474 // else will use the the PLT entry as the address.
475 //
476 // The remaining problem is making sure pointer equality still works. We
477 // need the help of the dynamic linker for that. We let it know that we have
478 // a direct reference to a so symbol by creating an undefined symbol with a
479 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
480 // the value of the symbol we created. This is true even for got entries, so
481 // pointer equality is maintained. To avoid an infinite loop, the only entry
482 // that points to the real function is a dedicated got entry used by the
483 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
484 // R_386_JMP_SLOT, etc).
485 Body.NeedsCopyOrPltAddr = true;
486 return toPlt(Expr);
487 }
Petr Hosek4071b1b2016-08-18 21:55:23 +0000488 error("symbol " + Body.getName() + " is missing type");
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000489
490 return Expr;
491}
492
493template <class ELFT, class RelTy>
494static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File,
495 const uint8_t *SectionData,
496 const RelTy *End, const RelTy &RI,
497 RelExpr Expr, SymbolBody &Body) {
498 typedef typename ELFT::uint uintX_t;
499
500 uint32_t Type = RI.getType(Config->Mips64EL);
501 uintX_t Addend = getAddend<ELFT>(RI);
502 const uint8_t *BufLoc = SectionData + RI.r_offset;
503 if (!RelTy::IsRela)
504 Addend += Target->getImplicitAddend(BufLoc, Type);
505 if (Config->EMachine == EM_MIPS) {
506 Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End);
507 if (Type == R_MIPS_LO16 && Expr == R_PC)
508 // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
509 // symbol. In that case we should use the following formula for
510 // calculation "AHL + GP - P + 4". Let's add 4 right here.
511 // For details see p. 4-19 at
512 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
513 Addend += 4;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000514 if (Expr == R_GOTREL) {
515 Addend -= MipsGPOffset;
516 if (Body.isLocal())
517 Addend += File.getMipsGp0();
518 }
519 }
520 if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
521 Addend += getPPC64TocBase();
522 return Addend;
523}
524
Eugene Leviantb380b242016-10-26 11:07:09 +0000525// Find symbol that encloses given offset. Used for error reporting.
526template <class ELFT>
527static DefinedRegular<ELFT> *getSymbolAt(InputSectionBase<ELFT> *S,
528 typename ELFT::uint Offset) {
529 for (SymbolBody *B : S->getFile()->getSymbols())
530 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B))
531 if (D->Value <= Offset && D->Value + D->Size > Offset && D->Section == S)
532 return D;
533
534 return nullptr;
535}
536
537template <class ELFT>
538static std::string getLocation(SymbolBody &Sym, InputSectionBase<ELFT> &S,
539 typename ELFT::uint Offset) {
540 ObjectFile<ELFT> *File = S.getFile();
541
542 // First check if we can get desired values from debugging information.
543 std::string LineInfo = File->getDIHelper()->getLineInfo(Offset);
544 if (!LineInfo.empty())
545 return LineInfo;
546
547 // If don't have STT_FILE typed symbol in object file then
548 // use object file name.
549 std::string SrcFile = File->SourceFile;
550 if (SrcFile.empty())
551 SrcFile = Sym.File ? getFilename(Sym.File) : getFilename(File);
552
Rui Ueyama5f7e6e62016-10-26 20:26:29 +0000553 // Find a symbol at a given location.
554 DefinedRegular<ELFT> *Sym = getSymbolAt(&S, Offset);
555 if (Sym && Sym->Type == STT_FUNC) {
556 StringRef Func = getSymbolName(*File, *Sym);
Rui Ueyama5ce977c2016-10-26 18:28:06 +0000557 return SrcFile + " (function " + maybeDemangle(Func) + ")";
Eugene Leviantb380b242016-10-26 11:07:09 +0000558 }
559
Rui Ueyama5f7e6e62016-10-26 20:26:29 +0000560 // If there's no symbol, print out the offset instead of a symbol name.
Eugene Leviantb380b242016-10-26 11:07:09 +0000561 return (SrcFile + " (" + S.Name + "+0x" + Twine::utohexstr(Offset) + ")")
562 .str();
563}
564
565template <class ELFT>
566static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &S,
567 typename ELFT::uint Offset) {
Eugene Leviant89837592016-10-06 09:45:04 +0000568 if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
569 return;
570
571 if (Config->Shared && Sym.symbol()->Visibility == STV_DEFAULT &&
572 Config->UnresolvedSymbols != UnresolvedPolicy::NoUndef)
573 return;
574
Rui Ueyama5ce977c2016-10-26 18:28:06 +0000575 std::string Msg = getLocation(Sym, S, Offset) + ": undefined symbol '" +
576 maybeDemangle(Sym.getName()) + "'";
Eugene Leviant89837592016-10-06 09:45:04 +0000577
Eugene Leviant89837592016-10-06 09:45:04 +0000578 if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn)
579 warn(Msg);
580 else
581 error(Msg);
582}
583
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000584// The reason we have to do this early scan is as follows
585// * To mmap the output file, we need to know the size
586// * For that, we need to know how many dynamic relocs we will have.
587// It might be possible to avoid this by outputting the file with write:
588// * Write the allocated output sections, computing addresses.
589// * Apply relocations, recording which ones require a dynamic reloc.
590// * Write the dynamic relocations.
591// * Write the rest of the file.
592// This would have some drawbacks. For example, we would only know if .rela.dyn
593// is needed after applying relocations. If it is, it will go after rw and rx
594// sections. Given that it is ro, we will need an extra PT_LOAD. This
595// complicates things for the dynamic linker and means we would have to reserve
596// space for the extra PT_LOAD even if we end up not using it.
597template <class ELFT, class RelTy>
Rui Ueyama2487f192016-05-25 03:40:02 +0000598static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000599 typedef typename ELFT::uint uintX_t;
600
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000601 bool IsWrite = C.Flags & SHF_WRITE;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000602
603 auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
604 Out<ELFT>::RelaDyn->addReloc(Reloc);
605 };
606
607 const elf::ObjectFile<ELFT> &File = *C.getFile();
Rafael Espindolac7e1e032016-09-12 13:13:53 +0000608 ArrayRef<uint8_t> SectionData = C.Data;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000609 const uint8_t *Buf = SectionData.begin();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000610
Rafael Espindola3abe3aa2016-07-21 21:15:32 +0000611 ArrayRef<EhSectionPiece> Pieces;
612 if (auto *Eh = dyn_cast<EhInputSection<ELFT>>(&C))
613 Pieces = Eh->Pieces;
614
615 ArrayRef<EhSectionPiece>::iterator PieceI = Pieces.begin();
616 ArrayRef<EhSectionPiece>::iterator PieceE = Pieces.end();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000617
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000618 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
619 const RelTy &RI = *I;
620 SymbolBody &Body = File.getRelocTargetSym(RI);
621 uint32_t Type = RI.getType(Config->Mips64EL);
622
George Rimara4c7e742016-10-20 08:36:42 +0000623 // We only report undefined symbols if they are referenced somewhere in the
624 // code.
Eugene Leviant89837592016-10-06 09:45:04 +0000625 if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak())
Eugene Leviantb380b242016-10-26 11:07:09 +0000626 reportUndefined(Body, C, RI.r_offset);
Eugene Leviant89837592016-10-06 09:45:04 +0000627
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000628 RelExpr Expr = Target->getRelExpr(Type, Body);
Rafael Espindola678844e2016-06-17 15:42:36 +0000629 bool Preemptible = isPreemptible(Body, Type);
630 Expr = adjustExpr(File, Body, IsWrite, Expr, Type, Buf + RI.r_offset);
631 if (HasError)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000632 continue;
633
Rui Ueyama809d8e22016-06-23 04:33:42 +0000634 // Skip a relocation that points to a dead piece
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000635 // in a eh_frame section.
636 while (PieceI != PieceE &&
637 (PieceI->InputOff + PieceI->size() <= RI.r_offset))
638 ++PieceI;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000639
640 // Compute the offset of this section in the output section. We do it here
641 // to try to compute it only once.
642 uintX_t Offset;
643 if (PieceI != PieceE) {
644 assert(PieceI->InputOff <= RI.r_offset && "Relocation not in any piece");
Rafael Espindola113860b2016-10-20 10:55:58 +0000645 if (PieceI->OutputOff == -1)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000646 continue;
647 Offset = PieceI->OutputOff + RI.r_offset - PieceI->InputOff;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000648 } else {
George Rimar3e6833b2016-08-19 15:46:28 +0000649 Offset = RI.r_offset;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000650 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000651
652 // This relocation does not require got entry, but it is relative to got and
653 // needs it to be created. Here we request for that.
Rafael Espindola79202c32016-08-31 23:24:11 +0000654 if (Expr == R_GOTONLY_PC || Expr == R_GOTONLY_PC_FROM_END ||
655 Expr == R_GOTREL || Expr == R_GOTREL_FROM_END || Expr == R_PPC_TOC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000656 Out<ELFT>::Got->HasGotOffRel = true;
657
658 uintX_t Addend = computeAddend(File, Buf, E, RI, Expr, Body);
659
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000660 if (unsigned Processed =
661 handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000662 I += (Processed - 1);
663 continue;
664 }
665
Peter Smithd6486032016-10-20 09:59:26 +0000666 // Ignore "hint" and TLS Descriptor call relocation because they are
667 // only markers for relaxation.
668 if (Expr == R_HINT || Expr == R_TLSDESC_CALL)
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000669 continue;
670
Peter Smithfb05cd92016-07-08 16:10:27 +0000671 if (needsPlt(Expr) || Expr == R_THUNK_ABS || Expr == R_THUNK_PC ||
672 Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) ||
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000673 !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000674 // If the relocation points to something in the file, we can process it.
675 bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body);
676
677 // If the output being produced is position independent, the final value
678 // is still not known. In that case we still need some help from the
679 // dynamic linker. We can however do better than just copying the incoming
680 // relocation. We can process some of it and and just ask the dynamic
681 // linker to add the load address.
682 if (!Constant)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000683 AddDyn({Target->RelativeRel, &C, Offset, true, &Body, Addend});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000684
685 // If the produced value is a constant, we just remember to write it
686 // when outputting this section. We also have to do it if the format
687 // uses Elf_Rel, since in that case the written value is the addend.
688 if (Constant || !RelTy::IsRela)
Rafael Espindola664c6522016-09-07 20:37:34 +0000689 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000690 } else {
691 // We don't know anything about the finaly symbol. Just ask the dynamic
692 // linker to handle the relocation for us.
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000693 AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000694 // MIPS ABI turns using of GOT and dynamic relocations inside out.
695 // While regular ABI uses dynamic relocations to fill up GOT entries
696 // MIPS ABI requires dynamic linker to fills up GOT entries using
697 // specially sorted dynamic symbol table. This affects even dynamic
698 // relocations against symbols which do not require GOT entries
699 // creation explicitly, i.e. do not have any GOT-relocations. So if
700 // a preemptible symbol has a dynamic relocation we anyway have
701 // to create a GOT entry for it.
702 // If a non-preemptible symbol has a dynamic relocation against it,
703 // dynamic linker takes it st_value, adds offset and writes down
704 // result of the dynamic relocation. In case of preemptible symbol
705 // dynamic linker performs symbol resolution, writes the symbol value
706 // to the GOT entry and reads the GOT entry when it needs to perform
707 // a dynamic relocation.
708 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
Simon Atanasyan41325112016-06-19 21:39:37 +0000709 if (Config->EMachine == EM_MIPS)
710 Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000711 continue;
712 }
713
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000714 // At this point we are done with the relocated position. Some relocations
715 // also require us to create a got or plt entry.
716
717 // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol.
718 if (needsPlt(Expr)) {
719 if (Body.isInPlt())
720 continue;
721 Out<ELFT>::Plt->addEntry(Body);
722
723 uint32_t Rel;
724 if (Body.isGnuIFunc() && !Preemptible)
725 Rel = Target->IRelativeRel;
726 else
727 Rel = Target->PltRel;
728
729 Out<ELFT>::GotPlt->addEntry(Body);
730 Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt,
731 Body.getGotPltOffset<ELFT>(), !Preemptible,
732 &Body, 0});
733 continue;
734 }
735
736 if (refersToGotEntry(Expr)) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000737 if (Config->EMachine == EM_MIPS) {
Simon Atanasyanaf52f6a2016-09-08 09:07:12 +0000738 // MIPS ABI has special rules to process GOT entries and doesn't
739 // require relocation entries for them. A special case is TLS
740 // relocations. In that case dynamic loader applies dynamic
741 // relocations to initialize TLS GOT entries.
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000742 // See "Global Offset Table" in Chapter 5 in the following document
743 // for detailed description:
744 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Simon Atanasyan41325112016-06-19 21:39:37 +0000745 Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000746 if (Body.isTls() && Body.isPreemptible())
Simon Atanasyan002e2442016-06-23 15:26:31 +0000747 AddDyn({Target->TlsGotRel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000748 false, &Body, 0});
Simon Atanasyan41325112016-06-19 21:39:37 +0000749 continue;
750 }
751
752 if (Body.isInGot())
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000753 continue;
754
Simon Atanasyan41325112016-06-19 21:39:37 +0000755 Out<ELFT>::Got->addEntry(Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000756 if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) {
757 uint32_t DynType;
758 if (Body.isTls())
759 DynType = Target->TlsGotRel;
760 else if (Preemptible)
761 DynType = Target->GotRel;
762 else
763 DynType = Target->RelativeRel;
764 AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
765 !Preemptible, &Body, 0});
766 }
767 continue;
768 }
769 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000770}
771
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000772template <class ELFT>
773void scanRelocations(InputSectionBase<ELFT> &S,
774 const typename ELFT::Shdr &RelSec) {
775 ELFFile<ELFT> &EObj = S.getFile()->getObj();
776 if (RelSec.sh_type == SHT_RELA)
777 scanRelocs(S, EObj.relas(&RelSec));
778 else
779 scanRelocs(S, EObj.rels(&RelSec));
780}
781
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000782template <class ELFT, class RelTy>
783static void createThunks(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
784 const elf::ObjectFile<ELFT> &File = *C.getFile();
785 for (const RelTy &Rel : Rels) {
786 SymbolBody &Body = File.getRelocTargetSym(Rel);
787 uint32_t Type = Rel.getType(Config->Mips64EL);
788 RelExpr Expr = Target->getRelExpr(Type, Body);
789 if (!isPreemptible(Body, Type) && needsPlt(Expr))
790 Expr = fromPlt(Expr);
791 Expr = Target->getThunkExpr(Expr, Type, File, Body);
792 // Some targets might require creation of thunks for relocations.
793 // Now we support only MIPS which requires LA25 thunk to call PIC
794 // code from non-PIC one, and ARM which requires interworking.
795 if (Expr == R_THUNK_ABS || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC) {
796 auto *Sec = cast<InputSection<ELFT>>(&C);
797 addThunk<ELFT>(Type, Body, *Sec);
798 }
799 }
800}
801
802template <class ELFT>
803void createThunks(InputSectionBase<ELFT> &S,
804 const typename ELFT::Shdr &RelSec) {
805 ELFFile<ELFT> &EObj = S.getFile()->getObj();
806 if (RelSec.sh_type == SHT_RELA)
807 createThunks(S, EObj.relas(&RelSec));
808 else
809 createThunks(S, EObj.rels(&RelSec));
810}
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000811
812template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &,
813 const ELF32LE::Shdr &);
814template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &,
815 const ELF32BE::Shdr &);
816template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &,
817 const ELF64LE::Shdr &);
818template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &,
819 const ELF64BE::Shdr &);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000820
821template void createThunks<ELF32LE>(InputSectionBase<ELF32LE> &,
822 const ELF32LE::Shdr &);
823template void createThunks<ELF32BE>(InputSectionBase<ELF32BE> &,
824 const ELF32BE::Shdr &);
825template void createThunks<ELF64LE>(InputSectionBase<ELF64LE> &,
826 const ELF64LE::Shdr &);
827template void createThunks<ELF64BE>(InputSectionBase<ELF64BE> &,
828 const ELF64BE::Shdr &);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000829}
830}