Coverage Report

Created: 2017-10-03 07:32

/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.54k
static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
84
7.54k
  // In case of MIPS GP-relative relocations always resolve to a definition
85
7.54k
  // in a regular input file, ignoring the one-definition rule. So we,
86
7.54k
  // for example, should not attempt to create a dynamic relocation even
87
7.54k
  // if the target symbol is preemptible. There are two two MIPS GP-relative
88
7.54k
  // relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
89
7.54k
  // can be against a preemptible symbol.
90
7.54k
  // To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
91
7.54k
  // relocation types occupy eight bit. In case of N64 ABI we extract first
92
7.54k
  // relocation from 3-in-1 packet because only the first relocation can
93
7.54k
  // be against a real symbol.
94
7.54k
  if (
Config->EMachine == EM_MIPS7.54k
) {
95
1.05k
    Type &= 0xff;
96
1.05k
    if (
Type == R_MIPS_GPREL16 || 1.05k
Type == R_MICROMIPS_GPREL161.00k
||
97
1.00k
        Type == R_MICROMIPS_GPREL7_S2)
98
53
      return false;
99
7.48k
  }
100
7.48k
  return Body.isPreemptible();
101
7.48k
}
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.88k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
1.88k
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
1.88k
204
1.88k
  
if (1.88k
!Body.isTls()1.88k
)
205
1.66k
    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
491
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
491
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
491
204
491
  
if (491
!Body.isTls()491
)
205
419
    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
231
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
231
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
231
204
231
  
if (231
!Body.isTls()231
)
205
208
    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.06k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
201
1.06k
  if (!(C.Flags & SHF_ALLOC))
202
0
    return 0;
203
1.06k
204
1.06k
  
if (1.06k
!Body.isTls()1.06k
)
205
955
    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
231
static uint32_t getMipsPairType(uint32_t Type, const SymbolBody &Sym) {
303
231
  switch (Type) {
304
23
  case R_MIPS_HI16:
305
23
    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
167
  default:
315
167
    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
625
static bool isAbsolute(const SymbolBody &Body) {
322
625
  if (Body.isUndefWeak())
323
10
    return true;
324
615
  
if (const auto *615
DR615
= dyn_cast<DefinedRegular>(&Body))
325
596
    return DR->Section == nullptr; // Absolute symbol.
326
19
  return false;
327
19
}
328
329
600
static bool isAbsoluteValue(const SymbolBody &Body) {
330
563
  return isAbsolute(Body) || Body.isTls();
331
600
}
332
333
// Returns true if Expr refers a PLT entry.
334
4.86k
static bool needsPlt(RelExpr Expr) {
335
4.86k
  return isRelExprOneOf<R_PLT_PC, R_PPC_PLT_OPD, R_PLT, R_PLT_PAGE_PC>(Expr);
336
4.86k
}
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.24k
static bool needsGot(RelExpr Expr) {
342
3.24k
  return isRelExprOneOf<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
343
3.24k
                        R_MIPS_GOT_OFF32, R_GOT_PAGE_PC, R_GOT_PC,
344
3.24k
                        R_GOT_FROM_END>(Expr);
345
3.24k
}
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
530
static bool isRelExpr(RelExpr Expr) {
350
530
  return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL,
351
530
                        R_PAGE_PC, R_RELAX_GOT_PC>(Expr);
352
530
}
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.36k
                                     InputSectionBase &S, uint64_t RelOff) {
367
3.36k
  // These expressions always compute a constant
368
3.36k
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
3.36k
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
3.36k
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
3.36k
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
3.36k
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
1.04k
    return true;
374
2.32k
375
2.32k
  // These never do, except if the entire file is position dependent or if
376
2.32k
  // only the low bits are used.
377
2.32k
  
if (2.32k
E == R_GOT || 2.32k
E == R_PLT2.27k
||
E == R_TLSDESC2.24k
)
378
90
    
return Target->usesOnlyLowPageBits(Type) || 90
!Config->Pic61
;
379
2.23k
380
2.23k
  
if (2.23k
isPreemptible(Body, Type)2.23k
)
381
97
    return false;
382
2.13k
  
if (2.13k
!Config->Pic2.13k
)
383
1.61k
    return true;
384
516
385
516
  // For the target and the relocation, we want to know if they are
386
516
  // absolute or relative.
387
516
  bool AbsVal = isAbsoluteValue(Body);
388
516
  bool RelE = isRelExpr(E);
389
516
  if (
AbsVal && 516
!RelE75
)
390
74
    return true;
391
442
  
if (442
!AbsVal && 442
RelE441
)
392
360
    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.88k
                                     InputSectionBase &S, uint64_t RelOff) {
367
1.88k
  // These expressions always compute a constant
368
1.88k
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
1.88k
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
1.88k
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
1.88k
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
1.88k
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
562
    return true;
374
1.32k
375
1.32k
  // These never do, except if the entire file is position dependent or if
376
1.32k
  // only the low bits are used.
377
1.32k
  
if (1.32k
E == R_GOT || 1.32k
E == R_PLT1.30k
||
E == R_TLSDESC1.28k
)
378
43
    
return Target->usesOnlyLowPageBits(Type) || 43
!Config->Pic14
;
379
1.27k
380
1.27k
  
if (1.27k
isPreemptible(Body, Type)1.27k
)
381
79
    return false;
382
1.19k
  
if (1.19k
!Config->Pic1.19k
)
383
861
    return true;
384
337
385
337
  // For the target and the relocation, we want to know if they are
386
337
  // absolute or relative.
387
337
  bool AbsVal = isAbsoluteValue(Body);
388
337
  bool RelE = isRelExpr(E);
389
337
  if (
AbsVal && 337
!RelE44
)
390
43
    return true;
391
294
  
if (294
!AbsVal && 294
RelE293
)
392
259
    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
443
                                     InputSectionBase &S, uint64_t RelOff) {
367
443
  // These expressions always compute a constant
368
443
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
443
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
443
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
443
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
443
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
175
    return true;
374
268
375
268
  // These never do, except if the entire file is position dependent or if
376
268
  // only the low bits are used.
377
268
  
if (268
E == R_GOT || 268
E == R_PLT268
||
E == R_TLSDESC255
)
378
13
    
return Target->usesOnlyLowPageBits(Type) || 13
!Config->Pic13
;
379
255
380
255
  
if (255
isPreemptible(Body, Type)255
)
381
8
    return false;
382
247
  
if (247
!Config->Pic247
)
383
202
    return true;
384
45
385
45
  // For the target and the relocation, we want to know if they are
386
45
  // absolute or relative.
387
45
  bool AbsVal = isAbsoluteValue(Body);
388
45
  bool RelE = isRelExpr(E);
389
45
  if (
AbsVal && 45
!RelE9
)
390
9
    return true;
391
36
  
if (36
!AbsVal && 36
RelE36
)
392
11
    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
891
                                     InputSectionBase &S, uint64_t RelOff) {
367
891
  // These expressions always compute a constant
368
891
  if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
369
891
                     R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
370
891
                     R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
371
891
                     R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_PC, R_TLSGD,
372
891
                     R_PPC_PLT_OPD, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
373
252
    return true;
374
639
375
639
  // These never do, except if the entire file is position dependent or if
376
639
  // only the low bits are used.
377
639
  
if (639
E == R_GOT || 639
E == R_PLT610
||
E == R_TLSDESC605
)
378
34
    
return Target->usesOnlyLowPageBits(Type) || 34
!Config->Pic34
;
379
605
380
605
  
if (605
isPreemptible(Body, Type)605
)
381
10
    return false;
382
595
  
if (595
!Config->Pic595
)
383
489
    return true;
384
106
385
106
  // For the target and the relocation, we want to know if they are
386
106
  // absolute or relative.
387
106
  bool AbsVal = isAbsoluteValue(Body);
388
106
  bool RelE = isRelExpr(E);
389
106
  if (
AbsVal && 106
!RelE19
)
390
19
    return true;
391
87
  
if (87
!AbsVal && 87
RelE87
)
392
80
    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
370
static RelExpr fromPlt(RelExpr Expr) {
426
370
  // We decided not to use a plt. Optimize a reference to the plt to a
427
370
  // reference to the symbol itself.
428
370
  if (Expr == R_PLT_PC)
429
260
    return R_PC;
430
110
  
if (110
Expr == R_PPC_PLT_OPD110
)
431
3
    return R_PPC_OPD;
432
107
  
if (107
Expr == R_PLT107
)
433
45
    return R_ABS;
434
62
  return Expr;
435
62
}
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.96k
                          typename ELFT::uint RelOff) {
558
1.96k
  if (
Body.isGnuIFunc()1.96k
) {
559
36
    Expr = toPlt(Expr);
560
1.96k
  } else 
if (1.93k
!isPreemptible(Body, Type)1.93k
) {
561
1.36k
    if (needsPlt(Expr))
562
299
      Expr = fromPlt(Expr);
563
1.36k
    if (
Expr == R_GOT_PC && 1.36k
!isAbsoluteValue(Body)84
)
564
46
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
1.93k
  }
566
1.96k
567
1.95k
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
1.96k
  if (
IsWrite || 1.96k
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)1.72k
)
569
1.86k
    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.14k
                          typename ELFT::uint RelOff) {
558
1.14k
  if (
Body.isGnuIFunc()1.14k
) {
559
24
    Expr = toPlt(Expr);
560
1.14k
  } else 
if (1.11k
!isPreemptible(Body, Type)1.11k
) {
561
747
    if (needsPlt(Expr))
562
81
      Expr = fromPlt(Expr);
563
747
    if (
Expr == R_GOT_PC && 747
!isAbsoluteValue(Body)78
)
564
43
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
1.11k
  }
566
1.14k
567
1.13k
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
1.14k
  if (
IsWrite || 1.14k
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)981
)
569
1.06k
    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
498
                          typename ELFT::uint RelOff) {
558
498
  if (
Body.isGnuIFunc()498
) {
559
12
    Expr = toPlt(Expr);
560
498
  } else 
if (486
!isPreemptible(Body, Type)486
) {
561
358
    if (needsPlt(Expr))
562
170
      Expr = fromPlt(Expr);
563
358
    if (
Expr == R_GOT_PC && 358
!isAbsoluteValue(Body)6
)
564
3
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
486
  }
566
498
567
498
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
498
  if (
IsWrite || 498
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)459
)
569
483
    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
231
                          typename ELFT::uint RelOff) {
558
231
  if (
Body.isGnuIFunc()231
) {
559
0
    Expr = toPlt(Expr);
560
231
  } else 
if (231
!isPreemptible(Body, Type)231
) {
561
186
    if (needsPlt(Expr))
562
45
      Expr = fromPlt(Expr);
563
186
    if (
Expr == R_GOT_PC && 186
!isAbsoluteValue(Body)0
)
564
0
      Expr = Target->adjustRelaxExpr(Type, Data, Expr);
565
231
  }
566
231
567
231
  bool IsWrite = !Config->ZText || (S.Flags & SHF_WRITE);
568
231
  if (
IsWrite || 231
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff)221
)
569
223
    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.88k
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
1.88k
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
1.88k
  int64_t A = RelTy::IsRela
651
1.17k
                  ? getAddend<ELFT>(Rel)
652
715
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
1.88k
654
1.88k
  if (
Config->EMachine == EM_PPC64 && 1.88k
Config->Pic45
&&
Type == R_PPC64_TOC14
)
655
2
    A += getPPC64TocBase();
656
1.88k
  return A;
657
1.88k
}
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.06k
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
1.06k
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
1.06k
  int64_t A = RelTy::IsRela
651
1.06k
                  ? getAddend<ELFT>(Rel)
652
0
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
1.06k
654
1.06k
  if (
Config->EMachine == EM_PPC64 && 1.06k
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
1.06k
  return A;
657
1.06k
}
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
223
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
223
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
223
  int64_t A = RelTy::IsRela
651
0
                  ? getAddend<ELFT>(Rel)
652
223
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
223
654
223
  if (
Config->EMachine == EM_PPC64 && 223
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
223
  return A;
657
223
}
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
490
static int64_t computeAddend(const RelTy &Rel, const uint8_t *Buf) {
649
490
  uint32_t Type = Rel.getType(Config->IsMips64EL);
650
490
  int64_t A = RelTy::IsRela
651
0
                  ? getAddend<ELFT>(Rel)
652
490
                  : Target->getImplicitAddend(Buf + Rel.r_offset, Type);
653
490
654
490
  if (
Config->EMachine == EM_PPC64 && 490
Config->Pic0
&&
Type == R_PPC64_TOC0
)
655
0
    A += getPPC64TocBase();
656
490
  return A;
657
490
}
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
292
                                 const RelTy *End) {
667
292
  if (
Expr == R_MIPS_GOTREL && 292
Body.isLocal()19
)
668
8
    return Sec.getFile<ELFT>()->MipsGp0;
669
284
670
284
  // The ABI says that the paired relocation is used only for REL.
671
284
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
284
  
if (284
RelTy::IsRela284
)
673
53
    return 0;
674
231
675
231
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
231
  uint32_t PairTy = getMipsPairType(Type, Body);
677
231
  if (PairTy == R_MIPS_NONE)
678
192
    return 0;
679
39
680
39
  const uint8_t *Buf = Sec.Data.data();
681
39
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
682
39
683
39
  // To make things worse, paired relocations might not be contiguous in
684
39
  // the relocation table, so we need to do linear search. *sigh*
685
79
  for (const RelTy *RI = &Rel; 
RI != End79
;
++RI40
) {
686
78
    if (RI->getType(Config->IsMips64EL) != PairTy)
687
40
      continue;
688
38
    
if (38
RI->getSymbol(Config->IsMips64EL) != SymIndex38
)
689
0
      continue;
690
38
691
38
    return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
692
38
  }
693
39
694
1
  warn("can't find matching " + toString(PairTy) + " relocation for " +
695
1
       toString(Type));
696
1
  return 0;
697
292
}
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
223
                                 const RelTy *End) {
667
223
  if (
Expr == R_MIPS_GOTREL && 223
Body.isLocal()7
)
668
7
    return Sec.getFile<ELFT>()->MipsGp0;
669
216
670
216
  // The ABI says that the paired relocation is used only for REL.
671
216
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
672
216
  
if (216
RelTy::IsRela216
)
673
0
    return 0;
674
216
675
216
  uint32_t Type = Rel.getType(Config->IsMips64EL);
676
216
  uint32_t PairTy = getMipsPairType(Type, Body);
677
216
  if (PairTy == R_MIPS_NONE)
678
178
    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
223
}
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
183
                            uint64_t Offset) {
702
183
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
183
705
183
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
180
                       Sym.getVisibility() == STV_DEFAULT;
707
183
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 183
CanBeExternal133
)
708
129
    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
183
}
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
10
                            uint64_t Offset) {
702
10
  if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
703
0
    return;
704
10
705
10
  bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
706
10
                       Sym.getVisibility() == STV_DEFAULT;
707
10
  if (
Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 10
CanBeExternal10
)
708
10
    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
10
}
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.4k
  explicit OffsetGetter(InputSectionBase &Sec) {
755
90.4k
    if (auto *
Eh90.4k
= dyn_cast<EhInputSection>(&Sec)) {
756
95
      P = Eh->Pieces;
757
95
      Size = Eh->Pieces.size();
758
95
    }
759
90.4k
  }
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.98k
  uint64_t get(uint64_t Off) {
765
1.98k
    if (P.empty())
766
1.86k
      return Off;
767
128
768
245
    
while (128
I != Size && 245
P[I].InputOff + P[I].Size <= Off245
)
769
117
      ++I;
770
128
    if (I == Size)
771
0
      return Off;
772
128
773
128
    // P must be contiguous, so there must be no holes in between.
774
128
    assert(P[I].InputOff <= Off && "Relocation not in any piece");
775
128
776
128
    // Offset -1 means that the piece is dead (i.e. garbage collected).
777
128
    if (P[I].OutputOff == -1)
778
10
      return -1;
779
118
    return P[I].OutputOff + Off - P[I].InputOff;
780
118
  }
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.4k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
90.4k
  OffsetGetter GetOffset(Sec);
840
90.4k
841
92.4k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End92.4k
;
++I1.98k
) {
842
1.98k
    const RelTy &Rel = *I;
843
1.98k
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
1.98k
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
1.98k
846
1.98k
    if (
Config->MipsN32Abi1.98k
) {
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.98k
853
1.98k
    // Compute the offset of this section in the output section.
854
1.98k
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
1.98k
    if (Offset == uint64_t(-1))
856
10
      continue;
857
1.97k
858
1.97k
    // Report undefined symbols. The fact that we report undefined
859
1.97k
    // symbols here means that we report undefined symbols only when
860
1.97k
    // they have relocations pointing to them. We don't care about
861
1.97k
    // undefined symbols that are in dead-stripped sections.
862
1.97k
    
if (1.97k
!Body.isLocal() && 1.97k
Body.isUndefined()1.43k
&&
!Body.symbol()->isWeak()242
)
863
183
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
1.97k
865
1.97k
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
1.97k
                                      Sec.Data.begin() + Rel.r_offset);
867
1.97k
868
1.97k
    // Ignore "hint" relocations because they are only markers for relaxation.
869
1.97k
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
13
      continue;
871
1.96k
872
1.96k
    bool Preemptible = isPreemptible(Body, Type);
873
1.96k
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
1.96k
                            Sec, Rel.r_offset);
875
1.96k
    if (ErrorCount)
876
80
      continue;
877
1.88k
878
1.88k
    // This relocation does not require got entry, but it is relative to got and
879
1.88k
    // needs it to be created. Here we request for that.
880
1.88k
    
if (1.88k
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
1.88k
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
24
      InX::Got->HasGotOffRel = true;
883
1.88k
884
1.88k
    // Read an addend.
885
1.88k
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
1.88k
    if (Config->EMachine == EM_MIPS)
887
292
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
1.88k
889
1.88k
    // Process some TLS relocations, including relaxing TLS relocations.
890
1.88k
    // Note that this function does not handle all TLS relocations.
891
1.88k
    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.75k
897
1.75k
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
1.75k
    
if (1.75k
needsPlt(Expr) && 1.75k
!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.75k
907
1.75k
    // Create a GOT slot if a relocation needs GOT.
908
1.75k
    if (
needsGot(Expr)1.75k
) {
909
241
      if (
Config->EMachine == EM_MIPS241
) {
910
92
        // MIPS ABI has special rules to process GOT entries and doesn't
911
92
        // require relocation entries for them. A special case is TLS
912
92
        // relocations. In that case dynamic loader applies dynamic
913
92
        // relocations to initialize TLS GOT entries.
914
92
        // See "Global Offset Table" in Chapter 5 in the following document
915
92
        // for detailed description:
916
92
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
92
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
92
        if (
Body.isTls() && 92
Body.isPreemptible()9
)
919
6
          In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, InX::MipsGot,
920
6
                                       Body.getGotOffset(), false, &Body, 0});
921
241
      } else 
if (149
!Body.isInGot()149
) {
922
103
        addGotEntry<ELFT>(Body, Preemptible);
923
103
      }
924
241
    }
925
1.75k
926
1.75k
    if (
!needsPlt(Expr) && 1.75k
!needsGot(Expr)1.49k
&&
isPreemptible(Body, Type)1.25k
) {
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.64k
958
1.64k
    // If the relocation points to something in the file, we can process it.
959
1.64k
    bool IsConstant =
960
1.64k
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
1.64k
962
1.64k
    // The size is not going to change, so we fold it in here.
963
1.64k
    if (Expr == R_SIZE)
964
24
      Addend += Body.getSize<ELFT>();
965
1.64k
966
1.64k
    // If the output being produced is position independent, the final value
967
1.64k
    // is still not known. In that case we still need some help from the
968
1.64k
    // dynamic linker. We can however do better than just copying the incoming
969
1.64k
    // relocation. We can process some of it and and just ask the dynamic
970
1.64k
    // linker to add the load address.
971
1.64k
    if (!IsConstant)
972
48
      In<ELFT>::RelaDyn->addReloc(
973
48
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
1.64k
975
1.64k
    // If the produced value is a constant, we just remember to write it
976
1.64k
    // when outputting this section. We also have to do it if the format
977
1.64k
    // uses Elf_Rel, since in that case the written value is the addend.
978
1.64k
    if (
IsConstant || 1.64k
!RelTy::IsRela14
)
979
1.60k
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
1.98k
  }
981
90.4k
}
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
84.0k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
84.0k
  OffsetGetter GetOffset(Sec);
840
84.0k
841
84.0k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End84.0k
;
++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
84.0k
}
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.11k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
3.11k
  OffsetGetter GetOffset(Sec);
840
3.11k
841
3.61k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End3.61k
;
++I505
) {
842
505
    const RelTy &Rel = *I;
843
505
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
505
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
505
846
505
    if (
Config->MipsN32Abi505
) {
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
505
853
505
    // Compute the offset of this section in the output section.
854
505
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
505
    if (Offset == uint64_t(-1))
856
0
      continue;
857
505
858
505
    // Report undefined symbols. The fact that we report undefined
859
505
    // symbols here means that we report undefined symbols only when
860
505
    // they have relocations pointing to them. We don't care about
861
505
    // undefined symbols that are in dead-stripped sections.
862
505
    
if (505
!Body.isLocal() && 505
Body.isUndefined()346
&&
!Body.symbol()->isWeak()26
)
863
10
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
505
865
505
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
505
                                      Sec.Data.begin() + Rel.r_offset);
867
505
868
505
    // Ignore "hint" relocations because they are only markers for relaxation.
869
505
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
8
      continue;
871
497
872
497
    bool Preemptible = isPreemptible(Body, Type);
873
497
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
497
                            Sec, Rel.r_offset);
875
497
    if (ErrorCount)
876
7
      continue;
877
490
878
490
    // This relocation does not require got entry, but it is relative to got and
879
490
    // needs it to be created. Here we request for that.
880
490
    
if (490
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
490
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
13
      InX::Got->HasGotOffRel = true;
883
490
884
490
    // Read an addend.
885
490
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
490
    if (Config->EMachine == EM_MIPS)
887
15
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
490
889
490
    // Process some TLS relocations, including relaxing TLS relocations.
890
490
    // Note that this function does not handle all TLS relocations.
891
490
    if (unsigned Processed =
892
37
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
37
      I += (Processed - 1);
894
37
      continue;
895
37
    }
896
453
897
453
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
453
    
if (453
needsPlt(Expr) && 453
!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
453
907
453
    // Create a GOT slot if a relocation needs GOT.
908
453
    if (
needsGot(Expr)453
) {
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
453
926
453
    if (
!needsPlt(Expr) && 453
!needsGot(Expr)383
&&
isPreemptible(Body, Type)330
) {
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
431
958
431
    // If the relocation points to something in the file, we can process it.
959
431
    bool IsConstant =
960
431
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
431
962
431
    // The size is not going to change, so we fold it in here.
963
431
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
431
966
431
    // If the output being produced is position independent, the final value
967
431
    // is still not known. In that case we still need some help from the
968
431
    // dynamic linker. We can however do better than just copying the incoming
969
431
    // relocation. We can process some of it and and just ask the dynamic
970
431
    // linker to add the load address.
971
431
    if (!IsConstant)
972
13
      In<ELFT>::RelaDyn->addReloc(
973
13
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
431
975
431
    // If the produced value is a constant, we just remember to write it
976
431
    // when outputting this section. We also have to do it if the format
977
431
    // uses Elf_Rel, since in that case the written value is the addend.
978
431
    if (
IsConstant || 431
!RelTy::IsRela13
)
979
431
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
505
  }
981
3.11k
}
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.79k
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
1.79k
  OffsetGetter GetOffset(Sec);
840
1.79k
841
2.01k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End2.01k
;
++I223
) {
842
223
    const RelTy &Rel = *I;
843
223
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
223
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
223
846
223
    if (
Config->MipsN32Abi223
) {
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
223
853
223
    // Compute the offset of this section in the output section.
854
223
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
223
    if (Offset == uint64_t(-1))
856
0
      continue;
857
223
858
223
    // Report undefined symbols. The fact that we report undefined
859
223
    // symbols here means that we report undefined symbols only when
860
223
    // they have relocations pointing to them. We don't care about
861
223
    // undefined symbols that are in dead-stripped sections.
862
223
    
if (223
!Body.isLocal() && 223
Body.isUndefined()157
&&
!Body.symbol()->isWeak()12
)
863
10
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
223
865
223
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
223
                                      Sec.Data.begin() + Rel.r_offset);
867
223
868
223
    // Ignore "hint" relocations because they are only markers for relaxation.
869
223
    if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
870
0
      continue;
871
223
872
223
    bool Preemptible = isPreemptible(Body, Type);
873
223
    Expr = adjustExpr<ELFT>(Body, Expr, Type, Sec.Data.data() + Rel.r_offset,
874
223
                            Sec, Rel.r_offset);
875
223
    if (ErrorCount)
876
0
      continue;
877
223
878
223
    // This relocation does not require got entry, but it is relative to got and
879
223
    // needs it to be created. Here we request for that.
880
223
    
if (223
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
223
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
223
884
223
    // Read an addend.
885
223
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
223
    if (Config->EMachine == EM_MIPS)
887
223
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
223
889
223
    // Process some TLS relocations, including relaxing TLS relocations.
890
223
    // Note that this function does not handle all TLS relocations.
891
223
    if (unsigned Processed =
892
8
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
8
      I += (Processed - 1);
894
8
      continue;
895
8
    }
896
215
897
215
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
215
    
if (215
needsPlt(Expr) && 215
!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
215
907
215
    // Create a GOT slot if a relocation needs GOT.
908
215
    if (
needsGot(Expr)215
) {
909
67
      if (
Config->EMachine == EM_MIPS67
) {
910
67
        // MIPS ABI has special rules to process GOT entries and doesn't
911
67
        // require relocation entries for them. A special case is TLS
912
67
        // relocations. In that case dynamic loader applies dynamic
913
67
        // relocations to initialize TLS GOT entries.
914
67
        // See "Global Offset Table" in Chapter 5 in the following document
915
67
        // for detailed description:
916
67
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
917
67
        InX::MipsGot->addEntry(Body, Addend, Expr);
918
67
        if (
Body.isTls() && 67
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
67
    }
925
215
926
215
    if (
!needsPlt(Expr) && 215
!needsGot(Expr)207
&&
isPreemptible(Body, Type)140
) {
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
214
958
214
    // If the relocation points to something in the file, we can process it.
959
214
    bool IsConstant =
960
214
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
214
962
214
    // The size is not going to change, so we fold it in here.
963
214
    if (Expr == R_SIZE)
964
0
      Addend += Body.getSize<ELFT>();
965
214
966
214
    // If the output being produced is position independent, the final value
967
214
    // is still not known. In that case we still need some help from the
968
214
    // dynamic linker. We can however do better than just copying the incoming
969
214
    // relocation. We can process some of it and and just ask the dynamic
970
214
    // linker to add the load address.
971
214
    if (!IsConstant)
972
1
      In<ELFT>::RelaDyn->addReloc(
973
1
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
214
975
214
    // If the produced value is a constant, we just remember to write it
976
214
    // when outputting this section. We also have to do it if the format
977
214
    // uses Elf_Rel, since in that case the written value is the addend.
978
214
    if (
IsConstant || 214
!RelTy::IsRela1
)
979
214
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
223
  }
981
1.79k
}
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
606
static void scanRelocs(InputSectionBase &Sec, ArrayRef<RelTy> Rels) {
839
606
  OffsetGetter GetOffset(Sec);
840
606
841
1.75k
  for (auto I = Rels.begin(), End = Rels.end(); 
I != End1.75k
;
++I1.15k
) {
842
1.15k
    const RelTy &Rel = *I;
843
1.15k
    SymbolBody &Body = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
844
1.15k
    uint32_t Type = Rel.getType(Config->IsMips64EL);
845
1.15k
846
1.15k
    if (
Config->MipsN32Abi1.15k
) {
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.15k
853
1.15k
    // Compute the offset of this section in the output section.
854
1.15k
    uint64_t Offset = GetOffset.get(Rel.r_offset);
855
1.15k
    if (Offset == uint64_t(-1))
856
10
      continue;
857
1.14k
858
1.14k
    // Report undefined symbols. The fact that we report undefined
859
1.14k
    // symbols here means that we report undefined symbols only when
860
1.14k
    // they have relocations pointing to them. We don't care about
861
1.14k
    // undefined symbols that are in dead-stripped sections.
862
1.14k
    
if (1.14k
!Body.isLocal() && 1.14k
Body.isUndefined()873
&&
!Body.symbol()->isWeak()197
)
863
159
      reportUndefined<ELFT>(Body, Sec, Rel.r_offset);
864
1.14k
865
1.14k
    RelExpr Expr = Target->getRelExpr(Type, Body, *Sec.File,
866
1.14k
                                      Sec.Data.begin() + Rel.r_offset);
867
1.14k
868
1.14k
    // Ignore "hint" relocations because they are only markers for relaxation.
869
1.14k
    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.06k
878
1.06k
    // This relocation does not require got entry, but it is relative to got and
879
1.06k
    // needs it to be created. Here we request for that.
880
1.06k
    
if (1.06k
isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
881
1.06k
                       R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
882
0
      InX::Got->HasGotOffRel = true;
883
1.06k
884
1.06k
    // Read an addend.
885
1.06k
    int64_t Addend = computeAddend<ELFT>(Rel, Sec.Data.data());
886
1.06k
    if (Config->EMachine == EM_MIPS)
887
0
      Addend += computeMipsAddend<ELFT>(Rel, Sec, Expr, Body, End);
888
1.06k
889
1.06k
    // Process some TLS relocations, including relaxing TLS relocations.
890
1.06k
    // Note that this function does not handle all TLS relocations.
891
1.06k
    if (unsigned Processed =
892
83
            handleTlsRelocation<ELFT>(Type, Body, Sec, Offset, Addend, Expr)) {
893
83
      I += (Processed - 1);
894
83
      continue;
895
83
    }
896
983
897
983
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
898
983
    
if (983
needsPlt(Expr) && 983
!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
983
907
983
    // Create a GOT slot if a relocation needs GOT.
908
983
    if (
needsGot(Expr)983
) {
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
983
926
983
    if (
!needsPlt(Expr) && 983
!needsGot(Expr)817
&&
isPreemptible(Body, Type)715
) {
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
901
958
901
    // If the relocation points to something in the file, we can process it.
959
901
    bool IsConstant =
960
901
        isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, Sec, Rel.r_offset);
961
901
962
901
    // The size is not going to change, so we fold it in here.
963
901
    if (Expr == R_SIZE)
964
24
      Addend += Body.getSize<ELFT>();
965
901
966
901
    // If the output being produced is position independent, the final value
967
901
    // is still not known. In that case we still need some help from the
968
901
    // dynamic linker. We can however do better than just copying the incoming
969
901
    // relocation. We can process some of it and and just ask the dynamic
970
901
    // linker to add the load address.
971
901
    if (!IsConstant)
972
21
      In<ELFT>::RelaDyn->addReloc(
973
21
          {Target->RelativeRel, &Sec, Offset, true, &Body, Addend});
974
901
975
901
    // If the produced value is a constant, we just remember to write it
976
901
    // when outputting this section. We also have to do it if the format
977
901
    // uses Elf_Rel, since in that case the written value is the addend.
978
901
    if (
IsConstant || 901
!RelTy::IsRela0
)
979
880
      Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
980
1.15k
  }
981
606
}
982
983
90.4k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
90.4k
  if (S.AreRelocsRela)
985
661
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
90.4k
  else
987
89.7k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
90.4k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase&)
Line
Count
Source
983
84.6k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
84.6k
  if (S.AreRelocsRela)
985
606
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
84.6k
  else
987
84.0k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
84.6k
}
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.79k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
1.79k
  if (S.AreRelocsRela)
985
7
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
1.79k
  else
987
1.79k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
1.79k
}
void lld::elf::scanRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase&)
Line
Count
Source
983
3.11k
template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
984
3.11k
  if (S.AreRelocsRela)
985
2
    scanRelocs<ELFT>(S, S.relas<ELFT>());
986
3.11k
  else
987
3.11k
    scanRelocs<ELFT>(S, S.rels<ELFT>());
988
3.11k
}
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
231
void ThunkCreator::mergeThunks() {
995
22
  for (auto &KV : ThunkSections) {
996
22
    std::vector<InputSection *> *ISR = KV.first;
997
22
    std::vector<ThunkSection *> &Thunks = KV.second;
998
22
999
22
    // 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
22
    std::stable_sort(Thunks.begin(), Thunks.end(), ThunkCmp);
1004
22
1005
22
    // Merge sorted vectors of Thunks and InputSections by OutSecOff
1006
22
    std::vector<InputSection *> Tmp;
1007
22
    Tmp.reserve(ISR->size() + Thunks.size());
1008
64
    auto MergeCmp = [](const InputSection *A, const InputSection *B) {
1009
64
      // std::merge requires a strict weak ordering.
1010
64
      if (A->OutSecOff < B->OutSecOff)
1011
2
        return true;
1012
62
      
if (62
A->OutSecOff == B->OutSecOff62
)
1013
62
        // Check if Thunk is immediately before any specific Target InputSection
1014
62
        // for example Mips LA25 Thunks.
1015
45
        
if (auto *45
TA45
= dyn_cast<ThunkSection>(A))
1016
45
          
if (45
TA && 45
TA->getTargetInputSection() == B45
)
1017
21
            return true;
1018
41
      return false;
1019
41
    };
1020
22
    std::merge(ISR->begin(), ISR->end(), Thunks.begin(), Thunks.end(),
1021
22
               std::back_inserter(Tmp), MergeCmp);
1022
22
    *ISR = std::move(Tmp);
1023
22
  }
1024
231
}
1025
1026
13
static uint32_t findEndOfFirstNonExec(OutputSection &Cmd) {
1027
13
  for (BaseCommand *Base : Cmd.Commands)
1028
13
    
if (auto *13
ISD13
= dyn_cast<InputSectionDescription>(Base))
1029
13
      for (auto *IS : ISD->Sections)
1030
17
        
if (17
(IS->Flags & SHF_EXECINSTR) == 017
)
1031
0
          return IS->OutSecOff + IS->getSize();
1032
13
  return 0;
1033
13
}
1034
1035
ThunkSection *ThunkCreator::getOSThunkSec(OutputSection *OS,
1036
25
                                          std::vector<InputSection *> *ISR) {
1037
25
  if (
CurTS == nullptr25
) {
1038
13
    uint32_t Off = findEndOfFirstNonExec(*OS);
1039
13
    CurTS = addThunkSection(OS, ISR, Off);
1040
13
  }
1041
25
  return CurTS;
1042
25
}
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
34
                                            uint64_t Off) {
1073
34
  auto *TS = make<ThunkSection>(OS, Off);
1074
34
  ThunkSections[ISR].push_back(TS);
1075
34
  return TS;
1076
34
}
1077
1078
std::pair<Thunk *, bool> ThunkCreator::getThunk(SymbolBody &Body,
1079
71
                                                uint32_t Type) {
1080
71
  auto Res = ThunkedSymbols.insert({&Body, std::vector<Thunk *>()});
1081
71
  if (
!Res.second71
) {
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
53
  }
1087
53
  // No existing compatible Thunk in range, create a new one
1088
53
  Thunk *T = addThunk(Type, Body);
1089
53
  Res.first->second.push_back(T);
1090
53
  return std::make_pair(T, true);
1091
53
}
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
231
        Fn) {
1100
2.66k
  for (OutputSection *OS : OutputSections) {
1101
2.66k
    if (
!(OS->Flags & SHF_ALLOC) || 2.66k
!(OS->Flags & SHF_EXECINSTR)1.72k
)
1102
2.31k
      continue;
1103
345
    for (BaseCommand *BC : OS->Commands)
1104
349
      
if (auto *349
ISD349
= dyn_cast<InputSectionDescription>(BC)) {
1105
347
        CurTS = nullptr;
1106
347
        for (InputSection *IS : ISD->Sections)
1107
507
          Fn(OS, &ISD->Sections, IS);
1108
349
      }
1109
2.66k
  }
1110
231
}
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
231
bool ThunkCreator::createThunks(ArrayRef<OutputSection *> OutputSections) {
1123
231
  if (Pass > 0)
1124
16
    ThunkSections.clear();
1125
231
1126
231
  // Create all the Thunks and insert them into synthetic ThunkSections. The
1127
231
  // ThunkSections are later inserted back into the OutputSection.
1128
231
1129
231
  // We separate the creation of ThunkSections from the insertion of the
1130
231
  // ThunkSections back into the OutputSection as ThunkSections are not always
1131
231
  // inserted into the same OutputSection as the caller.
1132
231
  forEachExecInputSection(OutputSections, [&](OutputSection *OS,
1133
231
                                              std::vector<InputSection *> *ISR,
1134
507
                                              InputSection *IS) {
1135
631
    for (Relocation &Rel : IS->Relocations) {
1136
631
      SymbolBody &Body = *Rel.Sym;
1137
631
      if (Thunks.find(&Body) != Thunks.end() ||
1138
560
          !Target->needsThunk(Rel.Expr, Rel.Type, IS->File, Body))
1139
560
        continue;
1140
71
      Thunk *T;
1141
71
      bool IsNew;
1142
71
      std::tie(T, IsNew) = getThunk(Body, Rel.Type);
1143
71
      if (
IsNew71
) {
1144
53
        // Find or create a ThunkSection for the new Thunk
1145
53
        ThunkSection *TS;
1146
53
        if (auto *TIS = T->getTargetInputSection())
1147
28
          TS = getISThunkSec(TIS);
1148
53
        else
1149
25
          TS = getOSThunkSec(OS, ISR);
1150
53
        TS->addThunk(T);
1151
53
        Thunks[T->ThunkSym] = T;
1152
53
      }
1153
631
      // Redirect relocation to Thunk, we never go via the PLT to a Thunk
1154
631
      Rel.Sym = T->ThunkSym;
1155
631
      Rel.Expr = fromPlt(Rel.Expr);
1156
631
    }
1157
507
  });
1158
231
  // Merge all created synthetic ThunkSections back into OutputSection
1159
231
  mergeThunks();
1160
231
  ++Pass;
1161
231
  return !ThunkSections.empty();
1162
231
}
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 &);