Coverage Report

Created: 2018-08-19 21:11

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