Coverage Report

Created: 2019-01-18 03:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Relocations.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- 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
//
10
// This file contains platform-independent functions to process relocations.
11
// 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 "LinkerScript.h"
47
#include "OutputSections.h"
48
#include "SymbolTable.h"
49
#include "Symbols.h"
50
#include "SyntheticSections.h"
51
#include "Target.h"
52
#include "Thunks.h"
53
#include "lld/Common/ErrorHandler.h"
54
#include "lld/Common/Memory.h"
55
#include "lld/Common/Strings.h"
56
#include "llvm/ADT/SmallSet.h"
57
#include "llvm/Support/Endian.h"
58
#include "llvm/Support/raw_ostream.h"
59
#include <algorithm>
60
61
using namespace llvm;
62
using namespace llvm::ELF;
63
using namespace llvm::object;
64
using namespace llvm::support::endian;
65
66
using namespace lld;
67
using namespace lld::elf;
68
69
1
static Optional<std::string> getLinkerScriptLocation(const Symbol &Sym) {
70
1
  for (BaseCommand *Base : Script->SectionCommands)
71
22
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
72
1
      if (Cmd->Sym == &Sym)
73
1
        return Cmd->Location;
74
1
  
return None0
;
75
1
}
76
77
// Construct a message in the following format.
78
//
79
// >>> defined in /home/alice/src/foo.o
80
// >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
81
// >>>               /home/alice/src/bar.o:(.text+0x1)
82
static std::string getLocation(InputSectionBase &S, const Symbol &Sym,
83
52
                               uint64_t Off) {
84
52
  std::string Msg = "\n>>> defined in ";
85
52
  if (Sym.File)
86
51
    Msg += toString(Sym.File);
87
1
  else if (Optional<std::string> Loc = getLinkerScriptLocation(Sym))
88
1
    Msg += *Loc;
89
52
90
52
  Msg += "\n>>> referenced by ";
91
52
  std::string Src = S.getSrcMsg(Sym, Off);
92
52
  if (!Src.empty())
93
1
    Msg += Src + "\n>>>               ";
94
52
  return Msg + S.getObjMsg(Off);
95
52
}
96
97
// This function is similar to the `handleTlsRelocation`. MIPS does not
98
// support any relaxations for TLS relocations so by factoring out MIPS
99
// handling in to the separate function we can simplify the code and do not
100
// pollute other `handleTlsRelocation` by MIPS `ifs` statements.
101
// Mips has a custom MipsGotSection that handles the writing of GOT entries
102
// without dynamic relocations.
103
static unsigned handleMipsTlsRelocation(RelType Type, Symbol &Sym,
104
                                        InputSectionBase &C, uint64_t Offset,
105
43
                                        int64_t Addend, RelExpr Expr) {
106
43
  if (Expr == R_MIPS_TLSLD) {
107
6
    In.MipsGot->addTlsIndex(*C.File);
108
6
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
109
6
    return 1;
110
6
  }
111
37
  if (Expr == R_MIPS_TLSGD) {
112
12
    In.MipsGot->addDynTlsEntry(*C.File, Sym);
113
12
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
114
12
    return 1;
115
12
  }
116
25
  return 0;
117
25
}
118
119
// This function is similar to the `handleMipsTlsRelocation`. ARM also does not
120
// support any relaxations for TLS relocations. ARM is logically similar to Mips
121
// in how it handles TLS, but Mips uses its own custom GOT which handles some
122
// of the cases that ARM uses GOT relocations for.
123
//
124
// We look for TLS global dynamic and local dynamic relocations, these may
125
// require the generation of a pair of GOT entries that have associated
126
// dynamic relocations. When the results of the dynamic relocations can be
127
// resolved at static link time we do so. This is necessary for static linking
128
// as there will be no dynamic loader to resolve them at load-time.
129
//
130
// The pair of GOT entries created are of the form
131
// GOT[e0] Module Index (Used to find pointer to TLS block at run-time)
132
// GOT[e1] Offset of symbol in TLS block
133
template <class ELFT>
134
static unsigned handleARMTlsRelocation(RelType Type, Symbol &Sym,
135
                                       InputSectionBase &C, uint64_t Offset,
136
28
                                       int64_t Addend, RelExpr Expr) {
137
28
  // The Dynamic TLS Module Index Relocation for a symbol defined in an
138
28
  // executable is always 1. If the target Symbol is not preemptible then
139
28
  // we know the offset into the TLS block at static link time.
140
28
  bool NeedDynId = Sym.IsPreemptible || 
Config->Shared21
;
141
28
  bool NeedDynOff = Sym.IsPreemptible;
142
28
143
30
  auto AddTlsReloc = [&](uint64_t Off, RelType Type, Symbol *Dest, bool Dyn) {
144
30
    if (Dyn)
145
14
      In.RelaDyn->addReloc(Type, In.Got, Off, Dest);
146
16
    else
147
16
      In.Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
148
30
  };
Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::Symbol*, bool)::operator()(unsigned long long, unsigned int, lld::elf::Symbol*, bool) const
Line
Count
Source
143
30
  auto AddTlsReloc = [&](uint64_t Off, RelType Type, Symbol *Dest, bool Dyn) {
144
30
    if (Dyn)
145
14
      In.RelaDyn->addReloc(Type, In.Got, Off, Dest);
146
16
    else
147
16
      In.Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
148
30
  };
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::Symbol*, bool)::operator()(unsigned long long, unsigned int, lld::elf::Symbol*, bool) const
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::Symbol*, bool)::operator()(unsigned long long, unsigned int, lld::elf::Symbol*, bool) const
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::Symbol*, bool)::operator()(unsigned long long, unsigned int, lld::elf::Symbol*, bool) const
149
28
150
28
  // Local Dynamic is for access to module local TLS variables, while still
151
28
  // being suitable for being dynamically loaded via dlopen.
152
28
  // GOT[e0] is the module index, with a special value of 0 for the current
153
28
  // module. GOT[e1] is unused. There only needs to be one module index entry.
154
28
  if (Expr == R_TLSLD_PC && 
In.Got->addTlsIndex()2
) {
155
2
    AddTlsReloc(In.Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
156
2
                NeedDynId ? 
nullptr1
:
&Sym1
, NeedDynId);
157
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
158
2
    return 1;
159
2
  }
160
26
161
26
  // Global Dynamic is the most general purpose access model. When we know
162
26
  // the module index and offset of symbol in TLS block we can fill these in
163
26
  // using static GOT relocations.
164
26
  if (Expr == R_TLSGD_PC) {
165
14
    if (In.Got->addDynTlsEntry(Sym)) {
166
14
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
167
14
      AddTlsReloc(Off, Target->TlsModuleIndexRel, &Sym, NeedDynId);
168
14
      AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Sym,
169
14
                  NeedDynOff);
170
14
    }
171
14
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
172
14
    return 1;
173
14
  }
174
12
  return 0;
175
12
}
Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Line
Count
Source
136
28
                                       int64_t Addend, RelExpr Expr) {
137
28
  // The Dynamic TLS Module Index Relocation for a symbol defined in an
138
28
  // executable is always 1. If the target Symbol is not preemptible then
139
28
  // we know the offset into the TLS block at static link time.
140
28
  bool NeedDynId = Sym.IsPreemptible || 
Config->Shared21
;
141
28
  bool NeedDynOff = Sym.IsPreemptible;
142
28
143
28
  auto AddTlsReloc = [&](uint64_t Off, RelType Type, Symbol *Dest, bool Dyn) {
144
28
    if (Dyn)
145
28
      In.RelaDyn->addReloc(Type, In.Got, Off, Dest);
146
28
    else
147
28
      In.Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
148
28
  };
149
28
150
28
  // Local Dynamic is for access to module local TLS variables, while still
151
28
  // being suitable for being dynamically loaded via dlopen.
152
28
  // GOT[e0] is the module index, with a special value of 0 for the current
153
28
  // module. GOT[e1] is unused. There only needs to be one module index entry.
154
28
  if (Expr == R_TLSLD_PC && 
In.Got->addTlsIndex()2
) {
155
2
    AddTlsReloc(In.Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
156
2
                NeedDynId ? 
nullptr1
:
&Sym1
, NeedDynId);
157
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
158
2
    return 1;
159
2
  }
160
26
161
26
  // Global Dynamic is the most general purpose access model. When we know
162
26
  // the module index and offset of symbol in TLS block we can fill these in
163
26
  // using static GOT relocations.
164
26
  if (Expr == R_TLSGD_PC) {
165
14
    if (In.Got->addDynTlsEntry(Sym)) {
166
14
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
167
14
      AddTlsReloc(Off, Target->TlsModuleIndexRel, &Sym, NeedDynId);
168
14
      AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Sym,
169
14
                  NeedDynOff);
170
14
    }
171
14
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
172
14
    return 1;
173
14
  }
174
12
  return 0;
175
12
}
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
176
177
// Returns the number of relocations processed.
178
template <class ELFT>
179
static unsigned
180
handleTlsRelocation(RelType Type, Symbol &Sym, InputSectionBase &C,
181
13.7k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
182
13.7k
  if (!Sym.isTls())
183
13.3k
    return 0;
184
455
185
455
  if (Config->EMachine == EM_ARM)
186
28
    return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
187
427
  if (Config->EMachine == EM_MIPS)
188
43
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
189
384
190
384
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
191
384
      
Config->Shared40
) {
192
22
    if (In.Got->addDynTlsEntry(Sym)) {
193
5
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
194
5
      In.RelaDyn->addReloc(
195
5
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
196
5
    }
197
22
    if (Expr != R_TLSDESC_CALL)
198
15
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
199
22
    return 1;
200
22
  }
201
362
202
362
  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC,
203
362
                     R_TLSLD_HINT>(Expr)) {
204
46
    // Local-Dynamic relocs can be relaxed to Local-Exec.
205
46
    if (!Config->Shared) {
206
13
      C.Relocations.push_back(
207
13
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
208
13
           Offset, Addend, &Sym});
209
13
      return Target->TlsGdRelaxSkip;
210
13
    }
211
33
    if (Expr == R_TLSLD_HINT)
212
8
      return 1;
213
25
    if (In.Got->addTlsIndex())
214
7
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
215
7
                           In.Got->getTlsIndexOff(), nullptr);
216
25
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
217
25
    return 1;
218
25
  }
219
316
220
316
  // Local-Dynamic relocs can be relaxed to Local-Exec.
221
316
  if (Expr == R_ABS && 
!Config->Shared45
) {
222
11
    C.Relocations.push_back(
223
11
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
224
11
         Offset, Addend, &Sym});
225
11
    return 1;
226
11
  }
227
305
228
305
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
229
305
  // thread pointer is stored in the got.
230
305
  if (Expr == R_TLSLD_GOT_OFF) {
231
8
    // Local-Dynamic relocs can be relaxed to local-exec
232
8
    if (!Config->Shared) {
233
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
234
0
      return 1;
235
0
    }
236
8
    if (!Sym.isInGot()) {
237
2
      In.Got->addEntry(Sym);
238
2
      uint64_t Off = Sym.getGotOffset();
239
2
      In.Got->Relocations.push_back(
240
2
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
241
2
    }
242
8
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
243
8
    return 1;
244
8
  }
245
297
246
297
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL,
247
297
                     R_TLSGD_GOT, R_TLSGD_GOT_FROM_END, R_TLSGD_PC>(Expr)) {
248
70
    if (Config->Shared) {
249
16
      if (In.Got->addDynTlsEntry(Sym)) {
250
13
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
251
13
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
252
13
253
13
        // If the symbol is preemptible we need the dynamic linker to write
254
13
        // the offset too.
255
13
        uint64_t OffsetOff = Off + Config->Wordsize;
256
13
        if (Sym.IsPreemptible)
257
10
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
258
3
        else
259
3
          In.Got->Relocations.push_back(
260
3
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
261
13
      }
262
16
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
263
16
      return 1;
264
16
    }
265
54
266
54
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
54
    // depending on the symbol being locally defined or not.
268
54
    if (Sym.IsPreemptible) {
269
30
      C.Relocations.push_back(
270
30
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
271
30
           Offset, Addend, &Sym});
272
30
      if (!Sym.isInGot()) {
273
12
        In.Got->addEntry(Sym);
274
12
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
275
12
                             &Sym);
276
12
      }
277
30
    } else {
278
24
      C.Relocations.push_back(
279
24
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
280
24
           Offset, Addend, &Sym});
281
24
    }
282
54
    return Target->TlsGdRelaxSkip;
283
54
  }
284
227
285
227
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
227
  // defined.
287
227
  if (isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_AARCH64_GOT_PAGE_PC,
288
227
                     R_GOT_OFF, R_TLSIE_HINT>(Expr) &&
289
227
      
!Config->Shared148
&&
!Sym.IsPreemptible130
) {
290
110
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
291
110
    return 1;
292
110
  }
293
117
294
117
  if (Expr == R_TLSIE_HINT)
295
2
    return 1;
296
115
  return 0;
297
115
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
181
916
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
182
916
  if (!Sym.isTls())
183
841
    return 0;
184
75
185
75
  if (Config->EMachine == EM_ARM)
186
28
    return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
187
47
  if (Config->EMachine == EM_MIPS)
188
0
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
189
47
190
47
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
191
47
      
Config->Shared0
) {
192
0
    if (In.Got->addDynTlsEntry(Sym)) {
193
0
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
194
0
      In.RelaDyn->addReloc(
195
0
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
196
0
    }
197
0
    if (Expr != R_TLSDESC_CALL)
198
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
199
0
    return 1;
200
0
  }
201
47
202
47
  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC,
203
47
                     R_TLSLD_HINT>(Expr)) {
204
4
    // Local-Dynamic relocs can be relaxed to Local-Exec.
205
4
    if (!Config->Shared) {
206
2
      C.Relocations.push_back(
207
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
208
2
           Offset, Addend, &Sym});
209
2
      return Target->TlsGdRelaxSkip;
210
2
    }
211
2
    if (Expr == R_TLSLD_HINT)
212
0
      return 1;
213
2
    if (In.Got->addTlsIndex())
214
1
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
215
1
                           In.Got->getTlsIndexOff(), nullptr);
216
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
217
2
    return 1;
218
2
  }
219
43
220
43
  // Local-Dynamic relocs can be relaxed to Local-Exec.
221
43
  if (Expr == R_ABS && 
!Config->Shared4
) {
222
2
    C.Relocations.push_back(
223
2
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
224
2
         Offset, Addend, &Sym});
225
2
    return 1;
226
2
  }
227
41
228
41
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
229
41
  // thread pointer is stored in the got.
230
41
  if (Expr == R_TLSLD_GOT_OFF) {
231
0
    // Local-Dynamic relocs can be relaxed to local-exec
232
0
    if (!Config->Shared) {
233
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
234
0
      return 1;
235
0
    }
236
0
    if (!Sym.isInGot()) {
237
0
      In.Got->addEntry(Sym);
238
0
      uint64_t Off = Sym.getGotOffset();
239
0
      In.Got->Relocations.push_back(
240
0
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
241
0
    }
242
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
243
0
    return 1;
244
0
  }
245
41
246
41
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL,
247
41
                     R_TLSGD_GOT, R_TLSGD_GOT_FROM_END, R_TLSGD_PC>(Expr)) {
248
6
    if (Config->Shared) {
249
2
      if (In.Got->addDynTlsEntry(Sym)) {
250
2
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
251
2
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
252
2
253
2
        // If the symbol is preemptible we need the dynamic linker to write
254
2
        // the offset too.
255
2
        uint64_t OffsetOff = Off + Config->Wordsize;
256
2
        if (Sym.IsPreemptible)
257
2
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
258
0
        else
259
0
          In.Got->Relocations.push_back(
260
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
261
2
      }
262
2
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
263
2
      return 1;
264
2
    }
265
4
266
4
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
4
    // depending on the symbol being locally defined or not.
268
4
    if (Sym.IsPreemptible) {
269
2
      C.Relocations.push_back(
270
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
271
2
           Offset, Addend, &Sym});
272
2
      if (!Sym.isInGot()) {
273
2
        In.Got->addEntry(Sym);
274
2
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
275
2
                             &Sym);
276
2
      }
277
2
    } else {
278
2
      C.Relocations.push_back(
279
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
280
2
           Offset, Addend, &Sym});
281
2
    }
282
4
    return Target->TlsGdRelaxSkip;
283
4
  }
284
35
285
35
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
35
  // defined.
287
35
  if (isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_AARCH64_GOT_PAGE_PC,
288
35
                     R_GOT_OFF, R_TLSIE_HINT>(Expr) &&
289
35
      
!Config->Shared25
&&
!Sym.IsPreemptible13
) {
290
10
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
291
10
    return 1;
292
10
  }
293
25
294
25
  if (Expr == R_TLSIE_HINT)
295
0
    return 1;
296
25
  return 0;
297
25
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
181
269
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
182
269
  if (!Sym.isTls())
183
239
    return 0;
184
30
185
30
  if (Config->EMachine == EM_ARM)
186
0
    return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
187
30
  if (Config->EMachine == EM_MIPS)
188
30
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
189
0
190
0
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
191
0
      Config->Shared) {
192
0
    if (In.Got->addDynTlsEntry(Sym)) {
193
0
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
194
0
      In.RelaDyn->addReloc(
195
0
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
196
0
    }
197
0
    if (Expr != R_TLSDESC_CALL)
198
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
199
0
    return 1;
200
0
  }
201
0
202
0
  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC,
203
0
                     R_TLSLD_HINT>(Expr)) {
204
0
    // Local-Dynamic relocs can be relaxed to Local-Exec.
205
0
    if (!Config->Shared) {
206
0
      C.Relocations.push_back(
207
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
208
0
           Offset, Addend, &Sym});
209
0
      return Target->TlsGdRelaxSkip;
210
0
    }
211
0
    if (Expr == R_TLSLD_HINT)
212
0
      return 1;
213
0
    if (In.Got->addTlsIndex())
214
0
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
215
0
                           In.Got->getTlsIndexOff(), nullptr);
216
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
217
0
    return 1;
218
0
  }
219
0
220
0
  // Local-Dynamic relocs can be relaxed to Local-Exec.
221
0
  if (Expr == R_ABS && !Config->Shared) {
222
0
    C.Relocations.push_back(
223
0
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
224
0
         Offset, Addend, &Sym});
225
0
    return 1;
226
0
  }
227
0
228
0
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
229
0
  // thread pointer is stored in the got.
230
0
  if (Expr == R_TLSLD_GOT_OFF) {
231
0
    // Local-Dynamic relocs can be relaxed to local-exec
232
0
    if (!Config->Shared) {
233
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
234
0
      return 1;
235
0
    }
236
0
    if (!Sym.isInGot()) {
237
0
      In.Got->addEntry(Sym);
238
0
      uint64_t Off = Sym.getGotOffset();
239
0
      In.Got->Relocations.push_back(
240
0
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
241
0
    }
242
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
243
0
    return 1;
244
0
  }
245
0
246
0
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL,
247
0
                     R_TLSGD_GOT, R_TLSGD_GOT_FROM_END, R_TLSGD_PC>(Expr)) {
248
0
    if (Config->Shared) {
249
0
      if (In.Got->addDynTlsEntry(Sym)) {
250
0
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
251
0
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
252
0
253
0
        // If the symbol is preemptible we need the dynamic linker to write
254
0
        // the offset too.
255
0
        uint64_t OffsetOff = Off + Config->Wordsize;
256
0
        if (Sym.IsPreemptible)
257
0
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
258
0
        else
259
0
          In.Got->Relocations.push_back(
260
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
261
0
      }
262
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
263
0
      return 1;
264
0
    }
265
0
266
0
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
0
    // depending on the symbol being locally defined or not.
268
0
    if (Sym.IsPreemptible) {
269
0
      C.Relocations.push_back(
270
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
271
0
           Offset, Addend, &Sym});
272
0
      if (!Sym.isInGot()) {
273
0
        In.Got->addEntry(Sym);
274
0
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
275
0
                             &Sym);
276
0
      }
277
0
    } else {
278
0
      C.Relocations.push_back(
279
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
280
0
           Offset, Addend, &Sym});
281
0
    }
282
0
    return Target->TlsGdRelaxSkip;
283
0
  }
284
0
285
0
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
0
  // defined.
287
0
  if (isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_AARCH64_GOT_PAGE_PC,
288
0
                     R_GOT_OFF, R_TLSIE_HINT>(Expr) &&
289
0
      !Config->Shared && !Sym.IsPreemptible) {
290
0
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
291
0
    return 1;
292
0
  }
293
0
294
0
  if (Expr == R_TLSIE_HINT)
295
0
    return 1;
296
0
  return 0;
297
0
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
181
2.19k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
182
2.19k
  if (!Sym.isTls())
183
1.94k
    return 0;
184
249
185
249
  if (Config->EMachine == EM_ARM)
186
0
    return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
187
249
  if (Config->EMachine == EM_MIPS)
188
0
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
189
249
190
249
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
191
249
      
Config->Shared34
) {
192
21
    if (In.Got->addDynTlsEntry(Sym)) {
193
5
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
194
5
      In.RelaDyn->addReloc(
195
5
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
196
5
    }
197
21
    if (Expr != R_TLSDESC_CALL)
198
15
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
199
21
    return 1;
200
21
  }
201
228
202
228
  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC,
203
228
                     R_TLSLD_HINT>(Expr)) {
204
25
    // Local-Dynamic relocs can be relaxed to Local-Exec.
205
25
    if (!Config->Shared) {
206
8
      C.Relocations.push_back(
207
8
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
208
8
           Offset, Addend, &Sym});
209
8
      return Target->TlsGdRelaxSkip;
210
8
    }
211
17
    if (Expr == R_TLSLD_HINT)
212
4
      return 1;
213
13
    if (In.Got->addTlsIndex())
214
4
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
215
4
                           In.Got->getTlsIndexOff(), nullptr);
216
13
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
217
13
    return 1;
218
13
  }
219
203
220
203
  // Local-Dynamic relocs can be relaxed to Local-Exec.
221
203
  if (Expr == R_ABS && 
!Config->Shared27
) {
222
7
    C.Relocations.push_back(
223
7
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
224
7
         Offset, Addend, &Sym});
225
7
    return 1;
226
7
  }
227
196
228
196
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
229
196
  // thread pointer is stored in the got.
230
196
  if (Expr == R_TLSLD_GOT_OFF) {
231
4
    // Local-Dynamic relocs can be relaxed to local-exec
232
4
    if (!Config->Shared) {
233
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
234
0
      return 1;
235
0
    }
236
4
    if (!Sym.isInGot()) {
237
1
      In.Got->addEntry(Sym);
238
1
      uint64_t Off = Sym.getGotOffset();
239
1
      In.Got->Relocations.push_back(
240
1
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
241
1
    }
242
4
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
243
4
    return 1;
244
4
  }
245
192
246
192
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL,
247
192
                     R_TLSGD_GOT, R_TLSGD_GOT_FROM_END, R_TLSGD_PC>(Expr)) {
248
46
    if (Config->Shared) {
249
10
      if (In.Got->addDynTlsEntry(Sym)) {
250
8
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
251
8
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
252
8
253
8
        // If the symbol is preemptible we need the dynamic linker to write
254
8
        // the offset too.
255
8
        uint64_t OffsetOff = Off + Config->Wordsize;
256
8
        if (Sym.IsPreemptible)
257
5
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
258
3
        else
259
3
          In.Got->Relocations.push_back(
260
3
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
261
8
      }
262
10
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
263
10
      return 1;
264
10
    }
265
36
266
36
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
36
    // depending on the symbol being locally defined or not.
268
36
    if (Sym.IsPreemptible) {
269
19
      C.Relocations.push_back(
270
19
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
271
19
           Offset, Addend, &Sym});
272
19
      if (!Sym.isInGot()) {
273
8
        In.Got->addEntry(Sym);
274
8
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
275
8
                             &Sym);
276
8
      }
277
19
    } else {
278
17
      C.Relocations.push_back(
279
17
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
280
17
           Offset, Addend, &Sym});
281
17
    }
282
36
    return Target->TlsGdRelaxSkip;
283
36
  }
284
146
285
146
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
146
  // defined.
287
146
  if (isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_AARCH64_GOT_PAGE_PC,
288
146
                     R_GOT_OFF, R_TLSIE_HINT>(Expr) &&
289
146
      
!Config->Shared89
&&
!Sym.IsPreemptible83
) {
290
71
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
291
71
    return 1;
292
71
  }
293
75
294
75
  if (Expr == R_TLSIE_HINT)
295
1
    return 1;
296
74
  return 0;
297
74
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
181
10.4k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
182
10.4k
  if (!Sym.isTls())
183
10.3k
    return 0;
184
101
185
101
  if (Config->EMachine == EM_ARM)
186
0
    return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
187
101
  if (Config->EMachine == EM_MIPS)
188
13
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
189
88
190
88
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
191
88
      
Config->Shared6
) {
192
1
    if (In.Got->addDynTlsEntry(Sym)) {
193
0
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
194
0
      In.RelaDyn->addReloc(
195
0
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
196
0
    }
197
1
    if (Expr != R_TLSDESC_CALL)
198
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
199
1
    return 1;
200
1
  }
201
87
202
87
  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC,
203
87
                     R_TLSLD_HINT>(Expr)) {
204
17
    // Local-Dynamic relocs can be relaxed to Local-Exec.
205
17
    if (!Config->Shared) {
206
3
      C.Relocations.push_back(
207
3
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
208
3
           Offset, Addend, &Sym});
209
3
      return Target->TlsGdRelaxSkip;
210
3
    }
211
14
    if (Expr == R_TLSLD_HINT)
212
4
      return 1;
213
10
    if (In.Got->addTlsIndex())
214
2
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
215
2
                           In.Got->getTlsIndexOff(), nullptr);
216
10
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
217
10
    return 1;
218
10
  }
219
70
220
70
  // Local-Dynamic relocs can be relaxed to Local-Exec.
221
70
  if (Expr == R_ABS && 
!Config->Shared14
) {
222
2
    C.Relocations.push_back(
223
2
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
224
2
         Offset, Addend, &Sym});
225
2
    return 1;
226
2
  }
227
68
228
68
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
229
68
  // thread pointer is stored in the got.
230
68
  if (Expr == R_TLSLD_GOT_OFF) {
231
4
    // Local-Dynamic relocs can be relaxed to local-exec
232
4
    if (!Config->Shared) {
233
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
234
0
      return 1;
235
0
    }
236
4
    if (!Sym.isInGot()) {
237
1
      In.Got->addEntry(Sym);
238
1
      uint64_t Off = Sym.getGotOffset();
239
1
      In.Got->Relocations.push_back(
240
1
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
241
1
    }
242
4
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
243
4
    return 1;
244
4
  }
245
64
246
64
  if (isRelExprOneOf<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL,
247
64
                     R_TLSGD_GOT, R_TLSGD_GOT_FROM_END, R_TLSGD_PC>(Expr)) {
248
18
    if (Config->Shared) {
249
4
      if (In.Got->addDynTlsEntry(Sym)) {
250
3
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
251
3
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
252
3
253
3
        // If the symbol is preemptible we need the dynamic linker to write
254
3
        // the offset too.
255
3
        uint64_t OffsetOff = Off + Config->Wordsize;
256
3
        if (Sym.IsPreemptible)
257
3
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
258
0
        else
259
0
          In.Got->Relocations.push_back(
260
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
261
3
      }
262
4
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
263
4
      return 1;
264
4
    }
265
14
266
14
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
14
    // depending on the symbol being locally defined or not.
268
14
    if (Sym.IsPreemptible) {
269
9
      C.Relocations.push_back(
270
9
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
271
9
           Offset, Addend, &Sym});
272
9
      if (!Sym.isInGot()) {
273
2
        In.Got->addEntry(Sym);
274
2
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
275
2
                             &Sym);
276
2
      }
277
9
    } else {
278
5
      C.Relocations.push_back(
279
5
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
280
5
           Offset, Addend, &Sym});
281
5
    }
282
14
    return Target->TlsGdRelaxSkip;
283
14
  }
284
46
285
46
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
46
  // defined.
287
46
  if (isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_AARCH64_GOT_PAGE_PC,
288
46
                     R_GOT_OFF, R_TLSIE_HINT>(Expr) &&
289
46
      
!Config->Shared34
&&
!Sym.IsPreemptible34
) {
290
29
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
291
29
    return 1;
292
29
  }
293
17
294
17
  if (Expr == R_TLSIE_HINT)
295
1
    return 1;
296
16
  return 0;
297
16
}
298
299
268
static RelType getMipsPairType(RelType Type, bool IsLocal) {
300
268
  switch (Type) {
301
268
  case R_MIPS_HI16:
302
23
    return R_MIPS_LO16;
303
268
  case R_MIPS_GOT16:
304
40
    // In case of global symbol, the R_MIPS_GOT16 relocation does not
305
40
    // have a pair. Each global symbol has a unique entry in the GOT
306
40
    // and a corresponding instruction with help of the R_MIPS_GOT16
307
40
    // relocation loads an address of the symbol. In case of local
308
40
    // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
309
40
    // the high 16 bits of the symbol's value. A paired R_MIPS_LO16
310
40
    // relocations handle low 16 bits of the address. That allows
311
40
    // to allocate only one GOT entry for every 64 KBytes of local data.
312
40
    return IsLocal ? 
R_MIPS_LO1616
:
R_MIPS_NONE24
;
313
268
  case R_MICROMIPS_GOT16:
314
4
    return IsLocal ? 
R_MICROMIPS_LO161
:
R_MIPS_NONE3
;
315
268
  case R_MIPS_PCHI16:
316
1
    return R_MIPS_PCLO16;
317
268
  case R_MICROMIPS_HI16:
318
3
    return R_MICROMIPS_LO16;
319
268
  default:
320
197
    return R_MIPS_NONE;
321
268
  }
322
268
}
323
324
// True if non-preemptable symbol always has the same value regardless of where
325
// the DSO is loaded.
326
745
static bool isAbsolute(const Symbol &Sym) {
327
745
  if (Sym.isUndefWeak())
328
7
    return true;
329
738
  if (const auto *DR = dyn_cast<Defined>(&Sym))
330
733
    return DR->Section == nullptr; // Absolute symbol.
331
5
  return false;
332
5
}
333
334
706
static bool isAbsoluteValue(const Symbol &Sym) {
335
706
  return isAbsolute(Sym) || 
Sym.isTls()678
;
336
706
}
337
338
// Returns true if Expr refers a PLT entry.
339
13.4k
static bool needsPlt(RelExpr Expr) {
340
13.4k
  return isRelExprOneOf<R_PLT_PC, R_PPC_CALL_PLT, R_PLT, R_AARCH64_PLT_PAGE_PC,
341
13.4k
                        R_GOT_PLT, R_AARCH64_GOT_PAGE_PC_PLT>(Expr);
342
13.4k
}
343
344
// Returns true if Expr refers a GOT entry. Note that this function
345
// returns false for TLS variables even though they need GOT, because
346
// TLS variables uses GOT differently than the regular variables.
347
13.4k
static bool needsGot(RelExpr Expr) {
348
13.4k
  return isRelExprOneOf<R_GOT, R_GOT_OFF, R_HEXAGON_GOT, R_MIPS_GOT_LOCAL_PAGE,
349
13.4k
                        R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC,
350
13.4k
                        R_AARCH64_GOT_PAGE_PC_PLT, R_GOT_PC, R_GOT_FROM_END,
351
13.4k
                        R_GOT_PLT>(Expr);
352
13.4k
}
353
354
// True if this expression is of the form Sym - X, where X is a position in the
355
// file (PC, or GOT for example).
356
642
static bool isRelExpr(RelExpr Expr) {
357
642
  return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL,
358
642
                        R_PPC_CALL, R_PPC_CALL_PLT, R_AARCH64_PAGE_PC,
359
642
                        R_AARCH64_PLT_PAGE_PC, R_RELAX_GOT_PC>(Expr);
360
642
}
361
362
// Returns true if a given relocation can be computed at link-time.
363
//
364
// For instance, we know the offset from a relocation to its target at
365
// link-time if the relocation is PC-relative and refers a
366
// non-interposable function in the same executable. This function
367
// will return true for such relocation.
368
//
369
// If this function returns false, that means we need to emit a
370
// dynamic relocation so that the relocation will be fixed at load-time.
371
static bool isStaticLinkTimeConstant(RelExpr E, RelType Type, const Symbol &Sym,
372
13.4k
                                     InputSectionBase &S, uint64_t RelOff) {
373
13.4k
  // These expressions always compute a constant
374
13.4k
  if (isRelExprOneOf<R_GOT_FROM_END, R_GOT_OFF, R_HEXAGON_GOT, R_TLSLD_GOT_OFF,
375
13.4k
                     R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOTREL, R_MIPS_GOT_OFF,
376
13.4k
                     R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, R_MIPS_TLSGD,
377
13.4k
                     R_AARCH64_GOT_PAGE_PC, R_AARCH64_GOT_PAGE_PC_PLT, R_GOT_PC,
378
13.4k
                     R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_GOT,
379
13.4k
                     R_TLSGD_GOT_FROM_END, R_TLSGD_PC, R_PPC_CALL_PLT,
380
13.4k
                     R_TLSDESC_CALL, R_AARCH64_TLSDESC_PAGE, R_HINT,
381
13.4k
                     R_TLSLD_HINT, R_TLSIE_HINT>(E))
382
10.6k
    return true;
383
2.79k
384
2.79k
  // These never do, except if the entire file is position dependent or if
385
2.79k
  // only the low bits are used.
386
2.79k
  if (E == R_GOT || 
E == R_GOT_PLT2.72k
||
E == R_PLT2.71k
||
E == R_TLSDESC2.70k
)
387
93
    return Target->usesOnlyLowPageBits(Type) || 
!Config->Pic30
;
388
2.70k
389
2.70k
  if (Sym.IsPreemptible)
390
307
    return false;
391
2.39k
  if (!Config->Pic)
392
1.77k
    return true;
393
621
394
621
  // The size of a non preemptible symbol is a constant.
395
621
  if (E == R_SIZE)
396
6
    return true;
397
615
398
615
  // For the target and the relocation, we want to know if they are
399
615
  // absolute or relative.
400
615
  bool AbsVal = isAbsoluteValue(Sym);
401
615
  bool RelE = isRelExpr(E);
402
615
  if (AbsVal && 
!RelE64
)
403
62
    return true;
404
553
  if (!AbsVal && 
RelE551
)
405
287
    return true;
406
266
  if (!AbsVal && 
!RelE264
)
407
264
    return Target->usesOnlyLowPageBits(Type);
408
2
409
2
  // Relative relocation to an absolute value. This is normally unrepresentable,
410
2
  // but if the relocation refers to a weak undefined symbol, we allow it to
411
2
  // resolve to the image base. This is a little strange, but it allows us to
412
2
  // link function calls to such symbols. Normally such a call will be guarded
413
2
  // with a comparison, which will load a zero from the GOT.
414
2
  // Another special case is MIPS _gp_disp symbol which represents offset
415
2
  // between start of a function and '_gp' value and defined as absolute just
416
2
  // to simplify the code.
417
2
  assert(AbsVal && RelE);
418
2
  if (Sym.isUndefWeak())
419
0
    return true;
420
2
421
2
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
422
2
        toString(Sym) + getLocation(S, Sym, RelOff));
423
2
  return true;
424
2
}
425
426
57
static RelExpr toPlt(RelExpr Expr) {
427
57
  switch (Expr) {
428
57
  case R_PPC_CALL:
429
0
    return R_PPC_CALL_PLT;
430
57
  case R_PC:
431
5
    return R_PLT_PC;
432
57
  case R_AARCH64_PAGE_PC:
433
2
    return R_AARCH64_PLT_PAGE_PC;
434
57
  case R_AARCH64_GOT_PAGE_PC:
435
2
    return R_AARCH64_GOT_PAGE_PC_PLT;
436
57
  case R_ABS:
437
2
    return R_PLT;
438
57
  case R_GOT:
439
2
    return R_GOT_PLT;
440
57
  default:
441
44
    return Expr;
442
57
  }
443
57
}
444
445
13.0k
static RelExpr fromPlt(RelExpr Expr) {
446
13.0k
  // We decided not to use a plt. Optimize a reference to the plt to a
447
13.0k
  // reference to the symbol itself.
448
13.0k
  switch (Expr) {
449
13.0k
  case R_PLT_PC:
450
599
    return R_PC;
451
13.0k
  case R_PPC_CALL_PLT:
452
147
    return R_PPC_CALL;
453
13.0k
  case R_PLT:
454
48
    return R_ABS;
455
13.0k
  default:
456
12.2k
    return Expr;
457
13.0k
  }
458
13.0k
}
459
460
// Returns true if a given shared symbol is in a read-only segment in a DSO.
461
51
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
462
51
  typedef typename ELFT::Phdr Elf_Phdr;
463
51
464
51
  // Determine if the symbol is read-only by scanning the DSO's program headers.
465
51
  const SharedFile<ELFT> &File = SS.getFile<ELFT>();
466
51
  for (const Elf_Phdr &Phdr : check(File.getObj().program_headers()))
467
281
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO170
) &&
468
281
        
!(Phdr.p_flags & ELF::PF_W)150
&&
SS.Value >= Phdr.p_vaddr106
&&
469
281
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz98
)
470
15
      return true;
471
51
  
return false36
;
472
51
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
461
6
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
462
6
  typedef typename ELFT::Phdr Elf_Phdr;
463
6
464
6
  // Determine if the symbol is read-only by scanning the DSO's program headers.
465
6
  const SharedFile<ELFT> &File = SS.getFile<ELFT>();
466
6
  for (const Elf_Phdr &Phdr : check(File.getObj().program_headers()))
467
35
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO23
) &&
468
35
        
!(Phdr.p_flags & ELF::PF_W)18
&&
SS.Value >= Phdr.p_vaddr12
&&
469
35
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz12
)
470
1
      return true;
471
6
  
return false5
;
472
6
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
461
5
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
462
5
  typedef typename ELFT::Phdr Elf_Phdr;
463
5
464
5
  // Determine if the symbol is read-only by scanning the DSO's program headers.
465
5
  const SharedFile<ELFT> &File = SS.getFile<ELFT>();
466
5
  for (const Elf_Phdr &Phdr : check(File.getObj().program_headers()))
467
30
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO15
) &&
468
30
        
!(Phdr.p_flags & ELF::PF_W)15
&&
SS.Value >= Phdr.p_vaddr10
&&
469
30
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz10
)
470
0
      return true;
471
5
  return false;
472
5
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
461
40
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
462
40
  typedef typename ELFT::Phdr Elf_Phdr;
463
40
464
40
  // Determine if the symbol is read-only by scanning the DSO's program headers.
465
40
  const SharedFile<ELFT> &File = SS.getFile<ELFT>();
466
40
  for (const Elf_Phdr &Phdr : check(File.getObj().program_headers()))
467
216
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO132
) &&
468
216
        
!(Phdr.p_flags & ELF::PF_W)117
&&
SS.Value >= Phdr.p_vaddr84
&&
469
216
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz76
)
470
14
      return true;
471
40
  
return false26
;
472
40
}
Unexecuted instantiation: Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
473
474
// Returns symbols at the same offset as a given symbol, including SS itself.
475
//
476
// If two or more symbols are at the same offset, and at least one of
477
// them are copied by a copy relocation, all of them need to be copied.
478
// Otherwise, they would refer to different places at runtime.
479
template <class ELFT>
480
51
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
481
51
  typedef typename ELFT::Sym Elf_Sym;
482
51
483
51
  SharedFile<ELFT> &File = SS.getFile<ELFT>();
484
51
485
51
  SmallSet<SharedSymbol *, 4> Ret;
486
159
  for (const Elf_Sym &S : File.getGlobalELFSyms()) {
487
159
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
488
159
        
S.getType() == STT_TLS155
||
S.st_value != SS.Value154
)
489
88
      continue;
490
71
    StringRef Name = check(S.getName(File.getStringTable()));
491
71
    Symbol *Sym = Symtab->find(Name);
492
71
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
493
68
      Ret.insert(Alias);
494
71
  }
495
51
  return Ret;
496
51
}
Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
480
6
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
481
6
  typedef typename ELFT::Sym Elf_Sym;
482
6
483
6
  SharedFile<ELFT> &File = SS.getFile<ELFT>();
484
6
485
6
  SmallSet<SharedSymbol *, 4> Ret;
486
16
  for (const Elf_Sym &S : File.getGlobalELFSyms()) {
487
16
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
488
16
        S.getType() == STT_TLS || S.st_value != SS.Value)
489
10
      continue;
490
6
    StringRef Name = check(S.getName(File.getStringTable()));
491
6
    Symbol *Sym = Symtab->find(Name);
492
6
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
493
6
      Ret.insert(Alias);
494
6
  }
495
6
  return Ret;
496
6
}
Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
480
5
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
481
5
  typedef typename ELFT::Sym Elf_Sym;
482
5
483
5
  SharedFile<ELFT> &File = SS.getFile<ELFT>();
484
5
485
5
  SmallSet<SharedSymbol *, 4> Ret;
486
25
  for (const Elf_Sym &S : File.getGlobalELFSyms()) {
487
25
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
488
25
        S.getType() == STT_TLS || S.st_value != SS.Value)
489
20
      continue;
490
5
    StringRef Name = check(S.getName(File.getStringTable()));
491
5
    Symbol *Sym = Symtab->find(Name);
492
5
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
493
5
      Ret.insert(Alias);
494
5
  }
495
5
  return Ret;
496
5
}
Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
480
40
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
481
40
  typedef typename ELFT::Sym Elf_Sym;
482
40
483
40
  SharedFile<ELFT> &File = SS.getFile<ELFT>();
484
40
485
40
  SmallSet<SharedSymbol *, 4> Ret;
486
118
  for (const Elf_Sym &S : File.getGlobalELFSyms()) {
487
118
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
488
118
        
S.getType() == STT_TLS114
||
S.st_value != SS.Value113
)
489
58
      continue;
490
60
    StringRef Name = check(S.getName(File.getStringTable()));
491
60
    Symbol *Sym = Symtab->find(Name);
492
60
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
493
57
      Ret.insert(Alias);
494
60
  }
495
40
  return Ret;
496
40
}
Unexecuted instantiation: Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
497
498
// When a symbol is copy relocated or we create a canonical plt entry, it is
499
// effectively a defined symbol. In the case of copy relocation the symbol is
500
// in .bss and in the case of a canonical plt entry it is in .plt. This function
501
// replaces the existing symbol with a Defined pointing to the appropriate
502
// location.
503
static void replaceWithDefined(Symbol &Sym, SectionBase *Sec, uint64_t Value,
504
110
                               uint64_t Size) {
505
110
  Symbol Old = Sym;
506
110
  replaceSymbol<Defined>(&Sym, Sym.File, Sym.getName(), Sym.Binding,
507
110
                         Sym.StOther, Sym.Type, Value, Size, Sec);
508
110
  Sym.PltIndex = Old.PltIndex;
509
110
  Sym.GotIndex = Old.GotIndex;
510
110
  Sym.VerdefIndex = Old.VerdefIndex;
511
110
  Sym.PPC64BranchltIndex = Old.PPC64BranchltIndex;
512
110
  Sym.IsPreemptible = true;
513
110
  Sym.ExportDynamic = true;
514
110
  Sym.IsUsedInRegularObj = true;
515
110
  Sym.Used = true;
516
110
}
517
518
// Reserve space in .bss or .bss.rel.ro for copy relocation.
519
//
520
// The copy relocation is pretty much a hack. If you use a copy relocation
521
// in your program, not only the symbol name but the symbol's size, RW/RO
522
// bit and alignment become part of the ABI. In addition to that, if the
523
// symbol has aliases, the aliases become part of the ABI. That's subtle,
524
// but if you violate that implicit ABI, that can cause very counter-
525
// intuitive consequences.
526
//
527
// So, what is the copy relocation? It's for linking non-position
528
// independent code to DSOs. In an ideal world, all references to data
529
// exported by DSOs should go indirectly through GOT. But if object files
530
// are compiled as non-PIC, all data references are direct. There is no
531
// way for the linker to transform the code to use GOT, as machine
532
// instructions are already set in stone in object files. This is where
533
// the copy relocation takes a role.
534
//
535
// A copy relocation instructs the dynamic linker to copy data from a DSO
536
// to a specified address (which is usually in .bss) at load-time. If the
537
// static linker (that's us) finds a direct data reference to a DSO
538
// symbol, it creates a copy relocation, so that the symbol can be
539
// resolved as if it were in .bss rather than in a DSO.
540
//
541
// As you can see in this function, we create a copy relocation for the
542
// dynamic linker, and the relocation contains not only symbol name but
543
// various other informtion about the symbol. So, such attributes become a
544
// part of the ABI.
545
//
546
// Note for application developers: I can give you a piece of advice if
547
// you are writing a shared library. You probably should export only
548
// functions from your library. You shouldn't export variables.
549
//
550
// As an example what can happen when you export variables without knowing
551
// the semantics of copy relocations, assume that you have an exported
552
// variable of type T. It is an ABI-breaking change to add new members at
553
// end of T even though doing that doesn't change the layout of the
554
// existing members. That's because the space for the new members are not
555
// reserved in .bss unless you recompile the main program. That means they
556
// are likely to overlap with other data that happens to be laid out next
557
// to the variable in .bss. This kind of issue is sometimes very hard to
558
// debug. What's a solution? Instead of exporting a varaible V from a DSO,
559
// define an accessor getV().
560
51
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
561
51
  // Copy relocation against zero-sized symbol doesn't make sense.
562
51
  uint64_t SymSize = SS.getSize();
563
51
  if (SymSize == 0 || SS.Alignment == 0)
564
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
565
51
566
51
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
567
51
  // memory protection by reserving space in the .bss.rel.ro section.
568
51
  bool IsReadOnly = isReadOnly<ELFT>(SS);
569
51
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"15
:
".bss"36
,
570
51
                                     SymSize, SS.Alignment);
571
51
  if (IsReadOnly)
572
15
    In.BssRelRo->getParent()->addSection(Sec);
573
36
  else
574
36
    In.Bss->getParent()->addSection(Sec);
575
51
576
51
  // Look through the DSO's dynamic symbol table for aliases and create a
577
51
  // dynamic symbol for each one. This causes the copy relocation to correctly
578
51
  // interpose any aliases.
579
51
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
580
67
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
581
51
582
51
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
583
51
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
560
6
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
561
6
  // Copy relocation against zero-sized symbol doesn't make sense.
562
6
  uint64_t SymSize = SS.getSize();
563
6
  if (SymSize == 0 || SS.Alignment == 0)
564
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
565
6
566
6
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
567
6
  // memory protection by reserving space in the .bss.rel.ro section.
568
6
  bool IsReadOnly = isReadOnly<ELFT>(SS);
569
6
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"1
:
".bss"5
,
570
6
                                     SymSize, SS.Alignment);
571
6
  if (IsReadOnly)
572
1
    In.BssRelRo->getParent()->addSection(Sec);
573
5
  else
574
5
    In.Bss->getParent()->addSection(Sec);
575
6
576
6
  // Look through the DSO's dynamic symbol table for aliases and create a
577
6
  // dynamic symbol for each one. This causes the copy relocation to correctly
578
6
  // interpose any aliases.
579
6
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
580
6
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
581
6
582
6
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
583
6
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
560
5
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
561
5
  // Copy relocation against zero-sized symbol doesn't make sense.
562
5
  uint64_t SymSize = SS.getSize();
563
5
  if (SymSize == 0 || SS.Alignment == 0)
564
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
565
5
566
5
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
567
5
  // memory protection by reserving space in the .bss.rel.ro section.
568
5
  bool IsReadOnly = isReadOnly<ELFT>(SS);
569
5
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"0
: ".bss",
570
5
                                     SymSize, SS.Alignment);
571
5
  if (IsReadOnly)
572
0
    In.BssRelRo->getParent()->addSection(Sec);
573
5
  else
574
5
    In.Bss->getParent()->addSection(Sec);
575
5
576
5
  // Look through the DSO's dynamic symbol table for aliases and create a
577
5
  // dynamic symbol for each one. This causes the copy relocation to correctly
578
5
  // interpose any aliases.
579
5
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
580
5
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
581
5
582
5
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
583
5
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
560
40
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
561
40
  // Copy relocation against zero-sized symbol doesn't make sense.
562
40
  uint64_t SymSize = SS.getSize();
563
40
  if (SymSize == 0 || SS.Alignment == 0)
564
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
565
40
566
40
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
567
40
  // memory protection by reserving space in the .bss.rel.ro section.
568
40
  bool IsReadOnly = isReadOnly<ELFT>(SS);
569
40
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"14
:
".bss"26
,
570
40
                                     SymSize, SS.Alignment);
571
40
  if (IsReadOnly)
572
14
    In.BssRelRo->getParent()->addSection(Sec);
573
26
  else
574
26
    In.Bss->getParent()->addSection(Sec);
575
40
576
40
  // Look through the DSO's dynamic symbol table for aliases and create a
577
40
  // dynamic symbol for each one. This causes the copy relocation to correctly
578
40
  // interpose any aliases.
579
40
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
580
56
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
581
40
582
40
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
583
40
}
Unexecuted instantiation: Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
584
585
// MIPS has an odd notion of "paired" relocations to calculate addends.
586
// For example, if a relocation is of R_MIPS_HI16, there must be a
587
// R_MIPS_LO16 relocation after that, and an addend is calculated using
588
// the two relocations.
589
template <class ELFT, class RelTy>
590
static int64_t computeMipsAddend(const RelTy &Rel, const RelTy *End,
591
                                 InputSectionBase &Sec, RelExpr Expr,
592
10.3k
                                 bool IsLocal) {
593
10.3k
  if (Expr == R_MIPS_GOTREL && 
IsLocal20
)
594
9
    return Sec.getFile<ELFT>()->MipsGp0;
595
10.3k
596
10.3k
  // The ABI says that the paired relocation is used only for REL.
597
10.3k
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
598
10.3k
  if (RelTy::IsRela)
599
10.0k
    return 0;
600
268
601
268
  RelType Type = Rel.getType(Config->IsMips64EL);
602
268
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
603
268
  if (PairTy == R_MIPS_NONE)
604
224
    return 0;
605
44
606
44
  const uint8_t *Buf = Sec.data().data();
607
44
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
608
44
609
44
  // To make things worse, paired relocations might not be contiguous in
610
44
  // the relocation table, so we need to do linear search. *sigh*
611
89
  for (const RelTy *RI = &Rel; RI != End; 
++RI45
)
612
88
    if (RI->getType(Config->IsMips64EL) == PairTy &&
613
88
        
RI->getSymbol(Config->IsMips64EL) == SymIndex43
)
614
43
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
615
44
616
44
  warn("can't find matching " + toString(PairTy) + " relocation for " +
617
1
       toString(Type));
618
1
  return 0;
619
44
}
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
592
20
                                 bool IsLocal) {
593
20
  if (Expr == R_MIPS_GOTREL && 
IsLocal0
)
594
0
    return Sec.getFile<ELFT>()->MipsGp0;
595
20
596
20
  // The ABI says that the paired relocation is used only for REL.
597
20
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
598
20
  if (RelTy::IsRela)
599
0
    return 0;
600
20
601
20
  RelType Type = Rel.getType(Config->IsMips64EL);
602
20
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
603
20
  if (PairTy == R_MIPS_NONE)
604
19
    return 0;
605
1
606
1
  const uint8_t *Buf = Sec.data().data();
607
1
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
608
1
609
1
  // To make things worse, paired relocations might not be contiguous in
610
1
  // the relocation table, so we need to do linear search. *sigh*
611
2
  for (const RelTy *RI = &Rel; RI != End; 
++RI1
)
612
2
    if (RI->getType(Config->IsMips64EL) == PairTy &&
613
2
        
RI->getSymbol(Config->IsMips64EL) == SymIndex1
)
614
1
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
615
1
616
1
  warn("can't find matching " + toString(PairTy) + " relocation for " +
617
0
       toString(Type));
618
0
  return 0;
619
1
}
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
592
5
                                 bool IsLocal) {
593
5
  if (Expr == R_MIPS_GOTREL && 
IsLocal3
)
594
1
    return Sec.getFile<ELFT>()->MipsGp0;
595
4
596
4
  // The ABI says that the paired relocation is used only for REL.
597
4
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
598
4
  if (RelTy::IsRela)
599
4
    return 0;
600
0
601
0
  RelType Type = Rel.getType(Config->IsMips64EL);
602
0
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
603
0
  if (PairTy == R_MIPS_NONE)
604
0
    return 0;
605
0
606
0
  const uint8_t *Buf = Sec.data().data();
607
0
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
608
0
609
0
  // To make things worse, paired relocations might not be contiguous in
610
0
  // the relocation table, so we need to do linear search. *sigh*
611
0
  for (const RelTy *RI = &Rel; RI != End; ++RI)
612
0
    if (RI->getType(Config->IsMips64EL) == PairTy &&
613
0
        RI->getSymbol(Config->IsMips64EL) == SymIndex)
614
0
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
615
0
616
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
617
0
       toString(Type));
618
0
  return 0;
619
0
}
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
592
255
                                 bool IsLocal) {
593
255
  if (Expr == R_MIPS_GOTREL && 
IsLocal7
)
594
7
    return Sec.getFile<ELFT>()->MipsGp0;
595
248
596
248
  // The ABI says that the paired relocation is used only for REL.
597
248
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
598
248
  if (RelTy::IsRela)
599
0
    return 0;
600
248
601
248
  RelType Type = Rel.getType(Config->IsMips64EL);
602
248
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
603
248
  if (PairTy == R_MIPS_NONE)
604
205
    return 0;
605
43
606
43
  const uint8_t *Buf = Sec.data().data();
607
43
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
608
43
609
43
  // To make things worse, paired relocations might not be contiguous in
610
43
  // the relocation table, so we need to do linear search. *sigh*
611
87
  for (const RelTy *RI = &Rel; RI != End; 
++RI44
)
612
86
    if (RI->getType(Config->IsMips64EL) == PairTy &&
613
86
        
RI->getSymbol(Config->IsMips64EL) == SymIndex42
)
614
42
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
615
43
616
43
  warn("can't find matching " + toString(PairTy) + " relocation for " +
617
1
       toString(Type));
618
1
  return 0;
619
43
}
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
592
10.0k
                                 bool IsLocal) {
593
10.0k
  if (Expr == R_MIPS_GOTREL && 
IsLocal10
)
594
1
    return Sec.getFile<ELFT>()->MipsGp0;
595
10.0k
596
10.0k
  // The ABI says that the paired relocation is used only for REL.
597
10.0k
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
598
10.0k
  if (RelTy::IsRela)
599
10.0k
    return 0;
600
0
601
0
  RelType Type = Rel.getType(Config->IsMips64EL);
602
0
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
603
0
  if (PairTy == R_MIPS_NONE)
604
0
    return 0;
605
0
606
0
  const uint8_t *Buf = Sec.data().data();
607
0
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
608
0
609
0
  // To make things worse, paired relocations might not be contiguous in
610
0
  // the relocation table, so we need to do linear search. *sigh*
611
0
  for (const RelTy *RI = &Rel; RI != End; ++RI)
612
0
    if (RI->getType(Config->IsMips64EL) == PairTy &&
613
0
        RI->getSymbol(Config->IsMips64EL) == SymIndex)
614
0
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
615
0
616
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
617
0
       toString(Type));
618
0
  return 0;
619
0
}
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
620
621
// Returns an addend of a given relocation. If it is RELA, an addend
622
// is in a relocation itself. If it is REL, we need to read it from an
623
// input section.
624
template <class ELFT, class RelTy>
625
static int64_t computeAddend(const RelTy &Rel, const RelTy *End,
626
                             InputSectionBase &Sec, RelExpr Expr,
627
13.7k
                             bool IsLocal) {
628
13.7k
  int64_t Addend;
629
13.7k
  RelType Type = Rel.getType(Config->IsMips64EL);
630
13.7k
631
13.7k
  if (RelTy::IsRela) {
632
12.7k
    Addend = getAddend<ELFT>(Rel);
633
12.7k
  } else {
634
1.06k
    const uint8_t *Buf = Sec.data().data();
635
1.06k
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
1.06k
  }
637
13.7k
638
13.7k
  if (Config->EMachine == EM_PPC64 && 
Config->Pic829
&&
Type == R_PPC64_TOC219
)
639
0
    Addend += getPPC64TocBase();
640
13.7k
  if (Config->EMachine == EM_MIPS)
641
10.3k
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
13.7k
643
13.7k
  return Addend;
644
13.7k
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
108
                             bool IsLocal) {
628
108
  int64_t Addend;
629
108
  RelType Type = Rel.getType(Config->IsMips64EL);
630
108
631
108
  if (RelTy::IsRela) {
632
108
    Addend = getAddend<ELFT>(Rel);
633
108
  } else {
634
0
    const uint8_t *Buf = Sec.data().data();
635
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
0
  }
637
108
638
108
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
639
0
    Addend += getPPC64TocBase();
640
108
  if (Config->EMachine == EM_MIPS)
641
0
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
108
643
108
  return Addend;
644
108
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
808
                             bool IsLocal) {
628
808
  int64_t Addend;
629
808
  RelType Type = Rel.getType(Config->IsMips64EL);
630
808
631
808
  if (RelTy::IsRela) {
632
0
    Addend = getAddend<ELFT>(Rel);
633
808
  } else {
634
808
    const uint8_t *Buf = Sec.data().data();
635
808
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
808
  }
637
808
638
808
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
639
0
    Addend += getPPC64TocBase();
640
808
  if (Config->EMachine == EM_MIPS)
641
20
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
808
643
808
  return Addend;
644
808
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
14
                             bool IsLocal) {
628
14
  int64_t Addend;
629
14
  RelType Type = Rel.getType(Config->IsMips64EL);
630
14
631
14
  if (RelTy::IsRela) {
632
14
    Addend = getAddend<ELFT>(Rel);
633
14
  } else {
634
0
    const uint8_t *Buf = Sec.data().data();
635
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
0
  }
637
14
638
14
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
639
0
    Addend += getPPC64TocBase();
640
14
  if (Config->EMachine == EM_MIPS)
641
5
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
14
643
14
  return Addend;
644
14
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
255
                             bool IsLocal) {
628
255
  int64_t Addend;
629
255
  RelType Type = Rel.getType(Config->IsMips64EL);
630
255
631
255
  if (RelTy::IsRela) {
632
0
    Addend = getAddend<ELFT>(Rel);
633
255
  } else {
634
255
    const uint8_t *Buf = Sec.data().data();
635
255
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
255
  }
637
255
638
255
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
639
0
    Addend += getPPC64TocBase();
640
255
  if (Config->EMachine == EM_MIPS)
641
255
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
255
643
255
  return Addend;
644
255
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
2.19k
                             bool IsLocal) {
628
2.19k
  int64_t Addend;
629
2.19k
  RelType Type = Rel.getType(Config->IsMips64EL);
630
2.19k
631
2.19k
  if (RelTy::IsRela) {
632
2.19k
    Addend = getAddend<ELFT>(Rel);
633
2.19k
  } else {
634
0
    const uint8_t *Buf = Sec.data().data();
635
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
0
  }
637
2.19k
638
2.19k
  if (Config->EMachine == EM_PPC64 && 
Config->Pic485
&&
Type == R_PPC64_TOC117
)
639
0
    Addend += getPPC64TocBase();
640
2.19k
  if (Config->EMachine == EM_MIPS)
641
0
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
2.19k
643
2.19k
  return Addend;
644
2.19k
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
2
                             bool IsLocal) {
628
2
  int64_t Addend;
629
2
  RelType Type = Rel.getType(Config->IsMips64EL);
630
2
631
2
  if (RelTy::IsRela) {
632
0
    Addend = getAddend<ELFT>(Rel);
633
2
  } else {
634
2
    const uint8_t *Buf = Sec.data().data();
635
2
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
2
  }
637
2
638
2
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
639
0
    Addend += getPPC64TocBase();
640
2
  if (Config->EMachine == EM_MIPS)
641
0
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
2
643
2
  return Addend;
644
2
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
627
10.4k
                             bool IsLocal) {
628
10.4k
  int64_t Addend;
629
10.4k
  RelType Type = Rel.getType(Config->IsMips64EL);
630
10.4k
631
10.4k
  if (RelTy::IsRela) {
632
10.4k
    Addend = getAddend<ELFT>(Rel);
633
10.4k
  } else {
634
0
    const uint8_t *Buf = Sec.data().data();
635
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
636
0
  }
637
10.4k
638
10.4k
  if (Config->EMachine == EM_PPC64 && 
Config->Pic344
&&
Type == R_PPC64_TOC102
)
639
0
    Addend += getPPC64TocBase();
640
10.4k
  if (Config->EMachine == EM_MIPS)
641
10.0k
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
642
10.4k
643
10.4k
  return Addend;
644
10.4k
}
Unexecuted instantiation: Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
645
646
// Report an undefined symbol if necessary.
647
// Returns true if this function printed out an error message.
648
static bool maybeReportUndefined(Symbol &Sym, InputSectionBase &Sec,
649
13.8k
                                 uint64_t Offset) {
650
13.8k
  if (Sym.isLocal() || 
!Sym.isUndefined()13.0k
||
Sym.isWeak()376
)
651
13.5k
    return false;
652
295
653
295
  bool CanBeExternal =
654
295
      Sym.computeBinding() != STB_LOCAL && 
Sym.Visibility == STV_DEFAULT285
;
655
295
  if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 
CanBeExternal218
)
656
210
    return false;
657
85
658
85
  std::string Msg =
659
85
      "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
660
85
661
85
  std::string Src = Sec.getSrcMsg(Sym, Offset);
662
85
  if (!Src.empty())
663
29
    Msg += Src + "\n>>>               ";
664
85
  Msg += Sec.getObjMsg(Offset);
665
85
666
85
  if (Sym.getName().startswith("_ZTV"))
667
3
    Msg += "\nthe vtable symbol may be undefined because the class is missing "
668
3
           "its key function (see https://lld.llvm.org/missingkeyfunction)";
669
85
670
85
  if ((Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 
CanBeExternal5
) ||
671
85
      
Config->NoinhibitExec82
) {
672
10
    warn(Msg);
673
10
    return false;
674
10
  }
675
75
676
75
  error(Msg);
677
75
  return true;
678
75
}
679
680
// MIPS N32 ABI treats series of successive relocations with the same offset
681
// as a single relocation. The similar approach used by N64 ABI, but this ABI
682
// packs all relocations into the single relocation record. Here we emulate
683
// this for the N32 ABI. Iterate over relocation with the same offset and put
684
// theirs types into the single bit-set.
685
5
template <class RelTy> static RelType getMipsN32RelType(RelTy *&Rel, RelTy *End) {
686
5
  RelType Type = 0;
687
5
  uint64_t Offset = Rel->r_offset;
688
5
689
5
  int N = 0;
690
14
  while (Rel != End && 
Rel->r_offset == Offset10
)
691
9
    Type |= (Rel++)->getType(Config->IsMips64EL) << (8 * N++);
692
5
  return Type;
693
5
}
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*)
Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*)
Line
Count
Source
685
5
template <class RelTy> static RelType getMipsN32RelType(RelTy *&Rel, RelTy *End) {
686
5
  RelType Type = 0;
687
5
  uint64_t Offset = Rel->r_offset;
688
5
689
5
  int N = 0;
690
14
  while (Rel != End && 
Rel->r_offset == Offset10
)
691
9
    Type |= (Rel++)->getType(Config->IsMips64EL) << (8 * N++);
692
5
  return Type;
693
5
}
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*)
694
695
// .eh_frame sections are mergeable input sections, so their input
696
// offsets are not linearly mapped to output section. For each input
697
// offset, we need to find a section piece containing the offset and
698
// add the piece's base address to the input offset to compute the
699
// output offset. That isn't cheap.
700
//
701
// This class is to speed up the offset computation. When we process
702
// relocations, we access offsets in the monotonically increasing
703
// order. So we can optimize for that access pattern.
704
//
705
// For sections other than .eh_frame, this class doesn't do anything.
706
namespace {
707
class OffsetGetter {
708
public:
709
106k
  explicit OffsetGetter(InputSectionBase &Sec) {
710
106k
    if (auto *Eh = dyn_cast<EhInputSection>(&Sec))
711
111
      Pieces = Eh->Pieces;
712
106k
  }
713
714
  // Translates offsets in input sections to offsets in output sections.
715
  // Given offset must increase monotonically. We assume that Piece is
716
  // sorted by InputOff.
717
13.8k
  uint64_t get(uint64_t Off) {
718
13.8k
    if (Pieces.empty())
719
13.7k
      return Off;
720
142
721
271
    
while (142
I != Pieces.size() && Pieces[I].InputOff + Pieces[I].Size <= Off)
722
129
      ++I;
723
142
    if (I == Pieces.size())
724
0
      fatal(".eh_frame: relocation is not in any piece");
725
142
726
142
    // Pieces must be contiguous, so there must be no holes in between.
727
142
    assert(Pieces[I].InputOff <= Off && "Relocation not in any piece");
728
142
729
142
    // Offset -1 means that the piece is dead (i.e. garbage collected).
730
142
    if (Pieces[I].OutputOff == -1)
731
13
      return -1;
732
129
    return Pieces[I].OutputOff + Off - Pieces[I].InputOff;
733
129
  }
734
735
private:
736
  ArrayRef<EhSectionPiece> Pieces;
737
  size_t I = 0;
738
};
739
} // namespace
740
741
static void addRelativeReloc(InputSectionBase *IS, uint64_t OffsetInSec,
742
                             Symbol *Sym, int64_t Addend, RelExpr Expr,
743
259
                             RelType Type) {
744
259
  // Add a relative relocation. If RelrDyn section is enabled, and the
745
259
  // relocation offset is guaranteed to be even, add the relocation to
746
259
  // the RelrDyn section, otherwise add it to the RelaDyn section.
747
259
  // RelrDyn sections don't support odd offsets. Also, RelrDyn sections
748
259
  // don't store the addend values, so we must write it to the relocated
749
259
  // address.
750
259
  if (In.RelrDyn && 
IS->Alignment >= 284
&&
OffsetInSec % 2 == 084
) {
751
82
    IS->Relocations.push_back({Expr, Type, OffsetInSec, Addend, Sym});
752
82
    In.RelrDyn->Relocs.push_back({IS, OffsetInSec});
753
82
    return;
754
82
  }
755
177
  In.RelaDyn->addReloc(Target->RelativeRel, IS, OffsetInSec, Sym, Addend, Expr,
756
177
                       Type);
757
177
}
758
759
template <class ELFT, class GotPltSection>
760
static void addPltEntry(PltSection *Plt, GotPltSection *GotPlt,
761
355
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
355
  Plt->addEntry<ELFT>(Sym);
763
355
  GotPlt->addEntry(Sym);
764
355
  Rel->addReloc(
765
355
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
355
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
13
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
13
  Plt->addEntry<ELFT>(Sym);
763
13
  GotPlt->addEntry(Sym);
764
13
  Rel->addReloc(
765
13
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
13
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
70
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
70
  Plt->addEntry<ELFT>(Sym);
763
70
  GotPlt->addEntry(Sym);
764
70
  Rel->addReloc(
765
70
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
70
}
Unexecuted instantiation: Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
13
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
13
  Plt->addEntry<ELFT>(Sym);
763
13
  GotPlt->addEntry(Sym);
764
13
  Rel->addReloc(
765
13
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
13
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
27
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
27
  Plt->addEntry<ELFT>(Sym);
763
27
  GotPlt->addEntry(Sym);
764
27
  Rel->addReloc(
765
27
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
27
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
215
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
215
  Plt->addEntry<ELFT>(Sym);
763
215
  GotPlt->addEntry(Sym);
764
215
  Rel->addReloc(
765
215
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
215
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
1
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
1
  Plt->addEntry<ELFT>(Sym);
763
1
  GotPlt->addEntry(Sym);
764
1
  Rel->addReloc(
765
1
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
1
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
761
16
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
762
16
  Plt->addEntry<ELFT>(Sym);
763
16
  GotPlt->addEntry(Sym);
764
16
  Rel->addReloc(
765
16
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
766
16
}
767
768
141
template <class ELFT> static void addGotEntry(Symbol &Sym) {
769
141
  In.Got->addEntry(Sym);
770
141
771
141
  RelExpr Expr;
772
141
  if (Sym.isTls())
773
31
    Expr = R_TLS;
774
110
  else if (Sym.isGnuIFunc())
775
4
    Expr = R_PLT;
776
106
  else
777
106
    Expr = R_ABS;
778
141
779
141
  uint64_t Off = Sym.getGotOffset();
780
141
781
141
  // If a GOT slot value can be calculated at link-time, which is now,
782
141
  // we can just fill that out.
783
141
  //
784
141
  // (We don't actually write a value to a GOT slot right now, but we
785
141
  // add a static relocation to a Relocations vector so that
786
141
  // InputSection::relocate will do the work for us. We may be able
787
141
  // to just write a value now, but it is a TODO.)
788
141
  bool IsLinkTimeConstant =
789
141
      !Sym.IsPreemptible && 
(48
!Config->Pic48
||
isAbsolute(Sym)25
);
790
141
  if (IsLinkTimeConstant) {
791
27
    In.Got->Relocations.push_back({Expr, Target->GotRel, Off, 0, &Sym});
792
27
    return;
793
27
  }
794
114
795
114
  // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
796
114
  // the GOT slot will be fixed at load-time.
797
114
  if (!Sym.isTls() && 
!Sym.IsPreemptible85
&&
Config->Pic14
&&
!isAbsolute(Sym)14
) {
798
14
    addRelativeReloc(In.Got, Off, &Sym, 0, R_ABS, Target->GotRel);
799
14
    return;
800
14
  }
801
100
  In.RelaDyn->addReloc(Sym.isTls() ? 
Target->TlsGotRel29
:
Target->GotRel71
, In.Got,
802
100
                       Off, &Sym, 0, Sym.IsPreemptible ? 
R_ADDEND93
:
R_ABS7
,
803
100
                       Target->GotRel);
804
100
}
Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::Symbol&)
Line
Count
Source
768
45
template <class ELFT> static void addGotEntry(Symbol &Sym) {
769
45
  In.Got->addEntry(Sym);
770
45
771
45
  RelExpr Expr;
772
45
  if (Sym.isTls())
773
16
    Expr = R_TLS;
774
29
  else if (Sym.isGnuIFunc())
775
1
    Expr = R_PLT;
776
28
  else
777
28
    Expr = R_ABS;
778
45
779
45
  uint64_t Off = Sym.getGotOffset();
780
45
781
45
  // If a GOT slot value can be calculated at link-time, which is now,
782
45
  // we can just fill that out.
783
45
  //
784
45
  // (We don't actually write a value to a GOT slot right now, but we
785
45
  // add a static relocation to a Relocations vector so that
786
45
  // InputSection::relocate will do the work for us. We may be able
787
45
  // to just write a value now, but it is a TODO.)
788
45
  bool IsLinkTimeConstant =
789
45
      !Sym.IsPreemptible && 
(18
!Config->Pic18
||
isAbsolute(Sym)10
);
790
45
  if (IsLinkTimeConstant) {
791
8
    In.Got->Relocations.push_back({Expr, Target->GotRel, Off, 0, &Sym});
792
8
    return;
793
8
  }
794
37
795
37
  // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
796
37
  // the GOT slot will be fixed at load-time.
797
37
  if (!Sym.isTls() && 
!Sym.IsPreemptible23
&&
Config->Pic7
&&
!isAbsolute(Sym)7
) {
798
7
    addRelativeReloc(In.Got, Off, &Sym, 0, R_ABS, Target->GotRel);
799
7
    return;
800
7
  }
801
30
  In.RelaDyn->addReloc(Sym.isTls() ? 
Target->TlsGotRel14
:
Target->GotRel16
, In.Got,
802
30
                       Off, &Sym, 0, Sym.IsPreemptible ? 
R_ADDEND27
:
R_ABS3
,
803
30
                       Target->GotRel);
804
30
}
Unexecuted instantiation: Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::Symbol&)
Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::Symbol&)
Line
Count
Source
768
91
template <class ELFT> static void addGotEntry(Symbol &Sym) {
769
91
  In.Got->addEntry(Sym);
770
91
771
91
  RelExpr Expr;
772
91
  if (Sym.isTls())
773
12
    Expr = R_TLS;
774
79
  else if (Sym.isGnuIFunc())
775
3
    Expr = R_PLT;
776
76
  else
777
76
    Expr = R_ABS;
778
91
779
91
  uint64_t Off = Sym.getGotOffset();
780
91
781
91
  // If a GOT slot value can be calculated at link-time, which is now,
782
91
  // we can just fill that out.
783
91
  //
784
91
  // (We don't actually write a value to a GOT slot right now, but we
785
91
  // add a static relocation to a Relocations vector so that
786
91
  // InputSection::relocate will do the work for us. We may be able
787
91
  // to just write a value now, but it is a TODO.)
788
91
  bool IsLinkTimeConstant =
789
91
      !Sym.IsPreemptible && 
(30
!Config->Pic30
||
isAbsolute(Sym)15
);
790
91
  if (IsLinkTimeConstant) {
791
19
    In.Got->Relocations.push_back({Expr, Target->GotRel, Off, 0, &Sym});
792
19
    return;
793
19
  }
794
72
795
72
  // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
796
72
  // the GOT slot will be fixed at load-time.
797
72
  if (!Sym.isTls() && 
!Sym.IsPreemptible60
&&
Config->Pic7
&&
!isAbsolute(Sym)7
) {
798
7
    addRelativeReloc(In.Got, Off, &Sym, 0, R_ABS, Target->GotRel);
799
7
    return;
800
7
  }
801
65
  In.RelaDyn->addReloc(Sym.isTls() ? 
Target->TlsGotRel12
:
Target->GotRel53
, In.Got,
802
65
                       Off, &Sym, 0, Sym.IsPreemptible ? 
R_ADDEND61
:
R_ABS4
,
803
65
                       Target->GotRel);
804
65
}
Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::Symbol&)
Line
Count
Source
768
5
template <class ELFT> static void addGotEntry(Symbol &Sym) {
769
5
  In.Got->addEntry(Sym);
770
5
771
5
  RelExpr Expr;
772
5
  if (Sym.isTls())
773
3
    Expr = R_TLS;
774
2
  else if (Sym.isGnuIFunc())
775
0
    Expr = R_PLT;
776
2
  else
777
2
    Expr = R_ABS;
778
5
779
5
  uint64_t Off = Sym.getGotOffset();
780
5
781
5
  // If a GOT slot value can be calculated at link-time, which is now,
782
5
  // we can just fill that out.
783
5
  //
784
5
  // (We don't actually write a value to a GOT slot right now, but we
785
5
  // add a static relocation to a Relocations vector so that
786
5
  // InputSection::relocate will do the work for us. We may be able
787
5
  // to just write a value now, but it is a TODO.)
788
5
  bool IsLinkTimeConstant =
789
5
      !Sym.IsPreemptible && 
(0
!Config->Pic0
||
isAbsolute(Sym)0
);
790
5
  if (IsLinkTimeConstant) {
791
0
    In.Got->Relocations.push_back({Expr, Target->GotRel, Off, 0, &Sym});
792
0
    return;
793
0
  }
794
5
795
5
  // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
796
5
  // the GOT slot will be fixed at load-time.
797
5
  if (!Sym.isTls() && 
!Sym.IsPreemptible2
&&
Config->Pic0
&&
!isAbsolute(Sym)0
) {
798
0
    addRelativeReloc(In.Got, Off, &Sym, 0, R_ABS, Target->GotRel);
799
0
    return;
800
0
  }
801
5
  In.RelaDyn->addReloc(Sym.isTls() ? 
Target->TlsGotRel3
:
Target->GotRel2
, In.Got,
802
5
                       Off, &Sym, 0, Sym.IsPreemptible ? R_ADDEND : 
R_ABS0
,
803
5
                       Target->GotRel);
804
5
}
805
806
// Return true if we can define a symbol in the executable that
807
// contains the value/function of a symbol defined in a shared
808
// library.
809
118
static bool canDefineSymbolInExecutable(Symbol &Sym) {
810
118
  // If the symbol has default visibility the symbol defined in the
811
118
  // executable will preempt it.
812
118
  // Note that we want the visibility of the shared symbol itself, not
813
118
  // the visibility of the symbol in the output file we are producing. That is
814
118
  // why we use Sym.StOther.
815
118
  if ((Sym.StOther & 0x3) == STV_DEFAULT)
816
111
    return true;
817
7
818
7
  // If we are allowed to break address equality of functions, defining
819
7
  // a plt entry will allow the program to call the function in the
820
7
  // .so, but the .so and the executable will no agree on the address
821
7
  // of the function. Similar logic for objects.
822
7
  return ((Sym.isFunc() && 
Config->IgnoreFunctionAddressEquality3
) ||
823
7
          
(6
Sym.isObject()6
&&
Config->IgnoreDataAddressEquality2
));
824
7
}
825
826
// The reason we have to do this early scan is as follows
827
// * To mmap the output file, we need to know the size
828
// * For that, we need to know how many dynamic relocs we will have.
829
// It might be possible to avoid this by outputting the file with write:
830
// * Write the allocated output sections, computing addresses.
831
// * Apply relocations, recording which ones require a dynamic reloc.
832
// * Write the dynamic relocations.
833
// * Write the rest of the file.
834
// This would have some drawbacks. For example, we would only know if .rela.dyn
835
// is needed after applying relocations. If it is, it will go after rw and rx
836
// sections. Given that it is ro, we will need an extra PT_LOAD. This
837
// complicates things for the dynamic linker and means we would have to reserve
838
// space for the extra PT_LOAD even if we end up not using it.
839
template <class ELFT, class RelTy>
840
static void processRelocAux(InputSectionBase &Sec, RelExpr Expr, RelType Type,
841
                            uint64_t Offset, Symbol &Sym, const RelTy &Rel,
842
13.4k
                            int64_t Addend) {
843
13.4k
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
12.9k
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
12.9k
    return;
846
12.9k
  }
847
560
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText166
;
848
560
  if (CanWrite) {
849
412
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
412
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT175
;
851
412
852
412
    if (!IsPreemptibleValue) {
853
245
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
245
      return;
855
245
    } else 
if (RelType 167
Rel167
= Target->getDynRel(Type)) {
856
149
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
149
858
149
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
149
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
149
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
149
      // specially sorted dynamic symbol table. This affects even dynamic
862
149
      // relocations against symbols which do not require GOT entries
863
149
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
149
      // a preemptible symbol has a dynamic relocation we anyway have
865
149
      // to create a GOT entry for it.
866
149
      // If a non-preemptible symbol has a dynamic relocation against it,
867
149
      // dynamic linker takes it st_value, adds offset and writes down
868
149
      // result of the dynamic relocation. In case of preemptible symbol
869
149
      // dynamic linker performs symbol resolution, writes the symbol value
870
149
      // to the GOT entry and reads the GOT entry when it needs to perform
871
149
      // a dynamic relocation.
872
149
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
149
      if (Config->EMachine == EM_MIPS)
874
4
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
149
      return;
876
149
    }
877
166
  }
878
166
879
166
  // If the relocation is to a weak undef, and we are producing
880
166
  // executable, give up on it and produce a non preemptible 0.
881
166
  if (!Config->Shared && 
Sym.isUndefWeak()134
) {
882
8
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
8
    return;
884
8
  }
885
158
886
158
  if (!CanWrite && 
(140
Config->Pic140
&&
!isRelExpr(Expr)27
)) {
887
19
    error(
888
19
        "can't create dynamic relocation " + toString(Type) + " against " +
889
19
        (Sym.getName().empty() ? 
"local symbol"3
:
"symbol: " + toString(Sym)16
) +
890
19
        " in readonly segment; recompile object files with -fPIC "
891
19
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
19
        getLocation(Sec, Sym, Offset));
893
19
    return;
894
19
  }
895
139
896
139
  // Copy relocations are only possible if we are creating an executable.
897
139
  if (Config->Shared) {
898
18
    errorOrWarn("relocation " + toString(Type) +
899
18
                " cannot be used against symbol " + toString(Sym) +
900
18
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
18
    return;
902
18
  }
903
121
904
121
  // If the symbol is undefined we already reported any relevant errors.
905
121
  if (Sym.isUndefined())
906
3
    return;
907
118
908
118
  if (!canDefineSymbolInExecutable(Sym)) {
909
5
    error("cannot preempt symbol: " + toString(Sym) +
910
5
          getLocation(Sec, Sym, Offset));
911
5
    return;
912
5
  }
913
113
914
113
  if (Sym.isObject()) {
915
64
    // Produce a copy relocation.
916
64
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
51
      if (!Config->ZCopyreloc)
918
3
        error("unresolvable relocation " + toString(Type) +
919
3
              " against symbol '" + toString(*SS) +
920
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
3
              getLocation(Sec, Sym, Offset));
922
51
      addCopyRelSymbol<ELFT>(*SS);
923
51
    }
924
64
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
64
    return;
926
64
  }
927
49
928
49
  if (Sym.isFunc()) {
929
46
    // This handles a non PIC program call to function in a shared library. In
930
46
    // an ideal world, we could just report an error saying the relocation can
931
46
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
46
    // R_X86_64_PC32 pointing to libc.so.
933
46
    //
934
46
    // The general idea on how to handle such cases is to create a PLT entry and
935
46
    // use that as the function value.
936
46
    //
937
46
    // For the static linking part, we just return a plt expr and everything
938
46
    // else will use the PLT entry as the address.
939
46
    //
940
46
    // The remaining problem is making sure pointer equality still works. We
941
46
    // need the help of the dynamic linker for that. We let it know that we have
942
46
    // a direct reference to a so symbol by creating an undefined symbol with a
943
46
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
46
    // the value of the symbol we created. This is true even for got entries, so
945
46
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
46
    // that points to the real function is a dedicated got entry used by the
947
46
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
46
    // R_386_JMP_SLOT, etc).
949
46
950
46
    // For position independent executable on i386, the plt entry requires ebx
951
46
    // to be set. This causes two problems:
952
46
    // * If some code has a direct reference to a function, it was probably
953
46
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
46
    // * If a library definition gets preempted to the executable, it will have
955
46
    //   the wrong ebx value.
956
46
    if (Config->Pie && 
Config->EMachine == EM_3862
)
957
1
      errorOrWarn("symbol '" + toString(Sym) +
958
1
                  "' cannot be preempted; recompile with -fPIE" +
959
1
                  getLocation(Sec, Sym, Offset));
960
46
    if (!Sym.isInPlt())
961
42
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
46
    if (!Sym.isDefined())
963
43
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
46
    Sym.NeedsPltAddr = true;
965
46
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
46
    return;
967
46
  }
968
3
969
3
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
3
              getLocation(Sec, Sym, Offset));
971
3
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, long long)
Line
Count
Source
842
108
                            int64_t Addend) {
843
108
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
108
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
108
    return;
846
108
  }
847
0
  bool CanWrite = (Sec.Flags & SHF_WRITE) || !Config->ZText;
848
0
  if (CanWrite) {
849
0
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
0
    bool IsPreemptibleValue = Sym.IsPreemptible && Expr != R_GOT;
851
0
852
0
    if (!IsPreemptibleValue) {
853
0
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
0
      return;
855
0
    } else if (RelType Rel = Target->getDynRel(Type)) {
856
0
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
0
858
0
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
0
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
0
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
0
      // specially sorted dynamic symbol table. This affects even dynamic
862
0
      // relocations against symbols which do not require GOT entries
863
0
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
0
      // a preemptible symbol has a dynamic relocation we anyway have
865
0
      // to create a GOT entry for it.
866
0
      // If a non-preemptible symbol has a dynamic relocation against it,
867
0
      // dynamic linker takes it st_value, adds offset and writes down
868
0
      // result of the dynamic relocation. In case of preemptible symbol
869
0
      // dynamic linker performs symbol resolution, writes the symbol value
870
0
      // to the GOT entry and reads the GOT entry when it needs to perform
871
0
      // a dynamic relocation.
872
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
0
      if (Config->EMachine == EM_MIPS)
874
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
0
      return;
876
0
    }
877
0
  }
878
0
879
0
  // If the relocation is to a weak undef, and we are producing
880
0
  // executable, give up on it and produce a non preemptible 0.
881
0
  if (!Config->Shared && Sym.isUndefWeak()) {
882
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
0
    return;
884
0
  }
885
0
886
0
  if (!CanWrite && (Config->Pic && !isRelExpr(Expr))) {
887
0
    error(
888
0
        "can't create dynamic relocation " + toString(Type) + " against " +
889
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
890
0
        " in readonly segment; recompile object files with -fPIC "
891
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
0
        getLocation(Sec, Sym, Offset));
893
0
    return;
894
0
  }
895
0
896
0
  // Copy relocations are only possible if we are creating an executable.
897
0
  if (Config->Shared) {
898
0
    errorOrWarn("relocation " + toString(Type) +
899
0
                " cannot be used against symbol " + toString(Sym) +
900
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
0
    return;
902
0
  }
903
0
904
0
  // If the symbol is undefined we already reported any relevant errors.
905
0
  if (Sym.isUndefined())
906
0
    return;
907
0
908
0
  if (!canDefineSymbolInExecutable(Sym)) {
909
0
    error("cannot preempt symbol: " + toString(Sym) +
910
0
          getLocation(Sec, Sym, Offset));
911
0
    return;
912
0
  }
913
0
914
0
  if (Sym.isObject()) {
915
0
    // Produce a copy relocation.
916
0
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
0
      if (!Config->ZCopyreloc)
918
0
        error("unresolvable relocation " + toString(Type) +
919
0
              " against symbol '" + toString(*SS) +
920
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
0
              getLocation(Sec, Sym, Offset));
922
0
      addCopyRelSymbol<ELFT>(*SS);
923
0
    }
924
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
0
    return;
926
0
  }
927
0
928
0
  if (Sym.isFunc()) {
929
0
    // This handles a non PIC program call to function in a shared library. In
930
0
    // an ideal world, we could just report an error saying the relocation can
931
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
0
    // R_X86_64_PC32 pointing to libc.so.
933
0
    //
934
0
    // The general idea on how to handle such cases is to create a PLT entry and
935
0
    // use that as the function value.
936
0
    //
937
0
    // For the static linking part, we just return a plt expr and everything
938
0
    // else will use the PLT entry as the address.
939
0
    //
940
0
    // The remaining problem is making sure pointer equality still works. We
941
0
    // need the help of the dynamic linker for that. We let it know that we have
942
0
    // a direct reference to a so symbol by creating an undefined symbol with a
943
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
0
    // the value of the symbol we created. This is true even for got entries, so
945
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
0
    // that points to the real function is a dedicated got entry used by the
947
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
0
    // R_386_JMP_SLOT, etc).
949
0
950
0
    // For position independent executable on i386, the plt entry requires ebx
951
0
    // to be set. This causes two problems:
952
0
    // * If some code has a direct reference to a function, it was probably
953
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
0
    // * If a library definition gets preempted to the executable, it will have
955
0
    //   the wrong ebx value.
956
0
    if (Config->Pie && Config->EMachine == EM_386)
957
0
      errorOrWarn("symbol '" + toString(Sym) +
958
0
                  "' cannot be preempted; recompile with -fPIE" +
959
0
                  getLocation(Sec, Sym, Offset));
960
0
    if (!Sym.isInPlt())
961
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
0
    if (!Sym.isDefined())
963
0
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
0
    Sym.NeedsPltAddr = true;
965
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
0
    return;
967
0
  }
968
0
969
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
0
              getLocation(Sec, Sym, Offset));
971
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, long long)
Line
Count
Source
842
770
                            int64_t Addend) {
843
770
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
598
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
598
    return;
846
598
  }
847
172
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText16
;
848
172
  if (CanWrite) {
849
156
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
156
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT41
;
851
156
852
156
    if (!IsPreemptibleValue) {
853
123
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
123
      return;
855
123
    } else 
if (RelType 33
Rel33
= Target->getDynRel(Type)) {
856
33
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
33
858
33
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
33
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
33
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
33
      // specially sorted dynamic symbol table. This affects even dynamic
862
33
      // relocations against symbols which do not require GOT entries
863
33
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
33
      // a preemptible symbol has a dynamic relocation we anyway have
865
33
      // to create a GOT entry for it.
866
33
      // If a non-preemptible symbol has a dynamic relocation against it,
867
33
      // dynamic linker takes it st_value, adds offset and writes down
868
33
      // result of the dynamic relocation. In case of preemptible symbol
869
33
      // dynamic linker performs symbol resolution, writes the symbol value
870
33
      // to the GOT entry and reads the GOT entry when it needs to perform
871
33
      // a dynamic relocation.
872
33
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
33
      if (Config->EMachine == EM_MIPS)
874
1
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
33
      return;
876
33
    }
877
16
  }
878
16
879
16
  // If the relocation is to a weak undef, and we are producing
880
16
  // executable, give up on it and produce a non preemptible 0.
881
16
  if (!Config->Shared && 
Sym.isUndefWeak()14
) {
882
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
0
    return;
884
0
  }
885
16
886
16
  if (!CanWrite && (Config->Pic && 
!isRelExpr(Expr)6
)) {
887
5
    error(
888
5
        "can't create dynamic relocation " + toString(Type) + " against " +
889
5
        (Sym.getName().empty() ? 
"local symbol"0
: "symbol: " + toString(Sym)) +
890
5
        " in readonly segment; recompile object files with -fPIC "
891
5
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
5
        getLocation(Sec, Sym, Offset));
893
5
    return;
894
5
  }
895
11
896
11
  // Copy relocations are only possible if we are creating an executable.
897
11
  if (Config->Shared) {
898
0
    errorOrWarn("relocation " + toString(Type) +
899
0
                " cannot be used against symbol " + toString(Sym) +
900
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
0
    return;
902
0
  }
903
11
904
11
  // If the symbol is undefined we already reported any relevant errors.
905
11
  if (Sym.isUndefined())
906
0
    return;
907
11
908
11
  if (!canDefineSymbolInExecutable(Sym)) {
909
0
    error("cannot preempt symbol: " + toString(Sym) +
910
0
          getLocation(Sec, Sym, Offset));
911
0
    return;
912
0
  }
913
11
914
11
  if (Sym.isObject()) {
915
7
    // Produce a copy relocation.
916
7
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
6
      if (!Config->ZCopyreloc)
918
0
        error("unresolvable relocation " + toString(Type) +
919
0
              " against symbol '" + toString(*SS) +
920
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
0
              getLocation(Sec, Sym, Offset));
922
6
      addCopyRelSymbol<ELFT>(*SS);
923
6
    }
924
7
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
7
    return;
926
7
  }
927
4
928
4
  if (Sym.isFunc()) {
929
4
    // This handles a non PIC program call to function in a shared library. In
930
4
    // an ideal world, we could just report an error saying the relocation can
931
4
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
4
    // R_X86_64_PC32 pointing to libc.so.
933
4
    //
934
4
    // The general idea on how to handle such cases is to create a PLT entry and
935
4
    // use that as the function value.
936
4
    //
937
4
    // For the static linking part, we just return a plt expr and everything
938
4
    // else will use the PLT entry as the address.
939
4
    //
940
4
    // The remaining problem is making sure pointer equality still works. We
941
4
    // need the help of the dynamic linker for that. We let it know that we have
942
4
    // a direct reference to a so symbol by creating an undefined symbol with a
943
4
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
4
    // the value of the symbol we created. This is true even for got entries, so
945
4
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
4
    // that points to the real function is a dedicated got entry used by the
947
4
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
4
    // R_386_JMP_SLOT, etc).
949
4
950
4
    // For position independent executable on i386, the plt entry requires ebx
951
4
    // to be set. This causes two problems:
952
4
    // * If some code has a direct reference to a function, it was probably
953
4
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
4
    // * If a library definition gets preempted to the executable, it will have
955
4
    //   the wrong ebx value.
956
4
    if (Config->Pie && 
Config->EMachine == EM_3861
)
957
1
      errorOrWarn("symbol '" + toString(Sym) +
958
1
                  "' cannot be preempted; recompile with -fPIE" +
959
1
                  getLocation(Sec, Sym, Offset));
960
4
    if (!Sym.isInPlt())
961
4
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
4
    if (!Sym.isDefined())
963
4
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
4
    Sym.NeedsPltAddr = true;
965
4
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
4
    return;
967
4
  }
968
0
969
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
0
              getLocation(Sec, Sym, Offset));
971
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, long long)
Line
Count
Source
842
14
                            int64_t Addend) {
843
14
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
13
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
13
    return;
846
13
  }
847
1
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText0
;
848
1
  if (CanWrite) {
849
1
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
1
    bool IsPreemptibleValue = Sym.IsPreemptible && Expr != R_GOT;
851
1
852
1
    if (!IsPreemptibleValue) {
853
0
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
0
      return;
855
1
    } else if (RelType Rel = Target->getDynRel(Type)) {
856
1
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
1
858
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
1
      // specially sorted dynamic symbol table. This affects even dynamic
862
1
      // relocations against symbols which do not require GOT entries
863
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
1
      // a preemptible symbol has a dynamic relocation we anyway have
865
1
      // to create a GOT entry for it.
866
1
      // If a non-preemptible symbol has a dynamic relocation against it,
867
1
      // dynamic linker takes it st_value, adds offset and writes down
868
1
      // result of the dynamic relocation. In case of preemptible symbol
869
1
      // dynamic linker performs symbol resolution, writes the symbol value
870
1
      // to the GOT entry and reads the GOT entry when it needs to perform
871
1
      // a dynamic relocation.
872
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
1
      if (Config->EMachine == EM_MIPS)
874
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
1
      return;
876
1
    }
877
0
  }
878
0
879
0
  // If the relocation is to a weak undef, and we are producing
880
0
  // executable, give up on it and produce a non preemptible 0.
881
0
  if (!Config->Shared && Sym.isUndefWeak()) {
882
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
0
    return;
884
0
  }
885
0
886
0
  if (!CanWrite && (Config->Pic && !isRelExpr(Expr))) {
887
0
    error(
888
0
        "can't create dynamic relocation " + toString(Type) + " against " +
889
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
890
0
        " in readonly segment; recompile object files with -fPIC "
891
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
0
        getLocation(Sec, Sym, Offset));
893
0
    return;
894
0
  }
895
0
896
0
  // Copy relocations are only possible if we are creating an executable.
897
0
  if (Config->Shared) {
898
0
    errorOrWarn("relocation " + toString(Type) +
899
0
                " cannot be used against symbol " + toString(Sym) +
900
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
0
    return;
902
0
  }
903
0
904
0
  // If the symbol is undefined we already reported any relevant errors.
905
0
  if (Sym.isUndefined())
906
0
    return;
907
0
908
0
  if (!canDefineSymbolInExecutable(Sym)) {
909
0
    error("cannot preempt symbol: " + toString(Sym) +
910
0
          getLocation(Sec, Sym, Offset));
911
0
    return;
912
0
  }
913
0
914
0
  if (Sym.isObject()) {
915
0
    // Produce a copy relocation.
916
0
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
0
      if (!Config->ZCopyreloc)
918
0
        error("unresolvable relocation " + toString(Type) +
919
0
              " against symbol '" + toString(*SS) +
920
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
0
              getLocation(Sec, Sym, Offset));
922
0
      addCopyRelSymbol<ELFT>(*SS);
923
0
    }
924
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
0
    return;
926
0
  }
927
0
928
0
  if (Sym.isFunc()) {
929
0
    // This handles a non PIC program call to function in a shared library. In
930
0
    // an ideal world, we could just report an error saying the relocation can
931
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
0
    // R_X86_64_PC32 pointing to libc.so.
933
0
    //
934
0
    // The general idea on how to handle such cases is to create a PLT entry and
935
0
    // use that as the function value.
936
0
    //
937
0
    // For the static linking part, we just return a plt expr and everything
938
0
    // else will use the PLT entry as the address.
939
0
    //
940
0
    // The remaining problem is making sure pointer equality still works. We
941
0
    // need the help of the dynamic linker for that. We let it know that we have
942
0
    // a direct reference to a so symbol by creating an undefined symbol with a
943
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
0
    // the value of the symbol we created. This is true even for got entries, so
945
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
0
    // that points to the real function is a dedicated got entry used by the
947
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
0
    // R_386_JMP_SLOT, etc).
949
0
950
0
    // For position independent executable on i386, the plt entry requires ebx
951
0
    // to be set. This causes two problems:
952
0
    // * If some code has a direct reference to a function, it was probably
953
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
0
    // * If a library definition gets preempted to the executable, it will have
955
0
    //   the wrong ebx value.
956
0
    if (Config->Pie && Config->EMachine == EM_386)
957
0
      errorOrWarn("symbol '" + toString(Sym) +
958
0
                  "' cannot be preempted; recompile with -fPIE" +
959
0
                  getLocation(Sec, Sym, Offset));
960
0
    if (!Sym.isInPlt())
961
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
0
    if (!Sym.isDefined())
963
0
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
0
    Sym.NeedsPltAddr = true;
965
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
0
    return;
967
0
  }
968
0
969
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
0
              getLocation(Sec, Sym, Offset));
971
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, long long)
Line
Count
Source
842
244
                            int64_t Addend) {
843
244
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
227
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
227
    return;
846
227
  }
847
17
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText15
;
848
17
  if (CanWrite) {
849
2
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
2
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT1
;
851
2
852
2
    if (!IsPreemptibleValue) {
853
1
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
1
      return;
855
1
    } else if (RelType Rel = Target->getDynRel(Type)) {
856
1
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
1
858
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
1
      // specially sorted dynamic symbol table. This affects even dynamic
862
1
      // relocations against symbols which do not require GOT entries
863
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
1
      // a preemptible symbol has a dynamic relocation we anyway have
865
1
      // to create a GOT entry for it.
866
1
      // If a non-preemptible symbol has a dynamic relocation against it,
867
1
      // dynamic linker takes it st_value, adds offset and writes down
868
1
      // result of the dynamic relocation. In case of preemptible symbol
869
1
      // dynamic linker performs symbol resolution, writes the symbol value
870
1
      // to the GOT entry and reads the GOT entry when it needs to perform
871
1
      // a dynamic relocation.
872
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
1
      if (Config->EMachine == EM_MIPS)
874
1
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
1
      return;
876
1
    }
877
15
  }
878
15
879
15
  // If the relocation is to a weak undef, and we are producing
880
15
  // executable, give up on it and produce a non preemptible 0.
881
15
  if (!Config->Shared && Sym.isUndefWeak()) {
882
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
0
    return;
884
0
  }
885
15
886
15
  if (!CanWrite && (Config->Pic && 
!isRelExpr(Expr)0
)) {
887
0
    error(
888
0
        "can't create dynamic relocation " + toString(Type) + " against " +
889
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
890
0
        " in readonly segment; recompile object files with -fPIC "
891
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
0
        getLocation(Sec, Sym, Offset));
893
0
    return;
894
0
  }
895
15
896
15
  // Copy relocations are only possible if we are creating an executable.
897
15
  if (Config->Shared) {
898
0
    errorOrWarn("relocation " + toString(Type) +
899
0
                " cannot be used against symbol " + toString(Sym) +
900
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
0
    return;
902
0
  }
903
15
904
15
  // If the symbol is undefined we already reported any relevant errors.
905
15
  if (Sym.isUndefined())
906
0
    return;
907
15
908
15
  if (!canDefineSymbolInExecutable(Sym)) {
909
0
    error("cannot preempt symbol: " + toString(Sym) +
910
0
          getLocation(Sec, Sym, Offset));
911
0
    return;
912
0
  }
913
15
914
15
  if (Sym.isObject()) {
915
8
    // Produce a copy relocation.
916
8
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
5
      if (!Config->ZCopyreloc)
918
0
        error("unresolvable relocation " + toString(Type) +
919
0
              " against symbol '" + toString(*SS) +
920
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
0
              getLocation(Sec, Sym, Offset));
922
5
      addCopyRelSymbol<ELFT>(*SS);
923
5
    }
924
8
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
8
    return;
926
8
  }
927
7
928
7
  if (Sym.isFunc()) {
929
7
    // This handles a non PIC program call to function in a shared library. In
930
7
    // an ideal world, we could just report an error saying the relocation can
931
7
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
7
    // R_X86_64_PC32 pointing to libc.so.
933
7
    //
934
7
    // The general idea on how to handle such cases is to create a PLT entry and
935
7
    // use that as the function value.
936
7
    //
937
7
    // For the static linking part, we just return a plt expr and everything
938
7
    // else will use the PLT entry as the address.
939
7
    //
940
7
    // The remaining problem is making sure pointer equality still works. We
941
7
    // need the help of the dynamic linker for that. We let it know that we have
942
7
    // a direct reference to a so symbol by creating an undefined symbol with a
943
7
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
7
    // the value of the symbol we created. This is true even for got entries, so
945
7
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
7
    // that points to the real function is a dedicated got entry used by the
947
7
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
7
    // R_386_JMP_SLOT, etc).
949
7
950
7
    // For position independent executable on i386, the plt entry requires ebx
951
7
    // to be set. This causes two problems:
952
7
    // * If some code has a direct reference to a function, it was probably
953
7
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
7
    // * If a library definition gets preempted to the executable, it will have
955
7
    //   the wrong ebx value.
956
7
    if (Config->Pie && 
Config->EMachine == EM_3860
)
957
0
      errorOrWarn("symbol '" + toString(Sym) +
958
0
                  "' cannot be preempted; recompile with -fPIE" +
959
0
                  getLocation(Sec, Sym, Offset));
960
7
    if (!Sym.isInPlt())
961
4
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
7
    if (!Sym.isDefined())
963
4
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
7
    Sym.NeedsPltAddr = true;
965
7
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
7
    return;
967
7
  }
968
0
969
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
0
              getLocation(Sec, Sym, Offset));
971
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, long long)
Line
Count
Source
842
2.01k
                            int64_t Addend) {
843
2.01k
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
1.66k
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
1.66k
    return;
846
1.66k
  }
847
354
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText133
;
848
354
  if (CanWrite) {
849
237
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
237
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT124
;
851
237
852
237
    if (!IsPreemptibleValue) {
853
113
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
113
      return;
855
124
    } else if (RelType Rel = Target->getDynRel(Type)) {
856
106
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
106
858
106
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
106
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
106
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
106
      // specially sorted dynamic symbol table. This affects even dynamic
862
106
      // relocations against symbols which do not require GOT entries
863
106
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
106
      // a preemptible symbol has a dynamic relocation we anyway have
865
106
      // to create a GOT entry for it.
866
106
      // If a non-preemptible symbol has a dynamic relocation against it,
867
106
      // dynamic linker takes it st_value, adds offset and writes down
868
106
      // result of the dynamic relocation. In case of preemptible symbol
869
106
      // dynamic linker performs symbol resolution, writes the symbol value
870
106
      // to the GOT entry and reads the GOT entry when it needs to perform
871
106
      // a dynamic relocation.
872
106
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
106
      if (Config->EMachine == EM_MIPS)
874
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
106
      return;
876
106
    }
877
135
  }
878
135
879
135
  // If the relocation is to a weak undef, and we are producing
880
135
  // executable, give up on it and produce a non preemptible 0.
881
135
  if (!Config->Shared && 
Sym.isUndefWeak()105
) {
882
8
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
8
    return;
884
8
  }
885
127
886
127
  if (!CanWrite && 
(109
Config->Pic109
&&
!isRelExpr(Expr)21
)) {
887
14
    error(
888
14
        "can't create dynamic relocation " + toString(Type) + " against " +
889
14
        (Sym.getName().empty() ? 
"local symbol"3
:
"symbol: " + toString(Sym)11
) +
890
14
        " in readonly segment; recompile object files with -fPIC "
891
14
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
14
        getLocation(Sec, Sym, Offset));
893
14
    return;
894
14
  }
895
113
896
113
  // Copy relocations are only possible if we are creating an executable.
897
113
  if (Config->Shared) {
898
18
    errorOrWarn("relocation " + toString(Type) +
899
18
                " cannot be used against symbol " + toString(Sym) +
900
18
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
18
    return;
902
18
  }
903
95
904
95
  // If the symbol is undefined we already reported any relevant errors.
905
95
  if (Sym.isUndefined())
906
3
    return;
907
92
908
92
  if (!canDefineSymbolInExecutable(Sym)) {
909
5
    error("cannot preempt symbol: " + toString(Sym) +
910
5
          getLocation(Sec, Sym, Offset));
911
5
    return;
912
5
  }
913
87
914
87
  if (Sym.isObject()) {
915
49
    // Produce a copy relocation.
916
49
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
40
      if (!Config->ZCopyreloc)
918
3
        error("unresolvable relocation " + toString(Type) +
919
3
              " against symbol '" + toString(*SS) +
920
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
3
              getLocation(Sec, Sym, Offset));
922
40
      addCopyRelSymbol<ELFT>(*SS);
923
40
    }
924
49
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
49
    return;
926
49
  }
927
38
928
38
  if (Sym.isFunc()) {
929
35
    // This handles a non PIC program call to function in a shared library. In
930
35
    // an ideal world, we could just report an error saying the relocation can
931
35
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
35
    // R_X86_64_PC32 pointing to libc.so.
933
35
    //
934
35
    // The general idea on how to handle such cases is to create a PLT entry and
935
35
    // use that as the function value.
936
35
    //
937
35
    // For the static linking part, we just return a plt expr and everything
938
35
    // else will use the PLT entry as the address.
939
35
    //
940
35
    // The remaining problem is making sure pointer equality still works. We
941
35
    // need the help of the dynamic linker for that. We let it know that we have
942
35
    // a direct reference to a so symbol by creating an undefined symbol with a
943
35
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
35
    // the value of the symbol we created. This is true even for got entries, so
945
35
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
35
    // that points to the real function is a dedicated got entry used by the
947
35
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
35
    // R_386_JMP_SLOT, etc).
949
35
950
35
    // For position independent executable on i386, the plt entry requires ebx
951
35
    // to be set. This causes two problems:
952
35
    // * If some code has a direct reference to a function, it was probably
953
35
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
35
    // * If a library definition gets preempted to the executable, it will have
955
35
    //   the wrong ebx value.
956
35
    if (Config->Pie && 
Config->EMachine == EM_3861
)
957
0
      errorOrWarn("symbol '" + toString(Sym) +
958
0
                  "' cannot be preempted; recompile with -fPIE" +
959
0
                  getLocation(Sec, Sym, Offset));
960
35
    if (!Sym.isInPlt())
961
34
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
35
    if (!Sym.isDefined())
963
35
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
35
    Sym.NeedsPltAddr = true;
965
35
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
35
    return;
967
35
  }
968
3
969
3
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
3
              getLocation(Sec, Sym, Offset));
971
3
}
Unexecuted instantiation: Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, long long)
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, long long)
Line
Count
Source
842
10.3k
                            int64_t Addend) {
843
10.3k
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
844
10.3k
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
845
10.3k
    return;
846
10.3k
  }
847
16
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText2
;
848
16
  if (CanWrite) {
849
16
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
850
16
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT8
;
851
16
852
16
    if (!IsPreemptibleValue) {
853
8
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
854
8
      return;
855
8
    } else if (RelType Rel = Target->getDynRel(Type)) {
856
8
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
857
8
858
8
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
859
8
      // While regular ABI uses dynamic relocations to fill up GOT entries
860
8
      // MIPS ABI requires dynamic linker to fills up GOT entries using
861
8
      // specially sorted dynamic symbol table. This affects even dynamic
862
8
      // relocations against symbols which do not require GOT entries
863
8
      // creation explicitly, i.e. do not have any GOT-relocations. So if
864
8
      // a preemptible symbol has a dynamic relocation we anyway have
865
8
      // to create a GOT entry for it.
866
8
      // If a non-preemptible symbol has a dynamic relocation against it,
867
8
      // dynamic linker takes it st_value, adds offset and writes down
868
8
      // result of the dynamic relocation. In case of preemptible symbol
869
8
      // dynamic linker performs symbol resolution, writes the symbol value
870
8
      // to the GOT entry and reads the GOT entry when it needs to perform
871
8
      // a dynamic relocation.
872
8
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
873
8
      if (Config->EMachine == EM_MIPS)
874
2
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
875
8
      return;
876
8
    }
877
0
  }
878
0
879
0
  // If the relocation is to a weak undef, and we are producing
880
0
  // executable, give up on it and produce a non preemptible 0.
881
0
  if (!Config->Shared && Sym.isUndefWeak()) {
882
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
883
0
    return;
884
0
  }
885
0
886
0
  if (!CanWrite && (Config->Pic && !isRelExpr(Expr))) {
887
0
    error(
888
0
        "can't create dynamic relocation " + toString(Type) + " against " +
889
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
890
0
        " in readonly segment; recompile object files with -fPIC "
891
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
892
0
        getLocation(Sec, Sym, Offset));
893
0
    return;
894
0
  }
895
0
896
0
  // Copy relocations are only possible if we are creating an executable.
897
0
  if (Config->Shared) {
898
0
    errorOrWarn("relocation " + toString(Type) +
899
0
                " cannot be used against symbol " + toString(Sym) +
900
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
901
0
    return;
902
0
  }
903
0
904
0
  // If the symbol is undefined we already reported any relevant errors.
905
0
  if (Sym.isUndefined())
906
0
    return;
907
0
908
0
  if (!canDefineSymbolInExecutable(Sym)) {
909
0
    error("cannot preempt symbol: " + toString(Sym) +
910
0
          getLocation(Sec, Sym, Offset));
911
0
    return;
912
0
  }
913
0
914
0
  if (Sym.isObject()) {
915
0
    // Produce a copy relocation.
916
0
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
917
0
      if (!Config->ZCopyreloc)
918
0
        error("unresolvable relocation " + toString(Type) +
919
0
              " against symbol '" + toString(*SS) +
920
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
921
0
              getLocation(Sec, Sym, Offset));
922
0
      addCopyRelSymbol<ELFT>(*SS);
923
0
    }
924
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
925
0
    return;
926
0
  }
927
0
928
0
  if (Sym.isFunc()) {
929
0
    // This handles a non PIC program call to function in a shared library. In
930
0
    // an ideal world, we could just report an error saying the relocation can
931
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
932
0
    // R_X86_64_PC32 pointing to libc.so.
933
0
    //
934
0
    // The general idea on how to handle such cases is to create a PLT entry and
935
0
    // use that as the function value.
936
0
    //
937
0
    // For the static linking part, we just return a plt expr and everything
938
0
    // else will use the PLT entry as the address.
939
0
    //
940
0
    // The remaining problem is making sure pointer equality still works. We
941
0
    // need the help of the dynamic linker for that. We let it know that we have
942
0
    // a direct reference to a so symbol by creating an undefined symbol with a
943
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
944
0
    // the value of the symbol we created. This is true even for got entries, so
945
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
946
0
    // that points to the real function is a dedicated got entry used by the
947
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
948
0
    // R_386_JMP_SLOT, etc).
949
0
950
0
    // For position independent executable on i386, the plt entry requires ebx
951
0
    // to be set. This causes two problems:
952
0
    // * If some code has a direct reference to a function, it was probably
953
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
954
0
    // * If a library definition gets preempted to the executable, it will have
955
0
    //   the wrong ebx value.
956
0
    if (Config->Pie && Config->EMachine == EM_386)
957
0
      errorOrWarn("symbol '" + toString(Sym) +
958
0
                  "' cannot be preempted; recompile with -fPIE" +
959
0
                  getLocation(Sec, Sym, Offset));
960
0
    if (!Sym.isInPlt())
961
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
962
0
    if (!Sym.isDefined())
963
0
      replaceWithDefined(Sym, In.Plt, getPltEntryOffset(Sym.PltIndex), 0);
964
0
    Sym.NeedsPltAddr = true;
965
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
966
0
    return;
967
0
  }
968
0
969
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
970
0
              getLocation(Sec, Sym, Offset));
971
0
}
Unexecuted instantiation: Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, long long)
972
973
template <class ELFT, class RelTy>
974
static void scanReloc(InputSectionBase &Sec, OffsetGetter &GetOffset, RelTy *&I,
975
13.8k
                      RelTy *End) {
976
13.8k
  const RelTy &Rel = *I;
977
13.8k
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
13.8k
  RelType Type;
979
13.8k
980
13.8k
  // Deal with MIPS oddity.
981
13.8k
  if (Config->MipsN32Abi) {
982
5
    Type = getMipsN32RelType(I, End);
983
13.8k
  } else {
984
13.8k
    Type = Rel.getType(Config->IsMips64EL);
985
13.8k
    ++I;
986
13.8k
  }
987
13.8k
988
13.8k
  // Get an offset in an output section this relocation is applied to.
989
13.8k
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
13.8k
  if (Offset == uint64_t(-1))
991
13
    return;
992
13.8k
993
13.8k
  // Skip if the target symbol is an erroneous undefined symbol.
994
13.8k
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
75
    return;
996
13.8k
997
13.8k
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
13.8k
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
13.8k
1000
13.8k
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
13.8k
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
22
    return;
1003
13.7k
1004
13.7k
  // Strenghten or relax relocations.
1005
13.7k
  //
1006
13.7k
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
13.7k
  // are determined by runtime.
1008
13.7k
  //
1009
13.7k
  // On the other hand, if we know that a PLT entry will be resolved within
1010
13.7k
  // the same ELF module, we can skip PLT access and directly jump to the
1011
13.7k
  // destination function. For example, if we are linking a main exectuable,
1012
13.7k
  // all dynamic symbols that can be resolved within the executable will
1013
13.7k
  // actually be resolved that way at runtime, because the main exectuable
1014
13.7k
  // is always at the beginning of a search list. We can leverage that fact.
1015
13.7k
  if (Sym.isGnuIFunc()) {
1016
56
    if (!Config->ZText && 
Config->WarnIfuncTextrel2
) {
1017
1
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
1
           "a binary that will segfault, if the object file is linked with "
1019
1
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
1
           "you, consider recompiling the object files without -fPIC and "
1021
1
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
1
           "turn off this warning." +
1023
1
           getLocation(Sec, Sym, Offset));
1024
1
    }
1025
56
    Expr = toPlt(Expr);
1026
13.7k
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC12.8k
&&
!isAbsoluteValue(Sym)91
) {
1027
50
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
13.6k
  } else if (!Sym.IsPreemptible) {
1029
12.7k
    Expr = fromPlt(Expr);
1030
12.7k
  }
1031
13.7k
1032
13.7k
  // This relocation does not require got entry, but it is relative to got and
1033
13.7k
  // needs it to be created. Here we request for that.
1034
13.7k
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
13.7k
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
148
    In.Got->HasGotOffRel = true;
1037
13.7k
1038
13.7k
  // Read an addend.
1039
13.7k
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
13.7k
1041
13.7k
  // Process some TLS relocations, including relaxing TLS relocations.
1042
13.7k
  // Note that this function does not handle all TLS relocations.
1043
13.7k
  if (unsigned Processed =
1044
303
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
303
    I += (Processed - 1);
1046
303
    return;
1047
303
  }
1048
13.4k
1049
13.4k
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
13.4k
  if (needsPlt(Expr) && 
!Sym.isInPlt()363
) {
1051
313
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible43
)
1052
41
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
41
                        Sym);
1054
272
    else
1055
272
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
313
  }
1057
13.4k
1058
13.4k
  // Create a GOT slot if a relocation needs GOT.
1059
13.4k
  if (needsGot(Expr)) {
1060
10.3k
    if (Config->EMachine == EM_MIPS) {
1061
10.1k
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
10.1k
      // require relocation entries for them. A special case is TLS
1063
10.1k
      // relocations. In that case dynamic loader applies dynamic
1064
10.1k
      // relocations to initialize TLS GOT entries.
1065
10.1k
      // See "Global Offset Table" in Chapter 5 in the following document
1066
10.1k
      // for detailed description:
1067
10.1k
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
10.1k
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
10.1k
    } else 
if (258
!Sym.isInGot()258
) {
1070
141
      addGotEntry<ELFT>(Sym);
1071
141
    }
1072
10.3k
  }
1073
13.4k
1074
13.4k
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
13.4k
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*)
Line
Count
Source
975
109
                      RelTy *End) {
976
109
  const RelTy &Rel = *I;
977
109
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
109
  RelType Type;
979
109
980
109
  // Deal with MIPS oddity.
981
109
  if (Config->MipsN32Abi) {
982
0
    Type = getMipsN32RelType(I, End);
983
109
  } else {
984
109
    Type = Rel.getType(Config->IsMips64EL);
985
109
    ++I;
986
109
  }
987
109
988
109
  // Get an offset in an output section this relocation is applied to.
989
109
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
109
  if (Offset == uint64_t(-1))
991
0
    return;
992
109
993
109
  // Skip if the target symbol is an erroneous undefined symbol.
994
109
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
0
    return;
996
109
997
109
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
109
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
109
1000
109
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
109
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
1
    return;
1003
108
1004
108
  // Strenghten or relax relocations.
1005
108
  //
1006
108
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
108
  // are determined by runtime.
1008
108
  //
1009
108
  // On the other hand, if we know that a PLT entry will be resolved within
1010
108
  // the same ELF module, we can skip PLT access and directly jump to the
1011
108
  // destination function. For example, if we are linking a main exectuable,
1012
108
  // all dynamic symbols that can be resolved within the executable will
1013
108
  // actually be resolved that way at runtime, because the main exectuable
1014
108
  // is always at the beginning of a search list. We can leverage that fact.
1015
108
  if (Sym.isGnuIFunc()) {
1016
0
    if (!Config->ZText && Config->WarnIfuncTextrel) {
1017
0
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
0
           "a binary that will segfault, if the object file is linked with "
1019
0
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
0
           "you, consider recompiling the object files without -fPIC and "
1021
0
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
0
           "turn off this warning." +
1023
0
           getLocation(Sec, Sym, Offset));
1024
0
    }
1025
0
    Expr = toPlt(Expr);
1026
108
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC100
&&
!isAbsoluteValue(Sym)0
) {
1027
0
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
108
  } else if (!Sym.IsPreemptible) {
1029
100
    Expr = fromPlt(Expr);
1030
100
  }
1031
108
1032
108
  // This relocation does not require got entry, but it is relative to got and
1033
108
  // needs it to be created. Here we request for that.
1034
108
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
108
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
0
    In.Got->HasGotOffRel = true;
1037
108
1038
108
  // Read an addend.
1039
108
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
108
1041
108
  // Process some TLS relocations, including relaxing TLS relocations.
1042
108
  // Note that this function does not handle all TLS relocations.
1043
108
  if (unsigned Processed =
1044
0
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
0
    I += (Processed - 1);
1046
0
    return;
1047
0
  }
1048
108
1049
108
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
108
  if (needsPlt(Expr) && 
!Sym.isInPlt()4
) {
1051
2
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible0
)
1052
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
0
                        Sym);
1054
2
    else
1055
2
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
2
  }
1057
108
1058
108
  // Create a GOT slot if a relocation needs GOT.
1059
108
  if (needsGot(Expr)) {
1060
4
    if (Config->EMachine == EM_MIPS) {
1061
0
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
0
      // require relocation entries for them. A special case is TLS
1063
0
      // relocations. In that case dynamic loader applies dynamic
1064
0
      // relocations to initialize TLS GOT entries.
1065
0
      // See "Global Offset Table" in Chapter 5 in the following document
1066
0
      // for detailed description:
1067
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
0
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
4
    } else if (!Sym.isInGot()) {
1070
1
      addGotEntry<ELFT>(Sym);
1071
1
    }
1072
4
  }
1073
108
1074
108
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
108
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*)
Line
Count
Source
975
825
                      RelTy *End) {
976
825
  const RelTy &Rel = *I;
977
825
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
825
  RelType Type;
979
825
980
825
  // Deal with MIPS oddity.
981
825
  if (Config->MipsN32Abi) {
982
0
    Type = getMipsN32RelType(I, End);
983
825
  } else {
984
825
    Type = Rel.getType(Config->IsMips64EL);
985
825
    ++I;
986
825
  }
987
825
988
825
  // Get an offset in an output section this relocation is applied to.
989
825
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
825
  if (Offset == uint64_t(-1))
991
0
    return;
992
825
993
825
  // Skip if the target symbol is an erroneous undefined symbol.
994
825
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
1
    return;
996
824
997
824
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
824
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
824
1000
824
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
824
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
16
    return;
1003
808
1004
808
  // Strenghten or relax relocations.
1005
808
  //
1006
808
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
808
  // are determined by runtime.
1008
808
  //
1009
808
  // On the other hand, if we know that a PLT entry will be resolved within
1010
808
  // the same ELF module, we can skip PLT access and directly jump to the
1011
808
  // destination function. For example, if we are linking a main exectuable,
1012
808
  // all dynamic symbols that can be resolved within the executable will
1013
808
  // actually be resolved that way at runtime, because the main exectuable
1014
808
  // is always at the beginning of a search list. We can leverage that fact.
1015
808
  if (Sym.isGnuIFunc()) {
1016
14
    if (!Config->ZText && 
Config->WarnIfuncTextrel0
) {
1017
0
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
0
           "a binary that will segfault, if the object file is linked with "
1019
0
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
0
           "you, consider recompiling the object files without -fPIC and "
1021
0
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
0
           "turn off this warning." +
1023
0
           getLocation(Sec, Sym, Offset));
1024
0
    }
1025
14
    Expr = toPlt(Expr);
1026
794
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC621
&&
!isAbsoluteValue(Sym)6
) {
1027
3
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
791
  } else if (!Sym.IsPreemptible) {
1029
618
    Expr = fromPlt(Expr);
1030
618
  }
1031
808
1032
808
  // This relocation does not require got entry, but it is relative to got and
1033
808
  // needs it to be created. Here we request for that.
1034
808
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
808
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
13
    In.Got->HasGotOffRel = true;
1037
808
1038
808
  // Read an addend.
1039
808
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
808
1041
808
  // Process some TLS relocations, including relaxing TLS relocations.
1042
808
  // Note that this function does not handle all TLS relocations.
1043
808
  if (unsigned Processed =
1044
38
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
38
    I += (Processed - 1);
1046
38
    return;
1047
38
  }
1048
770
1049
770
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
770
  if (needsPlt(Expr) && 
!Sym.isInPlt()96
) {
1051
77
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible13
)
1052
13
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
13
                        Sym);
1054
64
    else
1055
64
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
77
  }
1057
770
1058
770
  // Create a GOT slot if a relocation needs GOT.
1059
770
  if (needsGot(Expr)) {
1060
64
    if (Config->EMachine == EM_MIPS) {
1061
6
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
6
      // require relocation entries for them. A special case is TLS
1063
6
      // relocations. In that case dynamic loader applies dynamic
1064
6
      // relocations to initialize TLS GOT entries.
1065
6
      // See "Global Offset Table" in Chapter 5 in the following document
1066
6
      // for detailed description:
1067
6
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
6
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
58
    } else if (!Sym.isInGot()) {
1070
44
      addGotEntry<ELFT>(Sym);
1071
44
    }
1072
64
  }
1073
770
1074
770
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
770
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*)
Line
Count
Source
975
14
                      RelTy *End) {
976
14
  const RelTy &Rel = *I;
977
14
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
14
  RelType Type;
979
14
980
14
  // Deal with MIPS oddity.
981
14
  if (Config->MipsN32Abi) {
982
5
    Type = getMipsN32RelType(I, End);
983
9
  } else {
984
9
    Type = Rel.getType(Config->IsMips64EL);
985
9
    ++I;
986
9
  }
987
14
988
14
  // Get an offset in an output section this relocation is applied to.
989
14
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
14
  if (Offset == uint64_t(-1))
991
0
    return;
992
14
993
14
  // Skip if the target symbol is an erroneous undefined symbol.
994
14
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
0
    return;
996
14
997
14
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
14
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
14
1000
14
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
14
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
0
    return;
1003
14
1004
14
  // Strenghten or relax relocations.
1005
14
  //
1006
14
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
14
  // are determined by runtime.
1008
14
  //
1009
14
  // On the other hand, if we know that a PLT entry will be resolved within
1010
14
  // the same ELF module, we can skip PLT access and directly jump to the
1011
14
  // destination function. For example, if we are linking a main exectuable,
1012
14
  // all dynamic symbols that can be resolved within the executable will
1013
14
  // actually be resolved that way at runtime, because the main exectuable
1014
14
  // is always at the beginning of a search list. We can leverage that fact.
1015
14
  if (Sym.isGnuIFunc()) {
1016
0
    if (!Config->ZText && Config->WarnIfuncTextrel) {
1017
0
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
0
           "a binary that will segfault, if the object file is linked with "
1019
0
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
0
           "you, consider recompiling the object files without -fPIC and "
1021
0
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
0
           "turn off this warning." +
1023
0
           getLocation(Sec, Sym, Offset));
1024
0
    }
1025
0
    Expr = toPlt(Expr);
1026
14
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC11
&&
!isAbsoluteValue(Sym)0
) {
1027
0
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
14
  } else if (!Sym.IsPreemptible) {
1029
11
    Expr = fromPlt(Expr);
1030
11
  }
1031
14
1032
14
  // This relocation does not require got entry, but it is relative to got and
1033
14
  // needs it to be created. Here we request for that.
1034
14
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
14
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
0
    In.Got->HasGotOffRel = true;
1037
14
1038
14
  // Read an addend.
1039
14
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
14
1041
14
  // Process some TLS relocations, including relaxing TLS relocations.
1042
14
  // Note that this function does not handle all TLS relocations.
1043
14
  if (unsigned Processed =
1044
0
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
0
    I += (Processed - 1);
1046
0
    return;
1047
0
  }
1048
14
1049
14
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
14
  if (needsPlt(Expr) && 
!Sym.isInPlt()2
) {
1051
2
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible0
)
1052
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
0
                        Sym);
1054
2
    else
1055
2
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
2
  }
1057
14
1058
14
  // Create a GOT slot if a relocation needs GOT.
1059
14
  if (needsGot(Expr)) {
1060
0
    if (Config->EMachine == EM_MIPS) {
1061
0
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
0
      // require relocation entries for them. A special case is TLS
1063
0
      // relocations. In that case dynamic loader applies dynamic
1064
0
      // relocations to initialize TLS GOT entries.
1065
0
      // See "Global Offset Table" in Chapter 5 in the following document
1066
0
      // for detailed description:
1067
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
0
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
0
    } else if (!Sym.isInGot()) {
1070
0
      addGotEntry<ELFT>(Sym);
1071
0
    }
1072
0
  }
1073
14
1074
14
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
14
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*)
Line
Count
Source
975
255
                      RelTy *End) {
976
255
  const RelTy &Rel = *I;
977
255
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
255
  RelType Type;
979
255
980
255
  // Deal with MIPS oddity.
981
255
  if (Config->MipsN32Abi) {
982
0
    Type = getMipsN32RelType(I, End);
983
255
  } else {
984
255
    Type = Rel.getType(Config->IsMips64EL);
985
255
    ++I;
986
255
  }
987
255
988
255
  // Get an offset in an output section this relocation is applied to.
989
255
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
255
  if (Offset == uint64_t(-1))
991
0
    return;
992
255
993
255
  // Skip if the target symbol is an erroneous undefined symbol.
994
255
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
0
    return;
996
255
997
255
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
255
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
255
1000
255
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
255
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
0
    return;
1003
255
1004
255
  // Strenghten or relax relocations.
1005
255
  //
1006
255
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
255
  // are determined by runtime.
1008
255
  //
1009
255
  // On the other hand, if we know that a PLT entry will be resolved within
1010
255
  // the same ELF module, we can skip PLT access and directly jump to the
1011
255
  // destination function. For example, if we are linking a main exectuable,
1012
255
  // all dynamic symbols that can be resolved within the executable will
1013
255
  // actually be resolved that way at runtime, because the main exectuable
1014
255
  // is always at the beginning of a search list. We can leverage that fact.
1015
255
  if (Sym.isGnuIFunc()) {
1016
0
    if (!Config->ZText && Config->WarnIfuncTextrel) {
1017
0
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
0
           "a binary that will segfault, if the object file is linked with "
1019
0
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
0
           "you, consider recompiling the object files without -fPIC and "
1021
0
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
0
           "turn off this warning." +
1023
0
           getLocation(Sec, Sym, Offset));
1024
0
    }
1025
0
    Expr = toPlt(Expr);
1026
255
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC189
&&
!isAbsoluteValue(Sym)0
) {
1027
0
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
255
  } else if (!Sym.IsPreemptible) {
1029
189
    Expr = fromPlt(Expr);
1030
189
  }
1031
255
1032
255
  // This relocation does not require got entry, but it is relative to got and
1033
255
  // needs it to be created. Here we request for that.
1034
255
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
255
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
0
    In.Got->HasGotOffRel = true;
1037
255
1038
255
  // Read an addend.
1039
255
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
255
1041
255
  // Process some TLS relocations, including relaxing TLS relocations.
1042
255
  // Note that this function does not handle all TLS relocations.
1043
255
  if (unsigned Processed =
1044
11
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
11
    I += (Processed - 1);
1046
11
    return;
1047
11
  }
1048
244
1049
244
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
244
  if (needsPlt(Expr) && 
!Sym.isInPlt()8
) {
1051
7
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible0
)
1052
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
0
                        Sym);
1054
7
    else
1055
7
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
7
  }
1057
244
1058
244
  // Create a GOT slot if a relocation needs GOT.
1059
244
  if (needsGot(Expr)) {
1060
80
    if (Config->EMachine == EM_MIPS) {
1061
80
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
80
      // require relocation entries for them. A special case is TLS
1063
80
      // relocations. In that case dynamic loader applies dynamic
1064
80
      // relocations to initialize TLS GOT entries.
1065
80
      // See "Global Offset Table" in Chapter 5 in the following document
1066
80
      // for detailed description:
1067
80
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
80
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
80
    } else 
if (0
!Sym.isInGot()0
) {
1070
0
      addGotEntry<ELFT>(Sym);
1071
0
    }
1072
80
  }
1073
244
1074
244
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
244
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*)
Line
Count
Source
975
2.28k
                      RelTy *End) {
976
2.28k
  const RelTy &Rel = *I;
977
2.28k
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
2.28k
  RelType Type;
979
2.28k
980
2.28k
  // Deal with MIPS oddity.
981
2.28k
  if (Config->MipsN32Abi) {
982
0
    Type = getMipsN32RelType(I, End);
983
2.28k
  } else {
984
2.28k
    Type = Rel.getType(Config->IsMips64EL);
985
2.28k
    ++I;
986
2.28k
  }
987
2.28k
988
2.28k
  // Get an offset in an output section this relocation is applied to.
989
2.28k
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
2.28k
  if (Offset == uint64_t(-1))
991
13
    return;
992
2.27k
993
2.27k
  // Skip if the target symbol is an erroneous undefined symbol.
994
2.27k
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
74
    return;
996
2.19k
997
2.19k
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
2.19k
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
2.19k
1000
2.19k
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
2.19k
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
5
    return;
1003
2.19k
1004
2.19k
  // Strenghten or relax relocations.
1005
2.19k
  //
1006
2.19k
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
2.19k
  // are determined by runtime.
1008
2.19k
  //
1009
2.19k
  // On the other hand, if we know that a PLT entry will be resolved within
1010
2.19k
  // the same ELF module, we can skip PLT access and directly jump to the
1011
2.19k
  // destination function. For example, if we are linking a main exectuable,
1012
2.19k
  // all dynamic symbols that can be resolved within the executable will
1013
2.19k
  // actually be resolved that way at runtime, because the main exectuable
1014
2.19k
  // is always at the beginning of a search list. We can leverage that fact.
1015
2.19k
  if (Sym.isGnuIFunc()) {
1016
41
    if (!Config->ZText && 
Config->WarnIfuncTextrel2
) {
1017
1
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
1
           "a binary that will segfault, if the object file is linked with "
1019
1
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
1
           "you, consider recompiling the object files without -fPIC and "
1021
1
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
1
           "turn off this warning." +
1023
1
           getLocation(Sec, Sym, Offset));
1024
1
    }
1025
41
    Expr = toPlt(Expr);
1026
2.15k
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC1.58k
&&
!isAbsoluteValue(Sym)83
) {
1027
47
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
2.10k
  } else if (!Sym.IsPreemptible) {
1029
1.54k
    Expr = fromPlt(Expr);
1030
1.54k
  }
1031
2.19k
1032
2.19k
  // This relocation does not require got entry, but it is relative to got and
1033
2.19k
  // needs it to be created. Here we request for that.
1034
2.19k
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
2.19k
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
107
    In.Got->HasGotOffRel = true;
1037
2.19k
1038
2.19k
  // Read an addend.
1039
2.19k
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
2.19k
1041
2.19k
  // Process some TLS relocations, including relaxing TLS relocations.
1042
2.19k
  // Note that this function does not handle all TLS relocations.
1043
2.19k
  if (unsigned Processed =
1044
173
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
173
    I += (Processed - 1);
1046
173
    return;
1047
173
  }
1048
2.01k
1049
2.01k
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
2.01k
  if (needsPlt(Expr) && 
!Sym.isInPlt()233
) {
1051
208
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible29
)
1052
27
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
27
                        Sym);
1054
181
    else
1055
181
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
208
  }
1057
2.01k
1058
2.01k
  // Create a GOT slot if a relocation needs GOT.
1059
2.01k
  if (needsGot(Expr)) {
1060
180
    if (Config->EMachine == EM_MIPS) {
1061
0
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
0
      // require relocation entries for them. A special case is TLS
1063
0
      // relocations. In that case dynamic loader applies dynamic
1064
0
      // relocations to initialize TLS GOT entries.
1065
0
      // See "Global Offset Table" in Chapter 5 in the following document
1066
0
      // for detailed description:
1067
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
0
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
180
    } else if (!Sym.isInGot()) {
1070
91
      addGotEntry<ELFT>(Sym);
1071
91
    }
1072
180
  }
1073
2.01k
1074
2.01k
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
2.01k
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*)
Line
Count
Source
975
2
                      RelTy *End) {
976
2
  const RelTy &Rel = *I;
977
2
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
2
  RelType Type;
979
2
980
2
  // Deal with MIPS oddity.
981
2
  if (Config->MipsN32Abi) {
982
0
    Type = getMipsN32RelType(I, End);
983
2
  } else {
984
2
    Type = Rel.getType(Config->IsMips64EL);
985
2
    ++I;
986
2
  }
987
2
988
2
  // Get an offset in an output section this relocation is applied to.
989
2
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
2
  if (Offset == uint64_t(-1))
991
0
    return;
992
2
993
2
  // Skip if the target symbol is an erroneous undefined symbol.
994
2
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
0
    return;
996
2
997
2
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
2
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
2
1000
2
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
2
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
0
    return;
1003
2
1004
2
  // Strenghten or relax relocations.
1005
2
  //
1006
2
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
2
  // are determined by runtime.
1008
2
  //
1009
2
  // On the other hand, if we know that a PLT entry will be resolved within
1010
2
  // the same ELF module, we can skip PLT access and directly jump to the
1011
2
  // destination function. For example, if we are linking a main exectuable,
1012
2
  // all dynamic symbols that can be resolved within the executable will
1013
2
  // actually be resolved that way at runtime, because the main exectuable
1014
2
  // is always at the beginning of a search list. We can leverage that fact.
1015
2
  if (Sym.isGnuIFunc()) {
1016
0
    if (!Config->ZText && Config->WarnIfuncTextrel) {
1017
0
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
0
           "a binary that will segfault, if the object file is linked with "
1019
0
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
0
           "you, consider recompiling the object files without -fPIC and "
1021
0
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
0
           "turn off this warning." +
1023
0
           getLocation(Sec, Sym, Offset));
1024
0
    }
1025
0
    Expr = toPlt(Expr);
1026
2
  } else if (!Sym.IsPreemptible && Expr == R_GOT_PC && !isAbsoluteValue(Sym)) {
1027
0
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
2
  } else if (!Sym.IsPreemptible) {
1029
2
    Expr = fromPlt(Expr);
1030
2
  }
1031
2
1032
2
  // This relocation does not require got entry, but it is relative to got and
1033
2
  // needs it to be created. Here we request for that.
1034
2
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
2
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
0
    In.Got->HasGotOffRel = true;
1037
2
1038
2
  // Read an addend.
1039
2
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
2
1041
2
  // Process some TLS relocations, including relaxing TLS relocations.
1042
2
  // Note that this function does not handle all TLS relocations.
1043
2
  if (unsigned Processed =
1044
2
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
2
    I += (Processed - 1);
1046
2
    return;
1047
2
  }
1048
0
1049
0
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
0
  if (needsPlt(Expr) && !Sym.isInPlt()) {
1051
0
    if (Sym.isGnuIFunc() && !Sym.IsPreemptible)
1052
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
0
                        Sym);
1054
0
    else
1055
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
0
  }
1057
0
1058
0
  // Create a GOT slot if a relocation needs GOT.
1059
0
  if (needsGot(Expr)) {
1060
0
    if (Config->EMachine == EM_MIPS) {
1061
0
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
0
      // require relocation entries for them. A special case is TLS
1063
0
      // relocations. In that case dynamic loader applies dynamic
1064
0
      // relocations to initialize TLS GOT entries.
1065
0
      // See "Global Offset Table" in Chapter 5 in the following document
1066
0
      // for detailed description:
1067
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
0
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
0
    } else if (!Sym.isInGot()) {
1070
0
      addGotEntry<ELFT>(Sym);
1071
0
    }
1072
0
  }
1073
0
1074
0
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
0
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*)
Line
Count
Source
975
10.4k
                      RelTy *End) {
976
10.4k
  const RelTy &Rel = *I;
977
10.4k
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
978
10.4k
  RelType Type;
979
10.4k
980
10.4k
  // Deal with MIPS oddity.
981
10.4k
  if (Config->MipsN32Abi) {
982
0
    Type = getMipsN32RelType(I, End);
983
10.4k
  } else {
984
10.4k
    Type = Rel.getType(Config->IsMips64EL);
985
10.4k
    ++I;
986
10.4k
  }
987
10.4k
988
10.4k
  // Get an offset in an output section this relocation is applied to.
989
10.4k
  uint64_t Offset = GetOffset.get(Rel.r_offset);
990
10.4k
  if (Offset == uint64_t(-1))
991
0
    return;
992
10.4k
993
10.4k
  // Skip if the target symbol is an erroneous undefined symbol.
994
10.4k
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
995
0
    return;
996
10.4k
997
10.4k
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
998
10.4k
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
999
10.4k
1000
10.4k
  // Ignore "hint" relocations because they are only markers for relaxation.
1001
10.4k
  if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
1002
0
    return;
1003
10.4k
1004
10.4k
  // Strenghten or relax relocations.
1005
10.4k
  //
1006
10.4k
  // GNU ifunc symbols must be accessed via PLT because their addresses
1007
10.4k
  // are determined by runtime.
1008
10.4k
  //
1009
10.4k
  // On the other hand, if we know that a PLT entry will be resolved within
1010
10.4k
  // the same ELF module, we can skip PLT access and directly jump to the
1011
10.4k
  // destination function. For example, if we are linking a main exectuable,
1012
10.4k
  // all dynamic symbols that can be resolved within the executable will
1013
10.4k
  // actually be resolved that way at runtime, because the main exectuable
1014
10.4k
  // is always at the beginning of a search list. We can leverage that fact.
1015
10.4k
  if (Sym.isGnuIFunc()) {
1016
1
    if (!Config->ZText && 
Config->WarnIfuncTextrel0
) {
1017
0
      warn("using ifunc symbols when text relocations are allowed may produce "
1018
0
           "a binary that will segfault, if the object file is linked with "
1019
0
           "old version of glibc (glibc 2.28 and earlier). If this applies to "
1020
0
           "you, consider recompiling the object files without -fPIC and "
1021
0
           "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1022
0
           "turn off this warning." +
1023
0
           getLocation(Sec, Sym, Offset));
1024
0
    }
1025
1
    Expr = toPlt(Expr);
1026
10.4k
  } else if (!Sym.IsPreemptible && 
Expr == R_GOT_PC10.3k
&&
!isAbsoluteValue(Sym)0
) {
1027
0
    Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1028
10.4k
  } else if (!Sym.IsPreemptible) {
1029
10.3k
    Expr = fromPlt(Expr);
1030
10.3k
  }
1031
10.4k
1032
10.4k
  // This relocation does not require got entry, but it is relative to got and
1033
10.4k
  // needs it to be created. Here we request for that.
1034
10.4k
  if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
1035
10.4k
                     R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
1036
28
    In.Got->HasGotOffRel = true;
1037
10.4k
1038
10.4k
  // Read an addend.
1039
10.4k
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1040
10.4k
1041
10.4k
  // Process some TLS relocations, including relaxing TLS relocations.
1042
10.4k
  // Note that this function does not handle all TLS relocations.
1043
10.4k
  if (unsigned Processed =
1044
79
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1045
79
    I += (Processed - 1);
1046
79
    return;
1047
79
  }
1048
10.3k
1049
10.3k
  // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1050
10.3k
  if (needsPlt(Expr) && 
!Sym.isInPlt()20
) {
1051
17
    if (Sym.isGnuIFunc() && 
!Sym.IsPreemptible1
)
1052
1
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1053
1
                        Sym);
1054
16
    else
1055
16
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1056
17
  }
1057
10.3k
1058
10.3k
  // Create a GOT slot if a relocation needs GOT.
1059
10.3k
  if (needsGot(Expr)) {
1060
10.0k
    if (Config->EMachine == EM_MIPS) {
1061
10.0k
      // MIPS ABI has special rules to process GOT entries and doesn't
1062
10.0k
      // require relocation entries for them. A special case is TLS
1063
10.0k
      // relocations. In that case dynamic loader applies dynamic
1064
10.0k
      // relocations to initialize TLS GOT entries.
1065
10.0k
      // See "Global Offset Table" in Chapter 5 in the following document
1066
10.0k
      // for detailed description:
1067
10.0k
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068
10.0k
      In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1069
10.0k
    } else 
if (16
!Sym.isInGot()16
) {
1070
5
      addGotEntry<ELFT>(Sym);
1071
5
    }
1072
10.0k
  }
1073
10.3k
1074
10.3k
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1075
10.3k
}
Unexecuted instantiation: Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*)
1076
1077
template <class ELFT, class RelTy>
1078
106k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
106k
  OffsetGetter GetOffset(Sec);
1080
106k
1081
106k
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
106k
  Sec.Relocations.reserve(Rels.size());
1083
106k
1084
120k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
13.8k
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
106k
1087
106k
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
106k
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
106k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
Line
Count
Source
1078
6
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
6
  OffsetGetter GetOffset(Sec);
1080
6
1081
6
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
6
  Sec.Relocations.reserve(Rels.size());
1083
6
1084
115
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
109
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
6
1087
6
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
6
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
6
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
1078
5.29k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
5.29k
  OffsetGetter GetOffset(Sec);
1080
5.29k
1081
5.29k
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
5.29k
  Sec.Relocations.reserve(Rels.size());
1083
5.29k
1084
6.12k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
825
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
5.29k
1087
5.29k
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
5.29k
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
5.29k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
Line
Count
Source
1078
13
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
13
  OffsetGetter GetOffset(Sec);
1080
13
1081
13
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
13
  Sec.Relocations.reserve(Rels.size());
1083
13
1084
27
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
14
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
13
1087
13
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
13
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
13
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
1078
2.34k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
2.34k
  OffsetGetter GetOffset(Sec);
1080
2.34k
1081
2.34k
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
2.34k
  Sec.Relocations.reserve(Rels.size());
1083
2.34k
1084
2.60k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
255
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
2.34k
1087
2.34k
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
2.34k
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
2.34k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
1078
1.02k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
1.02k
  OffsetGetter GetOffset(Sec);
1080
1.02k
1081
1.02k
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
1.02k
  Sec.Relocations.reserve(Rels.size());
1083
1.02k
1084
3.30k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
2.28k
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
1.02k
1087
1.02k
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
1.02k
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
1.02k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Line
Count
Source
1078
96.1k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
96.1k
  OffsetGetter GetOffset(Sec);
1080
96.1k
1081
96.1k
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
96.1k
  Sec.Relocations.reserve(Rels.size());
1083
96.1k
1084
96.1k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
2
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
96.1k
1087
96.1k
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
96.1k
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
96.1k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Line
Count
Source
1078
104
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
104
  OffsetGetter GetOffset(Sec);
1080
104
1081
104
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
104
  Sec.Relocations.reserve(Rels.size());
1083
104
1084
10.5k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
10.4k
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
104
1087
104
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
104
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
104
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
Line
Count
Source
1078
1.89k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
1079
1.89k
  OffsetGetter GetOffset(Sec);
1080
1.89k
1081
1.89k
  // Not all relocations end up in Sec.Relocations, but a lot do.
1082
1.89k
  Sec.Relocations.reserve(Rels.size());
1083
1.89k
1084
1.89k
  for (auto I = Rels.begin(), End = Rels.end(); I != End;)
1085
0
    scanReloc<ELFT>(Sec, GetOffset, I, End);
1086
1.89k
1087
1.89k
  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
1088
1.89k
  if (Config->EMachine == EM_RISCV)
1089
0
    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
1090
0
                     RelocationOffsetComparator{});
1091
1.89k
}
1092
1093
106k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
1094
106k
  if (S.AreRelocsRela)
1095
1.14k
    scanRelocs<ELFT>(S, S.relas<ELFT>());
1096
105k
  else
1097
105k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
1098
106k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&)
Line
Count
Source
1093
5.30k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
1094
5.30k
  if (S.AreRelocsRela)
1095
6
    scanRelocs<ELFT>(S, S.relas<ELFT>());
1096
5.29k
  else
1097
5.29k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
1098
5.30k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&)
Line
Count
Source
1093
2.36k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
1094
2.36k
  if (S.AreRelocsRela)
1095
13
    scanRelocs<ELFT>(S, S.relas<ELFT>());
1096
2.34k
  else
1097
2.34k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
1098
2.36k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&)
Line
Count
Source
1093
97.1k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
1094
97.1k
  if (S.AreRelocsRela)
1095
1.02k
    scanRelocs<ELFT>(S, S.relas<ELFT>());
1096
96.1k
  else
1097
96.1k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
1098
97.1k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&)
Line
Count
Source
1093
2.00k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
1094
2.00k
  if (S.AreRelocsRela)
1095
104
    scanRelocs<ELFT>(S, S.relas<ELFT>());
1096
1.89k
  else
1097
1.89k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
1098
2.00k
}
1099
1100
406
static bool mergeCmp(const InputSection *A, const InputSection *B) {
1101
406
  // std::merge requires a strict weak ordering.
1102
406
  if (A->OutSecOff < B->OutSecOff)
1103
10
    return true;
1104
396
1105
396
  if (A->OutSecOff == B->OutSecOff) {
1106
82
    auto *TA = dyn_cast<ThunkSection>(A);
1107
82
    auto *TB = dyn_cast<ThunkSection>(B);
1108
82
1109
82
    // Check if Thunk is immediately before any specific Target
1110
82
    // InputSection for example Mips LA25 Thunks.
1111
82
    if (TA && TA->getTargetInputSection() == B)
1112
26
      return true;
1113
56
1114
56
    // Place Thunk Sections without specific targets before
1115
56
    // non-Thunk Sections.
1116
56
    if (TA && !TB && !TA->getTargetInputSection())
1117
47
      return true;
1118
323
  }
1119
323
1120
323
  return false;
1121
323
}
1122
1123
// Call Fn on every executable InputSection accessed via the linker script
1124
// InputSectionDescription::Sections.
1125
static void forEachInputSectionDescription(
1126
    ArrayRef<OutputSection *> OutputSections,
1127
1.56k
    llvm::function_ref<void(OutputSection *, InputSectionDescription *)> Fn) {
1128
16.4k
  for (OutputSection *OS : OutputSections) {
1129
16.4k
    if (!(OS->Flags & SHF_ALLOC) || 
!(OS->Flags & SHF_EXECINSTR)9.90k
)
1130
14.1k
      continue;
1131
2.33k
    for (BaseCommand *BC : OS->SectionCommands)
1132
2.48k
      if (auto *ISD = dyn_cast<InputSectionDescription>(BC))
1133
2.47k
        Fn(OS, ISD);
1134
2.33k
  }
1135
1.56k
}
1136
1137
// Thunk Implementation
1138
//
1139
// Thunks (sometimes called stubs, veneers or branch islands) are small pieces
1140
// of code that the linker inserts inbetween a caller and a callee. The thunks
1141
// are added at link time rather than compile time as the decision on whether
1142
// a thunk is needed, such as the caller and callee being out of range, can only
1143
// be made at link time.
1144
//
1145
// It is straightforward to tell given the current state of the program when a
1146
// thunk is needed for a particular call. The more difficult part is that
1147
// the thunk needs to be placed in the program such that the caller can reach
1148
// the thunk and the thunk can reach the callee; furthermore, adding thunks to
1149
// the program alters addresses, which can mean more thunks etc.
1150
//
1151
// In lld we have a synthetic ThunkSection that can hold many Thunks.
1152
// The decision to have a ThunkSection act as a container means that we can
1153
// more easily handle the most common case of a single block of contiguous
1154
// Thunks by inserting just a single ThunkSection.
1155
//
1156
// The implementation of Thunks in lld is split across these areas
1157
// Relocations.cpp : Framework for creating and placing thunks
1158
// Thunks.cpp : The code generated for each supported thunk
1159
// Target.cpp : Target specific hooks that the framework uses to decide when
1160
//              a thunk is used
1161
// Synthetic.cpp : Implementation of ThunkSection
1162
// Writer.cpp : Iteratively call framework until no more Thunks added
1163
//
1164
// Thunk placement requirements:
1165
// Mips LA25 thunks. These must be placed immediately before the callee section
1166
// We can assume that the caller is in range of the Thunk. These are modelled
1167
// by Thunks that return the section they must precede with
1168
// getTargetInputSection().
1169
//
1170
// ARM interworking and range extension thunks. These thunks must be placed
1171
// within range of the caller. All implemented ARM thunks can always reach the
1172
// callee as they use an indirect jump via a register that has no range
1173
// restrictions.
1174
//
1175
// Thunk placement algorithm:
1176
// For Mips LA25 ThunkSections; the placement is explicit, it has to be before
1177
// getTargetInputSection().
1178
//
1179
// For thunks that must be placed within range of the caller there are many
1180
// possible choices given that the maximum range from the caller is usually
1181
// much larger than the average InputSection size. Desirable properties include:
1182
// - Maximize reuse of thunks by multiple callers
1183
// - Minimize number of ThunkSections to simplify insertion
1184
// - Handle impact of already added Thunks on addresses
1185
// - Simple to understand and implement
1186
//
1187
// In lld for the first pass, we pre-create one or more ThunkSections per
1188
// InputSectionDescription at Target specific intervals. A ThunkSection is
1189
// placed so that the estimated end of the ThunkSection is within range of the
1190
// start of the InputSectionDescription or the previous ThunkSection. For
1191
// example:
1192
// InputSectionDescription
1193
// Section 0
1194
// ...
1195
// Section N
1196
// ThunkSection 0
1197
// Section N + 1
1198
// ...
1199
// Section N + K
1200
// Thunk Section 1
1201
//
1202
// The intention is that we can add a Thunk to a ThunkSection that is well
1203
// spaced enough to service a number of callers without having to do a lot
1204
// of work. An important principle is that it is not an error if a Thunk cannot
1205
// be placed in a pre-created ThunkSection; when this happens we create a new
1206
// ThunkSection placed next to the caller. This allows us to handle the vast
1207
// majority of thunks simply, but also handle rare cases where the branch range
1208
// is smaller than the target specific spacing.
1209
//
1210
// The algorithm is expected to create all the thunks that are needed in a
1211
// single pass, with a small number of programs needing a second pass due to
1212
// the insertion of thunks in the first pass increasing the offset between
1213
// callers and callees that were only just in range.
1214
//
1215
// A consequence of allowing new ThunkSections to be created outside of the
1216
// pre-created ThunkSections is that in rare cases calls to Thunks that were in
1217
// range in pass K, are out of range in some pass > K due to the insertion of
1218
// more Thunks in between the caller and callee. When this happens we retarget
1219
// the relocation back to the original target and create another Thunk.
1220
1221
// Remove ThunkSections that are empty, this should only be the initial set
1222
// precreated on pass 0.
1223
1224
// Insert the Thunks for OutputSection OS into their designated place
1225
// in the Sections vector, and recalculate the InputSection output section
1226
// offsets.
1227
// This may invalidate any output section offsets stored outside of InputSection
1228
664
void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> OutputSections) {
1229
664
  forEachInputSectionDescription(
1230
1.03k
      OutputSections, [&](OutputSection *OS, InputSectionDescription *ISD) {
1231
1.03k
        if (ISD->ThunkSections.empty())
1232
485
          return;
1233
550
1234
550
        // Remove any zero sized precreated Thunks.
1235
550
        llvm::erase_if(ISD->ThunkSections,
1236
608
                       [](const std::pair<ThunkSection *, uint32_t> &TS) {
1237
608
                         return TS.first->getSize() == 0;
1238
608
                       });
1239
550
1240
550
        // ISD->ThunkSections contains all created ThunkSections, including
1241
550
        // those inserted in previous passes. Extract the Thunks created this
1242
550
        // pass and order them in ascending OutSecOff.
1243
550
        std::vector<ThunkSection *> NewThunks;
1244
550
        for (const std::pair<ThunkSection *, uint32_t> TS : ISD->ThunkSections)
1245
253
          if (TS.second == Pass)
1246
124
            NewThunks.push_back(TS.first);
1247
550
        std::stable_sort(NewThunks.begin(), NewThunks.end(),
1248
550
                         [](const ThunkSection *A, const ThunkSection *B) {
1249
21
                           return A->OutSecOff < B->OutSecOff;
1250
21
                         });
1251
550
1252
550
        // Merge sorted vectors of Thunks and InputSections by OutSecOff
1253
550
        std::vector<InputSection *> Tmp;
1254
550
        Tmp.reserve(ISD->Sections.size() + NewThunks.size());
1255
550
1256
550
        std::merge(ISD->Sections.begin(), ISD->Sections.end(),
1257
550
                   NewThunks.begin(), NewThunks.end(), std::back_inserter(Tmp),
1258
550
                   mergeCmp);
1259
550
1260
550
        ISD->Sections = std::move(Tmp);
1261
550
      });
1262
664
}
1263
1264
// Find or create a ThunkSection within the InputSectionDescription (ISD) that
1265
// is in range of Src. An ISD maps to a range of InputSections described by a
1266
// linker script section pattern such as { .text .text.* }.
1267
ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *OS, InputSection *IS,
1268
                                           InputSectionDescription *ISD,
1269
150
                                           uint32_t Type, uint64_t Src) {
1270
150
  for (std::pair<ThunkSection *, uint32_t> TP : ISD->ThunkSections) {
1271
135
    ThunkSection *TS = TP.first;
1272
135
    uint64_t TSBase = OS->Addr + TS->OutSecOff;
1273
135
    uint64_t TSLimit = TSBase + TS->getSize();
1274
135
    if (Target->inBranchRange(Type, Src, (Src > TSLimit) ? 
TSBase37
:
TSLimit98
))
1275
113
      return TS;
1276
135
  }
1277
150
1278
150
  // No suitable ThunkSection exists. This can happen when there is a branch
1279
150
  // with lower range than the ThunkSection spacing or when there are too
1280
150
  // many Thunks. Create a new ThunkSection as close to the InputSection as
1281
150
  // possible. Error if InputSection is so large we cannot place ThunkSection
1282
150
  // anywhere in Range.
1283
150
  uint64_t ThunkSecOff = IS->OutSecOff;
1284
37
  if (!Target->inBranchRange(Type, Src, OS->Addr + ThunkSecOff)) {
1285
4
    ThunkSecOff = IS->OutSecOff + IS->getSize();
1286
4
    if (!Target->inBranchRange(Type, Src, OS->Addr + ThunkSecOff))
1287
0
      fatal("InputSection too large for range extension thunk " +
1288
0
            IS->getObjMsg(Src - (OS->Addr + IS->OutSecOff)));
1289
37
  }
1290
37
  return addThunkSection(OS, ISD, ThunkSecOff);
1291
37
}
1292
1293
// Add a Thunk that needs to be placed in a ThunkSection that immediately
1294
// precedes its Target.
1295
33
ThunkSection *ThunkCreator::getISThunkSec(InputSection *IS) {
1296
33
  ThunkSection *TS = ThunkedSections.lookup(IS);
1297
33
  if (TS)
1298
7
    return TS;
1299
26
1300
26
  // Find InputSectionRange within Target Output Section (TOS) that the
1301
26
  // InputSection (IS) that we need to precede is in.
1302
26
  OutputSection *TOS = IS->getParent();
1303
31
  for (BaseCommand *BC : TOS->SectionCommands) {
1304
31
    auto *ISD = dyn_cast<InputSectionDescription>(BC);
1305
31
    if (!ISD || 
ISD->Sections.empty()29
)
1306
3
      continue;
1307
28
1308
28
    InputSection *First = ISD->Sections.front();
1309
28
    InputSection *Last = ISD->Sections.back();
1310
28
1311
28
    if (IS->OutSecOff < First->OutSecOff || Last->OutSecOff < IS->OutSecOff)
1312
2
      continue;
1313
26
1314
26
    TS = addThunkSection(TOS, ISD, IS->OutSecOff);
1315
26
    ThunkedSections[IS] = TS;
1316
26
    return TS;
1317
26
  }
1318
26
1319
26
  
return nullptr0
;
1320
26
}
1321
1322
// Create one or more ThunkSections per OS that can be used to place Thunks.
1323
// We attempt to place the ThunkSections using the following desirable
1324
// properties:
1325
// - Within range of the maximum number of callers
1326
// - Minimise the number of ThunkSections
1327
//
1328
// We follow a simple but conservative heuristic to place ThunkSections at
1329
// offsets that are multiples of a Target specific branch range.
1330
// For an InputSectionDescription that is smaller than the range, a single
1331
// ThunkSection at the end of the range will do.
1332
//
1333
// For an InputSectionDescription that is more than twice the size of the range,
1334
// we place the last ThunkSection at range bytes from the end of the
1335
// InputSectionDescription in order to increase the likelihood that the
1336
// distance from a thunk to its target will be sufficiently small to
1337
// allow for the creation of a short thunk.
1338
void ThunkCreator::createInitialThunkSections(
1339
238
    ArrayRef<OutputSection *> OutputSections) {
1340
238
  uint32_t ThunkSectionSpacing = Target->getThunkSectionSpacing();
1341
238
1342
238
  forEachInputSectionDescription(
1343
403
      OutputSections, [&](OutputSection *OS, InputSectionDescription *ISD) {
1344
403
        if (ISD->Sections.empty())
1345
7
          return;
1346
396
1347
396
        uint32_t ISDBegin = ISD->Sections.front()->OutSecOff;
1348
396
        uint32_t ISDEnd =
1349
396
            ISD->Sections.back()->OutSecOff + ISD->Sections.back()->getSize();
1350
396
        uint32_t LastThunkLowerBound = -1;
1351
396
        if (ISDEnd - ISDBegin > ThunkSectionSpacing * 2)
1352
5
          LastThunkLowerBound = ISDEnd - ThunkSectionSpacing;
1353
396
1354
396
        uint32_t ISLimit;
1355
396
        uint32_t PrevISLimit = ISDBegin;
1356
396
        uint32_t ThunkUpperBound = ISDBegin + ThunkSectionSpacing;
1357
396
1358
860
        for (const InputSection *IS : ISD->Sections) {
1359
860
          ISLimit = IS->OutSecOff + IS->getSize();
1360
860
          if (ISLimit > ThunkUpperBound) {
1361
20
            addThunkSection(OS, ISD, PrevISLimit);
1362
20
            ThunkUpperBound = PrevISLimit + ThunkSectionSpacing;
1363
20
          }
1364
860
          if (ISLimit > LastThunkLowerBound)
1365
5
            break;
1366
855
          PrevISLimit = ISLimit;
1367
855
        }
1368
396
        addThunkSection(OS, ISD, ISLimit);
1369
396
      });
1370
238
}
1371
1372
ThunkSection *ThunkCreator::addThunkSection(OutputSection *OS,
1373
                                            InputSectionDescription *ISD,
1374
479
                                            uint64_t Off) {
1375
479
  auto *TS = make<ThunkSection>(OS, Off);
1376
479
  ISD->ThunkSections.push_back({TS, Pass});
1377
479
  return TS;
1378
479
}
1379
1380
std::pair<Thunk *, bool> ThunkCreator::getThunk(Symbol &Sym, RelType Type,
1381
230
                                                uint64_t Src) {
1382
230
  std::vector<Thunk *> *ThunkVec = nullptr;
1383
230
1384
230
  // We use (section, offset) pair to find the thunk position if possible so
1385
230
  // that we create only one thunk for aliased symbols or ICFed sections.
1386
230
  if (auto *D = dyn_cast<Defined>(&Sym))
1387
188
    if (!D->isInPlt() && 
D->Section173
)
1388
156
      ThunkVec = &ThunkedSymbolsBySection[{D->Section->Repl, D->Value}];
1389
230
  if (!ThunkVec)
1390
74
    ThunkVec = &ThunkedSymbols[&Sym];
1391
230
1392
230
  // Check existing Thunks for Sym to see if they can be reused
1393
230
  for (Thunk *T : *ThunkVec)
1394
55
    if (T->isCompatibleWith(Type) &&
1395
55
        Target->inBranchRange(Type, Src, T->getThunkTargetSym()->getVA()))
1396
47
      return std::make_pair(T, false);
1397
230
1398
230
  // No existing compatible Thunk in range, create a new one
1399
230
  Thunk *T = addThunk(Type, Sym);
1400
183
  ThunkVec->push_back(T);
1401
183
  return std::make_pair(T, true);
1402
230
}
1403
1404
// Return true if the relocation target is an in range Thunk.
1405
// Return false if the relocation is not to a Thunk. If the relocation target
1406
// was originally to a Thunk, but is no longer in range we revert the
1407
// relocation back to its original non-Thunk target.
1408
609
bool ThunkCreator::normalizeExistingThunk(Relocation &Rel, uint64_t Src) {
1409
609
  if (Thunk *T = Thunks.lookup(Rel.Sym)) {
1410
236
    if (Target->inBranchRange(Rel.Type, Src, Rel.Sym->getVA()))
1411
234
      return true;
1412
2
    Rel.Sym = &T->Destination;
1413
2
    if (Rel.Sym->isInPlt())
1414
1
      Rel.Expr = toPlt(Rel.Expr);
1415
2
  }
1416
609
  
return false375
;
1417
609
}
1418
1419
// Process all relocations from the InputSections that have been assigned
1420
// to InputSectionDescriptions and redirect through Thunks if needed. The
1421
// function should be called iteratively until it returns false.
1422
//
1423
// PreConditions:
1424
// All InputSections that may need a Thunk are reachable from
1425
// OutputSectionCommands.
1426
//
1427
// All OutputSections have an address and all InputSections have an offset
1428
// within the OutputSection.
1429
//
1430
// The offsets between caller (relocation place) and callee
1431
// (relocation target) will not be modified outside of createThunks().
1432
//
1433
// PostConditions:
1434
// If return value is true then ThunkSections have been inserted into
1435
// OutputSections. All relocations that needed a Thunk based on the information
1436
// available to createThunks() on entry have been redirected to a Thunk. Note
1437
// that adding Thunks changes offsets between caller and callee so more Thunks
1438
// may be required.
1439
//
1440
// If return value is false then no more Thunks are needed, and createThunks has
1441
// made no changes. If the target requires range extension thunks, currently
1442
// ARM, then any future change in offset between caller and callee risks a
1443
// relocation out of range error.
1444
664
bool ThunkCreator::createThunks(ArrayRef<OutputSection *> OutputSections) {
1445
664
  bool AddressesChanged = false;
1446
664
1447
664
  if (Pass == 0 && 
Target->getThunkSectionSpacing()560
)
1448
238
    createInitialThunkSections(OutputSections);
1449
664
1450
664
  // With Thunk Size much smaller than branch range we expect to
1451
664
  // converge quickly; if we get to 10 something has gone wrong.
1452
664
  if (Pass == 10)
1453
0
    fatal("thunk creation not converged");
1454
664
1455
664
  // Create all the Thunks and insert them into synthetic ThunkSections. The
1456
664
  // ThunkSections are later inserted back into InputSectionDescriptions.
1457
664
  // We separate the creation of ThunkSections from the insertion of the
1458
664
  // ThunkSections as ThunkSections are not always inserted into the same
1459
664
  // InputSectionDescription as the caller.
1460
664
  forEachInputSectionDescription(
1461
1.03k
      OutputSections, [&](OutputSection *OS, InputSectionDescription *ISD) {
1462
1.03k
        for (InputSection *IS : ISD->Sections)
1463
12.4k
          
for (Relocation &Rel : IS->Relocations)2.25k
{
1464
12.4k
            uint64_t Src = IS->getVA(Rel.Offset);
1465
12.4k
1466
12.4k
            // If we are a relocation to an existing Thunk, check if it is
1467
12.4k
            // still in range. If not then Rel will be altered to point to its
1468
12.4k
            // original target so another Thunk can be generated.
1469
12.4k
            if (Pass > 0 && 
normalizeExistingThunk(Rel, Src)609
)
1470
234
              continue;
1471
12.1k
1472
12.1k
            if (!Target->needsThunk(Rel.Expr, Rel.Type, IS->File, Src,
1473
12.1k
                                    *Rel.Sym))
1474
11.9k
              continue;
1475
230
1476
230
            Thunk *T;
1477
230
            bool IsNew;
1478
230
            std::tie(T, IsNew) = getThunk(*Rel.Sym, Rel.Type, Src);
1479
230
1480
230
            if (IsNew) {
1481
183
              // Find or create a ThunkSection for the new Thunk
1482
183
              ThunkSection *TS;
1483
183
              if (auto *TIS = T->getTargetInputSection())
1484
33
                TS = getISThunkSec(TIS);
1485
150
              else
1486
150
                TS = getISDThunkSec(OS, IS, ISD, Rel.Type, Src);
1487
183
              TS->addThunk(T);
1488
183
              Thunks[T->getThunkTargetSym()] = T;
1489
183
            }
1490
230
1491
230
            // Redirect relocation to Thunk, we never go via the PLT to a Thunk
1492
230
            Rel.Sym = T->getThunkTargetSym();
1493
230
            Rel.Expr = fromPlt(Rel.Expr);
1494
230
          }
1495
1.03k
1496
1.03k
        for (auto &P : ISD->ThunkSections)
1497
600
          AddressesChanged |= P.first->assignOffsets();
1498
1.03k
      });
1499
664
1500
664
  for (auto &P : ThunkedSections)
1501
52
    AddressesChanged |= P.second->assignOffsets();
1502
664
1503
664
  // Merge all created synthetic ThunkSections back into OutputSection
1504
664
  mergeThunks(OutputSections);
1505
664
  ++Pass;
1506
664
  return AddressesChanged;
1507
664
}
1508
1509
template void elf::scanRelocations<ELF32LE>(InputSectionBase &);
1510
template void elf::scanRelocations<ELF32BE>(InputSectionBase &);
1511
template void elf::scanRelocations<ELF64LE>(InputSectionBase &);
1512
template void elf::scanRelocations<ELF64BE>(InputSectionBase &);