Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Memory.h"
48
#include "OutputSections.h"
49
#include "Strings.h"
50
#include "SymbolTable.h"
51
#include "SyntheticSections.h"
52
#include "Target.h"
53
#include "Thunks.h"
54
55
#include "llvm/Support/Endian.h"
56
#include "llvm/Support/raw_ostream.h"
57
#include <algorithm>
58
59
using namespace llvm;
60
using namespace llvm::ELF;
61
using namespace llvm::object;
62
using namespace llvm::support::endian;
63
64
using namespace lld;
65
using namespace lld::elf;
66
67
// Construct a message in the following format.
68
//
69
// >>> defined in /home/alice/src/foo.o
70
// >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
71
// >>>               /home/alice/src/bar.o:(.text+0x1)
72
template <class ELFT>
73
static std::string getLocation(InputSectionBase &S, const SymbolBody &Sym,
74
38
                               uint64_t Off) {
75
38
  std::string Msg =
76
38
      "\n>>> defined in " + toString(Sym.getFile()) + "\n>>> referenced by ";
77
38
  std::string Src = S.getSrcMsg<ELFT>(Off);
78
38
  if (!Src.empty())
79
5
    Msg += Src + "\n>>>               ";
80
38
  return Msg + S.getObjMsg<ELFT>(Off);
81
38
}
Unexecuted instantiation: Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getLocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&, lld::elf::SymbolBody const&, unsigned long long)
Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getLocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&, lld::elf::SymbolBody const&, unsigned long long)
Line
Count
Source
74
5
                               uint64_t Off) {
75
5
  std::string Msg =
76
5
      "\n>>> defined in " + toString(Sym.getFile()) + "\n>>> referenced by ";
77
5
  std::string Src = S.getSrcMsg<ELFT>(Off);
78
5
  if (!Src.empty())
79
0
    Msg += Src + "\n>>>               ";
80
5
  return Msg + S.getObjMsg<ELFT>(Off);
81
5
}
Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getLocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&, lld::elf::SymbolBody const&, unsigned long long)
Line
Count
Source
74
33
                               uint64_t Off) {
75
33
  std::string Msg =
76
33
      "\n>>> defined in " + toString(Sym.getFile()) + "\n>>> referenced by ";
77
33
  std::string Src = S.getSrcMsg<ELFT>(Off);
78
33
  if (!Src.empty())
79
5
    Msg += Src + "\n>>>               ";
80
33
  return Msg + S.getObjMsg<ELFT>(Off);
81
33
}
Unexecuted instantiation: Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > getLocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&, lld::elf::SymbolBody const&, unsigned long long)
82
83
7.46k
static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
84
7.46k
  // In case of MIPS GP-relative relocations always resolve to a definition
85
7.46k
  // in a regular input file, ignoring the one-definition rule. So we,
86
7.46k
  // for example, should not attempt to create a dynamic relocation even
87
7.46k
  // if the target symbol is preemptible. There are two two MIPS GP-relative
88
7.46k
  // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
89
7.46k
  // can be against a preemptible symbol.
90
7.46k
  // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
91
7.46k
  // relocation types occupy eight bit. In case of N64 ABI we extract first
92
7.46k
  // relocation from 3-in-1 packet because only the first relocation can
93
7.46k
  // be against a real symbol.
94
7.46k
  if (
Config->EMachine == EM_MIPS7.46k
) {
95
1.04k
    Type &= 0xff;
96
1.04k
    if (
Type == R_MIPS_GPREL16 || 1.04k
Type == R_MICROMIPS_GPREL16990
||
97
990
        Type == R_MICROMIPS_GPREL7_S2)
98
53
      return false;
99
7.41k
  }
100
7.41k
  return Body.isPreemptible();
101
7.41k
}
102
103
// This function is similar to the `handleTlsRelocation`. MIPS does not
104
// support any relaxations for TLS relocations so by factoring out MIPS
105
// handling in to the separate function we can simplify the code and do not
106
// pollute other `handleTlsRelocation` by MIPS `ifs` statements.
107
// Mips has a custom MipsGotSection that handles the writing of GOT entries
108
// without dynamic relocations.
109
template <class ELFT>
110
static unsigned handleMipsTlsRelocation(uint32_t Type, SymbolBody &Body,
111
                                        InputSectionBase &C, uint64_t Offset,
112
35
                                        int64_t Addend, RelExpr Expr) {
113
35
  if (
Expr == R_MIPS_TLSLD35
) {
114
5
    if (
InX::MipsGot->addTlsIndex() && 5
Config->Pic5
)
115
2
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::MipsGot,
116
2
                                   InX::MipsGot->getTlsIndexOff(), false,
117
2
                                   nullptr, 0});
118
5
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
119
5
    return 1;
120
5
  }
121
30
122
30
  
if (30
Expr == R_MIPS_TLSGD30
) {
123
9
    if (
InX::MipsGot->addDynTlsEntry(Body) && 9
Body.isPreemptible()9
) {
124
6
      uint64_t Off = InX::MipsGot->getGlobalDynOffset(Body);
125
6
      In<ELFT>::RelaDyn->addReloc(
126
6
          {Target->TlsModuleIndexRel, InX::MipsGot, Off, false, &Body, 0});
127
6
      if (Body.isPreemptible())
128
6
        In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, InX::MipsGot,
129
6
                                     Off + Config->Wordsize, false, &Body, 0});
130
6
    }
131
9
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
132
9
    return 1;
133
9
  }
134
21
  return 0;
135
21
}
Unexecuted instantiation: Relocations.cpp:unsigned int handleMipsTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Relocations.cpp:unsigned int handleMipsTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Line
Count
Source
112
12
                                        int64_t Addend, RelExpr Expr) {
113
12
  if (
Expr == R_MIPS_TLSLD12
) {
114
2
    if (
InX::MipsGot->addTlsIndex() && 2
Config->Pic2
)
115
1
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::MipsGot,
116
1
                                   InX::MipsGot->getTlsIndexOff(), false,
117
1
                                   nullptr, 0});
118
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
119
2
    return 1;
120
2
  }
121
10
122
10
  
if (10
Expr == R_MIPS_TLSGD10
) {
123
4
    if (
InX::MipsGot->addDynTlsEntry(Body) && 4
Body.isPreemptible()4
) {
124
3
      uint64_t Off = InX::MipsGot->getGlobalDynOffset(Body);
125
3
      In<ELFT>::RelaDyn->addReloc(
126
3
          {Target->TlsModuleIndexRel, InX::MipsGot, Off, false, &Body, 0});
127
3
      if (Body.isPreemptible())
128
3
        In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, InX::MipsGot,
129
3
                                     Off + Config->Wordsize, false, &Body, 0});
130
3
    }
131
4
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
132
4
    return 1;
133
4
  }
134
6
  return 0;
135
6
}
Unexecuted instantiation: Relocations.cpp:unsigned int handleMipsTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::SymbolBody&, 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::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Line
Count
Source
112
23
                                        int64_t Addend, RelExpr Expr) {
113
23
  if (
Expr == R_MIPS_TLSLD23
) {
114
3
    if (
InX::MipsGot->addTlsIndex() && 3
Config->Pic3
)
115
1
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::MipsGot,
116
1
                                   InX::MipsGot->getTlsIndexOff(), false,
117
1
                                   nullptr, 0});
118
3
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
119
3
    return 1;
120
3
  }
121
20
122
20
  
if (20
Expr == R_MIPS_TLSGD20
) {
123
5
    if (
InX::MipsGot->addDynTlsEntry(Body) && 5
Body.isPreemptible()5
) {
124
3
      uint64_t Off = InX::MipsGot->getGlobalDynOffset(Body);
125
3
      In<ELFT>::RelaDyn->addReloc(
126
3
          {Target->TlsModuleIndexRel, InX::MipsGot, Off, false, &Body, 0});
127
3
      if (Body.isPreemptible())
128
3
        In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, InX::MipsGot,
129
3
                                     Off + Config->Wordsize, false, &Body, 0});
130
3
    }
131
5
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
132
5
    return 1;
133
5
  }
134
15
  return 0;
135
15
}
136
137
// This function is similar to the `handleMipsTlsRelocation`. ARM also does not
138
// support any relaxations for TLS relocations. ARM is logically similar to Mips
139
// in how it handles TLS, but Mips uses its own custom GOT which handles some
140
// of the cases that ARM uses GOT relocations for.
141
//
142
// We look for TLS global dynamic and local dynamic relocations, these may
143
// require the generation of a pair of GOT entries that have associated
144
// dynamic relocations. When the results of the dynamic relocations can be
145
// resolved at static link time we do so. This is necessary for static linking
146
// as there will be no dynamic loader to resolve them at load-time.
147
//
148
// The pair of GOT entries created are of the form
149
// GOT[e0] Module Index (Used to find pointer to TLS block at run-time)
150
// GOT[e1] Offset of symbol in TLS block
151
template <class ELFT>
152
static unsigned handleARMTlsRelocation(uint32_t Type, SymbolBody &Body,
153
                                       InputSectionBase &C, uint64_t Offset,
154
27
                                       int64_t Addend, RelExpr Expr) {
155
27
  // The Dynamic TLS Module Index Relocation for a symbol defined in an
156
27
  // executable is always 1. If the target Symbol is not preemptible then
157
27
  // we know the offset into the TLS block at static link time.
158
20
  bool NeedDynId = Body.isPreemptible() || Config->Shared;
159
27
  bool NeedDynOff = Body.isPreemptible();
160
27
161
27
  auto AddTlsReloc = [&](uint64_t Off, uint32_t Type, SymbolBody *Dest,
162
28
                         bool Dyn) {
163
28
    if (Dyn)
164
14
      In<ELFT>::RelaDyn->addReloc({Type, InX::Got, Off, false, Dest, 0});
165
28
    else
166
14
      InX::Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
167
28
  };
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool)::operator()(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool) const
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool)::operator()(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool) const
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool)::operator()(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool) const
Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)::'lambda'(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool)::operator()(unsigned long long, unsigned int, lld::elf::SymbolBody*, bool) const
Line
Count
Source
162
28
                         bool Dyn) {
163
28
    if (Dyn)
164
14
      In<ELFT>::RelaDyn->addReloc({Type, InX::Got, Off, false, Dest, 0});
165
28
    else
166
14
      InX::Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
167
28
  };
168
27
169
27
  // Local Dynamic is for access to module local TLS variables, while still
170
27
  // being suitable for being dynamically loaded via dlopen.
171
27
  // GOT[e0] is the module index, with a special value of 0 for the current
172
27
  // module. GOT[e1] is unused. There only needs to be one module index entry.
173
27
  if (
Expr == R_TLSLD_PC && 27
InX::Got->addTlsIndex()2
) {
174
2
    AddTlsReloc(InX::Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
175
2
                NeedDynId ? 
nullptr1
:
&Body1
, NeedDynId);
176
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
177
2
    return 1;
178
2
  }
179
25
180
25
  // Global Dynamic is the most general purpose access model. When we know
181
25
  // the module index and offset of symbol in TLS block we can fill these in
182
25
  // using static GOT relocations.
183
25
  
if (25
Expr == R_TLSGD_PC25
) {
184
13
    if (
InX::Got->addDynTlsEntry(Body)13
) {
185
13
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
186
13
      AddTlsReloc(Off, Target->TlsModuleIndexRel, &Body, NeedDynId);
187
13
      AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Body,
188
13
                  NeedDynOff);
189
13
    }
190
13
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
191
13
    return 1;
192
13
  }
193
12
  return 0;
194
12
}
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::SymbolBody&, 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::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
Line
Count
Source
154
27
                                       int64_t Addend, RelExpr Expr) {
155
27
  // The Dynamic TLS Module Index Relocation for a symbol defined in an
156
27
  // executable is always 1. If the target Symbol is not preemptible then
157
27
  // we know the offset into the TLS block at static link time.
158
20
  bool NeedDynId = Body.isPreemptible() || Config->Shared;
159
27
  bool NeedDynOff = Body.isPreemptible();
160
27
161
27
  auto AddTlsReloc = [&](uint64_t Off, uint32_t Type, SymbolBody *Dest,
162
27
                         bool Dyn) {
163
27
    if (Dyn)
164
27
      In<ELFT>::RelaDyn->addReloc({Type, InX::Got, Off, false, Dest, 0});
165
27
    else
166
27
      InX::Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
167
27
  };
168
27
169
27
  // Local Dynamic is for access to module local TLS variables, while still
170
27
  // being suitable for being dynamically loaded via dlopen.
171
27
  // GOT[e0] is the module index, with a special value of 0 for the current
172
27
  // module. GOT[e1] is unused. There only needs to be one module index entry.
173
27
  if (
Expr == R_TLSLD_PC && 27
InX::Got->addTlsIndex()2
) {
174
2
    AddTlsReloc(InX::Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
175
2
                NeedDynId ? 
nullptr1
:
&Body1
, NeedDynId);
176
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
177
2
    return 1;
178
2
  }
179
25
180
25
  // Global Dynamic is the most general purpose access model. When we know
181
25
  // the module index and offset of symbol in TLS block we can fill these in
182
25
  // using static GOT relocations.
183
25
  
if (25
Expr == R_TLSGD_PC25
) {
184
13
    if (
InX::Got->addDynTlsEntry(Body)13
) {
185
13
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
186
13
      AddTlsReloc(Off, Target->TlsModuleIndexRel, &Body, NeedDynId);
187
13
      AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Body,
188
13
                  NeedDynOff);
189
13
    }
190
13
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
191
13
    return 1;
192
13
  }
193
12
  return 0;
194
12
}
Unexecuted instantiation: Relocations.cpp:unsigned int handleARMTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long, long long, lld::elf::RelExpr)
195
196
// Returns the number of relocations processed.
197
template <class ELFT>
198
static unsigned
199
handleTlsRelocation(uint32_t Type, SymbolBody &Body, InputSectionBase &C,
200
1.86k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
1.86k
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
1.86k
204
1.86k
  
if (1.86k
!Body.isTls()1.86k
)
205
1.64k
    return 0;
206
220
207
220
  
if (220
Config->EMachine == EM_ARM220
)
208
27
    return handleARMTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
209
193
  
if (193
Config->EMachine == EM_MIPS193
)
210
35
    return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
211
158
212
158
  bool IsPreemptible = isPreemptible(Body, Type);
213
158
  if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
214
158
      
Config->Shared24
) {
215
16
    if (
InX::Got->addDynTlsEntry(Body)16
) {
216
4
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
217
4
      In<ELFT>::RelaDyn->addReloc(
218
4
          {Target->TlsDescRel, InX::Got, Off, !IsPreemptible, &Body, 0});
219
4
    }
220
16
    if (Expr != R_TLSDESC_CALL)
221
12
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
222
16
    return 1;
223
16
  }
224
142
225
142
  
if (142
isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)142
) {
226
9
    // Local-Dynamic relocs can be relaxed to Local-Exec.
227
9
    if (
!Config->Shared9
) {
228
5
      C.Relocations.push_back(
229
5
          {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
230
5
      return 2;
231
5
    }
232
4
    
if (4
InX::Got->addTlsIndex()4
)
233
2
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::Got,
234
2
                                   InX::Got->getTlsIndexOff(), false, nullptr,
235
2
                                   0});
236
9
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
237
9
    return 1;
238
9
  }
239
133
240
133
  // Local-Dynamic relocs can be relaxed to Local-Exec.
241
133
  
if (133
isRelExprOneOf<R_ABS, R_TLSLD, R_TLSLD_PC>(Expr) && 133
!Config->Shared16
) {
242
7
    C.Relocations.push_back(
243
7
        {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
244
7
    return 1;
245
7
  }
246
126
247
126
  
if (126
isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD,
248
126
                     R_TLSGD_PC>(Expr)) {
249
27
    if (
Config->Shared27
) {
250
7
      if (
InX::Got->addDynTlsEntry(Body)7
) {
251
6
        uint64_t Off = InX::Got->getGlobalDynOffset(Body);
252
6
        In<ELFT>::RelaDyn->addReloc(
253
6
            {Target->TlsModuleIndexRel, InX::Got, Off, false, &Body, 0});
254
6
255
6
        // If the symbol is preemptible we need the dynamic linker to write
256
6
        // the offset too.
257
6
        uint64_t OffsetOff = Off + Config->Wordsize;
258
6
        if (IsPreemptible)
259
4
          In<ELFT>::RelaDyn->addReloc(
260
4
              {Target->TlsOffsetRel, InX::Got, OffsetOff, false, &Body, 0});
261
6
        else
262
2
          InX::Got->Relocations.push_back(
263
2
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
264
6
      }
265
7
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
266
7
      return 1;
267
7
    }
268
20
269
20
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
270
20
    // depending on the symbol being locally defined or not.
271
20
    
if (20
IsPreemptible20
) {
272
10
      C.Relocations.push_back(
273
10
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
274
10
           Offset, Addend, &Body});
275
10
      if (
!Body.isInGot()10
) {
276
6
        InX::Got->addEntry(Body);
277
6
        In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::Got,
278
6
                                     Body.getGotOffset(), false, &Body, 0});
279
6
      }
280
20
    } else {
281
10
      C.Relocations.push_back(
282
10
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
283
10
           Offset, Addend, &Body});
284
10
    }
285
27
    return Target->TlsGdRelaxSkip;
286
27
  }
287
99
288
99
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
289
99
  // defined.
290
99
  
if (99
isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_GOT_PAGE_PC>(Expr) &&
291
99
      
!Config->Shared71
&&
!IsPreemptible58
) {
292
48
    C.Relocations.push_back(
293
48
        {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
294
48
    return 1;
295
48
  }
296
51
297
51
  
if (51
Expr == R_TLSDESC_CALL51
)
298
0
    return 1;
299
51
  return 0;
300
51
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
200
487
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
487
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
487
204
487
  
if (487
!Body.isTls()487
)
205
415
    return 0;
206
72
207
72
  
if (72
Config->EMachine == EM_ARM72
)
208
27
    return handleARMTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
209
45
  
if (45
Config->EMachine == EM_MIPS45
)
210
0
    return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
211
45
212
45
  bool IsPreemptible = isPreemptible(Body, Type);
213
45
  if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
214
45
      
Config->Shared0
) {
215
0
    if (
InX::Got->addDynTlsEntry(Body)0
) {
216
0
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
217
0
      In<ELFT>::RelaDyn->addReloc(
218
0
          {Target->TlsDescRel, InX::Got, Off, !IsPreemptible, &Body, 0});
219
0
    }
220
0
    if (Expr != R_TLSDESC_CALL)
221
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
222
0
    return 1;
223
0
  }
224
45
225
45
  
if (45
isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)45
) {
226
4
    // Local-Dynamic relocs can be relaxed to Local-Exec.
227
4
    if (
!Config->Shared4
) {
228
2
      C.Relocations.push_back(
229
2
          {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
230
2
      return 2;
231
2
    }
232
2
    
if (2
InX::Got->addTlsIndex()2
)
233
1
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::Got,
234
1
                                   InX::Got->getTlsIndexOff(), false, nullptr,
235
1
                                   0});
236
4
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
237
4
    return 1;
238
4
  }
239
41
240
41
  // Local-Dynamic relocs can be relaxed to Local-Exec.
241
41
  
if (41
isRelExprOneOf<R_ABS, R_TLSLD, R_TLSLD_PC>(Expr) && 41
!Config->Shared4
) {
242
2
    C.Relocations.push_back(
243
2
        {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
244
2
    return 1;
245
2
  }
246
39
247
39
  
if (39
isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD,
248
39
                     R_TLSGD_PC>(Expr)) {
249
6
    if (
Config->Shared6
) {
250
2
      if (
InX::Got->addDynTlsEntry(Body)2
) {
251
2
        uint64_t Off = InX::Got->getGlobalDynOffset(Body);
252
2
        In<ELFT>::RelaDyn->addReloc(
253
2
            {Target->TlsModuleIndexRel, InX::Got, Off, false, &Body, 0});
254
2
255
2
        // If the symbol is preemptible we need the dynamic linker to write
256
2
        // the offset too.
257
2
        uint64_t OffsetOff = Off + Config->Wordsize;
258
2
        if (IsPreemptible)
259
2
          In<ELFT>::RelaDyn->addReloc(
260
2
              {Target->TlsOffsetRel, InX::Got, OffsetOff, false, &Body, 0});
261
2
        else
262
0
          InX::Got->Relocations.push_back(
263
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
264
2
      }
265
2
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
266
2
      return 1;
267
2
    }
268
4
269
4
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
270
4
    // depending on the symbol being locally defined or not.
271
4
    
if (4
IsPreemptible4
) {
272
2
      C.Relocations.push_back(
273
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
274
2
           Offset, Addend, &Body});
275
2
      if (
!Body.isInGot()2
) {
276
2
        InX::Got->addEntry(Body);
277
2
        In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::Got,
278
2
                                     Body.getGotOffset(), false, &Body, 0});
279
2
      }
280
4
    } else {
281
2
      C.Relocations.push_back(
282
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
283
2
           Offset, Addend, &Body});
284
2
    }
285
6
    return Target->TlsGdRelaxSkip;
286
6
  }
287
33
288
33
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
289
33
  // defined.
290
33
  
if (33
isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_GOT_PAGE_PC>(Expr) &&
291
33
      
!Config->Shared23
&&
!IsPreemptible13
) {
292
10
    C.Relocations.push_back(
293
10
        {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
294
10
    return 1;
295
10
  }
296
23
297
23
  
if (23
Expr == R_TLSDESC_CALL23
)
298
0
    return 1;
299
23
  return 0;
300
23
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
200
227
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
227
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
227
204
227
  
if (227
!Body.isTls()227
)
205
204
    return 0;
206
23
207
23
  
if (23
Config->EMachine == EM_ARM23
)
208
0
    return handleARMTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
209
23
  
if (23
Config->EMachine == EM_MIPS23
)
210
23
    return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
211
0
212
0
  bool IsPreemptible = isPreemptible(Body, Type);
213
0
  if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
214
0
      
Config->Shared0
) {
215
0
    if (
InX::Got->addDynTlsEntry(Body)0
) {
216
0
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
217
0
      In<ELFT>::RelaDyn->addReloc(
218
0
          {Target->TlsDescRel, InX::Got, Off, !IsPreemptible, &Body, 0});
219
0
    }
220
0
    if (Expr != R_TLSDESC_CALL)
221
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
222
0
    return 1;
223
0
  }
224
0
225
0
  
if (0
isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)0
) {
226
0
    // Local-Dynamic relocs can be relaxed to Local-Exec.
227
0
    if (
!Config->Shared0
) {
228
0
      C.Relocations.push_back(
229
0
          {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
230
0
      return 2;
231
0
    }
232
0
    
if (0
InX::Got->addTlsIndex()0
)
233
0
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::Got,
234
0
                                   InX::Got->getTlsIndexOff(), false, nullptr,
235
0
                                   0});
236
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
237
0
    return 1;
238
0
  }
239
0
240
0
  // Local-Dynamic relocs can be relaxed to Local-Exec.
241
0
  
if (0
isRelExprOneOf<R_ABS, R_TLSLD, R_TLSLD_PC>(Expr) && 0
!Config->Shared0
) {
242
0
    C.Relocations.push_back(
243
0
        {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
244
0
    return 1;
245
0
  }
246
0
247
0
  
if (0
isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD,
248
0
                     R_TLSGD_PC>(Expr)) {
249
0
    if (
Config->Shared0
) {
250
0
      if (
InX::Got->addDynTlsEntry(Body)0
) {
251
0
        uint64_t Off = InX::Got->getGlobalDynOffset(Body);
252
0
        In<ELFT>::RelaDyn->addReloc(
253
0
            {Target->TlsModuleIndexRel, InX::Got, Off, false, &Body, 0});
254
0
255
0
        // If the symbol is preemptible we need the dynamic linker to write
256
0
        // the offset too.
257
0
        uint64_t OffsetOff = Off + Config->Wordsize;
258
0
        if (IsPreemptible)
259
0
          In<ELFT>::RelaDyn->addReloc(
260
0
              {Target->TlsOffsetRel, InX::Got, OffsetOff, false, &Body, 0});
261
0
        else
262
0
          InX::Got->Relocations.push_back(
263
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
264
0
      }
265
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
266
0
      return 1;
267
0
    }
268
0
269
0
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
270
0
    // depending on the symbol being locally defined or not.
271
0
    
if (0
IsPreemptible0
) {
272
0
      C.Relocations.push_back(
273
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
274
0
           Offset, Addend, &Body});
275
0
      if (
!Body.isInGot()0
) {
276
0
        InX::Got->addEntry(Body);
277
0
        In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::Got,
278
0
                                     Body.getGotOffset(), false, &Body, 0});
279
0
      }
280
0
    } else {
281
0
      C.Relocations.push_back(
282
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
283
0
           Offset, Addend, &Body});
284
0
    }
285
0
    return Target->TlsGdRelaxSkip;
286
0
  }
287
0
288
0
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
289
0
  // defined.
290
0
  
if (0
isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_GOT_PAGE_PC>(Expr) &&
291
0
      
!Config->Shared0
&&
!IsPreemptible0
) {
292
0
    C.Relocations.push_back(
293
0
        {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
294
0
    return 1;
295
0
  }
296
0
297
0
  
if (0
Expr == R_TLSDESC_CALL0
)
298
0
    return 1;
299
0
  return 0;
300
0
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
200
1.05k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
1.05k
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
1.05k
204
1.05k
  
if (1.05k
!Body.isTls()1.05k
)
205
946
    return 0;
206
113
207
113
  
if (113
Config->EMachine == EM_ARM113
)
208
0
    return handleARMTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
209
113
  
if (113
Config->EMachine == EM_MIPS113
)
210
0
    return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
211
113
212
113
  bool IsPreemptible = isPreemptible(Body, Type);
213
113
  if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
214
113
      
Config->Shared24
) {
215
16
    if (
InX::Got->addDynTlsEntry(Body)16
) {
216
4
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
217
4
      In<ELFT>::RelaDyn->addReloc(
218
4
          {Target->TlsDescRel, InX::Got, Off, !IsPreemptible, &Body, 0});
219
4
    }
220
16
    if (Expr != R_TLSDESC_CALL)
221
12
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
222
16
    return 1;
223
16
  }
224
97
225
97
  
if (97
isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)97
) {
226
5
    // Local-Dynamic relocs can be relaxed to Local-Exec.
227
5
    if (
!Config->Shared5
) {
228
3
      C.Relocations.push_back(
229
3
          {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
230
3
      return 2;
231
3
    }
232
2
    
if (2
InX::Got->addTlsIndex()2
)
233
1
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::Got,
234
1
                                   InX::Got->getTlsIndexOff(), false, nullptr,
235
1
                                   0});
236
5
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
237
5
    return 1;
238
5
  }
239
92
240
92
  // Local-Dynamic relocs can be relaxed to Local-Exec.
241
92
  
if (92
isRelExprOneOf<R_ABS, R_TLSLD, R_TLSLD_PC>(Expr) && 92
!Config->Shared12
) {
242
5
    C.Relocations.push_back(
243
5
        {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
244
5
    return 1;
245
5
  }
246
87
247
87
  
if (87
isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD,
248
87
                     R_TLSGD_PC>(Expr)) {
249
21
    if (
Config->Shared21
) {
250
5
      if (
InX::Got->addDynTlsEntry(Body)5
) {
251
4
        uint64_t Off = InX::Got->getGlobalDynOffset(Body);
252
4
        In<ELFT>::RelaDyn->addReloc(
253
4
            {Target->TlsModuleIndexRel, InX::Got, Off, false, &Body, 0});
254
4
255
4
        // If the symbol is preemptible we need the dynamic linker to write
256
4
        // the offset too.
257
4
        uint64_t OffsetOff = Off + Config->Wordsize;
258
4
        if (IsPreemptible)
259
2
          In<ELFT>::RelaDyn->addReloc(
260
2
              {Target->TlsOffsetRel, InX::Got, OffsetOff, false, &Body, 0});
261
4
        else
262
2
          InX::Got->Relocations.push_back(
263
2
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
264
4
      }
265
5
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
266
5
      return 1;
267
5
    }
268
16
269
16
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
270
16
    // depending on the symbol being locally defined or not.
271
16
    
if (16
IsPreemptible16
) {
272
8
      C.Relocations.push_back(
273
8
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
274
8
           Offset, Addend, &Body});
275
8
      if (
!Body.isInGot()8
) {
276
4
        InX::Got->addEntry(Body);
277
4
        In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::Got,
278
4
                                     Body.getGotOffset(), false, &Body, 0});
279
4
      }
280
16
    } else {
281
8
      C.Relocations.push_back(
282
8
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
283
8
           Offset, Addend, &Body});
284
8
    }
285
21
    return Target->TlsGdRelaxSkip;
286
21
  }
287
66
288
66
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
289
66
  // defined.
290
66
  
if (66
isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_GOT_PAGE_PC>(Expr) &&
291
66
      
!Config->Shared48
&&
!IsPreemptible45
) {
292
38
    C.Relocations.push_back(
293
38
        {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
294
38
    return 1;
295
38
  }
296
28
297
28
  
if (28
Expr == R_TLSDESC_CALL28
)
298
0
    return 1;
299
28
  return 0;
300
28
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::SymbolBody&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
200
96
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
96
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
96
204
96
  
if (96
!Body.isTls()96
)
205
84
    return 0;
206
12
207
12
  
if (12
Config->EMachine == EM_ARM12
)
208
0
    return handleARMTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
209
12
  
if (12
Config->EMachine == EM_MIPS12
)
210
12
    return handleMipsTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr);
211
0
212
0
  bool IsPreemptible = isPreemptible(Body, Type);
213
0
  if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
214
0
      
Config->Shared0
) {
215
0
    if (
InX::Got->addDynTlsEntry(Body)0
) {
216
0
      uint64_t Off = InX::Got->getGlobalDynOffset(Body);
217
0
      In<ELFT>::RelaDyn->addReloc(
218
0
          {Target->TlsDescRel, InX::Got, Off, !IsPreemptible, &Body, 0});
219
0
    }
220
0
    if (Expr != R_TLSDESC_CALL)
221
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
222
0
    return 1;
223
0
  }
224
0
225
0
  
if (0
isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)0
) {
226
0
    // Local-Dynamic relocs can be relaxed to Local-Exec.
227
0
    if (
!Config->Shared0
) {
228
0
      C.Relocations.push_back(
229
0
          {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
230
0
      return 2;
231
0
    }
232
0
    
if (0
InX::Got->addTlsIndex()0
)
233
0
      In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, InX::Got,
234
0
                                   InX::Got->getTlsIndexOff(), false, nullptr,
235
0
                                   0});
236
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
237
0
    return 1;
238
0
  }
239
0
240
0
  // Local-Dynamic relocs can be relaxed to Local-Exec.
241
0
  
if (0
isRelExprOneOf<R_ABS, R_TLSLD, R_TLSLD_PC>(Expr) && 0
!Config->Shared0
) {
242
0
    C.Relocations.push_back(
243
0
        {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
244
0
    return 1;
245
0
  }
246
0
247
0
  
if (0
isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD,
248
0
                     R_TLSGD_PC>(Expr)) {
249
0
    if (
Config->Shared0
) {
250
0
      if (
InX::Got->addDynTlsEntry(Body)0
) {
251
0
        uint64_t Off = InX::Got->getGlobalDynOffset(Body);
252
0
        In<ELFT>::RelaDyn->addReloc(
253
0
            {Target->TlsModuleIndexRel, InX::Got, Off, false, &Body, 0});
254
0
255
0
        // If the symbol is preemptible we need the dynamic linker to write
256
0
        // the offset too.
257
0
        uint64_t OffsetOff = Off + Config->Wordsize;
258
0
        if (IsPreemptible)
259
0
          In<ELFT>::RelaDyn->addReloc(
260
0
              {Target->TlsOffsetRel, InX::Got, OffsetOff, false, &Body, 0});
261
0
        else
262
0
          InX::Got->Relocations.push_back(
263
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
264
0
      }
265
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
266
0
      return 1;
267
0
    }
268
0
269
0
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
270
0
    // depending on the symbol being locally defined or not.
271
0
    
if (0
IsPreemptible0
) {
272
0
      C.Relocations.push_back(
273
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
274
0
           Offset, Addend, &Body});
275
0
      if (
!Body.isInGot()0
) {
276
0
        InX::Got->addEntry(Body);
277
0
        In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::Got,
278
0
                                     Body.getGotOffset(), false, &Body, 0});
279
0
      }
280
0
    } else {
281
0
      C.Relocations.push_back(
282
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
283
0
           Offset, Addend, &Body});
284
0
    }
285
0
    return Target->TlsGdRelaxSkip;
286
0
  }
287
0
288
0
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
289
0
  // defined.
290
0
  
if (0
isRelExprOneOf<R_GOT, R_GOT_FROM_END, R_GOT_PC, R_GOT_PAGE_PC>(Expr) &&
291
0
      
!Config->Shared0
&&
!IsPreemptible0
) {
292
0
    C.Relocations.push_back(
293
0
        {R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
294
0
    return 1;
295
0
  }
296
0
297
0
  
if (0
Expr == R_TLSDESC_CALL0
)
298
0
    return 1;
299
0
  return 0;
300
0
}
301
302
228
static uint32_t getMipsPairType(uint32_t Type, const SymbolBody &Sym) {
303
228
  switch (Type) {
304
22
  case R_MIPS_HI16:
305
22
    return R_MIPS_LO16;
306
35
  case R_MIPS_GOT16:
307
35
    return Sym.isLocal() ? 
R_MIPS_LO1612
:
R_MIPS_NONE23
;
308
3
  case R_MICROMIPS_GOT16:
309
3
    return Sym.isLocal() ? 
R_MICROMIPS_LO161
:
R_MIPS_NONE2
;
310
1
  case R_MIPS_PCHI16:
311
1
    return R_MIPS_PCLO16;
312
2
  case R_MICROMIPS_HI16:
313
2
    return R_MICROMIPS_LO16;
314
165
  default:
315
165
    return R_MIPS_NONE;
316
0
  }
317
0
}
318
319
// True if non-preemptable symbol always has the same value regardless of where
320
// the DSO is loaded.
321
610
static bool isAbsolute(const SymbolBody &Body) {
322
610
  if (Body.isUndefWeak())
323
10
    return true;
324
600
  
if (const auto *600
DR600
= dyn_cast<DefinedRegular>(&Body))
325
581
    return DR->Section == nullptr; // Absolute symbol.
326
19
  return false;
327
19
}
328
329
585
static bool isAbsoluteValue(const SymbolBody &Body) {
330
548
  return isAbsolute(Body) || Body.isTls();
331
585
}
332
333
// Returns true if Expr refers a PLT entry.
334
4.81k
static bool needsPlt(RelExpr Expr) {
335
4.81k
  return isRelExprOneOf<R_PLT_PC, R_PPC_PLT_OPD, R_PLT, R_PLT_PAGE_PC>(Expr);
336
4.81k
}
337
338
// Returns true if Expr refers a GOT entry. Note that this function
339
// returns false for TLS variables even though they need GOT, because
340
// TLS variables uses GOT differently than the regular variables.
341
3.21k
static bool needsGot(RelExpr Expr) {
342
3.21k
  return isRelExprOneOf<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
343
3.21k
                        R_MIPS_GOT_OFF32, R_GOT_PAGE_PC, R_GOT_PC,
344
3.21k
                        R_GOT_FROM_END>(Expr);
345
3.21k
}
346
347
// True if this expression is of the form Sym - X, where X is a position in the
348
// file (PC, or GOT for example).
349
515
static bool isRelExpr(RelExpr Expr) {
350
515
  return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL,
351
515
                        R_PAGE_PC, R_RELAX_GOT_PC>(Expr);
352
515
}
353
354
// Returns true if a given relocation can be computed at link-time.
355
//
356
// For instance, we know the offset from a relocation to its target at
357
// link-time if the relocation is PC-relative and refers a
358
// non-interposable function in the same executable. This function
359
// will return true for such relocation.
360
//
361
// If this function returns false, that means we need to emit a
362
// dynamic relocation so that the relocation will be fixed at load-time.
363
template <class ELFT>
364
static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
365
                                     const SymbolBody &Body,
366
3.33k
                                     InputSectionBase &S, uint64_t RelOff) {
367
3.33k
  // These expressions always compute a constant
368
3.33k
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
3.33k
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
3.33k
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
3.33k
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
3.33k
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
1.03k
    return true;
374
2.29k
375
2.29k
  // These never do, except if the entire file is position dependent or if
376
2.29k
  // only the low bits are used.
377
2.29k
  
if (2.29k
E == R_GOT || 2.29k
E == R_PLT2.24k
||
E == R_TLSDESC2.21k
)
378
90
    
return Target->usesOnlyLowPageBits(Type) || 90
!Config->Pic61
;
379
2.20k
380
2.20k
  
if (2.20k
isPreemptible(Body, Type)2.20k
)
381
97
    return false;
382
2.10k
  
if (2.10k
!Config->Pic2.10k
)
383
1.60k
    return true;
384
501
385
501
  // For the target and the relocation, we want to know if they are
386
501
  // absolute or relative.
387
501
  bool AbsVal = isAbsoluteValue(Body);
388
501
  bool RelE = isRelExpr(E);
389
501
  if (
AbsVal && 501
!RelE75
)
390
74
    return true;
391
427
  
if (427
!AbsVal && 427
RelE426
)
392
345
    return true;
393
82
  
if (82
!AbsVal && 82
!RelE81
)
394
81
    return Target->usesOnlyLowPageBits(Type);
395
1
396
1
  // Relative relocation to an absolute value. This is normally unrepresentable,
397
1
  // but if the relocation refers to a weak undefined symbol, we allow it to
398
1
  // resolve to the image base. This is a little strange, but it allows us to
399
1
  // link function calls to such symbols. Normally such a call will be guarded
400
1
  // with a comparison, which will load a zero from the GOT.
401
1
  // Another special case is MIPS _gp_disp symbol which represents offset
402
1
  // between start of a function and '_gp' value and defined as absolute just
403
1
  // to simplify the code.
404
82
  assert(AbsVal && RelE);
405
1
  if (Body.isUndefWeak())
406
0
    return true;
407
1
408
1
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
409
1
        toString(Body) + getLocation<ELFT>(S, Body, RelOff));
410
1
  return true;
411
1
}
Relocations.cpp:bool isStaticLinkTimeConstant<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::RelExpr, unsigned int, lld::elf::SymbolBody const&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
366
147
                                     InputSectionBase &S, uint64_t RelOff) {
367
147
  // These expressions always compute a constant
368
147
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
147
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
147
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
147
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
147
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
54
    return true;
374
93
375
93
  // These never do, except if the entire file is position dependent or if
376
93
  // only the low bits are used.
377
93
  
if (93
E == R_GOT || 93
E == R_PLT93
||
E == R_TLSDESC93
)
378
0
    
return Target->usesOnlyLowPageBits(Type) || 0
!Config->Pic0
;
379
93
380
93
  
if (93
isPreemptible(Body, Type)93
)
381
0
    return false;
382
93
  
if (93
!Config->Pic93
)
383
65
    return true;
384
28
385
28
  // For the target and the relocation, we want to know if they are
386
28
  // absolute or relative.
387
28
  bool AbsVal = isAbsoluteValue(Body);
388
28
  bool RelE = isRelExpr(E);
389
28
  if (
AbsVal && 28
!RelE3
)
390
3
    return true;
391
25
  
if (25
!AbsVal && 25
RelE25
)
392
10
    return true;
393
15
  
if (15
!AbsVal && 15
!RelE15
)
394
15
    return Target->usesOnlyLowPageBits(Type);
395
0
396
0
  // Relative relocation to an absolute value. This is normally unrepresentable,
397
0
  // but if the relocation refers to a weak undefined symbol, we allow it to
398
0
  // resolve to the image base. This is a little strange, but it allows us to
399
0
  // link function calls to such symbols. Normally such a call will be guarded
400
0
  // with a comparison, which will load a zero from the GOT.
401
0
  // Another special case is MIPS _gp_disp symbol which represents offset
402
0
  // between start of a function and '_gp' value and defined as absolute just
403
0
  // to simplify the code.
404
15
  assert(AbsVal && RelE);
405
0
  if (Body.isUndefWeak())
406
0
    return true;
407
0
408
0
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
409
0
        toString(Body) + getLocation<ELFT>(S, Body, RelOff));
410
0
  return true;
411
0
}
Relocations.cpp:bool isStaticLinkTimeConstant<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::RelExpr, unsigned int, lld::elf::SymbolBody const&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
366
1.86k
                                     InputSectionBase &S, uint64_t RelOff) {
367
1.86k
  // These expressions always compute a constant
368
1.86k
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
1.86k
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
1.86k
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
1.86k
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
1.86k
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
562
    return true;
374
1.30k
375
1.30k
  // These never do, except if the entire file is position dependent or if
376
1.30k
  // only the low bits are used.
377
1.30k
  
if (1.30k
E == R_GOT || 1.30k
E == R_PLT1.28k
||
E == R_TLSDESC1.27k
)
378
43
    
return Target->usesOnlyLowPageBits(Type) || 43
!Config->Pic14
;
379
1.25k
380
1.25k
  
if (1.25k
isPreemptible(Body, Type)1.25k
)
381
79
    return false;
382
1.18k
  
if (1.18k
!Config->Pic1.18k
)
383
849
    return true;
384
331
385
331
  // For the target and the relocation, we want to know if they are
386
331
  // absolute or relative.
387
331
  bool AbsVal = isAbsoluteValue(Body);
388
331
  bool RelE = isRelExpr(E);
389
331
  if (
AbsVal && 331
!RelE44
)
390
43
    return true;
391
288
  
if (288
!AbsVal && 288
RelE287
)
392
253
    return true;
393
35
  
if (35
!AbsVal && 35
!RelE34
)
394
34
    return Target->usesOnlyLowPageBits(Type);
395
1
396
1
  // Relative relocation to an absolute value. This is normally unrepresentable,
397
1
  // but if the relocation refers to a weak undefined symbol, we allow it to
398
1
  // resolve to the image base. This is a little strange, but it allows us to
399
1
  // link function calls to such symbols. Normally such a call will be guarded
400
1
  // with a comparison, which will load a zero from the GOT.
401
1
  // Another special case is MIPS _gp_disp symbol which represents offset
402
1
  // between start of a function and '_gp' value and defined as absolute just
403
1
  // to simplify the code.
404
35
  assert(AbsVal && RelE);
405
1
  if (Body.isUndefWeak())
406
0
    return true;
407
1
408
1
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
409
1
        toString(Body) + getLocation<ELFT>(S, Body, RelOff));
410
1
  return true;
411
1
}
Relocations.cpp:bool isStaticLinkTimeConstant<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::RelExpr, unsigned int, lld::elf::SymbolBody const&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
366
436
                                     InputSectionBase &S, uint64_t RelOff) {
367
436
  // These expressions always compute a constant
368
436
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
436
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
436
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
436
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
436
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
169
    return true;
374
267
375
267
  // These never do, except if the entire file is position dependent or if
376
267
  // only the low bits are used.
377
267
  
if (267
E == R_GOT || 267
E == R_PLT267
||
E == R_TLSDESC254
)
378
13
    
return Target->usesOnlyLowPageBits(Type) || 13
!Config->Pic13
;
379
254
380
254
  
if (254
isPreemptible(Body, Type)254
)
381
8
    return false;
382
246
  
if (246
!Config->Pic246
)
383
202
    return true;
384
44
385
44
  // For the target and the relocation, we want to know if they are
386
44
  // absolute or relative.
387
44
  bool AbsVal = isAbsoluteValue(Body);
388
44
  bool RelE = isRelExpr(E);
389
44
  if (
AbsVal && 44
!RelE9
)
390
9
    return true;
391
35
  
if (35
!AbsVal && 35
RelE35
)
392
10
    return true;
393
25
  
if (25
!AbsVal && 25
!RelE25
)
394
25
    return Target->usesOnlyLowPageBits(Type);
395
0
396
0
  // Relative relocation to an absolute value. This is normally unrepresentable,
397
0
  // but if the relocation refers to a weak undefined symbol, we allow it to
398
0
  // resolve to the image base. This is a little strange, but it allows us to
399
0
  // link function calls to such symbols. Normally such a call will be guarded
400
0
  // with a comparison, which will load a zero from the GOT.
401
0
  // Another special case is MIPS _gp_disp symbol which represents offset
402
0
  // between start of a function and '_gp' value and defined as absolute just
403
0
  // to simplify the code.
404
25
  assert(AbsVal && RelE);
405
0
  if (Body.isUndefWeak())
406
0
    return true;
407
0
408
0
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
409
0
        toString(Body) + getLocation<ELFT>(S, Body, RelOff));
410
0
  return true;
411
0
}
Relocations.cpp:bool isStaticLinkTimeConstant<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::RelExpr, unsigned int, lld::elf::SymbolBody const&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
366
883
                                     InputSectionBase &S, uint64_t RelOff) {
367
883
  // These expressions always compute a constant
368
883
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
883
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
883
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
883
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
883
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
252
    return true;
374
631
375
631
  // These never do, except if the entire file is position dependent or if
376
631
  // only the low bits are used.
377
631
  
if (631
E == R_GOT || 631
E == R_PLT602
||
E == R_TLSDESC597
)
378
34
    
return Target->usesOnlyLowPageBits(Type) || 34
!Config->Pic34
;
379
597
380
597
  
if (597
isPreemptible(Body, Type)597
)
381
10
    return false;
382
587
  
if (587
!Config->Pic587
)
383
489
    return true;
384
98
385
98
  // For the target and the relocation, we want to know if they are
386
98
  // absolute or relative.
387
98
  bool AbsVal = isAbsoluteValue(Body);
388
98
  bool RelE = isRelExpr(E);
389
98
  if (
AbsVal && 98
!RelE19
)
390
19
    return true;
391
79
  
if (79
!AbsVal && 79
RelE79
)
392
72
    return true;
393
7
  
if (7
!AbsVal && 7
!RelE7
)
394
7
    return Target->usesOnlyLowPageBits(Type);
395
0
396
0
  // Relative relocation to an absolute value. This is normally unrepresentable,
397
0
  // but if the relocation refers to a weak undefined symbol, we allow it to
398
0
  // resolve to the image base. This is a little strange, but it allows us to
399
0
  // link function calls to such symbols. Normally such a call will be guarded
400
0
  // with a comparison, which will load a zero from the GOT.
401
0
  // Another special case is MIPS _gp_disp symbol which represents offset
402
0
  // between start of a function and '_gp' value and defined as absolute just
403
0
  // to simplify the code.
404
7
  assert(AbsVal && RelE);
405
0
  if (Body.isUndefWeak())
406
0
    return true;
407
0
408
0
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
409
0
        toString(Body) + getLocation<ELFT>(S, Body, RelOff));
410
0
  return true;
411
0
}
412
413
74
static RelExpr toPlt(RelExpr Expr) {
414
74
  if (Expr == R_PPC_OPD)
415
0
    return R_PPC_PLT_OPD;
416
74
  
if (74
Expr == R_PC74
)
417
35
    return R_PLT_PC;
418
39
  
if (39
Expr == R_PAGE_PC39
)
419
1
    return R_PLT_PAGE_PC;
420
38
  
if (38
Expr == R_ABS38
)
421
18
    return R_PLT;
422
20
  return Expr;
423
20
}
424
425
362
static RelExpr fromPlt(RelExpr Expr) {
426
362
  // We decided not to use a plt. Optimize a reference to the plt to a
427
362
  // reference to the symbol itself.
428
362
  if (Expr == R_PLT_PC)
429
256
    return R_PC;
430
106
  
if (106
Expr == R_PPC_PLT_OPD106
)
431
3
    return R_PPC_OPD;
432
103
  
if (103
Expr == R_PLT103
)
433
45
    return R_ABS;
434
58
  return Expr;
435
58
}
436
437
// Returns true if a given shared symbol is in a read-only segment in a DSO.
438
29
template <class ELFT> static bool isReadOnly(SharedSymbol *SS) {
439
29
  typedef typename ELFT::Phdr Elf_Phdr;
440
29
  uint64_t Value = SS->getValue<ELFT>();
441
29
442
29
  // Determine if the symbol is read-only by scanning the DSO's program headers.
443
29
  const SharedFile<ELFT> *File = SS->getFile<ELFT>();
444
29
  for (const Elf_Phdr &Phdr : check(File->getObj().program_headers()))
445
167
    
if (167
(Phdr.p_type == ELF::PT_LOAD || 167
Phdr.p_type == ELF::PT_GNU_RELRO105
) &&
446
167
        
!(Phdr.p_flags & ELF::PF_W)85
&&
Value >= Phdr.p_vaddr57
&&
447
52
        Value < Phdr.p_vaddr + Phdr.p_memsz)
448
4
      return true;
449
25
  return false;
450
25
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol*)
Line
Count
Source
438
18
template <class ELFT> static bool isReadOnly(SharedSymbol *SS) {
439
18
  typedef typename ELFT::Phdr Elf_Phdr;
440
18
  uint64_t Value = SS->getValue<ELFT>();
441
18
442
18
  // Determine if the symbol is read-only by scanning the DSO's program headers.
443
18
  const SharedFile<ELFT> *File = SS->getFile<ELFT>();
444
18
  for (const Elf_Phdr &Phdr : check(File->getObj().program_headers()))
445
102
    
if (102
(Phdr.p_type == ELF::PT_LOAD || 102
Phdr.p_type == ELF::PT_GNU_RELRO67
) &&
446
102
        
!(Phdr.p_flags & ELF::PF_W)52
&&
Value >= Phdr.p_vaddr35
&&
447
30
        Value < Phdr.p_vaddr + Phdr.p_memsz)
448
3
      return true;
449
15
  return false;
450
15
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol*)
Line
Count
Source
438
5
template <class ELFT> static bool isReadOnly(SharedSymbol *SS) {
439
5
  typedef typename ELFT::Phdr Elf_Phdr;
440
5
  uint64_t Value = SS->getValue<ELFT>();
441
5
442
5
  // Determine if the symbol is read-only by scanning the DSO's program headers.
443
5
  const SharedFile<ELFT> *File = SS->getFile<ELFT>();
444
5
  for (const Elf_Phdr &Phdr : check(File->getObj().program_headers()))
445
30
    
if (30
(Phdr.p_type == ELF::PT_LOAD || 30
Phdr.p_type == ELF::PT_GNU_RELRO15
) &&
446
30
        
!(Phdr.p_flags & ELF::PF_W)15
&&
Value >= Phdr.p_vaddr10
&&
447
10
        Value < Phdr.p_vaddr + Phdr.p_memsz)
448
0
      return true;
449
5
  return false;
450
5
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol*)
Line
Count
Source
438
6
template <class ELFT> static bool isReadOnly(SharedSymbol *SS) {
439
6
  typedef typename ELFT::Phdr Elf_Phdr;
440
6
  uint64_t Value = SS->getValue<ELFT>();
441
6
442
6
  // Determine if the symbol is read-only by scanning the DSO's program headers.
443
6
  const SharedFile<ELFT> *File = SS->getFile<ELFT>();
444
6
  for (const Elf_Phdr &Phdr : check(File->getObj().program_headers()))
445
35
    
if (35
(Phdr.p_type == ELF::PT_LOAD || 35
Phdr.p_type == ELF::PT_GNU_RELRO23
) &&
446
35
        
!(Phdr.p_flags & ELF::PF_W)18
&&
Value >= Phdr.p_vaddr12
&&
447
12
        Value < Phdr.p_vaddr + Phdr.p_memsz)
448
1
      return true;
449
5
  return false;
450
5
}
Unexecuted instantiation: Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol*)
451
452
// Returns symbols at the same offset as a given symbol, including SS itself.
453
//
454
// If two or more symbols are at the same offset, and at least one of
455
// them are copied by a copy relocation, all of them need to be copied.
456
// Otherwise, they would refer different places at runtime.
457
template <class ELFT>
458
29
static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) {
459
29
  typedef typename ELFT::Sym Elf_Sym;
460
29
461
29
  SharedFile<ELFT> *File = SS->getFile<ELFT>();
462
29
  uint64_t Shndx = SS->getShndx<ELFT>();
463
29
  uint64_t Value = SS->getValue<ELFT>();
464
29
465
29
  std::vector<SharedSymbol *> Ret;
466
90
  for (const Elf_Sym &S : File->getGlobalELFSyms()) {
467
90
    if (
S.st_shndx != Shndx || 90
S.st_value != Value72
)
468
58
      continue;
469
32
    StringRef Name = check(S.getName(File->getStringTable()));
470
32
    SymbolBody *Sym = Symtab->find(Name);
471
32
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
472
32
      Ret.push_back(Alias);
473
90
  }
474
29
  return Ret;
475
29
}
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
458
6
static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) {
459
6
  typedef typename ELFT::Sym Elf_Sym;
460
6
461
6
  SharedFile<ELFT> *File = SS->getFile<ELFT>();
462
6
  uint64_t Shndx = SS->getShndx<ELFT>();
463
6
  uint64_t Value = SS->getValue<ELFT>();
464
6
465
6
  std::vector<SharedSymbol *> Ret;
466
16
  for (const Elf_Sym &S : File->getGlobalELFSyms()) {
467
16
    if (
S.st_shndx != Shndx || 16
S.st_value != Value16
)
468
10
      continue;
469
6
    StringRef Name = check(S.getName(File->getStringTable()));
470
6
    SymbolBody *Sym = Symtab->find(Name);
471
6
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
472
6
      Ret.push_back(Alias);
473
16
  }
474
6
  return Ret;
475
6
}
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
458
5
static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) {
459
5
  typedef typename ELFT::Sym Elf_Sym;
460
5
461
5
  SharedFile<ELFT> *File = SS->getFile<ELFT>();
462
5
  uint64_t Shndx = SS->getShndx<ELFT>();
463
5
  uint64_t Value = SS->getValue<ELFT>();
464
5
465
5
  std::vector<SharedSymbol *> Ret;
466
25
  for (const Elf_Sym &S : File->getGlobalELFSyms()) {
467
25
    if (
S.st_shndx != Shndx || 25
S.st_value != Value10
)
468
20
      continue;
469
5
    StringRef Name = check(S.getName(File->getStringTable()));
470
5
    SymbolBody *Sym = Symtab->find(Name);
471
5
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
472
5
      Ret.push_back(Alias);
473
25
  }
474
5
  return Ret;
475
5
}
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*)
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
458
18
static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) {
459
18
  typedef typename ELFT::Sym Elf_Sym;
460
18
461
18
  SharedFile<ELFT> *File = SS->getFile<ELFT>();
462
18
  uint64_t Shndx = SS->getShndx<ELFT>();
463
18
  uint64_t Value = SS->getValue<ELFT>();
464
18
465
18
  std::vector<SharedSymbol *> Ret;
466
49
  for (const Elf_Sym &S : File->getGlobalELFSyms()) {
467
49
    if (
S.st_shndx != Shndx || 49
S.st_value != Value46
)
468
28
      continue;
469
21
    StringRef Name = check(S.getName(File->getStringTable()));
470
21
    SymbolBody *Sym = Symtab->find(Name);
471
21
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
472
21
      Ret.push_back(Alias);
473
49
  }
474
18
  return Ret;
475
18
}
476
477
// Reserve space in .bss or .bss.rel.ro for copy relocation.
478
//
479
// The copy relocation is pretty much a hack. If you use a copy relocation
480
// in your program, not only the symbol name but the symbol's size, RW/RO
481
// bit and alignment become part of the ABI. In addition to that, if the
482
// symbol has aliases, the aliases become part of the ABI. That's subtle,
483
// but if you violate that implicit ABI, that can cause very counter-
484
// intuitive consequences.
485
//
486
// So, what is the copy relocation? It's for linking non-position
487
// independent code to DSOs. In an ideal world, all references to data
488
// exported by DSOs should go indirectly through GOT. But if object files
489
// are compiled as non-PIC, all data references are direct. There is no
490
// way for the linker to transform the code to use GOT, as machine
491
// instructions are already set in stone in object files. This is where
492
// the copy relocation takes a role.
493
//
494
// A copy relocation instructs the dynamic linker to copy data from a DSO
495
// to a specified address (which is usually in .bss) at load-time. If the
496
// static linker (that's us) finds a direct data reference to a DSO
497
// symbol, it creates a copy relocation, so that the symbol can be
498
// resolved as if it were in .bss rather than in a DSO.
499
//
500
// As you can see in this function, we create a copy relocation for the
501
// dynamic linker, and the relocation contains not only symbol name but
502
// various other informtion about the symbol. So, such attributes become a
503
// part of the ABI.
504
//
505
// Note for application developers: I can give you a piece of advice if
506
// you are writing a shared library. You probably should export only
507
// functions from your library. You shouldn't export variables.
508
//
509
// As an example what can happen when you export variables without knowing
510
// the semantics of copy relocations, assume that you have an exported
511
// variable of type T. It is an ABI-breaking change to add new members at
512
// end of T even though doing that doesn't change the layout of the
513
// existing members. That's because the space for the new members are not
514
// reserved in .bss unless you recompile the main program. That means they
515
// are likely to overlap with other data that happens to be laid out next
516
// to the variable in .bss. This kind of issue is sometimes very hard to
517
// debug. What's a solution? Instead of exporting a varaible V from a DSO,
518
// define an accessor getV().
519
29
template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) {
520
29
  // Copy relocation against zero-sized symbol doesn't make sense.
521
29
  uint64_t SymSize = SS->template getSize<ELFT>();
522
29
  if (SymSize == 0)
523
0
    fatal("cannot create a copy relocation for symbol " + toString(*SS));
524
29
525
29
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
526
29
  // memory protection by reserving space in the .bss.rel.ro section.
527
29
  bool IsReadOnly = isReadOnly<ELFT>(SS);
528
29
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"4
:
".bss"25
);
529
29
  Sec->reserveSpace(SymSize, SS->getAlignment<ELFT>());
530
29
  if (IsReadOnly)
531
4
    InX::BssRelRo->getParent()->addSection(Sec);
532
29
  else
533
25
    InX::Bss->getParent()->addSection(Sec);
534
29
535
29
  // Look through the DSO's dynamic symbol table for aliases and create a
536
29
  // dynamic symbol for each one. This causes the copy relocation to correctly
537
29
  // interpose any aliases.
538
32
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) {
539
32
    Sym->CopyRelSec = Sec;
540
32
    Sym->IsPreemptible = false;
541
32
    Sym->symbol()->IsUsedInRegularObj = true;
542
32
  }
543
29
544
29
  In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, 0, false, SS, 0});
545
29
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol*)
Line
Count
Source
519
6
template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) {
520
6
  // Copy relocation against zero-sized symbol doesn't make sense.
521
6
  uint64_t SymSize = SS->template getSize<ELFT>();
522
6
  if (SymSize == 0)
523
0
    fatal("cannot create a copy relocation for symbol " + toString(*SS));
524
6
525
6
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
526
6
  // memory protection by reserving space in the .bss.rel.ro section.
527
6
  bool IsReadOnly = isReadOnly<ELFT>(SS);
528
6
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"1
:
".bss"5
);
529
6
  Sec->reserveSpace(SymSize, SS->getAlignment<ELFT>());
530
6
  if (IsReadOnly)
531
1
    InX::BssRelRo->getParent()->addSection(Sec);
532
6
  else
533
5
    InX::Bss->getParent()->addSection(Sec);
534
6
535
6
  // Look through the DSO's dynamic symbol table for aliases and create a
536
6
  // dynamic symbol for each one. This causes the copy relocation to correctly
537
6
  // interpose any aliases.
538
6
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) {
539
6
    Sym->CopyRelSec = Sec;
540
6
    Sym->IsPreemptible = false;
541
6
    Sym->symbol()->IsUsedInRegularObj = true;
542
6
  }
543
6
544
6
  In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, 0, false, SS, 0});
545
6
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol*)
Line
Count
Source
519
5
template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) {
520
5
  // Copy relocation against zero-sized symbol doesn't make sense.
521
5
  uint64_t SymSize = SS->template getSize<ELFT>();
522
5
  if (SymSize == 0)
523
0
    fatal("cannot create a copy relocation for symbol " + toString(*SS));
524
5
525
5
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
526
5
  // memory protection by reserving space in the .bss.rel.ro section.
527
5
  bool IsReadOnly = isReadOnly<ELFT>(SS);
528
5
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"0
:
".bss"5
);
529
5
  Sec->reserveSpace(SymSize, SS->getAlignment<ELFT>());
530
5
  if (IsReadOnly)
531
0
    InX::BssRelRo->getParent()->addSection(Sec);
532
5
  else
533
5
    InX::Bss->getParent()->addSection(Sec);
534
5
535
5
  // Look through the DSO's dynamic symbol table for aliases and create a
536
5
  // dynamic symbol for each one. This causes the copy relocation to correctly
537
5
  // interpose any aliases.
538
5
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) {
539
5
    Sym->CopyRelSec = Sec;
540
5
    Sym->IsPreemptible = false;
541
5
    Sym->symbol()->IsUsedInRegularObj = true;
542
5
  }
543
5
544
5
  In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, 0, false, SS, 0});
545
5
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol*)
Line
Count
Source
519
18
template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) {
520
18
  // Copy relocation against zero-sized symbol doesn't make sense.
521
18
  uint64_t SymSize = SS->template getSize<ELFT>();
522
18
  if (SymSize == 0)
523
0
    fatal("cannot create a copy relocation for symbol " + toString(*SS));
524
18
525
18
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
526
18
  // memory protection by reserving space in the .bss.rel.ro section.
527
18
  bool IsReadOnly = isReadOnly<ELFT>(SS);
528
18
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"3
:
".bss"15
);
529
18
  Sec->reserveSpace(SymSize, SS->getAlignment<ELFT>());
530
18
  if (IsReadOnly)
531
3
    InX::BssRelRo->getParent()->addSection(Sec);
532
18
  else
533
15
    InX::Bss->getParent()->addSection(Sec);
534
18
535
18
  // Look through the DSO's dynamic symbol table for aliases and create a
536
18
  // dynamic symbol for each one. This causes the copy relocation to correctly
537
18
  // interpose any aliases.
538
21
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) {
539
21
    Sym->CopyRelSec = Sec;
540
21
    Sym->IsPreemptible = false;
541
21
    Sym->symbol()->IsUsedInRegularObj = true;
542
21
  }
543
18
544
18
  In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, 0, false, SS, 0});
545
18
}
Unexecuted instantiation: Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol*)
546
547
72
static void errorOrWarn(const Twine &Msg) {
548
72
  if (!Config->NoinhibitExec)
549
61
    error(Msg);
550
72
  else
551
11
    warn(Msg);
552
72
}
553
554
template <class ELFT>
555
static RelExpr adjustExpr(SymbolBody &Body, RelExpr Expr, uint32_t Type,
556
                          const uint8_t *Data, InputSectionBase &S,
557
1.94k
                          typename ELFT::uint RelOff) {
558
1.94k
  if (
Body.isGnuIFunc()1.94k
) {
559
36
    Expr = toPlt(Expr);
560
1.94k
  } else 
if (1.91k
!isPreemptible(Body, Type)1.91k
) {
561
1.35k
    if (needsPlt(Expr))
562
295
      Expr = fromPlt(Expr);
563
1.35k
    if (
Expr == R_GOT_PC && 1.35k
!isAbsoluteValue(Body)84
)
564
46
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
1.91k
  }
566
1.94k
567
1.93k
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
1.94k
  if (
IsWrite || 1.94k
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)1.70k
)
569
1.84k
    return Expr;
570
103
571
103
  // If we got here we know that this relocation would require the dynamic
572
103
  // linker to write a value to read only memory.
573
103
574
103
  // If the relocation is to a weak undef, give up on it and produce a
575
103
  // non preemptible 0.
576
103
  
if (103
Body.isUndefWeak()103
) {
577
5
    Body.IsPreemptible = false;
578
5
    return Expr;
579
5
  }
580
98
581
98
  // We can hack around it if we are producing an executable and
582
98
  // the refered symbol can be preemepted to refer to the executable.
583
98
  
if (98
Config->Shared || 98
(Config->Pic && 87
!isRelExpr(Expr)14
)) {
584
20
    error("can't create dynamic relocation " + toString(Type) + " against " +
585
1
          (Body.getName().empty() ? "local symbol"
586
19
                                  : "symbol: " + toString(Body)) +
587
20
          " in readonly segment; recompile object files with -fPIC" +
588
20
          getLocation<ELFT>(S, Body, RelOff));
589
20
    return Expr;
590
20
  }
591
78
592
78
  
if (78
Body.getVisibility() != STV_DEFAULT78
) {
593
2
    error("cannot preempt symbol: " + toString(Body) +
594
2
          getLocation<ELFT>(S, Body, RelOff));
595
2
    return Expr;
596
2
  }
597
76
598
76
  
if (76
Body.isObject()76
) {
599
29
    // Produce a copy relocation.
600
29
    auto *B = cast<SharedSymbol>(&Body);
601
29
    if (
!B->CopyRelSec29
) {
602
29
      if (Config->ZNocopyreloc)
603
3
        error("unresolvable relocation " + toString(Type) +
604
3
              " against symbol '" + toString(*B) +
605
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
606
3
              getLocation<ELFT>(S, Body, RelOff));
607
29
608
29
      addCopyRelSymbol<ELFT>(B);
609
29
    }
610
29
    return Expr;
611
29
  }
612
47
613
47
  
if (47
Body.isFunc()47
) {
614
38
    // This handles a non PIC program call to function in a shared library. In
615
38
    // an ideal world, we could just report an error saying the relocation can
616
38
    // overflow at runtime. In the real world with glibc, crt1.o has a
617
38
    // R_X86_64_PC32 pointing to libc.so.
618
38
    //
619
38
    // The general idea on how to handle such cases is to create a PLT entry and
620
38
    // use that as the function value.
621
38
    //
622
38
    // For the static linking part, we just return a plt expr and everything
623
38
    // else will use the the PLT entry as the address.
624
38
    //
625
38
    // The remaining problem is making sure pointer equality still works. We
626
38
    // need the help of the dynamic linker for that. We let it know that we have
627
38
    // a direct reference to a so symbol by creating an undefined symbol with a
628
38
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
629
38
    // the value of the symbol we created. This is true even for got entries, so
630
38
    // pointer equality is maintained. To avoid an infinite loop, the only entry
631
38
    // that points to the real function is a dedicated got entry used by the
632
38
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
633
38
    // R_386_JMP_SLOT, etc).
634
38
    Body.NeedsPltAddr = true;
635
38
    Body.IsPreemptible = false;
636
38
    return toPlt(Expr);
637
38
  }
638
9
639
9
  errorOrWarn("symbol '" + toString(Body) + "' defined in " +
640
9
              toString(Body.getFile()) + " has no type");
641
9
  return Expr;
642
9
}
Relocations.cpp:lld::elf::RelExpr adjustExpr<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SymbolBody&, lld::elf::RelExpr, unsigned int, unsigned char const*, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::uint)
Line
Count
Source
557
1.13k
                          typename ELFT::uint RelOff) {
558
1.13k
  if (
Body.isGnuIFunc()1.13k
) {
559
24
    Expr = toPlt(Expr);
560
1.13k
  } else 
if (1.10k
!isPreemptible(Body, Type)1.10k
) {
561
738
    if (needsPlt(Expr))
562
81
      Expr = fromPlt(Expr);
563
738
    if (
Expr == R_GOT_PC && 738
!isAbsoluteValue(Body)78
)
564
43
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
1.10k
  }
566
1.13k
567
1.12k
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
1.13k
  if (
IsWrite || 1.13k
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)972
)
569
1.05k
    return Expr;
570
80
571
80
  // If we got here we know that this relocation would require the dynamic
572
80
  // linker to write a value to read only memory.
573
80
574
80
  // If the relocation is to a weak undef, give up on it and produce a
575
80
  // non preemptible 0.
576
80
  
if (80
Body.isUndefWeak()80
) {
577
5
    Body.IsPreemptible = false;
578
5
    return Expr;
579
5
  }
580
75
581
75
  // We can hack around it if we are producing an executable and
582
75
  // the refered symbol can be preemepted to refer to the executable.
583
75
  
if (75
Config->Shared || 75
(Config->Pic && 66
!isRelExpr(Expr)11
)) {
584
15
    error("can't create dynamic relocation " + toString(Type) + " against " +
585
1
          (Body.getName().empty() ? "local symbol"
586
14
                                  : "symbol: " + toString(Body)) +
587
15
          " in readonly segment; recompile object files with -fPIC" +
588
15
          getLocation<ELFT>(S, Body, RelOff));
589
15
    return Expr;
590
15
  }
591
60
592
60
  
if (60
Body.getVisibility() != STV_DEFAULT60
) {
593
2
    error("cannot preempt symbol: " + toString(Body) +
594
2
          getLocation<ELFT>(S, Body, RelOff));
595
2
    return Expr;
596
2
  }
597
58
598
58
  
if (58
Body.isObject()58
) {
599
18
    // Produce a copy relocation.
600
18
    auto *B = cast<SharedSymbol>(&Body);
601
18
    if (
!B->CopyRelSec18
) {
602
18
      if (Config->ZNocopyreloc)
603
3
        error("unresolvable relocation " + toString(Type) +
604
3
              " against symbol '" + toString(*B) +
605
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
606
3
              getLocation<ELFT>(S, Body, RelOff));
607
18
608
18
      addCopyRelSymbol<ELFT>(B);
609
18
    }
610
18
    return Expr;
611
18
  }
612
40
613
40
  
if (40
Body.isFunc()40
) {
614
31
    // This handles a non PIC program call to function in a shared library. In
615
31
    // an ideal world, we could just report an error saying the relocation can
616
31
    // overflow at runtime. In the real world with glibc, crt1.o has a
617
31
    // R_X86_64_PC32 pointing to libc.so.
618
31
    //
619
31
    // The general idea on how to handle such cases is to create a PLT entry and
620
31
    // use that as the function value.
621
31
    //
622
31
    // For the static linking part, we just return a plt expr and everything
623
31
    // else will use the the PLT entry as the address.
624
31
    //
625
31
    // The remaining problem is making sure pointer equality still works. We
626
31
    // need the help of the dynamic linker for that. We let it know that we have
627
31
    // a direct reference to a so symbol by creating an undefined symbol with a
628
31
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
629
31
    // the value of the symbol we created. This is true even for got entries, so
630
31
    // pointer equality is maintained. To avoid an infinite loop, the only entry
631
31
    // that points to the real function is a dedicated got entry used by the
632
31
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
633
31
    // R_386_JMP_SLOT, etc).
634
31
    Body.NeedsPltAddr = true;
635
31
    Body.IsPreemptible = false;
636
31
    return toPlt(Expr);
637
31
  }
638
9
639
9
  errorOrWarn("symbol '" + toString(Body) + "' defined in " +
640
9
              toString(Body.getFile()) + " has no type");
641
9
  return Expr;
642
9
}
Relocations.cpp:lld::elf::RelExpr adjustExpr<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SymbolBody&, lld::elf::RelExpr, unsigned int, unsigned char const*, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::uint)
Line
Count
Source
557
494
                          typename ELFT::uint RelOff) {
558
494
  if (
Body.isGnuIFunc()494
) {
559
12
    Expr = toPlt(Expr);
560
494
  } else 
if (482
!isPreemptible(Body, Type)482
) {
561
354
    if (needsPlt(Expr))
562
166
      Expr = fromPlt(Expr);
563
354
    if (
Expr == R_GOT_PC && 354
!isAbsoluteValue(Body)6
)
564
3
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
482
  }
566
494
567
494
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
494
  if (
IsWrite || 494
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)455
)
569
479
    return Expr;
570
15
571
15
  // If we got here we know that this relocation would require the dynamic
572
15
  // linker to write a value to read only memory.
573
15
574
15
  // If the relocation is to a weak undef, give up on it and produce a
575
15
  // non preemptible 0.
576
15
  
if (15
Body.isUndefWeak()15
) {
577
0
    Body.IsPreemptible = false;
578
0
    return Expr;
579
0
  }
580
15
581
15
  // We can hack around it if we are producing an executable and
582
15
  // the refered symbol can be preemepted to refer to the executable.
583
15
  
if (15
Config->Shared || 15
(Config->Pic && 13
!isRelExpr(Expr)3
)) {
584
5
    error("can't create dynamic relocation " + toString(Type) + " against " +
585
0
          (Body.getName().empty() ? "local symbol"
586
5
                                  : "symbol: " + toString(Body)) +
587
5
          " in readonly segment; recompile object files with -fPIC" +
588
5
          getLocation<ELFT>(S, Body, RelOff));
589
5
    return Expr;
590
5
  }
591
10
592
10
  
if (10
Body.getVisibility() != STV_DEFAULT10
) {
593
0
    error("cannot preempt symbol: " + toString(Body) +
594
0
          getLocation<ELFT>(S, Body, RelOff));
595
0
    return Expr;
596
0
  }
597
10
598
10
  
if (10
Body.isObject()10
) {
599
6
    // Produce a copy relocation.
600
6
    auto *B = cast<SharedSymbol>(&Body);
601
6
    if (
!B->CopyRelSec6
) {
602
6
      if (Config->ZNocopyreloc)
603
0
        error("unresolvable relocation " + toString(Type) +
604
0
              " against symbol '" + toString(*B) +
605
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
606
0
              getLocation<ELFT>(S, Body, RelOff));
607
6
608
6
      addCopyRelSymbol<ELFT>(B);
609
6
    }
610
6
    return Expr;
611
6
  }
612
4
613
4
  
if (4
Body.isFunc()4
) {
614
4
    // This handles a non PIC program call to function in a shared library. In
615
4
    // an ideal world, we could just report an error saying the relocation can
616
4
    // overflow at runtime. In the real world with glibc, crt1.o has a
617
4
    // R_X86_64_PC32 pointing to libc.so.
618
4
    //
619
4
    // The general idea on how to handle such cases is to create a PLT entry and
620
4
    // use that as the function value.
621
4
    //
622
4
    // For the static linking part, we just return a plt expr and everything
623
4
    // else will use the the PLT entry as the address.
624
4
    //
625
4
    // The remaining problem is making sure pointer equality still works. We
626
4
    // need the help of the dynamic linker for that. We let it know that we have
627
4
    // a direct reference to a so symbol by creating an undefined symbol with a
628
4
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
629
4
    // the value of the symbol we created. This is true even for got entries, so
630
4
    // pointer equality is maintained. To avoid an infinite loop, the only entry
631
4
    // that points to the real function is a dedicated got entry used by the
632
4
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
633
4
    // R_386_JMP_SLOT, etc).
634
4
    Body.NeedsPltAddr = true;
635
4
    Body.IsPreemptible = false;
636
4
    return toPlt(Expr);
637
4
  }
638
0
639
0
  errorOrWarn("symbol '" + toString(Body) + "' defined in " +
640
0
              toString(Body.getFile()) + " has no type");
641
0
  return Expr;
642
0
}
Relocations.cpp:lld::elf::RelExpr adjustExpr<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SymbolBody&, lld::elf::RelExpr, unsigned int, unsigned char const*, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::uint)
Line
Count
Source
557
96
                          typename ELFT::uint RelOff) {
558
96
  if (
Body.isGnuIFunc()96
) {
559
0
    Expr = toPlt(Expr);
560
96
  } else 
if (96
!isPreemptible(Body, Type)96
) {
561
75
    if (needsPlt(Expr))
562
3
      Expr = fromPlt(Expr);
563
75
    if (
Expr == R_GOT_PC && 75
!isAbsoluteValue(Body)0
)
564
0
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
96
  }
566
96
567
92
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
96
  if (
IsWrite || 96
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)62
)
569
96
    return Expr;
570
0
571
0
  // If we got here we know that this relocation would require the dynamic
572
0
  // linker to write a value to read only memory.
573
0
574
0
  // If the relocation is to a weak undef, give up on it and produce a
575
0
  // non preemptible 0.
576
0
  
if (0
Body.isUndefWeak()0
) {
577
0
    Body.IsPreemptible = false;
578
0
    return Expr;
579
0
  }
580
0
581
0
  // We can hack around it if we are producing an executable and
582
0
  // the refered symbol can be preemepted to refer to the executable.
583
0
  
if (0
Config->Shared || 0
(Config->Pic && 0
!isRelExpr(Expr)0
)) {
584
0
    error("can't create dynamic relocation " + toString(Type) + " against " +
585
0
          (Body.getName().empty() ? "local symbol"
586
0
                                  : "symbol: " + toString(Body)) +
587
0
          " in readonly segment; recompile object files with -fPIC" +
588
0
          getLocation<ELFT>(S, Body, RelOff));
589
0
    return Expr;
590
0
  }
591
0
592
0
  
if (0
Body.getVisibility() != STV_DEFAULT0
) {
593
0
    error("cannot preempt symbol: " + toString(Body) +
594
0
          getLocation<ELFT>(S, Body, RelOff));
595
0
    return Expr;
596
0
  }
597
0
598
0
  
if (0
Body.isObject()0
) {
599
0
    // Produce a copy relocation.
600
0
    auto *B = cast<SharedSymbol>(&Body);
601
0
    if (
!B->CopyRelSec0
) {
602
0
      if (Config->ZNocopyreloc)
603
0
        error("unresolvable relocation " + toString(Type) +
604
0
              " against symbol '" + toString(*B) +
605
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
606
0
              getLocation<ELFT>(S, Body, RelOff));
607
0
608
0
      addCopyRelSymbol<ELFT>(B);
609
0
    }
610
0
    return Expr;
611
0
  }
612
0
613
0
  
if (0
Body.isFunc()0
) {
614
0
    // This handles a non PIC program call to function in a shared library. In
615
0
    // an ideal world, we could just report an error saying the relocation can
616
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
617
0
    // R_X86_64_PC32 pointing to libc.so.
618
0
    //
619
0
    // The general idea on how to handle such cases is to create a PLT entry and
620
0
    // use that as the function value.
621
0
    //
622
0
    // For the static linking part, we just return a plt expr and everything
623
0
    // else will use the the PLT entry as the address.
624
0
    //
625
0
    // The remaining problem is making sure pointer equality still works. We
626
0
    // need the help of the dynamic linker for that. We let it know that we have
627
0
    // a direct reference to a so symbol by creating an undefined symbol with a
628
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
629
0
    // the value of the symbol we created. This is true even for got entries, so
630
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
631
0
    // that points to the real function is a dedicated got entry used by the
632
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
633
0
    // R_386_JMP_SLOT, etc).
634
0
    Body.NeedsPltAddr = true;
635
0
    Body.IsPreemptible = false;
636
0
    return toPlt(Expr);
637
0
  }
638
0
639
0
  errorOrWarn("symbol '" + toString(Body) + "' defined in " +
640
0
              toString(Body.getFile()) + " has no type");
641
0
  return Expr;
642
0
}
Relocations.cpp:lld::elf::RelExpr adjustExpr<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SymbolBody&, lld::elf::RelExpr, unsigned int, unsigned char const*, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::uint)
Line
Count
Source
557
227
                          typename ELFT::uint RelOff) {
558
227
  if (
Body.isGnuIFunc()227
) {
559
0
    Expr = toPlt(Expr);
560
227
  } else 
if (227
!isPreemptible(Body, Type)227
) {
561
183
    if (needsPlt(Expr))
562
45
      Expr = fromPlt(Expr);
563
183
    if (
Expr == R_GOT_PC && 183
!isAbsoluteValue(Body)0
)
564
0
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
227
  }
566
227
567
227
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
227
  if (
IsWrite || 227
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)218
)
569
219
    return Expr;
570
8
571
8
  // If we got here we know that this relocation would require the dynamic
572
8
  // linker to write a value to read only memory.
573
8
574
8
  // If the relocation is to a weak undef, give up on it and produce a
575
8
  // non preemptible 0.
576
8
  
if (8
Body.isUndefWeak()8
) {
577
0
    Body.IsPreemptible = false;
578
0
    return Expr;
579
0
  }
580
8
581
8
  // We can hack around it if we are producing an executable and
582
8
  // the refered symbol can be preemepted to refer to the executable.
583
8
  
if (8
Config->Shared || 8
(Config->Pic && 8
!isRelExpr(Expr)0
)) {
584
0
    error("can't create dynamic relocation " + toString(Type) + " against " +
585
0
          (Body.getName().empty() ? "local symbol"
586
0
                                  : "symbol: " + toString(Body)) +
587
0
          " in readonly segment; recompile object files with -fPIC" +
588
0
          getLocation<ELFT>(S, Body, RelOff));
589
0
    return Expr;
590
0
  }
591
8
592
8
  
if (8
Body.getVisibility() != STV_DEFAULT8
) {
593
0
    error("cannot preempt symbol: " + toString(Body) +
594
0
          getLocation<ELFT>(S, Body, RelOff));
595
0
    return Expr;
596
0
  }
597
8
598
8
  
if (8
Body.isObject()8
) {
599
5
    // Produce a copy relocation.
600
5
    auto *B = cast<SharedSymbol>(&Body);
601
5
    if (
!B->CopyRelSec5
) {
602
5
      if (Config->ZNocopyreloc)
603
0
        error("unresolvable relocation " + toString(Type) +
604
0
              " against symbol '" + toString(*B) +
605
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
606
0
              getLocation<ELFT>(S, Body, RelOff));
607
5
608
5
      addCopyRelSymbol<ELFT>(B);
609
5
    }
610
5
    return Expr;
611
5
  }
612
3
613
3
  
if (3
Body.isFunc()3
) {
614
3
    // This handles a non PIC program call to function in a shared library. In
615
3
    // an ideal world, we could just report an error saying the relocation can
616
3
    // overflow at runtime. In the real world with glibc, crt1.o has a
617
3
    // R_X86_64_PC32 pointing to libc.so.
618
3
    //
619
3
    // The general idea on how to handle such cases is to create a PLT entry and
620
3
    // use that as the function value.
621
3
    //
622
3
    // For the static linking part, we just return a plt expr and everything
623
3
    // else will use the the PLT entry as the address.
624
3
    //
625
3
    // The remaining problem is making sure pointer equality still works. We
626
3
    // need the help of the dynamic linker for that. We let it know that we have
627
3
    // a direct reference to a so symbol by creating an undefined symbol with a
628
3
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
629
3
    // the value of the symbol we created. This is true even for got entries, so
630
3
    // pointer equality is maintained. To avoid an infinite loop, the only entry
631
3
    // that points to the real function is a dedicated got entry used by the
632
3
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
633
3
    // R_386_JMP_SLOT, etc).
634
3
    Body.NeedsPltAddr = true;
635
3
    Body.IsPreemptible = false;
636
3
    return toPlt(Expr);
637
3
  }
638
0
639
0
  errorOrWarn("symbol '" + toString(Body) + "' defined in " +
640
0
              toString(Body.getFile()) + " has no type");
641
0
  return Expr;
642
0
}
643
644
// Returns an addend of a given relocation. If it is RELA, an addend
645
// is in a relocation itself. If it is REL, we need to read it from an
646
// input section.
647
template <class ELFT, class RelTy>
648
1.86k
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
1.86k
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
1.86k
  int64_t A = RelTy::IsRela
651
1.16k
                  ? getAddend<ELFT>(Rel)
652
707
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
1.86k
654
1.86k
  if (
Config->EMachine == EM_PPC64 && 1.86k
Config->Pic45
&&
Type == R_PPC64_TOC14
)
655
2
    A += getPPC64TocBase();
656
1.86k
  return A;
657
1.86k
}
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&, unsigned char const*)
Line
Count
Source
648
8
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
8
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
8
  int64_t A = RelTy::IsRela
651
8
                  ? getAddend<ELFT>(Rel)
652
0
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
8
654
8
  if (
Config->EMachine == EM_PPC64 && 8
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
8
  return A;
657
8
}
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&, unsigned char const*)
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&, unsigned char const*)
Line
Count
Source
648
96
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
96
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
96
  int64_t A = RelTy::IsRela
651
96
                  ? getAddend<ELFT>(Rel)
652
0
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
96
654
96
  if (
Config->EMachine == EM_PPC64 && 96
Config->Pic45
&&
Type == R_PPC64_TOC14
)
655
2
    A += getPPC64TocBase();
656
96
  return A;
657
96
}
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&, unsigned char const*)
Line
Count
Source
648
2
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
2
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
2
  int64_t A = RelTy::IsRela
651
0
                  ? getAddend<ELFT>(Rel)
652
2
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
2
654
2
  if (
Config->EMachine == EM_PPC64 && 2
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
2
  return A;
657
2
}
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&, unsigned char const*)
Line
Count
Source
648
1.05k
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
1.05k
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
1.05k
  int64_t A = RelTy::IsRela
651
1.05k
                  ? getAddend<ELFT>(Rel)
652
0
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
1.05k
654
1.05k
  if (
Config->EMachine == EM_PPC64 && 1.05k
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
1.05k
  return A;
657
1.05k
}
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&, unsigned char const*)
Line
Count
Source
648
219
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
219
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
219
  int64_t A = RelTy::IsRela
651
0
                  ? getAddend<ELFT>(Rel)
652
219
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
219
654
219
  if (
Config->EMachine == EM_PPC64 && 219
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
219
  return A;
657
219
}
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&, unsigned char const*)
Line
Count
Source
648
486
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
486
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
486
  int64_t A = RelTy::IsRela
651
0
                  ? getAddend<ELFT>(Rel)
652
486
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
486
654
486
  if (
Config->EMachine == EM_PPC64 && 486
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
486
  return A;
657
486
}
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&, unsigned char const*)
Line
Count
Source
648
1
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
1
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
1
  int64_t A = RelTy::IsRela
651
1
                  ? getAddend<ELFT>(Rel)
652
0
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
1
654
1
  if (
Config->EMachine == EM_PPC64 && 1
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
1
  return A;
657
1
}
658
659
// MIPS has an odd notion of "paired" relocations to calculate addends.
660
// For example, if a relocation is of R_MIPS_HI16, there must be a
661
// R_MIPS_LO16 relocation after that, and an addend is calculated using
662
// the two relocations.
663
template <class ELFT, class RelTy>
664
static int64_t computeMipsAddend(const RelTy &Rel, InputSectionBase &Sec,
665
                                 RelExpr Expr, SymbolBody &Body,
666
288
                                 const RelTy *End) {
667
288
  if (
Expr == R_MIPS_GOTREL && 288
Body.isLocal()18
)
668
7
    return Sec.getFile<ELFT>()->MipsGp0;
669
281
670
281
  // The ABI says that the paired relocation is used only for REL.
671
281
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
281
  
if (281
RelTy::IsRela281
)
673
53
    return 0;
674
228
675
228
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
228
  uint32_t PairTy = getMipsPairType(Type, Body);
677
228
  if (PairTy == R_MIPS_NONE)
678
190
    return 0;
679
38
680
38
  const uint8_t *Buf = Sec.Data.data();
681
38
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
682
38
683
38
  // To make things worse, paired relocations might not be contiguous in
684
38
  // the relocation table, so we need to do linear search. *sigh*
685
77
  for (const RelTy *RI = &Rel; 
RI != End77
;
++RI39
) {
686
76
    if (RI->getType(Config->IsMips64EL) != PairTy)
687
39
      continue;
688
37
    
if (37
RI->getSymbol(Config->IsMips64EL) != SymIndex37
)
689
0
      continue;
690
37
691
37
    return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
692
37
  }
693
38
694
1
  warn("can't find matching " + toString(PairTy) + " relocation for " +
695
1
       toString(Type));
696
1
  return 0;
697
288
}
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*)
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*)
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*)
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*)
Line
Count
Source
666
51
                                 const RelTy *End) {
667
51
  if (
Expr == R_MIPS_GOTREL && 51
Body.isLocal()10
)
668
1
    return Sec.getFile<ELFT>()->MipsGp0;
669
50
670
50
  // The ABI says that the paired relocation is used only for REL.
671
50
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
50
  
if (50
RelTy::IsRela50
)
673
50
    return 0;
674
0
675
0
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
0
  uint32_t PairTy = getMipsPairType(Type, Body);
677
0
  if (PairTy == R_MIPS_NONE)
678
0
    return 0;
679
0
680
0
  const uint8_t *Buf = Sec.Data.data();
681
0
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
682
0
683
0
  // To make things worse, paired relocations might not be contiguous in
684
0
  // the relocation table, so we need to do linear search. *sigh*
685
0
  for (const RelTy *RI = &Rel; 
RI != End0
;
++RI0
) {
686
0
    if (RI->getType(Config->IsMips64EL) != PairTy)
687
0
      continue;
688
0
    
if (0
RI->getSymbol(Config->IsMips64EL) != SymIndex0
)
689
0
      continue;
690
0
691
0
    return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
692
0
  }
693
0
694
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
695
0
       toString(Type));
696
0
  return 0;
697
51
}
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*)
Line
Count
Source
666
219
                                 const RelTy *End) {
667
219
  if (
Expr == R_MIPS_GOTREL && 219
Body.isLocal()6
)
668
6
    return Sec.getFile<ELFT>()->MipsGp0;
669
213
670
213
  // The ABI says that the paired relocation is used only for REL.
671
213
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
213
  
if (213
RelTy::IsRela213
)
673
0
    return 0;
674
213
675
213
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
213
  uint32_t PairTy = getMipsPairType(Type, Body);
677
213
  if (PairTy == R_MIPS_NONE)
678
176
    return 0;
679
37
680
37
  const uint8_t *Buf = Sec.Data.data();
681
37
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
682
37
683
37
  // To make things worse, paired relocations might not be contiguous in
684
37
  // the relocation table, so we need to do linear search. *sigh*
685
75
  for (const RelTy *RI = &Rel; 
RI != End75
;
++RI38
) {
686
74
    if (RI->getType(Config->IsMips64EL) != PairTy)
687
38
      continue;
688
36
    
if (36
RI->getSymbol(Config->IsMips64EL) != SymIndex36
)
689
0
      continue;
690
36
691
36
    return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
692
36
  }
693
37
694
1
  warn("can't find matching " + toString(PairTy) + " relocation for " +
695
1
       toString(Type));
696
1
  return 0;
697
219
}
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*)
Line
Count
Source
666
15
                                 const RelTy *End) {
667
15
  if (
Expr == R_MIPS_GOTREL && 15
Body.isLocal()0
)
668
0
    return Sec.getFile<ELFT>()->MipsGp0;
669
15
670
15
  // The ABI says that the paired relocation is used only for REL.
671
15
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
15
  
if (15
RelTy::IsRela15
)
673
0
    return 0;
674
15
675
15
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
15
  uint32_t PairTy = getMipsPairType(Type, Body);
677
15
  if (PairTy == R_MIPS_NONE)
678
14
    return 0;
679
1
680
1
  const uint8_t *Buf = Sec.Data.data();
681
1
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
682
1
683
1
  // To make things worse, paired relocations might not be contiguous in
684
1
  // the relocation table, so we need to do linear search. *sigh*
685
2
  for (const RelTy *RI = &Rel; 
RI != End2
;
++RI1
) {
686
2
    if (RI->getType(Config->IsMips64EL) != PairTy)
687
1
      continue;
688
1
    
if (1
RI->getSymbol(Config->IsMips64EL) != SymIndex1
)
689
0
      continue;
690
1
691
1
    return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
692
1
  }
693
1
694
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
695
0
       toString(Type));
696
0
  return 0;
697
15
}
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*)
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&, lld::elf::InputSectionBase&, lld::elf::RelExpr, lld::elf::SymbolBody&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*)
Line
Count
Source
666
3
                                 const RelTy *End) {
667
3
  if (
Expr == R_MIPS_GOTREL && 3
Body.isLocal()2
)
668
0
    return Sec.getFile<ELFT>()->MipsGp0;
669
3
670
3
  // The ABI says that the paired relocation is used only for REL.
671
3
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
3
  
if (3
RelTy::IsRela3
)
673
3
    return 0;
674
0
675
0
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
0
  uint32_t PairTy = getMipsPairType(Type, Body);
677
0
  if (PairTy == R_MIPS_NONE)
678
0
    return 0;
679
0
680
0
  const uint8_t *Buf = Sec.Data.data();
681
0
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
682
0
683
0
  // To make things worse, paired relocations might not be contiguous in
684
0
  // the relocation table, so we need to do linear search. *sigh*
685
0
  for (const RelTy *RI = &Rel; 
RI != End0
;
++RI0
) {
686
0
    if (RI->getType(Config->IsMips64EL) != PairTy)
687
0
      continue;
688
0
    
if (0
RI->getSymbol(Config->IsMips64EL) != SymIndex0
)
689
0
      continue;
690
0
691
0
    return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
692
0
  }
693
0
694
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
695
0
       toString(Type));
696
0
  return 0;
697
3
}
698
699
template <class ELFT>
700
static void reportUndefined(SymbolBody &Sym, InputSectionBase &S,
701
182
                            uint64_t Offset) {
702
182
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
182
705
182
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
179
                       Sym.getVisibility() == STV_DEFAULT;
707
182
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 182
CanBeExternal132
)
708
128
    return;
709
54
710
54
  std::string Msg =
711
54
      "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
712
54
713
54
  std::string Src = S.getSrcMsg<ELFT>(Offset);
714
54
  if (!Src.empty())
715
20
    Msg += Src + "\n>>>               ";
716
54
  Msg += S.getObjMsg<ELFT>(Offset);
717
54
718
54
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 54
CanBeExternal5
)
719
3
    warn(Msg);
720
54
  else
721
51
    errorOrWarn(Msg);
722
182
}
Relocations.cpp:void reportUndefined<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
701
3
                            uint64_t Offset) {
702
3
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
3
705
3
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
3
                       Sym.getVisibility() == STV_DEFAULT;
707
3
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 3
CanBeExternal3
)
708
3
    return;
709
0
710
0
  std::string Msg =
711
0
      "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
712
0
713
0
  std::string Src = S.getSrcMsg<ELFT>(Offset);
714
0
  if (!Src.empty())
715
0
    Msg += Src + "\n>>>               ";
716
0
  Msg += S.getObjMsg<ELFT>(Offset);
717
0
718
0
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 0
CanBeExternal0
)
719
0
    warn(Msg);
720
0
  else
721
0
    errorOrWarn(Msg);
722
3
}
Relocations.cpp:void reportUndefined<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
701
11
                            uint64_t Offset) {
702
11
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
11
705
11
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
11
                       Sym.getVisibility() == STV_DEFAULT;
707
11
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 11
CanBeExternal11
)
708
11
    return;
709
0
710
0
  std::string Msg =
711
0
      "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
712
0
713
0
  std::string Src = S.getSrcMsg<ELFT>(Offset);
714
0
  if (!Src.empty())
715
0
    Msg += Src + "\n>>>               ";
716
0
  Msg += S.getObjMsg<ELFT>(Offset);
717
0
718
0
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 0
CanBeExternal0
)
719
0
    warn(Msg);
720
0
  else
721
0
    errorOrWarn(Msg);
722
11
}
Relocations.cpp:void reportUndefined<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
701
9
                            uint64_t Offset) {
702
9
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
9
705
9
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
9
                       Sym.getVisibility() == STV_DEFAULT;
707
9
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 9
CanBeExternal9
)
708
9
    return;
709
0
710
0
  std::string Msg =
711
0
      "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
712
0
713
0
  std::string Src = S.getSrcMsg<ELFT>(Offset);
714
0
  if (!Src.empty())
715
0
    Msg += Src + "\n>>>               ";
716
0
  Msg += S.getObjMsg<ELFT>(Offset);
717
0
718
0
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 0
CanBeExternal0
)
719
0
    warn(Msg);
720
0
  else
721
0
    errorOrWarn(Msg);
722
9
}
Relocations.cpp:void reportUndefined<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SymbolBody&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
701
159
                            uint64_t Offset) {
702
159
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
159
705
159
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
156
                       Sym.getVisibility() == STV_DEFAULT;
707
159
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 159
CanBeExternal109
)
708
105
    return;
709
54
710
54
  std::string Msg =
711
54
      "undefined symbol: " + toString(Sym) + "\n>>> referenced by ";
712
54
713
54
  std::string Src = S.getSrcMsg<ELFT>(Offset);
714
54
  if (!Src.empty())
715
20
    Msg += Src + "\n>>>               ";
716
54
  Msg += S.getObjMsg<ELFT>(Offset);
717
54
718
54
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 54
CanBeExternal5
)
719
3
    warn(Msg);
720
54
  else
721
51
    errorOrWarn(Msg);
722
159
}
723
724
template <class RelTy>
725
static std::pair<uint32_t, uint32_t>
726
3
mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) {
727
3
  // MIPS N32 ABI treats series of successive relocations with the same offset
728
3
  // as a single relocation. The similar approach used by N64 ABI, but this ABI
729
3
  // packs all relocations into the single relocation record. Here we emulate
730
3
  // this for the N32 ABI. Iterate over relocation with the same offset and put
731
3
  // theirs types into the single bit-set.
732
3
  uint32_t Processed = 0;
733
7
  for (; 
I != E && 7
Offset == I->r_offset5
;
++I4
) {
734
4
    ++Processed;
735
4
    Type |= I->getType(Config->IsMips64EL) << (8 * Processed);
736
4
  }
737
3
  return std::make_pair(Type, Processed);
738
3
}
Unexecuted instantiation: Relocations.cpp:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const>(unsigned int, unsigned int, 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:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const>(unsigned int, unsigned int, 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*)
Unexecuted instantiation: Relocations.cpp:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const>(unsigned int, unsigned int, 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:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const>(unsigned int, unsigned int, 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*)
Relocations.cpp:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const>(unsigned int, unsigned int, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*)
Line
Count
Source
726
3
mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) {
727
3
  // MIPS N32 ABI treats series of successive relocations with the same offset
728
3
  // as a single relocation. The similar approach used by N64 ABI, but this ABI
729
3
  // packs all relocations into the single relocation record. Here we emulate
730
3
  // this for the N32 ABI. Iterate over relocation with the same offset and put
731
3
  // theirs types into the single bit-set.
732
3
  uint32_t Processed = 0;
733
7
  for (; 
I != E && 7
Offset == I->r_offset5
;
++I4
) {
734
4
    ++Processed;
735
4
    Type |= I->getType(Config->IsMips64EL) << (8 * Processed);
736
4
  }
737
3
  return std::make_pair(Type, Processed);
738
3
}
Unexecuted instantiation: Relocations.cpp:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const>(unsigned int, unsigned int, 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:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const>(unsigned int, unsigned int, 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:std::__1::pair<unsigned int, unsigned int> mergeMipsN32RelTypes<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const>(unsigned int, unsigned int, 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*)
739
740
// .eh_frame sections are mergeable input sections, so their input
741
// offsets are not linearly mapped to output section. For each input
742
// offset, we need to find a section piece containing the offset and
743
// add the piece's base address to the input offset to compute the
744
// output offset. That isn't cheap.
745
//
746
// This class is to speed up the offset computation. When we process
747
// relocations, we access offsets in the monotonically increasing
748
// order. So we can optimize for that access pattern.
749
//
750
// For sections other than .eh_frame, this class doesn't do anything.
751
namespace {
752
class OffsetGetter {
753
public:
754
90.1k
  explicit OffsetGetter(InputSectionBase &Sec) {
755
90.1k
    if (auto *
Eh90.1k
= dyn_cast<EhInputSection>(&Sec)) {
756
92
      P = Eh->Pieces;
757
92
      Size = Eh->Pieces.size();
758
92
    }
759
90.1k
  }
760
761
  // Translates offsets in input sections to offsets in output sections.
762
  // Given offset must increase monotonically. We assume that P is
763
  // sorted by InputOff.
764
1.97k
  uint64_t get(uint64_t Off) {
765
1.97k
    if (P.empty())
766
1.84k
      return Off;
767
124
768
237
    
while (124
I != Size && 237
P[I].InputOff + P[I].Size <= Off237
)
769
113
      ++I;
770
124
    if (I == Size)
771
0
      return Off;
772
124
773
124
    // P must be contiguous, so there must be no holes in between.
774
124
    assert(P[I].InputOff <= Off && "Relocation not in any piece");
775
124
776
124
    // Offset -1 means that the piece is dead (i.e. garbage collected).
777
124
    if (P[I].OutputOff == -1)
778
10
      return -1;
779
114
    return P[I].OutputOff + Off - P[I].InputOff;
780
114
  }
781
782
private:
783
  ArrayRef<EhSectionPiece> P;
784
  size_t I = 0;
785
  size_t Size;
786
};
787
} // namespace
788
789
template <class ELFT, class GotPltSection>
790
static void addPltEntry(PltSection *Plt, GotPltSection *GotPlt,
791
                        RelocationSection<ELFT> *Rel, uint32_t Type,
792
214
                        SymbolBody &Sym, bool UseSymVA) {
793
214
  Plt->addEntry<ELFT>(Sym);
794
214
  GotPlt->addEntry(Sym);
795
214
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
214
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >*, unsigned int, lld::elf::SymbolBody&, bool)
Line
Count
Source
792
12
                        SymbolBody &Sym, bool UseSymVA) {
793
12
  Plt->addEntry<ELFT>(Sym);
794
12
  GotPlt->addEntry(Sym);
795
12
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
12
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >*, unsigned int, lld::elf::SymbolBody&, bool)
Line
Count
Source
792
42
                        SymbolBody &Sym, bool UseSymVA) {
793
42
  Plt->addEntry<ELFT>(Sym);
794
42
  GotPlt->addEntry(Sym);
795
42
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
42
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >*, unsigned int, lld::elf::SymbolBody&, bool)
Line
Count
Source
792
8
                        SymbolBody &Sym, bool UseSymVA) {
793
8
  Plt->addEntry<ELFT>(Sym);
794
8
  GotPlt->addEntry(Sym);
795
8
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
8
}
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::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >*, unsigned int, lld::elf::SymbolBody&, bool)
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >*, unsigned int, lld::elf::SymbolBody&, bool)
Line
Count
Source
792
135
                        SymbolBody &Sym, bool UseSymVA) {
793
135
  Plt->addEntry<ELFT>(Sym);
794
135
  GotPlt->addEntry(Sym);
795
135
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
135
}
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::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >*, unsigned int, lld::elf::SymbolBody&, bool)
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >*, unsigned int, lld::elf::SymbolBody&, bool)
Line
Count
Source
792
15
                        SymbolBody &Sym, bool UseSymVA) {
793
15
  Plt->addEntry<ELFT>(Sym);
794
15
  GotPlt->addEntry(Sym);
795
15
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
15
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >*, unsigned int, lld::elf::SymbolBody&, bool)
Line
Count
Source
792
2
                        SymbolBody &Sym, bool UseSymVA) {
793
2
  Plt->addEntry<ELFT>(Sym);
794
2
  GotPlt->addEntry(Sym);
795
2
  Rel->addReloc({Type, GotPlt, Sym.getGotPltOffset(), UseSymVA, &Sym, 0});
796
2
}
797
798
template <class ELFT>
799
103
static void addGotEntry(SymbolBody &Sym, bool Preemptible) {
800
103
  InX::Got->addEntry(Sym);
801
103
802
103
  uint64_t Off = Sym.getGotOffset();
803
103
  uint32_t DynType;
804
103
  RelExpr Expr = R_ABS;
805
103
806
103
  if (
Sym.isTls()103
) {
807
21
    DynType = Target->TlsGotRel;
808
21
    Expr = R_TLS;
809
103
  } else 
if (82
!Preemptible && 82
Config->Pic20
&&
!isAbsolute(Sym)11
) {
810
9
    DynType = Target->RelativeRel;
811
82
  } else {
812
73
    DynType = Target->GotRel;
813
73
  }
814
103
815
25
  bool Constant = !Preemptible && 
!(Config->Pic && 25
!isAbsolute(Sym)14
);
816
103
  if (!Constant)
817
90
    In<ELFT>::RelaDyn->addReloc(
818
90
        {DynType, InX::Got, Off, !Preemptible, &Sym, 0});
819
103
820
103
  if (
Constant || 103
(!Config->IsRela && 90
!Preemptible28
))
821
18
    InX::Got->Relocations.push_back({Expr, DynType, Off, 0, &Sym});
822
103
}
Unexecuted instantiation: Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SymbolBody&, bool)
Unexecuted instantiation: Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SymbolBody&, bool)
Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SymbolBody&, bool)
Line
Count
Source
799
36
static void addGotEntry(SymbolBody &Sym, bool Preemptible) {
800
36
  InX::Got->addEntry(Sym);
801
36
802
36
  uint64_t Off = Sym.getGotOffset();
803
36
  uint32_t DynType;
804
36
  RelExpr Expr = R_ABS;
805
36
806
36
  if (
Sym.isTls()36
) {
807
14
    DynType = Target->TlsGotRel;
808
14
    Expr = R_TLS;
809
36
  } else 
if (22
!Preemptible && 22
Config->Pic10
&&
!isAbsolute(Sym)4
) {
810
4
    DynType = Target->RelativeRel;
811
22
  } else {
812
18
    DynType = Target->GotRel;
813
18
  }
814
36
815
13
  bool Constant = !Preemptible && 
!(Config->Pic && 13
!isAbsolute(Sym)5
);
816
36
  if (!Constant)
817
28
    In<ELFT>::RelaDyn->addReloc(
818
28
        {DynType, InX::Got, Off, !Preemptible, &Sym, 0});
819
36
820
36
  if (
Constant || 36
(!Config->IsRela && 28
!Preemptible28
))
821
13
    InX::Got->Relocations.push_back({Expr, DynType, Off, 0, &Sym});
822
36
}
Relocations.cpp:void addGotEntry<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SymbolBody&, bool)
Line
Count
Source
799
67
static void addGotEntry(SymbolBody &Sym, bool Preemptible) {
800
67
  InX::Got->addEntry(Sym);
801
67
802
67
  uint64_t Off = Sym.getGotOffset();
803
67
  uint32_t DynType;
804
67
  RelExpr Expr = R_ABS;
805
67
806
67
  if (
Sym.isTls()67
) {
807
7
    DynType = Target->TlsGotRel;
808
7
    Expr = R_TLS;
809
67
  } else 
if (60
!Preemptible && 60
Config->Pic10
&&
!isAbsolute(Sym)7
) {
810
5
    DynType = Target->RelativeRel;
811
60
  } else {
812
55
    DynType = Target->GotRel;
813
55
  }
814
67
815
12
  bool Constant = !Preemptible && 
!(Config->Pic && 12
!isAbsolute(Sym)9
);
816
67
  if (!Constant)
817
62
    In<ELFT>::RelaDyn->addReloc(
818
62
        {DynType, InX::Got, Off, !Preemptible, &Sym, 0});
819
67
820
67
  if (
Constant || 67
(!Config->IsRela && 62
!Preemptible0
))
821
5
    InX::Got->Relocations.push_back({Expr, DynType, Off, 0, &Sym});
822
67
}
823
824
// The reason we have to do this early scan is as follows
825
// * To mmap the output file, we need to know the size
826
// * For that, we need to know how many dynamic relocs we will have.
827
// It might be possible to avoid this by outputting the file with write:
828
// * Write the allocated output sections, computing addresses.
829
// * Apply relocations, recording which ones require a dynamic reloc.
830
// * Write the dynamic relocations.
831
// * Write the rest of the file.
832
// This would have some drawbacks. For example, we would only know if .rela.dyn
833
// is needed after applying relocations. If it is, it will go after rw and rx
834
// sections. Given that it is ro, we will need an extra PT_LOAD. This
835
// complicates things for the dynamic linker and means we would have to reserve
836
// space for the extra PT_LOAD even if we end up not using it.
837
template <class ELFT, class RelTy>
838
90.1k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
90.1k
  OffsetGetter GetOffset(Sec);
840
90.1k
841
92.1k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End92.1k
;
++I1.97k
) {
842
1.97k
    const RelTy &Rel = *I;
843
1.97k
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
1.97k
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
1.97k
846
1.97k
    if (
Config->MipsN32Abi1.97k
) {
847
3
      uint32_t Processed;
848
3
      std::tie(Type, Processed) =
849
3
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
3
      I += Processed;
851
3
    }
852
1.97k
853
1.97k
    // Compute the offset of this section in the output section.
854
1.97k
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
1.97k
    if (Offset == uint64_t(-1))
856
10
      continue;
857
1.96k
858
1.96k
    // Report undefined symbols. The fact that we report undefined
859
1.96k
    // symbols here means that we report undefined symbols only when
860
1.96k
    // they have relocations pointing to them. We don't care about
861
1.96k
    // undefined symbols that are in dead-stripped sections.
862
1.96k
    
if (1.96k
!Body.isLocal() && 1.96k
Body.isUndefined()1.42k
&&
!Body.symbol()->isWeak()240
)
863
182
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
1.96k
865
1.96k
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
1.96k
                                      Sec.Data.begin() + Rel.r_offset);
867
1.96k
868
1.96k
    // Ignore "hint" relocations because they are only markers for relaxation.
869
1.96k
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
13
      continue;
871
1.94k
872
1.94k
    bool Preemptible = isPreemptible(Body, Type);
873
1.94k
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
1.94k
                            Sec, Rel.r_offset);
875
1.94k
    if (ErrorCount)
876
80
      continue;
877
1.86k
878
1.86k
    // This relocation does not require got entry, but it is relative to got and
879
1.86k
    // needs it to be created. Here we request for that.
880
1.86k
    
if (1.86k
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
1.86k
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
24
      InX::Got->HasGotOffRel = true;
883
1.86k
884
1.86k
    // Read an addend.
885
1.86k
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
1.86k
    if (Config->EMachine == EM_MIPS)
887
288
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
1.86k
889
1.86k
    // Process some TLS relocations, including relaxing TLS relocations.
890
1.86k
    // Note that this function does not handle all TLS relocations.
891
1.86k
    if (unsigned Processed =
892
136
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
136
      I += (Processed - 1);
894
136
      continue;
895
136
    }
896
1.73k
897
1.73k
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
1.73k
    
if (1.73k
needsPlt(Expr) && 1.73k
!Body.isInPlt()251
) {
899
214
      if (
Body.isGnuIFunc() && 214
!Preemptible28
)
900
27
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
27
                    Target->IRelativeRel, Body, true);
902
214
      else
903
187
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
187
                    Body, !Preemptible);
905
214
    }
906
1.73k
907
1.73k
    // Create a GOT slot if a relocation needs GOT.
908
1.73k
    if (
needsGot(Expr)1.73k
) {
909
240
      if (
Config->EMachine == EM_MIPS240
) {
910
91
        // MIPS ABI has special rules to process GOT entries and doesn't
911
91
        // require relocation entries for them. A special case is TLS
912
91
        // relocations. In that case dynamic loader applies dynamic
913
91
        // relocations to initialize TLS GOT entries.
914
91
        // See "Global Offset Table" in Chapter 5 in the following document
915
91
        // for detailed description:
916
91
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
91
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
91
        if (
Body.isTls() && 91
Body.isPreemptible()9
)
919
6
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
6
                                       Body.getGotOffset(), false, &Body, 0});
921
240
      } else 
if (149
!Body.isInGot()149
) {
922
103
        addGotEntry<ELFT>(Body, Preemptible);
923
103
      }
924
240
    }
925
1.73k
926
1.73k
    if (
!needsPlt(Expr) && 1.73k
!needsGot(Expr)1.48k
&&
isPreemptible(Body, Type)1.24k
) {
927
110
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
110
      // linker to handle the relocation for us.
929
110
      if (!Target->isPicRel(Type))
930
12
        errorOrWarn(
931
12
            "relocation " + toString(Type) +
932
12
            " cannot be used against shared object; recompile with -fPIC" +
933
12
            getLocation<ELFT>(Sec, Body, Offset));
934
110
935
110
      In<ELFT>::RelaDyn->addReloc(
936
110
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
110
938
110
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
110
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
110
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
110
      // specially sorted dynamic symbol table. This affects even dynamic
942
110
      // relocations against symbols which do not require GOT entries
943
110
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
110
      // a preemptible symbol has a dynamic relocation we anyway have
945
110
      // to create a GOT entry for it.
946
110
      // If a non-preemptible symbol has a dynamic relocation against it,
947
110
      // dynamic linker takes it st_value, adds offset and writes down
948
110
      // result of the dynamic relocation. In case of preemptible symbol
949
110
      // dynamic linker performs symbol resolution, writes the symbol value
950
110
      // to the GOT entry and reads the GOT entry when it needs to perform
951
110
      // a dynamic relocation.
952
110
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
110
      if (Config->EMachine == EM_MIPS)
954
3
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
110
      continue;
956
110
    }
957
1.62k
958
1.62k
    // If the relocation points to something in the file, we can process it.
959
1.62k
    bool IsConstant =
960
1.62k
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
1.62k
962
1.62k
    // The size is not going to change, so we fold it in here.
963
1.62k
    if (Expr == R_SIZE)
964
24
      Addend += Body.getSize<ELFT>();
965
1.62k
966
1.62k
    // If the output being produced is position independent, the final value
967
1.62k
    // is still not known. In that case we still need some help from the
968
1.62k
    // dynamic linker. We can however do better than just copying the incoming
969
1.62k
    // relocation. We can process some of it and and just ask the dynamic
970
1.62k
    // linker to add the load address.
971
1.62k
    if (!IsConstant)
972
48
      In<ELFT>::RelaDyn->addReloc(
973
48
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
1.62k
975
1.62k
    // If the produced value is a constant, we just remember to write it
976
1.62k
    // when outputting this section. We also have to do it if the format
977
1.62k
    // uses Elf_Rel, since in that case the written value is the addend.
978
1.62k
    if (
IsConstant || 1.62k
!RelTy::IsRela14
)
979
1.58k
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
1.97k
  }
981
90.1k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
Line
Count
Source
838
7
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
7
  OffsetGetter GetOffset(Sec);
840
7
841
15
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End15
;
++I8
) {
842
8
    const RelTy &Rel = *I;
843
8
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
8
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
8
846
8
    if (
Config->MipsN32Abi8
) {
847
3
      uint32_t Processed;
848
3
      std::tie(Type, Processed) =
849
3
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
3
      I += Processed;
851
3
    }
852
8
853
8
    // Compute the offset of this section in the output section.
854
8
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
8
    if (Offset == uint64_t(-1))
856
0
      continue;
857
8
858
8
    // Report undefined symbols. The fact that we report undefined
859
8
    // symbols here means that we report undefined symbols only when
860
8
    // they have relocations pointing to them. We don't care about
861
8
    // undefined symbols that are in dead-stripped sections.
862
8
    
if (8
!Body.isLocal() && 8
Body.isUndefined()2
&&
!Body.symbol()->isWeak()0
)
863
0
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
8
865
8
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
8
                                      Sec.Data.begin() + Rel.r_offset);
867
8
868
8
    // Ignore "hint" relocations because they are only markers for relaxation.
869
8
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
0
      continue;
871
8
872
8
    bool Preemptible = isPreemptible(Body, Type);
873
8
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
8
                            Sec, Rel.r_offset);
875
8
    if (ErrorCount)
876
0
      continue;
877
8
878
8
    // This relocation does not require got entry, but it is relative to got and
879
8
    // needs it to be created. Here we request for that.
880
8
    
if (8
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
8
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
8
884
8
    // Read an addend.
885
8
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
8
    if (Config->EMachine == EM_MIPS)
887
3
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
8
889
8
    // Process some TLS relocations, including relaxing TLS relocations.
890
8
    // Note that this function does not handle all TLS relocations.
891
8
    if (unsigned Processed =
892
0
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
0
      I += (Processed - 1);
894
0
      continue;
895
0
    }
896
8
897
8
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
8
    
if (8
needsPlt(Expr) && 8
!Body.isInPlt()0
) {
899
0
      if (
Body.isGnuIFunc() && 0
!Preemptible0
)
900
0
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
0
                    Target->IRelativeRel, Body, true);
902
0
      else
903
0
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
0
                    Body, !Preemptible);
905
0
    }
906
8
907
8
    // Create a GOT slot if a relocation needs GOT.
908
8
    if (
needsGot(Expr)8
) {
909
0
      if (
Config->EMachine == EM_MIPS0
) {
910
0
        // MIPS ABI has special rules to process GOT entries and doesn't
911
0
        // require relocation entries for them. A special case is TLS
912
0
        // relocations. In that case dynamic loader applies dynamic
913
0
        // relocations to initialize TLS GOT entries.
914
0
        // See "Global Offset Table" in Chapter 5 in the following document
915
0
        // for detailed description:
916
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
0
        if (
Body.isTls() && 0
Body.isPreemptible()0
)
919
0
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
0
                                       Body.getGotOffset(), false, &Body, 0});
921
0
      } else 
if (0
!Body.isInGot()0
) {
922
0
        addGotEntry<ELFT>(Body, Preemptible);
923
0
      }
924
0
    }
925
8
926
8
    if (
!needsPlt(Expr) && 8
!needsGot(Expr)8
&&
isPreemptible(Body, Type)8
) {
927
0
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
0
      // linker to handle the relocation for us.
929
0
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
0
935
0
      In<ELFT>::RelaDyn->addReloc(
936
0
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
0
938
0
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
0
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
0
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
0
      // specially sorted dynamic symbol table. This affects even dynamic
942
0
      // relocations against symbols which do not require GOT entries
943
0
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
0
      // a preemptible symbol has a dynamic relocation we anyway have
945
0
      // to create a GOT entry for it.
946
0
      // If a non-preemptible symbol has a dynamic relocation against it,
947
0
      // dynamic linker takes it st_value, adds offset and writes down
948
0
      // result of the dynamic relocation. In case of preemptible symbol
949
0
      // dynamic linker performs symbol resolution, writes the symbol value
950
0
      // to the GOT entry and reads the GOT entry when it needs to perform
951
0
      // a dynamic relocation.
952
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
0
      if (Config->EMachine == EM_MIPS)
954
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
0
      continue;
956
0
    }
957
8
958
8
    // If the relocation points to something in the file, we can process it.
959
8
    bool IsConstant =
960
8
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
8
962
8
    // The size is not going to change, so we fold it in here.
963
8
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
8
966
8
    // If the output being produced is position independent, the final value
967
8
    // is still not known. In that case we still need some help from the
968
8
    // dynamic linker. We can however do better than just copying the incoming
969
8
    // relocation. We can process some of it and and just ask the dynamic
970
8
    // linker to add the load address.
971
8
    if (!IsConstant)
972
0
      In<ELFT>::RelaDyn->addReloc(
973
0
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
8
975
8
    // If the produced value is a constant, we just remember to write it
976
8
    // when outputting this section. We also have to do it if the format
977
8
    // uses Elf_Rel, since in that case the written value is the addend.
978
8
    if (
IsConstant || 8
!RelTy::IsRela0
)
979
8
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
8
  }
981
7
}
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
838
83.8k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
83.8k
  OffsetGetter GetOffset(Sec);
840
83.8k
841
83.8k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End83.8k
;
++I2
) {
842
2
    const RelTy &Rel = *I;
843
2
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
2
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
2
846
2
    if (
Config->MipsN32Abi2
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
2
853
2
    // Compute the offset of this section in the output section.
854
2
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
2
    if (Offset == uint64_t(-1))
856
0
      continue;
857
2
858
2
    // Report undefined symbols. The fact that we report undefined
859
2
    // symbols here means that we report undefined symbols only when
860
2
    // they have relocations pointing to them. We don't care about
861
2
    // undefined symbols that are in dead-stripped sections.
862
2
    
if (2
!Body.isLocal() && 2
Body.isUndefined()2
&&
!Body.symbol()->isWeak()0
)
863
0
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
2
865
2
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
2
                                      Sec.Data.begin() + Rel.r_offset);
867
2
868
2
    // Ignore "hint" relocations because they are only markers for relaxation.
869
2
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
0
      continue;
871
2
872
2
    bool Preemptible = isPreemptible(Body, Type);
873
2
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
2
                            Sec, Rel.r_offset);
875
2
    if (ErrorCount)
876
0
      continue;
877
2
878
2
    // This relocation does not require got entry, but it is relative to got and
879
2
    // needs it to be created. Here we request for that.
880
2
    
if (2
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
2
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
2
884
2
    // Read an addend.
885
2
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
2
    if (Config->EMachine == EM_MIPS)
887
0
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
2
889
2
    // Process some TLS relocations, including relaxing TLS relocations.
890
2
    // Note that this function does not handle all TLS relocations.
891
2
    if (unsigned Processed =
892
2
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
2
      I += (Processed - 1);
894
2
      continue;
895
2
    }
896
0
897
0
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
0
    
if (0
needsPlt(Expr) && 0
!Body.isInPlt()0
) {
899
0
      if (
Body.isGnuIFunc() && 0
!Preemptible0
)
900
0
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
0
                    Target->IRelativeRel, Body, true);
902
0
      else
903
0
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
0
                    Body, !Preemptible);
905
0
    }
906
0
907
0
    // Create a GOT slot if a relocation needs GOT.
908
0
    if (
needsGot(Expr)0
) {
909
0
      if (
Config->EMachine == EM_MIPS0
) {
910
0
        // MIPS ABI has special rules to process GOT entries and doesn't
911
0
        // require relocation entries for them. A special case is TLS
912
0
        // relocations. In that case dynamic loader applies dynamic
913
0
        // relocations to initialize TLS GOT entries.
914
0
        // See "Global Offset Table" in Chapter 5 in the following document
915
0
        // for detailed description:
916
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
0
        if (
Body.isTls() && 0
Body.isPreemptible()0
)
919
0
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
0
                                       Body.getGotOffset(), false, &Body, 0});
921
0
      } else 
if (0
!Body.isInGot()0
) {
922
0
        addGotEntry<ELFT>(Body, Preemptible);
923
0
      }
924
0
    }
925
0
926
0
    if (
!needsPlt(Expr) && 0
!needsGot(Expr)0
&&
isPreemptible(Body, Type)0
) {
927
0
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
0
      // linker to handle the relocation for us.
929
0
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
0
935
0
      In<ELFT>::RelaDyn->addReloc(
936
0
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
0
938
0
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
0
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
0
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
0
      // specially sorted dynamic symbol table. This affects even dynamic
942
0
      // relocations against symbols which do not require GOT entries
943
0
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
0
      // a preemptible symbol has a dynamic relocation we anyway have
945
0
      // to create a GOT entry for it.
946
0
      // If a non-preemptible symbol has a dynamic relocation against it,
947
0
      // dynamic linker takes it st_value, adds offset and writes down
948
0
      // result of the dynamic relocation. In case of preemptible symbol
949
0
      // dynamic linker performs symbol resolution, writes the symbol value
950
0
      // to the GOT entry and reads the GOT entry when it needs to perform
951
0
      // a dynamic relocation.
952
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
0
      if (Config->EMachine == EM_MIPS)
954
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
0
      continue;
956
0
    }
957
0
958
0
    // If the relocation points to something in the file, we can process it.
959
0
    bool IsConstant =
960
0
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
0
962
0
    // The size is not going to change, so we fold it in here.
963
0
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
0
966
0
    // If the output being produced is position independent, the final value
967
0
    // is still not known. In that case we still need some help from the
968
0
    // dynamic linker. We can however do better than just copying the incoming
969
0
    // relocation. We can process some of it and and just ask the dynamic
970
0
    // linker to add the load address.
971
0
    if (!IsConstant)
972
0
      In<ELFT>::RelaDyn->addReloc(
973
0
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
0
975
0
    // If the produced value is a constant, we just remember to write it
976
0
    // when outputting this section. We also have to do it if the format
977
0
    // uses Elf_Rel, since in that case the written value is the addend.
978
0
    if (
IsConstant || 0
!RelTy::IsRela0
)
979
0
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
2
  }
981
83.8k
}
Relocations.cpp:void scanRelocs<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::InputSectionBase&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
Line
Count
Source
838
2
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
2
  OffsetGetter GetOffset(Sec);
840
2
841
4
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End4
;
++I2
) {
842
2
    const RelTy &Rel = *I;
843
2
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
2
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
2
846
2
    if (
Config->MipsN32Abi2
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
2
853
2
    // Compute the offset of this section in the output section.
854
2
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
2
    if (Offset == uint64_t(-1))
856
0
      continue;
857
2
858
2
    // Report undefined symbols. The fact that we report undefined
859
2
    // symbols here means that we report undefined symbols only when
860
2
    // they have relocations pointing to them. We don't care about
861
2
    // undefined symbols that are in dead-stripped sections.
862
2
    
if (2
!Body.isLocal() && 2
Body.isUndefined()1
&&
!Body.symbol()->isWeak()1
)
863
1
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
2
865
2
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
2
                                      Sec.Data.begin() + Rel.r_offset);
867
2
868
2
    // Ignore "hint" relocations because they are only markers for relaxation.
869
2
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
1
      continue;
871
1
872
1
    bool Preemptible = isPreemptible(Body, Type);
873
1
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
1
                            Sec, Rel.r_offset);
875
1
    if (ErrorCount)
876
0
      continue;
877
1
878
1
    // This relocation does not require got entry, but it is relative to got and
879
1
    // needs it to be created. Here we request for that.
880
1
    
if (1
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
1
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
1
884
1
    // Read an addend.
885
1
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
1
    if (Config->EMachine == EM_MIPS)
887
0
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
1
889
1
    // Process some TLS relocations, including relaxing TLS relocations.
890
1
    // Note that this function does not handle all TLS relocations.
891
1
    if (unsigned Processed =
892
0
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
0
      I += (Processed - 1);
894
0
      continue;
895
0
    }
896
1
897
1
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
1
    
if (1
needsPlt(Expr) && 1
!Body.isInPlt()1
) {
899
1
      if (
Body.isGnuIFunc() && 1
!Preemptible0
)
900
0
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
0
                    Target->IRelativeRel, Body, true);
902
1
      else
903
1
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
1
                    Body, !Preemptible);
905
1
    }
906
1
907
1
    // Create a GOT slot if a relocation needs GOT.
908
1
    if (
needsGot(Expr)1
) {
909
0
      if (
Config->EMachine == EM_MIPS0
) {
910
0
        // MIPS ABI has special rules to process GOT entries and doesn't
911
0
        // require relocation entries for them. A special case is TLS
912
0
        // relocations. In that case dynamic loader applies dynamic
913
0
        // relocations to initialize TLS GOT entries.
914
0
        // See "Global Offset Table" in Chapter 5 in the following document
915
0
        // for detailed description:
916
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
0
        if (
Body.isTls() && 0
Body.isPreemptible()0
)
919
0
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
0
                                       Body.getGotOffset(), false, &Body, 0});
921
0
      } else 
if (0
!Body.isInGot()0
) {
922
0
        addGotEntry<ELFT>(Body, Preemptible);
923
0
      }
924
0
    }
925
1
926
1
    if (
!needsPlt(Expr) && 1
!needsGot(Expr)0
&&
isPreemptible(Body, Type)0
) {
927
0
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
0
      // linker to handle the relocation for us.
929
0
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
0
935
0
      In<ELFT>::RelaDyn->addReloc(
936
0
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
0
938
0
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
0
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
0
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
0
      // specially sorted dynamic symbol table. This affects even dynamic
942
0
      // relocations against symbols which do not require GOT entries
943
0
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
0
      // a preemptible symbol has a dynamic relocation we anyway have
945
0
      // to create a GOT entry for it.
946
0
      // If a non-preemptible symbol has a dynamic relocation against it,
947
0
      // dynamic linker takes it st_value, adds offset and writes down
948
0
      // result of the dynamic relocation. In case of preemptible symbol
949
0
      // dynamic linker performs symbol resolution, writes the symbol value
950
0
      // to the GOT entry and reads the GOT entry when it needs to perform
951
0
      // a dynamic relocation.
952
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
0
      if (Config->EMachine == EM_MIPS)
954
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
0
      continue;
956
0
    }
957
1
958
1
    // If the relocation points to something in the file, we can process it.
959
1
    bool IsConstant =
960
1
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
1
962
1
    // The size is not going to change, so we fold it in here.
963
1
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
1
966
1
    // If the output being produced is position independent, the final value
967
1
    // is still not known. In that case we still need some help from the
968
1
    // dynamic linker. We can however do better than just copying the incoming
969
1
    // relocation. We can process some of it and and just ask the dynamic
970
1
    // linker to add the load address.
971
1
    if (!IsConstant)
972
0
      In<ELFT>::RelaDyn->addReloc(
973
0
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
1
975
1
    // If the produced value is a constant, we just remember to write it
976
1
    // when outputting this section. We also have to do it if the format
977
1
    // uses Elf_Rel, since in that case the written value is the addend.
978
1
    if (
IsConstant || 1
!RelTy::IsRela0
)
979
1
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
2
  }
981
2
}
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
838
3.07k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
3.07k
  OffsetGetter GetOffset(Sec);
840
3.07k
841
3.57k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End3.57k
;
++I501
) {
842
501
    const RelTy &Rel = *I;
843
501
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
501
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
501
846
501
    if (
Config->MipsN32Abi501
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
501
853
501
    // Compute the offset of this section in the output section.
854
501
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
501
    if (Offset == uint64_t(-1))
856
0
      continue;
857
501
858
501
    // Report undefined symbols. The fact that we report undefined
859
501
    // symbols here means that we report undefined symbols only when
860
501
    // they have relocations pointing to them. We don't care about
861
501
    // undefined symbols that are in dead-stripped sections.
862
501
    
if (501
!Body.isLocal() && 501
Body.isUndefined()342
&&
!Body.symbol()->isWeak()26
)
863
10
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
501
865
501
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
501
                                      Sec.Data.begin() + Rel.r_offset);
867
501
868
501
    // Ignore "hint" relocations because they are only markers for relaxation.
869
501
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
8
      continue;
871
493
872
493
    bool Preemptible = isPreemptible(Body, Type);
873
493
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
493
                            Sec, Rel.r_offset);
875
493
    if (ErrorCount)
876
7
      continue;
877
486
878
486
    // This relocation does not require got entry, but it is relative to got and
879
486
    // needs it to be created. Here we request for that.
880
486
    
if (486
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
486
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
13
      InX::Got->HasGotOffRel = true;
883
486
884
486
    // Read an addend.
885
486
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
486
    if (Config->EMachine == EM_MIPS)
887
15
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
486
889
486
    // Process some TLS relocations, including relaxing TLS relocations.
890
486
    // Note that this function does not handle all TLS relocations.
891
486
    if (unsigned Processed =
892
37
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
37
      I += (Processed - 1);
894
37
      continue;
895
37
    }
896
449
897
449
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
449
    
if (449
needsPlt(Expr) && 449
!Body.isInPlt()70
) {
899
53
      if (
Body.isGnuIFunc() && 53
!Preemptible12
)
900
12
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
12
                    Target->IRelativeRel, Body, true);
902
53
      else
903
41
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
41
                    Body, !Preemptible);
905
53
    }
906
449
907
449
    // Create a GOT slot if a relocation needs GOT.
908
449
    if (
needsGot(Expr)449
) {
909
53
      if (
Config->EMachine == EM_MIPS53
) {
910
6
        // MIPS ABI has special rules to process GOT entries and doesn't
911
6
        // require relocation entries for them. A special case is TLS
912
6
        // relocations. In that case dynamic loader applies dynamic
913
6
        // relocations to initialize TLS GOT entries.
914
6
        // See "Global Offset Table" in Chapter 5 in the following document
915
6
        // for detailed description:
916
6
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
6
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
6
        if (
Body.isTls() && 6
Body.isPreemptible()0
)
919
0
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
0
                                       Body.getGotOffset(), false, &Body, 0});
921
53
      } else 
if (47
!Body.isInGot()47
) {
922
36
        addGotEntry<ELFT>(Body, Preemptible);
923
36
      }
924
53
    }
925
449
926
449
    if (
!needsPlt(Expr) && 449
!needsGot(Expr)379
&&
isPreemptible(Body, Type)326
) {
927
22
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
22
      // linker to handle the relocation for us.
929
22
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
22
935
22
      In<ELFT>::RelaDyn->addReloc(
936
22
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
22
938
22
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
22
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
22
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
22
      // specially sorted dynamic symbol table. This affects even dynamic
942
22
      // relocations against symbols which do not require GOT entries
943
22
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
22
      // a preemptible symbol has a dynamic relocation we anyway have
945
22
      // to create a GOT entry for it.
946
22
      // If a non-preemptible symbol has a dynamic relocation against it,
947
22
      // dynamic linker takes it st_value, adds offset and writes down
948
22
      // result of the dynamic relocation. In case of preemptible symbol
949
22
      // dynamic linker performs symbol resolution, writes the symbol value
950
22
      // to the GOT entry and reads the GOT entry when it needs to perform
951
22
      // a dynamic relocation.
952
22
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
22
      if (Config->EMachine == EM_MIPS)
954
1
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
22
      continue;
956
22
    }
957
427
958
427
    // If the relocation points to something in the file, we can process it.
959
427
    bool IsConstant =
960
427
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
427
962
427
    // The size is not going to change, so we fold it in here.
963
427
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
427
966
427
    // If the output being produced is position independent, the final value
967
427
    // is still not known. In that case we still need some help from the
968
427
    // dynamic linker. We can however do better than just copying the incoming
969
427
    // relocation. We can process some of it and and just ask the dynamic
970
427
    // linker to add the load address.
971
427
    if (!IsConstant)
972
13
      In<ELFT>::RelaDyn->addReloc(
973
13
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
427
975
427
    // If the produced value is a constant, we just remember to write it
976
427
    // when outputting this section. We also have to do it if the format
977
427
    // uses Elf_Rel, since in that case the written value is the addend.
978
427
    if (
IsConstant || 427
!RelTy::IsRela13
)
979
427
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
501
  }
981
3.07k
}
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
838
785
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
785
  OffsetGetter GetOffset(Sec);
840
785
841
785
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End785
;
++I0
) {
842
0
    const RelTy &Rel = *I;
843
0
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
0
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
0
846
0
    if (
Config->MipsN32Abi0
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
0
853
0
    // Compute the offset of this section in the output section.
854
0
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
0
    if (Offset == uint64_t(-1))
856
0
      continue;
857
0
858
0
    // Report undefined symbols. The fact that we report undefined
859
0
    // symbols here means that we report undefined symbols only when
860
0
    // they have relocations pointing to them. We don't care about
861
0
    // undefined symbols that are in dead-stripped sections.
862
0
    
if (0
!Body.isLocal() && 0
Body.isUndefined()0
&&
!Body.symbol()->isWeak()0
)
863
0
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
0
865
0
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
0
                                      Sec.Data.begin() + Rel.r_offset);
867
0
868
0
    // Ignore "hint" relocations because they are only markers for relaxation.
869
0
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
0
      continue;
871
0
872
0
    bool Preemptible = isPreemptible(Body, Type);
873
0
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
0
                            Sec, Rel.r_offset);
875
0
    if (ErrorCount)
876
0
      continue;
877
0
878
0
    // This relocation does not require got entry, but it is relative to got and
879
0
    // needs it to be created. Here we request for that.
880
0
    
if (0
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
0
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
0
884
0
    // Read an addend.
885
0
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
0
    if (Config->EMachine == EM_MIPS)
887
0
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
0
889
0
    // Process some TLS relocations, including relaxing TLS relocations.
890
0
    // Note that this function does not handle all TLS relocations.
891
0
    if (unsigned Processed =
892
0
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
0
      I += (Processed - 1);
894
0
      continue;
895
0
    }
896
0
897
0
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
0
    
if (0
needsPlt(Expr) && 0
!Body.isInPlt()0
) {
899
0
      if (
Body.isGnuIFunc() && 0
!Preemptible0
)
900
0
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
0
                    Target->IRelativeRel, Body, true);
902
0
      else
903
0
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
0
                    Body, !Preemptible);
905
0
    }
906
0
907
0
    // Create a GOT slot if a relocation needs GOT.
908
0
    if (
needsGot(Expr)0
) {
909
0
      if (
Config->EMachine == EM_MIPS0
) {
910
0
        // MIPS ABI has special rules to process GOT entries and doesn't
911
0
        // require relocation entries for them. A special case is TLS
912
0
        // relocations. In that case dynamic loader applies dynamic
913
0
        // relocations to initialize TLS GOT entries.
914
0
        // See "Global Offset Table" in Chapter 5 in the following document
915
0
        // for detailed description:
916
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
0
        if (
Body.isTls() && 0
Body.isPreemptible()0
)
919
0
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
0
                                       Body.getGotOffset(), false, &Body, 0});
921
0
      } else 
if (0
!Body.isInGot()0
) {
922
0
        addGotEntry<ELFT>(Body, Preemptible);
923
0
      }
924
0
    }
925
0
926
0
    if (
!needsPlt(Expr) && 0
!needsGot(Expr)0
&&
isPreemptible(Body, Type)0
) {
927
0
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
0
      // linker to handle the relocation for us.
929
0
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
0
935
0
      In<ELFT>::RelaDyn->addReloc(
936
0
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
0
938
0
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
0
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
0
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
0
      // specially sorted dynamic symbol table. This affects even dynamic
942
0
      // relocations against symbols which do not require GOT entries
943
0
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
0
      // a preemptible symbol has a dynamic relocation we anyway have
945
0
      // to create a GOT entry for it.
946
0
      // If a non-preemptible symbol has a dynamic relocation against it,
947
0
      // dynamic linker takes it st_value, adds offset and writes down
948
0
      // result of the dynamic relocation. In case of preemptible symbol
949
0
      // dynamic linker performs symbol resolution, writes the symbol value
950
0
      // to the GOT entry and reads the GOT entry when it needs to perform
951
0
      // a dynamic relocation.
952
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
0
      if (Config->EMachine == EM_MIPS)
954
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
0
      continue;
956
0
    }
957
0
958
0
    // If the relocation points to something in the file, we can process it.
959
0
    bool IsConstant =
960
0
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
0
962
0
    // The size is not going to change, so we fold it in here.
963
0
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
0
966
0
    // If the output being produced is position independent, the final value
967
0
    // is still not known. In that case we still need some help from the
968
0
    // dynamic linker. We can however do better than just copying the incoming
969
0
    // relocation. We can process some of it and and just ask the dynamic
970
0
    // linker to add the load address.
971
0
    if (!IsConstant)
972
0
      In<ELFT>::RelaDyn->addReloc(
973
0
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
0
975
0
    // If the produced value is a constant, we just remember to write it
976
0
    // when outputting this section. We also have to do it if the format
977
0
    // uses Elf_Rel, since in that case the written value is the addend.
978
0
    if (
IsConstant || 0
!RelTy::IsRela0
)
979
0
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
0
  }
981
785
}
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
838
46
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
46
  OffsetGetter GetOffset(Sec);
840
46
841
142
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End142
;
++I96
) {
842
96
    const RelTy &Rel = *I;
843
96
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
96
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
96
846
96
    if (
Config->MipsN32Abi96
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
96
853
96
    // Compute the offset of this section in the output section.
854
96
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
96
    if (Offset == uint64_t(-1))
856
0
      continue;
857
96
858
96
    // Report undefined symbols. The fact that we report undefined
859
96
    // symbols here means that we report undefined symbols only when
860
96
    // they have relocations pointing to them. We don't care about
861
96
    // undefined symbols that are in dead-stripped sections.
862
96
    
if (96
!Body.isLocal() && 96
Body.isUndefined()50
&&
!Body.symbol()->isWeak()6
)
863
3
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
96
865
96
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
96
                                      Sec.Data.begin() + Rel.r_offset);
867
96
868
96
    // Ignore "hint" relocations because they are only markers for relaxation.
869
96
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
0
      continue;
871
96
872
96
    bool Preemptible = isPreemptible(Body, Type);
873
96
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
96
                            Sec, Rel.r_offset);
875
96
    if (ErrorCount)
876
0
      continue;
877
96
878
96
    // This relocation does not require got entry, but it is relative to got and
879
96
    // needs it to be created. Here we request for that.
880
96
    
if (96
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
96
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
11
      InX::Got->HasGotOffRel = true;
883
96
884
96
    // Read an addend.
885
96
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
96
    if (Config->EMachine == EM_MIPS)
887
51
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
96
889
96
    // Process some TLS relocations, including relaxing TLS relocations.
890
96
    // Note that this function does not handle all TLS relocations.
891
96
    if (unsigned Processed =
892
6
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
6
      I += (Processed - 1);
894
6
      continue;
895
6
    }
896
90
897
90
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
90
    
if (90
needsPlt(Expr) && 90
!Body.isInPlt()6
) {
899
2
      if (
Body.isGnuIFunc() && 2
!Preemptible0
)
900
0
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
0
                    Target->IRelativeRel, Body, true);
902
2
      else
903
2
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
2
                    Body, !Preemptible);
905
2
    }
906
90
907
90
    // Create a GOT slot if a relocation needs GOT.
908
90
    if (
needsGot(Expr)90
) {
909
19
      if (
Config->EMachine == EM_MIPS19
) {
910
19
        // MIPS ABI has special rules to process GOT entries and doesn't
911
19
        // require relocation entries for them. A special case is TLS
912
19
        // relocations. In that case dynamic loader applies dynamic
913
19
        // relocations to initialize TLS GOT entries.
914
19
        // See "Global Offset Table" in Chapter 5 in the following document
915
19
        // for detailed description:
916
19
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
19
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
19
        if (
Body.isTls() && 19
Body.isPreemptible()4
)
919
3
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
3
                                       Body.getGotOffset(), false, &Body, 0});
921
0
      } else 
if (0
!Body.isInGot()0
) {
922
0
        addGotEntry<ELFT>(Body, Preemptible);
923
0
      }
924
19
    }
925
90
926
90
    if (
!needsPlt(Expr) && 90
!needsGot(Expr)84
&&
isPreemptible(Body, Type)65
) {
927
5
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
5
      // linker to handle the relocation for us.
929
5
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
5
935
5
      In<ELFT>::RelaDyn->addReloc(
936
5
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
5
938
5
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
5
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
5
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
5
      // specially sorted dynamic symbol table. This affects even dynamic
942
5
      // relocations against symbols which do not require GOT entries
943
5
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
5
      // a preemptible symbol has a dynamic relocation we anyway have
945
5
      // to create a GOT entry for it.
946
5
      // If a non-preemptible symbol has a dynamic relocation against it,
947
5
      // dynamic linker takes it st_value, adds offset and writes down
948
5
      // result of the dynamic relocation. In case of preemptible symbol
949
5
      // dynamic linker performs symbol resolution, writes the symbol value
950
5
      // to the GOT entry and reads the GOT entry when it needs to perform
951
5
      // a dynamic relocation.
952
5
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
5
      if (Config->EMachine == EM_MIPS)
954
1
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
5
      continue;
956
5
    }
957
85
958
85
    // If the relocation points to something in the file, we can process it.
959
85
    bool IsConstant =
960
85
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
85
962
85
    // The size is not going to change, so we fold it in here.
963
85
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
85
966
85
    // If the output being produced is position independent, the final value
967
85
    // is still not known. In that case we still need some help from the
968
85
    // dynamic linker. We can however do better than just copying the incoming
969
85
    // relocation. We can process some of it and and just ask the dynamic
970
85
    // linker to add the load address.
971
85
    if (!IsConstant)
972
13
      In<ELFT>::RelaDyn->addReloc(
973
13
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
85
975
85
    // If the produced value is a constant, we just remember to write it
976
85
    // when outputting this section. We also have to do it if the format
977
85
    // uses Elf_Rel, since in that case the written value is the addend.
978
85
    if (
IsConstant || 85
!RelTy::IsRela0
)
979
72
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
96
  }
981
46
}
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
838
1.76k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
1.76k
  OffsetGetter GetOffset(Sec);
840
1.76k
841
1.98k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End1.98k
;
++I219
) {
842
219
    const RelTy &Rel = *I;
843
219
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
219
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
219
846
219
    if (
Config->MipsN32Abi219
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
219
853
219
    // Compute the offset of this section in the output section.
854
219
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
219
    if (Offset == uint64_t(-1))
856
0
      continue;
857
219
858
219
    // Report undefined symbols. The fact that we report undefined
859
219
    // symbols here means that we report undefined symbols only when
860
219
    // they have relocations pointing to them. We don't care about
861
219
    // undefined symbols that are in dead-stripped sections.
862
219
    
if (219
!Body.isLocal() && 219
Body.isUndefined()154
&&
!Body.symbol()->isWeak()11
)
863
9
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
219
865
219
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
219
                                      Sec.Data.begin() + Rel.r_offset);
867
219
868
219
    // Ignore "hint" relocations because they are only markers for relaxation.
869
219
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
0
      continue;
871
219
872
219
    bool Preemptible = isPreemptible(Body, Type);
873
219
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
219
                            Sec, Rel.r_offset);
875
219
    if (ErrorCount)
876
0
      continue;
877
219
878
219
    // This relocation does not require got entry, but it is relative to got and
879
219
    // needs it to be created. Here we request for that.
880
219
    
if (219
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
219
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
219
884
219
    // Read an addend.
885
219
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
219
    if (Config->EMachine == EM_MIPS)
887
219
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
219
889
219
    // Process some TLS relocations, including relaxing TLS relocations.
890
219
    // Note that this function does not handle all TLS relocations.
891
219
    if (unsigned Processed =
892
8
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
8
      I += (Processed - 1);
894
8
      continue;
895
8
    }
896
211
897
211
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
211
    
if (211
needsPlt(Expr) && 211
!Body.isInPlt()8
) {
899
8
      if (
Body.isGnuIFunc() && 8
!Preemptible0
)
900
0
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
0
                    Target->IRelativeRel, Body, true);
902
8
      else
903
8
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
8
                    Body, !Preemptible);
905
8
    }
906
211
907
211
    // Create a GOT slot if a relocation needs GOT.
908
211
    if (
needsGot(Expr)211
) {
909
66
      if (
Config->EMachine == EM_MIPS66
) {
910
66
        // MIPS ABI has special rules to process GOT entries and doesn't
911
66
        // require relocation entries for them. A special case is TLS
912
66
        // relocations. In that case dynamic loader applies dynamic
913
66
        // relocations to initialize TLS GOT entries.
914
66
        // See "Global Offset Table" in Chapter 5 in the following document
915
66
        // for detailed description:
916
66
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
66
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
66
        if (
Body.isTls() && 66
Body.isPreemptible()5
)
919
3
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
3
                                       Body.getGotOffset(), false, &Body, 0});
921
0
      } else 
if (0
!Body.isInGot()0
) {
922
0
        addGotEntry<ELFT>(Body, Preemptible);
923
0
      }
924
66
    }
925
211
926
211
    if (
!needsPlt(Expr) && 211
!needsGot(Expr)203
&&
isPreemptible(Body, Type)137
) {
927
1
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
1
      // linker to handle the relocation for us.
929
1
      if (!Target->isPicRel(Type))
930
0
        errorOrWarn(
931
0
            "relocation " + toString(Type) +
932
0
            " cannot be used against shared object; recompile with -fPIC" +
933
0
            getLocation<ELFT>(Sec, Body, Offset));
934
1
935
1
      In<ELFT>::RelaDyn->addReloc(
936
1
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
1
938
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
1
      // specially sorted dynamic symbol table. This affects even dynamic
942
1
      // relocations against symbols which do not require GOT entries
943
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
1
      // a preemptible symbol has a dynamic relocation we anyway have
945
1
      // to create a GOT entry for it.
946
1
      // If a non-preemptible symbol has a dynamic relocation against it,
947
1
      // dynamic linker takes it st_value, adds offset and writes down
948
1
      // result of the dynamic relocation. In case of preemptible symbol
949
1
      // dynamic linker performs symbol resolution, writes the symbol value
950
1
      // to the GOT entry and reads the GOT entry when it needs to perform
951
1
      // a dynamic relocation.
952
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
1
      if (Config->EMachine == EM_MIPS)
954
1
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
1
      continue;
956
1
    }
957
210
958
210
    // If the relocation points to something in the file, we can process it.
959
210
    bool IsConstant =
960
210
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
210
962
210
    // The size is not going to change, so we fold it in here.
963
210
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
210
966
210
    // If the output being produced is position independent, the final value
967
210
    // is still not known. In that case we still need some help from the
968
210
    // dynamic linker. We can however do better than just copying the incoming
969
210
    // relocation. We can process some of it and and just ask the dynamic
970
210
    // linker to add the load address.
971
210
    if (!IsConstant)
972
1
      In<ELFT>::RelaDyn->addReloc(
973
1
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
210
975
210
    // If the produced value is a constant, we just remember to write it
976
210
    // when outputting this section. We also have to do it if the format
977
210
    // uses Elf_Rel, since in that case the written value is the addend.
978
210
    if (
IsConstant || 210
!RelTy::IsRela1
)
979
210
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
219
  }
981
1.76k
}
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
838
599
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
599
  OffsetGetter GetOffset(Sec);
840
599
841
1.74k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End1.74k
;
++I1.14k
) {
842
1.14k
    const RelTy &Rel = *I;
843
1.14k
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
1.14k
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
1.14k
846
1.14k
    if (
Config->MipsN32Abi1.14k
) {
847
0
      uint32_t Processed;
848
0
      std::tie(Type, Processed) =
849
0
          mergeMipsN32RelTypes(Type, Rel.r_offset, I + 1, End);
850
0
      I += Processed;
851
0
    }
852
1.14k
853
1.14k
    // Compute the offset of this section in the output section.
854
1.14k
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
1.14k
    if (Offset == uint64_t(-1))
856
10
      continue;
857
1.13k
858
1.13k
    // Report undefined symbols. The fact that we report undefined
859
1.13k
    // symbols here means that we report undefined symbols only when
860
1.13k
    // they have relocations pointing to them. We don't care about
861
1.13k
    // undefined symbols that are in dead-stripped sections.
862
1.13k
    
if (1.13k
!Body.isLocal() && 1.13k
Body.isUndefined()871
&&
!Body.symbol()->isWeak()196
)
863
159
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
1.13k
865
1.13k
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
1.13k
                                      Sec.Data.begin() + Rel.r_offset);
867
1.13k
868
1.13k
    // Ignore "hint" relocations because they are only markers for relaxation.
869
1.13k
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
4
      continue;
871
1.13k
872
1.13k
    bool Preemptible = isPreemptible(Body, Type);
873
1.13k
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
1.13k
                            Sec, Rel.r_offset);
875
1.13k
    if (ErrorCount)
876
73
      continue;
877
1.05k
878
1.05k
    // This relocation does not require got entry, but it is relative to got and
879
1.05k
    // needs it to be created. Here we request for that.
880
1.05k
    
if (1.05k
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
1.05k
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
1.05k
884
1.05k
    // Read an addend.
885
1.05k
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
1.05k
    if (Config->EMachine == EM_MIPS)
887
0
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
1.05k
889
1.05k
    // Process some TLS relocations, including relaxing TLS relocations.
890
1.05k
    // Note that this function does not handle all TLS relocations.
891
1.05k
    if (unsigned Processed =
892
83
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
83
      I += (Processed - 1);
894
83
      continue;
895
83
    }
896
974
897
974
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
974
    
if (974
needsPlt(Expr) && 974
!Body.isInPlt()166
) {
899
150
      if (
Body.isGnuIFunc() && 150
!Preemptible16
)
900
15
        addPltEntry(InX::Iplt, InX::IgotPlt, In<ELFT>::RelaIplt,
901
15
                    Target->IRelativeRel, Body, true);
902
150
      else
903
135
        addPltEntry(InX::Plt, InX::GotPlt, In<ELFT>::RelaPlt, Target->PltRel,
904
135
                    Body, !Preemptible);
905
150
    }
906
974
907
974
    // Create a GOT slot if a relocation needs GOT.
908
974
    if (
needsGot(Expr)974
) {
909
102
      if (
Config->EMachine == EM_MIPS102
) {
910
0
        // MIPS ABI has special rules to process GOT entries and doesn't
911
0
        // require relocation entries for them. A special case is TLS
912
0
        // relocations. In that case dynamic loader applies dynamic
913
0
        // relocations to initialize TLS GOT entries.
914
0
        // See "Global Offset Table" in Chapter 5 in the following document
915
0
        // for detailed description:
916
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
0
        if (
Body.isTls() && 0
Body.isPreemptible()0
)
919
0
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
0
                                       Body.getGotOffset(), false, &Body, 0});
921
102
      } else 
if (102
!Body.isInGot()102
) {
922
67
        addGotEntry<ELFT>(Body, Preemptible);
923
67
      }
924
102
    }
925
974
926
974
    if (
!needsPlt(Expr) && 974
!needsGot(Expr)808
&&
isPreemptible(Body, Type)706
) {
927
82
      // We don't know anything about the finaly symbol. Just ask the dynamic
928
82
      // linker to handle the relocation for us.
929
82
      if (!Target->isPicRel(Type))
930
12
        errorOrWarn(
931
12
            "relocation " + toString(Type) +
932
12
            " cannot be used against shared object; recompile with -fPIC" +
933
12
            getLocation<ELFT>(Sec, Body, Offset));
934
82
935
82
      In<ELFT>::RelaDyn->addReloc(
936
82
          {Target->getDynRel(Type), &Sec, Offset, false, &Body, Addend});
937
82
938
82
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
939
82
      // While regular ABI uses dynamic relocations to fill up GOT entries
940
82
      // MIPS ABI requires dynamic linker to fills up GOT entries using
941
82
      // specially sorted dynamic symbol table. This affects even dynamic
942
82
      // relocations against symbols which do not require GOT entries
943
82
      // creation explicitly, i.e. do not have any GOT-relocations. So if
944
82
      // a preemptible symbol has a dynamic relocation we anyway have
945
82
      // to create a GOT entry for it.
946
82
      // If a non-preemptible symbol has a dynamic relocation against it,
947
82
      // dynamic linker takes it st_value, adds offset and writes down
948
82
      // result of the dynamic relocation. In case of preemptible symbol
949
82
      // dynamic linker performs symbol resolution, writes the symbol value
950
82
      // to the GOT entry and reads the GOT entry when it needs to perform
951
82
      // a dynamic relocation.
952
82
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
953
82
      if (Config->EMachine == EM_MIPS)
954
0
        InX::MipsGot->addEntry(Body, Addend, Expr);
955
82
      continue;
956
82
    }
957
892
958
892
    // If the relocation points to something in the file, we can process it.
959
892
    bool IsConstant =
960
892
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
892
962
892
    // The size is not going to change, so we fold it in here.
963
892
    if (Expr == R_SIZE)
964
24
      Addend += Body.getSize<ELFT>();
965
892
966
892
    // If the output being produced is position independent, the final value
967
892
    // is still not known. In that case we still need some help from the
968
892
    // dynamic linker. We can however do better than just copying the incoming
969
892
    // relocation. We can process some of it and and just ask the dynamic
970
892
    // linker to add the load address.
971
892
    if (!IsConstant)
972
21
      In<ELFT>::RelaDyn->addReloc(
973
21
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
892
975
892
    // If the produced value is a constant, we just remember to write it
976
892
    // when outputting this section. We also have to do it if the format
977
892
    // uses Elf_Rel, since in that case the written value is the addend.
978
892
    if (
IsConstant || 892
!RelTy::IsRela0
)
979
871
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
1.14k
  }
981
599
}
982
983
90.1k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
90.1k
  if (S.AreRelocsRela)
985
654
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
90.1k
  else
987
89.5k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
90.1k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&)
Line
Count
Source
983
84.4k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
84.4k
  if (S.AreRelocsRela)
985
599
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
84.4k
  else
987
83.8k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
84.4k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase&)
Line
Count
Source
983
831
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
831
  if (S.AreRelocsRela)
985
46
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
831
  else
987
785
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
831
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase&)
Line
Count
Source
983
1.77k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
1.77k
  if (S.AreRelocsRela)
985
7
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
1.77k
  else
987
1.76k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
1.77k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&)
Line
Count
Source
983
3.07k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
3.07k
  if (S.AreRelocsRela)
985
2
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
3.07k
  else
987
3.07k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
3.07k
}
989
990
// Insert the Thunks for OutputSection OS into their designated place
991
// in the Sections vector, and recalculate the InputSection output section
992
// offsets.
993
// This may invalidate any output section offsets stored outside of InputSection
994
226
void ThunkCreator::mergeThunks() {
995
18
  for (auto &KV : ThunkSections) {
996
18
    std::vector<InputSection *> *ISR = KV.first;
997
18
    std::vector<ThunkSection *> &Thunks = KV.second;
998
18
999
18
    // Order Thunks in ascending OutSecOff
1000
13
    auto ThunkCmp = [](const ThunkSection *A, const ThunkSection *B) {
1001
13
      return A->OutSecOff < B->OutSecOff;
1002
13
    };
1003
18
    std::stable_sort(Thunks.begin(), Thunks.end(), ThunkCmp);
1004
18
1005
18
    // Merge sorted vectors of Thunks and InputSections by OutSecOff
1006
18
    std::vector<InputSection *> Tmp;
1007
18
    Tmp.reserve(ISR->size() + Thunks.size());
1008
60
    auto MergeCmp = [](const InputSection *A, const InputSection *B) {
1009
60
      // std::merge requires a strict weak ordering.
1010
60
      if (A->OutSecOff < B->OutSecOff)
1011
2
        return true;
1012
58
      
if (58
A->OutSecOff == B->OutSecOff58
)
1013
58
        // Check if Thunk is immediately before any specific Target InputSection
1014
58
        // for example Mips LA25 Thunks.
1015
41
        
if (auto *41
TA41
= dyn_cast<ThunkSection>(A))
1016
41
          
if (41
TA && 41
TA->getTargetInputSection() == B41
)
1017
21
            return true;
1018
37
      return false;
1019
37
    };
1020
18
    std::merge(ISR->begin(), ISR->end(), Thunks.begin(), Thunks.end(),
1021
18
               std::back_inserter(Tmp), MergeCmp);
1022
18
    *ISR = std::move(Tmp);
1023
18
  }
1024
226
}
1025
1026
9
static uint32_t findEndOfFirstNonExec(OutputSection &Cmd) {
1027
9
  for (BaseCommand *Base : Cmd.Commands)
1028
9
    
if (auto *9
ISD9
= dyn_cast<InputSectionDescription>(Base))
1029
9
      for (auto *IS : ISD->Sections)
1030
13
        
if (13
(IS->Flags & SHF_EXECINSTR) == 013
)
1031
0
          return IS->OutSecOff + IS->getSize();
1032
9
  return 0;
1033
9
}
1034
1035
ThunkSection *ThunkCreator::getOSThunkSec(OutputSection *OS,
1036
21
                                          std::vector<InputSection *> *ISR) {
1037
21
  if (
CurTS == nullptr21
) {
1038
9
    uint32_t Off = findEndOfFirstNonExec(*OS);
1039
9
    CurTS = addThunkSection(OS, ISR, Off);
1040
9
  }
1041
21
  return CurTS;
1042
21
}
1043
1044
// Add a Thunk that needs to be placed in a ThunkSection that immediately
1045
// precedes its Target.
1046
28
ThunkSection *ThunkCreator::getISThunkSec(InputSection *IS) {
1047
28
  ThunkSection *TS = ThunkedSections.lookup(IS);
1048
28
  if (TS)
1049
7
    return TS;
1050
21
1051
21
  // Find InputSectionRange within Target Output Section (TOS) that the
1052
21
  // InputSection (IS) that we need to precede is in.
1053
21
  OutputSection *TOS = IS->getParent();
1054
21
  std::vector<InputSection *> *Range = nullptr;
1055
21
  for (BaseCommand *BC : TOS->Commands)
1056
23
    
if (auto *23
ISD23
= dyn_cast<InputSectionDescription>(BC)) {
1057
22
      InputSection *first = ISD->Sections.front();
1058
22
      InputSection *last = ISD->Sections.back();
1059
22
      if (IS->OutSecOff >= first->OutSecOff &&
1060
22
          
IS->OutSecOff <= last->OutSecOff22
) {
1061
21
        Range = &ISD->Sections;
1062
21
        break;
1063
21
      }
1064
21
    }
1065
21
  TS = addThunkSection(TOS, Range, IS->OutSecOff);
1066
28
  ThunkedSections[IS] = TS;
1067
28
  return TS;
1068
28
}
1069
1070
ThunkSection *ThunkCreator::addThunkSection(OutputSection *OS,
1071
                                            std::vector<InputSection *> *ISR,
1072
30
                                            uint64_t Off) {
1073
30
  auto *TS = make<ThunkSection>(OS, Off);
1074
30
  ThunkSections[ISR].push_back(TS);
1075
30
  return TS;
1076
30
}
1077
1078
std::pair<Thunk *, bool> ThunkCreator::getThunk(SymbolBody &Body,
1079
67
                                                uint32_t Type) {
1080
67
  auto Res = ThunkedSymbols.insert({&Body, std::vector<Thunk *>()});
1081
67
  if (
!Res.second67
) {
1082
18
    // Check existing Thunks for Body to see if they can be reused
1083
18
    for (Thunk *ET : Res.first->second)
1084
18
      
if (18
ET->isCompatibleWith(Type)18
)
1085
18
        return std::make_pair(ET, false);
1086
49
  }
1087
49
  // No existing compatible Thunk in range, create a new one
1088
49
  Thunk *T = addThunk(Type, Body);
1089
49
  Res.first->second.push_back(T);
1090
49
  return std::make_pair(T, true);
1091
49
}
1092
1093
// Call Fn on every executable InputSection accessed via the linker script
1094
// InputSectionDescription::Sections.
1095
void ThunkCreator::forEachExecInputSection(
1096
    ArrayRef<OutputSection *> OutputSections,
1097
    std::function<void(OutputSection *, std::vector<InputSection *> *,
1098
                       InputSection *)>
1099
226
        Fn) {
1100
2.60k
  for (OutputSection *OS : OutputSections) {
1101
2.60k
    if (
!(OS->Flags & SHF_ALLOC) || 2.60k
!(OS->Flags & SHF_EXECINSTR)1.69k
)
1102
2.27k
      continue;
1103
332
    for (BaseCommand *BC : OS->Commands)
1104
336
      
if (auto *336
ISD336
= dyn_cast<InputSectionDescription>(BC)) {
1105
334
        CurTS = nullptr;
1106
334
        for (InputSection *IS : ISD->Sections)
1107
490
          Fn(OS, &ISD->Sections, IS);
1108
336
      }
1109
2.60k
  }
1110
226
}
1111
1112
// Process all relocations from the InputSections that have been assigned
1113
// to OutputSections and redirect through Thunks if needed.
1114
//
1115
// createThunks must be called after scanRelocs has created the Relocations for
1116
// each InputSection. It must be called before the static symbol table is
1117
// finalized. If any Thunks are added to an OutputSection the output section
1118
// offsets of the InputSections will change.
1119
//
1120
// FIXME: All Thunks are assumed to be in range of the relocation. Range
1121
// extension Thunks are not yet supported.
1122
226
bool ThunkCreator::createThunks(ArrayRef<OutputSection *> OutputSections) {
1123
226
  if (Pass > 0)
1124
14
    ThunkSections.clear();
1125
226
1126
226
  // Create all the Thunks and insert them into synthetic ThunkSections. The
1127
226
  // ThunkSections are later inserted back into the OutputSection.
1128
226
1129
226
  // We separate the creation of ThunkSections from the insertion of the
1130
226
  // ThunkSections back into the OutputSection as ThunkSections are not always
1131
226
  // inserted into the same OutputSection as the caller.
1132
226
  forEachExecInputSection(OutputSections, [&](OutputSection *OS,
1133
226
                                              std::vector<InputSection *> *ISR,
1134
490
                                              InputSection *IS) {
1135
620
    for (Relocation &Rel : IS->Relocations) {
1136
620
      SymbolBody &Body = *Rel.Sym;
1137
620
      if (Thunks.find(&Body) != Thunks.end() ||
1138
553
          !Target->needsThunk(Rel.Expr, Rel.Type, IS->File, Body))
1139
553
        continue;
1140
67
      Thunk *T;
1141
67
      bool IsNew;
1142
67
      std::tie(T, IsNew) = getThunk(Body, Rel.Type);
1143
67
      if (
IsNew67
) {
1144
49
        // Find or create a ThunkSection for the new Thunk
1145
49
        ThunkSection *TS;
1146
49
        if (auto *TIS = T->getTargetInputSection())
1147
28
          TS = getISThunkSec(TIS);
1148
49
        else
1149
21
          TS = getOSThunkSec(OS, ISR);
1150
49
        TS->addThunk(T);
1151
49
        Thunks[T->ThunkSym] = T;
1152
49
      }
1153
620
      // Redirect relocation to Thunk, we never go via the PLT to a Thunk
1154
620
      Rel.Sym = T->ThunkSym;
1155
620
      Rel.Expr = fromPlt(Rel.Expr);
1156
620
    }
1157
490
  });
1158
226
  // Merge all created synthetic ThunkSections back into OutputSection
1159
226
  mergeThunks();
1160
226
  ++Pass;
1161
226
  return !ThunkSections.empty();
1162
226
}
1163
1164
template void elf::scanRelocations<ELF32LE>(InputSectionBase &);
1165
template void elf::scanRelocations<ELF32BE>(InputSectionBase &);
1166
template void elf::scanRelocations<ELF64LE>(InputSectionBase &);
1167
template void elf::scanRelocations<ELF64BE>(InputSectionBase &);