blob: 11c1552deaf10a7bade7c38a56dce1a22a46b6ce [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) {
Sean Silva2eed7592016-12-01 05:43:48 +000065 return isRelExprOneOf<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
66 R_MIPS_GOT_OFF32, R_MIPS_TLSGD, R_MIPS_TLSLD,
67 R_GOT_PAGE_PC, R_GOT_PC, R_GOT_FROM_END, R_TLSGD,
68 R_TLSGD_PC, R_TLSDESC, R_TLSDESC_PAGE>(Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +000069}
70
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000071static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
72 // In case of MIPS GP-relative relocations always resolve to a definition
73 // in a regular input file, ignoring the one-definition rule. So we,
74 // for example, should not attempt to create a dynamic relocation even
75 // if the target symbol is preemptible. There are two two MIPS GP-relative
76 // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
77 // can be against a preemptible symbol.
Simon Atanasyana26a1572016-06-10 12:26:09 +000078 // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000079 // relocation types occupy eight bit. In case of N64 ABI we extract first
80 // relocation from 3-in-1 packet because only the first relocation can
81 // be against a real symbol.
Simon Atanasyana26a1572016-06-10 12:26:09 +000082 if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16)
Simon Atanasyan9a9a3162016-05-28 04:49:57 +000083 return false;
84 return Body.isPreemptible();
85}
86
Peter Smithfde62132016-09-23 13:54:48 +000087// This function is similar to the `handleTlsRelocation`. ARM and MIPS do not
88// support any relaxations for TLS relocations so by factoring out ARM and MIPS
89// handling in to the separate function we can simplify the code and do not
90// pollute `handleTlsRelocation` by ARM and MIPS `ifs` statements.
Simon Atanasyan725dc142016-11-16 21:01:02 +000091template <class ELFT, class GOT>
92static unsigned handleNoRelaxTlsRelocation(
93 GOT *Got, uint32_t Type, SymbolBody &Body, InputSectionBase<ELFT> &C,
94 typename ELFT::uint Offset, typename ELFT::uint Addend, RelExpr Expr) {
Peter Smithde3e7382016-11-29 16:23:50 +000095 typedef typename ELFT::uint uintX_t;
96 auto addModuleReloc = [](SymbolBody &Body, GOT *Got, uintX_t Off, bool LD) {
97 // The Dynamic TLS Module Index Relocation can be statically resolved to 1
98 // if we know that we are linking an executable. For ARM we resolve the
99 // relocation when writing the Got. MIPS has a custom Got implementation
100 // that writes the Module index in directly.
101 if (!Body.isPreemptible() && !Config->Pic && Config->EMachine == EM_ARM)
102 Got->Relocations.push_back(
103 {R_ABS, Target->TlsModuleIndexRel, Off, 0, &Body});
104 else {
105 SymbolBody *Dest = LD ? nullptr : &Body;
106 In<ELFT>::RelaDyn->addReloc(
107 {Target->TlsModuleIndexRel, Got, Off, false, Dest, 0});
108 }
109 };
Peter Smithfde62132016-09-23 13:54:48 +0000110 if (Expr == R_MIPS_TLSLD || Expr == R_TLSLD_PC) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000111 if (Got->addTlsIndex() && (Config->Pic || Config->EMachine == EM_ARM))
Peter Smithde3e7382016-11-29 16:23:50 +0000112 addModuleReloc(Body, Got, Got->getTlsIndexOff(), true);
Rafael Espindola664c6522016-09-07 20:37:34 +0000113 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000114 return 1;
115 }
116 if (Target->isTlsGlobalDynamicRel(Type)) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000117 if (Got->addDynTlsEntry(Body) &&
Peter Smithfde62132016-09-23 13:54:48 +0000118 (Body.isPreemptible() || Config->EMachine == EM_ARM)) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000119 uintX_t Off = Got->getGlobalDynOffset(Body);
Peter Smithde3e7382016-11-29 16:23:50 +0000120 addModuleReloc(Body, Got, Off, false);
Peter Smithfde62132016-09-23 13:54:48 +0000121 if (Body.isPreemptible())
Simon Atanasyan725dc142016-11-16 21:01:02 +0000122 In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Got,
Eugene Levianta96d9022016-11-16 10:02:27 +0000123 Off + (uintX_t)sizeof(uintX_t), false,
124 &Body, 0});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000125 }
Rafael Espindola664c6522016-09-07 20:37:34 +0000126 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Simon Atanasyan002e2442016-06-23 15:26:31 +0000127 return 1;
128 }
129 return 0;
130}
131
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000132// Returns the number of relocations processed.
133template <class ELFT>
134static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body,
135 InputSectionBase<ELFT> &C,
136 typename ELFT::uint Offset,
137 typename ELFT::uint Addend, RelExpr Expr) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000138 if (!(C.Flags & SHF_ALLOC))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000139 return 0;
140
141 if (!Body.isTls())
142 return 0;
143
144 typedef typename ELFT::uint uintX_t;
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000145
Simon Atanasyan725dc142016-11-16 21:01:02 +0000146 if (Config->EMachine == EM_ARM)
147 return handleNoRelaxTlsRelocation<ELFT>(In<ELFT>::Got, Type, Body, C,
148 Offset, Addend, Expr);
149 if (Config->EMachine == EM_MIPS)
150 return handleNoRelaxTlsRelocation<ELFT>(In<ELFT>::MipsGot, Type, Body, C,
151 Offset, Addend, Expr);
Simon Atanasyan002e2442016-06-23 15:26:31 +0000152
Peter Smithd6486032016-10-20 09:59:26 +0000153 if ((Expr == R_TLSDESC || Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC_CALL) &&
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000154 Config->Shared) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000155 if (In<ELFT>::Got->addDynTlsEntry(Body)) {
156 uintX_t Off = In<ELFT>::Got->getGlobalDynOffset(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000157 In<ELFT>::RelaDyn->addReloc(
Eugene Leviantad4439e2016-11-11 11:33:32 +0000158 {Target->TlsDescRel, In<ELFT>::Got, Off, false, &Body, 0});
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000159 }
Peter Smithd6486032016-10-20 09:59:26 +0000160 if (Expr != R_TLSDESC_CALL)
Rafael Espindola664c6522016-09-07 20:37:34 +0000161 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000162 return 1;
163 }
164
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000165 if (Expr == R_TLSLD_PC || Expr == R_TLSLD) {
166 // Local-Dynamic relocs can be relaxed to Local-Exec.
167 if (!Config->Shared) {
168 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000169 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000170 return 2;
171 }
Eugene Leviantad4439e2016-11-11 11:33:32 +0000172 if (In<ELFT>::Got->addTlsIndex())
Eugene Levianta96d9022016-11-16 10:02:27 +0000173 In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, In<ELFT>::Got,
174 In<ELFT>::Got->getTlsIndexOff(), false,
175 nullptr, 0});
Rafael Espindola664c6522016-09-07 20:37:34 +0000176 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000177 return 1;
178 }
179
180 // Local-Dynamic relocs can be relaxed to Local-Exec.
181 if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) {
182 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000183 {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000184 return 1;
185 }
186
Peter Smithd6486032016-10-20 09:59:26 +0000187 if (Expr == R_TLSDESC_PAGE || Expr == R_TLSDESC || Expr == R_TLSDESC_CALL ||
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000188 Target->isTlsGlobalDynamicRel(Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000189 if (Config->Shared) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000190 if (In<ELFT>::Got->addDynTlsEntry(Body)) {
191 uintX_t Off = In<ELFT>::Got->getGlobalDynOffset(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000192 In<ELFT>::RelaDyn->addReloc(
Eugene Leviantad4439e2016-11-11 11:33:32 +0000193 {Target->TlsModuleIndexRel, In<ELFT>::Got, Off, false, &Body, 0});
Rafael Espindolaa8777c22016-06-08 21:31:59 +0000194
195 // If the symbol is preemptible we need the dynamic linker to write
196 // the offset too.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000197 uintX_t OffsetOff = Off + (uintX_t)sizeof(uintX_t);
Simon Atanasyan9b861182016-06-10 12:26:39 +0000198 if (isPreemptible(Body, Type))
Eugene Levianta96d9022016-11-16 10:02:27 +0000199 In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, In<ELFT>::Got,
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000200 OffsetOff, false, &Body, 0});
201 else
202 In<ELFT>::Got->Relocations.push_back(
203 {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000204 }
Rafael Espindola664c6522016-09-07 20:37:34 +0000205 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000206 return 1;
207 }
208
209 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
210 // depending on the symbol being locally defined or not.
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000211 if (isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000212 C.Relocations.push_back(
Rafael Espindola69f54022016-06-04 23:22:34 +0000213 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
Rafael Espindola664c6522016-09-07 20:37:34 +0000214 Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000215 if (!Body.isInGot()) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000216 In<ELFT>::Got->addEntry(Body);
Eugene Levianta96d9022016-11-16 10:02:27 +0000217 In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, In<ELFT>::Got,
218 Body.getGotOffset<ELFT>(), false, &Body,
219 0});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000220 }
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000221 return Target->TlsGdRelaxSkip;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000222 }
223 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000224 {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
Rafael Espindola69f54022016-06-04 23:22:34 +0000225 Offset, Addend, &Body});
Rafael Espindolaf807d472016-06-04 23:04:39 +0000226 return Target->TlsGdRelaxSkip;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000227 }
228
229 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
230 // defined.
231 if (Target->isTlsInitialExecRel(Type) && !Config->Shared &&
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000232 !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000233 C.Relocations.push_back(
Rafael Espindola664c6522016-09-07 20:37:34 +0000234 {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000235 return 1;
236 }
237 return 0;
238}
239
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000240template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
241 return read32<E>(Loc) & 0xffff;
242}
243
244template <class RelTy>
245static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
246 switch (Rel->getType(Config->Mips64EL)) {
247 case R_MIPS_HI16:
248 return R_MIPS_LO16;
249 case R_MIPS_GOT16:
250 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
251 case R_MIPS_PCHI16:
252 return R_MIPS_PCLO16;
253 case R_MICROMIPS_HI16:
254 return R_MICROMIPS_LO16;
255 default:
256 return R_MIPS_NONE;
257 }
258}
259
260template <class ELFT, class RelTy>
261static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc,
262 SymbolBody &Sym, const RelTy *Rel,
263 const RelTy *End) {
264 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
265 uint32_t Type = getMipsPairType(Rel, Sym);
266
267 // Some MIPS relocations use addend calculated from addend of the relocation
268 // itself and addend of paired relocation. ABI requires to compute such
269 // combined addend in case of REL relocation record format only.
270 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
271 if (RelTy::IsRela || Type == R_MIPS_NONE)
272 return 0;
273
274 for (const RelTy *RI = Rel; RI != End; ++RI) {
275 if (RI->getType(Config->Mips64EL) != Type)
276 continue;
277 if (RI->getSymbol(Config->Mips64EL) != SymIndex)
278 continue;
279 const endianness E = ELFT::TargetEndianness;
280 return ((read32<E>(BufLoc) & 0xffff) << 16) +
281 readSignedLo16<E>(Buf + RI->r_offset);
282 }
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000283 warn("can't find matching " + toString(Type) + " relocation for " +
284 toString(Rel->getType(Config->Mips64EL)));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000285 return 0;
286}
287
288// True if non-preemptable symbol always has the same value regardless of where
289// the DSO is loaded.
290template <class ELFT> static bool isAbsolute(const SymbolBody &Body) {
291 if (Body.isUndefined())
292 return !Body.isLocal() && Body.symbol()->isWeak();
293 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(&Body))
294 return DR->Section == nullptr; // Absolute symbol.
295 return false;
296}
297
Rafael Espindolad598c812016-10-27 17:28:56 +0000298template <class ELFT> static bool isAbsoluteValue(const SymbolBody &Body) {
299 return isAbsolute<ELFT>(Body) || Body.isTls();
300}
301
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000302static bool needsPlt(RelExpr Expr) {
Sean Silva2eed7592016-12-01 05:43:48 +0000303 return isRelExprOneOf<R_PLT_PC, R_PPC_PLT_OPD, R_PLT, R_PLT_PAGE_PC,
304 R_THUNK_PLT_PC>(Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000305}
306
307// True if this expression is of the form Sym - X, where X is a position in the
308// file (PC, or GOT for example).
309static bool isRelExpr(RelExpr Expr) {
Sean Silva2eed7592016-12-01 05:43:48 +0000310 return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL,
311 R_PAGE_PC, R_RELAX_GOT_PC, R_THUNK_PC, R_THUNK_PLT_PC>(
312 Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000313}
314
315template <class ELFT>
316static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
George Rimar463984d2016-11-15 08:07:14 +0000317 const SymbolBody &Body,
318 InputSectionBase<ELFT> &S,
319 typename ELFT::uint RelOff) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000320 // These expressions always compute a constant
Sean Silva2eed7592016-12-01 05:43:48 +0000321 if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
322 R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_TLSGD,
323 R_GOT_PAGE_PC, R_GOT_PC, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
324 R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT,
325 R_THUNK_PC, R_THUNK_PLT_PC>(E))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000326 return true;
327
328 // These never do, except if the entire file is position dependent or if
329 // only the low bits are used.
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000330 if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000331 return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
332
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000333 if (isPreemptible(Body, Type))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000334 return false;
335
336 if (!Config->Pic)
337 return true;
338
Rafael Espindolad598c812016-10-27 17:28:56 +0000339 bool AbsVal = isAbsoluteValue<ELFT>(Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000340 bool RelE = isRelExpr(E);
341 if (AbsVal && !RelE)
342 return true;
343 if (!AbsVal && RelE)
344 return true;
345
346 // Relative relocation to an absolute value. This is normally unrepresentable,
347 // but if the relocation refers to a weak undefined symbol, we allow it to
348 // resolve to the image base. This is a little strange, but it allows us to
349 // link function calls to such symbols. Normally such a call will be guarded
350 // with a comparison, which will load a zero from the GOT.
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000351 // Another special case is MIPS _gp_disp symbol which represents offset
352 // between start of a function and '_gp' value and defined as absolute just
353 // to simplify the code.
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000354 if (AbsVal && RelE) {
355 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
356 return true;
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000357 if (&Body == ElfSym<ELFT>::MipsGpDisp)
358 return true;
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000359 error(S.getLocation(RelOff) + ": relocation " + toString(Type) +
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000360 " cannot refer to absolute symbol '" + toString(Body) +
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000361 "' defined in " + toString(Body.File));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000362 return true;
363 }
364
365 return Target->usesOnlyLowPageBits(Type);
366}
367
368static RelExpr toPlt(RelExpr Expr) {
369 if (Expr == R_PPC_OPD)
370 return R_PPC_PLT_OPD;
371 if (Expr == R_PC)
372 return R_PLT_PC;
Rafael Espindola12dc4462016-06-04 19:11:14 +0000373 if (Expr == R_PAGE_PC)
374 return R_PLT_PAGE_PC;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000375 if (Expr == R_ABS)
376 return R_PLT;
377 return Expr;
378}
379
380static RelExpr fromPlt(RelExpr Expr) {
381 // We decided not to use a plt. Optimize a reference to the plt to a
382 // reference to the symbol itself.
383 if (Expr == R_PLT_PC)
384 return R_PC;
385 if (Expr == R_PPC_PLT_OPD)
386 return R_PPC_OPD;
387 if (Expr == R_PLT)
388 return R_ABS;
389 return Expr;
390}
391
392template <class ELFT> static uint32_t getAlignment(SharedSymbol<ELFT> *SS) {
393 typedef typename ELFT::uint uintX_t;
394
Rui Ueyama434b5612016-07-17 03:11:46 +0000395 uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000396 uintX_t SymValue = SS->Sym.st_value;
397 int TrailingZeros =
398 std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue));
399 return 1 << TrailingZeros;
400}
401
402// Reserve space in .bss for copy relocation.
403template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
404 typedef typename ELFT::uint uintX_t;
405 typedef typename ELFT::Sym Elf_Sym;
406
407 // Copy relocation against zero-sized symbol doesn't make sense.
408 uintX_t SymSize = SS->template getSize<ELFT>();
409 if (SymSize == 0)
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000410 fatal("cannot create a copy relocation for symbol " + toString(*SS));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000411
Rui Ueyama424b4082016-06-17 01:18:46 +0000412 uintX_t Alignment = getAlignment(SS);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000413 uintX_t Off = alignTo(Out<ELFT>::Bss->Size, Alignment);
414 Out<ELFT>::Bss->Size = Off + SymSize;
Rui Ueyama424b4082016-06-17 01:18:46 +0000415 Out<ELFT>::Bss->updateAlignment(Alignment);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000416 uintX_t Shndx = SS->Sym.st_shndx;
417 uintX_t Value = SS->Sym.st_value;
418 // Look through the DSO's dynamic symbol table for aliases and create a
419 // dynamic symbol for each one. This causes the copy relocation to correctly
420 // interpose any aliases.
Rafael Espindola8e232572016-11-03 20:48:57 +0000421 for (const Elf_Sym &S : SS->file()->getGlobalSymbols()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000422 if (S.st_shndx != Shndx || S.st_value != Value)
423 continue;
424 auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>(
Rui Ueyama434b5612016-07-17 03:11:46 +0000425 Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable()))));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000426 if (!Alias)
427 continue;
428 Alias->OffsetInBss = Off;
429 Alias->NeedsCopyOrPltAddr = true;
430 Alias->symbol()->IsUsedInRegularObj = true;
431 }
Eugene Levianta96d9022016-11-16 10:02:27 +0000432 In<ELFT>::RelaDyn->addReloc(
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000433 {Target->CopyRel, Out<ELFT>::Bss, SS->OffsetInBss, false, SS, 0});
434}
435
436template <class ELFT>
437static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
George Rimar5c33b912016-05-25 14:31:37 +0000438 bool IsWrite, RelExpr Expr, uint32_t Type,
George Rimar463984d2016-11-15 08:07:14 +0000439 const uint8_t *Data, InputSectionBase<ELFT> &S,
440 typename ELFT::uint RelOff) {
Simon Atanasyan9a9a3162016-05-28 04:49:57 +0000441 bool Preemptible = isPreemptible(Body, Type);
George Rimar5c33b912016-05-25 14:31:37 +0000442 if (Body.isGnuIFunc()) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000443 Expr = toPlt(Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000444 } else if (!Preemptible) {
445 if (needsPlt(Expr))
446 Expr = fromPlt(Expr);
Rafael Espindolad598c812016-10-27 17:28:56 +0000447 if (Expr == R_GOT_PC && !isAbsoluteValue<ELFT>(Body))
Rafael Espindolaf2956a32016-06-17 15:01:50 +0000448 Expr = Target->adjustRelaxExpr(Type, Data, Expr);
George Rimar5c33b912016-05-25 14:31:37 +0000449 }
Peter Smithfb05cd92016-07-08 16:10:27 +0000450 Expr = Target->getThunkExpr(Expr, Type, File, Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000451
George Rimar463984d2016-11-15 08:07:14 +0000452 if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff))
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000453 return Expr;
454
455 // This relocation would require the dynamic linker to write a value to read
456 // only memory. We can hack around it if we are producing an executable and
457 // the refered symbol can be preemepted to refer to the executable.
458 if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000459 error(S.getLocation(RelOff) + ": can't create dynamic relocation " +
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000460 toString(Type) + " against " +
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000461 (Body.getName().empty() ? "local symbol in readonly segment"
462 : "symbol '" + toString(Body) + "'") +
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000463 " defined in " + toString(Body.File));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000464 return Expr;
465 }
466 if (Body.getVisibility() != STV_DEFAULT) {
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000467 error(S.getLocation(RelOff) + ": cannot preempt symbol '" + toString(Body) +
468 "' defined in " + toString(Body.File));
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000469 return Expr;
470 }
471 if (Body.isObject()) {
472 // Produce a copy relocation.
473 auto *B = cast<SharedSymbol<ELFT>>(&Body);
474 if (!B->needsCopy())
475 addCopyRelSymbol(B);
476 return Expr;
477 }
478 if (Body.isFunc()) {
479 // This handles a non PIC program call to function in a shared library. In
480 // an ideal world, we could just report an error saying the relocation can
481 // overflow at runtime. In the real world with glibc, crt1.o has a
482 // R_X86_64_PC32 pointing to libc.so.
483 //
484 // The general idea on how to handle such cases is to create a PLT entry and
485 // use that as the function value.
486 //
487 // For the static linking part, we just return a plt expr and everything
488 // else will use the the PLT entry as the address.
489 //
490 // The remaining problem is making sure pointer equality still works. We
491 // need the help of the dynamic linker for that. We let it know that we have
492 // a direct reference to a so symbol by creating an undefined symbol with a
493 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
494 // the value of the symbol we created. This is true even for got entries, so
495 // pointer equality is maintained. To avoid an infinite loop, the only entry
496 // that points to the real function is a dedicated got entry used by the
497 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
498 // R_386_JMP_SLOT, etc).
499 Body.NeedsCopyOrPltAddr = true;
500 return toPlt(Expr);
501 }
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000502 error("symbol '" + toString(Body) + "' defined in " + toString(Body.File) +
George Rimar76f429b2016-11-16 17:24:06 +0000503 " is missing type");
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000504
505 return Expr;
506}
507
508template <class ELFT, class RelTy>
509static typename ELFT::uint computeAddend(const elf::ObjectFile<ELFT> &File,
510 const uint8_t *SectionData,
511 const RelTy *End, const RelTy &RI,
512 RelExpr Expr, SymbolBody &Body) {
513 typedef typename ELFT::uint uintX_t;
514
515 uint32_t Type = RI.getType(Config->Mips64EL);
516 uintX_t Addend = getAddend<ELFT>(RI);
517 const uint8_t *BufLoc = SectionData + RI.r_offset;
518 if (!RelTy::IsRela)
519 Addend += Target->getImplicitAddend(BufLoc, Type);
520 if (Config->EMachine == EM_MIPS) {
521 Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End);
522 if (Type == R_MIPS_LO16 && Expr == R_PC)
523 // R_MIPS_LO16 expression has R_PC type iif the target is _gp_disp
524 // symbol. In that case we should use the following formula for
525 // calculation "AHL + GP - P + 4". Let's add 4 right here.
526 // For details see p. 4-19 at
527 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
528 Addend += 4;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000529 if (Expr == R_MIPS_GOTREL && Body.isLocal())
530 Addend += File.MipsGp0;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000531 }
532 if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
533 Addend += getPPC64TocBase();
534 return Addend;
535}
536
Eugene Leviantb380b242016-10-26 11:07:09 +0000537template <class ELFT>
538static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &S,
539 typename ELFT::uint Offset) {
Eugene Leviant89837592016-10-06 09:45:04 +0000540 if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
541 return;
542
543 if (Config->Shared && Sym.symbol()->Visibility == STV_DEFAULT &&
544 Config->UnresolvedSymbols != UnresolvedPolicy::NoUndef)
545 return;
546
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000547 std::string Msg =
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000548 S.getLocation(Offset) + ": undefined symbol '" + toString(Sym) + "'";
Eugene Leviant89837592016-10-06 09:45:04 +0000549
Eugene Leviant89837592016-10-06 09:45:04 +0000550 if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn)
551 warn(Msg);
552 else
553 error(Msg);
554}
555
Simon Atanasyan9e0297b2016-11-05 22:58:01 +0000556template <class RelTy>
557static std::pair<uint32_t, uint32_t>
558mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) {
559 // MIPS N32 ABI treats series of successive relocations with the same offset
560 // as a single relocation. The similar approach used by N64 ABI, but this ABI
561 // packs all relocations into the single relocation record. Here we emulate
562 // this for the N32 ABI. Iterate over relocation with the same offset and put
563 // theirs types into the single bit-set.
564 uint32_t Processed = 0;
565 for (; I != E && Offset == I->r_offset; ++I) {
566 ++Processed;
567 Type |= I->getType(Config->Mips64EL) << (8 * Processed);
568 }
569 return std::make_pair(Type, Processed);
570}
571
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000572// The reason we have to do this early scan is as follows
573// * To mmap the output file, we need to know the size
574// * For that, we need to know how many dynamic relocs we will have.
575// It might be possible to avoid this by outputting the file with write:
576// * Write the allocated output sections, computing addresses.
577// * Apply relocations, recording which ones require a dynamic reloc.
578// * Write the dynamic relocations.
579// * Write the rest of the file.
580// This would have some drawbacks. For example, we would only know if .rela.dyn
581// is needed after applying relocations. If it is, it will go after rw and rx
582// sections. Given that it is ro, we will need an extra PT_LOAD. This
583// complicates things for the dynamic linker and means we would have to reserve
584// space for the extra PT_LOAD even if we end up not using it.
585template <class ELFT, class RelTy>
Rui Ueyama2487f192016-05-25 03:40:02 +0000586static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000587 typedef typename ELFT::uint uintX_t;
588
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000589 bool IsWrite = C.Flags & SHF_WRITE;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000590
591 auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
Eugene Levianta96d9022016-11-16 10:02:27 +0000592 In<ELFT>::RelaDyn->addReloc(Reloc);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000593 };
594
Vitaly Buka029d7302016-11-15 07:32:51 +0000595 const elf::ObjectFile<ELFT> *File = C.getFile();
Rafael Espindolac7e1e032016-09-12 13:13:53 +0000596 ArrayRef<uint8_t> SectionData = C.Data;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000597 const uint8_t *Buf = SectionData.begin();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000598
Rafael Espindola3abe3aa2016-07-21 21:15:32 +0000599 ArrayRef<EhSectionPiece> Pieces;
600 if (auto *Eh = dyn_cast<EhInputSection<ELFT>>(&C))
601 Pieces = Eh->Pieces;
602
603 ArrayRef<EhSectionPiece>::iterator PieceI = Pieces.begin();
604 ArrayRef<EhSectionPiece>::iterator PieceE = Pieces.end();
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000605
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000606 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
607 const RelTy &RI = *I;
Vitaly Buka029d7302016-11-15 07:32:51 +0000608 SymbolBody &Body = File->getRelocTargetSym(RI);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000609 uint32_t Type = RI.getType(Config->Mips64EL);
610
Simon Atanasyan9e0297b2016-11-05 22:58:01 +0000611 if (Config->MipsN32Abi) {
612 uint32_t Processed;
613 std::tie(Type, Processed) =
614 mergeMipsN32RelTypes(Type, RI.r_offset, I + 1, E);
615 I += Processed;
616 }
617
George Rimara4c7e742016-10-20 08:36:42 +0000618 // We only report undefined symbols if they are referenced somewhere in the
619 // code.
Eugene Leviant89837592016-10-06 09:45:04 +0000620 if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak())
Eugene Leviantb380b242016-10-26 11:07:09 +0000621 reportUndefined(Body, C, RI.r_offset);
Eugene Leviant89837592016-10-06 09:45:04 +0000622
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000623 RelExpr Expr = Target->getRelExpr(Type, Body);
Rafael Espindola678844e2016-06-17 15:42:36 +0000624 bool Preemptible = isPreemptible(Body, Type);
George Rimar463984d2016-11-15 08:07:14 +0000625 Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset, C,
626 RI.r_offset);
Rui Ueyamaf373dd72016-11-24 01:43:21 +0000627 if (ErrorCount)
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000628 continue;
629
Rui Ueyama809d8e22016-06-23 04:33:42 +0000630 // Skip a relocation that points to a dead piece
Rafael Espindola5b7a79f2016-07-20 11:47:50 +0000631 // in a eh_frame section.
632 while (PieceI != PieceE &&
633 (PieceI->InputOff + PieceI->size() <= RI.r_offset))
634 ++PieceI;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000635
636 // Compute the offset of this section in the output section. We do it here
637 // to try to compute it only once.
638 uintX_t Offset;
639 if (PieceI != PieceE) {
640 assert(PieceI->InputOff <= RI.r_offset && "Relocation not in any piece");
Rafael Espindola113860b2016-10-20 10:55:58 +0000641 if (PieceI->OutputOff == -1)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000642 continue;
643 Offset = PieceI->OutputOff + RI.r_offset - PieceI->InputOff;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000644 } else {
George Rimar3e6833b2016-08-19 15:46:28 +0000645 Offset = RI.r_offset;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000646 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000647
648 // This relocation does not require got entry, but it is relative to got and
649 // needs it to be created. Here we request for that.
Rafael Espindola79202c32016-08-31 23:24:11 +0000650 if (Expr == R_GOTONLY_PC || Expr == R_GOTONLY_PC_FROM_END ||
651 Expr == R_GOTREL || Expr == R_GOTREL_FROM_END || Expr == R_PPC_TOC)
Eugene Leviantad4439e2016-11-11 11:33:32 +0000652 In<ELFT>::Got->HasGotOffRel = true;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000653
Vitaly Buka029d7302016-11-15 07:32:51 +0000654 uintX_t Addend = computeAddend(*File, Buf, E, RI, Expr, Body);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000655
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000656 if (unsigned Processed =
657 handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000658 I += (Processed - 1);
659 continue;
660 }
661
Peter Smithd6486032016-10-20 09:59:26 +0000662 // Ignore "hint" and TLS Descriptor call relocation because they are
663 // only markers for relaxation.
Sean Silva2eed7592016-12-01 05:43:48 +0000664 if (isRelExprOneOf<R_HINT, R_TLSDESC_CALL>(Expr))
Rafael Espindolae37d13b2016-06-02 19:49:53 +0000665 continue;
666
Sean Silva2eed7592016-12-01 05:43:48 +0000667 if (needsPlt(Expr) ||
668 isRelExprOneOf<R_THUNK_ABS, R_THUNK_PC, R_THUNK_PLT_PC>(Expr) ||
669 refersToGotEntry(Expr) || !isPreemptible(Body, Type)) {
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000670 // If the relocation points to something in the file, we can process it.
George Rimar463984d2016-11-15 08:07:14 +0000671 bool Constant =
672 isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, C, RI.r_offset);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000673
674 // If the output being produced is position independent, the final value
675 // is still not known. In that case we still need some help from the
676 // dynamic linker. We can however do better than just copying the incoming
677 // relocation. We can process some of it and and just ask the dynamic
678 // linker to add the load address.
679 if (!Constant)
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000680 AddDyn({Target->RelativeRel, &C, Offset, true, &Body, Addend});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000681
682 // If the produced value is a constant, we just remember to write it
683 // when outputting this section. We also have to do it if the format
684 // uses Elf_Rel, since in that case the written value is the addend.
685 if (Constant || !RelTy::IsRela)
Rafael Espindola664c6522016-09-07 20:37:34 +0000686 C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000687 } else {
688 // We don't know anything about the finaly symbol. Just ask the dynamic
689 // linker to handle the relocation for us.
Eugene Leviantab024a32016-11-25 08:56:36 +0000690 if (!Target->isPicRel(Type))
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000691 error(C.getLocation(Offset) + ": relocation " + toString(Type) +
Eugene Leviantab024a32016-11-25 08:56:36 +0000692 " cannot be used against shared object; recompile with -fPIC.");
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000693 AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend});
Eugene Leviantab024a32016-11-25 08:56:36 +0000694
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000695 // MIPS ABI turns using of GOT and dynamic relocations inside out.
696 // While regular ABI uses dynamic relocations to fill up GOT entries
697 // MIPS ABI requires dynamic linker to fills up GOT entries using
698 // specially sorted dynamic symbol table. This affects even dynamic
699 // relocations against symbols which do not require GOT entries
700 // creation explicitly, i.e. do not have any GOT-relocations. So if
701 // a preemptible symbol has a dynamic relocation we anyway have
702 // to create a GOT entry for it.
703 // If a non-preemptible symbol has a dynamic relocation against it,
704 // dynamic linker takes it st_value, adds offset and writes down
705 // result of the dynamic relocation. In case of preemptible symbol
706 // dynamic linker performs symbol resolution, writes the symbol value
707 // to the GOT entry and reads the GOT entry when it needs to perform
708 // a dynamic relocation.
709 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
Simon Atanasyan41325112016-06-19 21:39:37 +0000710 if (Config->EMachine == EM_MIPS)
Simon Atanasyan725dc142016-11-16 21:01:02 +0000711 In<ELFT>::MipsGot->addEntry(Body, Addend, Expr);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000712 continue;
713 }
714
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000715 // At this point we are done with the relocated position. Some relocations
716 // also require us to create a got or plt entry.
717
718 // If a relocation needs PLT, we create a PLT and a GOT slot for the symbol.
719 if (needsPlt(Expr)) {
720 if (Body.isInPlt())
721 continue;
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000722
Peter Smithbaffdb82016-12-08 12:58:55 +0000723 if (Body.isGnuIFunc() && !Preemptible) {
724 In<ELFT>::Iplt->addEntry(Body);
725 In<ELFT>::IgotPlt->addEntry(Body);
726 In<ELFT>::RelaIplt->addReloc({Target->IRelativeRel, In<ELFT>::IgotPlt,
727 Body.getGotPltOffset<ELFT>(),
728 !Preemptible, &Body, 0});
729 } else {
730 In<ELFT>::Plt->addEntry(Body);
731 In<ELFT>::GotPlt->addEntry(Body);
732 In<ELFT>::RelaPlt->addReloc({Target->PltRel, In<ELFT>::GotPlt,
733 Body.getGotPltOffset<ELFT>(), !Preemptible,
734 &Body, 0});
735 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000736 continue;
737 }
738
739 if (refersToGotEntry(Expr)) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000740 if (Config->EMachine == EM_MIPS) {
Simon Atanasyanaf52f6a2016-09-08 09:07:12 +0000741 // MIPS ABI has special rules to process GOT entries and doesn't
742 // require relocation entries for them. A special case is TLS
743 // relocations. In that case dynamic loader applies dynamic
744 // relocations to initialize TLS GOT entries.
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000745 // See "Global Offset Table" in Chapter 5 in the following document
746 // for detailed description:
747 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Simon Atanasyan725dc142016-11-16 21:01:02 +0000748 In<ELFT>::MipsGot->addEntry(Body, Addend, Expr);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000749 if (Body.isTls() && Body.isPreemptible())
Simon Atanasyan725dc142016-11-16 21:01:02 +0000750 AddDyn({Target->TlsGotRel, In<ELFT>::MipsGot,
751 Body.getGotOffset<ELFT>(), false, &Body, 0});
Simon Atanasyan41325112016-06-19 21:39:37 +0000752 continue;
753 }
754
755 if (Body.isInGot())
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000756 continue;
757
Eugene Leviantad4439e2016-11-11 11:33:32 +0000758 In<ELFT>::Got->addEntry(Body);
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000759 uintX_t Off = Body.getGotOffset<ELFT>();
760 uint32_t DynType;
Peter Smithde3e7382016-11-29 16:23:50 +0000761 RelExpr GotRE = R_ABS;
762 if (Body.isTls()) {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000763 DynType = Target->TlsGotRel;
Peter Smithde3e7382016-11-29 16:23:50 +0000764 GotRE = R_TLS;
765 } else if (!Preemptible && Config->Pic && !isAbsolute<ELFT>(Body))
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000766 DynType = Target->RelativeRel;
767 else
768 DynType = Target->GotRel;
769
Rafael Espindolaf4ff80c2016-12-02 01:57:24 +0000770 // FIXME: this logic is almost duplicated above.
771 bool Constant = !Preemptible && !(Config->Pic && !isAbsolute<ELFT>(Body));
772 if (!Constant)
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000773 AddDyn({DynType, In<ELFT>::Got, Off, !Preemptible, &Body, 0});
Rafael Espindolae004d4b2016-12-06 12:19:24 +0000774 if (Constant || (!RelTy::IsRela && !Preemptible))
Peter Smithde3e7382016-11-29 16:23:50 +0000775 In<ELFT>::Got->Relocations.push_back({GotRE, DynType, Off, 0, &Body});
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000776 continue;
777 }
778 }
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000779}
780
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000781template <class ELFT> void scanRelocations(InputSectionBase<ELFT> &S) {
782 if (S.AreRelocsRela)
783 scanRelocs(S, S.relas());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000784 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000785 scanRelocs(S, S.rels());
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000786}
787
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000788template <class ELFT, class RelTy>
789static void createThunks(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
Vitaly Buka029d7302016-11-15 07:32:51 +0000790 const elf::ObjectFile<ELFT> *File = C.getFile();
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000791 for (const RelTy &Rel : Rels) {
Vitaly Buka029d7302016-11-15 07:32:51 +0000792 SymbolBody &Body = File->getRelocTargetSym(Rel);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000793 uint32_t Type = Rel.getType(Config->Mips64EL);
794 RelExpr Expr = Target->getRelExpr(Type, Body);
795 if (!isPreemptible(Body, Type) && needsPlt(Expr))
796 Expr = fromPlt(Expr);
Vitaly Buka029d7302016-11-15 07:32:51 +0000797 Expr = Target->getThunkExpr(Expr, Type, *File, Body);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000798 // Some targets might require creation of thunks for relocations.
799 // Now we support only MIPS which requires LA25 thunk to call PIC
800 // code from non-PIC one, and ARM which requires interworking.
801 if (Expr == R_THUNK_ABS || Expr == R_THUNK_PC || Expr == R_THUNK_PLT_PC) {
802 auto *Sec = cast<InputSection<ELFT>>(&C);
803 addThunk<ELFT>(Type, Body, *Sec);
804 }
805 }
806}
807
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000808template <class ELFT> void createThunks(InputSectionBase<ELFT> &S) {
809 if (S.AreRelocsRela)
810 createThunks(S, S.relas());
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000811 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000812 createThunks(S, S.rels());
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000813}
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000814
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000815template void scanRelocations<ELF32LE>(InputSectionBase<ELF32LE> &);
816template void scanRelocations<ELF32BE>(InputSectionBase<ELF32BE> &);
817template void scanRelocations<ELF64LE>(InputSectionBase<ELF64LE> &);
818template void scanRelocations<ELF64BE>(InputSectionBase<ELF64BE> &);
Rafael Espindola0f7ceda2016-07-20 17:58:07 +0000819
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000820template void createThunks<ELF32LE>(InputSectionBase<ELF32LE> &);
821template void createThunks<ELF32BE>(InputSectionBase<ELF32BE> &);
822template void createThunks<ELF64LE>(InputSectionBase<ELF64LE> &);
823template void createThunks<ELF64BE>(InputSectionBase<ELF64BE> &);
Rui Ueyama0fcdc732016-05-24 20:24:43 +0000824}
825}