Coverage Report

Created: 2018-01-17 21:32

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