Coverage Report

Created: 2018-06-25 02:00

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