blob: 8df3e3d4219aec40d57bd2a34a6601bc31b3c578 [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"
Eugene Leviant41ca3272016-11-10 09:48:29 +000047#include "Strings.h"
Rui Ueyama0fcdc732016-05-24 20:24:43 +000048#include "SymbolTable.h"
Eugene Leviant41ca3272016-11-10 09:48:29 +000049#include "SyntheticSections.h"
Rui Ueyama0fcdc732016-05-24 20:24:43 +000050#include "Target.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000051#include "Thunks.h"
Rui Ueyama0fcdc732016-05-24 20:24:43 +000052
53#include "llvm/Support/Endian.h"
54#include "llvm/Support/raw_ostream.h"
55
56using namespace llvm;
57using namespace llvm::ELF;
58using namespace llvm::object;
59using namespace llvm::support::endian;
60
61namespace lld {
62namespace elf {
63
64static bool refersToGotEntry(RelExpr Expr) {
Simon Atanasyan41325112016-06-19 21:39:37 +000065 return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT_LOCAL_PAGE ||
Simon Atanasyanbed04bf2016-10-21 07:22:30 +000066 Expr == R_MIPS_GOT_OFF || Expr == R_MIPS_GOT_OFF32 ||
67 Expr == R_MIPS_TLSGD || Expr == R_MIPS_TLSLD ||
68 Expr == R_GOT_PAGE_PC || Expr == R_GOT_PC || Expr == R_GOT_FROM_END ||
69 Expr == R_TLSGD || Expr == R_TLSGD_PC || Expr == R_TLSDESC ||
70 Expr == R_TLSDESC_PAGE;
Rui Ueyama0fcdc732016-05-24 20:24:43 +000071}
72
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000073static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
74 // In case of MIPS GP-relative relocations always resolve to a definition
75 // in a regular input file, ignoring the one-definition rule. So we,
76 // for example, should not attempt to create a dynamic relocation even
77 // if the target symbol is preemptible. There are two two MIPS GP-relative
78 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
79 // can be against a preemptible symbol.
Simon Atanasyana26a1572016-06-10 12:26:09 +000080 // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000081 // relocation types occupy eight bit. In case of N64 ABI we extract first
82 // relocation from 3-in-1 packet because only the first relocation can
83 // be against a real symbol.
Simon Atanasyana26a1572016-06-10 12:26:09 +000084 if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16)
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000085 return false;
86 return Body.isPreemptible();
87}
88
Peter Smithfde62132016-09-23 13:54:48 +000089// This function is similar to the `handleTlsRelocation`. ARM and MIPS do not
90// support any relaxations for TLS relocations so by factoring out ARM and MIPS
91// handling in to the separate function we can simplify the code and do not
92// pollute `handleTlsRelocation` by ARM and MIPS `ifs` statements.
93// FIXME: The ARM implementation always adds the module index dynamic
94// relocation even for non-preemptible symbols in applications. For static
95// linking support we must either resolve the module index relocation at static
96// link time, or hard code the module index (1) for the application in the GOT.
Simon Atanasyan725dc142016-11-16 21:01:02 +000097template <class ELFT, class GOT>
98static unsigned handleNoRelaxTlsRelocation(
99 GOT *Got, uint32_t Type, SymbolBody &Body, InputSectionBase<ELFT> &C,
100 typename ELFT::uint Offset, typename ELFT::uint Addend, RelExpr Expr) {
Peter Smithfde62132016-09-23 13:54:48 +0000101 if (Expr == R_MIPS_TLSLD || Expr == R_TLSLD_PC) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000102 if (Got->addTlsIndex() && (Config->Pic || Config->EMachine == EM_ARM))
103 In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, Got,
104 Got->getTlsIndexOff(), false, nullptr, 0});
Rafael Espindola664c6522016-09-07 20:37:34 +0000105 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000106 return 1;
107 }
Peter Smithfde62132016-09-23 13:54:48 +0000108 typedef typename ELFT::uint uintX_t;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000109 if (Target->isTlsGlobalDynamicRel(Type)) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000110 if (Got->addDynTlsEntry(Body) &&
Peter Smithfde62132016-09-23 13:54:48 +0000111 (Body.isPreemptible() || Config->EMachine == EM_ARM)) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000112 uintX_t Off = Got->getGlobalDynOffset(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000113 In<ELFT>::RelaDyn->addReloc(
Simon Atanasyan725dc142016-11-16 21:01:02 +0000114 {Target->TlsModuleIndexRel, Got, Off, false, &Body, 0});
Peter Smithfde62132016-09-23 13:54:48 +0000115 if (Body.isPreemptible())
Simon Atanasyan725dc142016-11-16 21:01:02 +0000116 In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Got,
Eugene Levianta96d9022016-11-16 10:02:27 +0000117 Off + (uintX_t)sizeof(uintX_t), false,
118 &Body, 0});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000119 }
Rafael Espindola664c6522016-09-07 20:37:34 +0000120 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000121 return 1;
122 }
123 return 0;
124}
125
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000126// Returns the number of relocations processed.
127template <class ELFT>
128static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body,
129 InputSectionBase<ELFT> &C,
130 typename ELFT::uint Offset,
131 typename ELFT::uint Addend, RelExpr Expr) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000132 if (!(C.Flags & SHF_ALLOC))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000133 return 0;
134
135 if (!Body.isTls())
136 return 0;
137
138 typedef typename ELFT::uint uintX_t;
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000139
Simon Atanasyan725dc142016-11-16 21:01:02 +0000140 if (Config->EMachine == EM_ARM)
141 return handleNoRelaxTlsRelocation<ELFT>(In<ELFT>::Got, Type, Body, C,
142 Offset, Addend, Expr);
143 if (Config->EMachine == EM_MIPS)
144 return handleNoRelaxTlsRelocation<ELFT>(In<ELFT>::MipsGot, Type, Body, C,
145 Offset, Addend, 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) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000149 if (In<ELFT>::Got->addDynTlsEntry(Body)) {
150 uintX_t Off = In<ELFT>::Got->getGlobalDynOffset(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000151 In<ELFT>::RelaDyn->addReloc(
Eugene Leviantad4439e2016-11-11 11:33:32 +0000152 {Target->TlsDescRel, In<ELFT>::Got, Off, false, &Body, 0});
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000153 }
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 }
Eugene Leviantad4439e2016-11-11 11:33:32 +0000166 if (In<ELFT>::Got->addTlsIndex())
Eugene Levianta96d9022016-11-16 10:02:27 +0000167 In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, In<ELFT>::Got,
168 In<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) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000184 if (In<ELFT>::Got->addDynTlsEntry(Body)) {
185 uintX_t Off = In<ELFT>::Got->getGlobalDynOffset(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000186 In<ELFT>::RelaDyn->addReloc(
Eugene Leviantad4439e2016-11-11 11:33:32 +0000187 {Target->TlsModuleIndexRel, In<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.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000191 uintX_t OffsetOff = Off + (uintX_t)sizeof(uintX_t);
Simon Atanasyan9b861182016-06-10 12:26:39 +0000192 if (isPreemptible(Body, Type))
Eugene Levianta96d9022016-11-16 10:02:27 +0000193 In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, In<ELFT>::Got,
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000194 OffsetOff, false, &Body, 0});
195 else
196 In<ELFT>::Got->Relocations.push_back(
197 {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000198 }
Rafael Espindola664c6522016-09-07 20:37:34 +0000199 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000200 return 1;
201 }
202
203 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
204 // depending on the symbol being locally defined or not.
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000205 if (isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000206 C.Relocations.push_back(
Rafael Espindola69f54022016-06-04 23:22:34 +0000207 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
Rafael Espindola664c6522016-09-07 20:37:34 +0000208 Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000209 if (!Body.isInGot()) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000210 In<ELFT>::Got->addEntry(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000211 In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, In<ELFT>::Got,
212 Body.getGotOffset<ELFT>(), false, &Body,
213 0});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000214 }
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000215 return Target->TlsGdRelaxSkip;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000216 }
217 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000218 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
Rafael Espindola69f54022016-06-04 23:22:34 +0000219 Offset, Addend, &Body});
Rafael Espindolaf807d472016-06-04 23:04:39 +0000220 return Target->TlsGdRelaxSkip;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000221 }
222
223 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
224 // defined.
225 if (Target->isTlsInitialExecRel(Type) && !Config->Shared &&
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000226 !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000227 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000228 {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000229 return 1;
230 }
231 return 0;
232}
233
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000234template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
235 return read32<E>(Loc) & 0xffff;
236}
237
238template <class RelTy>
239static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
240 switch (Rel->getType(Config->Mips64EL)) {
241 case R_MIPS_HI16:
242 return R_MIPS_LO16;
243 case R_MIPS_GOT16:
244 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
245 case R_MIPS_PCHI16:
246 return R_MIPS_PCLO16;
247 case R_MICROMIPS_HI16:
248 return R_MICROMIPS_LO16;
249 default:
250 return R_MIPS_NONE;
251 }
252}
253
254template <class ELFT, class RelTy>
255static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc,
256 SymbolBody &Sym, const RelTy *Rel,
257 const RelTy *End) {
258 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
259 uint32_t Type = getMipsPairType(Rel, Sym);
260
261 // Some MIPS relocations use addend calculated from addend of the relocation
262 // itself and addend of paired relocation. ABI requires to compute such
263 // combined addend in case of REL relocation record format only.
264 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
265 if (RelTy::IsRela || Type == R_MIPS_NONE)
266 return 0;
267
268 for (const RelTy *RI = Rel; RI != End; ++RI) {
269 if (RI->getType(Config->Mips64EL) != Type)
270 continue;
271 if (RI->getSymbol(Config->Mips64EL) != SymIndex)
272 continue;
273 const endianness E = ELFT::TargetEndianness;
274 return ((read32<E>(BufLoc) & 0xffff) << 16) +
275 readSignedLo16<E>(Buf + RI->r_offset);
276 }
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000277 warn("can't find matching " + toString(Type) + " relocation for " +
278 toString(Rel->getType(Config->Mips64EL)));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000279 return 0;
280}
281
282// True if non-preemptable symbol always has the same value regardless of where
283// the DSO is loaded.
284template <class ELFT> static bool isAbsolute(const SymbolBody &Body) {
285 if (Body.isUndefined())
286 return !Body.isLocal() && Body.symbol()->isWeak();
287 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body))
288 return DR->Section == nullptr; // Absolute symbol.
289 return false;
290}
291
Rafael Espindolad598c812016-10-27 17:28:56 +0000292template <class ELFT> static bool isAbsoluteValue(const SymbolBody &Body) {
293 return isAbsolute<ELFT>(Body) || Body.isTls();
294}
295
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000296static bool needsPlt(RelExpr Expr) {
Rafael Espindola12dc4462016-06-04 19:11:14 +0000297 return Expr == R_PLT_PC || Expr == R_PPC_PLT_OPD || Expr == R_PLT ||
Peter Smithfb05cd92016-07-08 16:10:27 +0000298 Expr == R_PLT_PAGE_PC || Expr == R_THUNK_PLT_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000299}
300
301// True if this expression is of the form Sym - X, where X is a position in the
302// file (PC, or GOT for example).
303static bool isRelExpr(RelExpr Expr) {
Rafael Espindola719f55d2016-09-06 13:57:15 +0000304 return Expr == R_PC || Expr == R_GOTREL || Expr == R_GOTREL_FROM_END ||
Simon Atanasyan725dc142016-11-16 21:01:02 +0000305 Expr == R_MIPS_GOTREL || Expr == R_PAGE_PC || Expr == R_RELAX_GOT_PC ||
306 Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000307}
308
309template <class ELFT>
310static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
George Rimar463984d2016-11-15 08:07:14 +0000311 const SymbolBody &Body,
312 InputSectionBase<ELFT> &S,
313 typename ELFT::uint RelOff) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000314 // These expressions always compute a constant
315 if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF ||
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000316 E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF ||
317 E == R_MIPS_GOT_OFF32 || E == R_MIPS_TLSGD || E == R_GOT_PAGE_PC ||
318 E == R_GOT_PC || E == R_PLT_PC || E == R_TLSGD_PC || E == R_TLSGD ||
319 E == R_PPC_PLT_OPD || E == R_TLSDESC_CALL || E == R_TLSDESC_PAGE ||
320 E == R_HINT || E == R_THUNK_PC || E == R_THUNK_PLT_PC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000321 return true;
322
323 // These never do, except if the entire file is position dependent or if
324 // only the low bits are used.
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000325 if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000326 return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
327
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000328 if (isPreemptible(Body, Type))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000329 return false;
330
331 if (!Config->Pic)
332 return true;
333
Rafael Espindolad598c812016-10-27 17:28:56 +0000334 bool AbsVal = isAbsoluteValue<ELFT>(Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000335 bool RelE = isRelExpr(E);
336 if (AbsVal && !RelE)
337 return true;
338 if (!AbsVal && RelE)
339 return true;
340
341 // Relative relocation to an absolute value. This is normally unrepresentable,
342 // but if the relocation refers to a weak undefined symbol, we allow it to
343 // resolve to the image base. This is a little strange, but it allows us to
344 // link function calls to such symbols. Normally such a call will be guarded
345 // with a comparison, which will load a zero from the GOT.
346 if (AbsVal && RelE) {
347 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
348 return true;
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000349 error(S.getLocation(RelOff) + ": relocation " + toString(Type) +
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000350 " cannot refer to absolute symbol '" + toString(Body) +
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000351 "' defined in " + toString(Body.File));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000352 return true;
353 }
354
355 return Target->usesOnlyLowPageBits(Type);
356}
357
358static RelExpr toPlt(RelExpr Expr) {
359 if (Expr == R_PPC_OPD)
360 return R_PPC_PLT_OPD;
361 if (Expr == R_PC)
362 return R_PLT_PC;
Rafael Espindola12dc4462016-06-04 19:11:14 +0000363 if (Expr == R_PAGE_PC)
364 return R_PLT_PAGE_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000365 if (Expr == R_ABS)
366 return R_PLT;
367 return Expr;
368}
369
370static RelExpr fromPlt(RelExpr Expr) {
371 // We decided not to use a plt. Optimize a reference to the plt to a
372 // reference to the symbol itself.
373 if (Expr == R_PLT_PC)
374 return R_PC;
375 if (Expr == R_PPC_PLT_OPD)
376 return R_PPC_OPD;
377 if (Expr == R_PLT)
378 return R_ABS;
379 return Expr;
380}
381
382template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
383 typedef typename ELFT::uint uintX_t;
384
Rui Ueyama434b5612016-07-17 03:11:46 +0000385 uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000386 uintX_t SymValue = SS->Sym.st_value;
387 int TrailingZeros =
388 std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
389 return 1 << TrailingZeros;
390}
391
392// Reserve space in .bss for copy relocation.
393template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
394 typedef typename ELFT::uint uintX_t;
395 typedef typename ELFT::Sym Elf_Sym;
396
397 // Copy relocation against zero-sized symbol doesn't make sense.
398 uintX_t SymSize = SS->template getSize<ELFT>();
399 if (SymSize == 0)
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000400 fatal("cannot create a copy relocation for symbol " + toString(*SS));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000401
Rui Ueyama424b4082016-06-17 01:18:46 +0000402 uintX_t Alignment = getAlignment(SS);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000403 uintX_t Off = alignTo(Out<ELFT>::Bss->Size, Alignment);
404 Out<ELFT>::Bss->Size = Off + SymSize;
Rui Ueyama424b4082016-06-17 01:18:46 +0000405 Out<ELFT>::Bss->updateAlignment(Alignment);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000406 uintX_t Shndx = SS->Sym.st_shndx;
407 uintX_t Value = SS->Sym.st_value;
408 // Look through the DSO's dynamic symbol table for aliases and create a
409 // dynamic symbol for each one. This causes the copy relocation to correctly
410 // interpose any aliases.
Rafael Espindola8e232572016-11-03 20:48:57 +0000411 for (const Elf_Sym &S : SS->file()->getGlobalSymbols()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000412 if (S.st_shndx != Shndx || S.st_value != Value)
413 continue;
414 auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>(
Rui Ueyama434b5612016-07-17 03:11:46 +0000415 Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable()))));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000416 if (!Alias)
417 continue;
418 Alias->OffsetInBss = Off;
419 Alias->NeedsCopyOrPltAddr = true;
420 Alias->symbol()->IsUsedInRegularObj = true;
421 }
Eugene Levianta96d9022016-11-16 10:02:27 +0000422 In<ELFT>::RelaDyn->addReloc(
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000423 {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
424}
425
426template <class ELFT>
427static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
George Rimar5c33b912016-05-25 14:31:37 +0000428 bool IsWrite, RelExpr Expr, uint32_t Type,
George Rimar463984d2016-11-15 08:07:14 +0000429 const uint8_t *Data, InputSectionBase<ELFT> &S,
430 typename ELFT::uint RelOff) {
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000431 bool Preemptible = isPreemptible(Body, Type);
George Rimar5c33b912016-05-25 14:31:37 +0000432 if (Body.isGnuIFunc()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000433 Expr = toPlt(Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000434 } else if (!Preemptible) {
435 if (needsPlt(Expr))
436 Expr = fromPlt(Expr);
Rafael Espindolad598c812016-10-27 17:28:56 +0000437 if (Expr == R_GOT_PC && !isAbsoluteValue<ELFT>(Body))
Rafael Espindolaf2956a32016-06-17 15:01:50 +0000438 Expr = Target->adjustRelaxExpr(Type, Data, Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000439 }
Peter Smithfb05cd92016-07-08 16:10:27 +0000440 Expr = Target->getThunkExpr(Expr, Type, File, Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000441
George Rimar463984d2016-11-15 08:07:14 +0000442 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000443 return Expr;
444
445 // This relocation would require the dynamic linker to write a value to read
446 // only memory. We can hack around it if we are producing an executable and
447 // the refered symbol can be preemepted to refer to the executable.
448 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000449 error(S.getLocation(RelOff) + ": can't create dynamic relocation " +
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000450 toString(Type) + " against " +
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000451 (Body.getName().empty() ? "local symbol in readonly segment"
452 : "symbol '" + toString(Body) + "'") +
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000453 " defined in " + toString(Body.File));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000454 return Expr;
455 }
456 if (Body.getVisibility() != STV_DEFAULT) {
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000457 error(S.getLocation(RelOff) + ": cannot preempt symbol '" + toString(Body) +
458 "' defined in " + toString(Body.File));
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 }
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000492 error("symbol '" + toString(Body) + "' defined in " + toString(Body.File) +
George Rimar76f429b2016-11-16 17:24:06 +0000493 " is missing type");
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000494
495 return Expr;
496}
497
498template <class ELFT, class RelTy>
499static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File,
500 const uint8_t *SectionData,
501 const RelTy *End, const RelTy &RI,
502 RelExpr Expr, SymbolBody &Body) {
503 typedef typename ELFT::uint uintX_t;
504
505 uint32_t Type = RI.getType(Config->Mips64EL);
506 uintX_t Addend = getAddend<ELFT>(RI);
507 const uint8_t *BufLoc = SectionData + RI.r_offset;
508 if (!RelTy::IsRela)
509 Addend += Target->getImplicitAddend(BufLoc, Type);
510 if (Config->EMachine == EM_MIPS) {
511 Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End);
512 if (Type == R_MIPS_LO16 && Expr == R_PC)
513 // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
514 // symbol. In that case we should use the following formula for
515 // calculation "AHL + GP - P + 4". Let's add 4 right here.
516 // For details see p. 4-19 at
517 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
518 Addend += 4;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000519 if (Expr == R_MIPS_GOTREL && Body.isLocal())
520 Addend += File.MipsGp0;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000521 }
522 if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
523 Addend += getPPC64TocBase();
524 return Addend;
525}
526
Eugene Leviantb380b242016-10-26 11:07:09 +0000527template <class ELFT>
528static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &S,
529 typename ELFT::uint Offset) {
Eugene Leviant89837592016-10-06 09:45:04 +0000530 if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
531 return;
532
533 if (Config->Shared && Sym.symbol()->Visibility == STV_DEFAULT &&
534 Config->UnresolvedSymbols != UnresolvedPolicy::NoUndef)
535 return;
536
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000537 std::string Msg =
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000538 S.getLocation(Offset) + ": undefined symbol '" + toString(Sym) + "'";
Eugene Leviant89837592016-10-06 09:45:04 +0000539
Eugene Leviant89837592016-10-06 09:45:04 +0000540 if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn)
541 warn(Msg);
542 else
543 error(Msg);
544}
545
Simon Atanasyan9e0297b2016-11-05 22:58:01 +0000546template <class RelTy>
547static std::pair<uint32_t, uint32_t>
548mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) {
549 // MIPS N32 ABI treats series of successive relocations with the same offset
550 // as a single relocation. The similar approach used by N64 ABI, but this ABI
551 // packs all relocations into the single relocation record. Here we emulate
552 // this for the N32 ABI. Iterate over relocation with the same offset and put
553 // theirs types into the single bit-set.
554 uint32_t Processed = 0;
555 for (; I != E && Offset == I->r_offset; ++I) {
556 ++Processed;
557 Type |= I->getType(Config->Mips64EL) << (8 * Processed);
558 }
559 return std::make_pair(Type, Processed);
560}
561
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000562// The reason we have to do this early scan is as follows
563// * To mmap the output file, we need to know the size
564// * For that, we need to know how many dynamic relocs we will have.
565// It might be possible to avoid this by outputting the file with write:
566// * Write the allocated output sections, computing addresses.
567// * Apply relocations, recording which ones require a dynamic reloc.
568// * Write the dynamic relocations.
569// * Write the rest of the file.
570// This would have some drawbacks. For example, we would only know if .rela.dyn
571// is needed after applying relocations. If it is, it will go after rw and rx
572// sections. Given that it is ro, we will need an extra PT_LOAD. This
573// complicates things for the dynamic linker and means we would have to reserve
574// space for the extra PT_LOAD even if we end up not using it.
575template <class ELFT, class RelTy>
Rui Ueyama2487f192016-05-25 03:40:02 +0000576static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000577 typedef typename ELFT::uint uintX_t;
578
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000579 bool IsWrite = C.Flags & SHF_WRITE;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000580
581 auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
Eugene Levianta96d9022016-11-16 10:02:27 +0000582 In<ELFT>::RelaDyn->addReloc(Reloc);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000583 };
584
Vitaly Buka029d7302016-11-15 07:32:51 +0000585 const elf::ObjectFile<ELFT> *File = C.getFile();
Rafael Espindolac7e1e032016-09-12 13:13:53 +0000586 ArrayRef<uint8_t> SectionData = C.Data;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000587 const uint8_t *Buf = SectionData.begin();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000588
Rafael Espindola3abe3aa2016-07-21 21:15:32 +0000589 ArrayRef<EhSectionPiece> Pieces;
590 if (auto *Eh = dyn_cast<EhInputSection<ELFT>>(&C))
591 Pieces = Eh->Pieces;
592
593 ArrayRef<EhSectionPiece>::iterator PieceI = Pieces.begin();
594 ArrayRef<EhSectionPiece>::iterator PieceE = Pieces.end();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000595
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000596 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
597 const RelTy &RI = *I;
Vitaly Buka029d7302016-11-15 07:32:51 +0000598 SymbolBody &Body = File->getRelocTargetSym(RI);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000599 uint32_t Type = RI.getType(Config->Mips64EL);
600
Simon Atanasyan9e0297b2016-11-05 22:58:01 +0000601 if (Config->MipsN32Abi) {
602 uint32_t Processed;
603 std::tie(Type, Processed) =
604 mergeMipsN32RelTypes(Type, RI.r_offset, I + 1, E);
605 I += Processed;
606 }
607
George Rimara4c7e742016-10-20 08:36:42 +0000608 // We only report undefined symbols if they are referenced somewhere in the
609 // code.
Eugene Leviant89837592016-10-06 09:45:04 +0000610 if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak())
Eugene Leviantb380b242016-10-26 11:07:09 +0000611 reportUndefined(Body, C, RI.r_offset);
Eugene Leviant89837592016-10-06 09:45:04 +0000612
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000613 RelExpr Expr = Target->getRelExpr(Type, Body);
Rafael Espindola678844e2016-06-17 15:42:36 +0000614 bool Preemptible = isPreemptible(Body, Type);
George Rimar463984d2016-11-15 08:07:14 +0000615 Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset, C,
616 RI.r_offset);
Rui Ueyamaf373dd72016-11-24 01:43:21 +0000617 if (ErrorCount)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000618 continue;
619
Rui Ueyama809d8e22016-06-23 04:33:42 +0000620 // Skip a relocation that points to a dead piece
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000621 // in a eh_frame section.
622 while (PieceI != PieceE &&
623 (PieceI->InputOff + PieceI->size() <= RI.r_offset))
624 ++PieceI;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000625
626 // Compute the offset of this section in the output section. We do it here
627 // to try to compute it only once.
628 uintX_t Offset;
629 if (PieceI != PieceE) {
630 assert(PieceI->InputOff <= RI.r_offset && "Relocation not in any piece");
Rafael Espindola113860b2016-10-20 10:55:58 +0000631 if (PieceI->OutputOff == -1)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000632 continue;
633 Offset = PieceI->OutputOff + RI.r_offset - PieceI->InputOff;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000634 } else {
George Rimar3e6833b2016-08-19 15:46:28 +0000635 Offset = RI.r_offset;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000636 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000637
638 // This relocation does not require got entry, but it is relative to got and
639 // needs it to be created. Here we request for that.
Rafael Espindola79202c32016-08-31 23:24:11 +0000640 if (Expr == R_GOTONLY_PC || Expr == R_GOTONLY_PC_FROM_END ||
641 Expr == R_GOTREL || Expr == R_GOTREL_FROM_END || Expr == R_PPC_TOC)
Eugene Leviantad4439e2016-11-11 11:33:32 +0000642 In<ELFT>::Got->HasGotOffRel = true;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000643
Vitaly Buka029d7302016-11-15 07:32:51 +0000644 uintX_t Addend = computeAddend(*File, Buf, E, RI, Expr, Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000645
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000646 if (unsigned Processed =
647 handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000648 I += (Processed - 1);
649 continue;
650 }
651
Peter Smithd6486032016-10-20 09:59:26 +0000652 // Ignore "hint" and TLS Descriptor call relocation because they are
653 // only markers for relaxation.
654 if (Expr == R_HINT || Expr == R_TLSDESC_CALL)
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000655 continue;
656
Peter Smithfb05cd92016-07-08 16:10:27 +0000657 if (needsPlt(Expr) || Expr == R_THUNK_ABS || Expr == R_THUNK_PC ||
658 Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) ||
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000659 !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000660 // If the relocation points to something in the file, we can process it.
George Rimar463984d2016-11-15 08:07:14 +0000661 bool Constant =
662 isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, C, RI.r_offset);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000663
664 // If the output being produced is position independent, the final value
665 // is still not known. In that case we still need some help from the
666 // dynamic linker. We can however do better than just copying the incoming
667 // relocation. We can process some of it and and just ask the dynamic
668 // linker to add the load address.
669 if (!Constant)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000670 AddDyn({Target->RelativeRel, &C, Offset, true, &Body, Addend});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000671
672 // If the produced value is a constant, we just remember to write it
673 // when outputting this section. We also have to do it if the format
674 // uses Elf_Rel, since in that case the written value is the addend.
675 if (Constant || !RelTy::IsRela)
Rafael Espindola664c6522016-09-07 20:37:34 +0000676 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000677 } else {
678 // We don't know anything about the finaly symbol. Just ask the dynamic
679 // linker to handle the relocation for us.
Eugene Leviantab024a32016-11-25 08:56:36 +0000680 if (!Target->isPicRel(Type))
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000681 error(C.getLocation(Offset) + ": relocation " + toString(Type) +
Eugene Leviantab024a32016-11-25 08:56:36 +0000682 " cannot be used against shared object; recompile with -fPIC.");
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000683 AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend});
Eugene Leviantab024a32016-11-25 08:56:36 +0000684
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000685 // MIPS ABI turns using of GOT and dynamic relocations inside out.
686 // While regular ABI uses dynamic relocations to fill up GOT entries
687 // MIPS ABI requires dynamic linker to fills up GOT entries using
688 // specially sorted dynamic symbol table. This affects even dynamic
689 // relocations against symbols which do not require GOT entries
690 // creation explicitly, i.e. do not have any GOT-relocations. So if
691 // a preemptible symbol has a dynamic relocation we anyway have
692 // to create a GOT entry for it.
693 // If a non-preemptible symbol has a dynamic relocation against it,
694 // dynamic linker takes it st_value, adds offset and writes down
695 // result of the dynamic relocation. In case of preemptible symbol
696 // dynamic linker performs symbol resolution, writes the symbol value
697 // to the GOT entry and reads the GOT entry when it needs to perform
698 // a dynamic relocation.
699 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
Simon Atanasyan41325112016-06-19 21:39:37 +0000700 if (Config->EMachine == EM_MIPS)
Simon Atanasyan725dc142016-11-16 21:01:02 +0000701 In<ELFT>::MipsGot->addEntry(Body, Addend, Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000702 continue;
703 }
704
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000705 // At this point we are done with the relocated position. Some relocations
706 // also require us to create a got or plt entry.
707
708 // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol.
709 if (needsPlt(Expr)) {
710 if (Body.isInPlt())
711 continue;
Eugene Leviantff23d3e2016-11-18 14:35:03 +0000712 In<ELFT>::Plt->addEntry(Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000713
714 uint32_t Rel;
715 if (Body.isGnuIFunc() && !Preemptible)
716 Rel = Target->IRelativeRel;
717 else
718 Rel = Target->PltRel;
719
Eugene Leviant41ca3272016-11-10 09:48:29 +0000720 In<ELFT>::GotPlt->addEntry(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000721 In<ELFT>::RelaPlt->addReloc({Rel, In<ELFT>::GotPlt,
722 Body.getGotPltOffset<ELFT>(), !Preemptible,
723 &Body, 0});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000724 continue;
725 }
726
727 if (refersToGotEntry(Expr)) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000728 if (Config->EMachine == EM_MIPS) {
Simon Atanasyanaf52f6a2016-09-08 09:07:12 +0000729 // MIPS ABI has special rules to process GOT entries and doesn't
730 // require relocation entries for them. A special case is TLS
731 // relocations. In that case dynamic loader applies dynamic
732 // relocations to initialize TLS GOT entries.
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000733 // See "Global Offset Table" in Chapter 5 in the following document
734 // for detailed description:
735 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Simon Atanasyan725dc142016-11-16 21:01:02 +0000736 In<ELFT>::MipsGot->addEntry(Body, Addend, Expr);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000737 if (Body.isTls() && Body.isPreemptible())
Simon Atanasyan725dc142016-11-16 21:01:02 +0000738 AddDyn({Target->TlsGotRel, In<ELFT>::MipsGot,
739 Body.getGotOffset<ELFT>(), false, &Body, 0});
Simon Atanasyan41325112016-06-19 21:39:37 +0000740 continue;
741 }
742
743 if (Body.isInGot())
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000744 continue;
745
Eugene Leviantad4439e2016-11-11 11:33:32 +0000746 In<ELFT>::Got->addEntry(Body);
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000747 uintX_t Off = Body.getGotOffset<ELFT>();
748 uint32_t DynType;
749 if (Body.isTls())
750 DynType = Target->TlsGotRel;
751 else if (!Preemptible && Config->Pic && !isAbsolute<ELFT>(Body))
752 DynType = Target->RelativeRel;
753 else
754 DynType = Target->GotRel;
755
756 if (Preemptible || (Config->Pic && !isAbsolute<ELFT>(Body)))
757 AddDyn({DynType, In<ELFT>::Got, Off, !Preemptible, &Body, 0});
758 else
759 In<ELFT>::Got->Relocations.push_back({R_ABS, DynType, Off, 0, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000760 continue;
761 }
762 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000763}
764
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000765template <class ELFT> void scanRelocations(InputSectionBase<ELFT> &S) {
766 if (S.AreRelocsRela)
767 scanRelocs(S, S.relas());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000768 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000769 scanRelocs(S, S.rels());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000770}
771
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000772template <class ELFT, class RelTy>
773static void createThunks(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
Vitaly Buka029d7302016-11-15 07:32:51 +0000774 const elf::ObjectFile<ELFT> *File = C.getFile();
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000775 for (const RelTy &Rel : Rels) {
Vitaly Buka029d7302016-11-15 07:32:51 +0000776 SymbolBody &Body = File->getRelocTargetSym(Rel);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000777 uint32_t Type = Rel.getType(Config->Mips64EL);
778 RelExpr Expr = Target->getRelExpr(Type, Body);
779 if (!isPreemptible(Body, Type) && needsPlt(Expr))
780 Expr = fromPlt(Expr);
Vitaly Buka029d7302016-11-15 07:32:51 +0000781 Expr = Target->getThunkExpr(Expr, Type, *File, Body);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000782 // Some targets might require creation of thunks for relocations.
783 // Now we support only MIPS which requires LA25 thunk to call PIC
784 // code from non-PIC one, and ARM which requires interworking.
785 if (Expr == R_THUNK_ABS || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC) {
786 auto *Sec = cast<InputSection<ELFT>>(&C);
787 addThunk<ELFT>(Type, Body, *Sec);
788 }
789 }
790}
791
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000792template <class ELFT> void createThunks(InputSectionBase<ELFT> &S) {
793 if (S.AreRelocsRela)
794 createThunks(S, S.relas());
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000795 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000796 createThunks(S, S.rels());
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000797}
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000798
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000799template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &);
800template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &);
801template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &);
802template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000803
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000804template void createThunks<ELF32LE>(InputSectionBase<ELF32LE> &);
805template void createThunks<ELF32BE>(InputSectionBase<ELF32BE> &);
806template void createThunks<ELF64LE>(InputSectionBase<ELF64LE> &);
807template void createThunks<ELF64BE>(InputSectionBase<ELF64BE> &);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000808}
809}