Coverage Report

Created: 2017-10-03 07:32

/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.59k
std::vector<InputSection *> elf::createCommonSections() {
58
1.59k
  if (!Config->DefineCommon)
59
56
    return {};
60
1.54k
61
1.54k
  std::vector<InputSection *> Ret;
62
3.48k
  for (Symbol *S : Symtab->getSymbols()) {
63
3.48k
    auto *Sym = dyn_cast<DefinedCommon>(S->body());
64
3.48k
    if (
!Sym || 3.48k
!Sym->Live86
)
65
3.40k
      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.59k
  return Ret;
75
1.59k
}
76
77
// Returns an LLD version string.
78
1.53k
static ArrayRef<uint8_t> getVersion() {
79
1.53k
  // Check LLD_VERSION first for ease of testing.
80
1.53k
  // You can get consitent output by using the environment variable.
81
1.53k
  // This is only for testing.
82
1.53k
  StringRef S = getenv("LLD_VERSION");
83
1.53k
  if (S.empty())
84
0
    S = Saver.save(Twine("Linker: ") + getLLDVersion());
85
1.53k
86
1.53k
  // +1 to include the terminating '\0'.
87
1.53k
  return {(const uint8_t *)S.data(), S.size() + 1};
88
1.53k
}
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.53k
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
1.53k
  typename ELFT::Shdr Hdr = {};
96
1.53k
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
1.53k
  Hdr.sh_type = SHT_PROGBITS;
98
1.53k
  Hdr.sh_entsize = 1;
99
1.53k
  Hdr.sh_addralign = 1;
100
1.53k
101
1.53k
  auto *Ret =
102
1.53k
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
1.53k
  Ret->Data = getVersion();
104
1.53k
  return Ret;
105
1.53k
}
lld::elf::MergeInputSection* lld::elf::createCommentSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
94
1.22k
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
1.22k
  typename ELFT::Shdr Hdr = {};
96
1.22k
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
1.22k
  Hdr.sh_type = SHT_PROGBITS;
98
1.22k
  Hdr.sh_entsize = 1;
99
1.22k
  Hdr.sh_addralign = 1;
100
1.22k
101
1.22k
  auto *Ret =
102
1.22k
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
1.22k
  Ret->Data = getVersion();
104
1.22k
  return Ret;
105
1.22k
}
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)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
}
lld::elf::MergeInputSection* lld::elf::createCommentSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
94
187
template <class ELFT> MergeInputSection *elf::createCommentSection() {
95
187
  typename ELFT::Shdr Hdr = {};
96
187
  Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
97
187
  Hdr.sh_type = SHT_PROGBITS;
98
187
  Hdr.sh_entsize = 1;
99
187
  Hdr.sh_addralign = 1;
100
187
101
187
  auto *Ret =
102
187
      make<MergeInputSection>((ObjFile<ELFT> *)nullptr, &Hdr, ".comment");
103
187
  Ret->Data = getVersion();
104
187
  return Ret;
105
187
}
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, 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
}
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, 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
}
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, 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
}
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
}
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)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, 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)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)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)0, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, false> >)
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
}
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> >)
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)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
}
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*)
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)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, 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)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
}
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
}
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
}
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> >)
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
}
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, 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
}
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
}
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)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)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, 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
170
                                   uint64_t Size, InputSectionBase *Section) {
289
170
  auto *S = make<DefinedRegular>(Name, /*IsLocal*/ true, STV_DEFAULT, Type,
290
170
                                 Value, Size, Section);
291
170
  if (InX::SymTab)
292
170
    InX::SymTab->addSymbol(S);
293
170
  return S;
294
170
}
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.30k
    : 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.53k
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::EhFrameSection()
Line
Count
Source
399
187
    : 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)1, true> >::EhFrameSection()
Line
Count
Source
399
1.22k
    : 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") {}
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
89
                                        ArrayRef<RelTy> Rels) {
408
89
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
89
  const endianness E = ELFT::TargetEndianness;
410
89
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
89
413
89
  SymbolBody *Personality = nullptr;
414
89
  unsigned FirstRelI = Cie.FirstRelocation;
415
89
  if (FirstRelI != (unsigned)-1)
416
9
    Personality =
417
9
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
89
419
89
  // Search for an existing CIE by CIE contents/relocation target pair.
420
89
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
89
422
89
  // If not found, create a new one.
423
89
  if (
!Rec89
) {
424
78
    Rec = make<CieRecord>();
425
78
    Rec->Cie = &Cie;
426
78
    CieRecords.push_back(Rec);
427
78
  }
428
89
  return Rec;
429
89
}
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
}
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> >)
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)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)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
81
                                        ArrayRef<RelTy> Rels) {
408
81
  auto *Sec = cast<EhInputSection>(Cie.Sec);
409
81
  const endianness E = ELFT::TargetEndianness;
410
81
  if (read32<E>(Cie.data().data() + 4) != 0)
411
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
412
81
413
81
  SymbolBody *Personality = nullptr;
414
81
  unsigned FirstRelI = Cie.FirstRelocation;
415
81
  if (FirstRelI != (unsigned)-1)
416
7
    Personality =
417
7
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
418
81
419
81
  // Search for an existing CIE by CIE contents/relocation target pair.
420
81
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
421
81
422
81
  // If not found, create a new one.
423
81
  if (
!Rec81
) {
424
71
    Rec = make<CieRecord>();
425
71
    Rec->Cie = &Cie;
426
71
    CieRecords.push_back(Rec);
427
71
  }
428
81
  return Rec;
429
81
}
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> >)
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
116
                                     ArrayRef<RelTy> Rels) {
437
116
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
116
  unsigned FirstRelI = Fde.FirstRelocation;
439
116
440
116
  // An FDE should point to some function because FDEs are to describe
441
116
  // functions. That's however not always the case due to an issue of
442
116
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
116
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
116
  // To deal with that, we ignore such FDEs.
445
116
  if (FirstRelI == (unsigned)-1)
446
1
    return false;
447
115
448
115
  const RelTy &Rel = Rels[FirstRelI];
449
115
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
115
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
114
    
if (114
D->Section114
)
452
114
      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
109
                                     ArrayRef<RelTy> Rels) {
437
109
  auto *Sec = cast<EhInputSection>(Fde.Sec);
438
109
  unsigned FirstRelI = Fde.FirstRelocation;
439
109
440
109
  // An FDE should point to some function because FDEs are to describe
441
109
  // functions. That's however not always the case due to an issue of
442
109
  // ld.gold with -r. ld.gold may discard only functions and leave their
443
109
  // corresponding FDEs, which results in creating bad .eh_frame sections.
444
109
  // To deal with that, we ignore such FDEs.
445
109
  if (FirstRelI == (unsigned)-1)
446
1
    return false;
447
108
448
108
  const RelTy &Rel = Rels[FirstRelI];
449
108
  SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
450
108
  if (auto *D = dyn_cast<DefinedRegular>(&B))
451
107
    
if (107
D->Section107
)
452
107
      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)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, 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> >)
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)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
}
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
}
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)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
}
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
92
                                         ArrayRef<RelTy> Rels) {
464
92
  const endianness E = ELFT::TargetEndianness;
465
92
466
92
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
210
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
210
    // The empty record is the end marker.
469
210
    if (Piece.Size == 4)
470
5
      return;
471
205
472
205
    size_t Offset = Piece.InputOff;
473
205
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
205
    if (
ID == 0205
) {
475
89
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
89
      continue;
477
89
    }
478
116
479
116
    uint32_t CieOffset = Offset + 4 - ID;
480
116
    CieRecord *Rec = OffsetToCie[CieOffset];
481
116
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
116
484
116
    
if (116
!isFdeLive(Piece, Rels)116
)
485
7
      continue;
486
109
    Rec->Fdes.push_back(&Piece);
487
109
    NumFdes++;
488
109
  }
489
92
}
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> >)
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
}
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)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> >)
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
}
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
84
                                         ArrayRef<RelTy> Rels) {
464
84
  const endianness E = ELFT::TargetEndianness;
465
84
466
84
  DenseMap<size_t, CieRecord *> OffsetToCie;
467
195
  for (EhSectionPiece &Piece : Sec->Pieces) {
468
195
    // The empty record is the end marker.
469
195
    if (Piece.Size == 4)
470
5
      return;
471
190
472
190
    size_t Offset = Piece.InputOff;
473
190
    uint32_t ID = read32<E>(Piece.data().data() + 4);
474
190
    if (
ID == 0190
) {
475
81
      OffsetToCie[Offset] = addCie(Piece, Rels);
476
81
      continue;
477
81
    }
478
109
479
109
    uint32_t CieOffset = Offset + 4 - ID;
480
109
    CieRecord *Rec = OffsetToCie[CieOffset];
481
109
    if (!Rec)
482
0
      fatal(toString(Sec) + ": invalid CIE reference");
483
109
484
109
    
if (109
!isFdeLive(Piece, Rels)109
)
485
7
      continue;
486
102
    Rec->Fdes.push_back(&Piece);
487
102
    NumFdes++;
488
102
  }
489
84
}
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
}
490
491
template <class ELFT>
492
95
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
95
  auto *Sec = cast<EhInputSection>(C);
494
95
  Sec->Parent = this;
495
95
  updateAlignment(Sec->Alignment);
496
95
  Sections.push_back(Sec);
497
95
  for (auto *DS : Sec->DependentSections)
498
1
    DependentSections.push_back(DS);
499
95
500
95
  // .eh_frame is a sequence of CIE or FDE records. This function
501
95
  // splits it into pieces so that we can call
502
95
  // SplitInputSection::getSectionPiece on the section.
503
95
  Sec->split<ELFT>();
504
95
  if (Sec->Pieces.empty())
505
3
    return;
506
92
507
92
  
if (92
Sec->NumRelocations == 092
)
508
7
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
85
  else 
if (85
Sec->AreRelocsRela85
)
510
80
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
85
  else
512
5
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
95
}
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
}
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
87
void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
493
87
  auto *Sec = cast<EhInputSection>(C);
494
87
  Sec->Parent = this;
495
87
  updateAlignment(Sec->Alignment);
496
87
  Sections.push_back(Sec);
497
87
  for (auto *DS : Sec->DependentSections)
498
1
    DependentSections.push_back(DS);
499
87
500
87
  // .eh_frame is a sequence of CIE or FDE records. This function
501
87
  // splits it into pieces so that we can call
502
87
  // SplitInputSection::getSectionPiece on the section.
503
87
  Sec->split<ELFT>();
504
87
  if (Sec->Pieces.empty())
505
3
    return;
506
84
507
84
  
if (84
Sec->NumRelocations == 084
)
508
6
    addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
509
78
  else 
if (78
Sec->AreRelocsRela78
)
510
78
    addSectionAux(Sec, Sec->template relas<ELFT>());
511
78
  else
512
0
    addSectionAux(Sec, Sec->template rels<ELFT>());
513
87
}
514
515
template <class ELFT>
516
179
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
179
  memcpy(Buf, D.data(), D.size());
518
179
519
179
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
179
521
179
  // Zero-clear trailing padding if it exists.
522
179
  memset(Buf + D.size(), 0, Aligned - D.size());
523
179
524
179
  // Fix the size field. -4 since size does not include the size field itself.
525
179
  const endianness E = ELFT::TargetEndianness;
526
179
  write32<E>(Buf, Aligned - 4);
527
179
}
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, 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
165
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
517
165
  memcpy(Buf, D.data(), D.size());
518
165
519
165
  size_t Aligned = alignTo(D.size(), sizeof(typename ELFT::uint));
520
165
521
165
  // Zero-clear trailing padding if it exists.
522
165
  memset(Buf + D.size(), 0, Aligned - D.size());
523
165
524
165
  // Fix the size field. -4 since size does not include the size field itself.
525
165
  const endianness E = ELFT::TargetEndianness;
526
165
  write32<E>(Buf, Aligned - 4);
527
165
}
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
81
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
81
  if (this->Size)
531
0
    return; // Already finalized.
532
81
533
81
  size_t Off = 0;
534
76
  for (CieRecord *Rec : CieRecords) {
535
76
    Rec->Cie->OutputOff = Off;
536
76
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
76
538
107
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
107
      Fde->OutputOff = Off;
540
107
      Off += alignTo(Fde->Size, Config->Wordsize);
541
107
    }
542
76
  }
543
81
544
81
  // The LSB standard does not allow a .eh_frame section with zero
545
81
  // Call Frame Information records. Therefore add a CIE record length
546
81
  // 0 as a terminator if this .eh_frame section is empty.
547
81
  if (Off == 0)
548
6
    Off = 4;
549
81
550
81
  this->Size = Off;
551
81
}
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
}
lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
529
74
template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
530
74
  if (this->Size)
531
0
    return; // Already finalized.
532
74
533
74
  size_t Off = 0;
534
69
  for (CieRecord *Rec : CieRecords) {
535
69
    Rec->Cie->OutputOff = Off;
536
69
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
537
69
538
100
    for (EhSectionPiece *Fde : Rec->Fdes) {
539
100
      Fde->OutputOff = Off;
540
100
      Off += alignTo(Fde->Size, Config->Wordsize);
541
100
    }
542
69
  }
543
74
544
74
  // The LSB standard does not allow a .eh_frame section with zero
545
74
  // Call Frame Information records. Therefore add a CIE record length
546
74
  // 0 as a terminator if this .eh_frame section is empty.
547
74
  if (Off == 0)
548
6
    Off = 4;
549
74
550
74
  this->Size = Off;
551
74
}
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
}
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)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
}
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
}
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
}
Unexecuted instantiation: SyntheticSections.cpp:unsigned long long readFdeAddr<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned char*, int)
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
}
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
}
Unexecuted instantiation: lld::elf::EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getFdePc(unsigned char*, unsigned long, unsigned char)
585
586
79
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
79
  const endianness E = ELFT::TargetEndianness;
588
74
  for (CieRecord *Rec : CieRecords) {
589
74
    size_t CieOffset = Rec->Cie->OutputOff;
590
74
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
74
592
105
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
105
      size_t Off = Fde->OutputOff;
594
105
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
105
596
105
      // FDE's second word should have the offset to an associated CIE.
597
105
      // Write it.
598
105
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
105
    }
600
74
  }
601
79
602
79
  for (EhInputSection *S : Sections)
603
90
    S->relocateAlloc(Buf, nullptr);
604
79
605
79
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
79
  // to get a FDE from an address to which FDE is applied. So here
607
79
  // we obtain two addresses and pass them to EhFrameHdr object.
608
79
  if (
In<ELFT>::EhFrameHdr79
) {
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
79
}
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
}
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)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, true> >::writeTo(unsigned char*)
Line
Count
Source
586
72
template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587
72
  const endianness E = ELFT::TargetEndianness;
588
67
  for (CieRecord *Rec : CieRecords) {
589
67
    size_t CieOffset = Rec->Cie->OutputOff;
590
67
    writeCieFde<ELFT>(Buf + CieOffset, Rec->Cie->data());
591
67
592
98
    for (EhSectionPiece *Fde : Rec->Fdes) {
593
98
      size_t Off = Fde->OutputOff;
594
98
      writeCieFde<ELFT>(Buf + Off, Fde->data());
595
98
596
98
      // FDE's second word should have the offset to an associated CIE.
597
98
      // Write it.
598
98
      write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599
98
    }
600
67
  }
601
72
602
72
  for (EhInputSection *S : Sections)
603
82
    S->relocateAlloc(Buf, nullptr);
604
72
605
72
  // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606
72
  // to get a FDE from an address to which FDE is applied. So here
607
72
  // we obtain two addresses and pass them to EhFrameHdr object.
608
72
  if (
In<ELFT>::EhFrameHdr72
) {
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
72
}
619
620
GotSection::GotSection()
621
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
622
1.46k
                       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
96
void GotSection::finalizeContents() { Size = NumEntries * Config->Wordsize; }
657
658
2.90k
bool GotSection::empty() const {
659
2.90k
  // We need to emit a GOT even if it's empty if there's a relocation that is
660
2.90k
  // relative to GOT(such as GOTOFFREL) or there's a symbol that points to a GOT
661
2.90k
  // (i.e. _GLOBAL_OFFSET_TABLE_).
662
2.90k
  return NumEntries == 0 && 
!HasGotOffRel2.64k
&&
!ElfSym::GlobalOffsetTable2.61k
;
663
2.90k
}
664
665
96
void GotSection::writeTo(uint8_t *Buf) {
666
96
  // Buf points to the start of this section's buffer,
667
96
  // whereas InputSectionBase::relocateAlloc() expects its argument
668
96
  // to point to the start of the output section.
669
96
  relocateAlloc(Buf - OutSecOff, Buf - OutSecOff + Size);
670
96
}
671
672
MipsGotSection::MipsGotSection()
673
    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
674
137
                       ".got") {}
675
676
95
void MipsGotSection::addEntry(SymbolBody &Sym, int64_t Addend, RelExpr Expr) {
677
95
  // For "true" local symbols which can be referenced from the same module
678
95
  // only compiler creates two instructions for address loading:
679
95
  //
680
95
  // lw   $8, 0($gp) # R_MIPS_GOT16
681
95
  // addi $8, $8, 0  # R_MIPS_LO16
682
95
  //
683
95
  // The first instruction loads high 16 bits of the symbol address while
684
95
  // the second adds an offset. That allows to reduce number of required
685
95
  // GOT entries because only one global offset table entry is necessary
686
95
  // for every 64 KBytes of local data. So for local symbols we need to
687
95
  // allocate number of GOT entries to hold all required "page" addresses.
688
95
  //
689
95
  // All global symbols (hidden and regular) considered by compiler uniformly.
690
95
  // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
691
95
  // to load address of the symbol. So for each such symbol we need to
692
95
  // allocate dedicated GOT entry to store its address.
693
95
  //
694
95
  // If a symbol is preemptible we need help of dynamic linker to get its
695
95
  // final address. The corresponding GOT entries are allocated in the
696
95
  // "global" part of GOT. Entries for non preemptible global symbol allocated
697
95
  // in the "local" part of GOT.
698
95
  //
699
95
  // See "Global Offset Table" in Chapter 5:
700
95
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
701
95
  if (
Expr == R_MIPS_GOT_LOCAL_PAGE95
) {
702
18
    // At this point we do not know final symbol value so to reduce number
703
18
    // of allocated GOT entries do the following trick. Save all output
704
18
    // sections referenced by GOT relocations. Then later in the `finalize`
705
18
    // method calculate number of "pages" required to cover all saved output
706
18
    // section and allocate appropriate number of GOT entries.
707
18
    PageIndexMap.insert({Sym.getOutputSection(), 0});
708
18
    return;
709
18
  }
710
77
  
if (77
Sym.isTls()77
) {
711
9
    // GOT entries created for MIPS TLS relocations behave like
712
9
    // almost GOT entries from other ABIs. They go to the end
713
9
    // of the global offset table.
714
9
    Sym.GotIndex = TlsEntries.size();
715
9
    TlsEntries.push_back(&Sym);
716
9
    return;
717
9
  }
718
68
  
auto AddEntry = [&](SymbolBody &S, uint64_t A, GotEntries &Items) 68
{
719
68
    if (
S.isInGot() && 68
!A11
)
720
11
      return;
721
57
    size_t NewIndex = Items.size();
722
57
    if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
723
0
      return;
724
57
    Items.emplace_back(&S, A);
725
57
    if (!A)
726
54
      S.GotIndex = NewIndex;
727
68
  };
728
68
  if (
Sym.isPreemptible()68
) {
729
35
    // Ignore addends for preemptible symbols. They got single GOT entry anyway.
730
35
    AddEntry(Sym, 0, GlobalEntries);
731
35
    Sym.IsInGlobalMipsGot = true;
732
68
  } else 
if (33
Expr == R_MIPS_GOT_OFF3233
) {
733
12
    AddEntry(Sym, Addend, LocalEntries32);
734
12
    Sym.Is32BitMipsGot = true;
735
33
  } else {
736
21
    // Hold local GOT entries accessed via a 16-bit index separately.
737
21
    // That allows to write them in the beginning of the GOT and keep
738
21
    // their indexes as less as possible to escape relocation's overflow.
739
21
    AddEntry(Sym, Addend, LocalEntries);
740
21
  }
741
95
}
742
743
9
bool MipsGotSection::addDynTlsEntry(SymbolBody &Sym) {
744
9
  if (Sym.GlobalDynIndex != -1U)
745
0
    return false;
746
9
  Sym.GlobalDynIndex = TlsEntries.size();
747
9
  // Global Dynamic TLS entries take two GOT slots.
748
9
  TlsEntries.push_back(nullptr);
749
9
  TlsEntries.push_back(&Sym);
750
9
  return true;
751
9
}
752
753
// Reserves TLS entries for a TLS module ID and a TLS block offset.
754
// In total it takes two GOT slots.
755
5
bool MipsGotSection::addTlsIndex() {
756
5
  if (TlsIndexOff != uint32_t(-1))
757
0
    return false;
758
5
  TlsIndexOff = TlsEntries.size() * Config->Wordsize;
759
5
  TlsEntries.push_back(nullptr);
760
5
  TlsEntries.push_back(nullptr);
761
5
  return true;
762
5
}
763
764
47
static uint64_t getMipsPageAddr(uint64_t Addr) {
765
47
  return (Addr + 0x8000) & ~0xffff;
766
47
}
767
768
22
static uint64_t getMipsPageCount(uint64_t Size) {
769
22
  return (Size + 0xfffe) / 0xffff + 1;
770
22
}
771
772
uint64_t MipsGotSection::getPageEntryOffset(const SymbolBody &B,
773
18
                                            int64_t Addend) const {
774
18
  const OutputSection *OutSec = B.getOutputSection();
775
18
  uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
776
18
  uint64_t SymAddr = getMipsPageAddr(B.getVA(Addend));
777
18
  uint64_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
778
18
  assert(Index < PageEntriesNum);
779
18
  return (HeaderEntriesNum + Index) * Config->Wordsize;
780
18
}
781
782
uint64_t MipsGotSection::getBodyEntryOffset(const SymbolBody &B,
783
74
                                            int64_t Addend) const {
784
74
  // Calculate offset of the GOT entries block: TLS, global, local.
785
74
  uint64_t Index = HeaderEntriesNum + PageEntriesNum;
786
74
  if (B.isTls())
787
9
    Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
788
65
  else 
if (65
B.IsInGlobalMipsGot65
)
789
32
    Index += LocalEntries.size() + LocalEntries32.size();
790
33
  else 
if (33
B.Is32BitMipsGot33
)
791
12
    Index += LocalEntries.size();
792
74
  // Calculate offset of the GOT entry in the block.
793
74
  if (B.isInGot())
794
71
    Index += B.GotIndex;
795
3
  else {
796
3
    auto It = EntryIndexMap.find({&B, Addend});
797
3
    assert(It != EntryIndexMap.end());
798
3
    Index += It->second;
799
3
  }
800
74
  return Index * Config->Wordsize;
801
74
}
802
803
34
uint64_t MipsGotSection::getTlsOffset() const {
804
34
  return (getLocalEntriesNum() + GlobalEntries.size()) * Config->Wordsize;
805
34
}
806
807
15
uint64_t MipsGotSection::getGlobalDynOffset(const SymbolBody &B) const {
808
15
  return B.GlobalDynIndex * Config->Wordsize;
809
15
}
810
811
77
const SymbolBody *MipsGotSection::getFirstGlobalEntry() const {
812
77
  return GlobalEntries.empty() ? 
nullptr48
:
GlobalEntries.front().first29
;
813
77
}
814
815
245
unsigned MipsGotSection::getLocalEntriesNum() const {
816
245
  return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
817
245
         LocalEntries32.size();
818
245
}
819
820
127
void MipsGotSection::finalizeContents() { updateAllocSize(); }
821
822
134
void MipsGotSection::updateAllocSize() {
823
134
  PageEntriesNum = 0;
824
11
  for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) {
825
11
    // For each output section referenced by GOT page relocations calculate
826
11
    // and save into PageIndexMap an upper bound of MIPS GOT entries required
827
11
    // to store page addresses of local symbols. We assume the worst case -
828
11
    // each 64kb page of the output section has at least one GOT relocation
829
11
    // against it. And take in account the case when the section intersects
830
11
    // page boundaries.
831
11
    P.second = PageEntriesNum;
832
11
    PageEntriesNum += getMipsPageCount(P.first->Size);
833
11
  }
834
134
  Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
835
134
         Config->Wordsize;
836
134
}
837
838
531
bool MipsGotSection::empty() const {
839
531
  // We add the .got section to the result for dynamic MIPS target because
840
531
  // its address and properties are mentioned in the .dynamic section.
841
531
  return Config->Relocatable;
842
531
}
843
844
272
uint64_t MipsGotSection::getGp() const { return ElfSym::MipsGp->getVA(0); }
845
846
7
static uint64_t readUint(uint8_t *Buf) {
847
7
  if (Config->Is64)
848
5
    return read64(Buf, Config->Endianness);
849
2
  return read32(Buf, Config->Endianness);
850
2
}
851
852
237
static void writeUint(uint8_t *Buf, uint64_t Val) {
853
237
  if (Config->Is64)
854
60
    write64(Buf, Val, Config->Endianness);
855
237
  else
856
177
    write32(Buf, Val, Config->Endianness);
857
237
}
858
859
127
void MipsGotSection::writeTo(uint8_t *Buf) {
860
127
  // Set the MSB of the second GOT slot. This is not required by any
861
127
  // MIPS ABI documentation, though.
862
127
  //
863
127
  // There is a comment in glibc saying that "The MSB of got[1] of a
864
127
  // gnu object is set to identify gnu objects," and in GNU gold it
865
127
  // says "the second entry will be used by some runtime loaders".
866
127
  // But how this field is being used is unclear.
867
127
  //
868
127
  // We are not really willing to mimic other linkers behaviors
869
127
  // without understanding why they do that, but because all files
870
127
  // generated by GNU tools have this special GOT value, and because
871
127
  // we've been doing this for years, it is probably a safe bet to
872
127
  // keep doing this for now. We really need to revisit this to see
873
127
  // if we had to do this.
874
127
  writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
875
127
  Buf += HeaderEntriesNum * Config->Wordsize;
876
127
  // Write 'page address' entries to the local part of the GOT.
877
11
  for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) {
878
11
    size_t PageCount = getMipsPageCount(L.first->Size);
879
11
    uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
880
36
    for (size_t PI = 0; 
PI < PageCount36
;
++PI25
) {
881
25
      uint8_t *Entry = Buf + (L.second + PI) * Config->Wordsize;
882
25
      writeUint(Entry, FirstPageAddr + PI * 0x10000);
883
25
    }
884
11
  }
885
127
  Buf += PageEntriesNum * Config->Wordsize;
886
57
  auto AddEntry = [&](const GotEntry &SA) {
887
57
    uint8_t *Entry = Buf;
888
57
    Buf += Config->Wordsize;
889
57
    const SymbolBody *Body = SA.first;
890
57
    uint64_t VA = Body->getVA(SA.second);
891
57
    writeUint(Entry, VA);
892
57
  };
893
127
  std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
894
127
  std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
895
127
  std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
896
127
  // Initialize TLS-related GOT entries. If the entry has a corresponding
897
127
  // dynamic relocations, leave it initialized by zero. Write down adjusted
898
127
  // TLS symbol's values otherwise. To calculate the adjustments use offsets
899
127
  // for thread-local storage.
900
127
  // https://www.linux-mips.org/wiki/NPTL
901
127
  if (
TlsIndexOff != -1U && 127
!Config->Pic5
)
902
3
    writeUint(Buf + TlsIndexOff, 1);
903
37
  for (const SymbolBody *B : TlsEntries) {
904
37
    if (
!B || 37
B->isPreemptible()18
)
905
31
      continue;
906
6
    uint64_t VA = B->getVA();
907
6
    if (
B->GotIndex != -1U6
) {
908
6
      uint8_t *Entry = Buf + B->GotIndex * Config->Wordsize;
909
6
      writeUint(Entry, VA - 0x7000);
910
6
    }
911
6
    if (
B->GlobalDynIndex != -1U6
) {
912
6
      uint8_t *Entry = Buf + B->GlobalDynIndex * Config->Wordsize;
913
6
      writeUint(Entry, 1);
914
6
      Entry += Config->Wordsize;
915
6
      writeUint(Entry, VA - 0x8000);
916
6
    }
917
37
  }
918
127
}
919
920
GotPltSection::GotPltSection()
921
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
922
1.59k
                       Target->GotPltEntrySize, ".got.plt") {}
923
924
187
void GotPltSection::addEntry(SymbolBody &Sym) {
925
187
  Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
926
187
  Entries.push_back(&Sym);
927
187
}
928
929
1.90k
size_t GotPltSection::getSize() const {
930
1.90k
  return (Target->GotPltHeaderEntriesNum + Entries.size()) *
931
1.90k
         Target->GotPltEntrySize;
932
1.90k
}
933
934
132
void GotPltSection::writeTo(uint8_t *Buf) {
935
132
  Target->writeGotPltHeader(Buf);
936
132
  Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
937
187
  for (const SymbolBody *B : Entries) {
938
187
    Target->writeGotPlt(Buf, *B);
939
187
    Buf += Config->Wordsize;
940
187
  }
941
132
}
942
943
// On ARM the IgotPltSection is part of the GotSection, on other Targets it is
944
// part of the .got.plt
945
IgotPltSection::IgotPltSection()
946
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
947
                       Target->GotPltEntrySize,
948
1.59k
                       Config->EMachine == EM_ARM ? ".got" : ".got.plt") {}
949
950
27
void IgotPltSection::addEntry(SymbolBody &Sym) {
951
27
  Sym.IsInIgot = true;
952
27
  Sym.GotPltIndex = Entries.size();
953
27
  Entries.push_back(&Sym);
954
27
}
955
956
1.63k
size_t IgotPltSection::getSize() const {
957
1.63k
  return Entries.size() * Target->GotPltEntrySize;
958
1.63k
}
959
960
15
void IgotPltSection::writeTo(uint8_t *Buf) {
961
27
  for (const SymbolBody *B : Entries) {
962
27
    Target->writeIgotPlt(Buf, *B);
963
27
    Buf += Config->Wordsize;
964
27
  }
965
15
}
966
967
StringTableSection::StringTableSection(StringRef Name, bool Dynamic)
968
    : SyntheticSection(Dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name),
969
4.79k
      Dynamic(Dynamic) {
970
4.79k
  // ELF string tables start with a NUL byte.
971
4.79k
  addString("");
972
4.79k
}
973
974
// Adds a string to the string table. If HashIt is true we hash and check for
975
// duplicates. It is optional because the name of global symbols are already
976
// uniqued and hashing them again has a big cost for a small value: uniquing
977
// them with some other string that happens to be the same.
978
220k
unsigned StringTableSection::addString(StringRef S, bool HashIt) {
979
220k
  if (
HashIt220k
) {
980
215k
    auto R = StringMap.insert(std::make_pair(S, this->Size));
981
215k
    if (!R.second)
982
288
      return R.first->second;
983
220k
  }
984
220k
  unsigned Ret = this->Size;
985
220k
  this->Size = this->Size + S.size() + 1;
986
220k
  Strings.push_back(S);
987
220k
  return Ret;
988
220k
}
989
990
3.79k
void StringTableSection::writeTo(uint8_t *Buf) {
991
219k
  for (StringRef S : Strings) {
992
219k
    memcpy(Buf, S.data(), S.size());
993
219k
    Buf[S.size()] = '\0';
994
219k
    Buf += S.size() + 1;
995
219k
  }
996
3.79k
}
997
998
// Returns the number of version definition entries. Because the first entry
999
// is for the version definition itself, it is the number of versioned symbols
1000
// plus one. Note that we don't support multiple versions yet.
1001
985
static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
1002
1003
template <class ELFT>
1004
DynamicSection<ELFT>::DynamicSection()
1005
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, Config->Wordsize,
1006
1.59k
                       ".dynamic") {
1007
1.59k
  this->Entsize = ELFT::Is64Bits ? 
161.31k
:
8288
;
1008
1.59k
1009
1.59k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1010
1.59k
  // which passes -z rodynamic.
1011
1.59k
  // See "Special Section" in Chapter 4 in the following document:
1012
1.59k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1013
1.59k
  if (
Config->EMachine == EM_MIPS || 1.59k
Config->ZRodynamic1.46k
)
1014
139
    this->Flags = SHF_ALLOC;
1015
1.59k
1016
1.59k
  addEntries();
1017
1.59k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::DynamicSection()
Line
Count
Source
1006
193
                       ".dynamic") {
1007
193
  this->Entsize = ELFT::Is64Bits ? 
160
:
8193
;
1008
193
1009
193
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1010
193
  // which passes -z rodynamic.
1011
193
  // See "Special Section" in Chapter 4 in the following document:
1012
193
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1013
193
  if (
Config->EMachine == EM_MIPS || 193
Config->ZRodynamic179
)
1014
14
    this->Flags = SHF_ALLOC;
1015
193
1016
193
  addEntries();
1017
193
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::DynamicSection()
Line
Count
Source
1006
44
                       ".dynamic") {
1007
44
  this->Entsize = ELFT::Is64Bits ? 
1644
:
80
;
1008
44
1009
44
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1010
44
  // which passes -z rodynamic.
1011
44
  // See "Special Section" in Chapter 4 in the following document:
1012
44
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1013
44
  if (
Config->EMachine == EM_MIPS || 44
Config->ZRodynamic16
)
1014
28
    this->Flags = SHF_ALLOC;
1015
44
1016
44
  addEntries();
1017
44
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::DynamicSection()
Line
Count
Source
1006
95
                       ".dynamic") {
1007
95
  this->Entsize = ELFT::Is64Bits ? 
160
:
895
;
1008
95
1009
95
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1010
95
  // which passes -z rodynamic.
1011
95
  // See "Special Section" in Chapter 4 in the following document:
1012
95
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1013
95
  if (
Config->EMachine == EM_MIPS || 95
Config->ZRodynamic2
)
1014
93
    this->Flags = SHF_ALLOC;
1015
95
1016
95
  addEntries();
1017
95
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::DynamicSection()
Line
Count
Source
1006
1.26k
                       ".dynamic") {
1007
1.26k
  this->Entsize = ELFT::Is64Bits ? 
161.26k
:
80
;
1008
1.26k
1009
1.26k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1010
1.26k
  // which passes -z rodynamic.
1011
1.26k
  // See "Special Section" in Chapter 4 in the following document:
1012
1.26k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1013
1.26k
  if (
Config->EMachine == EM_MIPS || 1.26k
Config->ZRodynamic1.26k
)
1014
4
    this->Flags = SHF_ALLOC;
1015
1.26k
1016
1.26k
  addEntries();
1017
1.26k
}
1018
1019
// There are some dynamic entries that don't depend on other sections.
1020
// Such entries can be set early.
1021
1.59k
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1022
1.59k
  // Add strings to .dynstr early so that .dynstr's size will be
1023
1.59k
  // fixed early.
1024
1.59k
  for (StringRef S : Config->FilterList)
1025
6
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1026
1.59k
  for (StringRef S : Config->AuxiliaryList)
1027
2
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1028
1.59k
  if (!Config->Rpath.empty())
1029
6
    
add({Config->EnableNewDtags ? 6
DT_RUNPATH5
:
DT_RPATH1
,
1030
6
         InX::DynStrTab->addString(Config->Rpath)});
1031
213
  for (InputFile *File : SharedFiles) {
1032
213
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1033
213
    if (F->isNeeded())
1034
208
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1035
213
  }
1036
1.59k
  if (!Config->SoName.empty())
1037
16
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1038
1.59k
1039
1.59k
  // Set DT_FLAGS and DT_FLAGS_1.
1040
1.59k
  uint32_t DtFlags = 0;
1041
1.59k
  uint32_t DtFlags1 = 0;
1042
1.59k
  if (Config->Bsymbolic)
1043
6
    DtFlags |= DF_SYMBOLIC;
1044
1.59k
  if (Config->ZNodelete)
1045
1
    DtFlags1 |= DF_1_NODELETE;
1046
1.59k
  if (Config->ZNodlopen)
1047
1
    DtFlags1 |= DF_1_NOOPEN;
1048
1.59k
  if (
Config->ZNow1.59k
) {
1049
2
    DtFlags |= DF_BIND_NOW;
1050
2
    DtFlags1 |= DF_1_NOW;
1051
2
  }
1052
1.59k
  if (
Config->ZOrigin1.59k
) {
1053
1
    DtFlags |= DF_ORIGIN;
1054
1
    DtFlags1 |= DF_1_ORIGIN;
1055
1
  }
1056
1.59k
1057
1.59k
  if (DtFlags)
1058
7
    add({DT_FLAGS, DtFlags});
1059
1.59k
  if (DtFlags1)
1060
2
    add({DT_FLAGS_1, DtFlags1});
1061
1.59k
1062
1.59k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1063
1.59k
  // need it for each process, so we don't write it for DSOs. The loader writes
1064
1.59k
  // the pointer into this entry.
1065
1.59k
  //
1066
1.59k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1067
1.59k
  // systems (currently only Fuchsia OS) provide other means to give the
1068
1.59k
  // debugger this information. Such systems may choose make .dynamic read-only.
1069
1.59k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1070
1.59k
  if (
!Config->Shared && 1.59k
!Config->Relocatable1.00k
&&
!Config->ZRodynamic942
)
1071
941
    add({DT_DEBUG, (uint64_t)0});
1072
1.59k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addEntries()
Line
Count
Source
1021
193
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1022
193
  // Add strings to .dynstr early so that .dynstr's size will be
1023
193
  // fixed early.
1024
193
  for (StringRef S : Config->FilterList)
1025
0
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1026
193
  for (StringRef S : Config->AuxiliaryList)
1027
0
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1028
193
  if (!Config->Rpath.empty())
1029
2
    
add({Config->EnableNewDtags ? 2
DT_RUNPATH2
:
DT_RPATH0
,
1030
2
         InX::DynStrTab->addString(Config->Rpath)});
1031
38
  for (InputFile *File : SharedFiles) {
1032
38
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1033
38
    if (F->isNeeded())
1034
34
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1035
38
  }
1036
193
  if (!Config->SoName.empty())
1037
3
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1038
193
1039
193
  // Set DT_FLAGS and DT_FLAGS_1.
1040
193
  uint32_t DtFlags = 0;
1041
193
  uint32_t DtFlags1 = 0;
1042
193
  if (Config->Bsymbolic)
1043
2
    DtFlags |= DF_SYMBOLIC;
1044
193
  if (Config->ZNodelete)
1045
0
    DtFlags1 |= DF_1_NODELETE;
1046
193
  if (Config->ZNodlopen)
1047
0
    DtFlags1 |= DF_1_NOOPEN;
1048
193
  if (
Config->ZNow193
) {
1049
0
    DtFlags |= DF_BIND_NOW;
1050
0
    DtFlags1 |= DF_1_NOW;
1051
0
  }
1052
193
  if (
Config->ZOrigin193
) {
1053
0
    DtFlags |= DF_ORIGIN;
1054
0
    DtFlags1 |= DF_1_ORIGIN;
1055
0
  }
1056
193
1057
193
  if (DtFlags)
1058
2
    add({DT_FLAGS, DtFlags});
1059
193
  if (DtFlags1)
1060
0
    add({DT_FLAGS_1, DtFlags1});
1061
193
1062
193
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1063
193
  // need it for each process, so we don't write it for DSOs. The loader writes
1064
193
  // the pointer into this entry.
1065
193
  //
1066
193
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1067
193
  // systems (currently only Fuchsia OS) provide other means to give the
1068
193
  // debugger this information. Such systems may choose make .dynamic read-only.
1069
193
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1070
193
  if (
!Config->Shared && 193
!Config->Relocatable116
&&
!Config->ZRodynamic110
)
1071
110
    add({DT_DEBUG, (uint64_t)0});
1072
193
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addEntries()
Line
Count
Source
1021
1.26k
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1022
1.26k
  // Add strings to .dynstr early so that .dynstr's size will be
1023
1.26k
  // fixed early.
1024
1.26k
  for (StringRef S : Config->FilterList)
1025
6
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1026
1.26k
  for (StringRef S : Config->AuxiliaryList)
1027
2
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1028
1.26k
  if (!Config->Rpath.empty())
1029
3
    
add({Config->EnableNewDtags ? 3
DT_RUNPATH2
:
DT_RPATH1
,
1030
3
         InX::DynStrTab->addString(Config->Rpath)});
1031
151
  for (InputFile *File : SharedFiles) {
1032
151
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1033
151
    if (F->isNeeded())
1034
150
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1035
151
  }
1036
1.26k
  if (!Config->SoName.empty())
1037
13
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1038
1.26k
1039
1.26k
  // Set DT_FLAGS and DT_FLAGS_1.
1040
1.26k
  uint32_t DtFlags = 0;
1041
1.26k
  uint32_t DtFlags1 = 0;
1042
1.26k
  if (Config->Bsymbolic)
1043
3
    DtFlags |= DF_SYMBOLIC;
1044
1.26k
  if (Config->ZNodelete)
1045
1
    DtFlags1 |= DF_1_NODELETE;
1046
1.26k
  if (Config->ZNodlopen)
1047
1
    DtFlags1 |= DF_1_NOOPEN;
1048
1.26k
  if (
Config->ZNow1.26k
) {
1049
2
    DtFlags |= DF_BIND_NOW;
1050
2
    DtFlags1 |= DF_1_NOW;
1051
2
  }
1052
1.26k
  if (
Config->ZOrigin1.26k
) {
1053
1
    DtFlags |= DF_ORIGIN;
1054
1
    DtFlags1 |= DF_1_ORIGIN;
1055
1
  }
1056
1.26k
1057
1.26k
  if (DtFlags)
1058
4
    add({DT_FLAGS, DtFlags});
1059
1.26k
  if (DtFlags1)
1060
2
    add({DT_FLAGS_1, DtFlags1});
1061
1.26k
1062
1.26k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1063
1.26k
  // need it for each process, so we don't write it for DSOs. The loader writes
1064
1.26k
  // the pointer into this entry.
1065
1.26k
  //
1066
1.26k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1067
1.26k
  // systems (currently only Fuchsia OS) provide other means to give the
1068
1.26k
  // debugger this information. Such systems may choose make .dynamic read-only.
1069
1.26k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1070
1.26k
  if (
!Config->Shared && 1.26k
!Config->Relocatable806
&&
!Config->ZRodynamic761
)
1071
760
    add({DT_DEBUG, (uint64_t)0});
1072
1.26k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addEntries()
Line
Count
Source
1021
95
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1022
95
  // Add strings to .dynstr early so that .dynstr's size will be
1023
95
  // fixed early.
1024
95
  for (StringRef S : Config->FilterList)
1025
0
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1026
95
  for (StringRef S : Config->AuxiliaryList)
1027
0
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1028
95
  if (!Config->Rpath.empty())
1029
0
    
add({Config->EnableNewDtags ? 0
DT_RUNPATH0
:
DT_RPATH0
,
1030
0
         InX::DynStrTab->addString(Config->Rpath)});
1031
17
  for (InputFile *File : SharedFiles) {
1032
17
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1033
17
    if (F->isNeeded())
1034
17
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1035
17
  }
1036
95
  if (!Config->SoName.empty())
1037
0
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1038
95
1039
95
  // Set DT_FLAGS and DT_FLAGS_1.
1040
95
  uint32_t DtFlags = 0;
1041
95
  uint32_t DtFlags1 = 0;
1042
95
  if (Config->Bsymbolic)
1043
1
    DtFlags |= DF_SYMBOLIC;
1044
95
  if (Config->ZNodelete)
1045
0
    DtFlags1 |= DF_1_NODELETE;
1046
95
  if (Config->ZNodlopen)
1047
0
    DtFlags1 |= DF_1_NOOPEN;
1048
95
  if (
Config->ZNow95
) {
1049
0
    DtFlags |= DF_BIND_NOW;
1050
0
    DtFlags1 |= DF_1_NOW;
1051
0
  }
1052
95
  if (
Config->ZOrigin95
) {
1053
0
    DtFlags |= DF_ORIGIN;
1054
0
    DtFlags1 |= DF_1_ORIGIN;
1055
0
  }
1056
95
1057
95
  if (DtFlags)
1058
1
    add({DT_FLAGS, DtFlags});
1059
95
  if (DtFlags1)
1060
0
    add({DT_FLAGS_1, DtFlags1});
1061
95
1062
95
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1063
95
  // need it for each process, so we don't write it for DSOs. The loader writes
1064
95
  // the pointer into this entry.
1065
95
  //
1066
95
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1067
95
  // systems (currently only Fuchsia OS) provide other means to give the
1068
95
  // debugger this information. Such systems may choose make .dynamic read-only.
1069
95
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1070
95
  if (
!Config->Shared && 95
!Config->Relocatable54
&&
!Config->ZRodynamic46
)
1071
46
    add({DT_DEBUG, (uint64_t)0});
1072
95
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addEntries()
Line
Count
Source
1021
44
template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1022
44
  // Add strings to .dynstr early so that .dynstr's size will be
1023
44
  // fixed early.
1024
44
  for (StringRef S : Config->FilterList)
1025
0
    add({DT_FILTER, InX::DynStrTab->addString(S)});
1026
44
  for (StringRef S : Config->AuxiliaryList)
1027
0
    add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1028
44
  if (!Config->Rpath.empty())
1029
1
    
add({Config->EnableNewDtags ? 1
DT_RUNPATH1
:
DT_RPATH0
,
1030
1
         InX::DynStrTab->addString(Config->Rpath)});
1031
7
  for (InputFile *File : SharedFiles) {
1032
7
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1033
7
    if (F->isNeeded())
1034
7
      add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1035
7
  }
1036
44
  if (!Config->SoName.empty())
1037
0
    add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1038
44
1039
44
  // Set DT_FLAGS and DT_FLAGS_1.
1040
44
  uint32_t DtFlags = 0;
1041
44
  uint32_t DtFlags1 = 0;
1042
44
  if (Config->Bsymbolic)
1043
0
    DtFlags |= DF_SYMBOLIC;
1044
44
  if (Config->ZNodelete)
1045
0
    DtFlags1 |= DF_1_NODELETE;
1046
44
  if (Config->ZNodlopen)
1047
0
    DtFlags1 |= DF_1_NOOPEN;
1048
44
  if (
Config->ZNow44
) {
1049
0
    DtFlags |= DF_BIND_NOW;
1050
0
    DtFlags1 |= DF_1_NOW;
1051
0
  }
1052
44
  if (
Config->ZOrigin44
) {
1053
0
    DtFlags |= DF_ORIGIN;
1054
0
    DtFlags1 |= DF_1_ORIGIN;
1055
0
  }
1056
44
1057
44
  if (DtFlags)
1058
0
    add({DT_FLAGS, DtFlags});
1059
44
  if (DtFlags1)
1060
0
    add({DT_FLAGS_1, DtFlags1});
1061
44
1062
44
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1063
44
  // need it for each process, so we don't write it for DSOs. The loader writes
1064
44
  // the pointer into this entry.
1065
44
  //
1066
44
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1067
44
  // systems (currently only Fuchsia OS) provide other means to give the
1068
44
  // debugger this information. Such systems may choose make .dynamic read-only.
1069
44
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1070
44
  if (
!Config->Shared && 44
!Config->Relocatable26
&&
!Config->ZRodynamic25
)
1071
25
    add({DT_DEBUG, (uint64_t)0});
1072
44
}
1073
1074
// Add remaining entries to complete .dynamic contents.
1075
765
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1076
765
  if (this->Size)
1077
0
    return; // Already finalized.
1078
765
1079
765
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1080
765
  if (
In<ELFT>::RelaDyn->getParent() && 765
!In<ELFT>::RelaDyn->empty()765
) {
1081
148
    bool IsRela = Config->IsRela;
1082
148
    add({IsRela ? 
DT_RELA105
:
DT_REL43
, In<ELFT>::RelaDyn});
1083
148
    add({IsRela ? 
DT_RELASZ105
:
DT_RELSZ43
, In<ELFT>::RelaDyn->getParent(),
1084
148
         Entry::SecSize});
1085
148
    add({IsRela ? 
DT_RELAENT105
:
DT_RELENT43
,
1086
148
         uint64_t(IsRela ? 
sizeof(Elf_Rela)105
:
sizeof(Elf_Rel)43
)});
1087
148
1088
148
    // MIPS dynamic loader does not support RELCOUNT tag.
1089
148
    // The problem is in the tight relation between dynamic
1090
148
    // relocations and GOT. So do not emit this tag on MIPS.
1091
148
    if (
Config->EMachine != EM_MIPS148
) {
1092
136
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1093
136
      if (
Config->ZCombreloc && 136
NumRelativeRels135
)
1094
29
        
add({IsRela ? 29
DT_RELACOUNT20
:
DT_RELCOUNT9
, NumRelativeRels});
1095
136
    }
1096
148
  }
1097
765
  if (
In<ELFT>::RelaPlt->getParent() && 765
!In<ELFT>::RelaPlt->empty()765
) {
1098
132
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1099
132
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1100
132
    switch (Config->EMachine) {
1101
8
    case EM_MIPS:
1102
8
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1103
8
      break;
1104
0
    case EM_SPARCV9:
1105
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1106
0
      break;
1107
124
    default:
1108
124
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1109
124
      break;
1110
132
    }
1111
132
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 132
DT_RELA103
:
DT_REL29
)});
1112
132
  }
1113
765
1114
765
  add({DT_SYMTAB, InX::DynSymTab});
1115
765
  add({DT_SYMENT, sizeof(Elf_Sym)});
1116
765
  add({DT_STRTAB, InX::DynStrTab});
1117
765
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1118
765
  if (!Config->ZText)
1119
5
    add({DT_TEXTREL, (uint64_t)0});
1120
765
  if (InX::GnuHashTab)
1121
5
    add({DT_GNU_HASH, InX::GnuHashTab});
1122
765
  if (InX::HashTab)
1123
761
    add({DT_HASH, InX::HashTab});
1124
765
1125
765
  if (
Out::PreinitArray765
) {
1126
3
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1127
3
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1128
3
  }
1129
765
  if (
Out::InitArray765
) {
1130
3
    add({DT_INIT_ARRAY, Out::InitArray});
1131
3
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1132
3
  }
1133
765
  if (
Out::FiniArray765
) {
1134
2
    add({DT_FINI_ARRAY, Out::FiniArray});
1135
2
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1136
2
  }
1137
765
1138
765
  if (SymbolBody *B = Symtab->find(Config->Init))
1139
6
    
if (6
B->isInCurrentDSO()6
)
1140
4
      add({DT_INIT, B});
1141
765
  if (SymbolBody *B = Symtab->find(Config->Fini))
1142
6
    
if (6
B->isInCurrentDSO()6
)
1143
4
      add({DT_FINI, B});
1144
765
1145
765
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1146
765
  if (
HasVerNeed || 765
In<ELFT>::VerDef759
)
1147
41
    add({DT_VERSYM, In<ELFT>::VerSym});
1148
765
  if (
In<ELFT>::VerDef765
) {
1149
36
    add({DT_VERDEF, In<ELFT>::VerDef});
1150
36
    add({DT_VERDEFNUM, getVerDefNum()});
1151
36
  }
1152
765
  if (
HasVerNeed765
) {
1153
6
    add({DT_VERNEED, In<ELFT>::VerNeed});
1154
6
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1155
6
  }
1156
765
1157
765
  if (
Config->EMachine == EM_MIPS765
) {
1158
77
    add({DT_MIPS_RLD_VERSION, 1});
1159
77
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1160
77
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1161
77
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1162
77
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1163
77
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1164
29
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1165
77
    else
1166
48
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1167
77
    add({DT_PLTGOT, InX::MipsGot});
1168
77
    if (InX::MipsRldMap)
1169
20
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1170
77
  }
1171
765
1172
765
  getParent()->Link = this->Link;
1173
765
1174
765
  // +1 for DT_NULL
1175
765
  this->Size = (Entries.size() + 1) * this->Entsize;
1176
765
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1075
104
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1076
104
  if (this->Size)
1077
0
    return; // Already finalized.
1078
104
1079
104
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1080
104
  if (
In<ELFT>::RelaDyn->getParent() && 104
!In<ELFT>::RelaDyn->empty()104
) {
1081
37
    bool IsRela = Config->IsRela;
1082
37
    add({IsRela ? 
DT_RELA0
:
DT_REL37
, In<ELFT>::RelaDyn});
1083
37
    add({IsRela ? 
DT_RELASZ0
:
DT_RELSZ37
, In<ELFT>::RelaDyn->getParent(),
1084
37
         Entry::SecSize});
1085
37
    add({IsRela ? 
DT_RELAENT0
:
DT_RELENT37
,
1086
37
         uint64_t(IsRela ? 
sizeof(Elf_Rela)0
:
sizeof(Elf_Rel)37
)});
1087
37
1088
37
    // MIPS dynamic loader does not support RELCOUNT tag.
1089
37
    // The problem is in the tight relation between dynamic
1090
37
    // relocations and GOT. So do not emit this tag on MIPS.
1091
37
    if (
Config->EMachine != EM_MIPS37
) {
1092
36
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1093
36
      if (
Config->ZCombreloc && 36
NumRelativeRels36
)
1094
9
        
add({IsRela ? 9
DT_RELACOUNT0
:
DT_RELCOUNT9
, NumRelativeRels});
1095
36
    }
1096
37
  }
1097
104
  if (
In<ELFT>::RelaPlt->getParent() && 104
!In<ELFT>::RelaPlt->empty()104
) {
1098
24
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1099
24
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1100
24
    switch (Config->EMachine) {
1101
2
    case EM_MIPS:
1102
2
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1103
2
      break;
1104
0
    case EM_SPARCV9:
1105
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1106
0
      break;
1107
22
    default:
1108
22
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1109
22
      break;
1110
24
    }
1111
24
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 24
DT_RELA1
:
DT_REL23
)});
1112
24
  }
1113
104
1114
104
  add({DT_SYMTAB, InX::DynSymTab});
1115
104
  add({DT_SYMENT, sizeof(Elf_Sym)});
1116
104
  add({DT_STRTAB, InX::DynStrTab});
1117
104
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1118
104
  if (!Config->ZText)
1119
0
    add({DT_TEXTREL, (uint64_t)0});
1120
104
  if (InX::GnuHashTab)
1121
2
    add({DT_GNU_HASH, InX::GnuHashTab});
1122
104
  if (InX::HashTab)
1123
102
    add({DT_HASH, InX::HashTab});
1124
104
1125
104
  if (
Out::PreinitArray104
) {
1126
0
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1127
0
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1128
0
  }
1129
104
  if (
Out::InitArray104
) {
1130
0
    add({DT_INIT_ARRAY, Out::InitArray});
1131
0
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1132
0
  }
1133
104
  if (
Out::FiniArray104
) {
1134
0
    add({DT_FINI_ARRAY, Out::FiniArray});
1135
0
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1136
0
  }
1137
104
1138
104
  if (SymbolBody *B = Symtab->find(Config->Init))
1139
0
    
if (0
B->isInCurrentDSO()0
)
1140
0
      add({DT_INIT, B});
1141
104
  if (SymbolBody *B = Symtab->find(Config->Fini))
1142
0
    
if (0
B->isInCurrentDSO()0
)
1143
0
      add({DT_FINI, B});
1144
104
1145
104
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1146
104
  if (
HasVerNeed || 104
In<ELFT>::VerDef104
)
1147
0
    add({DT_VERSYM, In<ELFT>::VerSym});
1148
104
  if (
In<ELFT>::VerDef104
) {
1149
0
    add({DT_VERDEF, In<ELFT>::VerDef});
1150
0
    add({DT_VERDEFNUM, getVerDefNum()});
1151
0
  }
1152
104
  if (
HasVerNeed104
) {
1153
0
    add({DT_VERNEED, In<ELFT>::VerNeed});
1154
0
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1155
0
  }
1156
104
1157
104
  if (
Config->EMachine == EM_MIPS104
) {
1158
8
    add({DT_MIPS_RLD_VERSION, 1});
1159
8
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1160
8
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1161
8
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1162
8
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1163
8
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1164
3
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1165
8
    else
1166
5
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1167
8
    add({DT_PLTGOT, InX::MipsGot});
1168
8
    if (InX::MipsRldMap)
1169
2
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1170
8
  }
1171
104
1172
104
  getParent()->Link = this->Link;
1173
104
1174
104
  // +1 for DT_NULL
1175
104
  this->Size = (Entries.size() + 1) * this->Entsize;
1176
104
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1075
54
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1076
54
  if (this->Size)
1077
0
    return; // Already finalized.
1078
54
1079
54
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1080
54
  if (
In<ELFT>::RelaDyn->getParent() && 54
!In<ELFT>::RelaDyn->empty()54
) {
1081
6
    bool IsRela = Config->IsRela;
1082
6
    add({IsRela ? 
DT_RELA0
:
DT_REL6
, In<ELFT>::RelaDyn});
1083
6
    add({IsRela ? 
DT_RELASZ0
:
DT_RELSZ6
, In<ELFT>::RelaDyn->getParent(),
1084
6
         Entry::SecSize});
1085
6
    add({IsRela ? 
DT_RELAENT0
:
DT_RELENT6
,
1086
6
         uint64_t(IsRela ? 
sizeof(Elf_Rela)0
:
sizeof(Elf_Rel)6
)});
1087
6
1088
6
    // MIPS dynamic loader does not support RELCOUNT tag.
1089
6
    // The problem is in the tight relation between dynamic
1090
6
    // relocations and GOT. So do not emit this tag on MIPS.
1091
6
    if (
Config->EMachine != EM_MIPS6
) {
1092
0
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1093
0
      if (
Config->ZCombreloc && 0
NumRelativeRels0
)
1094
0
        
add({IsRela ? 0
DT_RELACOUNT0
:
DT_RELCOUNT0
, NumRelativeRels});
1095
0
    }
1096
6
  }
1097
54
  if (
In<ELFT>::RelaPlt->getParent() && 54
!In<ELFT>::RelaPlt->empty()54
) {
1098
6
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1099
6
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1100
6
    switch (Config->EMachine) {
1101
6
    case EM_MIPS:
1102
6
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1103
6
      break;
1104
0
    case EM_SPARCV9:
1105
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1106
0
      break;
1107
0
    default:
1108
0
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1109
0
      break;
1110
6
    }
1111
6
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 6
DT_RELA0
:
DT_REL6
)});
1112
6
  }
1113
54
1114
54
  add({DT_SYMTAB, InX::DynSymTab});
1115
54
  add({DT_SYMENT, sizeof(Elf_Sym)});
1116
54
  add({DT_STRTAB, InX::DynStrTab});
1117
54
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1118
54
  if (!Config->ZText)
1119
0
    add({DT_TEXTREL, (uint64_t)0});
1120
54
  if (InX::GnuHashTab)
1121
0
    add({DT_GNU_HASH, InX::GnuHashTab});
1122
54
  if (InX::HashTab)
1123
54
    add({DT_HASH, InX::HashTab});
1124
54
1125
54
  if (
Out::PreinitArray54
) {
1126
0
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1127
0
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1128
0
  }
1129
54
  if (
Out::InitArray54
) {
1130
0
    add({DT_INIT_ARRAY, Out::InitArray});
1131
0
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1132
0
  }
1133
54
  if (
Out::FiniArray54
) {
1134
0
    add({DT_FINI_ARRAY, Out::FiniArray});
1135
0
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1136
0
  }
1137
54
1138
54
  if (SymbolBody *B = Symtab->find(Config->Init))
1139
0
    
if (0
B->isInCurrentDSO()0
)
1140
0
      add({DT_INIT, B});
1141
54
  if (SymbolBody *B = Symtab->find(Config->Fini))
1142
0
    
if (0
B->isInCurrentDSO()0
)
1143
0
      add({DT_FINI, B});
1144
54
1145
54
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1146
54
  if (
HasVerNeed || 54
In<ELFT>::VerDef54
)
1147
0
    add({DT_VERSYM, In<ELFT>::VerSym});
1148
54
  if (
In<ELFT>::VerDef54
) {
1149
0
    add({DT_VERDEF, In<ELFT>::VerDef});
1150
0
    add({DT_VERDEFNUM, getVerDefNum()});
1151
0
  }
1152
54
  if (
HasVerNeed54
) {
1153
0
    add({DT_VERNEED, In<ELFT>::VerNeed});
1154
0
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1155
0
  }
1156
54
1157
54
  if (
Config->EMachine == EM_MIPS54
) {
1158
53
    add({DT_MIPS_RLD_VERSION, 1});
1159
53
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1160
53
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1161
53
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1162
53
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1163
53
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1164
21
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1165
53
    else
1166
32
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1167
53
    add({DT_PLTGOT, InX::MipsGot});
1168
53
    if (InX::MipsRldMap)
1169
13
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1170
53
  }
1171
54
1172
54
  getParent()->Link = this->Link;
1173
54
1174
54
  // +1 for DT_NULL
1175
54
  this->Size = (Entries.size() + 1) * this->Entsize;
1176
54
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1075
25
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1076
25
  if (this->Size)
1077
0
    return; // Already finalized.
1078
25
1079
25
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1080
25
  if (
In<ELFT>::RelaDyn->getParent() && 25
!In<ELFT>::RelaDyn->empty()25
) {
1081
10
    bool IsRela = Config->IsRela;
1082
10
    add({IsRela ? 
DT_RELA10
:
DT_REL0
, In<ELFT>::RelaDyn});
1083
10
    add({IsRela ? 
DT_RELASZ10
:
DT_RELSZ0
, In<ELFT>::RelaDyn->getParent(),
1084
10
         Entry::SecSize});
1085
10
    add({IsRela ? 
DT_RELAENT10
:
DT_RELENT0
,
1086
10
         uint64_t(IsRela ? 
sizeof(Elf_Rela)10
:
sizeof(Elf_Rel)0
)});
1087
10
1088
10
    // MIPS dynamic loader does not support RELCOUNT tag.
1089
10
    // The problem is in the tight relation between dynamic
1090
10
    // relocations and GOT. So do not emit this tag on MIPS.
1091
10
    if (
Config->EMachine != EM_MIPS10
) {
1092
5
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1093
5
      if (
Config->ZCombreloc && 5
NumRelativeRels5
)
1094
3
        
add({IsRela ? 3
DT_RELACOUNT3
:
DT_RELCOUNT0
, NumRelativeRels});
1095
5
    }
1096
10
  }
1097
25
  if (
In<ELFT>::RelaPlt->getParent() && 25
!In<ELFT>::RelaPlt->empty()25
) {
1098
2
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1099
2
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1100
2
    switch (Config->EMachine) {
1101
0
    case EM_MIPS:
1102
0
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1103
0
      break;
1104
0
    case EM_SPARCV9:
1105
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1106
0
      break;
1107
2
    default:
1108
2
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1109
2
      break;
1110
2
    }
1111
2
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 2
DT_RELA2
:
DT_REL0
)});
1112
2
  }
1113
25
1114
25
  add({DT_SYMTAB, InX::DynSymTab});
1115
25
  add({DT_SYMENT, sizeof(Elf_Sym)});
1116
25
  add({DT_STRTAB, InX::DynStrTab});
1117
25
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1118
25
  if (!Config->ZText)
1119
2
    add({DT_TEXTREL, (uint64_t)0});
1120
25
  if (InX::GnuHashTab)
1121
1
    add({DT_GNU_HASH, InX::GnuHashTab});
1122
25
  if (InX::HashTab)
1123
25
    add({DT_HASH, InX::HashTab});
1124
25
1125
25
  if (
Out::PreinitArray25
) {
1126
0
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1127
0
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1128
0
  }
1129
25
  if (
Out::InitArray25
) {
1130
0
    add({DT_INIT_ARRAY, Out::InitArray});
1131
0
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1132
0
  }
1133
25
  if (
Out::FiniArray25
) {
1134
0
    add({DT_FINI_ARRAY, Out::FiniArray});
1135
0
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1136
0
  }
1137
25
1138
25
  if (SymbolBody *B = Symtab->find(Config->Init))
1139
0
    
if (0
B->isInCurrentDSO()0
)
1140
0
      add({DT_INIT, B});
1141
25
  if (SymbolBody *B = Symtab->find(Config->Fini))
1142
0
    
if (0
B->isInCurrentDSO()0
)
1143
0
      add({DT_FINI, B});
1144
25
1145
25
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1146
25
  if (
HasVerNeed || 25
In<ELFT>::VerDef25
)
1147
0
    add({DT_VERSYM, In<ELFT>::VerSym});
1148
25
  if (
In<ELFT>::VerDef25
) {
1149
0
    add({DT_VERDEF, In<ELFT>::VerDef});
1150
0
    add({DT_VERDEFNUM, getVerDefNum()});
1151
0
  }
1152
25
  if (
HasVerNeed25
) {
1153
0
    add({DT_VERNEED, In<ELFT>::VerNeed});
1154
0
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1155
0
  }
1156
25
1157
25
  if (
Config->EMachine == EM_MIPS25
) {
1158
16
    add({DT_MIPS_RLD_VERSION, 1});
1159
16
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1160
16
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1161
16
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1162
16
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1163
16
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1164
5
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1165
16
    else
1166
11
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1167
16
    add({DT_PLTGOT, InX::MipsGot});
1168
16
    if (InX::MipsRldMap)
1169
5
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1170
16
  }
1171
25
1172
25
  getParent()->Link = this->Link;
1173
25
1174
25
  // +1 for DT_NULL
1175
25
  this->Size = (Entries.size() + 1) * this->Entsize;
1176
25
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1075
582
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1076
582
  if (this->Size)
1077
0
    return; // Already finalized.
1078
582
1079
582
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1080
582
  if (
In<ELFT>::RelaDyn->getParent() && 582
!In<ELFT>::RelaDyn->empty()582
) {
1081
95
    bool IsRela = Config->IsRela;
1082
95
    add({IsRela ? 
DT_RELA95
:
DT_REL0
, In<ELFT>::RelaDyn});
1083
95
    add({IsRela ? 
DT_RELASZ95
:
DT_RELSZ0
, In<ELFT>::RelaDyn->getParent(),
1084
95
         Entry::SecSize});
1085
95
    add({IsRela ? 
DT_RELAENT95
:
DT_RELENT0
,
1086
95
         uint64_t(IsRela ? 
sizeof(Elf_Rela)95
:
sizeof(Elf_Rel)0
)});
1087
95
1088
95
    // MIPS dynamic loader does not support RELCOUNT tag.
1089
95
    // The problem is in the tight relation between dynamic
1090
95
    // relocations and GOT. So do not emit this tag on MIPS.
1091
95
    if (
Config->EMachine != EM_MIPS95
) {
1092
95
      size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1093
95
      if (
Config->ZCombreloc && 95
NumRelativeRels94
)
1094
17
        
add({IsRela ? 17
DT_RELACOUNT17
:
DT_RELCOUNT0
, NumRelativeRels});
1095
95
    }
1096
95
  }
1097
582
  if (
In<ELFT>::RelaPlt->getParent() && 582
!In<ELFT>::RelaPlt->empty()582
) {
1098
100
    add({DT_JMPREL, In<ELFT>::RelaPlt});
1099
100
    add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent(), Entry::SecSize});
1100
100
    switch (Config->EMachine) {
1101
0
    case EM_MIPS:
1102
0
      add({DT_MIPS_PLTGOT, In<ELFT>::GotPlt});
1103
0
      break;
1104
0
    case EM_SPARCV9:
1105
0
      add({DT_PLTGOT, In<ELFT>::Plt});
1106
0
      break;
1107
100
    default:
1108
100
      add({DT_PLTGOT, In<ELFT>::GotPlt});
1109
100
      break;
1110
100
    }
1111
100
    
add({DT_PLTREL, uint64_t(Config->IsRela ? 100
DT_RELA100
:
DT_REL0
)});
1112
100
  }
1113
582
1114
582
  add({DT_SYMTAB, InX::DynSymTab});
1115
582
  add({DT_SYMENT, sizeof(Elf_Sym)});
1116
582
  add({DT_STRTAB, InX::DynStrTab});
1117
582
  add({DT_STRSZ, InX::DynStrTab->getSize()});
1118
582
  if (!Config->ZText)
1119
3
    add({DT_TEXTREL, (uint64_t)0});
1120
582
  if (InX::GnuHashTab)
1121
2
    add({DT_GNU_HASH, InX::GnuHashTab});
1122
582
  if (InX::HashTab)
1123
580
    add({DT_HASH, InX::HashTab});
1124
582
1125
582
  if (
Out::PreinitArray582
) {
1126
3
    add({DT_PREINIT_ARRAY, Out::PreinitArray});
1127
3
    add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1128
3
  }
1129
582
  if (
Out::InitArray582
) {
1130
3
    add({DT_INIT_ARRAY, Out::InitArray});
1131
3
    add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1132
3
  }
1133
582
  if (
Out::FiniArray582
) {
1134
2
    add({DT_FINI_ARRAY, Out::FiniArray});
1135
2
    add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1136
2
  }
1137
582
1138
582
  if (SymbolBody *B = Symtab->find(Config->Init))
1139
6
    
if (6
B->isInCurrentDSO()6
)
1140
4
      add({DT_INIT, B});
1141
582
  if (SymbolBody *B = Symtab->find(Config->Fini))
1142
6
    
if (6
B->isInCurrentDSO()6
)
1143
4
      add({DT_FINI, B});
1144
582
1145
582
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1146
582
  if (
HasVerNeed || 582
In<ELFT>::VerDef576
)
1147
41
    add({DT_VERSYM, In<ELFT>::VerSym});
1148
582
  if (
In<ELFT>::VerDef582
) {
1149
36
    add({DT_VERDEF, In<ELFT>::VerDef});
1150
36
    add({DT_VERDEFNUM, getVerDefNum()});
1151
36
  }
1152
582
  if (
HasVerNeed582
) {
1153
6
    add({DT_VERNEED, In<ELFT>::VerNeed});
1154
6
    add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1155
6
  }
1156
582
1157
582
  if (
Config->EMachine == EM_MIPS582
) {
1158
0
    add({DT_MIPS_RLD_VERSION, 1});
1159
0
    add({DT_MIPS_FLAGS, RHF_NOTPOT});
1160
0
    add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1161
0
    add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1162
0
    add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1163
0
    if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1164
0
      add({DT_MIPS_GOTSYM, B->DynsymIndex});
1165
0
    else
1166
0
      add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1167
0
    add({DT_PLTGOT, InX::MipsGot});
1168
0
    if (InX::MipsRldMap)
1169
0
      add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1170
0
  }
1171
582
1172
582
  getParent()->Link = this->Link;
1173
582
1174
582
  // +1 for DT_NULL
1175
582
  this->Size = (Entries.size() + 1) * this->Entsize;
1176
582
}
1177
1178
763
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1179
763
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1180
763
1181
5.95k
  for (const Entry &E : Entries) {
1182
5.95k
    P->d_tag = E.Tag;
1183
5.95k
    switch (E.Kind) {
1184
8
    case Entry::SecAddr:
1185
8
      P->d_un.d_ptr = E.OutSec->Addr;
1186
8
      break;
1187
2.88k
    case Entry::InSecAddr:
1188
2.88k
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1189
2.88k
      break;
1190
287
    case Entry::SecSize:
1191
287
      P->d_un.d_val = E.OutSec->Size;
1192
287
      break;
1193
8
    case Entry::SymAddr:
1194
8
      P->d_un.d_ptr = E.Sym->getVA();
1195
8
      break;
1196
2.77k
    case Entry::PlainInt:
1197
2.77k
      P->d_un.d_val = E.Val;
1198
2.77k
      break;
1199
5.95k
    }
1200
5.95k
    ++P;
1201
5.95k
  }
1202
763
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1178
580
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1179
580
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1180
580
1181
4.06k
  for (const Entry &E : Entries) {
1182
4.06k
    P->d_tag = E.Tag;
1183
4.06k
    switch (E.Kind) {
1184
8
    case Entry::SecAddr:
1185
8
      P->d_un.d_ptr = E.OutSec->Addr;
1186
8
      break;
1187
2.11k
    case Entry::InSecAddr:
1188
2.11k
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1189
2.11k
      break;
1190
202
    case Entry::SecSize:
1191
202
      P->d_un.d_val = E.OutSec->Size;
1192
202
      break;
1193
8
    case Entry::SymAddr:
1194
8
      P->d_un.d_ptr = E.Sym->getVA();
1195
8
      break;
1196
1.72k
    case Entry::PlainInt:
1197
1.72k
      P->d_un.d_val = E.Val;
1198
1.72k
      break;
1199
4.06k
    }
1200
4.06k
    ++P;
1201
4.06k
  }
1202
580
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1178
25
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1179
25
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1180
25
1181
301
  for (const Entry &E : Entries) {
1182
301
    P->d_tag = E.Tag;
1183
301
    switch (E.Kind) {
1184
0
    case Entry::SecAddr:
1185
0
      P->d_un.d_ptr = E.OutSec->Addr;
1186
0
      break;
1187
111
    case Entry::InSecAddr:
1188
111
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1189
111
      break;
1190
12
    case Entry::SecSize:
1191
12
      P->d_un.d_val = E.OutSec->Size;
1192
12
      break;
1193
0
    case Entry::SymAddr:
1194
0
      P->d_un.d_ptr = E.Sym->getVA();
1195
0
      break;
1196
178
    case Entry::PlainInt:
1197
178
      P->d_un.d_val = E.Val;
1198
178
      break;
1199
301
    }
1200
301
    ++P;
1201
301
  }
1202
25
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1178
54
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1179
54
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1180
54
1181
727
  for (const Entry &E : Entries) {
1182
727
    P->d_tag = E.Tag;
1183
727
    switch (E.Kind) {
1184
0
    case Entry::SecAddr:
1185
0
      P->d_un.d_ptr = E.OutSec->Addr;
1186
0
      break;
1187
246
    case Entry::InSecAddr:
1188
246
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1189
246
      break;
1190
12
    case Entry::SecSize:
1191
12
      P->d_un.d_val = E.OutSec->Size;
1192
12
      break;
1193
0
    case Entry::SymAddr:
1194
0
      P->d_un.d_ptr = E.Sym->getVA();
1195
0
      break;
1196
469
    case Entry::PlainInt:
1197
469
      P->d_un.d_val = E.Val;
1198
469
      break;
1199
727
    }
1200
727
    ++P;
1201
727
  }
1202
54
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1178
104
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1179
104
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1180
104
1181
864
  for (const Entry &E : Entries) {
1182
864
    P->d_tag = E.Tag;
1183
864
    switch (E.Kind) {
1184
0
    case Entry::SecAddr:
1185
0
      P->d_un.d_ptr = E.OutSec->Addr;
1186
0
      break;
1187
407
    case Entry::InSecAddr:
1188
407
      P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1189
407
      break;
1190
61
    case Entry::SecSize:
1191
61
      P->d_un.d_val = E.OutSec->Size;
1192
61
      break;
1193
0
    case Entry::SymAddr:
1194
0
      P->d_un.d_ptr = E.Sym->getVA();
1195
0
      break;
1196
396
    case Entry::PlainInt:
1197
396
      P->d_un.d_val = E.Val;
1198
396
      break;
1199
864
    }
1200
864
    ++P;
1201
864
  }
1202
104
}
1203
1204
530
uint64_t DynamicReloc::getOffset() const {
1205
530
  return InputSec->getOutputSection()->Addr + InputSec->getOffset(OffsetInSec);
1206
530
}
1207
1208
362
int64_t DynamicReloc::getAddend() const {
1209
362
  if (UseSymVA)
1210
59
    return Sym->getVA(Addend);
1211
303
  return Addend;
1212
303
}
1213
1214
530
uint32_t DynamicReloc::getSymIndex() const {
1215
530
  if (
Sym && 530
!UseSymVA525
)
1216
435
    return Sym->DynsymIndex;
1217
95
  return 0;
1218
95
}
1219
1220
template <class ELFT>
1221
RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
1222
    : SyntheticSection(SHF_ALLOC, Config->IsRela ? SHT_RELA : SHT_REL,
1223
                       Config->Wordsize, Name),
1224
4.79k
      Sort(Sort) {
1225
4.79k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)3.94k
:
sizeof(Elf_Rel)852
;
1226
4.79k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1224
285
      Sort(Sort) {
1225
285
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)3
:
sizeof(Elf_Rel)282
;
1226
285
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1224
132
      Sort(Sort) {
1225
132
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)132
:
sizeof(Elf_Rel)0
;
1226
132
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1224
579
      Sort(Sort) {
1225
579
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)9
:
sizeof(Elf_Rel)570
;
1226
579
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1224
3.79k
      Sort(Sort) {
1225
3.79k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)3.79k
:
sizeof(Elf_Rel)0
;
1226
3.79k
}
1227
1228
template <class ELFT>
1229
547
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1230
547
  if (Reloc.Type == Target->RelativeRel)
1231
60
    ++NumRelativeRelocs;
1232
547
  Relocs.push_back(Reloc);
1233
547
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1229
144
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1230
144
  if (Reloc.Type == Target->RelativeRel)
1231
18
    ++NumRelativeRelocs;
1232
144
  Relocs.push_back(Reloc);
1233
144
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1229
25
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1230
25
  if (Reloc.Type == Target->RelativeRel)
1231
2
    ++NumRelativeRelocs;
1232
25
  Relocs.push_back(Reloc);
1233
25
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1229
348
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1230
348
  if (Reloc.Type == Target->RelativeRel)
1231
26
    ++NumRelativeRelocs;
1232
348
  Relocs.push_back(Reloc);
1233
348
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addReloc(lld::elf::DynamicReloc const&)
Line
Count
Source
1229
30
void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1230
30
  if (Reloc.Type == Target->RelativeRel)
1231
14
    ++NumRelativeRelocs;
1232
30
  Relocs.push_back(Reloc);
1233
30
}
1234
1235
template <class ELFT, class RelTy>
1236
243
static bool compRelocations(const RelTy &A, const RelTy &B) {
1237
243
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
243
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1239
243
  if (AIsRel != BIsRel)
1240
28
    return AIsRel;
1241
215
1242
215
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1243
215
}
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
1236
109
static bool compRelocations(const RelTy &A, const RelTy &B) {
1237
109
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
109
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1239
109
  if (AIsRel != BIsRel)
1240
10
    return AIsRel;
1241
99
1242
99
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1243
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
1236
22
static bool compRelocations(const RelTy &A, const RelTy &B) {
1237
22
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
22
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1239
22
  if (AIsRel != BIsRel)
1240
0
    return AIsRel;
1241
22
1242
22
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1243
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&)
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
1236
83
static bool compRelocations(const RelTy &A, const RelTy &B) {
1237
83
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
83
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1239
83
  if (AIsRel != BIsRel)
1240
17
    return AIsRel;
1241
66
1242
66
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1243
66
}
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&)
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&)
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
1236
29
static bool compRelocations(const RelTy &A, const RelTy &B) {
1237
29
  bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1238
29
  bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1239
29
  if (AIsRel != BIsRel)
1240
1
    return AIsRel;
1241
28
1242
28
  return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1243
28
}
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&)
1244
1245
294
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1246
294
  uint8_t *BufBegin = Buf;
1247
530
  for (const DynamicReloc &Rel : Relocs) {
1248
530
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1249
530
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)362
:
sizeof(Elf_Rel)168
;
1250
530
1251
530
    if (Config->IsRela)
1252
362
      P->r_addend = Rel.getAddend();
1253
530
    P->r_offset = Rel.getOffset();
1254
530
    if (
Config->EMachine == EM_MIPS && 530
Rel.getInputSec() == InX::MipsGot43
)
1255
530
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1256
530
      // allocated in the end of the GOT. We need to adjust the offset to take
1257
530
      // in account 'local' and 'global' GOT entries.
1258
20
      P->r_offset += InX::MipsGot->getTlsOffset();
1259
530
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1260
530
  }
1261
294
1262
294
  if (
Sort294
) {
1263
146
    if (Config->IsRela)
1264
103
      std::stable_sort((Elf_Rela *)BufBegin,
1265
103
                       (Elf_Rela *)BufBegin + Relocs.size(),
1266
103
                       compRelocations<ELFT, Elf_Rela>);
1267
146
    else
1268
43
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1269
43
                       compRelocations<ELFT, Elf_Rel>);
1270
146
  }
1271
294
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1245
12
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1246
12
  uint8_t *BufBegin = Buf;
1247
25
  for (const DynamicReloc &Rel : Relocs) {
1248
25
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1249
25
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)0
:
sizeof(Elf_Rel)25
;
1250
25
1251
25
    if (Config->IsRela)
1252
0
      P->r_addend = Rel.getAddend();
1253
25
    P->r_offset = Rel.getOffset();
1254
25
    if (
Config->EMachine == EM_MIPS && 25
Rel.getInputSec() == InX::MipsGot25
)
1255
25
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1256
25
      // allocated in the end of the GOT. We need to adjust the offset to take
1257
25
      // in account 'local' and 'global' GOT entries.
1258
10
      P->r_offset += InX::MipsGot->getTlsOffset();
1259
25
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1260
25
  }
1261
12
1262
12
  if (
Sort12
) {
1263
6
    if (Config->IsRela)
1264
0
      std::stable_sort((Elf_Rela *)BufBegin,
1265
0
                       (Elf_Rela *)BufBegin + Relocs.size(),
1266
0
                       compRelocations<ELFT, Elf_Rela>);
1267
6
    else
1268
6
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1269
6
                       compRelocations<ELFT, Elf_Rel>);
1270
6
  }
1271
12
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1245
12
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1246
12
  uint8_t *BufBegin = Buf;
1247
30
  for (const DynamicReloc &Rel : Relocs) {
1248
30
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1249
30
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)30
:
sizeof(Elf_Rel)0
;
1250
30
1251
30
    if (Config->IsRela)
1252
30
      P->r_addend = Rel.getAddend();
1253
30
    P->r_offset = Rel.getOffset();
1254
30
    if (
Config->EMachine == EM_MIPS && 30
Rel.getInputSec() == InX::MipsGot14
)
1255
30
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1256
30
      // allocated in the end of the GOT. We need to adjust the offset to take
1257
30
      // in account 'local' and 'global' GOT entries.
1258
10
      P->r_offset += InX::MipsGot->getTlsOffset();
1259
30
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1260
30
  }
1261
12
1262
12
  if (
Sort12
) {
1263
10
    if (Config->IsRela)
1264
10
      std::stable_sort((Elf_Rela *)BufBegin,
1265
10
                       (Elf_Rela *)BufBegin + Relocs.size(),
1266
10
                       compRelocations<ELFT, Elf_Rela>);
1267
10
    else
1268
0
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1269
0
                       compRelocations<ELFT, Elf_Rel>);
1270
10
  }
1271
12
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1245
203
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1246
203
  uint8_t *BufBegin = Buf;
1247
331
  for (const DynamicReloc &Rel : Relocs) {
1248
331
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1249
331
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)331
:
sizeof(Elf_Rel)0
;
1250
331
1251
331
    if (Config->IsRela)
1252
331
      P->r_addend = Rel.getAddend();
1253
331
    P->r_offset = Rel.getOffset();
1254
331
    if (
Config->EMachine == EM_MIPS && 331
Rel.getInputSec() == InX::MipsGot0
)
1255
331
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1256
331
      // allocated in the end of the GOT. We need to adjust the offset to take
1257
331
      // in account 'local' and 'global' GOT entries.
1258
0
      P->r_offset += InX::MipsGot->getTlsOffset();
1259
331
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1260
331
  }
1261
203
1262
203
  if (
Sort203
) {
1263
93
    if (Config->IsRela)
1264
93
      std::stable_sort((Elf_Rela *)BufBegin,
1265
93
                       (Elf_Rela *)BufBegin + Relocs.size(),
1266
93
                       compRelocations<ELFT, Elf_Rela>);
1267
93
    else
1268
0
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1269
0
                       compRelocations<ELFT, Elf_Rel>);
1270
93
  }
1271
203
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1245
67
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1246
67
  uint8_t *BufBegin = Buf;
1247
144
  for (const DynamicReloc &Rel : Relocs) {
1248
144
    auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1249
144
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)143
;
1250
144
1251
144
    if (Config->IsRela)
1252
1
      P->r_addend = Rel.getAddend();
1253
144
    P->r_offset = Rel.getOffset();
1254
144
    if (
Config->EMachine == EM_MIPS && 144
Rel.getInputSec() == InX::MipsGot4
)
1255
144
      // Dynamic relocation against MIPS GOT section make deal TLS entries
1256
144
      // allocated in the end of the GOT. We need to adjust the offset to take
1257
144
      // in account 'local' and 'global' GOT entries.
1258
0
      P->r_offset += InX::MipsGot->getTlsOffset();
1259
144
    P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1260
144
  }
1261
67
1262
67
  if (
Sort67
) {
1263
37
    if (Config->IsRela)
1264
0
      std::stable_sort((Elf_Rela *)BufBegin,
1265
0
                       (Elf_Rela *)BufBegin + Relocs.size(),
1266
0
                       compRelocations<ELFT, Elf_Rela>);
1267
37
    else
1268
37
      std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1269
37
                       compRelocations<ELFT, Elf_Rel>);
1270
37
  }
1271
67
}
1272
1273
214
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1274
214
  return this->Entsize * Relocs.size();
1275
214
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getRelocOffset()
Line
Count
Source
1273
2
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1274
2
  return this->Entsize * Relocs.size();
1275
2
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getRelocOffset()
Line
Count
Source
1273
54
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1274
54
  return this->Entsize * Relocs.size();
1275
54
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getRelocOffset()
Line
Count
Source
1273
150
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1274
150
  return this->Entsize * Relocs.size();
1275
150
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getRelocOffset()
Line
Count
Source
1273
8
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1274
8
  return this->Entsize * Relocs.size();
1275
8
}
1276
1277
295
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1278
286
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1279
9
                              : InX::SymTab->getParent()->SectionIndex;
1280
295
1281
295
  // Set required output section properties.
1282
295
  getParent()->Link = this->Link;
1283
295
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1277
12
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1278
12
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1279
0
                              : InX::SymTab->getParent()->SectionIndex;
1280
12
1281
12
  // Set required output section properties.
1282
12
  getParent()->Link = this->Link;
1283
12
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1277
67
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1278
63
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1279
4
                              : InX::SymTab->getParent()->SectionIndex;
1280
67
1281
67
  // Set required output section properties.
1282
67
  getParent()->Link = this->Link;
1283
67
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1277
204
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1278
199
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1279
5
                              : InX::SymTab->getParent()->SectionIndex;
1280
204
1281
204
  // Set required output section properties.
1282
204
  getParent()->Link = this->Link;
1283
204
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1277
12
template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1278
12
  this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1279
0
                              : InX::SymTab->getParent()->SectionIndex;
1280
12
1281
12
  // Set required output section properties.
1282
12
  getParent()->Link = this->Link;
1283
12
}
1284
1285
SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &StrTabSec)
1286
    : SyntheticSection(StrTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1287
                       StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1288
                       Config->Wordsize,
1289
                       StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1290
2.40k
      StrTabSec(StrTabSec) {}
1291
1292
// Orders symbols according to their positions in the GOT,
1293
// in compliance with MIPS ABI rules.
1294
// See "Global Offset Table" in Chapter 5 in the following document
1295
// for detailed description:
1296
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1297
static bool sortMipsSymbols(const SymbolTableEntry &L,
1298
115
                            const SymbolTableEntry &R) {
1299
115
  // Sort entries related to non-local preemptible symbols by GOT indexes.
1300
115
  // All other entries go to the first part of GOT in arbitrary order.
1301
115
  bool LIsInLocalGot = !L.Symbol->IsInGlobalMipsGot;
1302
115
  bool RIsInLocalGot = !R.Symbol->IsInGlobalMipsGot;
1303
115
  if (
LIsInLocalGot || 115
RIsInLocalGot20
)
1304
111
    return !RIsInLocalGot;
1305
4
  return L.Symbol->GotIndex < R.Symbol->GotIndex;
1306
4
}
1307
1308
// Finalize a symbol table. The ELF spec requires that all local
1309
// symbols precede global symbols, so we sort symbol entries in this
1310
// function. (For .dynsym, we don't do that because symbols for
1311
// dynamic linking are inherently all globals.)
1312
2.29k
void SymbolTableBaseSection::finalizeContents() {
1313
2.29k
  getParent()->Link = StrTabSec.getParent()->SectionIndex;
1314
2.29k
1315
2.29k
  // If it is a .dynsym, there should be no local symbols, but we need
1316
2.29k
  // to do a few things for the dynamic linker.
1317
2.29k
  if (
this->Type == SHT_DYNSYM2.29k
) {
1318
765
    // Section's Info field has the index of the first non-local symbol.
1319
765
    // Because the first symbol entry is a null entry, 1 is the first.
1320
765
    getParent()->Info = 1;
1321
765
1322
765
    if (
InX::GnuHashTab765
) {
1323
5
      // NB: It also sorts Symbols to meet the GNU hash table requirements.
1324
5
      InX::GnuHashTab->addSymbols(Symbols);
1325
765
    } else 
if (760
Config->EMachine == EM_MIPS760
) {
1326
77
      std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
1327
77
    }
1328
765
1329
765
    size_t I = 0;
1330
765
    for (const SymbolTableEntry &S : Symbols)
1331
1.25k
      S.Symbol->DynsymIndex = ++I;
1332
765
    return;
1333
765
  }
1334
1.53k
}
1335
1336
1.53k
void SymbolTableBaseSection::postThunkContents() {
1337
1.53k
  if (this->Type == SHT_DYNSYM)
1338
0
    return;
1339
1.53k
  // move all local symbols before global symbols.
1340
1.53k
  auto It = std::stable_partition(
1341
5.08k
      Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) {
1342
5.08k
        return S.Symbol->isLocal() ||
1343
4.11k
               S.Symbol->symbol()->computeBinding() == STB_LOCAL;
1344
5.08k
      });
1345
1.53k
  size_t NumLocals = It - Symbols.begin();
1346
1.53k
  getParent()->Info = NumLocals + 1;
1347
1.53k
}
1348
1349
6.59k
void SymbolTableBaseSection::addSymbol(SymbolBody *B) {
1350
6.59k
  // Adding a local symbol to a .dynsym is a bug.
1351
6.59k
  assert(this->Type != SHT_DYNSYM || !B->isLocal());
1352
6.59k
1353
6.59k
  bool HashIt = B->isLocal();
1354
6.59k
  Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)});
1355
6.59k
}
1356
1357
106
size_t SymbolTableBaseSection::getSymbolIndex(SymbolBody *Body) {
1358
106
  // Initializes symbol lookup tables lazily. This is used only
1359
106
  // for -r or -emit-relocs.
1360
42
  llvm::call_once(OnceFlag, [&] {
1361
42
    SymbolIndexMap.reserve(Symbols.size());
1362
42
    size_t I = 0;
1363
242
    for (const SymbolTableEntry &E : Symbols) {
1364
242
      if (E.Symbol->Type == STT_SECTION)
1365
113
        SectionIndexMap[E.Symbol->getOutputSection()] = ++I;
1366
242
      else
1367
129
        SymbolIndexMap[E.Symbol] = ++I;
1368
242
    }
1369
42
  });
1370
106
1371
106
  // Section symbols are mapped based on their output sections
1372
106
  // to maintain their semantics.
1373
106
  if (Body->Type == STT_SECTION)
1374
47
    return SectionIndexMap.lookup(Body->getOutputSection());
1375
59
  return SymbolIndexMap.lookup(Body);
1376
59
}
1377
1378
template <class ELFT>
1379
SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &StrTabSec)
1380
2.40k
    : SymbolTableBaseSection(StrTabSec) {
1381
2.40k
  this->Entsize = sizeof(Elf_Sym);
1382
2.40k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1380
69
    : SymbolTableBaseSection(StrTabSec) {
1381
69
  this->Entsize = sizeof(Elf_Sym);
1382
69
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1380
301
    : SymbolTableBaseSection(StrTabSec) {
1381
301
  this->Entsize = sizeof(Elf_Sym);
1382
301
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1380
149
    : SymbolTableBaseSection(StrTabSec) {
1381
149
  this->Entsize = sizeof(Elf_Sym);
1382
149
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1380
1.88k
    : SymbolTableBaseSection(StrTabSec) {
1381
1.88k
  this->Entsize = sizeof(Elf_Sym);
1382
1.88k
}
1383
1384
// Write the internal symbol table contents to the output symbol table.
1385
2.27k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1386
2.27k
  // The first entry is a null entry as per the ELF spec.
1387
2.27k
  Buf += sizeof(Elf_Sym);
1388
2.27k
1389
2.27k
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1390
2.27k
1391
6.30k
  for (SymbolTableEntry &Ent : Symbols) {
1392
6.30k
    SymbolBody *Body = Ent.Symbol;
1393
6.30k
1394
6.30k
    // Set st_info and st_other.
1395
6.30k
    if (
Body->isLocal()6.30k
) {
1396
963
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1397
6.30k
    } else {
1398
5.34k
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1399
5.34k
      ESym->setVisibility(Body->symbol()->Visibility);
1400
5.34k
    }
1401
6.30k
1402
6.30k
    ESym->st_name = Ent.StrTabOffset;
1403
6.30k
1404
6.30k
    // Set a section index.
1405
6.30k
    if (const OutputSection *OutSec = Body->getOutputSection())
1406
5.24k
      ESym->st_shndx = OutSec->SectionIndex;
1407
1.06k
    else 
if (1.06k
isa<DefinedRegular>(Body)1.06k
)
1408
322
      ESym->st_shndx = SHN_ABS;
1409
738
    else 
if (738
isa<DefinedCommon>(Body)738
)
1410
2
      ESym->st_shndx = SHN_COMMON;
1411
6.30k
1412
6.30k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1413
6.30k
    // for undefined symbols, so whether copying it or not is up to us if that's
1414
6.30k
    // the case. We'll leave it as zero because by not setting a value, we can
1415
6.30k
    // get the exact same outputs for two sets of input files that differ only
1416
6.30k
    // in undefined symbol size in DSOs.
1417
6.30k
    if (ESym->st_shndx != SHN_UNDEF)
1418
5.57k
      ESym->st_size = Body->getSize<ELFT>();
1419
6.30k
1420
6.30k
    // st_value is usually an address of a symbol, but that has a
1421
6.30k
    // special meaining for uninstantiated common symbols (this can
1422
6.30k
    // occur if -r is given).
1423
6.30k
    if (
!Config->DefineCommon && 6.30k
isa<DefinedCommon>(Body)291
)
1424
2
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1425
6.30k
    else
1426
6.30k
      ESym->st_value = Body->getVA();
1427
6.30k
1428
6.30k
    ++ESym;
1429
6.30k
  }
1430
2.27k
1431
2.27k
  // On MIPS we need to mark symbol which has a PLT entry and requires
1432
2.27k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1433
2.27k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1434
2.27k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1435
2.27k
  if (
Config->EMachine == EM_MIPS2.27k
) {
1436
212
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1437
212
1438
867
    for (SymbolTableEntry &Ent : Symbols) {
1439
867
      SymbolBody *Body = Ent.Symbol;
1440
867
      if (
Body->isInPlt() && 867
Body->NeedsPltAddr20
)
1441
6
        ESym->st_other |= STO_MIPS_PLT;
1442
867
1443
867
      if (Config->Relocatable)
1444
49
        
if (auto *49
D49
= dyn_cast<DefinedRegular>(Body))
1445
49
          
if (49
D->isMipsPIC<ELFT>()49
)
1446
6
            ESym->st_other |= STO_MIPS_PIC;
1447
867
      ++ESym;
1448
867
    }
1449
212
  }
1450
2.27k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1385
69
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1386
69
  // The first entry is a null entry as per the ELF spec.
1387
69
  Buf += sizeof(Elf_Sym);
1388
69
1389
69
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1390
69
1391
236
  for (SymbolTableEntry &Ent : Symbols) {
1392
236
    SymbolBody *Body = Ent.Symbol;
1393
236
1394
236
    // Set st_info and st_other.
1395
236
    if (
Body->isLocal()236
) {
1396
18
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1397
236
    } else {
1398
218
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1399
218
      ESym->setVisibility(Body->symbol()->Visibility);
1400
218
    }
1401
236
1402
236
    ESym->st_name = Ent.StrTabOffset;
1403
236
1404
236
    // Set a section index.
1405
236
    if (const OutputSection *OutSec = Body->getOutputSection())
1406
204
      ESym->st_shndx = OutSec->SectionIndex;
1407
32
    else 
if (32
isa<DefinedRegular>(Body)32
)
1408
5
      ESym->st_shndx = SHN_ABS;
1409
27
    else 
if (27
isa<DefinedCommon>(Body)27
)
1410
0
      ESym->st_shndx = SHN_COMMON;
1411
236
1412
236
    // Copy symbol size if it is a defined symbol. st_size is not significant
1413
236
    // for undefined symbols, so whether copying it or not is up to us if that's
1414
236
    // the case. We'll leave it as zero because by not setting a value, we can
1415
236
    // get the exact same outputs for two sets of input files that differ only
1416
236
    // in undefined symbol size in DSOs.
1417
236
    if (ESym->st_shndx != SHN_UNDEF)
1418
209
      ESym->st_size = Body->getSize<ELFT>();
1419
236
1420
236
    // st_value is usually an address of a symbol, but that has a
1421
236
    // special meaining for uninstantiated common symbols (this can
1422
236
    // occur if -r is given).
1423
236
    if (
!Config->DefineCommon && 236
isa<DefinedCommon>(Body)5
)
1424
0
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1425
236
    else
1426
236
      ESym->st_value = Body->getVA();
1427
236
1428
236
    ++ESym;
1429
236
  }
1430
69
1431
69
  // On MIPS we need to mark symbol which has a PLT entry and requires
1432
69
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1433
69
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1434
69
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1435
69
  if (
Config->EMachine == EM_MIPS69
) {
1436
44
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1437
44
1438
169
    for (SymbolTableEntry &Ent : Symbols) {
1439
169
      SymbolBody *Body = Ent.Symbol;
1440
169
      if (
Body->isInPlt() && 169
Body->NeedsPltAddr0
)
1441
0
        ESym->st_other |= STO_MIPS_PLT;
1442
169
1443
169
      if (Config->Relocatable)
1444
5
        
if (auto *5
D5
= dyn_cast<DefinedRegular>(Body))
1445
5
          
if (5
D->isMipsPIC<ELFT>()5
)
1446
0
            ESym->st_other |= STO_MIPS_PIC;
1447
169
      ++ESym;
1448
169
    }
1449
44
  }
1450
69
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1385
292
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1386
292
  // The first entry is a null entry as per the ELF spec.
1387
292
  Buf += sizeof(Elf_Sym);
1388
292
1389
292
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1390
292
1391
1.19k
  for (SymbolTableEntry &Ent : Symbols) {
1392
1.19k
    SymbolBody *Body = Ent.Symbol;
1393
1.19k
1394
1.19k
    // Set st_info and st_other.
1395
1.19k
    if (
Body->isLocal()1.19k
) {
1396
405
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1397
1.19k
    } else {
1398
791
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1399
791
      ESym->setVisibility(Body->symbol()->Visibility);
1400
791
    }
1401
1.19k
1402
1.19k
    ESym->st_name = Ent.StrTabOffset;
1403
1.19k
1404
1.19k
    // Set a section index.
1405
1.19k
    if (const OutputSection *OutSec = Body->getOutputSection())
1406
1.03k
      ESym->st_shndx = OutSec->SectionIndex;
1407
162
    else 
if (162
isa<DefinedRegular>(Body)162
)
1408
41
      ESym->st_shndx = SHN_ABS;
1409
121
    else 
if (121
isa<DefinedCommon>(Body)121
)
1410
0
      ESym->st_shndx = SHN_COMMON;
1411
1.19k
1412
1.19k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1413
1.19k
    // for undefined symbols, so whether copying it or not is up to us if that's
1414
1.19k
    // the case. We'll leave it as zero because by not setting a value, we can
1415
1.19k
    // get the exact same outputs for two sets of input files that differ only
1416
1.19k
    // in undefined symbol size in DSOs.
1417
1.19k
    if (ESym->st_shndx != SHN_UNDEF)
1418
1.07k
      ESym->st_size = Body->getSize<ELFT>();
1419
1.19k
1420
1.19k
    // st_value is usually an address of a symbol, but that has a
1421
1.19k
    // special meaining for uninstantiated common symbols (this can
1422
1.19k
    // occur if -r is given).
1423
1.19k
    if (
!Config->DefineCommon && 1.19k
isa<DefinedCommon>(Body)46
)
1424
0
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1425
1.19k
    else
1426
1.19k
      ESym->st_value = Body->getVA();
1427
1.19k
1428
1.19k
    ++ESym;
1429
1.19k
  }
1430
292
1431
292
  // On MIPS we need to mark symbol which has a PLT entry and requires
1432
292
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1433
292
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1434
292
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1435
292
  if (
Config->EMachine == EM_MIPS292
) {
1436
22
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1437
22
1438
66
    for (SymbolTableEntry &Ent : Symbols) {
1439
66
      SymbolBody *Body = Ent.Symbol;
1440
66
      if (
Body->isInPlt() && 66
Body->NeedsPltAddr4
)
1441
0
        ESym->st_other |= STO_MIPS_PLT;
1442
66
1443
66
      if (Config->Relocatable)
1444
0
        
if (auto *0
D0
= dyn_cast<DefinedRegular>(Body))
1445
0
          
if (0
D->isMipsPIC<ELFT>()0
)
1446
0
            ESym->st_other |= STO_MIPS_PIC;
1447
66
      ++ESym;
1448
66
    }
1449
22
  }
1450
292
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1385
1.76k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1386
1.76k
  // The first entry is a null entry as per the ELF spec.
1387
1.76k
  Buf += sizeof(Elf_Sym);
1388
1.76k
1389
1.76k
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1390
1.76k
1391
4.23k
  for (SymbolTableEntry &Ent : Symbols) {
1392
4.23k
    SymbolBody *Body = Ent.Symbol;
1393
4.23k
1394
4.23k
    // Set st_info and st_other.
1395
4.23k
    if (
Body->isLocal()4.23k
) {
1396
435
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1397
4.23k
    } else {
1398
3.80k
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1399
3.80k
      ESym->setVisibility(Body->symbol()->Visibility);
1400
3.80k
    }
1401
4.23k
1402
4.23k
    ESym->st_name = Ent.StrTabOffset;
1403
4.23k
1404
4.23k
    // Set a section index.
1405
4.23k
    if (const OutputSection *OutSec = Body->getOutputSection())
1406
3.43k
      ESym->st_shndx = OutSec->SectionIndex;
1407
804
    else 
if (804
isa<DefinedRegular>(Body)804
)
1408
264
      ESym->st_shndx = SHN_ABS;
1409
540
    else 
if (540
isa<DefinedCommon>(Body)540
)
1410
2
      ESym->st_shndx = SHN_COMMON;
1411
4.23k
1412
4.23k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1413
4.23k
    // for undefined symbols, so whether copying it or not is up to us if that's
1414
4.23k
    // the case. We'll leave it as zero because by not setting a value, we can
1415
4.23k
    // get the exact same outputs for two sets of input files that differ only
1416
4.23k
    // in undefined symbol size in DSOs.
1417
4.23k
    if (ESym->st_shndx != SHN_UNDEF)
1418
3.70k
      ESym->st_size = Body->getSize<ELFT>();
1419
4.23k
1420
4.23k
    // st_value is usually an address of a symbol, but that has a
1421
4.23k
    // special meaining for uninstantiated common symbols (this can
1422
4.23k
    // occur if -r is given).
1423
4.23k
    if (
!Config->DefineCommon && 4.23k
isa<DefinedCommon>(Body)196
)
1424
2
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1425
4.23k
    else
1426
4.23k
      ESym->st_value = Body->getVA();
1427
4.23k
1428
4.23k
    ++ESym;
1429
4.23k
  }
1430
1.76k
1431
1.76k
  // On MIPS we need to mark symbol which has a PLT entry and requires
1432
1.76k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1433
1.76k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1434
1.76k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1435
1.76k
  if (
Config->EMachine == EM_MIPS1.76k
) {
1436
2
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1437
2
1438
4
    for (SymbolTableEntry &Ent : Symbols) {
1439
4
      SymbolBody *Body = Ent.Symbol;
1440
4
      if (
Body->isInPlt() && 4
Body->NeedsPltAddr0
)
1441
0
        ESym->st_other |= STO_MIPS_PLT;
1442
4
1443
4
      if (Config->Relocatable)
1444
0
        
if (auto *0
D0
= dyn_cast<DefinedRegular>(Body))
1445
0
          
if (0
D->isMipsPIC<ELFT>()0
)
1446
0
            ESym->st_other |= STO_MIPS_PIC;
1447
4
      ++ESym;
1448
4
    }
1449
2
  }
1450
1.76k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1385
147
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1386
147
  // The first entry is a null entry as per the ELF spec.
1387
147
  Buf += sizeof(Elf_Sym);
1388
147
1389
147
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1390
147
1391
636
  for (SymbolTableEntry &Ent : Symbols) {
1392
636
    SymbolBody *Body = Ent.Symbol;
1393
636
1394
636
    // Set st_info and st_other.
1395
636
    if (
Body->isLocal()636
) {
1396
105
      ESym->setBindingAndType(STB_LOCAL, Body->Type);
1397
636
    } else {
1398
531
      ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1399
531
      ESym->setVisibility(Body->symbol()->Visibility);
1400
531
    }
1401
636
1402
636
    ESym->st_name = Ent.StrTabOffset;
1403
636
1404
636
    // Set a section index.
1405
636
    if (const OutputSection *OutSec = Body->getOutputSection())
1406
574
      ESym->st_shndx = OutSec->SectionIndex;
1407
62
    else 
if (62
isa<DefinedRegular>(Body)62
)
1408
12
      ESym->st_shndx = SHN_ABS;
1409
50
    else 
if (50
isa<DefinedCommon>(Body)50
)
1410
0
      ESym->st_shndx = SHN_COMMON;
1411
636
1412
636
    // Copy symbol size if it is a defined symbol. st_size is not significant
1413
636
    // for undefined symbols, so whether copying it or not is up to us if that's
1414
636
    // the case. We'll leave it as zero because by not setting a value, we can
1415
636
    // get the exact same outputs for two sets of input files that differ only
1416
636
    // in undefined symbol size in DSOs.
1417
636
    if (ESym->st_shndx != SHN_UNDEF)
1418
586
      ESym->st_size = Body->getSize<ELFT>();
1419
636
1420
636
    // st_value is usually an address of a symbol, but that has a
1421
636
    // special meaining for uninstantiated common symbols (this can
1422
636
    // occur if -r is given).
1423
636
    if (
!Config->DefineCommon && 636
isa<DefinedCommon>(Body)44
)
1424
0
      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1425
636
    else
1426
636
      ESym->st_value = Body->getVA();
1427
636
1428
636
    ++ESym;
1429
636
  }
1430
147
1431
147
  // On MIPS we need to mark symbol which has a PLT entry and requires
1432
147
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1433
147
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1434
147
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1435
147
  if (
Config->EMachine == EM_MIPS147
) {
1436
144
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1437
144
1438
628
    for (SymbolTableEntry &Ent : Symbols) {
1439
628
      SymbolBody *Body = Ent.Symbol;
1440
628
      if (
Body->isInPlt() && 628
Body->NeedsPltAddr16
)
1441
6
        ESym->st_other |= STO_MIPS_PLT;
1442
628
1443
628
      if (Config->Relocatable)
1444
44
        
if (auto *44
D44
= dyn_cast<DefinedRegular>(Body))
1445
44
          
if (44
D->isMipsPIC<ELFT>()44
)
1446
6
            ESym->st_other |= STO_MIPS_PIC;
1447
628
      ++ESym;
1448
628
    }
1449
144
  }
1450
147
}
1451
1452
// .hash and .gnu.hash sections contain on-disk hash tables that map
1453
// symbol names to their dynamic symbol table indices. Their purpose
1454
// is to help the dynamic linker resolve symbols quickly. If ELF files
1455
// don't have them, the dynamic linker has to do linear search on all
1456
// dynamic symbols, which makes programs slower. Therefore, a .hash
1457
// section is added to a DSO by default. A .gnu.hash is added if you
1458
// give the -hash-style=gnu or -hash-style=both option.
1459
//
1460
// The Unix semantics of resolving dynamic symbols is somewhat expensive.
1461
// Each ELF file has a list of DSOs that the ELF file depends on and a
1462
// list of dynamic symbols that need to be resolved from any of the
1463
// DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
1464
// where m is the number of DSOs and n is the number of dynamic
1465
// symbols. For modern large programs, both m and n are large.  So
1466
// making each step faster by using hash tables substiantially
1467
// improves time to load programs.
1468
//
1469
// (Note that this is not the only way to design the shared library.
1470
// For instance, the Windows DLL takes a different approach. On
1471
// Windows, each dynamic symbol has a name of DLL from which the symbol
1472
// has to be resolved. That makes the cost of symbol resolution O(n).
1473
// This disables some hacky techniques you can use on Unix such as
1474
// LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
1475
//
1476
// Due to historical reasons, we have two different hash tables, .hash
1477
// and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
1478
// and better version of .hash. .hash is just an on-disk hash table, but
1479
// .gnu.hash has a bloom filter in addition to a hash table to skip
1480
// DSOs very quickly. If you are sure that your dynamic linker knows
1481
// about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a
1482
// safe bet is to specify -hash-style=both for backward compatibilty.
1483
GnuHashTableSection::GnuHashTableSection()
1484
5
    : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, Config->Wordsize, ".gnu.hash") {
1485
5
}
1486
1487
5
void GnuHashTableSection::finalizeContents() {
1488
5
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1489
5
1490
5
  // Computes bloom filter size in word size. We want to allocate 8
1491
5
  // bits for each symbol. It must be a power of two.
1492
5
  if (Symbols.empty())
1493
1
    MaskWords = 1;
1494
5
  else
1495
4
    MaskWords = NextPowerOf2((Symbols.size() - 1) / Config->Wordsize);
1496
5
1497
5
  Size = 16;                            // Header
1498
5
  Size += Config->Wordsize * MaskWords; // Bloom filter
1499
5
  Size += NBuckets * 4;                 // Hash buckets
1500
5
  Size += Symbols.size() * 4;           // Hash values
1501
5
}
1502
1503
5
void GnuHashTableSection::writeTo(uint8_t *Buf) {
1504
5
  // Write a header.
1505
5
  write32(Buf, NBuckets, Config->Endianness);
1506
5
  write32(Buf + 4, InX::DynSymTab->getNumSymbols() - Symbols.size(),
1507
5
          Config->Endianness);
1508
5
  write32(Buf + 8, MaskWords, Config->Endianness);
1509
5
  write32(Buf + 12, getShift2(), Config->Endianness);
1510
5
  Buf += 16;
1511
5
1512
5
  // Write a bloom filter and a hash table.
1513
5
  writeBloomFilter(Buf);
1514
5
  Buf += Config->Wordsize * MaskWords;
1515
5
  writeHashTable(Buf);
1516
5
}
1517
1518
// This function writes a 2-bit bloom filter. This bloom filter alone
1519
// usually filters out 80% or more of all symbol lookups [1].
1520
// The dynamic linker uses the hash table only when a symbol is not
1521
// filtered out by a bloom filter.
1522
//
1523
// [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2),
1524
//     p.9, https://www.akkadia.org/drepper/dsohowto.pdf
1525
5
void GnuHashTableSection::writeBloomFilter(uint8_t *Buf) {
1526
5
  const unsigned C = Config->Wordsize * 8;
1527
7
  for (const Entry &Sym : Symbols) {
1528
7
    size_t I = (Sym.Hash / C) & (MaskWords - 1);
1529
7
    uint64_t Val = readUint(Buf + I * Config->Wordsize);
1530
7
    Val |= uint64_t(1) << (Sym.Hash % C);
1531
7
    Val |= uint64_t(1) << ((Sym.Hash >> getShift2()) % C);
1532
7
    writeUint(Buf + I * Config->Wordsize, Val);
1533
7
  }
1534
5
}
1535
1536
5
void GnuHashTableSection::writeHashTable(uint8_t *Buf) {
1537
5
  // Group symbols by hash value.
1538
5
  std::vector<std::vector<Entry>> Syms(NBuckets);
1539
5
  for (const Entry &Ent : Symbols)
1540
7
    Syms[Ent.Hash % NBuckets].push_back(Ent);
1541
5
1542
5
  // Write hash buckets. Hash buckets contain indices in the following
1543
5
  // hash value table.
1544
5
  uint32_t *Buckets = reinterpret_cast<uint32_t *>(Buf);
1545
9
  for (size_t I = 0; 
I < NBuckets9
;
++I4
)
1546
4
    
if (4
!Syms[I].empty()4
)
1547
4
      write32(Buckets + I, Syms[I][0].Body->DynsymIndex, Config->Endianness);
1548
5
1549
5
  // Write a hash value table. It represents a sequence of chains that
1550
5
  // share the same hash modulo value. The last element of each chain
1551
5
  // is terminated by LSB 1.
1552
5
  uint32_t *Values = Buckets + NBuckets;
1553
5
  size_t I = 0;
1554
4
  for (std::vector<Entry> &Vec : Syms) {
1555
4
    if (Vec.empty())
1556
0
      continue;
1557
4
    for (const Entry &Ent : makeArrayRef(Vec).drop_back())
1558
3
      write32(Values + I++, Ent.Hash & ~1, Config->Endianness);
1559
4
    write32(Values + I++, Vec.back().Hash | 1, Config->Endianness);
1560
4
  }
1561
5
}
1562
1563
7
static uint32_t hashGnu(StringRef Name) {
1564
7
  uint32_t H = 5381;
1565
7
  for (uint8_t C : Name)
1566
24
    H = (H << 5) + H + C;
1567
7
  return H;
1568
7
}
1569
1570
// Returns a number of hash buckets to accomodate given number of elements.
1571
// We want to choose a moderate number that is not too small (which
1572
// causes too many hash collisions) and not too large (which wastes
1573
// disk space.)
1574
//
1575
// We return a prime number because it (is believed to) achieve good
1576
// hash distribution.
1577
4
static size_t getBucketSize(size_t NumSymbols) {
1578
4
  // List of largest prime numbers that are not greater than 2^n + 1.
1579
4
  for (size_t N : {131071, 65521, 32749, 16381, 8191, 4093, 2039, 1021, 509,
1580
4
                   251, 127, 61, 31, 13, 7, 3, 1})
1581
68
    
if (68
N <= NumSymbols68
)
1582
4
      return N;
1583
0
  return 0;
1584
0
}
1585
1586
// Add symbols to this symbol hash table. Note that this function
1587
// destructively sort a given vector -- which is needed because
1588
// GNU-style hash table places some sorting requirements.
1589
5
void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &V) {
1590
5
  // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
1591
5
  // its type correctly.
1592
5
  std::vector<SymbolTableEntry>::iterator Mid =
1593
11
      std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1594
11
        return S.Symbol->isUndefined();
1595
11
      });
1596
5
  if (Mid == V.end())
1597
1
    return;
1598
4
1599
4
  
for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) 4
{
1600
7
    SymbolBody *B = Ent.Symbol;
1601
7
    Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())});
1602
7
  }
1603
4
1604
4
  NBuckets = getBucketSize(Symbols.size());
1605
4
  std::stable_sort(Symbols.begin(), Symbols.end(),
1606
3
                   [&](const Entry &L, const Entry &R) {
1607
3
                     return L.Hash % NBuckets < R.Hash % NBuckets;
1608
3
                   });
1609
4
1610
4
  V.erase(Mid, V.end());
1611
4
  for (const Entry &Ent : Symbols)
1612
7
    V.push_back({Ent.Body, Ent.StrTabOffset});
1613
5
}
1614
1615
HashTableSection::HashTableSection()
1616
801
    : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1617
801
  this->Entsize = 4;
1618
801
}
1619
1620
761
void HashTableSection::finalizeContents() {
1621
761
  getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1622
761
1623
761
  unsigned NumEntries = 2;                       // nbucket and nchain.
1624
761
  NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1625
761
1626
761
  // Create as many buckets as there are symbols.
1627
761
  // FIXME: This is simplistic. We can try to optimize it, but implementing
1628
761
  // support for SHT_GNU_HASH is probably even more profitable.
1629
761
  NumEntries += InX::DynSymTab->getNumSymbols();
1630
761
  this->Size = NumEntries * 4;
1631
761
}
1632
1633
759
void HashTableSection::writeTo(uint8_t *Buf) {
1634
759
  unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1635
759
1636
759
  uint32_t *P = reinterpret_cast<uint32_t *>(Buf);
1637
759
  write32(P++, NumSymbols, Config->Endianness); // nbucket
1638
759
  write32(P++, NumSymbols, Config->Endianness); // nchain
1639
759
1640
759
  uint32_t *Buckets = P;
1641
759
  uint32_t *Chains = P + NumSymbols;
1642
759
1643
1.24k
  for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1644
1.24k
    SymbolBody *Body = S.Symbol;
1645
1.24k
    StringRef Name = Body->getName();
1646
1.24k
    unsigned I = Body->DynsymIndex;
1647
1.24k
    uint32_t Hash = hashSysV(Name) % NumSymbols;
1648
1.24k
    Chains[I] = Buckets[Hash];
1649
1.24k
    write32(Buckets + Hash, I, Config->Endianness);
1650
1.24k
  }
1651
759
}
1652
1653
PltSection::PltSection(size_t S)
1654
    : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"),
1655
3.19k
      HeaderSize(S) {
1656
3.19k
  // The PLT needs to be writable on SPARC as the dynamic linker will
1657
3.19k
  // modify the instructions in the PLT entries.
1658
3.19k
  if (Config->EMachine == EM_SPARCV9)
1659
2
    this->Flags |= SHF_WRITE;
1660
3.19k
}
1661
1662
147
void PltSection::writeTo(uint8_t *Buf) {
1663
147
  // At beginning of PLT but not the IPLT, we have code to call the dynamic
1664
147
  // linker to resolve dynsyms at runtime. Write such code.
1665
147
  if (HeaderSize != 0)
1666
130
    Target->writePltHeader(Buf);
1667
147
  size_t Off = HeaderSize;
1668
147
  // The IPlt is immediately after the Plt, account for this in RelOff
1669
147
  unsigned PltOff = getPltRelocOff();
1670
147
1671
214
  for (auto &I : Entries) {
1672
214
    const SymbolBody *B = I.first;
1673
214
    unsigned RelOff = I.second + PltOff;
1674
214
    uint64_t Got = B->getGotPltVA();
1675
214
    uint64_t Plt = this->getVA() + Off;
1676
214
    Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1677
214
    Off += Target->PltEntrySize;
1678
214
  }
1679
147
}
1680
1681
214
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1682
214
  Sym.PltIndex = Entries.size();
1683
214
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1684
214
  if (
HeaderSize == 0214
) {
1685
29
    PltRelocSection = In<ELFT>::RelaIplt;
1686
29
    Sym.IsInIplt = true;
1687
29
  }
1688
214
  unsigned RelOff = PltRelocSection->getRelocOffset();
1689
214
  Entries.push_back(std::make_pair(&Sym, RelOff));
1690
214
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SymbolBody&)
Line
Count
Source
1681
150
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1682
150
  Sym.PltIndex = Entries.size();
1683
150
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1684
150
  if (
HeaderSize == 0150
) {
1685
15
    PltRelocSection = In<ELFT>::RelaIplt;
1686
15
    Sym.IsInIplt = true;
1687
15
  }
1688
150
  unsigned RelOff = PltRelocSection->getRelocOffset();
1689
150
  Entries.push_back(std::make_pair(&Sym, RelOff));
1690
150
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SymbolBody&)
Line
Count
Source
1681
2
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1682
2
  Sym.PltIndex = Entries.size();
1683
2
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1684
2
  if (
HeaderSize == 02
) {
1685
2
    PltRelocSection = In<ELFT>::RelaIplt;
1686
2
    Sym.IsInIplt = true;
1687
2
  }
1688
2
  unsigned RelOff = PltRelocSection->getRelocOffset();
1689
2
  Entries.push_back(std::make_pair(&Sym, RelOff));
1690
2
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SymbolBody&)
Line
Count
Source
1681
8
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1682
8
  Sym.PltIndex = Entries.size();
1683
8
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1684
8
  if (
HeaderSize == 08
) {
1685
0
    PltRelocSection = In<ELFT>::RelaIplt;
1686
0
    Sym.IsInIplt = true;
1687
0
  }
1688
8
  unsigned RelOff = PltRelocSection->getRelocOffset();
1689
8
  Entries.push_back(std::make_pair(&Sym, RelOff));
1690
8
}
void lld::elf::PltSection::addEntry<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SymbolBody&)
Line
Count
Source
1681
54
template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1682
54
  Sym.PltIndex = Entries.size();
1683
54
  RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1684
54
  if (
HeaderSize == 054
) {
1685
12
    PltRelocSection = In<ELFT>::RelaIplt;
1686
12
    Sym.IsInIplt = true;
1687
12
  }
1688
54
  unsigned RelOff = PltRelocSection->getRelocOffset();
1689
54
  Entries.push_back(std::make_pair(&Sym, RelOff));
1690
54
}
1691
1692
3.69k
size_t PltSection::getSize() const {
1693
3.69k
  return HeaderSize + Entries.size() * Target->PltEntrySize;
1694
3.69k
}
1695
1696
// Some architectures such as additional symbols in the PLT section. For
1697
// example ARM uses mapping symbols to aid disassembly
1698
147
void PltSection::addSymbols() {
1699
147
  // The PLT may have symbols defined for the Header, the IPLT has no header
1700
147
  if (HeaderSize != 0)
1701
130
    Target->addPltHeaderSymbols(this);
1702
147
  size_t Off = HeaderSize;
1703
361
  for (size_t I = 0; 
I < Entries.size()361
;
++I214
) {
1704
214
    Target->addPltSymbols(this, Off);
1705
214
    Off += Target->PltEntrySize;
1706
214
  }
1707
147
}
1708
1709
147
unsigned PltSection::getPltRelocOff() const {
1710
147
  return (HeaderSize == 0) ? 
InX::Plt->getSize()17
:
0130
;
1711
147
}
1712
1713
// The string hash function for .gdb_index.
1714
5
static uint32_t computeGdbHash(StringRef S) {
1715
5
  uint32_t H = 0;
1716
5
  for (uint8_t C : S)
1717
26
    H = H * 67 + tolower(C) - 113;
1718
5
  return H;
1719
5
}
1720
1721
9
static std::vector<GdbIndexChunk::CuEntry> readCuList(DWARFContext &Dwarf) {
1722
9
  std::vector<GdbIndexChunk::CuEntry> Ret;
1723
9
  for (std::unique_ptr<DWARFCompileUnit> &Cu : Dwarf.compile_units())
1724
7
    Ret.push_back({Cu->getOffset(), Cu->getLength() + 4});
1725
9
  return Ret;
1726
9
}
1727
1728
static std::vector<GdbIndexChunk::AddressEntry>
1729
8
readAddressAreas(DWARFContext &Dwarf, InputSection *Sec) {
1730
8
  std::vector<GdbIndexChunk::AddressEntry> Ret;
1731
8
1732
8
  uint32_t CuIdx = 0;
1733
7
  for (std::unique_ptr<DWARFCompileUnit> &Cu : Dwarf.compile_units()) {
1734
7
    DWARFAddressRangesVector Ranges;
1735
7
    Cu->collectAddressRanges(Ranges);
1736
7
1737
7
    ArrayRef<InputSectionBase *> Sections = Sec->File->getSections();
1738
8
    for (DWARFAddressRange &R : Ranges) {
1739
8
      InputSectionBase *S = Sections[R.SectionIndex];
1740
9
      if (
!S || 8
S == &InputSection::Discarded9
||
!S->Live9
)
1741
1
        continue;
1742
7
      // Range list with zero size has no effect.
1743
7
      
if (7
R.LowPC == R.HighPC7
)
1744
1
        continue;
1745
6
      auto *IS = cast<InputSection>(S);
1746
6
      uint64_t Offset = IS->getOffsetInFile();
1747
6
      Ret.push_back({IS, R.LowPC - Offset, R.HighPC - Offset, CuIdx});
1748
6
    }
1749
7
    ++CuIdx;
1750
7
  }
1751
8
  return Ret;
1752
8
}
1753
1754
static std::vector<GdbIndexChunk::NameTypeEntry>
1755
9
readPubNamesAndTypes(DWARFContext &Dwarf) {
1756
9
  StringRef Sec1 = Dwarf.getDWARFObj().getGnuPubNamesSection();
1757
9
  StringRef Sec2 = Dwarf.getDWARFObj().getGnuPubTypesSection();
1758
9
1759
9
  std::vector<GdbIndexChunk::NameTypeEntry> Ret;
1760
17
  for (StringRef Sec : {Sec1, Sec2}) {
1761
17
    DWARFDebugPubTable Table(Sec, Config->IsLE, true);
1762
5
    for (const DWARFDebugPubTable::Set &Set : Table.getData()) {
1763
6
      for (const DWARFDebugPubTable::Entry &Ent : Set.Entries) {
1764
6
        CachedHashStringRef S(Ent.Name, computeGdbHash(Ent.Name));
1765
6
        Ret.push_back({S, Ent.Descriptor.toBits()});
1766
6
      }
1767
5
    }
1768
17
  }
1769
9
  return Ret;
1770
9
}
1771
1772
9
static std::vector<InputSection *> getDebugInfoSections() {
1773
9
  std::vector<InputSection *> Ret;
1774
9
  for (InputSectionBase *S : InputSections)
1775
111
    
if (InputSection *111
IS111
= dyn_cast<InputSection>(S))
1776
110
      
if (110
IS->Name == ".debug_info"110
)
1777
9
        Ret.push_back(IS);
1778
9
  return Ret;
1779
9
}
1780
1781
9
void GdbIndexSection::fixCuIndex() {
1782
9
  uint32_t Idx = 0;
1783
9
  for (GdbIndexChunk &Chunk : Chunks) {
1784
9
    for (GdbIndexChunk::AddressEntry &Ent : Chunk.AddressAreas)
1785
7
      Ent.CuIndex += Idx;
1786
9
    Idx += Chunk.CompilationUnits.size();
1787
9
  }
1788
9
}
1789
1790
9
std::vector<std::vector<uint32_t>> GdbIndexSection::createCuVectors() {
1791
9
  std::vector<std::vector<uint32_t>> Ret;
1792
9
  uint32_t Idx = 0;
1793
9
  uint32_t Off = 0;
1794
9
1795
9
  for (GdbIndexChunk &Chunk : Chunks) {
1796
6
    for (GdbIndexChunk::NameTypeEntry &Ent : Chunk.NamesAndTypes) {
1797
6
      GdbSymbol *&Sym = Symbols[Ent.Name];
1798
6
      if (
!Sym6
) {
1799
4
        Sym = make<GdbSymbol>(GdbSymbol{Ent.Name.hash(), Off, Ret.size()});
1800
4
        Off += Ent.Name.size() + 1;
1801
4
        Ret.push_back({});
1802
4
      }
1803
6
1804
6
      // gcc 5.4.1 produces a buggy .debug_gnu_pubnames that contains
1805
6
      // duplicate entries, so we want to dedup them.
1806
6
      std::vector<uint32_t> &Vec = Ret[Sym->CuVectorIndex];
1807
6
      uint32_t Val = (Ent.Type << 24) | Idx;
1808
6
      if (
Vec.empty() || 6
Vec.back() != Val2
)
1809
5
        Vec.push_back(Val);
1810
6
    }
1811
9
    Idx += Chunk.CompilationUnits.size();
1812
9
  }
1813
9
1814
9
  StringPoolSize = Off;
1815
9
  return Ret;
1816
9
}
1817
1818
9
template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
1819
9
  std::vector<InputSection *> Sections = getDebugInfoSections();
1820
9
  std::vector<GdbIndexChunk> Chunks(Sections.size());
1821
9
1822
9
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1823
9
    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
1824
9
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
1825
9
1826
9
    Chunks[I].DebugInfoSec = Sections[I];
1827
9
    Chunks[I].CompilationUnits = readCuList(Dwarf);
1828
9
    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
1829
9
    Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf);
1830
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
1822
1
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1823
1
    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
1824
1
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
1825
1
1826
1
    Chunks[I].DebugInfoSec = Sections[I];
1827
1
    Chunks[I].CompilationUnits = readCuList(Dwarf);
1828
1
    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
1829
1
    Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf);
1830
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
1822
8
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1823
8
    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
1824
8
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
1825
8
1826
8
    Chunks[I].DebugInfoSec = Sections[I];
1827
8
    Chunks[I].CompilationUnits = readCuList(Dwarf);
1828
8
    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
1829
8
    Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf);
1830
8
  });
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(unsigned long)::operator()(unsigned long) const
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(unsigned long)::operator()(unsigned long) const
1831
9
1832
9
  return make<GdbIndexSection>(std::move(Chunks));
1833
9
}
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, false> >()
Line
Count
Source
1818
1
template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
1819
1
  std::vector<InputSection *> Sections = getDebugInfoSections();
1820
1
  std::vector<GdbIndexChunk> Chunks(Sections.size());
1821
1
1822
1
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1823
1
    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
1824
1
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
1825
1
1826
1
    Chunks[I].DebugInfoSec = Sections[I];
1827
1
    Chunks[I].CompilationUnits = readCuList(Dwarf);
1828
1
    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
1829
1
    Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf);
1830
1
  });
1831
1
1832
1
  return make<GdbIndexSection>(std::move(Chunks));
1833
1
}
lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
1818
8
template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
1819
8
  std::vector<InputSection *> Sections = getDebugInfoSections();
1820
8
  std::vector<GdbIndexChunk> Chunks(Sections.size());
1821
8
1822
8
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
1823
8
    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
1824
8
    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
1825
8
1826
8
    Chunks[I].DebugInfoSec = Sections[I];
1827
8
    Chunks[I].CompilationUnits = readCuList(Dwarf);
1828
8
    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
1829
8
    Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf);
1830
8
  });
1831
8
1832
8
  return make<GdbIndexSection>(std::move(Chunks));
1833
8
}
Unexecuted instantiation: lld::elf::GdbIndexSection* lld::elf::createGdbIndex<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
1834
1835
9
static size_t getCuSize(ArrayRef<GdbIndexChunk> Arr) {
1836
9
  size_t Ret = 0;
1837
9
  for (const GdbIndexChunk &D : Arr)
1838
9
    Ret += D.CompilationUnits.size();
1839
9
  return Ret;
1840
9
}
1841
1842
9
static size_t getAddressAreaSize(ArrayRef<GdbIndexChunk> Arr) {
1843
9
  size_t Ret = 0;
1844
9
  for (const GdbIndexChunk &D : Arr)
1845
9
    Ret += D.AddressAreas.size();
1846
9
  return Ret;
1847
9
}
1848
1849
9
std::vector<GdbSymbol *> GdbIndexSection::createGdbSymtab() {
1850
9
  uint32_t Size = NextPowerOf2(Symbols.size() * 4 / 3);
1851
9
  if (Size < 1024)
1852
9
    Size = 1024;
1853
9
1854
9
  uint32_t Mask = Size - 1;
1855
9
  std::vector<GdbSymbol *> Ret(Size);
1856
9
1857
4
  for (auto &KV : Symbols) {
1858
4
    GdbSymbol *Sym = KV.second;
1859
4
    uint32_t I = Sym->NameHash & Mask;
1860
4
    uint32_t Step = ((Sym->NameHash * 17) & Mask) | 1;
1861
4
1862
4
    while (Ret[I])
1863
0
      I = (I + Step) & Mask;
1864
4
    Ret[I] = Sym;
1865
4
  }
1866
9
  return Ret;
1867
9
}
1868
1869
GdbIndexSection::GdbIndexSection(std::vector<GdbIndexChunk> &&C)
1870
9
    : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"), Chunks(std::move(C)) {
1871
9
  fixCuIndex();
1872
9
  CuVectors = createCuVectors();
1873
9
  GdbSymtab = createGdbSymtab();
1874
9
1875
9
  // Compute offsets early to know the section size.
1876
9
  // Each chunk size needs to be in sync with what we write in writeTo.
1877
9
  CuTypesOffset = CuListOffset + getCuSize(Chunks) * 16;
1878
9
  SymtabOffset = CuTypesOffset + getAddressAreaSize(Chunks) * 20;
1879
9
  ConstantPoolOffset = SymtabOffset + GdbSymtab.size() * 8;
1880
9
1881
9
  size_t Off = 0;
1882
4
  for (ArrayRef<uint32_t> Vec : CuVectors) {
1883
4
    CuVectorOffsets.push_back(Off);
1884
4
    Off += (Vec.size() + 1) * 4;
1885
4
  }
1886
9
  StringPoolOffset = ConstantPoolOffset + Off;
1887
9
}
1888
1889
25
size_t GdbIndexSection::getSize() const {
1890
25
  return StringPoolOffset + StringPoolSize;
1891
25
}
1892
1893
8
void GdbIndexSection::writeTo(uint8_t *Buf) {
1894
8
  // Write the section header.
1895
8
  write32le(Buf, 7);
1896
8
  write32le(Buf + 4, CuListOffset);
1897
8
  write32le(Buf + 8, CuTypesOffset);
1898
8
  write32le(Buf + 12, CuTypesOffset);
1899
8
  write32le(Buf + 16, SymtabOffset);
1900
8
  write32le(Buf + 20, ConstantPoolOffset);
1901
8
  Buf += 24;
1902
8
1903
8
  // Write the CU list.
1904
9
  for (GdbIndexChunk &D : Chunks) {
1905
8
    for (GdbIndexChunk::CuEntry &Cu : D.CompilationUnits) {
1906
8
      write64le(Buf, D.DebugInfoSec->OutSecOff + Cu.CuOffset);
1907
8
      write64le(Buf + 8, Cu.CuLength);
1908
8
      Buf += 16;
1909
8
    }
1910
9
  }
1911
8
1912
8
  // Write the address area.
1913
9
  for (GdbIndexChunk &D : Chunks) {
1914
7
    for (GdbIndexChunk::AddressEntry &E : D.AddressAreas) {
1915
7
      uint64_t BaseAddr =
1916
7
          E.Section->getParent()->Addr + E.Section->getOffset(0);
1917
7
      write64le(Buf, BaseAddr + E.LowAddress);
1918
7
      write64le(Buf + 8, BaseAddr + E.HighAddress);
1919
7
      write32le(Buf + 16, E.CuIndex);
1920
7
      Buf += 20;
1921
7
    }
1922
9
  }
1923
8
1924
8
  // Write the symbol table.
1925
8.19k
  for (GdbSymbol *Sym : GdbSymtab) {
1926
8.19k
    if (
Sym8.19k
) {
1927
4
      write32le(Buf, Sym->NameOffset + StringPoolOffset - ConstantPoolOffset);
1928
4
      write32le(Buf + 4, CuVectorOffsets[Sym->CuVectorIndex]);
1929
4
    }
1930
8.19k
    Buf += 8;
1931
8.19k
  }
1932
8
1933
8
  // Write the CU vectors.
1934
4
  for (ArrayRef<uint32_t> Vec : CuVectors) {
1935
4
    write32le(Buf, Vec.size());
1936
4
    Buf += 4;
1937
5
    for (uint32_t Val : Vec) {
1938
5
      write32le(Buf, Val);
1939
5
      Buf += 4;
1940
5
    }
1941
4
  }
1942
8
1943
8
  // Write the string pool.
1944
4
  for (auto &KV : Symbols) {
1945
4
    CachedHashStringRef S = KV.first;
1946
4
    GdbSymbol *Sym = KV.second;
1947
4
    size_t Off = Sym->NameOffset;
1948
4
    memcpy(Buf + Off, S.val().data(), S.size());
1949
4
    Buf[Off + S.size()] = '\0';
1950
4
  }
1951
8
}
1952
1953
17
bool GdbIndexSection::empty() const { return !Out::DebugInfo; }
1954
1955
template <class ELFT>
1956
EhFrameHeader<ELFT>::EhFrameHeader()
1957
16
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::EhFrameHeader()
Line
Count
Source
1957
12
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::EhFrameHeader()
Line
Count
Source
1957
1
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::EhFrameHeader()
Line
Count
Source
1957
3
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::EhFrameHeader()
1958
1959
// .eh_frame_hdr contains a binary search table of pointers to FDEs.
1960
// Each entry of the search table consists of two values,
1961
// the starting PC from where FDEs covers, and the FDE's address.
1962
// It is sorted by PC.
1963
14
template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1964
14
  const endianness E = ELFT::TargetEndianness;
1965
14
1966
14
  // Sort the FDE list by their PC and uniqueify. Usually there is only
1967
14
  // one FDE for a PC (i.e. function), but if ICF merges two functions
1968
14
  // into one, there can be more than one FDEs pointing to the address.
1969
4
  auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)::'lambda'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&) const
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)::'lambda'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&) const
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)::'lambda'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&) const
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)::'lambda'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&) const
Line
Count
Source
1969
4
  auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1970
14
  std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1971
4
  auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)::'lambda0'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::FdeData const&) const
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)::'lambda0'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::FdeData const&) const
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)::'lambda0'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::FdeData const&) const
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)::'lambda0'(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&)::operator()(lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&, lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, true> >::FdeData const&) const
Line
Count
Source
1971
4
  auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1972
14
  Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1973
14
1974
14
  Buf[0] = 1;
1975
14
  Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1976
14
  Buf[2] = DW_EH_PE_udata4;
1977
14
  Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1978
14
  write32<E>(Buf + 4, In<ELFT>::EhFrame->getParent()->Addr - this->getVA() - 4);
1979
14
  write32<E>(Buf + 8, Fdes.size());
1980
14
  Buf += 12;
1981
14
1982
14
  uint64_t VA = this->getVA();
1983
14
  for (FdeData &Fde : Fdes) {
1984
14
    write32<E>(Buf, Fde.Pc - VA);
1985
14
    write32<E>(Buf + 4, Fde.FdeVA - VA);
1986
14
    Buf += 8;
1987
14
  }
1988
14
}
Unexecuted instantiation: lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1963
1
template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1964
1
  const endianness E = ELFT::TargetEndianness;
1965
1
1966
1
  // Sort the FDE list by their PC and uniqueify. Usually there is only
1967
1
  // one FDE for a PC (i.e. function), but if ICF merges two functions
1968
1
  // into one, there can be more than one FDEs pointing to the address.
1969
1
  auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1970
1
  std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1971
1
  auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1972
1
  Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1973
1
1974
1
  Buf[0] = 1;
1975
1
  Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1976
1
  Buf[2] = DW_EH_PE_udata4;
1977
1
  Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1978
1
  write32<E>(Buf + 4, In<ELFT>::EhFrame->getParent()->Addr - this->getVA() - 4);
1979
1
  write32<E>(Buf + 8, Fdes.size());
1980
1
  Buf += 12;
1981
1
1982
1
  uint64_t VA = this->getVA();
1983
1
  for (FdeData &Fde : Fdes) {
1984
1
    write32<E>(Buf, Fde.Pc - VA);
1985
1
    write32<E>(Buf + 4, Fde.FdeVA - VA);
1986
1
    Buf += 8;
1987
1
  }
1988
1
}
lld::elf::EhFrameHeader<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1963
3
template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1964
3
  const endianness E = ELFT::TargetEndianness;
1965
3
1966
3
  // Sort the FDE list by their PC and uniqueify. Usually there is only
1967
3
  // one FDE for a PC (i.e. function), but if ICF merges two functions
1968
3
  // into one, there can be more than one FDEs pointing to the address.
1969
3
  auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1970
3
<