blob: d111c75453d3d8aa07d7861d3109f6264e3a6676 [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
Rafael Espindolad598c812016-10-27 17:28:56 +0000289template <class ELFT> static bool isAbsoluteValue(const SymbolBody &Body) {
290 return isAbsolute<ELFT>(Body) || Body.isTls();
291}
292
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000293static bool needsPlt(RelExpr Expr) {
Rafael Espindola12dc4462016-06-04 19:11:14 +0000294 return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT ||
Peter Smithfb05cd92016-07-08 16:10:27 +0000295 Expr == R_PLT_PAGE_PC || Expr == R_THUNK_PLT_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000296}
297
298// True if this expression is of the form Sym - X, where X is a position in the
299// file (PC, or GOT for example).
300static bool isRelExpr(RelExpr Expr) {
Rafael Espindola719f55d2016-09-06 13:57:15 +0000301 return Expr == R_PC || Expr == R_GOTREL || Expr == R_GOTREL_FROM_END ||
302 Expr == R_PAGE_PC || Expr == R_RELAX_GOT_PC || Expr == R_THUNK_PC ||
303 Expr == R_THUNK_PLT_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000304}
305
306template <class ELFT>
307static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
308 const SymbolBody &Body) {
309 // These expressions always compute a constant
310 if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF ||
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000311 E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF ||
312 E == R_MIPS_GOT_OFF32 || E == R_MIPS_TLSGD || E == R_GOT_PAGE_PC ||
313 E == R_GOT_PC || E == R_PLT_PC || E == R_TLSGD_PC || E == R_TLSGD ||
314 E == R_PPC_PLT_OPD || E == R_TLSDESC_CALL || E == R_TLSDESC_PAGE ||
315 E == R_HINT || E == R_THUNK_PC || E == R_THUNK_PLT_PC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000316 return true;
317
318 // These never do, except if the entire file is position dependent or if
319 // only the low bits are used.
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000320 if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000321 return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
322
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000323 if (isPreemptible(Body, Type))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000324 return false;
325
326 if (!Config->Pic)
327 return true;
328
Rafael Espindolad598c812016-10-27 17:28:56 +0000329 bool AbsVal = isAbsoluteValue<ELFT>(Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000330 bool RelE = isRelExpr(E);
331 if (AbsVal && !RelE)
332 return true;
333 if (!AbsVal && RelE)
334 return true;
335
336 // Relative relocation to an absolute value. This is normally unrepresentable,
337 // but if the relocation refers to a weak undefined symbol, we allow it to
338 // resolve to the image base. This is a little strange, but it allows us to
339 // link function calls to such symbols. Normally such a call will be guarded
340 // with a comparison, which will load a zero from the GOT.
341 if (AbsVal && RelE) {
342 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
343 return true;
George Rimare6389d12016-06-08 12:22:26 +0000344 error("relocation " + getRelName(Type) +
345 " cannot refer to absolute symbol " + Body.getName());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000346 return true;
347 }
348
349 return Target->usesOnlyLowPageBits(Type);
350}
351
352static RelExpr toPlt(RelExpr Expr) {
353 if (Expr == R_PPC_OPD)
354 return R_PPC_PLT_OPD;
355 if (Expr == R_PC)
356 return R_PLT_PC;
Rafael Espindola12dc4462016-06-04 19:11:14 +0000357 if (Expr == R_PAGE_PC)
358 return R_PLT_PAGE_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000359 if (Expr == R_ABS)
360 return R_PLT;
361 return Expr;
362}
363
364static RelExpr fromPlt(RelExpr Expr) {
365 // We decided not to use a plt. Optimize a reference to the plt to a
366 // reference to the symbol itself.
367 if (Expr == R_PLT_PC)
368 return R_PC;
369 if (Expr == R_PPC_PLT_OPD)
370 return R_PPC_OPD;
371 if (Expr == R_PLT)
372 return R_ABS;
373 return Expr;
374}
375
376template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
377 typedef typename ELFT::uint uintX_t;
378
Rui Ueyama434b5612016-07-17 03:11:46 +0000379 uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000380 uintX_t SymValue = SS->Sym.st_value;
381 int TrailingZeros =
382 std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
383 return 1 << TrailingZeros;
384}
385
386// Reserve space in .bss for copy relocation.
387template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
388 typedef typename ELFT::uint uintX_t;
389 typedef typename ELFT::Sym Elf_Sym;
390
391 // Copy relocation against zero-sized symbol doesn't make sense.
392 uintX_t SymSize = SS->template getSize<ELFT>();
393 if (SymSize == 0)
Petr Hosek4071b1b2016-08-18 21:55:23 +0000394 fatal("cannot create a copy relocation for symbol " + SS->getName());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000395
Rui Ueyama424b4082016-06-17 01:18:46 +0000396 uintX_t Alignment = getAlignment(SS);
397 uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Alignment);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000398 Out<ELFT>::Bss->setSize(Off + SymSize);
Rui Ueyama424b4082016-06-17 01:18:46 +0000399 Out<ELFT>::Bss->updateAlignment(Alignment);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000400 uintX_t Shndx = SS->Sym.st_shndx;
401 uintX_t Value = SS->Sym.st_value;
402 // Look through the DSO's dynamic symbol table for aliases and create a
403 // dynamic symbol for each one. This causes the copy relocation to correctly
404 // interpose any aliases.
Rafael Espindola8e232572016-11-03 20:48:57 +0000405 for (const Elf_Sym &S : SS->file()->getGlobalSymbols()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000406 if (S.st_shndx != Shndx || S.st_value != Value)
407 continue;
408 auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>(
Rui Ueyama434b5612016-07-17 03:11:46 +0000409 Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable()))));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000410 if (!Alias)
411 continue;
412 Alias->OffsetInBss = Off;
413 Alias->NeedsCopyOrPltAddr = true;
414 Alias->symbol()->IsUsedInRegularObj = true;
415 }
416 Out<ELFT>::RelaDyn->addReloc(
417 {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
418}
419
420template <class ELFT>
Petr Hosek5b4f6c62016-08-22 19:01:53 +0000421static StringRef getSymbolName(const elf::ObjectFile<ELFT> &File,
422 SymbolBody &Body) {
423 if (Body.isLocal() && Body.getNameOffset())
424 return File.getStringTable().data() + Body.getNameOffset();
425 if (!Body.isLocal())
426 return Body.getName();
427 return "";
Petr Hosek4071b1b2016-08-18 21:55:23 +0000428}
429
430template <class ELFT>
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000431static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
George Rimar5c33b912016-05-25 14:31:37 +0000432 bool IsWrite, RelExpr Expr, uint32_t Type,
Rafael Espindolaf2956a32016-06-17 15:01:50 +0000433 const uint8_t *Data) {
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000434 bool Preemptible = isPreemptible(Body, Type);
George Rimar5c33b912016-05-25 14:31:37 +0000435 if (Body.isGnuIFunc()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000436 Expr = toPlt(Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000437 } else if (!Preemptible) {
438 if (needsPlt(Expr))
439 Expr = fromPlt(Expr);
Rafael Espindolad598c812016-10-27 17:28:56 +0000440 if (Expr == R_GOT_PC && !isAbsoluteValue<ELFT>(Body))
Rafael Espindolaf2956a32016-06-17 15:01:50 +0000441 Expr = Target->adjustRelaxExpr(Type, Data, Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000442 }
Peter Smithfb05cd92016-07-08 16:10:27 +0000443 Expr = Target->getThunkExpr(Expr, Type, File, Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000444
445 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body))
446 return Expr;
447
448 // This relocation would require the dynamic linker to write a value to read
449 // only memory. We can hack around it if we are producing an executable and
450 // the refered symbol can be preemepted to refer to the executable.
451 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
Petr Hosek5b4f6c62016-08-22 19:01:53 +0000452 StringRef Name = getSymbolName(File, Body);
George Rimara4c7e742016-10-20 08:36:42 +0000453 error("can't create dynamic relocation " + getRelName(Type) + " against " +
454 (Name.empty() ? "readonly segment" : "symbol " + Name));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000455 return Expr;
456 }
457 if (Body.getVisibility() != STV_DEFAULT) {
Petr Hosek4071b1b2016-08-18 21:55:23 +0000458 error("cannot preempt symbol " + Body.getName());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000459 return Expr;
460 }
461 if (Body.isObject()) {
462 // Produce a copy relocation.
463 auto *B = cast<SharedSymbol<ELFT>>(&Body);
464 if (!B->needsCopy())
465 addCopyRelSymbol(B);
466 return Expr;
467 }
468 if (Body.isFunc()) {
469 // This handles a non PIC program call to function in a shared library. In
470 // an ideal world, we could just report an error saying the relocation can
471 // overflow at runtime. In the real world with glibc, crt1.o has a
472 // R_X86_64_PC32 pointing to libc.so.
473 //
474 // The general idea on how to handle such cases is to create a PLT entry and
475 // use that as the function value.
476 //
477 // For the static linking part, we just return a plt expr and everything
478 // else will use the the PLT entry as the address.
479 //
480 // The remaining problem is making sure pointer equality still works. We
481 // need the help of the dynamic linker for that. We let it know that we have
482 // a direct reference to a so symbol by creating an undefined symbol with a
483 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
484 // the value of the symbol we created. This is true even for got entries, so
485 // pointer equality is maintained. To avoid an infinite loop, the only entry
486 // that points to the real function is a dedicated got entry used by the
487 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
488 // R_386_JMP_SLOT, etc).
489 Body.NeedsCopyOrPltAddr = true;
490 return toPlt(Expr);
491 }
Petr Hosek4071b1b2016-08-18 21:55:23 +0000492 error("symbol " + Body.getName() + " is missing type");
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000493
494 return Expr;
495}
496
497template <class ELFT, class RelTy>
498static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File,
499 const uint8_t *SectionData,
500 const RelTy *End, const RelTy &RI,
501 RelExpr Expr, SymbolBody &Body) {
502 typedef typename ELFT::uint uintX_t;
503
504 uint32_t Type = RI.getType(Config->Mips64EL);
505 uintX_t Addend = getAddend<ELFT>(RI);
506 const uint8_t *BufLoc = SectionData + RI.r_offset;
507 if (!RelTy::IsRela)
508 Addend += Target->getImplicitAddend(BufLoc, Type);
509 if (Config->EMachine == EM_MIPS) {
510 Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End);
511 if (Type == R_MIPS_LO16 && Expr == R_PC)
512 // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
513 // symbol. In that case we should use the following formula for
514 // calculation "AHL + GP - P + 4". Let's add 4 right here.
515 // For details see p. 4-19 at
516 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
517 Addend += 4;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000518 if (Expr == R_GOTREL) {
519 Addend -= MipsGPOffset;
520 if (Body.isLocal())
521 Addend += File.getMipsGp0();
522 }
523 }
524 if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
525 Addend += getPPC64TocBase();
526 return Addend;
527}
528
Eugene Leviantb380b242016-10-26 11:07:09 +0000529// Find symbol that encloses given offset. Used for error reporting.
530template <class ELFT>
531static DefinedRegular<ELFT> *getSymbolAt(InputSectionBase<ELFT> *S,
532 typename ELFT::uint Offset) {
533 for (SymbolBody *B : S->getFile()->getSymbols())
534 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B))
535 if (D->Value <= Offset && D->Value + D->Size > Offset && D->Section == S)
536 return D;
537
538 return nullptr;
539}
540
541template <class ELFT>
542static std::string getLocation(SymbolBody &Sym, InputSectionBase<ELFT> &S,
543 typename ELFT::uint Offset) {
544 ObjectFile<ELFT> *File = S.getFile();
545
546 // First check if we can get desired values from debugging information.
Rui Ueyama7556f6b2016-11-02 18:42:13 +0000547 std::string LineInfo = File->getLineInfo(&S, Offset);
Eugene Leviantb380b242016-10-26 11:07:09 +0000548 if (!LineInfo.empty())
549 return LineInfo;
550
551 // If don't have STT_FILE typed symbol in object file then
552 // use object file name.
553 std::string SrcFile = File->SourceFile;
554 if (SrcFile.empty())
555 SrcFile = Sym.File ? getFilename(Sym.File) : getFilename(File);
556
Rui Ueyama5f7e6e62016-10-26 20:26:29 +0000557 // Find a symbol at a given location.
Rui Ueyamae784ee72016-10-26 20:27:38 +0000558 DefinedRegular<ELFT> *Encl = getSymbolAt(&S, Offset);
559 if (Encl && Encl->Type == STT_FUNC) {
560 StringRef Func = getSymbolName(*File, *Encl);
Rui Ueyama5ce977c2016-10-26 18:28:06 +0000561 return SrcFile + " (function " + maybeDemangle(Func) + ")";
Eugene Leviantb380b242016-10-26 11:07:09 +0000562 }
563
Rui Ueyama5f7e6e62016-10-26 20:26:29 +0000564 // If there's no symbol, print out the offset instead of a symbol name.
Eugene Leviantb380b242016-10-26 11:07:09 +0000565 return (SrcFile + " (" + S.Name + "+0x" + Twine::utohexstr(Offset) + ")")
566 .str();
567}
568
569template <class ELFT>
570static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &S,
571 typename ELFT::uint Offset) {
Eugene Leviant89837592016-10-06 09:45:04 +0000572 if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
573 return;
574
575 if (Config->Shared && Sym.symbol()->Visibility == STV_DEFAULT &&
576 Config->UnresolvedSymbols != UnresolvedPolicy::NoUndef)
577 return;
578
Rui Ueyama5ce977c2016-10-26 18:28:06 +0000579 std::string Msg = getLocation(Sym, S, Offset) + ": undefined symbol '" +
580 maybeDemangle(Sym.getName()) + "'";
Eugene Leviant89837592016-10-06 09:45:04 +0000581
Eugene Leviant89837592016-10-06 09:45:04 +0000582 if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn)
583 warn(Msg);
584 else
585 error(Msg);
586}
587
Simon Atanasyan9e0297b2016-11-05 22:58:01 +0000588template <class RelTy>
589static std::pair<uint32_t, uint32_t>
590mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) {
591 // MIPS N32 ABI treats series of successive relocations with the same offset
592 // as a single relocation. The similar approach used by N64 ABI, but this ABI
593 // packs all relocations into the single relocation record. Here we emulate
594 // this for the N32 ABI. Iterate over relocation with the same offset and put
595 // theirs types into the single bit-set.
596 uint32_t Processed = 0;
597 for (; I != E && Offset == I->r_offset; ++I) {
598 ++Processed;
599 Type |= I->getType(Config->Mips64EL) << (8 * Processed);
600 }
601 return std::make_pair(Type, Processed);
602}
603
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000604// The reason we have to do this early scan is as follows
605// * To mmap the output file, we need to know the size
606// * For that, we need to know how many dynamic relocs we will have.
607// It might be possible to avoid this by outputting the file with write:
608// * Write the allocated output sections, computing addresses.
609// * Apply relocations, recording which ones require a dynamic reloc.
610// * Write the dynamic relocations.
611// * Write the rest of the file.
612// This would have some drawbacks. For example, we would only know if .rela.dyn
613// is needed after applying relocations. If it is, it will go after rw and rx
614// sections. Given that it is ro, we will need an extra PT_LOAD. This
615// complicates things for the dynamic linker and means we would have to reserve
616// space for the extra PT_LOAD even if we end up not using it.
617template <class ELFT, class RelTy>
Rui Ueyama2487f192016-05-25 03:40:02 +0000618static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000619 typedef typename ELFT::uint uintX_t;
620
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000621 bool IsWrite = C.Flags & SHF_WRITE;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000622
623 auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
624 Out<ELFT>::RelaDyn->addReloc(Reloc);
625 };
626
627 const elf::ObjectFile<ELFT> &File = *C.getFile();
Rafael Espindolac7e1e032016-09-12 13:13:53 +0000628 ArrayRef<uint8_t> SectionData = C.Data;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000629 const uint8_t *Buf = SectionData.begin();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000630
Rafael Espindola3abe3aa2016-07-21 21:15:32 +0000631 ArrayRef<EhSectionPiece> Pieces;
632 if (auto *Eh = dyn_cast<EhInputSection<ELFT>>(&C))
633 Pieces = Eh->Pieces;
634
635 ArrayRef<EhSectionPiece>::iterator PieceI = Pieces.begin();
636 ArrayRef<EhSectionPiece>::iterator PieceE = Pieces.end();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000637
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000638 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
639 const RelTy &RI = *I;
640 SymbolBody &Body = File.getRelocTargetSym(RI);
641 uint32_t Type = RI.getType(Config->Mips64EL);
642
Simon Atanasyan9e0297b2016-11-05 22:58:01 +0000643 if (Config->MipsN32Abi) {
644 uint32_t Processed;
645 std::tie(Type, Processed) =
646 mergeMipsN32RelTypes(Type, RI.r_offset, I + 1, E);
647 I += Processed;
648 }
649
George Rimara4c7e742016-10-20 08:36:42 +0000650 // We only report undefined symbols if they are referenced somewhere in the
651 // code.
Eugene Leviant89837592016-10-06 09:45:04 +0000652 if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak())
Eugene Leviantb380b242016-10-26 11:07:09 +0000653 reportUndefined(Body, C, RI.r_offset);
Eugene Leviant89837592016-10-06 09:45:04 +0000654
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000655 RelExpr Expr = Target->getRelExpr(Type, Body);
Rafael Espindola678844e2016-06-17 15:42:36 +0000656 bool Preemptible = isPreemptible(Body, Type);
657 Expr = adjustExpr(File, Body, IsWrite, Expr, Type, Buf + RI.r_offset);
658 if (HasError)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000659 continue;
660
Rui Ueyama809d8e22016-06-23 04:33:42 +0000661 // Skip a relocation that points to a dead piece
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000662 // in a eh_frame section.
663 while (PieceI != PieceE &&
664 (PieceI->InputOff + PieceI->size() <= RI.r_offset))
665 ++PieceI;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000666
667 // Compute the offset of this section in the output section. We do it here
668 // to try to compute it only once.
669 uintX_t Offset;
670 if (PieceI != PieceE) {
671 assert(PieceI->InputOff <= RI.r_offset && "Relocation not in any piece");
Rafael Espindola113860b2016-10-20 10:55:58 +0000672 if (PieceI->OutputOff == -1)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000673 continue;
674 Offset = PieceI->OutputOff + RI.r_offset - PieceI->InputOff;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000675 } else {
George Rimar3e6833b2016-08-19 15:46:28 +0000676 Offset = RI.r_offset;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000677 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000678
679 // This relocation does not require got entry, but it is relative to got and
680 // needs it to be created. Here we request for that.
Rafael Espindola79202c32016-08-31 23:24:11 +0000681 if (Expr == R_GOTONLY_PC || Expr == R_GOTONLY_PC_FROM_END ||
682 Expr == R_GOTREL || Expr == R_GOTREL_FROM_END || Expr == R_PPC_TOC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000683 Out<ELFT>::Got->HasGotOffRel = true;
684
685 uintX_t Addend = computeAddend(File, Buf, E, RI, Expr, Body);
686
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000687 if (unsigned Processed =
688 handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000689 I += (Processed - 1);
690 continue;
691 }
692
Peter Smithd6486032016-10-20 09:59:26 +0000693 // Ignore "hint" and TLS Descriptor call relocation because they are
694 // only markers for relaxation.
695 if (Expr == R_HINT || Expr == R_TLSDESC_CALL)
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000696 continue;
697
Peter Smithfb05cd92016-07-08 16:10:27 +0000698 if (needsPlt(Expr) || Expr == R_THUNK_ABS || Expr == R_THUNK_PC ||
699 Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) ||
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000700 !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000701 // If the relocation points to something in the file, we can process it.
702 bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body);
703
704 // If the output being produced is position independent, the final value
705 // is still not known. In that case we still need some help from the
706 // dynamic linker. We can however do better than just copying the incoming
707 // relocation. We can process some of it and and just ask the dynamic
708 // linker to add the load address.
709 if (!Constant)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000710 AddDyn({Target->RelativeRel, &C, Offset, true, &Body, Addend});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000711
712 // If the produced value is a constant, we just remember to write it
713 // when outputting this section. We also have to do it if the format
714 // uses Elf_Rel, since in that case the written value is the addend.
715 if (Constant || !RelTy::IsRela)
Rafael Espindola664c6522016-09-07 20:37:34 +0000716 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000717 } else {
718 // We don't know anything about the finaly symbol. Just ask the dynamic
719 // linker to handle the relocation for us.
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000720 AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000721 // MIPS ABI turns using of GOT and dynamic relocations inside out.
722 // While regular ABI uses dynamic relocations to fill up GOT entries
723 // MIPS ABI requires dynamic linker to fills up GOT entries using
724 // specially sorted dynamic symbol table. This affects even dynamic
725 // relocations against symbols which do not require GOT entries
726 // creation explicitly, i.e. do not have any GOT-relocations. So if
727 // a preemptible symbol has a dynamic relocation we anyway have
728 // to create a GOT entry for it.
729 // If a non-preemptible symbol has a dynamic relocation against it,
730 // dynamic linker takes it st_value, adds offset and writes down
731 // result of the dynamic relocation. In case of preemptible symbol
732 // dynamic linker performs symbol resolution, writes the symbol value
733 // to the GOT entry and reads the GOT entry when it needs to perform
734 // a dynamic relocation.
735 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
Simon Atanasyan41325112016-06-19 21:39:37 +0000736 if (Config->EMachine == EM_MIPS)
737 Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000738 continue;
739 }
740
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000741 // At this point we are done with the relocated position. Some relocations
742 // also require us to create a got or plt entry.
743
744 // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol.
745 if (needsPlt(Expr)) {
746 if (Body.isInPlt())
747 continue;
748 Out<ELFT>::Plt->addEntry(Body);
749
750 uint32_t Rel;
751 if (Body.isGnuIFunc() && !Preemptible)
752 Rel = Target->IRelativeRel;
753 else
754 Rel = Target->PltRel;
755
756 Out<ELFT>::GotPlt->addEntry(Body);
757 Out<ELFT>::RelaPlt->addReloc({Rel, Out<ELFT>::GotPlt,
758 Body.getGotPltOffset<ELFT>(), !Preemptible,
759 &Body, 0});
760 continue;
761 }
762
763 if (refersToGotEntry(Expr)) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000764 if (Config->EMachine == EM_MIPS) {
Simon Atanasyanaf52f6a2016-09-08 09:07:12 +0000765 // MIPS ABI has special rules to process GOT entries and doesn't
766 // require relocation entries for them. A special case is TLS
767 // relocations. In that case dynamic loader applies dynamic
768 // relocations to initialize TLS GOT entries.
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000769 // See "Global Offset Table" in Chapter 5 in the following document
770 // for detailed description:
771 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Simon Atanasyan41325112016-06-19 21:39:37 +0000772 Out<ELFT>::Got->addMipsEntry(Body, Addend, Expr);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000773 if (Body.isTls() && Body.isPreemptible())
Simon Atanasyan002e2442016-06-23 15:26:31 +0000774 AddDyn({Target->TlsGotRel, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000775 false, &Body, 0});
Simon Atanasyan41325112016-06-19 21:39:37 +0000776 continue;
777 }
778
779 if (Body.isInGot())
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000780 continue;
781
Simon Atanasyan41325112016-06-19 21:39:37 +0000782 Out<ELFT>::Got->addEntry(Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000783 if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body))) {
784 uint32_t DynType;
785 if (Body.isTls())
786 DynType = Target->TlsGotRel;
787 else if (Preemptible)
788 DynType = Target->GotRel;
789 else
790 DynType = Target->RelativeRel;
791 AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
792 !Preemptible, &Body, 0});
793 }
794 continue;
795 }
796 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000797}
798
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000799template <class ELFT>
800void scanRelocations(InputSectionBase<ELFT> &S,
801 const typename ELFT::Shdr &RelSec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000802 ELFFile<ELFT> EObj = S.getFile()->getObj();
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000803 if (RelSec.sh_type == SHT_RELA)
Rafael Espindola454fe152016-11-03 19:07:44 +0000804 scanRelocs(S, check(EObj.relas(&RelSec)));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000805 else
Rafael Espindola454fe152016-11-03 19:07:44 +0000806 scanRelocs(S, check(EObj.rels(&RelSec)));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000807}
808
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000809template <class ELFT, class RelTy>
810static void createThunks(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
811 const elf::ObjectFile<ELFT> &File = *C.getFile();
812 for (const RelTy &Rel : Rels) {
813 SymbolBody &Body = File.getRelocTargetSym(Rel);
814 uint32_t Type = Rel.getType(Config->Mips64EL);
815 RelExpr Expr = Target->getRelExpr(Type, Body);
816 if (!isPreemptible(Body, Type) && needsPlt(Expr))
817 Expr = fromPlt(Expr);
818 Expr = Target->getThunkExpr(Expr, Type, File, Body);
819 // Some targets might require creation of thunks for relocations.
820 // Now we support only MIPS which requires LA25 thunk to call PIC
821 // code from non-PIC one, and ARM which requires interworking.
822 if (Expr == R_THUNK_ABS || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC) {
823 auto *Sec = cast<InputSection<ELFT>>(&C);
824 addThunk<ELFT>(Type, Body, *Sec);
825 }
826 }
827}
828
829template <class ELFT>
830void createThunks(InputSectionBase<ELFT> &S,
831 const typename ELFT::Shdr &RelSec) {
Rafael Espindolae19abab2016-11-03 20:44:50 +0000832 ELFFile<ELFT> EObj = S.getFile()->getObj();
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000833 if (RelSec.sh_type == SHT_RELA)
Rafael Espindola454fe152016-11-03 19:07:44 +0000834 createThunks(S, check(EObj.relas(&RelSec)));
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000835 else
Rafael Espindola454fe152016-11-03 19:07:44 +0000836 createThunks(S, check(EObj.rels(&RelSec)));
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000837}
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000838
839template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &,
840 const ELF32LE::Shdr &);
841template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &,
842 const ELF32BE::Shdr &);
843template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &,
844 const ELF64LE::Shdr &);
845template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &,
846 const ELF64BE::Shdr &);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000847
848template void createThunks<ELF32LE>(InputSectionBase<ELF32LE> &,
849 const ELF32LE::Shdr &);
850template void createThunks<ELF32BE>(InputSectionBase<ELF32BE> &,
851 const ELF32BE::Shdr &);
852template void createThunks<ELF64LE>(InputSectionBase<ELF64LE> &,
853 const ELF64LE::Shdr &);
854template void createThunks<ELF64BE>(InputSectionBase<ELF64BE> &,
855 const ELF64BE::Shdr &);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000856}
857}