Coverage Report

Created: 2018-10-20 06:24

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