Coverage Report

Created: 2017-09-21 03:39

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/SyntheticSections.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SyntheticSections.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 linker-synthesized sections. Currently,
11
// synthetic sections are created either output sections or input sections,
12
// but we are rewriting code so that all synthetic sections are created as
13
// input sections.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "SyntheticSections.h"
18
#include "Config.h"
19
#include "Error.h"
20
#include "InputFiles.h"
21
#include "LinkerScript.h"
22
#include "Memory.h"
23
#include "OutputSections.h"
24
#include "Strings.h"
25
#include "SymbolTable.h"
26
#include "Target.h"
27
#include "Threads.h"
28
#include "Writer.h"
29
#include "lld/Config/Version.h"
30
#include "llvm/BinaryFormat/Dwarf.h"
31
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
32
#include "llvm/Object/Decompressor.h"
33
#include "llvm/Object/ELFObjectFile.h"
34
#include "llvm/Support/Endian.h"
35
#include "llvm/Support/MD5.h"
36
#include "llvm/Support/RandomNumberGenerator.h"
37
#include "llvm/Support/SHA1.h"
38
#include "llvm/Support/xxhash.h"
39
#include <cstdlib>
40
41
using namespace llvm;
42
using namespace llvm::dwarf;
43
using namespace llvm::ELF;
44
using namespace llvm::object;
45
using namespace llvm::support;
46
using namespace llvm::support::endian;
47
48
using namespace lld;
49
using namespace lld::elf;
50
51
1.65k
uint64_t SyntheticSection::getVA() const {
52
1.65k
  if (OutputSection *Sec = getParent())
53
1.65k
    return Sec->Addr + OutSecOff;
54
0
  return 0;
55
0
}
56
57
1.58k
std::vector<InputSection *> elf::createCommonSections() {
58
1.58k
  if (!Config->DefineCommon)
59
56
    return {};
60
1.53k
61
1.53k
  std::vector<InputSection *> Ret;
62
3.47k
  for (Symbol *S : Symtab->getSymbols()) {
63
3.47k
    auto *Sym = dyn_cast<DefinedCommon>(S->body());
64
3.47k
    if (
!Sym || 3.47k
!Sym->Live86
)
65
3.38k
      continue;
66
84
67
84
    Sym->Section = make<BssSection>("COMMON");
68
84
    size_t Pos = Sym->Section->reserveSpace(Sym->Size, Sym->Alignment);
69
84
    assert(Pos == 0);
70
84
    (void)Pos;
71
84
    Sym->Section->File = Sym->getFile();
72
84
    Ret.push_back(Sym->Section);
73
84
  }
74
1.58k
  return Ret;
75
1.58k
}
76
77
// Returns an LLD version string.
78
1.52k
static ArrayRef<uint8_t> getVersion() {
79
1.52k
  // Check LLD_VERSION first for ease of testing.
80
1.52k
  // You can get consitent output by using the environment variable.
81
1.52k
  // This is only for testing.
82
1.52k
  StringRef S = getenv("LLD_VERSION");
83
1.52k
  if (S.empty())
84
0
    S = Saver.save(Twine("Linker: ") + getLLDVersion());
85
1.52k
86
1.52k
  // +1 to include the terminating '\0'.
87
1.52k
  return {(const uint8_t *)S.data(), S.size() + 1};
88
1.52k
}
89
90
// Creates a .comment section containing LLD version info.
91
// With this feature, you can identify LLD-generated binaries easily
92
// by "readelf --string-dump .comment <file>".
93
// The returned object is a mergeable string section.
94
1.52k
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
1.52k
  typename ELFT::Shdr Hdr = {};
96
1.52k
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
1.52k
  Hdr.sh_type = SHT_PROGBITS;
98
1.52k
  Hdr.sh_entsize = 1;
99
1.52k
  Hdr.sh_addralign = 1;
100
1.52k
101
1.52k
  auto *Ret =
102
1.52k
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
1.52k
  Ret->Data = getVersion();
104
1.52k
  return Ret;
105
1.52k
}
lld::elf::MergeInputSection* lld::elf::createCommentSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
94
185
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
185
  typename ELFT::Shdr Hdr = {};
96
185
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
185
  Hdr.sh_type = SHT_PROGBITS;
98
185
  Hdr.sh_entsize = 1;
99
185
  Hdr.sh_addralign = 1;
100
185
101
185
  auto *Ret =
102
185
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
185
  Ret->Data = getVersion();
104
185
  return Ret;
105
185
}
lld::elf::MergeInputSection* lld::elf::createCommentSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
94
87
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
87
  typename ELFT::Shdr Hdr = {};
96
87
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
87
  Hdr.sh_type = SHT_PROGBITS;
98
87
  Hdr.sh_entsize = 1;
99
87
  Hdr.sh_addralign = 1;
100
87
101
87
  auto *Ret =
102
87
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
87
  Ret->Data = getVersion();
104
87
  return Ret;
105
87
}
lld::elf::MergeInputSection* lld::elf::createCommentSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
94
1.21k
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
1.21k
  typename ELFT::Shdr Hdr = {};
96
1.21k
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
1.21k
  Hdr.sh_type = SHT_PROGBITS;
98
1.21k
  Hdr.sh_entsize = 1;
99
1.21k
  Hdr.sh_addralign = 1;
100
1.21k
101
1.21k
  auto *Ret =
102
1.21k
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
1.21k
  Ret->Data = getVersion();
104
1.21k
  return Ret;
105
1.21k
}
lld::elf::MergeInputSection* lld::elf::createCommentSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
94
43
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
43
  typename ELFT::Shdr Hdr = {};
96
43
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
43
  Hdr.sh_type = SHT_PROGBITS;
98
43
  Hdr.sh_entsize = 1;
99
43
  Hdr.sh_addralign = 1;
100
43
101
43
  auto *Ret =
102
43
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
43
  Ret->Data = getVersion();
104
43
  return Ret;
105
43
}
106
107
// .MIPS.abiflags section.
108
template <class ELFT>
109
MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
110
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
111
135
      Flags(Flags) {
112
135
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
113
135
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::MipsAbiFlagsSection(llvm::object::Elf_Mips_ABIFlags<llvm::object::ELFType<(llvm::support::endianness)1, false> >)
Line
Count
Source
111
13
      Flags(Flags) {
112
13
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
113
13
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::MipsAbiFlagsSection(llvm::object::Elf_Mips_ABIFlags<llvm::object::ELFType<(llvm::support::endianness)0, true> >)
Line
Count
Source
111
28
      Flags(Flags) {
112
28
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
113
28
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::MipsAbiFlagsSection(llvm::object::Elf_Mips_ABIFlags<llvm::object::ELFType<(llvm::support::endianness)0, false> >)
Line
Count
Source
111
92
      Flags(Flags) {
112
92
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
113
92
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::MipsAbiFlagsSection(llvm::object::Elf_Mips_ABIFlags<llvm::object::ELFType<(llvm::support::endianness)1, true> >)
Line
Count
Source
111
2
      Flags(Flags) {
112
2
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
113
2
}
114
115
133
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
116
133
  memcpy(Buf, &Flags, sizeof(Flags));
117
133
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
115
90
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
116
90
  memcpy(Buf, &Flags, sizeof(Flags));
117
90
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
115
28
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
116
28
  memcpy(Buf, &Flags, sizeof(Flags));
117
28
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
115
13
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
116
13
  memcpy(Buf, &Flags, sizeof(Flags));
117
13
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
115
2
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
116
2
  memcpy(Buf, &Flags, sizeof(Flags));
117
2
}
118
119
template <class ELFT>
120
137
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
121
137
  Elf_Mips_ABIFlags Flags = {};
122
137
  bool Create = false;
123
137
124
1.36k
  for (InputSectionBase *Sec : InputSections) {
125
1.36k
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
126
1.18k
      continue;
127
173
    Sec->Live = false;
128
173
    Create = true;
129
173
130
173
    std::string Filename = toString(Sec->getFile<ELFT>());
131
173
    const size_t Size = Sec->Data.size();
132
173
    // Older version of BFD (such as the default FreeBSD linker) concatenate
133
173
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
134
173
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
135
173
    if (
Size < sizeof(Elf_Mips_ABIFlags)173
) {
136
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
137
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
138
0
      return nullptr;
139
0
    }
140
173
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
141
173
    if (
S->version != 0173
) {
142
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
143
0
            Twine(S->version));
144
0
      return nullptr;
145
0
    }
146
173
147
173
    // LLD checks ISA compatibility in getMipsEFlags(). Here we just
148
173
    // select the highest number of ISA/Rev/Ext.
149
173
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
150
173
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
151
173
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
152
173
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
153
173
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
154
173
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
155
173
    Flags.ases |= S->ases;
156
173
    Flags.flags1 |= S->flags1;
157
173
    Flags.flags2 |= S->flags2;
158
173
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
159
137
  };
160
137
161
137
  if (Create)
162
135
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
163
2
  return nullptr;
164
2
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
120
28
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
121
28
  Elf_Mips_ABIFlags Flags = {};
122
28
  bool Create = false;
123
28
124
282
  for (InputSectionBase *Sec : InputSections) {
125
282
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
126
248
      continue;
127
34
    Sec->Live = false;
128
34
    Create = true;
129
34
130
34
    std::string Filename = toString(Sec->getFile<ELFT>());
131
34
    const size_t Size = Sec->Data.size();
132
34
    // Older version of BFD (such as the default FreeBSD linker) concatenate
133
34
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
134
34
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
135
34
    if (
Size < sizeof(Elf_Mips_ABIFlags)34
) {
136
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
137
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
138
0
      return nullptr;
139
0
    }
140
34
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
141
34
    if (
S->version != 034
) {
142
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
143
0
            Twine(S->version));
144
0
      return nullptr;
145
0
    }
146
34
147
34
    // LLD checks ISA compatibility in getMipsEFlags(). Here we just
148
34
    // select the highest number of ISA/Rev/Ext.
149
34
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
150
34
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
151
34
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
152
34
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
153
34
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
154
34
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
155
34
    Flags.ases |= S->ases;
156
34
    Flags.flags1 |= S->flags1;
157
34
    Flags.flags2 |= S->flags2;
158
34
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
159
28
  };
160
28
161
28
  if (Create)
162
28
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
163
0
  return nullptr;
164
0
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
120
2
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
121
2
  Elf_Mips_ABIFlags Flags = {};
122
2
  bool Create = false;
123
2
124
16
  for (InputSectionBase *Sec : InputSections) {
125
16
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
126
14
      continue;
127
2
    Sec->Live = false;
128
2
    Create = true;
129
2
130
2
    std::string Filename = toString(Sec->getFile<ELFT>());
131
2
    const size_t Size = Sec->Data.size();
132
2
    // Older version of BFD (such as the default FreeBSD linker) concatenate
133
2
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
134
2
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
135
2
    if (
Size < sizeof(Elf_Mips_ABIFlags)2
) {
136
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
137
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
138
0
      return nullptr;
139
0
    }
140
2
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
141
2
    if (
S->version != 02
) {
142
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
143
0
            Twine(S->version));
144
0
      return nullptr;
145
0
    }
146
2
147
2
    // LLD checks ISA compatibility in getMipsEFlags(). Here we just
148
2
    // select the highest number of ISA/Rev/Ext.
149
2
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
150
2
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
151
2
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
152
2
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
153
2
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
154
2
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
155
2
    Flags.ases |= S->ases;
156
2
    Flags.flags1 |= S->flags1;
157
2
    Flags.flags2 |= S->flags2;
158
2
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
159
2
  };
160
2
161
2
  if (Create)
162
2
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
163
0
  return nullptr;
164
0
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
120
93
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
121
93
  Elf_Mips_ABIFlags Flags = {};
122
93
  bool Create = false;
123
93
124
948
  for (InputSectionBase *Sec : InputSections) {
125
948
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
126
825
      continue;
127
123
    Sec->Live = false;
128
123
    Create = true;
129
123
130
123
    std::string Filename = toString(Sec->getFile<ELFT>());
131
123
    const size_t Size = Sec->Data.size();
132
123
    // Older version of BFD (such as the default FreeBSD linker) concatenate
133
123
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
134
123
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
135
123
    if (
Size < sizeof(Elf_Mips_ABIFlags)123
) {
136
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
137
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
138
0
      return nullptr;
139
0
    }
140
123
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
141
123
    if (
S->version != 0123
) {
142
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
143
0
            Twine(S->version));
144
0
      return nullptr;
145
0
    }
146
123
147
123
    // LLD checks ISA compatibility in getMipsEFlags(). Here we just
148
123
    // select the highest number of ISA/Rev/Ext.
149
123
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
150
123
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
151
123
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
152
123
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
153
123
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
154
123
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
155
123
    Flags.ases |= S->ases;
156
123
    Flags.flags1 |= S->flags1;
157
123
    Flags.flags2 |= S->flags2;
158
123
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
159
93
  };
160
93
161
93
  if (Create)
162
92
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
163
1
  return nullptr;
164
1
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
120
14
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
121
14
  Elf_Mips_ABIFlags Flags = {};
122
14
  bool Create = false;
123
14
124
115
  for (InputSectionBase *Sec : InputSections) {
125
115
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
126
101
      continue;
127
14
    Sec->Live = false;
128
14
    Create = true;
129
14
130
14
    std::string Filename = toString(Sec->getFile<ELFT>());
131
14
    const size_t Size = Sec->Data.size();
132
14
    // Older version of BFD (such as the default FreeBSD linker) concatenate
133
14
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
134
14
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
135
14
    if (
Size < sizeof(Elf_Mips_ABIFlags)14
) {
136
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
137
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
138
0
      return nullptr;
139
0
    }
140
14
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
141
14
    if (
S->version != 014
) {
142
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
143
0
            Twine(S->version));
144
0
      return nullptr;
145
0
    }
146
14
147
14
    // LLD checks ISA compatibility in getMipsEFlags(). Here we just
148
14
    // select the highest number of ISA/Rev/Ext.
149
14
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
150
14
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
151
14
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
152
14
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
153
14
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
154
14
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
155
14
    Flags.ases |= S->ases;
156
14
    Flags.flags1 |= S->flags1;
157
14
    Flags.flags2 |= S->flags2;
158
14
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
159
14
  };
160
14
161
14
  if (Create)
162
13
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
163
1
  return nullptr;
164
1
}
165
166
// .MIPS.options section.
167
template <class ELFT>
168
MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
169
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
170
30
      Reginfo(Reginfo) {
171
30
  this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
172
30
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, true> >)
Line
Count
Source
170
28
      Reginfo(Reginfo) {
171
28
  this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
172
28
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, true> >)
Line
Count
Source
170
2
      Reginfo(Reginfo) {
171
2
  this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
172
2
}
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, false> >)
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, false> >)
173
174
30
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
175
30
  auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
176
30
  Options->kind = ODK_REGINFO;
177
30
  Options->size = getSize();
178
30
179
30
  if (!Config->Relocatable)
180
29
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
181
30
  memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
182
30
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
174
2
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
175
2
  auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
176
2
  Options->kind = ODK_REGINFO;
177
2
  Options->size = getSize();
178
2
179
2
  if (!Config->Relocatable)
180
2
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
181
2
  memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
182
2
}
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
174
28
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
175
28
  auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
176
28
  Options->kind = ODK_REGINFO;
177
28
  Options->size = getSize();
178
28
179
28
  if (!Config->Relocatable)
180
27
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
181
28
  memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
182
28
}
183
184
template <class ELFT>
185
137
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
186
137
  // N64 ABI only.
187
137
  if (!ELFT::Is64Bits)
188
107
    return nullptr;
189
30
190
30
  Elf_Mips_RegInfo Reginfo = {};
191
30
  bool Create = false;
192
30
193
328
  for (InputSectionBase *Sec : InputSections) {
194
328
    if (Sec->Type != SHT_MIPS_OPTIONS)
195
292
      continue;
196
36
    Sec->Live = false;
197
36
    Create = true;
198
36
199
36
    std::string Filename = toString(Sec->getFile<ELFT>());
200
36
    ArrayRef<uint8_t> D = Sec->Data;
201
36
202
36
    while (
!D.empty()36
) {
203
36
      if (
D.size() < sizeof(Elf_Mips_Options)36
) {
204
0
        error(Filename + ": invalid size of .MIPS.options section");
205
0
        break;
206
0
      }
207
36
208
36
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
209
36
      if (
Opt->kind == ODK_REGINFO36
) {
210
36
        if (
Config->Relocatable && 36
Opt->getRegInfo().ri_gp_value1
)
211
0
          error(Filename + ": unsupported non-zero ri_gp_value");
212
36
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
213
36
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
214
36
        break;
215
36
      }
216
0
217
0
      
if (0
!Opt->size0
)
218
0
        fatal(Filename + ": zero option descriptor size");
219
0
      D = D.slice(Opt->size);
220
0
    }
221
30
  };
222
30
223
30
  if (Create)
224
30
    return make<MipsOptionsSection<ELFT>>(Reginfo);
225
0
  return nullptr;
226
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
185
93
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
186
93
  // N64 ABI only.
187
93
  if (!ELFT::Is64Bits)
188
93
    return nullptr;
189
0
190
0
  Elf_Mips_RegInfo Reginfo = {};
191
0
  bool Create = false;
192
0
193
0
  for (InputSectionBase *Sec : InputSections) {
194
0
    if (Sec->Type != SHT_MIPS_OPTIONS)
195
0
      continue;
196
0
    Sec->Live = false;
197
0
    Create = true;
198
0
199
0
    std::string Filename = toString(Sec->getFile<ELFT>());
200
0
    ArrayRef<uint8_t> D = Sec->Data;
201
0
202
0
    while (
!D.empty()0
) {
203
0
      if (
D.size() < sizeof(Elf_Mips_Options)0
) {
204
0
        error(Filename + ": invalid size of .MIPS.options section");
205
0
        break;
206
0
      }
207
0
208
0
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
209
0
      if (
Opt->kind == ODK_REGINFO0
) {
210
0
        if (
Config->Relocatable && 0
Opt->getRegInfo().ri_gp_value0
)
211
0
          error(Filename + ": unsupported non-zero ri_gp_value");
212
0
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
213
0
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
214
0
        break;
215
0
      }
216
0
217
0
      
if (0
!Opt->size0
)
218
0
        fatal(Filename + ": zero option descriptor size");
219
0
      D = D.slice(Opt->size);
220
0
    }
221
0
  };
222
0
223
0
  if (Create)
224
0
    return make<MipsOptionsSection<ELFT>>(Reginfo);
225
0
  return nullptr;
226
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
185
14
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
186
14
  // N64 ABI only.
187
14
  if (!ELFT::Is64Bits)
188
14
    return nullptr;
189
0
190
0
  Elf_Mips_RegInfo Reginfo = {};
191
0
  bool Create = false;
192
0
193
0
  for (InputSectionBase *Sec : InputSections) {
194
0
    if (Sec->Type != SHT_MIPS_OPTIONS)
195
0
      continue;
196
0
    Sec->Live = false;
197
0
    Create = true;
198
0
199
0
    std::string Filename = toString(Sec->getFile<ELFT>());
200
0
    ArrayRef<uint8_t> D = Sec->Data;
201
0
202
0
    while (
!D.empty()0
) {
203
0
      if (
D.size() < sizeof(Elf_Mips_Options)0
) {
204
0
        error(Filename + ": invalid size of .MIPS.options section");
205
0
        break;
206
0
      }
207
0
208
0
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
209
0
      if (
Opt->kind == ODK_REGINFO0
) {
210
0
        if (
Config->Relocatable && 0
Opt->getRegInfo().ri_gp_value0
)
211
0
          error(Filename + ": unsupported non-zero ri_gp_value");
212
0
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
213
0
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
214
0
        break;
215
0
      }
216
0
217
0
      
if (0
!Opt->size0
)
218
0
        fatal(Filename + ": zero option descriptor size");
219
0
      D = D.slice(Opt->size);
220
0
    }
221
0
  };
222
0
223
0
  if (Create)
224
0
    return make<MipsOptionsSection<ELFT>>(Reginfo);
225
0
  return nullptr;
226
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
185
2
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
186
2
  // N64 ABI only.
187
2
  if (!ELFT::Is64Bits)
188
0
    return nullptr;
189
2
190
2
  Elf_Mips_RegInfo Reginfo = {};
191
2
  bool Create = false;
192
2
193
18
  for (InputSectionBase *Sec : InputSections) {
194
18
    if (Sec->Type != SHT_MIPS_OPTIONS)
195
16
      continue;
196
2
    Sec->Live = false;
197
2
    Create = true;
198
2
199
2
    std::string Filename = toString(Sec->getFile<ELFT>());
200
2
    ArrayRef<uint8_t> D = Sec->Data;
201
2
202
2
    while (
!D.empty()2
) {
203
2
      if (
D.size() < sizeof(Elf_Mips_Options)2
) {
204
0
        error(Filename + ": invalid size of .MIPS.options section");
205
0
        break;
206
0
      }
207
2
208
2
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
209
2
      if (
Opt->kind == ODK_REGINFO2
) {
210
2
        if (
Config->Relocatable && 2
Opt->getRegInfo().ri_gp_value0
)
211
0
          error(Filename + ": unsupported non-zero ri_gp_value");
212
2
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
213
2
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
214
2
        break;
215
2
      }
216
0
217
0
      
if (0
!Opt->size0
)
218
0
        fatal(Filename + ": zero option descriptor size");
219
0
      D = D.slice(Opt->size);
220
0
    }
221
2
  };
222
2
223
2
  if (Create)
224
2
    return make<MipsOptionsSection<ELFT>>(Reginfo);
225
0
  return nullptr;
226
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
185
28
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
186
28
  // N64 ABI only.
187
28
  if (!ELFT::Is64Bits)
188
0
    return nullptr;
189
28
190
28
  Elf_Mips_RegInfo Reginfo = {};
191
28
  bool Create = false;
192
28
193
310
  for (InputSectionBase *Sec : InputSections) {
194
310
    if (Sec->Type != SHT_MIPS_OPTIONS)
195
276
      continue;
196
34
    Sec->Live = false;
197
34
    Create = true;
198
34
199
34
    std::string Filename = toString(Sec->getFile<ELFT>());
200
34
    ArrayRef<uint8_t> D = Sec->Data;
201
34
202
34
    while (
!D.empty()34
) {
203
34
      if (
D.size() < sizeof(Elf_Mips_Options)34
) {
204
0
        error(Filename + ": invalid size of .MIPS.options section");
205
0
        break;
206
0
      }
207
34
208
34
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
209
34
      if (
Opt->kind == ODK_REGINFO34
) {
210
34
        if (
Config->Relocatable && 34
Opt->getRegInfo().ri_gp_value1
)
211
0
          error(Filename + ": unsupported non-zero ri_gp_value");
212
34
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
213
34
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
214
34
        break;
215
34
      }
216
0
217
0
      
if (0
!Opt->size0
)
218
0
        fatal(Filename + ": zero option descriptor size");
219
0
      D = D.slice(Opt->size);
220
0
    }
221
28
  };
222
28
223
28
  if (Create)
224
28
    return make<MipsOptionsSection<ELFT>>(Reginfo);
225
0
  return nullptr;
226
0
}
227
228
// MIPS .reginfo section.
229
template <class ELFT>
230
MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
231
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
232
105
      Reginfo(Reginfo) {
233
105
  this->Entsize = sizeof(Elf_Mips_RegInfo);
234
105
}
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::MipsReginfoSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, true> >)
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::MipsReginfoSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, false> >)
Line
Count
Source
232
92
      Reginfo(Reginfo) {
233
92
  this->Entsize = sizeof(Elf_Mips_RegInfo);
234
92
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::MipsReginfoSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, false> >)
Line
Count
Source
232
13
      Reginfo(Reginfo) {
233
13
  this->Entsize = sizeof(Elf_Mips_RegInfo);
234
13
}
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::MipsReginfoSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, true> >)
235
236
103
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
237
103
  if (!Config->Relocatable)
238
96
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
239
103
  memcpy(Buf, &Reginfo, sizeof(Reginfo));
240
103
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
236
13
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
237
13
  if (!Config->Relocatable)
238
13
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
239
13
  memcpy(Buf, &Reginfo, sizeof(Reginfo));
240
13
}
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
236
90
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
237
90
  if (!Config->Relocatable)
238
83
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
239
90
  memcpy(Buf, &Reginfo, sizeof(Reginfo));
240
90
}
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
241
242
template <class ELFT>
243
137
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
244
137
  // Section should be alive for O32 and N32 ABIs only.
245
137
  if (ELFT::Is64Bits)
246
30
    return nullptr;
247
107
248
107
  Elf_Mips_RegInfo Reginfo = {};
249
107
  bool Create = false;
250
107
251
1.16k
  for (InputSectionBase *Sec : InputSections) {
252
1.16k
    if (Sec->Type != SHT_MIPS_REGINFO)
253
1.03k
      continue;
254
137
    Sec->Live = false;
255
137
    Create = true;
256
137
257
137
    if (
Sec->Data.size() != sizeof(Elf_Mips_RegInfo)137
) {
258
0
      error(toString(Sec->getFile<ELFT>()) +
259
0
            ": invalid size of .reginfo section");
260
0
      return nullptr;
261
0
    }
262
137
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
263
137
    if (
Config->Relocatable && 137
R->ri_gp_value13
)
264
1
      error(toString(Sec->getFile<ELFT>()) +
265
1
            ": unsupported non-zero ri_gp_value");
266
1.16k
267
1.16k
    Reginfo.ri_gprmask |= R->ri_gprmask;
268
1.16k
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
269
107
  };
270
107
271
107
  if (Create)
272
105
    return make<MipsReginfoSection<ELFT>>(Reginfo);
273
2
  return nullptr;
274
2
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
243
28
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
244
28
  // Section should be alive for O32 and N32 ABIs only.
245
28
  if (ELFT::Is64Bits)
246
28
    return nullptr;
247
0
248
0
  Elf_Mips_RegInfo Reginfo = {};
249
0
  bool Create = false;
250
0
251
0
  for (InputSectionBase *Sec : InputSections) {
252
0
    if (Sec->Type != SHT_MIPS_REGINFO)
253
0
      continue;
254
0
    Sec->Live = false;
255
0
    Create = true;
256
0
257
0
    if (
Sec->Data.size() != sizeof(Elf_Mips_RegInfo)0
) {
258
0
      error(toString(Sec->getFile<ELFT>()) +
259
0
            ": invalid size of .reginfo section");
260
0
      return nullptr;
261
0
    }
262
0
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
263
0
    if (
Config->Relocatable && 0
R->ri_gp_value0
)
264
0
      error(toString(Sec->getFile<ELFT>()) +
265
0
            ": unsupported non-zero ri_gp_value");
266
0
267
0
    Reginfo.ri_gprmask |= R->ri_gprmask;
268
0
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
269
0
  };
270
0
271
0
  if (Create)
272
0
    return make<MipsReginfoSection<ELFT>>(Reginfo);
273
0
  return nullptr;
274
0
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
243
2
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
244
2
  // Section should be alive for O32 and N32 ABIs only.
245
2
  if (ELFT::Is64Bits)
246
2
    return nullptr;
247
0
248
0
  Elf_Mips_RegInfo Reginfo = {};
249
0
  bool Create = false;
250
0
251
0
  for (InputSectionBase *Sec : InputSections) {
252
0
    if (Sec->Type != SHT_MIPS_REGINFO)
253
0
      continue;
254
0
    Sec->Live = false;
255
0
    Create = true;
256
0
257
0
    if (
Sec->Data.size() != sizeof(Elf_Mips_RegInfo)0
) {
258
0
      error(toString(Sec->getFile<ELFT>()) +
259
0
            ": invalid size of .reginfo section");
260
0
      return nullptr;
261
0
    }
262
0
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
263
0
    if (
Config->Relocatable && 0
R->ri_gp_value0
)
264
0
      error(toString(Sec->getFile<ELFT>()) +
265
0
            ": unsupported non-zero ri_gp_value");
266
0
267
0
    Reginfo.ri_gprmask |= R->ri_gprmask;
268
0
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
269
0
  };
270
0
271
0
  if (Create)
272
0
    return make<MipsReginfoSection<ELFT>>(Reginfo);
273
0
  return nullptr;
274
0
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
243
14
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
244
14
  // Section should be alive for O32 and N32 ABIs only.
245
14
  if (ELFT::Is64Bits)
246
0
    return nullptr;
247
14
248
14
  Elf_Mips_RegInfo Reginfo = {};
249
14
  bool Create = false;
250
14
251
128
  for (InputSectionBase *Sec : InputSections) {
252
128
    if (Sec->Type != SHT_MIPS_REGINFO)
253
114
      continue;
254
14
    Sec->Live = false;
255
14
    Create = true;
256
14
257
14
    if (
Sec->Data.size() != sizeof(Elf_Mips_RegInfo)14
) {
258
0
      error(toString(Sec->getFile<ELFT>()) +
259
0
            ": invalid size of .reginfo section");
260
0
      return nullptr;
261
0
    }
262
14
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
263
14
    if (
Config->Relocatable && 14
R->ri_gp_value0
)
264
0
      error(toString(Sec->getFile<ELFT>()) +
265
0
            ": unsupported non-zero ri_gp_value");
266
128
267
128
    Reginfo.ri_gprmask |= R->ri_gprmask;
268
128
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
269
14
  };
270
14
271
14
  if (Create)
272
13
    return make<MipsReginfoSection<ELFT>>(Reginfo);
273
1
  return nullptr;
274
1
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
243
93
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
244
93
  // Section should be alive for O32 and N32 ABIs only.
245
93
  if (ELFT::Is64Bits)
246
0
    return nullptr;
247
93
248
93
  Elf_Mips_RegInfo Reginfo = {};
249
93
  bool Create = false;
250
93
251
1.04k
  for (InputSectionBase *Sec : InputSections) {
252
1.04k
    if (Sec->Type != SHT_MIPS_REGINFO)
253
917
      continue;
254
123
    Sec->Live = false;
255
123
    Create = true;
256
123
257
123
    if (
Sec->Data.size() != sizeof(Elf_Mips_RegInfo)123
) {
258
0
      error(toString(Sec->getFile<ELFT>()) +
259
0
            ": invalid size of .reginfo section");
260
0
      return nullptr;
261
0
    }
262
123
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
263
123
    if (
Config->Relocatable && 123
R->ri_gp_value13
)
264
1
      error(toString(Sec->getFile<ELFT>()) +
265
1
            ": unsupported non-zero ri_gp_value");
266
1.04k
267
1.04k
    Reginfo.ri_gprmask |= R->ri_gprmask;
268
1.04k
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
269
93
  };
270
93
271
93
  if (Create)
272
92
    return make<MipsReginfoSection<ELFT>>(Reginfo);
273
1
  return nullptr;
274
1
}
275
276
4
InputSection *elf::createInterpSection() {
277
4
  // StringSaver guarantees that the returned string ends with '\0'.
278
4
  StringRef S = Saver.save(Config->DynamicLinker);
279
4
  ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1};
280
4
281
4
  auto *Sec =
282
4
      make<InputSection>(SHF_ALLOC, SHT_PROGBITS, 1, Contents, ".interp");
283
4
  Sec->Live = true;
284
4
  return Sec;
285
4
}
286
287
SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
288
162
                                   uint64_t Size, InputSectionBase *Section) {
289
162
  auto *S = make<DefinedRegular>(Name, /*IsLocal*/ true, STV_DEFAULT, Type,
290
162
                                 Value, Size, Section);
291
162
  if (InX::SymTab)
292
162
    InX::SymTab->addSymbol(S);
293
162
  return S;
294
162
}
295
296
12
static size_t getHashSize() {
297
12
  switch (Config->BuildId) {
298
4
  case BuildIdKind::Fast:
299
4
    return 8;
300
3
  case BuildIdKind::Md5:
301
3
  case BuildIdKind::Uuid:
302
3
    return 16;
303
4
  case BuildIdKind::Sha1:
304
4
    return 20;
305
1
  case BuildIdKind::Hexstring:
306
1
    return Config->BuildIdVector.size();
307
0
  default:
308
0
    llvm_unreachable("unknown BuildIdKind");
309
0
  }
310
0
}
311
312
BuildIdSection::BuildIdSection()
313
    : SyntheticSection(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"),
314
12
      HashSize(getHashSize()) {}
315
316
12
void BuildIdSection::writeTo(uint8_t *Buf) {
317
12
  endianness E = Config->Endianness;
318
12
  write32(Buf, 4, E);                   // Name size
319
12
  write32(Buf + 4, HashSize, E);        // Content size
320
12
  write32(Buf + 8, NT_GNU_BUILD_ID, E); // Type
321
12
  memcpy(Buf + 12, "GNU", 4);           // Name string
322
12
  HashBuf = Buf + 16;
323
12
}
324
325
// Split one uint8 array into small pieces of uint8 arrays.
326
static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
327
10
                                            size_t ChunkSize) {
328
10
  std::vector<ArrayRef<uint8_t>> Ret;
329
10
  while (
Arr.size() > ChunkSize10
) {
330
0
    Ret.push_back(Arr.take_front(ChunkSize));
331
0
    Arr = Arr.drop_front(ChunkSize);
332
0
  }
333
10
  if (!Arr.empty())
334
10
    Ret.push_back(Arr);
335
10
  return Ret;
336
10
}
337
338
// Computes a hash value of Data using a given hash function.
339
// In order to utilize multiple cores, we first split data into 1MB
340
// chunks, compute a hash for each chunk, and then compute a hash value
341
// of the hash values.
342
void BuildIdSection::computeHash(
343
    llvm::ArrayRef<uint8_t> Data,
344
10
    std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) {
345
10
  std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
346
10
  std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
347
10
348
10
  // Compute hash values.
349
10
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
350
10
    HashFn(Hashes.data() + I * HashSize, Chunks[I]);
351
10
  });
352
10
353
10
  // Write to the final output buffer.
354
10
  HashFn(HashBuf, Hashes);
355
10
}
356
357
BssSection::BssSection(StringRef Name)
358
3.29k
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 0, Name) {}
359
360
113
size_t BssSection::reserveSpace(uint64_t Size, uint32_t Alignment) {
361
113
  if (OutputSection *Sec = getParent())
362
0
    Sec->updateAlignment(Alignment);
363
113
  this->Size = alignTo(this->Size, Alignment) + Size;
364
113
  this->Alignment = std::max(this->Alignment, Alignment);
365
113
  return this->Size - Size;
366
113
}
367
368
12
void BuildIdSection::writeBuildId(ArrayRef<uint8_t> Buf) {
369
12
  switch (Config->BuildId) {
370
4
  case BuildIdKind::Fast:
371
8
    computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
372
8
      write64le(Dest, xxHash64(toStringRef(Arr)));
373
8
    });
374
4
    break;
375
2
  case BuildIdKind::Md5:
376
4
    computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
377
4
      memcpy(Dest, MD5::hash(Arr).data(), 16);
378
4
    });
379
2
    break;
380
4
  case BuildIdKind::Sha1:
381
8
    computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
382
8
      memcpy(Dest, SHA1::hash(Arr).data(), 20);
383
8
    });
384
4
    break;
385
1
  case BuildIdKind::Uuid:
386
1
    if (getRandomBytes(HashBuf, HashSize))
387
0
      error("entropy source failure");
388
1
    break;
389
1
  case BuildIdKind::Hexstring:
390
1
    memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
391
1
    break;
392
0
  default:
393
0
    llvm_unreachable("unknown BuildIdKind");
394
12
  }
395
12
}
396
397
template <class ELFT>
398
EhFrameSection<ELFT>::EhFrameSection()
399
1.52k
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::EhFrameSection()
Line
Count
Source
399
43
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::EhFrameSection()
Line
Count
Source
399
87
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::EhFrameSection()
Line
Count
Source
399
1.21k
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::EhFrameSection()
Line
Count
Source
399
185
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
400
401
// Search for an existing CIE record or create a new one.
402
// CIE records from input object files are uniquified by their contents
403
// and where their relocations point to.
404
template <class ELFT>
405
template <class RelTy>
406
CieRecord *EhFrameSection<ELFT>::addCie(EhSectionPiece &Cie,
407
86
                                        ArrayRef<RelTy> Rels) {
408
86
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
86
  const endianness E = ELFT::TargetEndianness;
410
86
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
86
413
86
  SymbolBody *Personality = nullptr;
414
86
  unsigned FirstRelI = Cie.FirstRelocation;
415
86
  if (FirstRelI != (unsigned)-1)
416
9
    Personality =
417
9
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
86
419
86
  // Search for an existing CIE by CIE contents/relocation target pair.
420
86
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
86
422
86
  // If not found, create a new one.
423
86
  if (
!Rec86
) {
424
76
    Rec = make<CieRecord>();
425
76
    Rec->Cie = &Cie;
426
76
    CieRecords.push_back(Rec);
427
76
  }
428
86
  return Rec;
429
86
}
lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
407
78
                                        ArrayRef<RelTy> Rels) {
408
78
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
78
  const endianness E = ELFT::TargetEndianness;
410
78
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
78
413
78
  SymbolBody *Personality = nullptr;
414
78
  unsigned FirstRelI = Cie.FirstRelocation;
415
78
  if (FirstRelI != (unsigned)-1)
416
7
    Personality =
417
7
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
78
419
78
  // Search for an existing CIE by CIE contents/relocation target pair.
420
78
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
78
422
78
  // If not found, create a new one.
423
78
  if (
!Rec78
) {
424
69
    Rec = make<CieRecord>();
425
69
    Rec->Cie = &Cie;
426
69
    CieRecords.push_back(Rec);
427
69
  }
428
78
  return Rec;
429
78
}
Unexecuted instantiation: lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Line
Count
Source
407
3
                                        ArrayRef<RelTy> Rels) {
408
3
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
3
  const endianness E = ELFT::TargetEndianness;
410
3
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
3
413
3
  SymbolBody *Personality = nullptr;
414
3
  unsigned FirstRelI = Cie.FirstRelocation;
415
3
  if (FirstRelI != (unsigned)-1)
416
0
    Personality =
417
0
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
3
419
3
  // Search for an existing CIE by CIE contents/relocation target pair.
420
3
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
3
422
3
  // If not found, create a new one.
423
3
  if (
!Rec3
) {
424
3
    Rec = make<CieRecord>();
425
3
    Rec->Cie = &Cie;
426
3
    CieRecords.push_back(Rec);
427
3
  }
428
3
  return Rec;
429
3
}
Unexecuted instantiation: lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
Unexecuted instantiation: lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
407
4
                                        ArrayRef<RelTy> Rels) {
408
4
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
4
  const endianness E = ELFT::TargetEndianness;
410
4
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
4
413
4
  SymbolBody *Personality = nullptr;
414
4
  unsigned FirstRelI = Cie.FirstRelocation;
415
4
  if (FirstRelI != (unsigned)-1)
416
2
    Personality =
417
2
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
4
419
4
  // Search for an existing CIE by CIE contents/relocation target pair.
420
4
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
4
422
4
  // If not found, create a new one.
423
4
  if (
!Rec4
) {
424
3
    Rec = make<CieRecord>();
425
3
    Rec->Cie = &Cie;
426
3
    CieRecords.push_back(Rec);
427
3
  }
428
4
  return Rec;
429
4
}
Unexecuted instantiation: lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
lld::elf::CieRecord* lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addCie<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
407
1
                                        ArrayRef<RelTy> Rels) {
408
1
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
1
  const endianness E = ELFT::TargetEndianness;
410
1
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
1
413
1
  SymbolBody *Personality = nullptr;
414
1
  unsigned FirstRelI = Cie.FirstRelocation;
415
1
  if (FirstRelI != (unsigned)-1)
416
0
    Personality =
417
0
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
1
419
1
  // Search for an existing CIE by CIE contents/relocation target pair.
420
1
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
1
422
1
  // If not found, create a new one.
423
1
  if (
!Rec1
) {
424
1
    Rec = make<CieRecord>();
425
1
    Rec->Cie = &Cie;
426
1
    CieRecords.push_back(Rec);
427
1
  }
428
1
  return Rec;
429
1
}
430
431
// There is one FDE per function. Returns true if a given FDE
432
// points to a live function.
433
template <class ELFT>
434
template <class RelTy>
435
bool EhFrameSection<ELFT>::isFdeLive(EhSectionPiece &Fde,
436
112
                                     ArrayRef<RelTy> Rels) {
437
112
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
112
  unsigned FirstRelI = Fde.FirstRelocation;
439
112
440
112
  // An FDE should point to some function because FDEs are to describe
441
112
  // functions. That's however not always the case due to an issue of
442
112
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
112
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
112
  // To deal with that, we ignore such FDEs.
445
112
  if (FirstRelI == (unsigned)-1)
446
1
    return false;
447
111
448
111
  const RelTy &Rel = Rels[FirstRelI];
449
111
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
111
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
110
    
if (110
D->Section110
)
452
110
      return cast<InputSectionBase>(D->Section)->Repl->Live;
453
1
  return false;
454
1
}
bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
436
105
                                     ArrayRef<RelTy> Rels) {
437
105
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
105
  unsigned FirstRelI = Fde.FirstRelocation;
439
105
440
105
  // An FDE should point to some function because FDEs are to describe
441
105
  // functions. That's however not always the case due to an issue of
442
105
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
105
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
105
  // To deal with that, we ignore such FDEs.
445
105
  if (FirstRelI == (unsigned)-1)
446
1
    return false;
447
104
448
104
  const RelTy &Rel = Rels[FirstRelI];
449
104
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
104
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
103
    
if (103
D->Section103
)
452
103
      return cast<InputSectionBase>(D->Section)->Repl->Live;
453
1
  return false;
454
1
}
Unexecuted instantiation: bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Line
Count
Source
436
2
                                     ArrayRef<RelTy> Rels) {
437
2
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
2
  unsigned FirstRelI = Fde.FirstRelocation;
439
2
440
2
  // An FDE should point to some function because FDEs are to describe
441
2
  // functions. That's however not always the case due to an issue of
442
2
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
2
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
2
  // To deal with that, we ignore such FDEs.
445
2
  if (FirstRelI == (unsigned)-1)
446
0
    return false;
447
2
448
2
  const RelTy &Rel = Rels[FirstRelI];
449
2
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
2
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
2
    
if (2
D->Section2
)
452
2
      return cast<InputSectionBase>(D->Section)->Repl->Live;
453
0
  return false;
454
0
}
Unexecuted instantiation: bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
Unexecuted instantiation: bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
436
4
                                     ArrayRef<RelTy> Rels) {
437
4
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
4
  unsigned FirstRelI = Fde.FirstRelocation;
439
4
440
4
  // An FDE should point to some function because FDEs are to describe
441
4
  // functions. That's however not always the case due to an issue of
442
4
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
4
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
4
  // To deal with that, we ignore such FDEs.
445
4
  if (FirstRelI == (unsigned)-1)
446
0
    return false;
447
4
448
4
  const RelTy &Rel = Rels[FirstRelI];
449
4
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
4
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
4
    
if (4
D->Section4
)
452
4
      return cast<InputSectionBase>(D->Section)->Repl->Live;
453
0
  return false;
454
0
}
Unexecuted instantiation: bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
bool lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::isFdeLive<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
436
1
                                     ArrayRef<RelTy> Rels) {
437
1
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
1
  unsigned FirstRelI = Fde.FirstRelocation;
439
1
440
1
  // An FDE should point to some function because FDEs are to describe
441
1
  // functions. That's however not always the case due to an issue of
442
1
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
1
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
1
  // To deal with that, we ignore such FDEs.
445
1
  if (FirstRelI == (unsigned)-1)
446
0
    return false;
447
1
448
1
  const RelTy &Rel = Rels[FirstRelI];
449
1
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
1
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
1
    
if (1
D->Section1
)
452
1
      return cast<InputSectionBase>(D->Section)->Repl->Live;
453
0
  return false;
454
0
}
455
456
// .eh_frame is a sequence of CIE or FDE records. In general, there
457
// is one CIE record per input object file which is followed by
458
// a list of FDEs. This function searches an existing CIE or create a new
459
// one and associates FDEs to the CIE.
460
template <class ELFT>
461
template <class RelTy>
462
void EhFrameSection<ELFT>::addSectionAux(EhInputSection *Sec,
463
89
                                         ArrayRef<RelTy> Rels) {
464
89
  const endianness E = ELFT::TargetEndianness;
465
89
466
89
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
203
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
203
    // The empty record is the end marker.
469
203
    if (Piece.Size == 4)
470
5
      return;
471
198
472
198
    size_t Offset = Piece.InputOff;
473
198
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
198
    if (
ID == 0198
) {
475
86
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
86
      continue;
477
86
    }
478
112
479
112
    uint32_t CieOffset = Offset + 4 - ID;
480
112
    CieRecord *Rec = OffsetToCie[CieOffset];
481
112
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
112
484
112
    
if (112
!isFdeLive(Piece, Rels)112
)
485
7
      continue;
486
105
    Rec->Fdes.push_back(&Piece);
487
105
    NumFdes++;
488
105
  }
489
89
}
void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
Line
Count
Source
463
1
                                         ArrayRef<RelTy> Rels) {
464
1
  const endianness E = ELFT::TargetEndianness;
465
1
466
1
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
2
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
2
    // The empty record is the end marker.
469
2
    if (Piece.Size == 4)
470
0
      return;
471
2
472
2
    size_t Offset = Piece.InputOff;
473
2
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
2
    if (
ID == 02
) {
475
1
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
1
      continue;
477
1
    }
478
1
479
1
    uint32_t CieOffset = Offset + 4 - ID;
480
1
    CieRecord *Rec = OffsetToCie[CieOffset];
481
1
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
1
484
1
    
if (1
!isFdeLive(Piece, Rels)1
)
485
0
      continue;
486
1
    Rec->Fdes.push_back(&Piece);
487
1
    NumFdes++;
488
1
  }
489
1
}
Unexecuted instantiation: void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Unexecuted instantiation: void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
463
4
                                         ArrayRef<RelTy> Rels) {
464
4
  const endianness E = ELFT::TargetEndianness;
465
4
466
4
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
8
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
8
    // The empty record is the end marker.
469
8
    if (Piece.Size == 4)
470
0
      return;
471
8
472
8
    size_t Offset = Piece.InputOff;
473
8
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
8
    if (
ID == 08
) {
475
4
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
4
      continue;
477
4
    }
478
4
479
4
    uint32_t CieOffset = Offset + 4 - ID;
480
4
    CieRecord *Rec = OffsetToCie[CieOffset];
481
4
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
4
484
4
    
if (4
!isFdeLive(Piece, Rels)4
)
485
0
      continue;
486
4
    Rec->Fdes.push_back(&Piece);
487
4
    NumFdes++;
488
4
  }
489
4
}
Unexecuted instantiation: void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
Unexecuted instantiation: void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >)
void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >)
Line
Count
Source
463
3
                                         ArrayRef<RelTy> Rels) {
464
3
  const endianness E = ELFT::TargetEndianness;
465
3
466
3
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
5
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
5
    // The empty record is the end marker.
469
5
    if (Piece.Size == 4)
470
0
      return;
471
5
472
5
    size_t Offset = Piece.InputOff;
473
5
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
5
    if (
ID == 05
) {
475
3
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
3
      continue;
477
3
    }
478
2
479
2
    uint32_t CieOffset = Offset + 4 - ID;
480
2
    CieRecord *Rec = OffsetToCie[CieOffset];
481
2
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
2
484
2
    
if (2
!isFdeLive(Piece, Rels)2
)
485
0
      continue;
486
2
    Rec->Fdes.push_back(&Piece);
487
2
    NumFdes++;
488
2
  }
489
3
}
void lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSectionAux<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >)
Line
Count
Source
463
81
                                         ArrayRef<RelTy> Rels) {
464
81
  const endianness E = ELFT::TargetEndianness;
465
81
466
81
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
188
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
188
    // The empty record is the end marker.
469
188
    if (Piece.Size == 4)
470
5
      return;
471
183
472
183
    size_t Offset = Piece.InputOff;
473
183
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
183
    if (
ID == 0183
) {
475
78
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
78
      continue;
477
78
    }
478
105
479
105
    uint32_t CieOffset = Offset + 4 - ID;
480
105
    CieRecord *Rec = OffsetToCie[CieOffset];
481
105
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
105
484
105
    
if (105
!isFdeLive(Piece, Rels)105
)
485
7
      continue;
486
98
    Rec->Fdes.push_back(&Piece);
487
98
    NumFdes++;
488
98
  }
489
81
}
490
491
template <class ELFT>
492
92
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
92
  auto *Sec = cast<EhInputSection>(C);
494
92
  Sec->Parent = this;
495
92
  updateAlignment(Sec->Alignment);
496
92
  Sections.push_back(Sec);
497
92
  for (auto *DS : Sec->DependentSections)
498
1
    DependentSections.push_back(DS);
499
92
500
92
  // .eh_frame is a sequence of CIE or FDE records. This function
501
92
  // splits it into pieces so that we can call
502
92
  // SplitInputSection::getSectionPiece on the section.
503
92
  Sec->split<ELFT>();
504
92
  if (Sec->Pieces.empty())
505
3
    return;
506
89
507
89
  
if (89
Sec->NumRelocations == 089
)
508
7
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
82
  else 
if (82
Sec->AreRelocsRela82
)
510
77
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
82
  else
512
5
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
92
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSection(lld::elf::InputSectionBase*)
Line
Count
Source
492
3
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
3
  auto *Sec = cast<EhInputSection>(C);
494
3
  Sec->Parent = this;
495
3
  updateAlignment(Sec->Alignment);
496
3
  Sections.push_back(Sec);
497
3
  for (auto *DS : Sec->DependentSections)
498
0
    DependentSections.push_back(DS);
499
3
500
3
  // .eh_frame is a sequence of CIE or FDE records. This function
501
3
  // splits it into pieces so that we can call
502
3
  // SplitInputSection::getSectionPiece on the section.
503
3
  Sec->split<ELFT>();
504
3
  if (Sec->Pieces.empty())
505
0
    return;
506
3
507
3
  
if (3
Sec->NumRelocations == 03
)
508
1
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
2
  else 
if (2
Sec->AreRelocsRela2
)
510
2
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
2
  else
512
0
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
3
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSection(lld::elf::InputSectionBase*)
Line
Count
Source
492
84
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
84
  auto *Sec = cast<EhInputSection>(C);
494
84
  Sec->Parent = this;
495
84
  updateAlignment(Sec->Alignment);
496
84
  Sections.push_back(Sec);
497
84
  for (auto *DS : Sec->DependentSections)
498
1
    DependentSections.push_back(DS);
499
84
500
84
  // .eh_frame is a sequence of CIE or FDE records. This function
501
84
  // splits it into pieces so that we can call
502
84
  // SplitInputSection::getSectionPiece on the section.
503
84
  Sec->split<ELFT>();
504
84
  if (Sec->Pieces.empty())
505
3
    return;
506
81
507
81
  
if (81
Sec->NumRelocations == 081
)
508
6
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
75
  else 
if (75
Sec->AreRelocsRela75
)
510
75
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
75
  else
512
0
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
84
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSection(lld::elf::InputSectionBase*)
Line
Count
Source
492
1
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
1
  auto *Sec = cast<EhInputSection>(C);
494
1
  Sec->Parent = this;
495
1
  updateAlignment(Sec->Alignment);
496
1
  Sections.push_back(Sec);
497
1
  for (auto *DS : Sec->DependentSections)
498
0
    DependentSections.push_back(DS);
499
1
500
1
  // .eh_frame is a sequence of CIE or FDE records. This function
501
1
  // splits it into pieces so that we can call
502
1
  // SplitInputSection::getSectionPiece on the section.
503
1
  Sec->split<ELFT>();
504
1
  if (Sec->Pieces.empty())
505
0
    return;
506
1
507
1
  
if (1
Sec->NumRelocations == 01
)
508
0
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
1
  else 
if (1
Sec->AreRelocsRela1
)
510
0
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
1
  else
512
1
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
1
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSection(lld::elf::InputSectionBase*)
Line
Count
Source
492
4
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
4
  auto *Sec = cast<EhInputSection>(C);
494
4
  Sec->Parent = this;
495
4
  updateAlignment(Sec->Alignment);
496
4
  Sections.push_back(Sec);
497
4
  for (auto *DS : Sec->DependentSections)
498
0
    DependentSections.push_back(DS);
499
4
500
4
  // .eh_frame is a sequence of CIE or FDE records. This function
501
4
  // splits it into pieces so that we can call
502
4
  // SplitInputSection::getSectionPiece on the section.
503
4
  Sec->split<ELFT>();
504
4
  if (Sec->Pieces.empty())
505
0
    return;
506
4
507
4
  
if (4
Sec->NumRelocations == 04
)
508
0
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
4
  else 
if (4
Sec->AreRelocsRela4
)
510
0
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
4
  else
512
4
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
4
}
514
515
template <class ELFT>
516
173
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
173
  memcpy(Buf, D.data(), D.size());
518
173
519
173
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
173
521
173
  // Zero-clear trailing padding if it exists.
522
173
  memset(Buf + D.size(), 0, Aligned - D.size());
523
173
524
173
  // Fix the size field. -4 since size does not include the size field itself.
525
173
  const endianness E = ELFT::TargetEndianness;
526
173
  write32<E>(Buf, Aligned - 4);
527
173
}
SyntheticSections.cpp:void writeCieFde<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, llvm::ArrayRef<unsigned char>)
Line
Count
Source
516
2
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
2
  memcpy(Buf, D.data(), D.size());
518
2
519
2
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
2
521
2
  // Zero-clear trailing padding if it exists.
522
2
  memset(Buf + D.size(), 0, Aligned - D.size());
523
2
524
2
  // Fix the size field. -4 since size does not include the size field itself.
525
2
  const endianness E = ELFT::TargetEndianness;
526
2
  write32<E>(Buf, Aligned - 4);
527
2
}
SyntheticSections.cpp:void writeCieFde<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, llvm::ArrayRef<unsigned char>)
Line
Count
Source
516
159
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
159
  memcpy(Buf, D.data(), D.size());
518
159
519
159
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
159
521
159
  // Zero-clear trailing padding if it exists.
522
159
  memset(Buf + D.size(), 0, Aligned - D.size());
523
159
524
159
  // Fix the size field. -4 since size does not include the size field itself.
525
159
  const endianness E = ELFT::TargetEndianness;
526
159
  write32<E>(Buf, Aligned - 4);
527
159
}
SyntheticSections.cpp:void writeCieFde<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, llvm::ArrayRef<unsigned char>)
Line
Count
Source
516
7
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
7
  memcpy(Buf, D.data(), D.size());
518
7
519
7
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
7
521
7
  // Zero-clear trailing padding if it exists.
522
7
  memset(Buf + D.size(), 0, Aligned - D.size());
523
7
524
7
  // Fix the size field. -4 since size does not include the size field itself.
525
7
  const endianness E = ELFT::TargetEndianness;
526
7
  write32<E>(Buf, Aligned - 4);
527
7
}
SyntheticSections.cpp:void writeCieFde<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, llvm::ArrayRef<unsigned char>)
Line
Count
Source
516
5
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
5
  memcpy(Buf, D.data(), D.size());
518
5
519
5
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
5
521
5
  // Zero-clear trailing padding if it exists.
522
5
  memset(Buf + D.size(), 0, Aligned - D.size());
523
5
524
5
  // Fix the size field. -4 since size does not include the size field itself.
525
5
  const endianness E = ELFT::TargetEndianness;
526
5
  write32<E>(Buf, Aligned - 4);
527
5
}
528
529
79
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
79
  if (this->Size)
531
0
    return; // Already finalized.
532
79
533
79
  size_t Off = 0;
534
74
  for (CieRecord *Rec : CieRecords) {
535
74
    Rec->Cie->OutputOff = Off;
536
74
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
74
538
103
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
103
      Fde->OutputOff = Off;
540
103
      Off += alignTo(Fde->Size, Config->Wordsize);
541
103
    }
542
74
  }
543
79
544
79
  // The LSB standard does not allow a .eh_frame section with zero
545
79
  // Call Frame Information records. Therefore add a CIE record length
546
79
  // 0 as a terminator if this .eh_frame section is empty.
547
79
  if (Off == 0)
548
6
    Off = 4;
549
79
550
79
  this->Size = Off;
551
79
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
529
72
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
72
  if (this->Size)
531
0
    return; // Already finalized.
532
72
533
72
  size_t Off = 0;
534
67
  for (CieRecord *Rec : CieRecords) {
535
67
    Rec->Cie->OutputOff = Off;
536
67
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
67
538
96
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
96
      Fde->OutputOff = Off;
540
96
      Off += alignTo(Fde->Size, Config->Wordsize);
541
96
    }
542
67
  }
543
72
544
72
  // The LSB standard does not allow a .eh_frame section with zero
545
72
  // Call Frame Information records. Therefore add a CIE record length
546
72
  // 0 as a terminator if this .eh_frame section is empty.
547
72
  if (Off == 0)
548
6
    Off = 4;
549
72
550
72
  this->Size = Off;
551
72
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
529
1
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
1
  if (this->Size)
531
0
    return; // Already finalized.
532
1
533
1
  size_t Off = 0;
534
1
  for (CieRecord *Rec : CieRecords) {
535
1
    Rec->Cie->OutputOff = Off;
536
1
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
1
538
1
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
1
      Fde->OutputOff = Off;
540
1
      Off += alignTo(Fde->Size, Config->Wordsize);
541
1
    }
542
1
  }
543
1
544
1
  // The LSB standard does not allow a .eh_frame section with zero
545
1
  // Call Frame Information records. Therefore add a CIE record length
546
1
  // 0 as a terminator if this .eh_frame section is empty.
547
1
  if (Off == 0)
548
0
    Off = 4;
549
1
550
1
  this->Size = Off;
551
1
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
529
3
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
3
  if (this->Size)
531
0
    return; // Already finalized.
532
3
533
3
  size_t Off = 0;
534
3
  for (CieRecord *Rec : CieRecords) {
535
3
    Rec->Cie->OutputOff = Off;
536
3
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
3
538
4
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
4
      Fde->OutputOff = Off;
540
4
      Off += alignTo(Fde->Size, Config->Wordsize);
541
4
    }
542
3
  }
543
3
544
3
  // The LSB standard does not allow a .eh_frame section with zero
545
3
  // Call Frame Information records. Therefore add a CIE record length
546
3
  // 0 as a terminator if this .eh_frame section is empty.
547
3
  if (Off == 0)
548
0
    Off = 4;
549
3
550
3
  this->Size = Off;
551
3
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
529
3
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
3
  if (this->Size)
531
0
    return; // Already finalized.
532
3
533
3
  size_t Off = 0;
534
3
  for (CieRecord *Rec : CieRecords) {
535
3
    Rec->Cie->OutputOff = Off;
536
3
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
3
538
2
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
2
      Fde->OutputOff = Off;
540
2
      Off += alignTo(Fde->Size, Config->Wordsize);
541
2
    }
542
3
  }
543
3
544
3
  // The LSB standard does not allow a .eh_frame section with zero
545
3
  // Call Frame Information records. Therefore add a CIE record length
546
3
  // 0 as a terminator if this .eh_frame section is empty.
547
3
  if (Off == 0)
548
0
    Off = 4;
549
3
550
3
  this->Size = Off;
551
3
}
552
553
16
template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
554
16
  const endianness E = ELFT::TargetEndianness;
555
16
  switch (Size) {
556
0
  case DW_EH_PE_udata2:
557
0
    return read16<E>(Buf);
558
14
  case DW_EH_PE_udata4:
559
14
    return read32<E>(Buf);
560
2
  case DW_EH_PE_udata8:
561
2
    return read64<E>(Buf);
562
0
  case DW_EH_PE_absptr:
563
0
    if (ELFT::Is64Bits)
564
0
      return read64<E>(Buf);
565
0
    return read32<E>(Buf);
566
0
  }
567
0
  fatal("unknown FDE size encoding");
568
0
}
SyntheticSections.cpp:unsigned long long readFdeAddr<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned char*, int)
Line
Count
Source
553
13
template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
554
13
  const endianness E = ELFT::TargetEndianness;
555
13
  switch (Size) {
556
0
  case DW_EH_PE_udata2:
557
0
    return read16<E>(Buf);
558
13
  case DW_EH_PE_udata4:
559
13
    return read32<E>(Buf);
560
0
  case DW_EH_PE_udata8:
561
0
    return read64<E>(Buf);
562
0
  case DW_EH_PE_absptr:
563
0
    if (ELFT::Is64Bits)
564
0
      return read64<E>(Buf);
565
0
    return read32<E>(Buf);
566
0
  }
567
0
  fatal("unknown FDE size encoding");
568
0
}
Unexecuted instantiation: SyntheticSections.cpp:unsigned long long readFdeAddr<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, int)
SyntheticSections.cpp:unsigned long long readFdeAddr<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned char*, int)
Line
Count
Source
553
1
template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
554
1
  const endianness E = ELFT::TargetEndianness;
555
1
  switch (Size) {
556
0
  case DW_EH_PE_udata2:
557
0
    return read16<E>(Buf);
558
1
  case DW_EH_PE_udata4:
559
1
    return read32<E>(Buf);
560
0
  case DW_EH_PE_udata8:
561
0
    return read64<E>(Buf);
562
0
  case DW_EH_PE_absptr:
563
0
    if (ELFT::Is64Bits)
564
0
      return read64<E>(Buf);
565
0
    return read32<E>(Buf);
566
0
  }
567
0
  fatal("unknown FDE size encoding");
568
0
}
SyntheticSections.cpp:unsigned long long readFdeAddr<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned char*, int)
Line
Count
Source
553
2
template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
554
2
  const endianness E = ELFT::TargetEndianness;
555
2
  switch (Size) {
556
0
  case DW_EH_PE_udata2:
557
0
    return read16<E>(Buf);
558
0
  case DW_EH_PE_udata4:
559
0
    return read32<E>(Buf);
560
2
  case DW_EH_PE_udata8:
561
2
    return read64<E>(Buf);
562
0
  case DW_EH_PE_absptr:
563
0
    if (ELFT::Is64Bits)
564
0
      return read64<E>(Buf);
565
0
    return read32<E>(Buf);
566
0
  }
567
0
  fatal("unknown FDE size encoding");
568
0
}
569
570
// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
571
// We need it to create .eh_frame_hdr section.
572
template <class ELFT>
573
uint64_t EhFrameSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
574
16
                                        uint8_t Enc) {
575
16
  // The starting address to which this FDE applies is
576
16
  // stored at FDE + 8 byte.
577
16
  size_t Off = FdeOff + 8;
578
16
  uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
579
16
  if ((Enc & 0x70) == DW_EH_PE_absptr)
580
3
    return Addr;
581
13
  
if (13
(Enc & 0x70) == DW_EH_PE_pcrel13
)
582
13
    return Addr + getParent()->Addr + Off;
583
0
  fatal("unknown FDE size relative encoding");
584
0
}
Unexecuted instantiation: lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getFdePc(unsigned char*, unsigned long, unsigned char)
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getFdePc(unsigned char*, unsigned long, unsigned char)
Line
Count
Source
574
2
                                        uint8_t Enc) {
575
2
  // The starting address to which this FDE applies is
576
2
  // stored at FDE + 8 byte.
577
2
  size_t Off = FdeOff + 8;
578
2
  uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
579
2
  if ((Enc & 0x70) == DW_EH_PE_absptr)
580
2
    return Addr;
581
0
  
if (0
(Enc & 0x70) == DW_EH_PE_pcrel0
)
582
0
    return Addr + getParent()->Addr + Off;
583
0
  fatal("unknown FDE size relative encoding");
584
0
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getFdePc(unsigned char*, unsigned long, unsigned char)
Line
Count
Source
574
1
                                        uint8_t Enc) {
575
1
  // The starting address to which this FDE applies is
576
1
  // stored at FDE + 8 byte.
577
1
  size_t Off = FdeOff + 8;
578
1
  uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
579
1
  if ((Enc & 0x70) == DW_EH_PE_absptr)
580
1
    return Addr;
581
0
  
if (0
(Enc & 0x70) == DW_EH_PE_pcrel0
)
582
0
    return Addr + getParent()->Addr + Off;
583
0
  fatal("unknown FDE size relative encoding");
584
0
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getFdePc(unsigned char*, unsigned long, unsigned char)
Line
Count
Source
574
13
                                        uint8_t Enc) {
575
13
  // The starting address to which this FDE applies is
576
13
  // stored at FDE + 8 byte.
577
13
  size_t Off = FdeOff + 8;
578
13
  uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
579
13
  if ((Enc & 0x70) == DW_EH_PE_absptr)
580
0
    return Addr;
581
13
  
if (13
(Enc & 0x70) == DW_EH_PE_pcrel13
)
582
13
    return Addr + getParent()->Addr + Off;
583
0
  fatal("unknown FDE size relative encoding");
584
0
}
585
586
77
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
77
  const endianness E = ELFT::TargetEndianness;
588
72
  for (CieRecord *Rec : CieRecords) {
589
72
    size_t CieOffset = Rec->Cie->OutputOff;
590
72
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
72
592
101
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
101
      size_t Off = Fde->OutputOff;
594
101
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
101
596
101
      // FDE's second word should have the offset to an associated CIE.
597
101
      // Write it.
598
101
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
101
    }
600
72
  }
601
77
602
77
  for (EhInputSection *S : Sections)
603
87
    S->relocateAlloc(Buf, nullptr);
604
77
605
77
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
77
  // to get a FDE from an address to which FDE is applied. So here
607
77
  // we obtain two addresses and pass them to EhFrameHdr object.
608
77
  if (
In<ELFT>::EhFrameHdr77
) {
609
14
    for (CieRecord *Rec : CieRecords) {
610
14
      uint8_t Enc = getFdeEncoding<ELFT>(Rec->Cie);
611
16
      for (EhSectionPiece *Fde : Rec->Fdes) {
612
16
        uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
613
16
        uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
614
16
        In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
615
16
      }
616
14
    }
617
14
  }
618
77
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
586
1
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
1
  const endianness E = ELFT::TargetEndianness;
588
1
  for (CieRecord *Rec : CieRecords) {
589
1
    size_t CieOffset = Rec->Cie->OutputOff;
590
1
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
1
592
1
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
1
      size_t Off = Fde->OutputOff;
594
1
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
1
596
1
      // FDE's second word should have the offset to an associated CIE.
597
1
      // Write it.
598
1
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
1
    }
600
1
  }
601
1
602
1
  for (EhInputSection *S : Sections)
603
1
    S->relocateAlloc(Buf, nullptr);
604
1
605
1
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
1
  // to get a FDE from an address to which FDE is applied. So here
607
1
  // we obtain two addresses and pass them to EhFrameHdr object.
608
1
  if (
In<ELFT>::EhFrameHdr1
) {
609
1
    for (CieRecord *Rec : CieRecords) {
610
1
      uint8_t Enc = getFdeEncoding<ELFT>(Rec->Cie);
611
1
      for (EhSectionPiece *Fde : Rec->Fdes) {
612
1
        uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
613
1
        uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
614
1
        In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
615
1
      }
616
1
    }
617
1
  }
618
1
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
586
70
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
70
  const endianness E = ELFT::TargetEndianness;
588
65
  for (CieRecord *Rec : CieRecords) {
589
65
    size_t CieOffset = Rec->Cie->OutputOff;
590
65
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
65
592
94
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
94
      size_t Off = Fde->OutputOff;
594
94
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
94
596
94
      // FDE's second word should have the offset to an associated CIE.
597
94
      // Write it.
598
94
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
94
    }
600
65
  }
601
70
602
70
  for (EhInputSection *S : Sections)
603
79
    S->relocateAlloc(Buf, nullptr);
604
70
605
70
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
70
  // to get a FDE from an address to which FDE is applied. So here
607
70
  // we obtain two addresses and pass them to EhFrameHdr object.
608
70
  if (
In<ELFT>::EhFrameHdr70
) {
609
10
    for (CieRecord *Rec : CieRecords) {
610
10
      uint8_t Enc = getFdeEncoding<ELFT>(Rec->Cie);
611
13
      for (EhSectionPiece *Fde : Rec->Fdes) {
612
13
        uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
613
13
        uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
614
13
        In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
615
13
      }
616
10
    }
617
10
  }
618
70
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
586
3
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
3
  const endianness E = ELFT::TargetEndianness;
588
3
  for (CieRecord *Rec : CieRecords) {
589
3
    size_t CieOffset = Rec->Cie->OutputOff;
590
3
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
3
592
2
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
2
      size_t Off = Fde->OutputOff;
594
2
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
2
596
2
      // FDE's second word should have the offset to an associated CIE.
597
2
      // Write it.
598
2
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
2
    }
600
3
  }
601
3
602
3
  for (EhInputSection *S : Sections)
603
3
    S->relocateAlloc(Buf, nullptr);
604
3
605
3
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
3
  // to get a FDE from an address to which FDE is applied. So here
607
3
  // we obtain two addresses and pass them to EhFrameHdr object.
608
3
  if (
In<ELFT>::EhFrameHdr3
) {
609
3
    for (CieRecord *Rec : CieRecords) {
610
3
      uint8_t Enc = getFdeEncoding<ELFT>(Rec->Cie);
611
2
      for (EhSectionPiece *Fde : Rec->Fdes) {
612
2
        uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
613
2
        uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
614
2
        In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
615
2
      }
616
3
    }
617
3
  }
618
3
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
586
3
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
3
  const endianness E = ELFT::TargetEndianness;
588
3
  for (CieRecord *Rec : CieRecords) {
589
3
    size_t CieOffset = Rec->Cie->OutputOff;
590
3
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
3
592
4
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
4
      size_t Off = Fde->OutputOff;
594
4
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
4
596
4
      // FDE's second word should have the offset to an associated CIE.
597
4
      // Write it.
598
4
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
4
    }
600
3
  }
601
3
602
3
  for (EhInputSection *S : Sections)
603
4
    S->relocateAlloc(Buf, nullptr);
604
3
605
3
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
3
  // to get a FDE from an address to which FDE is applied. So here
607
3
  // we obtain two addresses and pass them to EhFrameHdr object.
608
3
  if (
In<ELFT>::EhFrameHdr3
) {
609
0
    for (CieRecord *Rec : CieRecords) {
610
0
      uint8_t Enc = getFdeEncoding<ELFT>(Rec->Cie);
611
0
      for (EhSectionPiece *Fde : Rec->Fdes) {
612
0
        uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
613
0
        uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
614
0
        In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
615
0
      }
616
0
    }
617
0
  }
618
3
}
619
620
GotSection::GotSection()
621
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
622
1.45k
                       Target->GotEntrySize, ".got") {}
623
624
109
void GotSection::addEntry(SymbolBody &Sym) {
625
109
  Sym.GotIndex = NumEntries;
626
109
  ++NumEntries;
627
109
}
628
629
36
bool GotSection::addDynTlsEntry(SymbolBody &Sym) {
630
36
  if (Sym.GlobalDynIndex != -1U)
631
13
    return false;
632
23
  Sym.GlobalDynIndex = NumEntries;
633
23
  // Global Dynamic TLS entries take two GOT slots.
634
23
  NumEntries += 2;
635
23
  return true;
636
23
}
637
638
// Reserves TLS entries for a TLS module ID and a TLS block offset.
639
// In total it takes two GOT slots.
640
6
bool GotSection::addTlsIndex() {
641
6
  if (TlsIndexOff != uint32_t(-1))
642
2
    return false;
643
4
  TlsIndexOff = NumEntries * Config->Wordsize;
644
4
  NumEntries += 2;
645
4
  return true;
646
4
}
647
648
30
uint64_t GotSection::getGlobalDynAddr(const SymbolBody &B) const {
649
30
  return this->getVA() + B.GlobalDynIndex * Config->Wordsize;
650
30
}
651
652
25
uint64_t GotSection::getGlobalDynOffset(const SymbolBody &B) const {
653
25
  return B.GlobalDynIndex * Config->Wordsize;
654
25
}
655
656
93
void GotSection::finalizeContents() { Size = NumEntries * Config->Wordsize; }
657
658
2.89k
bool GotSection::empty() const {
659
2.89k
  // If we have a relocation that is relative to GOT (such as GOTOFFREL),
660
2.89k
  // we need to emit a GOT even if it's empty.
661
2.62k
  return NumEntries == 0 && !HasGotOffRel;
662
2.89k
}
663
664
96
void GotSection::writeTo(uint8_t *Buf) {
665
96
  // Buf points to the start of this section's buffer,
666
96
  // whereas InputSectionBase::relocateAlloc() expects its argument
667
96
  // to point to the start of the output section.
668
96
  relocateAlloc(Buf - OutSecOff, Buf - OutSecOff + Size);
669
96
}
670
671
MipsGotSection::MipsGotSection()
672
    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
673
137
                       ".got") {}
674
675
95
void MipsGotSection::addEntry(SymbolBody &Sym, int64_t Addend, RelExpr Expr) {
676
95
  // For "true" local symbols which can be referenced from the same module
677
95
  // only compiler creates two instructions for address loading:
678
95
  //
679
95
  // lw   $8, 0($gp) # R_MIPS_GOT16
680
95
  // addi $8, $8, 0  # R_MIPS_LO16
681
95
  //
682
95
  // The first instruction loads high 16 bits of the symbol address while
683
95
  // the second adds an offset. That allows to reduce number of required
684
95
  // GOT entries because only one global offset table entry is necessary
685
95
  // for every 64 KBytes of local data. So for local symbols we need to
686
95
  // allocate number of GOT entries to hold all required "page" addresses.
687
95
  //
688
95
  // All global symbols (hidden and regular) considered by compiler uniformly.
689
95
  // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
690
95
  // to load address of the symbol. So for each such symbol we need to
691
95
  // allocate dedicated GOT entry to store its address.
692
95
  //
693
95
  // If a symbol is preemptible we need help of dynamic linker to get its
694
95
  // final address. The corresponding GOT entries are allocated in the
695
95
  // "global" part of GOT. Entries for non preemptible global symbol allocated
696
95
  // in the "local" part of GOT.
697
95
  //
698
95
  // See "Global Offset Table" in Chapter 5:
699
95
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
700
95
  if (
Expr == R_MIPS_GOT_LOCAL_PAGE95
) {
701
18
    // At this point we do not know final symbol value so to reduce number
702
18
    // of allocated GOT entries do the following trick. Save all output
703
18
    // sections referenced by GOT relocations. Then later in the `finalize`
704
18
    // method calculate number of "pages" required to cover all saved output
705
18
    // section and allocate appropriate number of GOT entries.
706
18
    PageIndexMap.insert({Sym.getOutputSection(), 0});
707
18
    return;
708
18
  }
709
77
  
if (77
Sym.isTls()77
) {
710
9
    // GOT entries created for MIPS TLS relocations behave like
711
9
    // almost GOT entries from other ABIs. They go to the end
712
9
    // of the global offset table.
713
9
    Sym.GotIndex = TlsEntries.size();
714
9
    TlsEntries.push_back(&Sym);
715
9
    return;
716
9
  }
717
68
  
auto AddEntry = [&](SymbolBody &S, uint64_t A, GotEntries &Items) 68
{
718
68
    if (
S.isInGot() && 68
!A11
)
719
11
      return;
720
57
    size_t NewIndex = Items.size();
721
57
    if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
722
0
      return;
723
57
    Items.emplace_back(&S, A);
724
57
    if (!A)
725
54
      S.GotIndex = NewIndex;
726
68
  };
727
68
  if (
Sym.isPreemptible()68
) {
728
35
    // Ignore addends for preemptible symbols. They got single GOT entry anyway.
729
35
    AddEntry(Sym, 0, GlobalEntries);
730
35
    Sym.IsInGlobalMipsGot = true;
731
68
  } else 
if (33
Expr == R_MIPS_GOT_OFF3233
) {
732
12
    AddEntry(Sym, Addend, LocalEntries32);
733
12
    Sym.Is32BitMipsGot = true;
734
33
  } else {
735
21
    // Hold local GOT entries accessed via a 16-bit index separately.
736
21
    // That allows to write them in the beginning of the GOT and keep
737
21
    // their indexes as less as possible to escape relocation's overflow.
738
21
    AddEntry(Sym, Addend, LocalEntries);
739
21
  }
740
95
}
741
742
9
bool MipsGotSection::addDynTlsEntry(SymbolBody &Sym) {
743
9
  if (Sym.GlobalDynIndex != -1U)
744
0
    return false;
745
9
  Sym.GlobalDynIndex = TlsEntries.size();
746
9
  // Global Dynamic TLS entries take two GOT slots.
747
9
  TlsEntries.push_back(nullptr);
748
9
  TlsEntries.push_back(&Sym);
749
9
  return true;
750
9
}
751
752
// Reserves TLS entries for a TLS module ID and a TLS block offset.
753
// In total it takes two GOT slots.
754
5
bool MipsGotSection::addTlsIndex() {
755
5
  if (TlsIndexOff != uint32_t(-1))
756
0
    return false;
757
5
  TlsIndexOff = TlsEntries.size() * Config->Wordsize;
758
5
  TlsEntries.push_back(nullptr);
759
5
  TlsEntries.push_back(nullptr);
760
5
  return true;
761
5
}
762
763
47
static uint64_t getMipsPageAddr(uint64_t Addr) {
764
47
  return (Addr + 0x8000) & ~0xffff;
765
47
}
766
767
22
static uint64_t getMipsPageCount(uint64_t Size) {
768
22
  return (Size + 0xfffe) / 0xffff + 1;
769
22
}
770
771
uint64_t MipsGotSection::getPageEntryOffset(const SymbolBody &B,
772
18
                                            int64_t Addend) const {
773
18
  const OutputSection *OutSec = B.getOutputSection();
774
18
  uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
775
18
  uint64_t SymAddr = getMipsPageAddr(B.getVA(Addend));
776
18
  uint64_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
777
18
  assert(Index < PageEntriesNum);
778
18
  return (HeaderEntriesNum + Index) * Config->Wordsize;
779
18
}
780
781
uint64_t MipsGotSection::getBodyEntryOffset(const SymbolBody &B,
782
74
                                            int64_t Addend) const {
783
74
  // Calculate offset of the GOT entries block: TLS, global, local.
784
74
  uint64_t Index = HeaderEntriesNum + PageEntriesNum;
785
74
  if (B.isTls())
786
9
    Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
787
65
  else 
if (65
B.IsInGlobalMipsGot65
)
788
32
    Index += LocalEntries.size() + LocalEntries32.size();
789
33
  else 
if (33
B.Is32BitMipsGot33
)
790
12
    Index += LocalEntries.size();
791
74
  // Calculate offset of the GOT entry in the block.
792
74
  if (B.isInGot())
793
71
    Index += B.GotIndex;
794
3
  else {
795
3
    auto It = EntryIndexMap.find({&B, Addend});
796
3
    assert(It != EntryIndexMap.end());
797
3
    Index += It->second;
798
3
  }
799
74
  return Index * Config->Wordsize;
800
74
}
801
802
34
uint64_t MipsGotSection::getTlsOffset() const {
803
34
  return (getLocalEntriesNum() + GlobalEntries.size()) * Config->Wordsize;
804
34
}
805
806
15
uint64_t MipsGotSection::getGlobalDynOffset(const SymbolBody &B) const {
807
15
  return B.GlobalDynIndex * Config->Wordsize;
808
15
}
809
810
77
const SymbolBody *MipsGotSection::getFirstGlobalEntry() const {
811
77
  return GlobalEntries.empty() ? 
nullptr48
:
GlobalEntries.front().first29
;
812
77
}
813
814
245
unsigned MipsGotSection::getLocalEntriesNum() const {
815
245
  return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
816
245
         LocalEntries32.size();
817
245
}
818
819
127
void MipsGotSection::finalizeContents() { updateAllocSize(); }
820
821
134
void MipsGotSection::updateAllocSize() {
822
134
  PageEntriesNum = 0;
823
11
  for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) {
824
11
    // For each output section referenced by GOT page relocations calculate
825
11
    // and save into PageIndexMap an upper bound of MIPS GOT entries required
826
11
    // to store page addresses of local symbols. We assume the worst case -
827
11
    // each 64kb page of the output section has at least one GOT relocation
828
11
    // against it. And take in account the case when the section intersects
829
11
    // page boundaries.
830
11
    P.second = PageEntriesNum;
831
11
    PageEntriesNum += getMipsPageCount(P.first->Size);
832
11
  }
833
134
  Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
834
134
         Config->Wordsize;
835
134
}
836
837
531
bool MipsGotSection::empty() const {
838
531
  // We add the .got section to the result for dynamic MIPS target because
839
531
  // its address and properties are mentioned in the .dynamic section.
840
531
  return Config->Relocatable;
841
531
}
842
843
272
uint64_t MipsGotSection::getGp() const { return ElfSym::MipsGp->getVA(0); }
844
845
7
static uint64_t readUint(uint8_t *Buf) {
846
7
  if (Config->Is64)
847
5
    return read64(Buf, Config->Endianness);
848
2
  return read32(Buf, Config->Endianness);
849
2
}
850
851
237
static void writeUint(uint8_t *Buf, uint64_t Val) {
852
237
  if (Config->Is64)
853
60
    write64(Buf, Val, Config->Endianness);
854
237
  else
855
177
    write32(Buf, Val, Config->Endianness);
856
237
}
857
858
127
void MipsGotSection::writeTo(uint8_t *Buf) {
859
127
  // Set the MSB of the second GOT slot. This is not required by any
860
127
  // MIPS ABI documentation, though.
861
127
  //
862
127
  // There is a comment in glibc saying that "The MSB of got[1] of a
863
127
  // gnu object is set to identify gnu objects," and in GNU gold it
864
127
  // says "the second entry will be used by some runtime loaders".
865
127
  // But how this field is being used is unclear.
866
127
  //
867
127
  // We are not really willing to mimic other linkers behaviors
868
127
  // without understanding why they do that, but because all files
869
127
  // generated by GNU tools have this special GOT value, and because
870
127
  // we've been doing this for years, it is probably a safe bet to
871
127
  // keep doing this for now. We really need to revisit this to see
872
127
  // if we had to do this.
873
127
  writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
874
127
  Buf += HeaderEntriesNum * Config->Wordsize;
875
127
  // Write 'page address' entries to the local part of the GOT.
876
11
  for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) {
877
11
    size_t PageCount = getMipsPageCount(L.first->Size);
878
11
    uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
879
36
    for (size_t PI = 0; 
PI < PageCount36
;
++PI25
) {
880
25
      uint8_t *Entry = Buf + (L.second + PI) * Config->Wordsize;
881
25
      writeUint(Entry, FirstPageAddr + PI * 0x10000);
882
25
    }
883
11
  }
884
127
  Buf += PageEntriesNum * Config->Wordsize;
885
57
  auto AddEntry = [&](const GotEntry &SA) {
886
57
    uint8_t *Entry = Buf;
887
57
    Buf += Config->Wordsize;
888
57
    const SymbolBody *Body = SA.first;
889
57
    uint64_t VA = Body->getVA(SA.second);
890
57
    writeUint(Entry, VA);
891
57
  };
892
127
  std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
893
127
  std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
894
127
  std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
895
127
  // Initialize TLS-related GOT entries. If the entry has a corresponding
896
127
  // dynamic relocations, leave it initialized by zero. Write down adjusted
897
127
  // TLS symbol's values otherwise. To calculate the adjustments use offsets
898
127
  // for thread-local storage.
899
127
  // https://www.linux-mips.org/wiki/NPTL
900
127
  if (
TlsIndexOff != -1U && 127
!Config->Pic5
)
901
3
    writeUint(Buf + TlsIndexOff, 1);
902
37
  for (const SymbolBody *B : TlsEntries) {
903
37
    if (
!B || 37
B->isPreemptible()18
)
904
31
      continue;
905
6
    uint64_t VA = B->getVA();
906
6
    if (
B->GotIndex != -1U6
) {
907
6
      uint8_t *Entry = Buf + B->GotIndex * Config->Wordsize;
908
6
      writeUint(Entry, VA - 0x7000);
909
6
    }
910
6
    if (
B->GlobalDynIndex != -1U6
) {
911
6
      uint8_t *Entry = Buf + B->GlobalDynIndex * Config->Wordsize;
912
6
      writeUint(Entry, 1);
913
6
      Entry += Config->Wordsize;
914
6
      writeUint(Entry, VA - 0x8000);
915
6
    }
916
37
  }
917
127
}
918
919
GotPltSection::GotPltSection()
920
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
921
1.58k
                       Target->GotPltEntrySize, ".got.plt") {}
922
923
187
void GotPltSection::addEntry(SymbolBody &Sym) {
924
187
  Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
925
187
  Entries.push_back(&Sym);
926
187
}
927
928
1.89k
size_t GotPltSection::getSize() const {
929
1.89k
  return (Target->GotPltHeaderEntriesNum + Entries.size()) *
930
1.89k
         Target->GotPltEntrySize;
931
1.89k
}
932
933
132
void GotPltSection::writeTo(uint8_t *Buf) {
934
132
  Target->writeGotPltHeader(Buf);
935
132
  Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
936
187
  for (const SymbolBody *B : Entries) {
937
187
    Target->writeGotPlt(Buf, *B);
938
187
    Buf += Config->Wordsize;
939
187
  }
940
132
}
941
942
// On ARM the IgotPltSection is part of the GotSection, on other Targets it is
943
// part of the .got.plt
944
IgotPltSection::IgotPltSection()
945
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
946
                       Target->GotPltEntrySize,
947
1.58k
                       Config->EMachine == EM_ARM ? ".got" : ".got.plt") {}
948
949
27
void IgotPltSection::addEntry(SymbolBody &Sym) {
950
27
  Sym.IsInIgot = true;
951
27
  Sym.GotPltIndex = Entries.size();
952
27
  Entries.push_back(&Sym);
953
27
}
954
955
1.62k
size_t IgotPltSection::getSize() const {
956
1.62k
  return Entries.size() * Target->GotPltEntrySize;
957
1.62k
}
958
959
15
void IgotPltSection::writeTo(uint8_t *Buf) {
960
27
  for (const SymbolBody *B : Entries) {
961
27
    Target->writeIgotPlt(Buf, *B);
962
27
    Buf += Config->Wordsize;
963
27
  }
964
15
}
965
966
StringTableSection::StringTableSection(StringRef Name, bool Dynamic)
967
    : SyntheticSection(Dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name),
968
4.76k
      Dynamic(Dynamic) {
969
4.76k
  // ELF string tables start with a NUL byte.
970
4.76k
  addString("");
971
4.76k
}
972
973
// Adds a string to the string table. If HashIt is true we hash and check for
974
// duplicates. It is optional because the name of global symbols are already
975
// uniqued and hashing them again has a big cost for a small value: uniquing
976
// them with some other string that happens to be the same.
977
220k
unsigned StringTableSection::addString(StringRef S, bool HashIt) {
978
220k
  if (
HashIt220k
) {
979
215k
    auto R = StringMap.insert(std::make_pair(S, this->Size));
980
215k
    if (!R.second)
981
288
      return R.first->second;
982
220k
  }
983
220k
  unsigned Ret = this->Size;
984
220k
  this->Size = this->Size + S.size() + 1;
985
220k
  Strings.push_back(S);
986
220k
  return Ret;
987
220k
}
988
989
3.76k
void StringTableSection::writeTo(uint8_t *Buf) {
990
219k
  for (StringRef S : Strings) {
991
219k
    memcpy(Buf, S.data(), S.size());
992
219k
    Buf[S.size()] = '\0';
993
219k
    Buf += S.size() + 1;
994
219k
  }
995
3.76k
}
996
997
// Returns the number of version definition entries. Because the first entry
998
// is for the version definition itself, it is the number of versioned symbols
999
// plus one. Note that we don't support multiple versions yet.
1000
982
static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
1001
1002
template <class ELFT>
1003
DynamicSection<ELFT>::DynamicSection()
1004
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, Config->Wordsize,
1005
1.58k
                       ".dynamic") {
1006
1.58k
  this->Entsize = ELFT::Is64Bits ? 
161.30k
:
8286
;
1007
1.58k
1008
1.58k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1009
1.58k
  // which passes -z rodynamic.
1010
1.58k
  // See "Special Section" in Chapter 4 in the following document:
1011
1.58k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1012
1.58k
  if (
Config->EMachine == EM_MIPS || 1.58k
Config->ZRodynamic1.45k
)
1013
139
    this->Flags = SHF_ALLOC;
1014
1.58k
1015
1.58k
  addEntries();
1016
1.58k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::DynamicSection()
Line
Count
Source
1005
44
                       ".dynamic") {
1006
44
  this->Entsize = ELFT::Is64Bits ? 
1644
:
80
;
1007
44
1008
44
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1009
44
  // which passes -z rodynamic.
1010
44
  // See "Special Section" in Chapter 4 in the following document:
1011
44
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1012
44
  if (
Config->EMachine == EM_MIPS || 44
Config->ZRodynamic16
)
1013
28
    this->Flags = SHF_ALLOC;
1014
44
1015
44
  addEntries();
1016
44
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::DynamicSection()
Line
Count
Source
1005
191
                       ".dynamic") {
1006
191
  this->Entsize = ELFT::Is64Bits ? 
160
:
8191
;
1007
191
1008
191
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1009
191
  // which passes -z rodynamic.
1010
191
  // See "Special Section" in Chapter 4 in the following document:
1011
191
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1012
191
  if (
Config->EMachine == EM_MIPS || 191
Config->ZRodynamic177
)
1013
14
    this->Flags = SHF_ALLOC;
1014
191
1015
191
  addEntries();
1016
191
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::DynamicSection()
Line
Count
Source
1005
95
                       ".dynamic") {
1006
95
  this->Entsize = ELFT::Is64Bits ? 
160
:
895
;
1007
95
1008
95
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1009
95
  // which passes -z rodynamic.
1010
95
  // See "Special Section" in Chapter 4 in the following document:
1011
95
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1012
95
  if (
Config->EMachine == EM_MIPS || 95
Config->ZRodynamic2
)
1013
93
    this->Flags = SHF_ALLOC;
1014
95
1015
95
  addEntries();
1016
95
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::DynamicSection()
Line
Count
Source
1005
1.25k
                       ".dynamic") {
1006
1.25k
  this->Entsize = ELFT::Is64Bits ? 
161.25k
:
80
;
1007
1.25k
1008
1.25k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1009
1.25k
  // which passes -z rodynamic.
1010
1.25k
  // See "Special Section" in Chapter 4 in the following document:
1011
1.25k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1012
1.25k
  if (
Config->EMachine == EM_MIPS || 1.25k
Config->ZRodynamic1.25k
)
1013
4
    this->Flags = SHF_ALLOC;
1014
1.25k
1015
1.25k
  addEntries();
1016
1.25k
}
1017
1018
// There are some dynamic entries that don't depend on other sections.
1019
// Such entries can be set early.
1020
1.58k
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1021
1.58k
  // Add strings to .dynstr early so that .dynstr's size will be
1022
1.58k
  // fixed early.
1023
1.58k
  for (StringRef S : Config->FilterList)
1024
6
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1025
1.58k
  for (StringRef S : Config->AuxiliaryList)
1026
2
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1027
1.58k
  if (!Config->Rpath.empty())
1028
6
    
add({Config->EnableNewDtags ? 6
DT_RUNPATH5
:
DT_RPATH1
,
1029
6
         InX::DynStrTab->addString(Config->Rpath)});
1030
213
  for (InputFile *File : SharedFiles) {
1031
213
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1032
213
    if (F->isNeeded())
1033
208
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1034
213
  }
1035
1.58k
  if (!Config->SoName.empty())
1036
16
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1037
1.58k
1038
1.58k
  // Set DT_FLAGS and DT_FLAGS_1.
1039
1.58k
  uint32_t DtFlags = 0;
1040
1.58k
  uint32_t DtFlags1 = 0;
1041
1.58k
  if (Config->Bsymbolic)
1042
4
    DtFlags |= DF_SYMBOLIC;
1043
1.58k
  if (Config->ZNodelete)
1044
1
    DtFlags1 |= DF_1_NODELETE;
1045
1.58k
  if (Config->ZNodlopen)
1046
1
    DtFlags1 |= DF_1_NOOPEN;
1047
1.58k
  if (
Config->ZNow1.58k
) {
1048
2
    DtFlags |= DF_BIND_NOW;
1049
2
    DtFlags1 |= DF_1_NOW;
1050
2
  }
1051
1.58k
  if (
Config->ZOrigin1.58k
) {
1052
1
    DtFlags |= DF_ORIGIN;
1053
1
    DtFlags1 |= DF_1_ORIGIN;
1054
1
  }
1055
1.58k
1056
1.58k
  if (DtFlags)
1057
5
    add({DT_FLAGS, DtFlags});
1058
1.58k
  if (DtFlags1)
1059
2
    add({DT_FLAGS_1, DtFlags1});
1060
1.58k
1061
1.58k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1062
1.58k
  // need it for each process, so we don't write it for DSOs. The loader writes
1063
1.58k
  // the pointer into this entry.
1064
1.58k
  //
1065
1.58k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1066
1.58k
  // systems (currently only Fuchsia OS) provide other means to give the
1067
1.58k
  // debugger this information. Such systems may choose make .dynamic read-only.
1068
1.58k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1069
1.58k
  if (
!Config->Shared && 1.58k
!Config->Relocatable996
&&
!Config->ZRodynamic936
)
1070
935
    add({DT_DEBUG, (uint64_t)0});
1071
1.58k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addEntries()
Line
Count
Source
1020
191
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1021
191
  // Add strings to .dynstr early so that .dynstr's size will be
1022
191
  // fixed early.
1023
191
  for (StringRef S : Config->FilterList)
1024
0
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1025
191
  for (StringRef S : Config->AuxiliaryList)
1026
0
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1027
191
  if (!Config->Rpath.empty())
1028
2
    
add({Config->EnableNewDtags ? 2
DT_RUNPATH2
:
DT_RPATH0
,
1029
2
         InX::DynStrTab->addString(Config->Rpath)});
1030
38
  for (InputFile *File : SharedFiles) {
1031
38
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1032
38
    if (F->isNeeded())
1033
34
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1034
38
  }
1035
191
  if (!Config->SoName.empty())
1036
3
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1037
191
1038
191
  // Set DT_FLAGS and DT_FLAGS_1.
1039
191
  uint32_t DtFlags = 0;
1040
191
  uint32_t DtFlags1 = 0;
1041
191
  if (Config->Bsymbolic)
1042
0
    DtFlags |= DF_SYMBOLIC;
1043
191
  if (Config->ZNodelete)
1044
0
    DtFlags1 |= DF_1_NODELETE;
1045
191
  if (Config->ZNodlopen)
1046
0
    DtFlags1 |= DF_1_NOOPEN;
1047
191
  if (
Config->ZNow191
) {
1048
0
    DtFlags |= DF_BIND_NOW;
1049
0
    DtFlags1 |= DF_1_NOW;
1050
0
  }
1051
191
  if (
Config->ZOrigin191
) {
1052
0
    DtFlags |= DF_ORIGIN;
1053
0
    DtFlags1 |= DF_1_ORIGIN;
1054
0
  }
1055
191
1056
191
  if (DtFlags)
1057
0
    add({DT_FLAGS, DtFlags});
1058
191
  if (DtFlags1)
1059
0
    add({DT_FLAGS_1, DtFlags1});
1060
191
1061
191
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1062
191
  // need it for each process, so we don't write it for DSOs. The loader writes
1063
191
  // the pointer into this entry.
1064
191
  //
1065
191
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1066
191
  // systems (currently only Fuchsia OS) provide other means to give the
1067
191
  // debugger this information. Such systems may choose make .dynamic read-only.
1068
191
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1069
191
  if (
!Config->Shared && 191
!Config->Relocatable116
&&
!Config->ZRodynamic110
)
1070
110
    add({DT_DEBUG, (uint64_t)0});
1071
191
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addEntries()
Line
Count
Source
1020
1.25k
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1021
1.25k
  // Add strings to .dynstr early so that .dynstr's size will be
1022
1.25k
  // fixed early.
1023
1.25k
  for (StringRef S : Config->FilterList)
1024
6
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1025
1.25k
  for (StringRef S : Config->AuxiliaryList)
1026
2
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1027
1.25k
  if (!Config->Rpath.empty())
1028
3
    
add({Config->EnableNewDtags ? 3
DT_RUNPATH2
:
DT_RPATH1
,
1029
3
         InX::DynStrTab->addString(Config->Rpath)});
1030
151
  for (InputFile *File : SharedFiles) {
1031
151
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1032
151
    if (F->isNeeded())
1033
150
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1034
151
  }
1035
1.25k
  if (!Config->SoName.empty())
1036
13
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1037
1.25k
1038
1.25k
  // Set DT_FLAGS and DT_FLAGS_1.
1039
1.25k
  uint32_t DtFlags = 0;
1040
1.25k
  uint32_t DtFlags1 = 0;
1041
1.25k
  if (Config->Bsymbolic)
1042
3
    DtFlags |= DF_SYMBOLIC;
1043
1.25k
  if (Config->ZNodelete)
1044
1
    DtFlags1 |= DF_1_NODELETE;
1045
1.25k
  if (Config->ZNodlopen)
1046
1
    DtFlags1 |= DF_1_NOOPEN;
1047
1.25k
  if (
Config->ZNow1.25k
) {
1048
2
    DtFlags |= DF_BIND_NOW;
1049
2
    DtFlags1 |= DF_1_NOW;
1050
2
  }
1051
1.25k
  if (
Config->ZOrigin1.25k
) {
1052
1
    DtFlags |= DF_ORIGIN;
1053
1
    DtFlags1 |= DF_1_ORIGIN;
1054
1
  }
1055
1.25k
1056
1.25k
  if (DtFlags)
1057
4
    add({DT_FLAGS, DtFlags});
1058
1.25k
  if (DtFlags1)
1059
2
    add({DT_FLAGS_1, DtFlags1});
1060
1.25k
1061
1.25k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1062
1.25k
  // need it for each process, so we don't write it for DSOs. The loader writes
1063
1.25k
  // the pointer into this entry.
1064
1.25k
  //
1065
1.25k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1066
1.25k
  // systems (currently only Fuchsia OS) provide other means to give the
1067
1.25k
  // debugger this information. Such systems may choose make .dynamic read-only.
1068
1.25k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1069
1.25k
  if (
!Config->Shared && 1.25k
!Config->Relocatable800
&&
!Config->ZRodynamic755
)
1070
754
    add({DT_DEBUG, (uint64_t)0});
1071
1.25k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addEntries()
Line
Count
Source
1020
95
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1021
95
  // Add strings to .dynstr early so that .dynstr's size will be
1022
95
  // fixed early.
1023
95
  for (StringRef S : Config->FilterList)
1024
0
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1025
95
  for (StringRef S : Config->AuxiliaryList)
1026
0
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1027
95
  if (!Config->Rpath.empty())
1028
0
    
add({Config->EnableNewDtags ? 0
DT_RUNPATH0
:
DT_RPATH0
,
1029
0
         InX::DynStrTab->addString(Config->Rpath)});
1030
17
  for (InputFile *File : SharedFiles) {
1031
17
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1032
17
    if (F->isNeeded())
1033
17
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1034
17
  }
1035
95
  if (!Config->SoName.empty())
1036
0
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1037
95
1038
95
  // Set DT_FLAGS and DT_FLAGS_1.
1039
95
  uint32_t DtFlags = 0;
1040
95
  uint32_t DtFlags1 = 0;
1041
95
  if (Config->Bsymbolic)
1042
1
    DtFlags |= DF_SYMBOLIC;
1043
95
  if (Config->ZNodelete)
1044
0
    DtFlags1 |= DF_1_NODELETE;
1045
95
  if (Config->ZNodlopen)
1046
0
    DtFlags1 |= DF_1_NOOPEN;
1047
95
  if (
Config->ZNow95
) {
1048
0
    DtFlags |= DF_BIND_NOW;
1049
0
    DtFlags1 |= DF_1_NOW;
1050
0
  }
1051
95
  if (
Config->ZOrigin95
) {
1052
0
    DtFlags |= DF_ORIGIN;
1053
0
    DtFlags1 |= DF_1_ORIGIN;
1054
0
  }
1055
95
1056
95
  if (DtFlags)
1057
1
    add({DT_FLAGS, DtFlags});
1058
95
  if (DtFlags1)
1059
0
    add({DT_FLAGS_1, DtFlags1});
1060
95
1061
95
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1062
95
  // need it for each process, so we don't write it for DSOs. The loader writes
1063
95
  // the pointer into this entry.
1064
95
  //
1065
95
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1066
95
  // systems (currently only Fuchsia OS) provide other means to give the
1067
95
  // debugger this information. Such systems may choose make .dynamic read-only.
1068
95
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1069
95
  if (
!Config->Shared && 95
!Config->Relocatable54
&&
!Config->ZRodynamic46
)
1070
46
    add({DT_DEBUG, (uint64_t)0});
1071
95
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addEntries()
Line
Count
Source
1020
44
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1021
44
  // Add strings to .dynstr early so that .dynstr's size will be
1022
44
  // fixed early.
1023
44
  for (StringRef S : Config->FilterList)
1024
0
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1025
44
  for (StringRef S : Config->AuxiliaryList)
1026
0
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1027
44
  if (!Config->Rpath.empty())
1028
1
    
add({Config->EnableNewDtags ? 1
DT_RUNPATH1
:
DT_RPATH0
,
1029
1
         InX::DynStrTab->addString(Config->Rpath)});
1030
7
  for (InputFile *File : SharedFiles) {
1031
7
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1032
7
    if (F->isNeeded())
1033
7
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1034
7
  }
1035
44
  if (!Config->SoName.empty())
1036
0
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1037
44
1038
44
  // Set DT_FLAGS and DT_FLAGS_1.
1039
44
  uint32_t DtFlags = 0;
1040
44
  uint32_t DtFlags1 = 0;
1041
44
  if (Config->Bsymbolic)
1042
0
    DtFlags |= DF_SYMBOLIC;
1043
44
  if (Config->ZNodelete)
1044
0
    DtFlags1 |= DF_1_NODELETE;
1045
44
  if (Config->ZNodlopen)
1046
0
    DtFlags1 |= DF_1_NOOPEN;
1047
44
  if (
Config->ZNow44
) {
1048
0
    DtFlags |= DF_BIND_NOW;
1049
0
    DtFlags1 |= DF_1_NOW;
1050
0
  }
1051
44
  if (
Config->ZOrigin44
) {
1052
0
    DtFlags |= DF_ORIGIN;
1053
0
    DtFlags1 |= DF_1_ORIGIN;
1054
0
  }
1055
44
1056
44
  if (DtFlags)
1057
0
    add({DT_FLAGS, DtFlags});
1058
44
  if (DtFlags1)
1059
0
    add({DT_FLAGS_1, DtFlags1});
1060
44
1061
44
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1062
44
  // need it for each process, so we don't write it for DSOs. The loader writes
1063
44
  // the pointer into this entry.
1064
44
  //
1065
44
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1066
44
  // systems (currently only Fuchsia OS) provide other means to give the
1067
44
  // debugger this information. Such systems may choose make .dynamic read-only.
1068
44
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1069
44
  if (
!Config->Shared && 44
!Config->Relocatable26
&&
!Config->ZRodynamic25
)
1070
25
    add({DT_DEBUG, (uint64_t)0});
1071
44
}
1072
1073
// Add remaining entries to complete .dynamic contents.
1074
762
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1075
762
  if (this->Size)
1076
0
    return; // Already finalized.
1077
762
1078
762
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1079
762
  if (
In<ELFT>::RelaDyn->getParent() && 762
!In<ELFT>::RelaDyn->empty()762
) {
1080
148
    bool IsRela = Config->IsRela;
1081
148
    add({IsRela ? 
DT_RELA105
:
DT_REL43
, In<ELFT>::RelaDyn});
1082
148
    add({IsRela ? 
DT_RELASZ105
:
DT_RELSZ43
, In<ELFT>::RelaDyn->getParent(),
1083
148
         Entry::SecSize});
1084
148
    add({IsRela ? 
DT_RELAENT105
:
DT_RELENT43
,
1085
148
         uint64_t(IsRela ? 
sizeof(Elf_Rela)105
:
sizeof(Elf_Rel)43
)});
1086
148
1087
148
    // MIPS dynamic loader does not support RELCOUNT tag.
1088
148
    // The problem is in the tight relation between dynamic
1089
148
    // relocations and GOT. So do not emit this tag on MIPS.
1090
148
    if (
Config->EMachine != EM_MIPS148
) {
1091
136
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1092
136
      if (
Config->ZCombreloc && 136
NumRelativeRels135
)
1093
29
        
add({IsRela ? 29
DT_RELACOUNT20
:
DT_RELCOUNT9
, NumRelativeRels});
1094
136
    }
1095
148
  }
1096
762
  if (
In<ELFT>::RelaPlt->getParent() && 762
!In<ELFT>::RelaPlt->empty()762
) {
1097
132
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1098
132
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1099
132
    switch (Config->EMachine) {
1100
8
    case EM_MIPS:
1101
8
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1102
8
      break;
1103
0
    case EM_SPARCV9:
1104
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1105
0
      break;
1106
124
    default:
1107
124
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1108
124
      break;
1109
132
    }
1110
132
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 132
DT_RELA103
:
DT_REL29
)});
1111
132
  }
1112
762
1113
762
  add({DT_SYMTAB, InX::DynSymTab});
1114
762
  add({DT_SYMENT, sizeof(Elf_Sym)});
1115
762
  add({DT_STRTAB, InX::DynStrTab});
1116
762
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1117
762
  if (!Config->ZText)
1118
5
    add({DT_TEXTREL, (uint64_t)0});
1119
762
  if (InX::GnuHashTab)
1120
5
    add({DT_GNU_HASH, InX::GnuHashTab});
1121
762
  if (In<ELFT>::HashTab)
1122
758
    add({DT_HASH, In<ELFT>::HashTab});
1123
762
1124
762
  if (
Out::PreinitArray762
) {
1125
3
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1126
3
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1127
3
  }
1128
762
  if (
Out::InitArray762
) {
1129
3
    add({DT_INIT_ARRAY, Out::InitArray});
1130
3
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1131
3
  }
1132
762
  if (
Out::FiniArray762
) {
1133
2
    add({DT_FINI_ARRAY, Out::FiniArray});
1134
2
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1135
2
  }
1136
762
1137
762
  if (SymbolBody *B = Symtab->find(Config->Init))
1138
6
    
if (6
B->isInCurrentDSO()6
)
1139
4
      add({DT_INIT, B});
1140
762
  if (SymbolBody *B = Symtab->find(Config->Fini))
1141
6
    
if (6
B->isInCurrentDSO()6
)
1142
4
      add({DT_FINI, B});
1143
762
1144
762
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1145
762
  if (
HasVerNeed || 762
In<ELFT>::VerDef756
)
1146
41
    add({DT_VERSYM, In<ELFT>::VerSym});
1147
762
  if (
In<ELFT>::VerDef762
) {
1148
36
    add({DT_VERDEF, In<ELFT>::VerDef});
1149
36
    add({DT_VERDEFNUM, getVerDefNum()});
1150
36
  }
1151
762
  if (
HasVerNeed762
) {
1152
6
    add({DT_VERNEED, In<ELFT>::VerNeed});
1153
6
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1154
6
  }
1155
762
1156
762
  if (
Config->EMachine == EM_MIPS762
) {
1157
77
    add({DT_MIPS_RLD_VERSION, 1});
1158
77
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1159
77
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1160
77
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1161
77
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1162
77
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1163
29
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1164
77
    else
1165
48
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1166
77
    add({DT_PLTGOT, InX::MipsGot});
1167
77
    if (InX::MipsRldMap)
1168
20
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1169
77
  }
1170
762
1171
762
  getParent()->Link = this->Link;
1172
762
1173
762
  // +1 for DT_NULL
1174
762
  this->Size = (Entries.size() + 1) * this->Entsize;
1175
762
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1074
54
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1075
54
  if (this->Size)
1076
0
    return; // Already finalized.
1077
54
1078
54
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1079
54
  if (
In<ELFT>::RelaDyn->getParent() && 54
!In<ELFT>::RelaDyn->empty()54
) {
1080
6
    bool IsRela = Config->IsRela;
1081
6
    add({IsRela ? 
DT_RELA0
:
DT_REL6
, In<ELFT>::RelaDyn});
1082
6
    add({IsRela ? 
DT_RELASZ0
:
DT_RELSZ6
, In<ELFT>::RelaDyn->getParent(),
1083
6
         Entry::SecSize});
1084
6
    add({IsRela ? 
DT_RELAENT0
:
DT_RELENT6
,
1085
6
         uint64_t(IsRela ? 
sizeof(Elf_Rela)0
:
sizeof(Elf_Rel)6
)});
1086
6
1087
6
    // MIPS dynamic loader does not support RELCOUNT tag.
1088
6
    // The problem is in the tight relation between dynamic
1089
6
    // relocations and GOT. So do not emit this tag on MIPS.
1090
6
    if (
Config->EMachine != EM_MIPS6
) {
1091
0
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1092
0
      if (
Config->ZCombreloc && 0
NumRelativeRels0
)
1093
0
        
add({IsRela ? 0
DT_RELACOUNT0
:
DT_RELCOUNT0
, NumRelativeRels});
1094
0
    }
1095
6
  }
1096
54
  if (
In<ELFT>::RelaPlt->getParent() && 54
!In<ELFT>::RelaPlt->empty()54
) {
1097
6
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1098
6
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1099
6
    switch (Config->EMachine) {
1100
6
    case EM_MIPS:
1101
6
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1102
6
      break;
1103
0
    case EM_SPARCV9:
1104
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1105
0
      break;
1106
0
    default:
1107
0
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1108
0
      break;
1109
6
    }
1110
6
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 6
DT_RELA0
:
DT_REL6
)});
1111
6
  }
1112
54
1113
54
  add({DT_SYMTAB, InX::DynSymTab});
1114
54
  add({DT_SYMENT, sizeof(Elf_Sym)});
1115
54
  add({DT_STRTAB, InX::DynStrTab});
1116
54
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1117
54
  if (!Config->ZText)
1118
0
    add({DT_TEXTREL, (uint64_t)0});
1119
54
  if (InX::GnuHashTab)
1120
0
    add({DT_GNU_HASH, InX::GnuHashTab});
1121
54
  if (In<ELFT>::HashTab)
1122
54
    add({DT_HASH, In<ELFT>::HashTab});
1123
54
1124
54
  if (
Out::PreinitArray54
) {
1125
0
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1126
0
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1127
0
  }
1128
54
  if (
Out::InitArray54
) {
1129
0
    add({DT_INIT_ARRAY, Out::InitArray});
1130
0
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1131
0
  }
1132
54
  if (
Out::FiniArray54
) {
1133
0
    add({DT_FINI_ARRAY, Out::FiniArray});
1134
0
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1135
0
  }
1136
54
1137
54
  if (SymbolBody *B = Symtab->find(Config->Init))
1138
0
    
if (0
B->isInCurrentDSO()0
)
1139
0
      add({DT_INIT, B});
1140
54
  if (SymbolBody *B = Symtab->find(Config->Fini))
1141
0
    
if (0
B->isInCurrentDSO()0
)
1142
0
      add({DT_FINI, B});
1143
54
1144
54
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1145
54
  if (
HasVerNeed || 54
In<ELFT>::VerDef54
)
1146
0
    add({DT_VERSYM, In<ELFT>::VerSym});
1147
54
  if (
In<ELFT>::VerDef54
) {
1148
0
    add({DT_VERDEF, In<ELFT>::VerDef});
1149
0
    add({DT_VERDEFNUM, getVerDefNum()});
1150
0
  }
1151
54
  if (
HasVerNeed54
) {
1152
0
    add({DT_VERNEED, In<ELFT>::VerNeed});
1153
0
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1154
0
  }
1155
54
1156
54
  if (
Config->EMachine == EM_MIPS54
) {
1157
53
    add({DT_MIPS_RLD_VERSION, 1});
1158
53
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1159
53
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1160
53
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1161
53
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1162
53
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1163
21
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1164
53
    else
1165
32
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1166
53
    add({DT_PLTGOT, InX::MipsGot});
1167
53
    if (InX::MipsRldMap)
1168
13
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1169
53
  }
1170
54
1171
54
  getParent()->Link = this->Link;
1172
54
1173
54
  // +1 for DT_NULL
1174
54
  this->Size = (Entries.size() + 1) * this->Entsize;
1175
54
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1074
102
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1075
102
  if (this->Size)
1076
0
    return; // Already finalized.
1077
102
1078
102
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1079
102
  if (
In<ELFT>::RelaDyn->getParent() && 102
!In<ELFT>::RelaDyn->empty()102
) {
1080
37
    bool IsRela = Config->IsRela;
1081
37
    add({IsRela ? 
DT_RELA0
:
DT_REL37
, In<ELFT>::RelaDyn});
1082
37
    add({IsRela ? 
DT_RELASZ0
:
DT_RELSZ37
, In<ELFT>::RelaDyn->getParent(),
1083
37
         Entry::SecSize});
1084
37
    add({IsRela ? 
DT_RELAENT0
:
DT_RELENT37
,
1085
37
         uint64_t(IsRela ? 
sizeof(Elf_Rela)0
:
sizeof(Elf_Rel)37
)});
1086
37
1087
37
    // MIPS dynamic loader does not support RELCOUNT tag.
1088
37
    // The problem is in the tight relation between dynamic
1089
37
    // relocations and GOT. So do not emit this tag on MIPS.
1090
37
    if (
Config->EMachine != EM_MIPS37
) {
1091
36
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1092
36
      if (
Config->ZCombreloc && 36
NumRelativeRels36
)
1093
9
        
add({IsRela ? 9
DT_RELACOUNT0
:
DT_RELCOUNT9
, NumRelativeRels});
1094
36
    }
1095
37
  }
1096
102
  if (
In<ELFT>::RelaPlt->getParent() && 102
!In<ELFT>::RelaPlt->empty()102
) {
1097
24
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1098
24
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1099
24
    switch (Config->EMachine) {
1100
2
    case EM_MIPS:
1101
2
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1102
2
      break;
1103
0
    case EM_SPARCV9:
1104
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1105
0
      break;
1106
22
    default:
1107
22
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1108
22
      break;
1109
24
    }
1110
24
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 24
DT_RELA1
:
DT_REL23
)});
1111
24
  }
1112
102
1113
102
  add({DT_SYMTAB, InX::DynSymTab});
1114
102
  add({DT_SYMENT, sizeof(Elf_Sym)});
1115
102
  add({DT_STRTAB, InX::DynStrTab});
1116
102
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1117
102
  if (!Config->ZText)
1118
0
    add({DT_TEXTREL, (uint64_t)0});
1119
102
  if (InX::GnuHashTab)
1120
2
    add({DT_GNU_HASH, InX::GnuHashTab});
1121
102
  if (In<ELFT>::HashTab)
1122
100
    add({DT_HASH, In<ELFT>::HashTab});
1123
102
1124
102
  if (
Out::PreinitArray102
) {
1125
0
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1126
0
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1127
0
  }
1128
102
  if (
Out::InitArray102
) {
1129
0
    add({DT_INIT_ARRAY, Out::InitArray});
1130
0
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1131
0
  }
1132
102
  if (
Out::FiniArray102
) {
1133
0
    add({DT_FINI_ARRAY, Out::FiniArray});
1134
0
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1135
0
  }
1136
102
1137
102
  if (SymbolBody *B = Symtab->find(Config->Init))
1138
0
    
if (0
B->isInCurrentDSO()0
)
1139
0
      add({DT_INIT, B});
1140
102
  if (SymbolBody *B = Symtab->find(Config->Fini))
1141
0
    
if (0
B->isInCurrentDSO()0
)
1142
0
      add({DT_FINI, B});
1143
102
1144
102
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1145
102
  if (
HasVerNeed || 102
In<ELFT>::VerDef102
)
1146
0
    add({DT_VERSYM, In<ELFT>::VerSym});
1147
102
  if (
In<ELFT>::VerDef102
) {
1148
0
    add({DT_VERDEF, In<ELFT>::VerDef});
1149
0
    add({DT_VERDEFNUM, getVerDefNum()});
1150
0
  }
1151
102
  if (
HasVerNeed102
) {
1152
0
    add({DT_VERNEED, In<ELFT>::VerNeed});
1153
0
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1154
0
  }
1155
102
1156
102
  if (
Config->EMachine == EM_MIPS102
) {
1157
8
    add({DT_MIPS_RLD_VERSION, 1});
1158
8
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1159
8
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1160
8
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1161
8
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1162
8
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1163
3
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1164
8
    else
1165
5
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1166
8
    add({DT_PLTGOT, InX::MipsGot});
1167
8
    if (InX::MipsRldMap)
1168
2
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1169
8
  }
1170
102
1171
102
  getParent()->Link = this->Link;
1172
102
1173
102
  // +1 for DT_NULL
1174
102
  this->Size = (Entries.size() + 1) * this->Entsize;
1175
102
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1074
25
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1075
25
  if (this->Size)
1076
0
    return; // Already finalized.
1077
25
1078
25
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1079
25
  if (
In<ELFT>::RelaDyn->getParent() && 25
!In<ELFT>::RelaDyn->empty()25
) {
1080
10
    bool IsRela = Config->IsRela;
1081
10
    add({IsRela ? 
DT_RELA10
:
DT_REL0
, In<ELFT>::RelaDyn});
1082
10
    add({IsRela ? 
DT_RELASZ10
:
DT_RELSZ0
, In<ELFT>::RelaDyn->getParent(),
1083
10
         Entry::SecSize});
1084
10
    add({IsRela ? 
DT_RELAENT10
:
DT_RELENT0
,
1085
10
         uint64_t(IsRela ? 
sizeof(Elf_Rela)10
:
sizeof(Elf_Rel)0
)});
1086
10
1087
10
    // MIPS dynamic loader does not support RELCOUNT tag.
1088
10
    // The problem is in the tight relation between dynamic
1089
10
    // relocations and GOT. So do not emit this tag on MIPS.
1090
10
    if (
Config->EMachine != EM_MIPS10
) {
1091
5
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1092
5
      if (
Config->ZCombreloc && 5
NumRelativeRels5
)
1093
3
        
add({IsRela ? 3
DT_RELACOUNT3
:
DT_RELCOUNT0
, NumRelativeRels});
1094
5
    }
1095
10
  }
1096
25
  if (
In<ELFT>::RelaPlt->getParent() && 25
!In<ELFT>::RelaPlt->empty()25
) {
1097
2
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1098
2
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1099
2
    switch (Config->EMachine) {
1100
0
    case EM_MIPS:
1101
0
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1102
0
      break;
1103
0
    case EM_SPARCV9:
1104
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1105
0
      break;
1106
2
    default:
1107
2
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1108
2
      break;
1109
2
    }
1110
2
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 2
DT_RELA2
:
DT_REL0
)});
1111
2
  }
1112
25
1113
25
  add({DT_SYMTAB, InX::DynSymTab});
1114
25
  add({DT_SYMENT, sizeof(Elf_Sym)});
1115
25
  add({DT_STRTAB, InX::DynStrTab});
1116
25
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1117
25
  if (!Config->ZText)
1118
2
    add({DT_TEXTREL, (uint64_t)0});
1119
25
  if (InX::GnuHashTab)
1120
1
    add({DT_GNU_HASH, InX::GnuHashTab});
1121
25
  if (In<ELFT>::HashTab)
1122
25
    add({DT_HASH, In<ELFT>::HashTab});
1123
25
1124
25
  if (
Out::PreinitArray25
) {
1125
0
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1126
0
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1127
0
  }
1128
25
  if (
Out::InitArray25
) {
1129
0
    add({DT_INIT_ARRAY, Out::InitArray});
1130
0
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1131
0
  }
1132
25
  if (
Out::FiniArray25
) {
1133
0
    add({DT_FINI_ARRAY, Out::FiniArray});
1134
0
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1135
0
  }
1136
25
1137
25
  if (SymbolBody *B = Symtab->find(Config->Init))
1138
0
    
if (0
B->isInCurrentDSO()0
)
1139
0
      add({DT_INIT, B});
1140
25
  if (SymbolBody *B = Symtab->find(Config->Fini))
1141
0
    
if (0
B->isInCurrentDSO()0
)
1142
0
      add({DT_FINI, B});
1143
25
1144
25
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1145
25
  if (
HasVerNeed || 25
In<ELFT>::VerDef25
)
1146
0
    add({DT_VERSYM, In<ELFT>::VerSym});
1147
25
  if (
In<ELFT>::VerDef25
) {
1148
0
    add({DT_VERDEF, In<ELFT>::VerDef});
1149
0
    add({DT_VERDEFNUM, getVerDefNum()});
1150
0
  }
1151
25
  if (
HasVerNeed25
) {
1152
0
    add({DT_VERNEED, In<ELFT>::VerNeed});
1153
0
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1154
0
  }
1155
25
1156
25
  if (
Config->EMachine == EM_MIPS25
) {
1157
16
    add({DT_MIPS_RLD_VERSION, 1});
1158
16
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1159
16
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1160
16
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1161
16
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1162
16
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1163
5
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1164
16
    else
1165
11
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1166
16
    add({DT_PLTGOT, InX::MipsGot});
1167
16
    if (InX::MipsRldMap)
1168
5
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1169
16
  }
1170
25
1171
25
  getParent()->Link = this->Link;
1172
25
1173
25
  // +1 for DT_NULL
1174
25
  this->Size = (Entries.size() + 1) * this->Entsize;
1175
25
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1074
581
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1075
581
  if (this->Size)
1076
0
    return; // Already finalized.
1077
581
1078
581
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1079
581
  if (
In<ELFT>::RelaDyn->getParent() && 581
!In<ELFT>::RelaDyn->empty()581
) {
1080
95
    bool IsRela = Config->IsRela;
1081
95
    add({IsRela ? 
DT_RELA95
:
DT_REL0
, In<ELFT>::RelaDyn});
1082
95
    add({IsRela ? 
DT_RELASZ95
:
DT_RELSZ0
, In<ELFT>::RelaDyn->getParent(),
1083
95
         Entry::SecSize});
1084
95
    add({IsRela ? 
DT_RELAENT95
:
DT_RELENT0
,
1085
95
         uint64_t(IsRela ? 
sizeof(Elf_Rela)95
:
sizeof(Elf_Rel)0
)});
1086
95
1087
95
    // MIPS dynamic loader does not support RELCOUNT tag.
1088
95
    // The problem is in the tight relation between dynamic
1089
95
    // relocations and GOT. So do not emit this tag on MIPS.
1090
95
    if (
Config->EMachine != EM_MIPS95
) {
1091
95
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1092
95
      if (
Config->ZCombreloc && 95
NumRelativeRels94
)
1093
17
        
add({IsRela ? 17
DT_RELACOUNT17
:
DT_RELCOUNT0
, NumRelativeRels});
1094
95
    }
1095
95
  }
1096
581
  if (
In<ELFT>::RelaPlt->getParent() && 581
!In<ELFT>::RelaPlt->empty()581
) {
1097
100
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1098
100
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1099
100
    switch (Config->EMachine) {
1100
0
    case EM_MIPS:
1101
0
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1102
0
      break;
1103
0
    case EM_SPARCV9:
1104
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1105
0
      break;
1106
100
    default:
1107
100
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1108
100
      break;
1109
100
    }
1110
100
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 100
DT_RELA100
:
DT_REL0
)});
1111
100
  }
1112
581
1113
581
  add({DT_SYMTAB, InX::DynSymTab});
1114
581
  add({DT_SYMENT, sizeof(Elf_Sym)});
1115
581
  add({DT_STRTAB, InX::DynStrTab});
1116
581
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1117
581
  if (!Config->ZText)
1118
3
    add({DT_TEXTREL, (uint64_t)0});
1119
581
  if (InX::GnuHashTab)
1120
2
    add({DT_GNU_HASH, InX::GnuHashTab});
1121
581
  if (In<ELFT>::HashTab)
1122
579
    add({DT_HASH, In<ELFT>::HashTab});
1123
581
1124
581
  if (
Out::PreinitArray581
) {
1125
3
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1126
3
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1127
3
  }
1128
581
  if (
Out::InitArray581
) {
1129
3
    add({DT_INIT_ARRAY, Out::InitArray});
1130
3
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1131
3
  }
1132
581
  if (
Out::FiniArray581
) {
1133
2
    add({DT_FINI_ARRAY, Out::FiniArray});
1134
2
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1135
2
  }
1136
581
1137
581
  if (SymbolBody *B = Symtab->find(Config->Init))
1138
6
    
if (6
B->isInCurrentDSO()6
)
1139
4
      add({DT_INIT, B});
1140
581
  if (SymbolBody *B = Symtab->find(Config->Fini))
1141
6
    
if (6
B->isInCurrentDSO()6
)
1142
4
      add({DT_FINI, B});
1143
581
1144
581
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1145
581
  if (
HasVerNeed || 581
In<ELFT>::VerDef575
)
1146
41
    add({DT_VERSYM, In<ELFT>::VerSym});
1147
581
  if (
In<ELFT>::VerDef581
) {
1148
36
    add({DT_VERDEF, In<ELFT>::VerDef});
1149
36
    add({DT_VERDEFNUM, getVerDefNum()});
1150
36
  }
1151
581
  if (
HasVerNeed581
) {
1152
6
    add({DT_VERNEED, In<ELFT>::VerNeed});
1153
6
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1154
6
  }
1155
581
1156
581
  if (
Config->EMachine == EM_MIPS581
) {
1157
0
    add({DT_MIPS_RLD_VERSION, 1});
1158
0
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1159
0
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1160
0
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1161
0
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1162
0
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1163
0
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1164
0
    else
1165
0
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1166
0
    add({DT_PLTGOT, InX::MipsGot});
1167
0
    if (InX::MipsRldMap)
1168
0
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1169
0
  }
1170
581
1171
581
  getParent()->Link = this->Link;
1172
581
1173
581
  // +1 for DT_NULL
1174
581
  this->Size = (Entries.size() + 1) * this->Entsize;
1175
581
}
1176
1177
760
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1178
760
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1179
760
1180
5.93k
  for (const Entry &E : Entries) {
1181
5.93k
    P->d_tag = E.Tag;
1182
5.93k
    switch (E.Kind) {
1183
8
    case Entry::SecAddr:
1184
8
      P->d_un.d_ptr = E.OutSec->Addr;
1185
8
      break;
1186
2.87k
    case Entry::InSecAddr:
1187
2.87k
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1188
2.87k
      break;
1189
287
    case Entry::SecSize:
1190
287
      P->d_un.d_val = E.OutSec->Size;
1191
287
      break;
1192
8
    case Entry::SymAddr:
1193
8
      P->d_un.d_ptr = E.Sym->getVA();
1194
8
      break;
1195
2.76k
    case Entry::PlainInt:
1196
2.76k
      P->d_un.d_val = E.Val;
1197
2.76k
      break;
1198
5.93k
    }
1199
5.93k
    ++P;
1200
5.93k
  }
1201
760
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1177
102
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1178
102
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1179
102
1180
852
  for (const Entry &E : Entries) {
1181
852
    P->d_tag = E.Tag;
1182
852
    switch (E.Kind) {
1183
0
    case Entry::SecAddr:
1184
0
      P->d_un.d_ptr = E.OutSec->Addr;
1185
0
      break;
1186
401
    case Entry::InSecAddr:
1187
401
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1188
401
      break;
1189
61
    case Entry::SecSize:
1190
61
      P->d_un.d_val = E.OutSec->Size;
1191
61
      break;
1192
0
    case Entry::SymAddr:
1193
0
      P->d_un.d_ptr = E.Sym->getVA();
1194
0
      break;
1195
390
    case Entry::PlainInt:
1196
390
      P->d_un.d_val = E.Val;
1197
390
      break;
1198
852
    }
1199
852
    ++P;
1200
852
  }
1201
102
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1177
25
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1178
25
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1179
25
1180
301
  for (const Entry &E : Entries) {
1181
301
    P->d_tag = E.Tag;
1182
301
    switch (E.Kind) {
1183
0
    case Entry::SecAddr:
1184
0
      P->d_un.d_ptr = E.OutSec->Addr;
1185
0
      break;
1186
111
    case Entry::InSecAddr:
1187
111
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1188
111
      break;
1189
12
    case Entry::SecSize:
1190
12
      P->d_un.d_val = E.OutSec->Size;
1191
12
      break;
1192
0
    case Entry::SymAddr:
1193
0
      P->d_un.d_ptr = E.Sym->getVA();
1194
0
      break;
1195
178
    case Entry::PlainInt:
1196
178
      P->d_un.d_val = E.Val;
1197
178
      break;
1198
301
    }
1199
301
    ++P;
1200
301
  }
1201
25
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1177
579
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1178
579
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1179
579
1180
4.05k
  for (const Entry &E : Entries) {
1181
4.05k
    P->d_tag = E.Tag;
1182
4.05k
    switch (E.Kind) {
1183
8
    case Entry::SecAddr:
1184
8
      P->d_un.d_ptr = E.OutSec->Addr;
1185
8
      break;
1186
2.11k
    case Entry::InSecAddr:
1187
2.11k
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1188
2.11k
      break;
1189
202
    case Entry::SecSize:
1190
202
      P->d_un.d_val = E.OutSec->Size;
1191
202
      break;
1192
8
    case Entry::SymAddr:
1193
8
      P->d_un.d_ptr = E.Sym->getVA();
1194
8
      break;
1195
1.72k
    case Entry::PlainInt:
1196
1.72k
      P->d_un.d_val = E.Val;
1197
1.72k
      break;
1198
4.05k
    }
1199
4.05k
    ++P;
1200
4.05k
  }
1201
579
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1177
54
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1178
54
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1179
54
1180
727
  for (const Entry &E : Entries) {
1181
727
    P->d_tag = E.Tag;
1182
727
    switch (E.Kind) {
1183
0
    case Entry::SecAddr:
1184
0
      P->d_un.d_ptr = E.OutSec->Addr;
1185
0
      break;
1186
246
    case Entry::InSecAddr:
1187
246
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1188
246
      break;
1189
12
    case Entry::SecSize:
1190
12
      P->d_un.d_val = E.OutSec->Size;
1191
12
      break;
1192
0
    case Entry::SymAddr:
1193
0
      P->d_un.d_ptr = E.Sym->getVA();
1194
0
      break;
1195
469
    case Entry::PlainInt:
1196
469
      P->d_un.d_val = E.Val;
1197
469
      break;
1198
727
    }
1199
727
    ++P;
1200
727
  }
1201
54
}
1202
1203
530
uint64_t DynamicReloc::getOffset() const {
1204
530
  return InputSec->getOutputSection()->Addr + InputSec->getOffset(OffsetInSec);
1205
530
}
1206
1207
362
int64_t DynamicReloc::getAddend() const {
1208
362
  if (UseSymVA)
1209
59
    return Sym->getVA(Addend);
1210
303
  return Addend;
1211
303
}
1212
1213
530
uint32_t DynamicReloc::getSymIndex() const {
1214
530
  if (
Sym && 530
!UseSymVA525
)
1215
435
    return Sym->DynsymIndex;
1216
95
  return 0;
1217
95
}
1218
1219
template <class ELFT>
1220
RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
1221
    : SyntheticSection(SHF_ALLOC, Config->IsRela ? SHT_RELA : SHT_REL,
1222
                       Config->Wordsize, Name),
1223
4.76k
      Sort(Sort) {
1224
4.76k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)3.92k
:
sizeof(Elf_Rel)846
;
1225
4.76k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1223
285
      Sort(Sort) {
1224
285
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)3
:
sizeof(Elf_Rel)282
;
1225
285
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1223
573
      Sort(Sort) {
1224
573
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)9
:
sizeof(Elf_Rel)564
;
1225
573
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1223
3.77k
      Sort(Sort) {
1224
3.77k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)3.77k
:
sizeof(Elf_Rel)0
;
1225
3.77k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1223
132
      Sort(Sort) {
1224
132
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)132
:
sizeof(Elf_Rel)0
;
1225
132
}
1226
1227
template <class ELFT>
1228
547
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1229
547
  if (Reloc.Type == Target->RelativeRel)
1230
60
    ++NumRelativeRelocs;
1231
547
  Relocs.push_back(Reloc);
1232
547
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1228
348
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1229
348
  if (Reloc.Type == Target->RelativeRel)
1230
26
    ++NumRelativeRelocs;
1231
348
  Relocs.push_back(Reloc);
1232
348
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1228
30
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1229
30
  if (Reloc.Type == Target->RelativeRel)
1230
14
    ++NumRelativeRelocs;
1231
30
  Relocs.push_back(Reloc);
1232
30
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1228
25
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1229
25
  if (Reloc.Type == Target->RelativeRel)
1230
2
    ++NumRelativeRelocs;
1231
25
  Relocs.push_back(Reloc);
1232
25
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1228
144
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1229
144
  if (Reloc.Type == Target->RelativeRel)
1230
18
    ++NumRelativeRelocs;
1231
144
  Relocs.push_back(Reloc);
1232
144
}
1233
1234
template <class ELFT, class RelTy>
1235
243
static bool compRelocations(const RelTy &A, const RelTy &B) {
1236
243
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1237
243
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
243
  if (AIsRel != BIsRel)
1239
28
    return AIsRel;
1240
215
1241
215
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1242
215
}
Unexecuted instantiation: SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&)
SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&)
Line
Count
Source
1235
83
static bool compRelocations(const RelTy &A, const RelTy &B) {
1236
83
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1237
83
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
83
  if (AIsRel != BIsRel)
1239
17
    return AIsRel;
1240
66
1241
66
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1242
66
}
SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&)
Line
Count
Source
1235
109
static bool compRelocations(const RelTy &A, const RelTy &B) {
1236
109
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1237
109
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
109
  if (AIsRel != BIsRel)
1239
10
    return AIsRel;
1240
99
1241
99
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1242
99
}
SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&)
Line
Count
Source
1235
22
static bool compRelocations(const RelTy &A, const RelTy &B) {
1236
22
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1237
22
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
22
  if (AIsRel != BIsRel)
1239
0
    return AIsRel;
1240
22
1241
22
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1242
22
}
Unexecuted instantiation: SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&)
Unexecuted instantiation: SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&)
SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&)
Line
Count
Source
1235
29
static bool compRelocations(const RelTy &A, const RelTy &B) {
1236
29
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1237
29
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
29
  if (AIsRel != BIsRel)
1239
1
    return AIsRel;
1240
28
1241
28
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1242
28
}
Unexecuted instantiation: SyntheticSections.cpp:bool compRelocations<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&)
1243
1244
294
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1245
294
  uint8_t *BufBegin = Buf;
1246
530
  for (const DynamicReloc &Rel : Relocs) {
1247
530
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1248
530
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)362
:
sizeof(Elf_Rel)168
;
1249
530
1250
530
    if (Config->IsRela)
1251
362
      P->r_addend = Rel.getAddend();
1252
530
    P->r_offset = Rel.getOffset();
1253
530
    if (
Config->EMachine == EM_MIPS && 530
Rel.getInputSec() == InX::MipsGot43
)
1254
530
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1255
530
      // allocated in the end of the GOT. We need to adjust the offset to take
1256
530
      // in account 'local' and 'global' GOT entries.
1257
20
      P->r_offset += InX::MipsGot->getTlsOffset();
1258
530
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1259
530
  }
1260
294
1261
294
  if (
Sort294
) {
1262
146
    if (Config->IsRela)
1263
103
      std::stable_sort((Elf_Rela *)BufBegin,
1264
103
                       (Elf_Rela *)BufBegin + Relocs.size(),
1265
103
                       compRelocations<ELFT, Elf_Rela>);
1266
146
    else
1267
43
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1268
43
                       compRelocations<ELFT, Elf_Rel>);
1269
146
  }
1270
294
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1244
12
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1245
12
  uint8_t *BufBegin = Buf;
1246
25
  for (const DynamicReloc &Rel : Relocs) {
1247
25
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1248
25
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)0
:
sizeof(Elf_Rel)25
;
1249
25
1250
25
    if (Config->IsRela)
1251
0
      P->r_addend = Rel.getAddend();
1252
25
    P->r_offset = Rel.getOffset();
1253
25
    if (
Config->EMachine == EM_MIPS && 25
Rel.getInputSec() == InX::MipsGot25
)
1254
25
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1255
25
      // allocated in the end of the GOT. We need to adjust the offset to take
1256
25
      // in account 'local' and 'global' GOT entries.
1257
10
      P->r_offset += InX::MipsGot->getTlsOffset();
1258
25
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1259
25
  }
1260
12
1261
12
  if (
Sort12
) {
1262
6
    if (Config->IsRela)
1263
0
      std::stable_sort((Elf_Rela *)BufBegin,
1264
0
                       (Elf_Rela *)BufBegin + Relocs.size(),
1265
0
                       compRelocations<ELFT, Elf_Rela>);
1266
6
    else
1267
6
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1268
6
                       compRelocations<ELFT, Elf_Rel>);
1269
6
  }
1270
12
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1244
203
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1245
203
  uint8_t *BufBegin = Buf;
1246
331
  for (const DynamicReloc &Rel : Relocs) {
1247
331
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1248
331
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)331
:
sizeof(Elf_Rel)0
;
1249
331
1250
331
    if (Config->IsRela)
1251
331
      P->r_addend = Rel.getAddend();
1252
331
    P->r_offset = Rel.getOffset();
1253
331
    if (
Config->EMachine == EM_MIPS && 331
Rel.getInputSec() == InX::MipsGot0
)
1254
331
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1255
331
      // allocated in the end of the GOT. We need to adjust the offset to take
1256
331
      // in account 'local' and 'global' GOT entries.
1257
0
      P->r_offset += InX::MipsGot->getTlsOffset();
1258
331
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1259
331
  }
1260
203
1261
203
  if (
Sort203
) {
1262
93
    if (Config->IsRela)
1263
93
      std::stable_sort((Elf_Rela *)BufBegin,
1264
93
                       (Elf_Rela *)BufBegin + Relocs.size(),
1265
93
                       compRelocations<ELFT, Elf_Rela>);
1266
93
    else
1267
0
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1268
0
                       compRelocations<ELFT, Elf_Rel>);
1269
93
  }
1270
203
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1244
12
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1245
12
  uint8_t *BufBegin = Buf;
1246
30
  for (const DynamicReloc &Rel : Relocs) {
1247
30
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1248
30
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)30
:
sizeof(Elf_Rel)0
;
1249
30
1250
30
    if (Config->IsRela)
1251
30
      P->r_addend = Rel.getAddend();
1252
30
    P->r_offset = Rel.getOffset();
1253
30
    if (
Config->EMachine == EM_MIPS && 30
Rel.getInputSec() == InX::MipsGot14
)
1254
30
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1255
30
      // allocated in the end of the GOT. We need to adjust the offset to take
1256
30
      // in account 'local' and 'global' GOT entries.
1257
10
      P->r_offset += InX::MipsGot->getTlsOffset();
1258
30
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1259
30
  }
1260
12
1261
12
  if (
Sort12
) {
1262
10
    if (Config->IsRela)
1263
10
      std::stable_sort((Elf_Rela *)BufBegin,
1264
10
                       (Elf_Rela *)BufBegin + Relocs.size(),
1265
10
                       compRelocations<ELFT, Elf_Rela>);
1266
10
    else
1267
0
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1268
0
                       compRelocations<ELFT, Elf_Rel>);
1269
10
  }
1270
12
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1244
67
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1245
67
  uint8_t *BufBegin = Buf;
1246
144
  for (const DynamicReloc &Rel : Relocs) {
1247
144
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1248
144
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)143
;
1249
144
1250
144
    if (Config->IsRela)
1251
1
      P->r_addend = Rel.getAddend();
1252
144
    P->r_offset = Rel.getOffset();
1253
144
    if (
Config->EMachine == EM_MIPS && 144
Rel.getInputSec() == InX::MipsGot4
)
1254
144
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1255
144
      // allocated in the end of the GOT. We need to adjust the offset to take
1256
144
      // in account 'local' and 'global' GOT entries.
1257
0
      P->r_offset += InX::MipsGot->getTlsOffset();
1258
144
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1259
144
  }
1260
67
1261
67
  if (
Sort67
) {
1262
37
    if (Config->IsRela)
1263
0
      std::stable_sort((Elf_Rela *)BufBegin,
1264
0
                       (Elf_Rela *)BufBegin + Relocs.size(),
1265
0
                       compRelocations<ELFT, Elf_Rela>);
1266
37
    else
1267
37
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1268
37
                       compRelocations<ELFT, Elf_Rel>);
1269
37
  }
1270
67
}
1271
1272
214
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1273
214
  return this->Entsize * Relocs.size();
1274
214
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getRelocOffset()
Line
Count
Source
1272
8
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1273
8
  return this->Entsize * Relocs.size();
1274
8
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getRelocOffset()
Line
Count
Source
1272
150
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1273
150
  return this->Entsize * Relocs.size();
1274
150
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getRelocOffset()
Line
Count
Source
1272
54
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1273
54
  return this->Entsize * Relocs.size();
1274
54
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getRelocOffset()
Line
Count
Source
1272
2
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1273
2
  return this->Entsize * Relocs.size();
1274
2
}
1275
1276
295
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1277
286
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1278
9
                              : InX::SymTab->getParent()->SectionIndex;
1279
295
1280
295
  // Set required output section properties.
1281
295
  getParent()->Link = this->Link;
1282
295
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1276
67
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1277
63
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1278
4
                              : InX::SymTab->getParent()->SectionIndex;
1279
67
1280
67
  // Set required output section properties.
1281
67
  getParent()->Link = this->Link;
1282
67
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1276
12
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1277
12
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1278
0
                              : InX::SymTab->getParent()->SectionIndex;
1279
12
1280
12
  // Set required output section properties.
1281
12
  getParent()->Link = this->Link;
1282
12
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1276
204
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1277
199
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1278
5
                              : InX::SymTab->getParent()->SectionIndex;
1279
204
1280
204
  // Set required output section properties.
1281
204
  getParent()->Link = this->Link;
1282
204
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1276
12
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1277
12
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1278
0
                              : InX::SymTab->getParent()->SectionIndex;
1279
12
1280
12
  // Set required output section properties.
1281
12
  getParent()->Link = this->Link;
1282
12
}
1283
1284
SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &StrTabSec)
1285
    : SyntheticSection(StrTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1286
                       StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1287
                       Config->Wordsize,
1288
                       StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1289
2.38k
      StrTabSec(StrTabSec) {}
1290
1291
// Orders symbols according to their positions in the GOT,
1292
// in compliance with MIPS ABI rules.
1293
// See "Global Offset Table" in Chapter 5 in the following document
1294
// for detailed description:
1295
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1296
static bool sortMipsSymbols(const SymbolTableEntry &L,
1297
115
                            const SymbolTableEntry &R) {
1298
115
  // Sort entries related to non-local preemptible symbols by GOT indexes.
1299
115
  // All other entries go to the first part of GOT in arbitrary order.
1300
115
  bool LIsInLocalGot = !L.Symbol->IsInGlobalMipsGot;
1301
115
  bool RIsInLocalGot = !R.Symbol->IsInGlobalMipsGot;
1302
115
  if (
LIsInLocalGot || 115
RIsInLocalGot20
)
1303
111
    return !RIsInLocalGot;
1304
4
  return L.Symbol->GotIndex < R.Symbol->GotIndex;
1305
4
}
1306
1307
// Finalize a symbol table. The ELF spec requires that all local
1308
// symbols precede global symbols, so we sort symbol entries in this
1309
// function. (For .dynsym, we don't do that because symbols for
1310
// dynamic linking are inherently all globals.)
1311
2.28k
void SymbolTableBaseSection::finalizeContents() {
1312
2.28k
  getParent()->Link = StrTabSec.getParent()->SectionIndex;
1313
2.28k
1314
2.28k
  // If it is a .dynsym, there should be no local symbols, but we need
1315
2.28k
  // to do a few things for the dynamic linker.
1316
2.28k
  if (
this->Type == SHT_DYNSYM2.28k
) {
1317
762
    // Section's Info field has the index of the first non-local symbol.
1318
762
    // Because the first symbol entry is a null entry, 1 is the first.
1319
762
    getParent()->Info = 1;
1320
762
1321
762
    if (
InX::GnuHashTab762
) {
1322
5
      // NB: It also sorts Symbols to meet the GNU hash table requirements.
1323
5
      InX::GnuHashTab->addSymbols(Symbols);
1324
762
    } else 
if (757
Config->EMachine == EM_MIPS757
) {
1325
77
      std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
1326
77
    }
1327
762
1328
762
    size_t I = 0;
1329
762
    for (const SymbolTableEntry &S : Symbols)
1330
1.25k
      S.Symbol->DynsymIndex = ++I;
1331
762
    return;
1332
762
  }
1333
1.52k
}
1334
1335
1.52k
void SymbolTableBaseSection::postThunkContents() {
1336
1.52k
  if (this->Type == SHT_DYNSYM)
1337
0
    return;
1338
1.52k
  // move all local symbols before global symbols.
1339
1.52k
  auto It = std::stable_partition(
1340
5.06k
      Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) {
1341
5.06k
        return S.Symbol->isLocal() ||
1342
4.10k
               S.Symbol->symbol()->computeBinding() == STB_LOCAL;
1343
5.06k
      });
1344
1.52k
  size_t NumLocals = It - Symbols.begin();
1345
1.52k
  getParent()->Info = NumLocals + 1;
1346
1.52k
}
1347
1348
6.56k
void SymbolTableBaseSection::addSymbol(SymbolBody *B) {
1349
6.56k
  // Adding a local symbol to a .dynsym is a bug.
1350
6.56k
  assert(this->Type != SHT_DYNSYM || !B->isLocal());
1351
6.56k
1352
6.56k
  bool HashIt = B->isLocal();
1353
6.56k
  Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)});
1354
6.56k
}
1355
1356
109
size_t SymbolTableBaseSection::getSymbolIndex(SymbolBody *Body) {
1357
597
  auto I = llvm::find_if(Symbols, [&](const SymbolTableEntry &E) {
1358
597
    if (E.Symbol == Body)
1359
61
      return true;
1360
536
    // This is used for -r, so we have to handle multiple section
1361
536
    // symbols being combined.
1362
536
    
if (536
Body->Type == STT_SECTION && 536
E.Symbol->Type == STT_SECTION236
)
1363
129
      return Body->getOutputSection() == E.Symbol->getOutputSection();
1364
407
    return false;
1365
407
  });
1366
109
  if (I == Symbols.end())
1367
3
    return 0;
1368
106
  return I - Symbols.begin() + 1;
1369
106
}
1370
1371
template <class ELFT>
1372
SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &StrTabSec)
1373
2.38k
    : SymbolTableBaseSection(StrTabSec) {
1374
2.38k
  this->Entsize = sizeof(Elf_Sym);
1375
2.38k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1373
297
    : SymbolTableBaseSection(StrTabSec) {
1374
297
  this->Entsize = sizeof(Elf_Sym);
1375
297
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1373
149
    : SymbolTableBaseSection(StrTabSec) {
1374
149
  this->Entsize = sizeof(Elf_Sym);
1375
149
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1373
1.87k
    : SymbolTableBaseSection(StrTabSec) {
1374
1.87k
  this->Entsize = sizeof(Elf_Sym);
1375
1.87k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1373
69
    : SymbolTableBaseSection(StrTabSec) {
1374
69
  this->Entsize = sizeof(Elf_Sym);
1375
69
}
1376
1377
// Write the internal symbol table contents to the output symbol table.
1378
2.26k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1379
2.26k
  // The first entry is a null entry as per the ELF spec.
1380
2.26k
  Buf += sizeof(Elf_Sym);
1381
2.26k
1382
2.26k
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1383
2.26k
1384
6.27k
  for (SymbolTableEntry &Ent : Symbols) {
1385
6.27k
    SymbolBody *Body = Ent.Symbol;
1386
6.27k
1387
6.27k
    // Set st_info and st_other.
1388
6.27k
    if (
Body->isLocal()6.27k
) {
1389
951
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1390
6.27k
    } else {
1391
5.32k
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1392
5.32k
      ESym->setVisibility(Body->symbol()->Visibility);
1393
5.32k
    }
1394
6.27k
1395
6.27k
    ESym->st_name = Ent.StrTabOffset;
1396
6.27k
1397
6.27k
    // Set a section index.
1398
6.27k
    if (const OutputSection *OutSec = Body->getOutputSection())
1399
5.22k
      ESym->st_shndx = OutSec->SectionIndex;
1400
1.05k
    else 
if (1.05k
isa<DefinedRegular>(Body)1.05k
)
1401
320
      ESym->st_shndx = SHN_ABS;
1402
738
    else 
if (738
isa<DefinedCommon>(Body)738
)
1403
2
      ESym->st_shndx = SHN_COMMON;
1404
6.27k
1405
6.27k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1406
6.27k
    // for undefined symbols, so whether copying it or not is up to us if that's
1407
6.27k
    // the case. We'll leave it as zero because by not setting a value, we can
1408
6.27k
    // get the exact same outputs for two sets of input files that differ only
1409
6.27k
    // in undefined symbol size in DSOs.
1410
6.27k
    if (ESym->st_shndx != SHN_UNDEF)
1411
5.54k
      ESym->st_size = Body->getSize<ELFT>();
1412
6.27k
1413
6.27k
    // st_value is usually an address of a symbol, but that has a
1414
6.27k
    // special meaining for uninstantiated common symbols (this can
1415
6.27k
    // occur if -r is given).
1416
6.27k
    if (
!Config->DefineCommon && 6.27k
isa<DefinedCommon>(Body)291
)
1417
2
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1418
6.27k
    else
1419
6.27k
      ESym->st_value = Body->getVA();
1420
6.27k
1421
6.27k
    ++ESym;
1422
6.27k
  }
1423
2.26k
1424
2.26k
  // On MIPS we need to mark symbol which has a PLT entry and requires
1425
2.26k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1426
2.26k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1427
2.26k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1428
2.26k
  if (
Config->EMachine == EM_MIPS2.26k
) {
1429
212
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1430
212
1431
867
    for (SymbolTableEntry &Ent : Symbols) {
1432
867
      SymbolBody *Body = Ent.Symbol;
1433
867
      if (
Body->isInPlt() && 867
Body->NeedsPltAddr20
)
1434
6
        ESym->st_other |= STO_MIPS_PLT;
1435
867
1436
867
      if (Config->Relocatable)
1437
49
        
if (auto *49
D49
= dyn_cast<DefinedRegular>(Body))
1438
49
          
if (49
D->isMipsPIC<ELFT>()49
)
1439
6
            ESym->st_other |= STO_MIPS_PIC;
1440
867
      ++ESym;
1441
867
    }
1442
212
  }
1443
2.26k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1378
147
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1379
147
  // The first entry is a null entry as per the ELF spec.
1380
147
  Buf += sizeof(Elf_Sym);
1381
147
1382
147
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1383
147
1384
636
  for (SymbolTableEntry &Ent : Symbols) {
1385
636
    SymbolBody *Body = Ent.Symbol;
1386
636
1387
636
    // Set st_info and st_other.
1388
636
    if (
Body->isLocal()636
) {
1389
105
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1390
636
    } else {
1391
531
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1392
531
      ESym->setVisibility(Body->symbol()->Visibility);
1393
531
    }
1394
636
1395
636
    ESym->st_name = Ent.StrTabOffset;
1396
636
1397
636
    // Set a section index.
1398
636
    if (const OutputSection *OutSec = Body->getOutputSection())
1399
574
      ESym->st_shndx = OutSec->SectionIndex;
1400
62
    else 
if (62
isa<DefinedRegular>(Body)62
)
1401
12
      ESym->st_shndx = SHN_ABS;
1402
50
    else 
if (50
isa<DefinedCommon>(Body)50
)
1403
0
      ESym->st_shndx = SHN_COMMON;
1404
636
1405
636
    // Copy symbol size if it is a defined symbol. st_size is not significant
1406
636
    // for undefined symbols, so whether copying it or not is up to us if that's
1407
636
    // the case. We'll leave it as zero because by not setting a value, we can
1408
636
    // get the exact same outputs for two sets of input files that differ only
1409
636
    // in undefined symbol size in DSOs.
1410
636
    if (ESym->st_shndx != SHN_UNDEF)
1411
586
      ESym->st_size = Body->getSize<ELFT>();
1412
636
1413
636
    // st_value is usually an address of a symbol, but that has a
1414
636
    // special meaining for uninstantiated common symbols (this can
1415
636
    // occur if -r is given).
1416
636
    if (
!Config->DefineCommon && 636
isa<DefinedCommon>(Body)44
)
1417
0
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1418
636
    else
1419
636
      ESym->st_value = Body->getVA();
1420
636
1421
636
    ++ESym;
1422
636
  }
1423
147
1424
147
  // On MIPS we need to mark symbol which has a PLT entry and requires
1425
147
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1426
147
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1427
147
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1428
147
  if (
Config->EMachine == EM_MIPS147
) {
1429
144
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1430
144
1431
628
    for (SymbolTableEntry &Ent : Symbols) {
1432
628
      SymbolBody *Body = Ent.Symbol;
1433
628
      if (
Body->isInPlt() && 628
Body->NeedsPltAddr16
)
1434
6
        ESym->st_other |= STO_MIPS_PLT;
1435
628
1436
628
      if (Config->Relocatable)
1437
44
        
if (auto *44
D44
= dyn_cast<DefinedRegular>(Body))
1438
44
          
if (44
D->isMipsPIC<ELFT>()44
)
1439
6
            ESym->st_other |= STO_MIPS_PIC;
1440
628
      ++ESym;
1441
628
    }
1442
144
  }
1443
147
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1378
69
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1379
69
  // The first entry is a null entry as per the ELF spec.
1380
69
  Buf += sizeof(Elf_Sym);
1381
69
1382
69
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1383
69
1384
236
  for (SymbolTableEntry &Ent : Symbols) {
1385
236
    SymbolBody *Body = Ent.Symbol;
1386
236
1387
236
    // Set st_info and st_other.
1388
236
    if (
Body->isLocal()236
) {
1389
18
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1390
236
    } else {
1391
218
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1392
218
      ESym->setVisibility(Body->symbol()->Visibility);
1393
218
    }
1394
236
1395
236
    ESym->st_name = Ent.StrTabOffset;
1396
236
1397
236
    // Set a section index.
1398
236
    if (const OutputSection *OutSec = Body->getOutputSection())
1399
204
      ESym->st_shndx = OutSec->SectionIndex;
1400
32
    else 
if (32
isa<DefinedRegular>(Body)32
)
1401
5
      ESym->st_shndx = SHN_ABS;
1402
27
    else 
if (27
isa<DefinedCommon>(Body)27
)
1403
0
      ESym->st_shndx = SHN_COMMON;
1404
236
1405
236
    // Copy symbol size if it is a defined symbol. st_size is not significant
1406
236
    // for undefined symbols, so whether copying it or not is up to us if that's
1407
236
    // the case. We'll leave it as zero because by not setting a value, we can
1408
236
    // get the exact same outputs for two sets of input files that differ only
1409
236
    // in undefined symbol size in DSOs.
1410
236
    if (ESym->st_shndx != SHN_UNDEF)
1411
209
      ESym->st_size = Body->getSize<ELFT>();
1412
236
1413
236
    // st_value is usually an address of a symbol, but that has a
1414
236
    // special meaining for uninstantiated common symbols (this can
1415
236
    // occur if -r is given).
1416
236
    if (
!Config->DefineCommon && 236
isa<DefinedCommon>(Body)5
)
1417
0
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1418
236
    else
1419
236
      ESym->st_value = Body->getVA();
1420
236
1421
236
    ++ESym;
1422
236
  }
1423
69
1424
69
  // On MIPS we need to mark symbol which has a PLT entry and requires
1425
69
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1426
69
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1427
69
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1428
69
  if (
Config->EMachine == EM_MIPS69
) {
1429
44
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1430
44
1431
169
    for (SymbolTableEntry &Ent : Symbols) {
1432
169
      SymbolBody *Body = Ent.Symbol;
1433
169
      if (
Body->isInPlt() && 169
Body->NeedsPltAddr0
)
1434
0
        ESym->st_other |= STO_MIPS_PLT;
1435
169
1436
169
      if (Config->Relocatable)
1437
5
        
if (auto *5
D5
= dyn_cast<DefinedRegular>(Body))
1438
5
          
if (5
D->isMipsPIC<ELFT>()5
)
1439
0
            ESym->st_other |= STO_MIPS_PIC;
1440
169
      ++ESym;
1441
169
    }
1442
44
  }
1443
69
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1378
1.75k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1379
1.75k
  // The first entry is a null entry as per the ELF spec.
1380
1.75k
  Buf += sizeof(Elf_Sym);
1381
1.75k
1382
1.75k
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1383
1.75k
1384
4.23k
  for (SymbolTableEntry &Ent : Symbols) {
1385
4.23k
    SymbolBody *Body = Ent.Symbol;
1386
4.23k
1387
4.23k
    // Set st_info and st_other.
1388
4.23k
    if (
Body->isLocal()4.23k
) {
1389
435
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1390
4.23k
    } else {
1391
3.79k
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1392
3.79k
      ESym->setVisibility(Body->symbol()->Visibility);
1393
3.79k
    }
1394
4.23k
1395
4.23k
    ESym->st_name = Ent.StrTabOffset;
1396
4.23k
1397
4.23k
    // Set a section index.
1398
4.23k
    if (const OutputSection *OutSec = Body->getOutputSection())
1399
3.43k
      ESym->st_shndx = OutSec->SectionIndex;
1400
802
    else 
if (802
isa<DefinedRegular>(Body)802
)
1401
262
      ESym->st_shndx = SHN_ABS;
1402
540
    else 
if (540
isa<DefinedCommon>(Body)540
)
1403
2
      ESym->st_shndx = SHN_COMMON;
1404
4.23k
1405
4.23k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1406
4.23k
    // for undefined symbols, so whether copying it or not is up to us if that's
1407
4.23k
    // the case. We'll leave it as zero because by not setting a value, we can
1408
4.23k
    // get the exact same outputs for two sets of input files that differ only
1409
4.23k
    // in undefined symbol size in DSOs.
1410
4.23k
    if (ESym->st_shndx != SHN_UNDEF)
1411
3.69k
      ESym->st_size = Body->getSize<ELFT>();
1412
4.23k
1413
4.23k
    // st_value is usually an address of a symbol, but that has a
1414
4.23k
    // special meaining for uninstantiated common symbols (this can
1415
4.23k
    // occur if -r is given).
1416
4.23k
    if (
!Config->DefineCommon && 4.23k
isa<DefinedCommon>(Body)196
)
1417
2
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1418
4.23k
    else
1419
4.23k
      ESym->st_value = Body->getVA();
1420
4.23k
1421
4.23k
    ++ESym;
1422
4.23k
  }
1423
1.75k
1424
1.75k
  // On MIPS we need to mark symbol which has a PLT entry and requires
1425
1.75k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1426
1.75k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1427
1.75k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1428
1.75k
  if (
Config->EMachine == EM_MIPS1.75k
) {
1429
2
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1430
2
1431
4
    for (SymbolTableEntry &Ent : Symbols) {
1432
4
      SymbolBody *Body = Ent.Symbol;
1433
4
      if (
Body->isInPlt() && 4
Body->NeedsPltAddr0
)
1434
0
        ESym->st_other |= STO_MIPS_PLT;
1435
4
1436
4
      if (Config->Relocatable)
1437
0
        
if (auto *0
D0
= dyn_cast<DefinedRegular>(Body))
1438
0
          
if (0
D->isMipsPIC<ELFT>()0
)
1439
0
            ESym->st_other |= STO_MIPS_PIC;
1440
4
      ++ESym;
1441
4
    }
1442
2
  }
1443
1.75k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1378
288
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1379
288
  // The first entry is a null entry as per the ELF spec.
1380
288
  Buf += sizeof(Elf_Sym);
1381
288
1382
288
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1383
288
1384
1.17k
  for (SymbolTableEntry &Ent : Symbols) {
1385
1.17k
    SymbolBody *Body = Ent.Symbol;
1386
1.17k
1387
1.17k
    // Set st_info and st_other.
1388
1.17k
    if (
Body->isLocal()1.17k
) {
1389
393
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1390
1.17k
    } else {
1391
781
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1392
781
      ESym->setVisibility(Body->symbol()->Visibility);
1393
781
    }
1394
1.17k
1395
1.17k
    ESym->st_name = Ent.StrTabOffset;
1396
1.17k
1397
1.17k
    // Set a section index.
1398
1.17k
    if (const OutputSection *OutSec = Body->getOutputSection())
1399
1.01k
      ESym->st_shndx = OutSec->SectionIndex;
1400
162
    else 
if (162
isa<DefinedRegular>(Body)162
)
1401
41
      ESym->st_shndx = SHN_ABS;
1402
121
    else 
if (121
isa<DefinedCommon>(Body)121
)
1403
0
      ESym->st_shndx = SHN_COMMON;
1404
1.17k
1405
1.17k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1406
1.17k
    // for undefined symbols, so whether copying it or not is up to us if that's
1407
1.17k
    // the case. We'll leave it as zero because by not setting a value, we can
1408
1.17k
    // get the exact same outputs for two sets of input files that differ only
1409
1.17k
    // in undefined symbol size in DSOs.
1410
1.17k
    if (ESym->st_shndx != SHN_UNDEF)
1411
1.05k
      ESym->st_size = Body->getSize<ELFT>();
1412
1.17k
1413
1.17k
    // st_value is usually an address of a symbol, but that has a
1414
1.17k
    // special meaining for uninstantiated common symbols (this can
1415
1.17k
    // occur if -r is given).
1416
1.17k
    if (
!Config->DefineCommon && 1.17k
isa<DefinedCommon>(Body)46
)
1417
0
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1418
1.17k
    else
1419
1.17k
      ESym->st_value = Body->getVA();
1420
1.17k
1421
1.17k
    ++ESym;
1422
1.17k
  }
1423
288
1424
288
  // On MIPS we need to mark symbol which has a PLT entry and requires
1425
288
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1426
288
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1427
288
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1428
288
  if (
Config->EMachine == EM_MIPS288
) {
1429
22
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1430
22
1431
66
    for (SymbolTableEntry &Ent : Symbols) {
1432
66
      SymbolBody *Body = Ent.Symbol;
1433
66
      if (
Body->isInPlt() && 66
Body->NeedsPltAddr4
)
1434
0
        ESym->st_other |= STO_MIPS_PLT;
1435
66
1436
66
      if (Config->Relocatable)
1437
0
        
if (auto *0
D0
= dyn_cast<DefinedRegular>(Body))
1438
0
          
if (0
D->isMipsPIC<ELFT>()0
)
1439
0
            ESym->st_other |= STO_MIPS_PIC;
1440
66
      ++ESym;
1441
66
    }
1442
22
  }
1443
288
}
1444
1445
// .hash and .gnu.hash sections contain on-disk hash tables that map
1446
// symbol names to their dynamic symbol table indices. Their purpose
1447
// is to help the dynamic linker resolve symbols quickly. If ELF files
1448
// don't have them, the dynamic linker has to do linear search on all
1449
// dynamic symbols, which makes programs slower. Therefore, a .hash
1450
// section is added to a DSO by default. A .gnu.hash is added if you
1451
// give the -hash-style=gnu or -hash-style=both option.
1452
//
1453
// The Unix semantics of resolving dynamic symbols is somewhat expensive.
1454
// Each ELF file has a list of DSOs that the ELF file depends on and a
1455
// list of dynamic symbols that need to be resolved from any of the
1456
// DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
1457
// where m is the number of DSOs and n is the number of dynamic
1458
// symbols. For modern large programs, both m and n are large.  So
1459
// making each step faster by using hash tables substiantially
1460
// improves time to load programs.
1461
//
1462
// (Note that this is not the only way to design the shared library.
1463
// For instance, the Windows DLL takes a different approach. On
1464
// Windows, each dynamic symbol has a name of DLL from which the symbol
1465
// has to be resolved. That makes the cost of symbol resolution O(n).
1466
// This disables some hacky techniques you can use on Unix such as
1467
// LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
1468
//
1469
// Due to historical reasons, we have two different hash tables, .hash
1470
// and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
1471
// and better version of .hash. .hash is just an on-disk hash table, but
1472
// .gnu.hash has a bloom filter in addition to a hash table to skip
1473
// DSOs very quickly. If you are sure that your dynamic linker knows
1474
// about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a
1475
// safe bet is to specify -hash-style=both for backward compatibilty.
1476
GnuHashTableSection::GnuHashTableSection()
1477
5
    : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, Config->Wordsize, ".gnu.hash") {
1478
5
}
1479
1480
5
void GnuHashTableSection::finalizeContents() {
1481
5
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1482
5
1483
5
  // Computes bloom filter size in word size. We want to allocate 8
1484
5
  // bits for each symbol. It must be a power of two.
1485
5
  if (Symbols.empty())
1486
1
    MaskWords = 1;
1487
5
  else
1488
4
    MaskWords = NextPowerOf2((Symbols.size() - 1) / Config->Wordsize);
1489
5
1490
5
  Size = 16;                            // Header
1491
5
  Size += Config->Wordsize * MaskWords; // Bloom filter
1492
5
  Size += NBuckets * 4;                 // Hash buckets
1493
5
  Size += Symbols.size() * 4;           // Hash values
1494
5
}
1495
1496
5
void GnuHashTableSection::writeTo(uint8_t *Buf) {
1497
5
  // Write a header.
1498
5
  write32(Buf, NBuckets, Config->Endianness);
1499
5
  write32(Buf + 4, InX::DynSymTab->getNumSymbols() - Symbols.size(),
1500
5
          Config->Endianness);
1501
5
  write32(Buf + 8, MaskWords, Config->Endianness);
1502
5
  write32(Buf + 12, getShift2(), Config->Endianness);
1503
5
  Buf += 16;
1504
5
1505
5
  // Write a bloom filter and a hash table.
1506
5
  writeBloomFilter(Buf);
1507
5
  Buf += Config->Wordsize * MaskWords;
1508
5
  writeHashTable(Buf);
1509
5
}
1510
1511
// This function writes a 2-bit bloom filter. This bloom filter alone
1512
// usually filters out 80% or more of all symbol lookups [1].
1513
// The dynamic linker uses the hash table only when a symbol is not
1514
// filtered out by a bloom filter.
1515
//
1516
// [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2),
1517
//     p.9, https://www.akkadia.org/drepper/dsohowto.pdf
1518
5
void GnuHashTableSection::writeBloomFilter(uint8_t *Buf) {
1519
5
  const unsigned C = Config->Wordsize * 8;
1520
7
  for (const Entry &Sym : Symbols) {
1521
7
    size_t I = (Sym.Hash / C) & (MaskWords - 1);
1522
7
    uint64_t Val = readUint(Buf + I * Config->Wordsize);
1523
7
    Val |= uint64_t(1) << (Sym.Hash % C);
1524
7
    Val |= uint64_t(1) << ((Sym.Hash >> getShift2()) % C);
1525
7
    writeUint(Buf + I * Config->Wordsize, Val);
1526
7
  }
1527
5
}
1528
1529
5
void GnuHashTableSection::writeHashTable(uint8_t *Buf) {
1530
5
  // Group symbols by hash value.
1531
5
  std::vector<std::vector<Entry>> Syms(NBuckets);
1532
5
  for (const Entry &Ent : Symbols)
1533
7
    Syms[Ent.Hash % NBuckets].push_back(Ent);
1534
5
1535
5
  // Write hash buckets. Hash buckets contain indices in the following
1536
5
  // hash value table.
1537
5
  uint32_t *Buckets = reinterpret_cast<uint32_t *>(Buf);
1538
9
  for (size_t I = 0; 
I < NBuckets9
;
++I4
)
1539
4
    
if (4
!Syms[I].empty()4
)
1540
4
      write32(Buckets + I, Syms[I][0].Body->DynsymIndex, Config->Endianness);
1541
5
1542
5
  // Write a hash value table. It represents a sequence of chains that
1543
5
  // share the same hash modulo value. The last element of each chain
1544
5
  // is terminated by LSB 1.
1545
5
  uint32_t *Values = Buckets + NBuckets;
1546
5
  size_t I = 0;
1547
4
  for (std::vector<Entry> &Vec : Syms) {
1548
4
    if (Vec.empty())
1549
0
      continue;
1550
4
    for (const Entry &Ent : makeArrayRef(Vec).drop_back())
1551
3
      write32(Values + I++, Ent.Hash & ~1, Config->Endianness);
1552
4
    write32(Values + I++, Vec.back().Hash | 1, Config->Endianness);
1553
4
  }
1554
5
}
1555
1556
7
static uint32_t hashGnu(StringRef Name) {
1557
7
  uint32_t H = 5381;
1558
7
  for (uint8_t C : Name)
1559
24
    H = (H << 5) + H + C;
1560
7
  return H;
1561
7
}
1562
1563
// Returns a number of hash buckets to accomodate given number of elements.
1564
// We want to choose a moderate number that is not too small (which
1565
// causes too many hash collisions) and not too large (which wastes
1566
// disk space.)
1567
//
1568
// We return a prime number because it (is believed to) achieve good
1569
// hash distribution.
1570
4
static size_t getBucketSize(size_t NumSymbols) {
1571
4
  // List of largest prime numbers that are not greater than 2^n + 1.
1572
4
  for (size_t N : {131071, 65521, 32749, 16381, 8191, 4093, 2039, 1021, 509,
1573
4
                   251, 127, 61, 31, 13, 7, 3, 1})
1574
68
    
if (68
N <= NumSymbols68
)
1575
4
      return N;
1576
0
  return 0;
1577
0
}
1578
1579
// Add symbols to this symbol hash table. Note that this function
1580
// destructively sort a given vector -- which is needed because
1581
// GNU-style hash table places some sorting requirements.
1582
5
void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &V) {
1583
5
  // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
1584
5
  // its type correctly.
1585
5
  std::vector<SymbolTableEntry>::iterator Mid =
1586
11
      std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1587
11
        return S.Symbol->isUndefined();
1588
11
      });
1589
5
  if (Mid == V.end())
1590
1
    return;
1591
4
1592
4
  
for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) 4
{
1593
7
    SymbolBody *B = Ent.Symbol;
1594
7
    Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())});
1595
7
  }
1596
4
1597
4
  NBuckets = getBucketSize(Symbols.size());
1598
4
  std::stable_sort(Symbols.begin(), Symbols.end(),
1599
3
                   [&](const Entry &L, const Entry &R) {
1600
3
                     return L.Hash % NBuckets < R.Hash % NBuckets;
1601
3
                   });
1602
4
1603
4
  V.erase(Mid, V.end());
1604
4
  for (const Entry &Ent : Symbols)
1605
7
    V.push_back({Ent.Body, Ent.StrTabOffset});
1606
5
}
1607
1608
template <class ELFT>
1609
HashTableSection<ELFT>::HashTableSection()
1610
798
    : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1611
798
  this->Entsize = 4;
1612
798
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::HashTableSection()
Line
Count
Source
1610
54
    : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1611
54
  this->Entsize = 4;
1612
54
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::HashTableSection()
Line
Count
Source
1610
615
    : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1611
615
  this->Entsize = 4;
1612
615
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::HashTableSection()
Line
Count
Source
1610
25
    : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1611
25
  this->Entsize = 4;
1612
25
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::HashTableSection()
Line
Count
Source
1610
104
    : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1611
104
  this->Entsize = 4;
1612
104
}
1613
1614
758
template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
1615
758
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1616
758
1617
758
  unsigned NumEntries = 2;                       // nbucket and nchain.
1618
758
  NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1619
758
1620
758
  // Create as many buckets as there are symbols.
1621
758
  // FIXME: This is simplistic. We can try to optimize it, but implementing
1622
758
  // support for SHT_GNU_HASH is probably even more profitable.
1623
758
  NumEntries += InX::DynSymTab->getNumSymbols();
1624
758
  this->Size = NumEntries * 4;
1625
758
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1614
579
template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
1615
579
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1616
579
1617
579
  unsigned NumEntries = 2;                       // nbucket and nchain.
1618
579
  NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1619
579
1620
579
  // Create as many buckets as there are symbols.
1621
579
  // FIXME: This is simplistic. We can try to optimize it, but implementing
1622
579
  // support for SHT_GNU_HASH is probably even more profitable.
1623
579
  NumEntries += InX::DynSymTab->getNumSymbols();
1624
579
  this->Size = NumEntries * 4;
1625
579
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1614
54
template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
1615
54
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1616
54
1617
54
  unsigned NumEntries = 2;                       // nbucket and nchain.
1618
54
  NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1619
54
1620
54
  // Create as many buckets as there are symbols.
1621
54
  // FIXME: This is simplistic. We can try to optimize it, but implementing
1622
54
  // support for SHT_GNU_HASH is probably even more profitable.
1623
54
  NumEntries += InX::DynSymTab->getNumSymbols();
1624
54
  this->Size = NumEntries * 4;
1625
54
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1614
100
template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
1615
100
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1616
100
1617
100
  unsigned NumEntries = 2;                       // nbucket and nchain.
1618
100
  NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1619
100
1620
100
  // Create as many buckets as there are symbols.
1621
100
  // FIXME: This is simplistic. We can try to optimize it, but implementing
1622
100
  // support for SHT_GNU_HASH is probably even more profitable.
1623
100
  NumEntries += InX::DynSymTab->getNumSymbols();
1624
100
  this->Size = NumEntries * 4;
1625
100
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1614
25
template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
1615
25
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1616
25
1617
25
  unsigned NumEntries = 2;                       // nbucket and nchain.
1618
25
  NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1619
25
1620
25
  // Create as many buckets as there are symbols.
1621
25
  // FIXME: This is simplistic. We can try to optimize it, but implementing
1622
25
  // support for SHT_GNU_HASH is probably even more profitable.
1623
25
  NumEntries += InX::DynSymTab->getNumSymbols();
1624
25
  this->Size = NumEntries * 4;
1625
25
}
1626
1627
756
template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1628
756
  // A 32-bit integer type in the target endianness.
1629
756
  typedef typename ELFT::Word Elf_Word;
1630
756
1631
756
  unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1632
756
1633
756
  auto *P = reinterpret_cast<Elf_Word *>(Buf);
1634
756
  *P++ = NumSymbols; // nbucket
1635
756
  *P++ = NumSymbols; // nchain
1636
756
1637
756
  Elf_Word *Buckets = P;
1638
756
  Elf_Word *Chains = P + NumSymbols;
1639
756
1640
1.24k
  for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1641
1.24k
    SymbolBody *Body = S.Symbol;
1642
1.24k
    StringRef Name = Body->getName();
1643
1.24k
    unsigned I = Body->DynsymIndex;
1644
1.24k
    uint32_t Hash = hashSysV(Name) % NumSymbols;
1645
1.24k
    Chains[I] = Buckets[Hash];
1646
1.24k
    Buckets[Hash] = I;
1647
1.24k
  }
1648
756
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1627
100
template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1628
100
  // A 32-bit integer type in the target endianness.
1629
100
  typedef typename ELFT::Word Elf_Word;
1630
100
1631
100
  unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1632
100
1633
100
  auto *P = reinterpret_cast<Elf_Word *>(Buf);
1634
100
  *P++ = NumSymbols; // nbucket
1635
100
  *P++ = NumSymbols; // nchain
1636
100
1637
100
  Elf_Word *Buckets = P;
1638
100
  Elf_Word *Chains = P + NumSymbols;
1639
100
1640
187
  for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1641
187
    SymbolBody *Body = S.Symbol;
1642
187
    StringRef Name = Body->getName();
1643
187
    unsigned I = Body->DynsymIndex;
1644
187
    uint32_t Hash = hashSysV(Name) % NumSymbols;
1645
187
    Chains[I] = Buckets[Hash];
1646
187
    Buckets[Hash] = I;
1647
187
  }
1648
100
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1627
54
template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1628
54
  // A 32-bit integer type in the target endianness.
1629
54
  typedef typename ELFT::Word Elf_Word;
1630
54
1631
54
  unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1632
54
1633
54
  auto *P = reinterpret_cast<Elf_Word *>(Buf);
1634
54
  *P++ = NumSymbols; // nbucket
1635
54
  *P++ = NumSymbols; // nchain
1636
54
1637
54
  Elf_Word *Buckets = P;
1638
54
  Elf_Word *Chains = P + NumSymbols;
1639
54
1640
123
  for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1641
123
    SymbolBody *Body = S.Symbol;
1642
123
    StringRef Name = Body->getName();
1643
123
    unsigned I = Body->DynsymIndex;
1644
123
    uint32_t Hash = hashSysV(Name) % NumSymbols;
1645
123
    Chains[I] = Buckets[Hash];
1646
123
    Buckets[Hash] = I;
1647
123
  }
1648
54
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1627
25
template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1628
25
  // A 32-bit integer type in the target endianness.
1629
25
  typedef typename ELFT::Word Elf_Word;
1630
25
1631
25
  unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1632
25
1633
25
  auto *P = reinterpret_cast<Elf_Word *>(Buf);
1634
25
  *P++ = NumSymbols; // nbucket
1635
25
  *P++ = NumSymbols; // nchain
1636
25
1637
25
  Elf_Word *Buckets = P;
1638
25
  Elf_Word *Chains = P + NumSymbols;
1639
25
1640
47
  for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1641
47
    SymbolBody *Body = S.Symbol;
1642
47
    StringRef Name = Body->getName();
1643
47
    unsigned I = Body->DynsymIndex;
1644
47
    uint32_t Hash = hashSysV(Name) % NumSymbols;
1645
47
    Chains[I] = Buckets[Hash];
1646
47
    Buckets[Hash] = I;
1647
47
  }
1648
25
}
lld::elf::HashTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1627
577
template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1628
577
  // A 32-bit integer type in the target endianness.
1629
577
  typedef typename ELFT::Word Elf_Word;
1630
577
1631
577
  unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1632
577
1633
577
  auto *P = reinterpret_cast<Elf_Word *>(Buf);
1634
577
  *P++ = NumSymbols; // nbucket
1635
577
  *P++ = NumSymbols; // nchain
1636
577
1637
577
  Elf_Word *Buckets = P;
1638
577
  Elf_Word *Chains = P + NumSymbols;
1639
577
1640
883
  for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1641
883
    SymbolBody *Body = S.Symbol;
1642
883
    StringRef Name = Body->getName();
1643
883
    unsigned I = Body->DynsymIndex;
1644
883
    uint32_t Hash = hashSysV(Name) % NumSymbols;
1645
883
    Chains[I] = Buckets[Hash];
1646
883
    Buckets[Hash] = I;
1647
883
  }
1648
577
}
1649
1650
PltSection::PltSection(size_t S)
1651
    : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"),
1652
3.17k
      HeaderSize(S) {
1653
3.17k
  // The PLT needs to be writable on SPARC as the dynamic linker will
1654
3.17k
  // modify the instructions in the PLT entries.
1655
3.17k
  if (Config->EMachine == EM_SPARCV9)
1656
2
    this->Flags |= SHF_WRITE;
1657
3.17k
}
1658
1659
147
void PltSection::writeTo(uint8_t *Buf) {
1660
147
  // At beginning of PLT but not the IPLT, we have code to call the dynamic
1661
147
  // linker to resolve dynsyms at runtime. Write such code.
1662
147
  if (HeaderSize != 0)
1663
130
    Target->writePltHeader(Buf);
1664
147
  size_t Off = HeaderSize;
1665
147
  // The IPlt is immediately after the Plt, account for this in RelOff
1666
147
  unsigned PltOff = getPltRelocOff();
1667
147
1668
214
  for (auto &I : Entries) {
1669
214
    const SymbolBody *B = I.first;
1670
214
    unsigned RelOff = I.second + PltOff;
1671
214
    uint64_t Got = B->getGotPltVA();
1672
214
    uint64_t Plt = this->getVA() + Off;
1673
214
    Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1674
214
    Off += Target->PltEntrySize;
1675
214
  }
1676
147
}
1677
1678
214
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1679
214
  Sym.PltIndex = Entries.size();
1680
214
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1681
214
  if (
HeaderSize == 0214
) {
1682
29
    PltRelocSection = In<ELFT>::RelaIplt;
1683
29
    Sym.IsInIplt = true;
1684
29
  }
1685
214
  unsigned RelOff = PltRelocSection->getRelocOffset();
1686
214
  Entries.push_back(std::make_pair(&Sym, RelOff));
1687
214
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SymbolBody&)
Line
Count
Source
1678
150
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1679
150
  Sym.PltIndex = Entries.size();
1680
150
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1681
150
  if (
HeaderSize == 0150
) {
1682
15
    PltRelocSection = In<ELFT>::RelaIplt;
1683
15
    Sym.IsInIplt = true;
1684
15
  }
1685
150
  unsigned RelOff = PltRelocSection->getRelocOffset();
1686
150
  Entries.push_back(std::make_pair(&Sym, RelOff));
1687
150
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SymbolBody&)
Line
Count
Source
1678
54
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1679
54
  Sym.PltIndex = Entries.size();
1680
54
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1681
54
  if (
HeaderSize == 054
) {
1682
12
    PltRelocSection = In<ELFT>::RelaIplt;
1683
12
    Sym.IsInIplt = true;
1684
12
  }
1685
54
  unsigned RelOff = PltRelocSection->getRelocOffset();
1686
54
  Entries.push_back(std::make_pair(&Sym, RelOff));
1687
54
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SymbolBody&)
Line
Count
Source
1678
8
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1679
8
  Sym.PltIndex = Entries.size();
1680
8
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1681
8
  if (
HeaderSize == 08
) {
1682
0
    PltRelocSection = In<ELFT>::RelaIplt;
1683
0
    Sym.IsInIplt = true;
1684
0
  }
1685
8
  unsigned RelOff = PltRelocSection->getRelocOffset();
1686
8
  Entries.push_back(std::make_pair(&Sym, RelOff));
1687
8
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SymbolBody&)
Line
Count
Source
1678
2
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1679
2
  Sym.PltIndex = Entries.size();
1680
2
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1681
2
  if (
HeaderSize == 02
) {
1682
2
    PltRelocSection = In<ELFT>::RelaIplt;
1683
2
    Sym.IsInIplt = true;
1684
2
  }
1685
2
  unsigned RelOff = PltRelocSection->getRelocOffset();
1686
2
  Entries.push_back(std::make_pair(&Sym, RelOff));
1687
2
}
1688
1689
3.67k
size_t PltSection::getSize() const {
1690
3.67k
  return HeaderSize + Entries.size() * Target->PltEntrySize;
1691
3.67k
}
1692
1693
// Some architectures such as additional symbols in the PLT section. For
1694
// example ARM uses mapping symbols to aid disassembly
1695
147
void PltSection::addSymbols() {
1696
147
  // The PLT may have symbols defined for the Header, the IPLT has no header
1697
147
  if (HeaderSize != 0)
1698
130
    Target->addPltHeaderSymbols(this);
1699
147
  size_t Off = HeaderSize;
1700
361
  for (size_t I = 0; 
I < Entries.size()361
;
++I214
) {
1701
214
    Target->addPltSymbols(this, Off);
1702
214
    Off += Target->PltEntrySize;
1703
214
  }
1704
147
}
1705
1706
147
unsigned PltSection::getPltRelocOff() const {
1707
147
  return (HeaderSize == 0) ? 
InX::Plt->getSize()17
:
0130
;
1708
147
}
1709
1710
// The hash function used for .gdb_index version 5 or above.
1711
6
static uint32_t gdbHash(StringRef Str) {
1712
6
  uint32_t R = 0;
1713
6
  for (uint8_t C : Str)
1714
21
    R = R * 67 + tolower(C) - 113;
1715
6
  return R;
1716
6
}
1717
1718
9
static std::vector<CompilationUnitEntry> readCuList(DWARFContext &Dwarf) {
1719
9
  std::vector<CompilationUnitEntry> Ret;
1720
9
  for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units())
1721
7
    Ret.push_back({CU->getOffset(), CU->getLength() + 4});
1722
9
  return Ret;
1723
9
}
1724
1725
static std::vector<AddressEntry> readAddressArea(DWARFContext &Dwarf,
1726
8
                                                 InputSection *Sec) {
1727
8
  std::vector<AddressEntry> Ret;
1728
8
1729
8
  uint32_t CurrentCu = 0;
1730
8
  for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) {
1731
8
    DWARFAddressRangesVector Ranges;
1732
8
    CU->collectAddressRanges(Ranges);
1733
8
1734
8
    ArrayRef<InputSectionBase *> Sections = Sec->File->getSections();
1735
9
    for (DWARFAddressRange &R : Ranges) {
1736
9
      InputSectionBase *S = Sections[R.SectionIndex];
1737
9
      if (
!S || 9
S == &InputSection::Discarded9
||
!S->Live9
)
1738
1
        continue;
1739
8
      // Range list with zero size has no effect.
1740
8
      
if (8
R.LowPC == R.HighPC8
)
1741
1
        continue;
1742
7
      auto *IS = cast<InputSection>(S);
1743
7
      uint64_t Offset = IS->getOffsetInFile();
1744
7
      Ret.push_back({IS, R.LowPC - Offset, R.HighPC - Offset, CurrentCu});
1745
7
    }
1746
8
    ++CurrentCu;
1747
8
  }
1748
8
  return Ret;
1749
8
}
1750
1751
static std::vector<NameTypeEntry> readPubNamesAndTypes(DWARFContext &Dwarf,
1752
9
                                                       bool IsLE) {
1753
9
  StringRef Data[] = {Dwarf.getDWARFObj().getGnuPubNamesSection(),
1754
9
                      Dwarf.getDWARFObj().getGnuPubTypesSection()};
1755
9
1756
9
  std::vector<NameTypeEntry> Ret;
1757
18
  for (StringRef D : Data) {
1758
18
    DWARFDebugPubTable PubTable(D, IsLE, true);
1759
18
    for (const DWARFDebugPubTable::Set &Set : PubTable.getData())
1760
5
      for (const DWARFDebugPubTable::Entry &Ent : Set.Entries)
1761
6
        Ret.push_back({Ent.Name, Ent.Descriptor.toBits()});
1762
18
  }
1763
9
  return Ret;
1764
9
}
1765
1766
9
static std::vector<InputSection *> getDebugInfoSections() {
1767
9
  std::vector<InputSection *> Ret;
1768
9
  for (InputSectionBase *S : InputSections)
1769
111
    
if (InputSection *111
IS111
= dyn_cast<InputSection>(S))
1770
110
      
if (110
IS->Name == ".debug_info"110
)
1771
9
        Ret.push_back(IS);
1772
9
  return Ret;
1773
9
}
1774
1775
9
void GdbIndexSection::buildIndex() {
1776
9
  if (Chunks.empty())
1777
1
    return;
1778
8
1779
8
  uint32_t CuId = 0;
1780
9
  for (GdbIndexChunk &D : Chunks) {
1781
9
    for (AddressEntry &E : D.AddressArea)
1782
7
      E.CuIndex += CuId;
1783
9
1784
9
    // Populate constant pool area.
1785
6
    for (NameTypeEntry &NameType : D.NamesAndTypes) {
1786
6
      uint32_t Hash = gdbHash(NameType.Name);
1787
6
      size_t Offset = StringPool.add(NameType.Name);
1788
6
1789
6
      bool IsNew;
1790
6
      GdbSymbol *Sym;
1791
6
      std::tie(IsNew, Sym) = HashTab.add(Hash, Offset);
1792
6
      if (
IsNew6
) {
1793
4
        Sym->CuVectorIndex = CuVectors.size();
1794
4
        CuVectors.resize(CuVectors.size() + 1);
1795
4
      }
1796
6
1797
6
      CuVectors[Sym->CuVectorIndex].insert(CuId | (NameType.Type << 24));
1798
6
    }
1799
9
1800
9
    CuId += D.CompilationUnits.size();
1801
9
  }
1802
9
}
1803
1804
9
static GdbIndexChunk readDwarf(DWARFContext &Dwarf, InputSection *Sec) {
1805
9
  GdbIndexChunk Ret;
1806
9
  Ret.DebugInfoSec = Sec;
1807
9
  Ret.CompilationUnits = readCuList(Dwarf);
1808
9
  Ret.AddressArea = readAddressArea(Dwarf, Sec);
1809
9
  Ret.NamesAndTypes = readPubNamesAndTypes(Dwarf, Config->IsLE);
1810
9
  return Ret;
1811
9
}
1812
1813
9
template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
1814
9
  std::vector<InputSection *> Sections = getDebugInfoSections();
1815
9
  std::vector<GdbIndexChunk> Chunks(Sections.size());
1816
9
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1817
9
    ObjFile<ELFT> *F = Sections[I]->getFile<ELFT>();
1818
9
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(F));
1819
9
    Chunks[I] = readDwarf(Dwarf, Sections[I]);
1820
9
  });
lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
1816
1
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1817
1
    ObjFile<ELFT> *F = Sections[I]->getFile<ELFT>();
1818
1
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(F));
1819
1
    Chunks[I] = readDwarf(Dwarf, Sections[I]);
1820
1
  });
lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(unsigned long)::operator()(unsigned long) const
Line
Count
Source
1816
8
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1817
8
    ObjFile<ELFT> *F = Sections[I]->getFile<ELFT>();
1818
8
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(F));
1819
8
    Chunks[I] = readDwarf(Dwarf, Sections[I]);
1820
8
  });
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(unsigned long)::operator()(unsigned long) const
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(unsigned long)::operator()(unsigned long) const
1821
9
  return make<GdbIndexSection>(std::move(Chunks));
1822
9
}
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
1813
1
template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
1814
1
  std::vector<InputSection *> Sections = getDebugInfoSections();
1815
1
  std::vector<GdbIndexChunk> Chunks(Sections.size());
1816
1
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1817
1
    ObjFile<ELFT> *F = Sections[I]->getFile<ELFT>();
1818
1
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(F));
1819
1
    Chunks[I] = readDwarf(Dwarf, Sections[I]);
1820
1
  });
1821
1
  return make<GdbIndexSection>(std::move(Chunks));
1822
1
}
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1813
8
template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
1814
8
  std::vector<InputSection *> Sections = getDebugInfoSections();
1815
8
  std::vector<GdbIndexChunk> Chunks(Sections.size());
1816
8
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1817
8
    ObjFile<ELFT> *F = Sections[I]->getFile<ELFT>();
1818
8
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(F));
1819
8
    Chunks[I] = readDwarf(Dwarf, Sections[I]);
1820
8
  });
1821
8
  return make<GdbIndexSection>(std::move(Chunks));
1822
8
}
1823
1824
9
static size_t getCuSize(ArrayRef<GdbIndexChunk> Arr) {
1825
9
  size_t Ret = 0;
1826
9
  for (const GdbIndexChunk &D : Arr)
1827
9
    Ret += D.CompilationUnits.size();
1828
9
  return Ret;
1829
9
}
1830
1831
9
static size_t getAddressAreaSize(ArrayRef<GdbIndexChunk> Arr) {
1832
9
  size_t Ret = 0;
1833
9
  for (const GdbIndexChunk &D : Arr)
1834
9
    Ret += D.AddressArea.size();
1835
9
  return Ret;
1836
9
}
1837
1838
GdbIndexSection::GdbIndexSection(std::vector<GdbIndexChunk> &&C)
1839
    : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"),
1840
9
      StringPool(llvm::StringTableBuilder::ELF), Chunks(std::move(C)) {
1841
9
  buildIndex();
1842
9
  HashTab.finalizeContents();
1843
9
1844
9
  // GdbIndex header consist from version fields
1845
9
  // and 5 more fields with different kinds of offsets.
1846
9
  CuTypesOffset = CuListOffset + getCuSize(Chunks) * CompilationUnitSize;
1847
9
  SymTabOffset = CuTypesOffset + getAddressAreaSize(Chunks) * AddressEntrySize;
1848
9
  ConstantPoolOffset = SymTabOffset + HashTab.getCapacity() * SymTabEntrySize;
1849
9
1850
4
  for (std::set<uint32_t> &CuVec : CuVectors) {
1851
4
    CuVectorsOffset.push_back(CuVectorsSize);
1852
4
    CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1);
1853
4
  }
1854
9
  StringPoolOffset = ConstantPoolOffset + CuVectorsSize;
1855
9
  StringPool.finalizeInOrder();
1856
9
}
1857
1858
25
size_t GdbIndexSection::getSize() const {
1859
25
  return StringPoolOffset + StringPool.getSize();
1860
25
}
1861
1862
8
void GdbIndexSection::writeTo(uint8_t *Buf) {
1863
8
  write32le(Buf, 7);                       // Write version
1864
8
  write32le(Buf + 4, CuListOffset);        // CU list offset
1865
8
  write32le(Buf + 8, CuTypesOffset);       // Types CU list offset
1866
8
  write32le(Buf + 12, CuTypesOffset);      // Address area offset
1867
8
  write32le(Buf + 16, SymTabOffset);       // Symbol table offset
1868
8
  write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset
1869
8
  Buf += 24;
1870
8
1871
8
  // Write the CU list.
1872
9
  for (GdbIndexChunk &D : Chunks) {
1873
8
    for (CompilationUnitEntry &Cu : D.CompilationUnits) {
1874
8
      write64le(Buf, D.DebugInfoSec->OutSecOff + Cu.CuOffset);
1875
8
      write64le(Buf + 8, Cu.CuLength);
1876
8
      Buf += 16;
1877
8
    }
1878
9
  }
1879
8
1880
8
  // Write the address area.
1881
9
  for (GdbIndexChunk &D : Chunks) {
1882
7
    for (AddressEntry &E : D.AddressArea) {
1883
7
      uint64_t BaseAddr =
1884
7
          E.Section->getParent()->Addr + E.Section->getOffset(0);
1885
7
      write64le(Buf, BaseAddr + E.LowAddress);
1886
7
      write64le(Buf + 8, BaseAddr + E.HighAddress);
1887
7
      write32le(Buf + 16, E.CuIndex);
1888
7
      Buf += 20;
1889
7
    }
1890
9
  }
1891
8
1892
8
  // Write the symbol table.
1893
8.20k
  for (size_t I = 0; 
I < HashTab.getCapacity()8.20k
;
++I8.19k
) {
1894
8.19k
    GdbSymbol *Sym = HashTab.getSymbol(I);
1895
8.19k
    if (
Sym8.19k
) {
1896
4
      size_t NameOffset =
1897
4
          Sym->NameOffset + StringPoolOffset - ConstantPoolOffset;
1898
4
      size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex];
1899
4
      write32le(Buf, NameOffset);
1900
4
      write32le(Buf + 4, CuVectorOffset);
1901
4
    }
1902
8.19k
    Buf += 8;
1903
8.19k
  }
1904
8
1905
8
  // Write the CU vectors into the constant pool.
1906
4
  for (std::set<uint32_t> &CuVec : CuVectors) {
1907
4
    write32le(Buf, CuVec.size());
1908
4
    Buf += 4;
1909
5
    for (uint32_t Val : CuVec) {
1910
5
      write32le(Buf, Val);
1911
5
      Buf += 4;
1912
5
    }
1913
4
  }
1914
8
1915
8
  StringPool.write(Buf);
1916
8
}
1917
1918
17
bool GdbIndexSection::empty() const { return !Out::DebugInfo; }
1919
1920
template <class ELFT>
1921
EhFrameHeader<ELFT>::EhFrameHeader()
1922
16
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::EhFrameHeader()
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::EhFrameHeader()
Line
Count
Source
1922
3
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::EhFrameHeader()
Line
Count
Source
1922
1
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Eh