Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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 "Bits.h"
19
#include "Config.h"
20
#include "InputFiles.h"
21
#include "LinkerScript.h"
22
#include "OutputSections.h"
23
#include "SymbolTable.h"
24
#include "Symbols.h"
25
#include "Target.h"
26
#include "Writer.h"
27
#include "lld/Common/ErrorHandler.h"
28
#include "lld/Common/Memory.h"
29
#include "lld/Common/Strings.h"
30
#include "lld/Common/Threads.h"
31
#include "lld/Common/Version.h"
32
#include "llvm/ADT/SetOperations.h"
33
#include "llvm/BinaryFormat/Dwarf.h"
34
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
35
#include "llvm/Object/Decompressor.h"
36
#include "llvm/Object/ELFObjectFile.h"
37
#include "llvm/Support/Endian.h"
38
#include "llvm/Support/LEB128.h"
39
#include "llvm/Support/MD5.h"
40
#include "llvm/Support/RandomNumberGenerator.h"
41
#include "llvm/Support/SHA1.h"
42
#include "llvm/Support/xxhash.h"
43
#include <cstdlib>
44
#include <thread>
45
46
using namespace llvm;
47
using namespace llvm::dwarf;
48
using namespace llvm::ELF;
49
using namespace llvm::object;
50
using namespace llvm::support;
51
52
using namespace lld;
53
using namespace lld::elf;
54
55
using llvm::support::endian::write32le;
56
using llvm::support::endian::write64le;
57
58
constexpr size_t MergeNoTailSection::NumShards;
59
60
// Returns an LLD version string.
61
2.06k
static ArrayRef<uint8_t> getVersion() {
62
2.06k
  // Check LLD_VERSION first for ease of testing.
63
2.06k
  // You can get consistent output by using the environment variable.
64
2.06k
  // This is only for testing.
65
2.06k
  StringRef S = getenv("LLD_VERSION");
66
2.06k
  if (S.empty())
67
0
    S = Saver.save(Twine("Linker: ") + getLLDVersion());
68
2.06k
69
2.06k
  // +1 to include the terminating '\0'.
70
2.06k
  return {(const uint8_t *)S.data(), S.size() + 1};
71
2.06k
}
72
73
// Creates a .comment section containing LLD version info.
74
// With this feature, you can identify LLD-generated binaries easily
75
// by "readelf --string-dump .comment <file>".
76
// The returned object is a mergeable string section.
77
2.06k
MergeInputSection *elf::createCommentSection() {
78
2.06k
  return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,
79
2.06k
                                 getVersion(), ".comment");
80
2.06k
}
81
82
// .MIPS.abiflags section.
83
template <class ELFT>
84
MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
85
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
86
173
      Flags(Flags) {
87
173
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
88
173
}
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
86
16
      Flags(Flags) {
87
16
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
88
16
}
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
86
115
      Flags(Flags) {
87
115
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
88
115
}
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
86
3
      Flags(Flags) {
87
3
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
88
3
}
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
86
39
      Flags(Flags) {
87
39
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
88
39
}
89
90
169
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
91
169
  memcpy(Buf, &Flags, sizeof(Flags));
92
169
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
90
16
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
91
16
  memcpy(Buf, &Flags, sizeof(Flags));
92
16
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
90
112
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
91
112
  memcpy(Buf, &Flags, sizeof(Flags));
92
112
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
90
3
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
91
3
  memcpy(Buf, &Flags, sizeof(Flags));
92
3
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
90
38
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
91
38
  memcpy(Buf, &Flags, sizeof(Flags));
92
38
}
93
94
template <class ELFT>
95
175
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
96
175
  Elf_Mips_ABIFlags Flags = {};
97
175
  bool Create = false;
98
175
99
1.74k
  for (InputSectionBase *Sec : InputSections) {
100
1.74k
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
101
1.52k
      continue;
102
223
    Sec->Live = false;
103
223
    Create = true;
104
223
105
223
    std::string Filename = toString(Sec->File);
106
223
    const size_t Size = Sec->Data.size();
107
223
    // Older version of BFD (such as the default FreeBSD linker) concatenate
108
223
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
109
223
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
110
223
    if (Size < sizeof(Elf_Mips_ABIFlags)) {
111
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
112
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
113
0
      return nullptr;
114
0
    }
115
223
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
116
223
    if (S->version != 0) {
117
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
118
0
            Twine(S->version));
119
0
      return nullptr;
120
0
    }
121
223
122
223
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
123
223
    // select the highest number of ISA/Rev/Ext.
124
223
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
125
223
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
126
223
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
127
223
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
128
223
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
129
223
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
130
223
    Flags.ases |= S->ases;
131
223
    Flags.flags1 |= S->flags1;
132
223
    Flags.flags2 |= S->flags2;
133
223
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
134
223
  };
135
175
136
175
  if (Create)
137
173
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
138
2
  return nullptr;
139
2
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
95
17
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
96
17
  Elf_Mips_ABIFlags Flags = {};
97
17
  bool Create = false;
98
17
99
154
  for (InputSectionBase *Sec : InputSections) {
100
154
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
101
134
      continue;
102
20
    Sec->Live = false;
103
20
    Create = true;
104
20
105
20
    std::string Filename = toString(Sec->File);
106
20
    const size_t Size = Sec->Data.size();
107
20
    // Older version of BFD (such as the default FreeBSD linker) concatenate
108
20
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
109
20
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
110
20
    if (Size < sizeof(Elf_Mips_ABIFlags)) {
111
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
112
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
113
0
      return nullptr;
114
0
    }
115
20
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
116
20
    if (S->version != 0) {
117
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
118
0
            Twine(S->version));
119
0
      return nullptr;
120
0
    }
121
20
122
20
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
123
20
    // select the highest number of ISA/Rev/Ext.
124
20
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
125
20
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
126
20
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
127
20
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
128
20
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
129
20
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
130
20
    Flags.ases |= S->ases;
131
20
    Flags.flags1 |= S->flags1;
132
20
    Flags.flags2 |= S->flags2;
133
20
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
134
20
  };
135
17
136
17
  if (Create)
137
16
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
138
1
  return nullptr;
139
1
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
95
116
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
96
116
  Elf_Mips_ABIFlags Flags = {};
97
116
  bool Create = false;
98
116
99
1.18k
  for (InputSectionBase *Sec : InputSections) {
100
1.18k
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
101
1.03k
      continue;
102
154
    Sec->Live = false;
103
154
    Create = true;
104
154
105
154
    std::string Filename = toString(Sec->File);
106
154
    const size_t Size = Sec->Data.size();
107
154
    // Older version of BFD (such as the default FreeBSD linker) concatenate
108
154
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
109
154
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
110
154
    if (Size < sizeof(Elf_Mips_ABIFlags)) {
111
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
112
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
113
0
      return nullptr;
114
0
    }
115
154
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
116
154
    if (S->version != 0) {
117
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
118
0
            Twine(S->version));
119
0
      return nullptr;
120
0
    }
121
154
122
154
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
123
154
    // select the highest number of ISA/Rev/Ext.
124
154
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
125
154
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
126
154
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
127
154
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
128
154
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
129
154
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
130
154
    Flags.ases |= S->ases;
131
154
    Flags.flags1 |= S->flags1;
132
154
    Flags.flags2 |= S->flags2;
133
154
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
134
154
  };
135
116
136
116
  if (Create)
137
115
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
138
1
  return nullptr;
139
1
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
95
3
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
96
3
  Elf_Mips_ABIFlags Flags = {};
97
3
  bool Create = false;
98
3
99
29
  for (InputSectionBase *Sec : InputSections) {
100
29
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
101
25
      continue;
102
4
    Sec->Live = false;
103
4
    Create = true;
104
4
105
4
    std::string Filename = toString(Sec->File);
106
4
    const size_t Size = Sec->Data.size();
107
4
    // Older version of BFD (such as the default FreeBSD linker) concatenate
108
4
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
109
4
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
110
4
    if (Size < sizeof(Elf_Mips_ABIFlags)) {
111
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
112
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
113
0
      return nullptr;
114
0
    }
115
4
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
116
4
    if (S->version != 0) {
117
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
118
0
            Twine(S->version));
119
0
      return nullptr;
120
0
    }
121
4
122
4
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
123
4
    // select the highest number of ISA/Rev/Ext.
124
4
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
125
4
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
126
4
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
127
4
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
128
4
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
129
4
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
130
4
    Flags.ases |= S->ases;
131
4
    Flags.flags1 |= S->flags1;
132
4
    Flags.flags2 |= S->flags2;
133
4
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
134
4
  };
135
3
136
3
  if (Create)
137
3
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
138
0
  return nullptr;
139
0
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
95
39
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
96
39
  Elf_Mips_ABIFlags Flags = {};
97
39
  bool Create = false;
98
39
99
376
  for (InputSectionBase *Sec : InputSections) {
100
376
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
101
331
      continue;
102
45
    Sec->Live = false;
103
45
    Create = true;
104
45
105
45
    std::string Filename = toString(Sec->File);
106
45
    const size_t Size = Sec->Data.size();
107
45
    // Older version of BFD (such as the default FreeBSD linker) concatenate
108
45
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
109
45
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
110
45
    if (Size < sizeof(Elf_Mips_ABIFlags)) {
111
0
      error(Filename + ": invalid size of .MIPS.abiflags section: got " +
112
0
            Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
113
0
      return nullptr;
114
0
    }
115
45
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
116
45
    if (S->version != 0) {
117
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
118
0
            Twine(S->version));
119
0
      return nullptr;
120
0
    }
121
45
122
45
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
123
45
    // select the highest number of ISA/Rev/Ext.
124
45
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
125
45
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
126
45
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
127
45
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
128
45
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
129
45
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
130
45
    Flags.ases |= S->ases;
131
45
    Flags.flags1 |= S->flags1;
132
45
    Flags.flags2 |= S->flags2;
133
45
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
134
45
  };
135
39
136
39
  if (Create)
137
39
    return make<MipsAbiFlagsSection<ELFT>>(Flags);
138
0
  return nullptr;
139
0
}
140
141
// .MIPS.options section.
142
template <class ELFT>
143
MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
144
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
145
42
      Reginfo(Reginfo) {
146
42
  this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
147
42
}
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, false> >)
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, false> >)
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
145
3
      Reginfo(Reginfo) {
146
3
  this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
147
3
}
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
145
39
      Reginfo(Reginfo) {
146
39
  this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
147
39
}
148
149
41
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
150
41
  auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
151
41
  Options->kind = ODK_REGINFO;
152
41
  Options->size = getSize();
153
41
154
41
  if (!Config->Relocatable)
155
38
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
156
41
  memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
157
41
}
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
149
3
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
150
3
  auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
151
3
  Options->kind = ODK_REGINFO;
152
3
  Options->size = getSize();
153
3
154
3
  if (!Config->Relocatable)
155
3
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
156
3
  memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
157
3
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
149
38
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
150
38
  auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
151
38
  Options->kind = ODK_REGINFO;
152
38
  Options->size = getSize();
153
38
154
38
  if (!Config->Relocatable)
155
35
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
156
38
  memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
157
38
}
158
159
template <class ELFT>
160
175
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
161
175
  // N64 ABI only.
162
175
  if (!ELFT::Is64Bits)
163
133
    return nullptr;
164
42
165
42
  std::vector<InputSectionBase *> Sections;
166
42
  for (InputSectionBase *Sec : InputSections)
167
447
    if (Sec->Type == SHT_MIPS_OPTIONS)
168
49
      Sections.push_back(Sec);
169
42
170
42
  if (Sections.empty())
171
0
    return nullptr;
172
42
173
42
  Elf_Mips_RegInfo Reginfo = {};
174
49
  for (InputSectionBase *Sec : Sections) {
175
49
    Sec->Live = false;
176
49
177
49
    std::string Filename = toString(Sec->File);
178
49
    ArrayRef<uint8_t> D = Sec->Data;
179
49
180
49
    while (!D.empty()) {
181
49
      if (D.size() < sizeof(Elf_Mips_Options)) {
182
0
        error(Filename + ": invalid size of .MIPS.options section");
183
0
        break;
184
0
      }
185
49
186
49
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
187
49
      if (Opt->kind == ODK_REGINFO) {
188
49
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
189
49
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
190
49
        break;
191
49
      }
192
0
193
0
      if (!Opt->size)
194
0
        fatal(Filename + ": zero option descriptor size");
195
0
      D = D.slice(Opt->size);
196
0
    }
197
49
  };
198
42
199
42
  return make<MipsOptionsSection<ELFT>>(Reginfo);
200
42
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
160
17
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
161
17
  // N64 ABI only.
162
17
  if (!ELFT::Is64Bits)
163
17
    return nullptr;
164
0
165
0
  std::vector<InputSectionBase *> Sections;
166
0
  for (InputSectionBase *Sec : InputSections)
167
0
    if (Sec->Type == SHT_MIPS_OPTIONS)
168
0
      Sections.push_back(Sec);
169
0
170
0
  if (Sections.empty())
171
0
    return nullptr;
172
0
173
0
  Elf_Mips_RegInfo Reginfo = {};
174
0
  for (InputSectionBase *Sec : Sections) {
175
0
    Sec->Live = false;
176
0
177
0
    std::string Filename = toString(Sec->File);
178
0
    ArrayRef<uint8_t> D = Sec->Data;
179
0
180
0
    while (!D.empty()) {
181
0
      if (D.size() < sizeof(Elf_Mips_Options)) {
182
0
        error(Filename + ": invalid size of .MIPS.options section");
183
0
        break;
184
0
      }
185
0
186
0
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
187
0
      if (Opt->kind == ODK_REGINFO) {
188
0
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
189
0
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
190
0
        break;
191
0
      }
192
0
193
0
      if (!Opt->size)
194
0
        fatal(Filename + ": zero option descriptor size");
195
0
      D = D.slice(Opt->size);
196
0
    }
197
0
  };
198
0
199
0
  return make<MipsOptionsSection<ELFT>>(Reginfo);
200
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
160
116
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
161
116
  // N64 ABI only.
162
116
  if (!ELFT::Is64Bits)
163
116
    return nullptr;
164
0
165
0
  std::vector<InputSectionBase *> Sections;
166
0
  for (InputSectionBase *Sec : InputSections)
167
0
    if (Sec->Type == SHT_MIPS_OPTIONS)
168
0
      Sections.push_back(Sec);
169
0
170
0
  if (Sections.empty())
171
0
    return nullptr;
172
0
173
0
  Elf_Mips_RegInfo Reginfo = {};
174
0
  for (InputSectionBase *Sec : Sections) {
175
0
    Sec->Live = false;
176
0
177
0
    std::string Filename = toString(Sec->File);
178
0
    ArrayRef<uint8_t> D = Sec->Data;
179
0
180
0
    while (!D.empty()) {
181
0
      if (D.size() < sizeof(Elf_Mips_Options)) {
182
0
        error(Filename + ": invalid size of .MIPS.options section");
183
0
        break;
184
0
      }
185
0
186
0
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
187
0
      if (Opt->kind == ODK_REGINFO) {
188
0
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
189
0
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
190
0
        break;
191
0
      }
192
0
193
0
      if (!Opt->size)
194
0
        fatal(Filename + ": zero option descriptor size");
195
0
      D = D.slice(Opt->size);
196
0
    }
197
0
  };
198
0
199
0
  return make<MipsOptionsSection<ELFT>>(Reginfo);
200
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
160
3
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
161
3
  // N64 ABI only.
162
3
  if (!ELFT::Is64Bits)
163
0
    return nullptr;
164
3
165
3
  std::vector<InputSectionBase *> Sections;
166
3
  for (InputSectionBase *Sec : InputSections)
167
32
    if (Sec->Type == SHT_MIPS_OPTIONS)
168
4
      Sections.push_back(Sec);
169
3
170
3
  if (Sections.empty())
171
0
    return nullptr;
172
3
173
3
  Elf_Mips_RegInfo Reginfo = {};
174
4
  for (InputSectionBase *Sec : Sections) {
175
4
    Sec->Live = false;
176
4
177
4
    std::string Filename = toString(Sec->File);
178
4
    ArrayRef<uint8_t> D = Sec->Data;
179
4
180
4
    while (!D.empty()) {
181
4
      if (D.size() < sizeof(Elf_Mips_Options)) {
182
0
        error(Filename + ": invalid size of .MIPS.options section");
183
0
        break;
184
0
      }
185
4
186
4
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
187
4
      if (Opt->kind == ODK_REGINFO) {
188
4
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
189
4
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
190
4
        break;
191
4
      }
192
0
193
0
      if (!Opt->size)
194
0
        fatal(Filename + ": zero option descriptor size");
195
0
      D = D.slice(Opt->size);
196
0
    }
197
4
  };
198
3
199
3
  return make<MipsOptionsSection<ELFT>>(Reginfo);
200
3
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
160
39
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
161
39
  // N64 ABI only.
162
39
  if (!ELFT::Is64Bits)
163
0
    return nullptr;
164
39
165
39
  std::vector<InputSectionBase *> Sections;
166
39
  for (InputSectionBase *Sec : InputSections)
167
415
    if (Sec->Type == SHT_MIPS_OPTIONS)
168
45
      Sections.push_back(Sec);
169
39
170
39
  if (Sections.empty())
171
0
    return nullptr;
172
39
173
39
  Elf_Mips_RegInfo Reginfo = {};
174
45
  for (InputSectionBase *Sec : Sections) {
175
45
    Sec->Live = false;
176
45
177
45
    std::string Filename = toString(Sec->File);
178
45
    ArrayRef<uint8_t> D = Sec->Data;
179
45
180
45
    while (!D.empty()) {
181
45
      if (D.size() < sizeof(Elf_Mips_Options)) {
182
0
        error(Filename + ": invalid size of .MIPS.options section");
183
0
        break;
184
0
      }
185
45
186
45
      auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
187
45
      if (Opt->kind == ODK_REGINFO) {
188
45
        Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
189
45
        Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
190
45
        break;
191
45
      }
192
0
193
0
      if (!Opt->size)
194
0
        fatal(Filename + ": zero option descriptor size");
195
0
      D = D.slice(Opt->size);
196
0
    }
197
45
  };
198
39
199
39
  return make<MipsOptionsSection<ELFT>>(Reginfo);
200
39
}
201
202
// MIPS .reginfo section.
203
template <class ELFT>
204
MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
205
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
206
131
      Reginfo(Reginfo) {
207
131
  this->Entsize = sizeof(Elf_Mips_RegInfo);
208
131
}
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
206
16
      Reginfo(Reginfo) {
207
16
  this->Entsize = sizeof(Elf_Mips_RegInfo);
208
16
}
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
206
115
      Reginfo(Reginfo) {
207
115
  this->Entsize = sizeof(Elf_Mips_RegInfo);
208
115
}
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> >)
209
210
128
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
211
128
  if (!Config->Relocatable)
212
119
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
213
128
  memcpy(Buf, &Reginfo, sizeof(Reginfo));
214
128
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
210
16
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
211
16
  if (!Config->Relocatable)
212
16
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
213
16
  memcpy(Buf, &Reginfo, sizeof(Reginfo));
214
16
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
210
112
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
211
112
  if (!Config->Relocatable)
212
103
    Reginfo.ri_gp_value = InX::MipsGot->getGp();
213
112
  memcpy(Buf, &Reginfo, sizeof(Reginfo));
214
112
}
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Unexecuted instantiation: lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
215
216
template <class ELFT>
217
175
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
218
175
  // Section should be alive for O32 and N32 ABIs only.
219
175
  if (ELFT::Is64Bits)
220
42
    return nullptr;
221
133
222
133
  std::vector<InputSectionBase *> Sections;
223
133
  for (InputSectionBase *Sec : InputSections)
224
1.47k
    if (Sec->Type == SHT_MIPS_REGINFO)
225
174
      Sections.push_back(Sec);
226
133
227
133
  if (Sections.empty())
228
2
    return nullptr;
229
131
230
131
  Elf_Mips_RegInfo Reginfo = {};
231
174
  for (InputSectionBase *Sec : Sections) {
232
174
    Sec->Live = false;
233
174
234
174
    if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
235
0
      error(toString(Sec->File) + ": invalid size of .reginfo section");
236
0
      return nullptr;
237
0
    }
238
174
239
174
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
240
174
    Reginfo.ri_gprmask |= R->ri_gprmask;
241
174
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
242
174
  };
243
131
244
131
  return make<MipsReginfoSection<ELFT>>(Reginfo);
245
131
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
217
17
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
218
17
  // Section should be alive for O32 and N32 ABIs only.
219
17
  if (ELFT::Is64Bits)
220
0
    return nullptr;
221
17
222
17
  std::vector<InputSectionBase *> Sections;
223
17
  for (InputSectionBase *Sec : InputSections)
224
170
    if (Sec->Type == SHT_MIPS_REGINFO)
225
20
      Sections.push_back(Sec);
226
17
227
17
  if (Sections.empty())
228
1
    return nullptr;
229
16
230
16
  Elf_Mips_RegInfo Reginfo = {};
231
20
  for (InputSectionBase *Sec : Sections) {
232
20
    Sec->Live = false;
233
20
234
20
    if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
235
0
      error(toString(Sec->File) + ": invalid size of .reginfo section");
236
0
      return nullptr;
237
0
    }
238
20
239
20
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
240
20
    Reginfo.ri_gprmask |= R->ri_gprmask;
241
20
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
242
20
  };
243
16
244
16
  return make<MipsReginfoSection<ELFT>>(Reginfo);
245
16
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
217
116
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
218
116
  // Section should be alive for O32 and N32 ABIs only.
219
116
  if (ELFT::Is64Bits)
220
0
    return nullptr;
221
116
222
116
  std::vector<InputSectionBase *> Sections;
223
116
  for (InputSectionBase *Sec : InputSections)
224
1.30k
    if (Sec->Type == SHT_MIPS_REGINFO)
225
154
      Sections.push_back(Sec);
226
116
227
116
  if (Sections.empty())
228
1
    return nullptr;
229
115
230
115
  Elf_Mips_RegInfo Reginfo = {};
231
154
  for (InputSectionBase *Sec : Sections) {
232
154
    Sec->Live = false;
233
154
234
154
    if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
235
0
      error(toString(Sec->File) + ": invalid size of .reginfo section");
236
0
      return nullptr;
237
0
    }
238
154
239
154
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
240
154
    Reginfo.ri_gprmask |= R->ri_gprmask;
241
154
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
242
154
  };
243
115
244
115
  return make<MipsReginfoSection<ELFT>>(Reginfo);
245
115
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
217
3
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
218
3
  // Section should be alive for O32 and N32 ABIs only.
219
3
  if (ELFT::Is64Bits)
220
3
    return nullptr;
221
0
222
0
  std::vector<InputSectionBase *> Sections;
223
0
  for (InputSectionBase *Sec : InputSections)
224
0
    if (Sec->Type == SHT_MIPS_REGINFO)
225
0
      Sections.push_back(Sec);
226
0
227
0
  if (Sections.empty())
228
0
    return nullptr;
229
0
230
0
  Elf_Mips_RegInfo Reginfo = {};
231
0
  for (InputSectionBase *Sec : Sections) {
232
0
    Sec->Live = false;
233
0
234
0
    if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
235
0
      error(toString(Sec->File) + ": invalid size of .reginfo section");
236
0
      return nullptr;
237
0
    }
238
0
239
0
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
240
0
    Reginfo.ri_gprmask |= R->ri_gprmask;
241
0
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
242
0
  };
243
0
244
0
  return make<MipsReginfoSection<ELFT>>(Reginfo);
245
0
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
217
39
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
218
39
  // Section should be alive for O32 and N32 ABIs only.
219
39
  if (ELFT::Is64Bits)
220
39
    return nullptr;
221
0
222
0
  std::vector<InputSectionBase *> Sections;
223
0
  for (InputSectionBase *Sec : InputSections)
224
0
    if (Sec->Type == SHT_MIPS_REGINFO)
225
0
      Sections.push_back(Sec);
226
0
227
0
  if (Sections.empty())
228
0
    return nullptr;
229
0
230
0
  Elf_Mips_RegInfo Reginfo = {};
231
0
  for (InputSectionBase *Sec : Sections) {
232
0
    Sec->Live = false;
233
0
234
0
    if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
235
0
      error(toString(Sec->File) + ": invalid size of .reginfo section");
236
0
      return nullptr;
237
0
    }
238
0
239
0
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
240
0
    Reginfo.ri_gprmask |= R->ri_gprmask;
241
0
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
242
0
  };
243
0
244
0
  return make<MipsReginfoSection<ELFT>>(Reginfo);
245
0
}
246
247
8
InputSection *elf::createInterpSection() {
248
8
  // StringSaver guarantees that the returned string ends with '\0'.
249
8
  StringRef S = Saver.save(Config->DynamicLinker);
250
8
  ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1};
251
8
252
8
  auto *Sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, Contents,
253
8
                                 ".interp");
254
8
  Sec->Live = true;
255
8
  return Sec;
256
8
}
257
258
Defined *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
259
455
                                uint64_t Size, InputSectionBase &Section) {
260
455
  auto *S = make<Defined>(Section.File, Name, STB_LOCAL, STV_DEFAULT, Type,
261
455
                          Value, Size, &Section);
262
455
  if (InX::SymTab)
263
455
    InX::SymTab->addSymbol(S);
264
455
  return S;
265
455
}
266
267
16
static size_t getHashSize() {
268
16
  switch (Config->BuildId) {
269
16
  case BuildIdKind::Fast:
270
6
    return 8;
271
16
  case BuildIdKind::Md5:
272
3
  case BuildIdKind::Uuid:
273
3
    return 16;
274
4
  case BuildIdKind::Sha1:
275
4
    return 20;
276
3
  case BuildIdKind::Hexstring:
277
3
    return Config->BuildIdVector.size();
278
3
  default:
279
0
    llvm_unreachable("unknown BuildIdKind");
280
16
  }
281
16
}
282
283
BuildIdSection::BuildIdSection()
284
    : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"),
285
16
      HashSize(getHashSize()) {}
286
287
16
void BuildIdSection::writeTo(uint8_t *Buf) {
288
16
  write32(Buf, 4);                      // Name size
289
16
  write32(Buf + 4, HashSize);           // Content size
290
16
  write32(Buf + 8, NT_GNU_BUILD_ID);    // Type
291
16
  memcpy(Buf + 12, "GNU", 4);           // Name string
292
16
  HashBuf = Buf + 16;
293
16
}
294
295
// Split one uint8 array into small pieces of uint8 arrays.
296
static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
297
12
                                            size_t ChunkSize) {
298
12
  std::vector<ArrayRef<uint8_t>> Ret;
299
12
  while (Arr.size() > ChunkSize) {
300
0
    Ret.push_back(Arr.take_front(ChunkSize));
301
0
    Arr = Arr.drop_front(ChunkSize);
302
0
  }
303
12
  if (!Arr.empty())
304
12
    Ret.push_back(Arr);
305
12
  return Ret;
306
12
}
307
308
// Computes a hash value of Data using a given hash function.
309
// In order to utilize multiple cores, we first split data into 1MB
310
// chunks, compute a hash for each chunk, and then compute a hash value
311
// of the hash values.
312
void BuildIdSection::computeHash(
313
    llvm::ArrayRef<uint8_t> Data,
314
12
    std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) {
315
12
  std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
316
12
  std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
317
12
318
12
  // Compute hash values.
319
12
  parallelForEachN(0, Chunks.size(), [&](size_t I) {
320
12
    HashFn(Hashes.data() + I * HashSize, Chunks[I]);
321
12
  });
322
12
323
12
  // Write to the final output buffer.
324
12
  HashFn(HashBuf, Hashes);
325
12
}
326
327
BssSection::BssSection(StringRef Name, uint64_t Size, uint32_t Alignment)
328
4.41k
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, Alignment, Name) {
329
4.41k
  this->Bss = true;
330
4.41k
  if (OutputSection *Sec = getParent())
331
0
    Sec->Alignment = std::max(Sec->Alignment, Alignment);
332
4.41k
  this->Size = Size;
333
4.41k
}
334
335
16
void BuildIdSection::writeBuildId(ArrayRef<uint8_t> Buf) {
336
16
  switch (Config->BuildId) {
337
16
  case BuildIdKind::Fast:
338
12
    computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
339
12
      write64le(Dest, xxHash64(toStringRef(Arr)));
340
12
    });
341
6
    break;
342
16
  case BuildIdKind::Md5:
343
4
    computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
344
4
      memcpy(Dest, MD5::hash(Arr).data(), 16);
345
4
    });
346
2
    break;
347
16
  case BuildIdKind::Sha1:
348
8
    computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
349
8
      memcpy(Dest, SHA1::hash(Arr).data(), 20);
350
8
    });
351
4
    break;
352
16
  case BuildIdKind::Uuid:
353
1
    if (auto EC = getRandomBytes(HashBuf, HashSize))
354
0
      error("entropy source failure: " + EC.message());
355
1
    break;
356
16
  case BuildIdKind::Hexstring:
357
3
    memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
358
3
    break;
359
16
  default:
360
0
    llvm_unreachable("unknown BuildIdKind");
361
16
  }
362
16
}
363
364
EhFrameSection::EhFrameSection()
365
2.06k
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
366
367
// Search for an existing CIE record or create a new one.
368
// CIE records from input object files are uniquified by their contents
369
// and where their relocations point to.
370
template <class ELFT, class RelTy>
371
91
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
91
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
91
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
91
376
91
  Symbol *Personality = nullptr;
377
91
  unsigned FirstRelI = Cie.FirstRelocation;
378
91
  if (FirstRelI != (unsigned)-1)
379
11
    Personality =
380
11
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
91
382
91
  // Search for an existing CIE by CIE contents/relocation target pair.
383
91
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
91
385
91
  // If not found, create a new one.
386
91
  if (!Rec) {
387
77
    Rec = make<CieRecord>();
388
77
    Rec->Cie = &Cie;
389
77
    CieRecords.push_back(Rec);
390
77
  }
391
91
  return Rec;
392
91
}
Unexecuted instantiation: lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)1, false>, 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
371
4
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
4
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
4
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
4
376
4
  Symbol *Personality = nullptr;
377
4
  unsigned FirstRelI = Cie.FirstRelocation;
378
4
  if (FirstRelI != (unsigned)-1)
379
2
    Personality =
380
2
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
4
382
4
  // Search for an existing CIE by CIE contents/relocation target pair.
383
4
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
4
385
4
  // If not found, create a new one.
386
4
  if (!Rec) {
387
3
    Rec = make<CieRecord>();
388
3
    Rec->Cie = &Cie;
389
3
    CieRecords.push_back(Rec);
390
3
  }
391
4
  return Rec;
392
4
}
Unexecuted instantiation: lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)0, false>, 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::addCie<llvm::object::ELFType<(llvm::support::endianness)0, false>, 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
371
1
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
1
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
1
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
1
376
1
  Symbol *Personality = nullptr;
377
1
  unsigned FirstRelI = Cie.FirstRelocation;
378
1
  if (FirstRelI != (unsigned)-1)
379
0
    Personality =
380
0
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
1
382
1
  // Search for an existing CIE by CIE contents/relocation target pair.
383
1
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
1
385
1
  // If not found, create a new one.
386
1
  if (!Rec) {
387
1
    Rec = make<CieRecord>();
388
1
    Rec->Cie = &Cie;
389
1
    CieRecords.push_back(Rec);
390
1
  }
391
1
  return Rec;
392
1
}
lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)1, true>, 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
371
81
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
81
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
81
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
81
376
81
  Symbol *Personality = nullptr;
377
81
  unsigned FirstRelI = Cie.FirstRelocation;
378
81
  if (FirstRelI != (unsigned)-1)
379
8
    Personality =
380
8
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
81
382
81
  // Search for an existing CIE by CIE contents/relocation target pair.
383
81
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
81
385
81
  // If not found, create a new one.
386
81
  if (!Rec) {
387
68
    Rec = make<CieRecord>();
388
68
    Rec->Cie = &Cie;
389
68
    CieRecords.push_back(Rec);
390
68
  }
391
81
  return Rec;
392
81
}
lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)1, true>, 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> >)
Line
Count
Source
371
1
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
1
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
1
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
1
376
1
  Symbol *Personality = nullptr;
377
1
  unsigned FirstRelI = Cie.FirstRelocation;
378
1
  if (FirstRelI != (unsigned)-1)
379
0
    Personality =
380
0
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
1
382
1
  // Search for an existing CIE by CIE contents/relocation target pair.
383
1
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
1
385
1
  // If not found, create a new one.
386
1
  if (!Rec) {
387
1
    Rec = make<CieRecord>();
388
1
    Rec->Cie = &Cie;
389
1
    CieRecords.push_back(Rec);
390
1
  }
391
1
  return Rec;
392
1
}
lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)0, true>, 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
371
3
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
3
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
3
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
3
376
3
  Symbol *Personality = nullptr;
377
3
  unsigned FirstRelI = Cie.FirstRelocation;
378
3
  if (FirstRelI != (unsigned)-1)
379
1
    Personality =
380
1
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
3
382
3
  // Search for an existing CIE by CIE contents/relocation target pair.
383
3
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
3
385
3
  // If not found, create a new one.
386
3
  if (!Rec) {
387
3
    Rec = make<CieRecord>();
388
3
    Rec->Cie = &Cie;
389
3
    CieRecords.push_back(Rec);
390
3
  }
391
3
  return Rec;
392
3
}
lld::elf::CieRecord* lld::elf::EhFrameSection::addCie<llvm::object::ELFType<(llvm::support::endianness)0, true>, 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> >)
Line
Count
Source
371
1
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
372
1
  auto *Sec = cast<EhInputSection>(Cie.Sec);
373
1
  if (read32(Cie.data().data() + 4) != 0)
374
0
    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
375
1
376
1
  Symbol *Personality = nullptr;
377
1
  unsigned FirstRelI = Cie.FirstRelocation;
378
1
  if (FirstRelI != (unsigned)-1)
379
0
    Personality =
380
0
        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
381
1
382
1
  // Search for an existing CIE by CIE contents/relocation target pair.
383
1
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
384
1
385
1
  // If not found, create a new one.
386
1
  if (!Rec) {
387
1
    Rec = make<CieRecord>();
388
1
    Rec->Cie = &Cie;
389
1
    CieRecords.push_back(Rec);
390
1
  }
391
1
  return Rec;
392
1
}
393
394
// There is one FDE per function. Returns true if a given FDE
395
// points to a live function.
396
template <class ELFT, class RelTy>
397
121
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
398
121
  auto *Sec = cast<EhInputSection>(Fde.Sec);
399
121
  unsigned FirstRelI = Fde.FirstRelocation;
400
121
401
121
  // An FDE should point to some function because FDEs are to describe
402
121
  // functions. That's however not always the case due to an issue of
403
121
  // ld.gold with -r. ld.gold may discard only functions and leave their
404
121
  // corresponding FDEs, which results in creating bad .eh_frame sections.
405
121
  // To deal with that, we ignore such FDEs.
406
121
  if (FirstRelI == (unsigned)-1)
407
1
    return false;
408
120
409
120
  const RelTy &Rel = Rels[FirstRelI];
410
120
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
411
120
412
120
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
413
120
  if (auto *D = dyn_cast<Defined>(&B))
414
119
    if (SectionBase *Sec = D->Section)
415
119
      return Sec->Live;
416
1
  return false;
417
1
}
Unexecuted instantiation: bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, false>, 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::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, false>, 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
397
4
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
398
4
  auto *Sec = cast<EhInputSection>(Fde.Sec);
399
4
  unsigned FirstRelI = Fde.FirstRelocation;
400
4
401
4
  // An FDE should point to some function because FDEs are to describe
402
4
  // functions. That's however not always the case due to an issue of
403
4
  // ld.gold with -r. ld.gold may discard only functions and leave their
404
4
  // corresponding FDEs, which results in creating bad .eh_frame sections.
405
4
  // To deal with that, we ignore such FDEs.
406
4
  if (FirstRelI == (unsigned)-1)
407
0
    return false;
408
4
409
4
  const RelTy &Rel = Rels[FirstRelI];
410
4
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
411
4
412
4
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
413
4
  if (auto *D = dyn_cast<Defined>(&B))
414
4
    if (SectionBase *Sec = D->Section)
415
4
      return Sec->Live;
416
0
  return false;
417
0
}
Unexecuted instantiation: bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)0, false>, 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::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)0, false>, 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
397
1
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
398
1
  auto *Sec = cast<EhInputSection>(Fde.Sec);
399
1
  unsigned FirstRelI = Fde.FirstRelocation;
400
1
401
1
  // An FDE should point to some function because FDEs are to describe
402
1
  // functions. That's however not always the case due to an issue of
403
1
  // ld.gold with -r. ld.gold may discard only functions and leave their
404
1
  // corresponding FDEs, which results in creating bad .eh_frame sections.
405
1
  // To deal with that, we ignore such FDEs.
406
1
  if (FirstRelI == (unsigned)-1)
407
0
    return false;
408
1
409
1
  const RelTy &Rel = Rels[FirstRelI];
410
1
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
411
1
412
1
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
413
1
  if (auto *D = dyn_cast<Defined>(&B))
414
1
    if (SectionBase *Sec = D->Section)
415
1
      return Sec->Live;
416
0
  return false;
417
0
}
bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, true>, 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
397
112
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
398
112
  auto *Sec = cast<EhInputSection>(Fde.Sec);
399
112
  unsigned FirstRelI = Fde.FirstRelocation;
400
112
401
112
  // An FDE should point to some function because FDEs are to describe
402
112
  // functions. That's however not always the case due to an issue of
403
112
  // ld.gold with -r. ld.gold may discard only functions and leave their
404
112
  // corresponding FDEs, which results in creating bad .eh_frame sections.
405
112
  // To deal with that, we ignore such FDEs.
406
112
  if (FirstRelI == (unsigned)-1)
407
0
    return false;
408
112
409
112
  const RelTy &Rel = Rels[FirstRelI];
410
112
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
411
112
412
112
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
413
112
  if (auto *D = dyn_cast<Defined>(&B))
414
111
    if (SectionBase *Sec = D->Section)
415
111
      return Sec->Live;
416
1
  return false;
417
1
}
bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, true>, 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> >)
Line
Count
Source
397
1
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
398
1
  auto *Sec = cast<EhInputSection>(Fde.Sec);
399
1
  unsigned FirstRelI = Fde.FirstRelocation;
400
1
401
1
  // An FDE should point to some function because FDEs are to describe
402
1
  // functions. That's however not always the case due to an issue of
403
1
  // ld.gold with -r. ld.gold may discard only functions and leave their
404
1
  // corresponding FDEs, which results in creating bad .eh_frame sections.
405
1
  // To deal with that, we ignore such FDEs.
406
1
  if (FirstRelI == (unsigned)-1)
407
1
    return false;
408
0
409
0
  const RelTy &Rel = Rels[FirstRelI];
410
0
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
411
0
412
0
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
413
0
  if (auto *D = dyn_cast<Defined>(&B))
414
0
    if (SectionBase *Sec = D->Section)
415
0
      return Sec->Live;
416
0
  return false;
417
0
}
bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)0, true>, 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
397
3
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
398
3
  auto *Sec = cast<EhInputSection>(Fde.Sec);
399
3
  unsigned FirstRelI = Fde.FirstRelocation;
400
3
401
3
  // An FDE should point to some function because FDEs are to describe
402
3
  // functions. That's however not always the case due to an issue of
403
3
  // ld.gold with -r. ld.gold may discard only functions and leave their
404
3
  // corresponding FDEs, which results in creating bad .eh_frame sections.
405
3
  // To deal with that, we ignore such FDEs.
406
3
  if (FirstRelI == (unsigned)-1)
407
0
    return false;
408
3
409
3
  const RelTy &Rel = Rels[FirstRelI];
410
3
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
411
3
412
3
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
413
3
  if (auto *D = dyn_cast<Defined>(&B))
414
3
    if (SectionBase *Sec = D->Section)
415
3
      return Sec->Live;
416
0
  return false;
417
0
}
Unexecuted instantiation: bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)0, true>, 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> >)
418
419
// .eh_frame is a sequence of CIE or FDE records. In general, there
420
// is one CIE record per input object file which is followed by
421
// a list of FDEs. This function searches an existing CIE or create a new
422
// one and associates FDEs to the CIE.
423
template <class ELFT, class RelTy>
424
94
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
94
  OffsetToCie.clear();
426
217
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
217
    // The empty record is the end marker.
428
217
    if (Piece.Size == 4)
429
5
      return;
430
212
431
212
    size_t Offset = Piece.InputOff;
432
212
    uint32_t ID = read32(Piece.data().data() + 4);
433
212
    if (ID == 0) {
434
91
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
91
      continue;
436
91
    }
437
121
438
121
    uint32_t CieOffset = Offset + 4 - ID;
439
121
    CieRecord *Rec = OffsetToCie[CieOffset];
440
121
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
121
443
121
    if (!isFdeLive<ELFT>(Piece, Rels))
444
10
      continue;
445
111
    Rec->Fdes.push_back(&Piece);
446
111
    NumFdes++;
447
111
  }
448
94
}
Unexecuted instantiation: void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)1, false>, 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::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)1, false>, 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
424
4
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
4
  OffsetToCie.clear();
426
8
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
8
    // The empty record is the end marker.
428
8
    if (Piece.Size == 4)
429
0
      return;
430
8
431
8
    size_t Offset = Piece.InputOff;
432
8
    uint32_t ID = read32(Piece.data().data() + 4);
433
8
    if (ID == 0) {
434
4
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
4
      continue;
436
4
    }
437
4
438
4
    uint32_t CieOffset = Offset + 4 - ID;
439
4
    CieRecord *Rec = OffsetToCie[CieOffset];
440
4
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
4
443
4
    if (!isFdeLive<ELFT>(Piece, Rels))
444
0
      continue;
445
4
    Rec->Fdes.push_back(&Piece);
446
4
    NumFdes++;
447
4
  }
448
4
}
Unexecuted instantiation: void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >)
void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)0, false>, 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
424
1
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
1
  OffsetToCie.clear();
426
2
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
2
    // The empty record is the end marker.
428
2
    if (Piece.Size == 4)
429
0
      return;
430
2
431
2
    size_t Offset = Piece.InputOff;
432
2
    uint32_t ID = read32(Piece.data().data() + 4);
433
2
    if (ID == 0) {
434
1
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
1
      continue;
436
1
    }
437
1
438
1
    uint32_t CieOffset = Offset + 4 - ID;
439
1
    CieRecord *Rec = OffsetToCie[CieOffset];
440
1
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
1
443
1
    if (!isFdeLive<ELFT>(Piece, Rels))
444
0
      continue;
445
1
    Rec->Fdes.push_back(&Piece);
446
1
    NumFdes++;
447
1
  }
448
1
}
void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)1, true>, 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
424
79
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
79
  OffsetToCie.clear();
426
193
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
193
    // The empty record is the end marker.
428
193
    if (Piece.Size == 4)
429
0
      return;
430
193
431
193
    size_t Offset = Piece.InputOff;
432
193
    uint32_t ID = read32(Piece.data().data() + 4);
433
193
    if (ID == 0) {
434
81
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
81
      continue;
436
81
    }
437
112
438
112
    uint32_t CieOffset = Offset + 4 - ID;
439
112
    CieRecord *Rec = OffsetToCie[CieOffset];
440
112
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
112
443
112
    if (!isFdeLive<ELFT>(Piece, Rels))
444
9
      continue;
445
103
    Rec->Fdes.push_back(&Piece);
446
103
    NumFdes++;
447
103
  }
448
79
}
void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)1, true>, 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> >)
Line
Count
Source
424
6
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
6
  OffsetToCie.clear();
426
7
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
7
    // The empty record is the end marker.
428
7
    if (Piece.Size == 4)
429
5
      return;
430
2
431
2
    size_t Offset = Piece.InputOff;
432
2
    uint32_t ID = read32(Piece.data().data() + 4);
433
2
    if (ID == 0) {
434
1
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
1
      continue;
436
1
    }
437
1
438
1
    uint32_t CieOffset = Offset + 4 - ID;
439
1
    CieRecord *Rec = OffsetToCie[CieOffset];
440
1
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
1
443
1
    if (!isFdeLive<ELFT>(Piece, Rels))
444
1
      continue;
445
0
    Rec->Fdes.push_back(&Piece);
446
0
    NumFdes++;
447
0
  }
448
6
}
void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)0, true>, 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
424
3
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
3
  OffsetToCie.clear();
426
6
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
6
    // The empty record is the end marker.
428
6
    if (Piece.Size == 4)
429
0
      return;
430
6
431
6
    size_t Offset = Piece.InputOff;
432
6
    uint32_t ID = read32(Piece.data().data() + 4);
433
6
    if (ID == 0) {
434
3
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
3
      continue;
436
3
    }
437
3
438
3
    uint32_t CieOffset = Offset + 4 - ID;
439
3
    CieRecord *Rec = OffsetToCie[CieOffset];
440
3
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
3
443
3
    if (!isFdeLive<ELFT>(Piece, Rels))
444
0
      continue;
445
3
    Rec->Fdes.push_back(&Piece);
446
3
    NumFdes++;
447
3
  }
448
3
}
void lld::elf::EhFrameSection::addSectionAux<llvm::object::ELFType<(llvm::support::endianness)0, true>, 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> >)
Line
Count
Source
424
1
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
425
1
  OffsetToCie.clear();
426
1
  for (EhSectionPiece &Piece : Sec->Pieces) {
427
1
    // The empty record is the end marker.
428
1
    if (Piece.Size == 4)
429
0
      return;
430
1
431
1
    size_t Offset = Piece.InputOff;
432
1
    uint32_t ID = read32(Piece.data().data() + 4);
433
1
    if (ID == 0) {
434
1
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
435
1
      continue;
436
1
    }
437
0
438
0
    uint32_t CieOffset = Offset + 4 - ID;
439
0
    CieRecord *Rec = OffsetToCie[CieOffset];
440
0
    if (!Rec)
441
0
      fatal(toString(Sec) + ": invalid CIE reference");
442
0
443
0
    if (!isFdeLive<ELFT>(Piece, Rels))
444
0
      continue;
445
0
    Rec->Fdes.push_back(&Piece);
446
0
    NumFdes++;
447
0
  }
448
1
}
449
450
97
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
451
97
  auto *Sec = cast<EhInputSection>(C);
452
97
  Sec->Parent = this;
453
97
454
97
  Alignment = std::max(Alignment, Sec->Alignment);
455
97
  Sections.push_back(Sec);
456
97
457
97
  for (auto *DS : Sec->DependentSections)
458
3
    DependentSections.push_back(DS);
459
97
460
97
  if (Sec->Pieces.empty())
461
3
    return;
462
94
463
94
  if (Sec->AreRelocsRela)
464
82
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
465
12
  else
466
12
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
467
94
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
450
4
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
451
4
  auto *Sec = cast<EhInputSection>(C);
452
4
  Sec->Parent = this;
453
4
454
4
  Alignment = std::max(Alignment, Sec->Alignment);
455
4
  Sections.push_back(Sec);
456
4
457
4
  for (auto *DS : Sec->DependentSections)
458
0
    DependentSections.push_back(DS);
459
4
460
4
  if (Sec->Pieces.empty())
461
0
    return;
462
4
463
4
  if (Sec->AreRelocsRela)
464
0
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
465
4
  else
466
4
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
467
4
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
450
1
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
451
1
  auto *Sec = cast<EhInputSection>(C);
452
1
  Sec->Parent = this;
453
1
454
1
  Alignment = std::max(Alignment, Sec->Alignment);
455
1
  Sections.push_back(Sec);
456
1
457
1
  for (auto *DS : Sec->DependentSections)
458
0
    DependentSections.push_back(DS);
459
1
460
1
  if (Sec->Pieces.empty())
461
0
    return;
462
1
463
1
  if (Sec->AreRelocsRela)
464
0
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
465
1
  else
466
1
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
467
1
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
450
88
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
451
88
  auto *Sec = cast<EhInputSection>(C);
452
88
  Sec->Parent = this;
453
88
454
88
  Alignment = std::max(Alignment, Sec->Alignment);
455
88
  Sections.push_back(Sec);
456
88
457
88
  for (auto *DS : Sec->DependentSections)
458
3
    DependentSections.push_back(DS);
459
88
460
88
  if (Sec->Pieces.empty())
461
3
    return;
462
85
463
85
  if (Sec->AreRelocsRela)
464
79
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
465
6
  else
466
6
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
467
85
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
450
4
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
451
4
  auto *Sec = cast<EhInputSection>(C);
452
4
  Sec->Parent = this;
453
4
454
4
  Alignment = std::max(Alignment, Sec->Alignment);
455
4
  Sections.push_back(Sec);
456
4
457
4
  for (auto *DS : Sec->DependentSections)
458
0
    DependentSections.push_back(DS);
459
4
460
4
  if (Sec->Pieces.empty())
461
0
    return;
462
4
463
4
  if (Sec->AreRelocsRela)
464
3
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
465
1
  else
466
1
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
467
4
}
468
469
180
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
470
180
  memcpy(Buf, D.data(), D.size());
471
180
472
180
  size_t Aligned = alignTo(D.size(), Config->Wordsize);
473
180
474
180
  // Zero-clear trailing padding if it exists.
475
180
  memset(Buf + D.size(), 0, Aligned - D.size());
476
180
477
180
  // Fix the size field. -4 since size does not include the size field itself.
478
180
  write32(Buf, Aligned - 4);
479
180
}
480
481
80
void EhFrameSection::finalizeContents() {
482
80
  if (this->Size)
483
0
    return; // Already finalized.
484
80
485
80
  size_t Off = 0;
486
80
  for (CieRecord *Rec : CieRecords) {
487
75
    Rec->Cie->OutputOff = Off;
488
75
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
489
75
490
109
    for (EhSectionPiece *Fde : Rec->Fdes) {
491
109
      Fde->OutputOff = Off;
492
109
      Off += alignTo(Fde->Size, Config->Wordsize);
493
109
    }
494
75
  }
495
80
496
80
  // The LSB standard does not allow a .eh_frame section with zero
497
80
  // Call Frame Information records. glibc unwind-dw2-fde.c
498
80
  // classify_object_over_fdes expects there is a CIE record length 0 as a
499
80
  // terminator. Thus we add one unconditionally.
500
80
  Off += 4;
501
80
502
80
  this->Size = Off;
503
80
}
504
505
// Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table
506
// to get an FDE from an address to which FDE is applied. This function
507
// returns a list of such pairs.
508
15
std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const {
509
15
  uint8_t *Buf = getParent()->Loc + OutSecOff;
510
15
  std::vector<FdeData> Ret;
511
15
512
15
  for (CieRecord *Rec : CieRecords) {
513
15
    uint8_t Enc = getFdeEncoding(Rec->Cie);
514
17
    for (EhSectionPiece *Fde : Rec->Fdes) {
515
17
      uint32_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
516
17
      uint32_t FdeVA = getParent()->Addr + Fde->OutputOff;
517
17
      Ret.push_back({Pc, FdeVA});
518
17
    }
519
15
  }
520
15
  return Ret;
521
15
}
522
523
17
static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
524
17
  switch (Size) {
525
17
  case DW_EH_PE_udata2:
526
0
    return read16(Buf);
527
17
  case DW_EH_PE_udata4:
528
15
    return read32(Buf);
529
17
  case DW_EH_PE_udata8:
530
2
    return read64(Buf);
531
17
  case DW_EH_PE_absptr:
532
0
    return readUint(Buf);
533
0
  }
534
0
  fatal("unknown FDE size encoding");
535
0
}
536
537
// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
538
// We need it to create .eh_frame_hdr section.
539
uint64_t EhFrameSection::getFdePc(uint8_t *Buf, size_t FdeOff,
540
17
                                  uint8_t Enc) const {
541
17
  // The starting address to which this FDE applies is
542
17
  // stored at FDE + 8 byte.
543
17
  size_t Off = FdeOff + 8;
544
17
  uint64_t Addr = readFdeAddr(Buf + Off, Enc & 0x7);
545
17
  if ((Enc & 0x70) == DW_EH_PE_absptr)
546
3
    return Addr;
547
14
  if ((Enc & 0x70) == DW_EH_PE_pcrel)
548
14
    return Addr + getParent()->Addr + Off;
549
0
  fatal("unknown FDE size relative encoding");
550
0
}
551
552
78
void EhFrameSection::writeTo(uint8_t *Buf) {
553
78
  // Write CIE and FDE records.
554
78
  for (CieRecord *Rec : CieRecords) {
555
73
    size_t CieOffset = Rec->Cie->OutputOff;
556
73
    writeCieFde(Buf + CieOffset, Rec->Cie->data());
557
73
558
107
    for (EhSectionPiece *Fde : Rec->Fdes) {
559
107
      size_t Off = Fde->OutputOff;
560
107
      writeCieFde(Buf + Off, Fde->data());
561
107
562
107
      // FDE's second word should have the offset to an associated CIE.
563
107
      // Write it.
564
107
      write32(Buf + Off + 4, Off + 4 - CieOffset);
565
107
    }
566
73
  }
567
78
568
78
  // Apply relocations. .eh_frame section contents are not contiguous
569
78
  // in the output buffer, but relocateAlloc() still works because
570
78
  // getOffset() takes care of discontiguous section pieces.
571
78
  for (EhInputSection *S : Sections)
572
92
    S->relocateAlloc(Buf, nullptr);
573
78
}
574
575
GotSection::GotSection()
576
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
577
1.95k
                       Target->GotEntrySize, ".got") {
578
1.95k
  // PPC64 saves the ElfSym::GlobalOffsetTable .TOC. as the first entry in the
579
1.95k
  // .got. If there are no references to .TOC. in the symbol table,
580
1.95k
  // ElfSym::GlobalOffsetTable will not be defined and we won't need to save
581
1.95k
  // .TOC. in the .got. When it is defined, we increase NumEntries by the number
582
1.95k
  // of entries used to emit ElfSym::GlobalOffsetTable.
583
1.95k
  if (ElfSym::GlobalOffsetTable && 
!Target->GotBaseSymInGotPlt58
)
584
27
    NumEntries += Target->GotHeaderEntriesNum;
585
1.95k
}
586
587
134
void GotSection::addEntry(Symbol &Sym) {
588
134
  Sym.GotIndex = NumEntries;
589
134
  ++NumEntries;
590
134
}
591
592
46
bool GotSection::addDynTlsEntry(Symbol &Sym) {
593
46
  if (Sym.GlobalDynIndex != -1U)
594
15
    return false;
595
31
  Sym.GlobalDynIndex = NumEntries;
596
31
  // Global Dynamic TLS entries take two GOT slots.
597
31
  NumEntries += 2;
598
31
  return true;
599
31
}
600
601
// Reserves TLS entries for a TLS module ID and a TLS block offset.
602
// In total it takes two GOT slots.
603
27
bool GotSection::addTlsIndex() {
604
27
  if (TlsIndexOff != uint32_t(-1))
605
18
    return false;
606
9
  TlsIndexOff = NumEntries * Config->Wordsize;
607
9
  NumEntries += 2;
608
9
  return true;
609
9
}
610
611
31
uint64_t GotSection::getGlobalDynAddr(const Symbol &B) const {
612
31
  return this->getVA() + B.GlobalDynIndex * Config->Wordsize;
613
31
}
614
615
41
uint64_t GotSection::getGlobalDynOffset(const Symbol &B) const {
616
41
  return B.GlobalDynIndex * Config->Wordsize;
617
41
}
618
619
129
void GotSection::finalizeContents() {
620
129
  Size = NumEntries * Config->Wordsize;
621
129
}
622
623
3.93k
bool GotSection::empty() const {
624
3.93k
  // We need to emit a GOT even if it's empty if there's a relocation that is
625
3.93k
  // relative to GOT(such as GOTOFFREL) or there's a symbol that points to a GOT
626
3.93k
  // (i.e. _GLOBAL_OFFSET_TABLE_) that the target defines relative to the .got.
627
3.93k
  return NumEntries == 0 && 
!HasGotOffRel3.49k
&&
628
3.93k
         
!(3.46k
ElfSym::GlobalOffsetTable3.46k
&&
!Target->GotBaseSymInGotPlt42
);
629
3.93k
}
630
631
128
void GotSection::writeTo(uint8_t *Buf) {
632
128
  // Buf points to the start of this section's buffer,
633
128
  // whereas InputSectionBase::relocateAlloc() expects its argument
634
128
  // to point to the start of the output section.
635
128
  Target->writeGotHeader(Buf);
636
128
  Buf += Target->GotHeaderEntriesNum * Target->GotEntrySize;
637
128
  relocateAlloc(Buf - OutSecOff, Buf - OutSecOff + Size);
638
128
}
639
640
75
static uint64_t getMipsPageAddr(uint64_t Addr) {
641
75
  return (Addr + 0x8000) & ~0xffff;
642
75
}
643
644
15
static uint64_t getMipsPageCount(uint64_t Size) {
645
15
  return (Size + 0xfffe) / 0xffff + 1;
646
15
}
647
648
MipsGotSection::MipsGotSection()
649
    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
650
175
                       ".got") {}
651
652
void MipsGotSection::addEntry(InputFile &File, Symbol &Sym, int64_t Addend,
653
10.1k
                              RelExpr Expr) {
654
10.1k
  FileGot &G = getGot(File);
655
10.1k
  if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
656
30
    if (const OutputSection *OS = Sym.getOutputSection())
657
26
      G.PagesMap.insert({OS, {}});
658
4
    else
659
4
      G.Local16.insert({{nullptr, getMipsPageAddr(Sym.getVA(Addend))}, 0});
660
10.0k
  } else if (Sym.isTls())
661
13
    G.Tls.insert({&Sym, 0});
662
10.0k
  else if (Sym.IsPreemptible && 
Expr == R_ABS42
)
663
4
    G.Relocs.insert({&Sym, 0});
664
10.0k
  else if (Sym.IsPreemptible)
665
38
    G.Global.insert({&Sym, 0});
666
10.0k
  else if (Expr == R_MIPS_GOT_OFF32)
667
12
    G.Local32.insert({{&Sym, Addend}, 0});
668
10.0k
  else
669
10.0k
    G.Local16.insert({{&Sym, Addend}, 0});
670
10.1k
}
671
672
12
void MipsGotSection::addDynTlsEntry(InputFile &File, Symbol &Sym) {
673
12
  getGot(File).DynTlsSymbols.insert({&Sym, 0});
674
12
}
675
676
6
void MipsGotSection::addTlsIndex(InputFile &File) {
677
6
  getGot(File).DynTlsSymbols.insert({nullptr, 0});
678
6
}
679
680
112
size_t MipsGotSection::FileGot::getEntriesNum() const {
681
112
  return getPageEntriesNum() + Local16.size() + Global.size() + Relocs.size() +
682
112
         Tls.size() + DynTlsSymbols.size() * 2;
683
112
}
684
685
211
size_t MipsGotSection::FileGot::getPageEntriesNum() const {
686
211
  size_t Num = 0;
687
211
  for (const std::pair<const OutputSection *, FileGot::PageBlock> &P : PagesMap)
688
58
    Num += P.second.Count;
689
211
  return Num;
690
211
}
691
692
55
size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
693
55
  size_t Count = getPageEntriesNum() + Local16.size() + Global.size();
694
55
  // If there are relocation-only entries in the GOT, TLS entries
695
55
  // are allocated after them. TLS entries should be addressable
696
55
  // by 16-bit index so count both reloc-only and TLS entries.
697
55
  if (!Tls.empty() || 
!DynTlsSymbols.empty()47
)
698
9
    Count += Relocs.size() + Tls.size() + DynTlsSymbols.size() * 2;
699
55
  return Count;
700
55
}
701
702
10.1k
MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &F) {
703
10.1k
  if (!F.MipsGotIndex.hasValue()) {
704
55
    Gots.emplace_back();
705
55
    Gots.back().File = &F;
706
55
    F.MipsGotIndex = Gots.size() - 1;
707
55
  }
708
10.1k
  return Gots[*F.MipsGotIndex];
709
10.1k
}
710
711
uint64_t MipsGotSection::getPageEntryOffset(const InputFile *F,
712
                                            const Symbol &Sym,
713
30
                                            int64_t Addend) const {
714
30
  const FileGot &G = Gots[*F->MipsGotIndex];
715
30
  uint64_t Index = 0;
716
30
  if (const OutputSection *OutSec = Sym.getOutputSection()) {
717
26
    uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
718
26
    uint64_t SymAddr = getMipsPageAddr(Sym.getVA(Addend));
719
26
    Index = G.PagesMap.lookup(OutSec).FirstIndex + (SymAddr - SecAddr) / 0xffff;
720
26
  } else {
721
4
    Index = G.Local16.lookup({nullptr, getMipsPageAddr(Sym.getVA(Addend))});
722
4
  }
723
30
  return Index * Config->Wordsize;
724
30
}
725
726
uint64_t MipsGotSection::getSymEntryOffset(const InputFile *F, const Symbol &S,
727
10.0k
                                           int64_t Addend) const {
728
10.0k
  const FileGot &G = Gots[*F->MipsGotIndex];
729
10.0k
  Symbol *Sym = const_cast<Symbol *>(&S);
730
10.0k
  if (Sym->isTls())
731
13
    return G.Tls.lookup(Sym) * Config->Wordsize;
732
10.0k
  if (Sym->IsPreemptible)
733
38
    return G.Global.lookup(Sym) * Config->Wordsize;
734
10.0k
  return G.Local16.lookup({Sym, Addend}) * Config->Wordsize;
735
10.0k
}
736
737
6
uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *F) const {
738
6
  const FileGot &G = Gots[*F->MipsGotIndex];
739
6
  return G.DynTlsSymbols.lookup(nullptr) * Config->Wordsize;
740
6
}
741
742
uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *F,
743
12
                                            const Symbol &S) const {
744
12
  const FileGot &G = Gots[*F->MipsGotIndex];
745
12
  Symbol *Sym = const_cast<Symbol *>(&S);
746
12
  return G.DynTlsSymbols.lookup(Sym) * Config->Wordsize;
747
12
}
748
749
100
const Symbol *MipsGotSection::getFirstGlobalEntry() const {
750
100
  if (Gots.empty())
751
56
    return nullptr;
752
44
  const FileGot &PrimGot = Gots.front();
753
44
  if (!PrimGot.Global.empty())
754
28
    return PrimGot.Global.front().first;
755
16
  if (!PrimGot.Relocs.empty())
756
5
    return PrimGot.Relocs.front().first;
757
11
  return nullptr;
758
11
}
759
760
100
unsigned MipsGotSection::getLocalEntriesNum() const {
761
100
  if (Gots.empty())
762
56
    return HeaderEntriesNum;
763
44
  return HeaderEntriesNum + Gots.front().getPageEntriesNum() +
764
44
         Gots.front().Local16.size();
765
44
}
766
767
55
bool MipsGotSection::tryMergeGots(FileGot &Dst, FileGot &Src, bool IsPrimary) {
768
55
  FileGot Tmp = Dst;
769
55
  set_union(Tmp.PagesMap, Src.PagesMap);
770
55
  set_union(Tmp.Local16, Src.Local16);
771
55
  set_union(Tmp.Global, Src.Global);
772
55
  set_union(Tmp.Relocs, Src.Relocs);
773
55
  set_union(Tmp.Tls, Src.Tls);
774
55
  set_union(Tmp.DynTlsSymbols, Src.DynTlsSymbols);
775
55
776
55
  size_t Count = IsPrimary ? 
HeaderEntriesNum53
:
02
;
777
55
  Count += Tmp.getIndexedEntriesNum();
778
55
779
55
  if (Count * Config->Wordsize > Config->MipsGotSize)
780
3
    return false;
781
52
782
52
  std::swap(Tmp, Dst);
783
52
  return true;
784
52
}
785
786
159
void MipsGotSection::finalizeContents() { updateAllocSize(); }
787
788
342
bool MipsGotSection::updateAllocSize() {
789
342
  Size = HeaderEntriesNum * Config->Wordsize;
790
342
  for (const FileGot &G : Gots)
791
112
    Size += G.getEntriesNum() * Config->Wordsize;
792
342
  return false;
793
342
}
794
795
171
template <class ELFT> void MipsGotSection::build() {
796
171
  if (Gots.empty())
797
118
    return;
798
53
799
53
  std::vector<FileGot> MergedGots(1);
800
53
801
53
  // For each GOT move non-preemptible symbols from the `Global`
802
53
  // to `Local16` list. Preemptible symbol might become non-preemptible
803
53
  // one if, for example, it gets a related copy relocation.
804
55
  for (FileGot &Got : Gots) {
805
55
    for (auto &P: Got.Global)
806
36
      if (!P.first->IsPreemptible)
807
0
        Got.Local16.insert({{P.first, 0}, 0});
808
55
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
36
      return !P.first->IsPreemptible;
810
36
    });
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
808
3
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
3
      return !P.first->IsPreemptible;
810
3
    });
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
808
28
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
28
      return !P.first->IsPreemptible;
810
28
    });
Unexecuted instantiation: void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
808
5
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
5
      return !P.first->IsPreemptible;
810
5
    });
811
55
  }
812
53
813
53
  // For each GOT remove "reloc-only" entry if there is "global"
814
53
  // entry for the same symbol. And add local entries which indexed
815
53
  // using 32-bit value at the end of 16-bit entries.
816
55
  for (FileGot &Got : Gots) {
817
55
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
4
      return Got.Global.count(P.first);
819
4
    });
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda0'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
817
1
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
1
      return Got.Global.count(P.first);
819
1
    });
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda0'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
817
1
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
1
      return Got.Global.count(P.first);
819
1
    });
Unexecuted instantiation: void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda0'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda0'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
817
2
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
2
      return Got.Global.count(P.first);
819
2
    });
820
55
    set_union(Got.Local16, Got.Local32);
821
55
    Got.Local32.clear();
822
55
  }
823
53
824
53
  // Evaluate number of "reloc-only" entries in the resulting GOT.
825
53
  // To do that put all unique "reloc-only" and "global" entries
826
53
  // from all GOTs to the future primary GOT.
827
53
  FileGot *PrimGot = &MergedGots.front();
828
55
  for (FileGot &Got : Gots) {
829
55
    set_union(PrimGot->Relocs, Got.Global);
830
55
    set_union(PrimGot->Relocs, Got.Relocs);
831
55
    Got.Relocs.clear();
832
55
  }
833
53
834
53
  // Evaluate number of "page" entries in each GOT.
835
55
  for (FileGot &Got : Gots) {
836
55
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
837
55
         Got.PagesMap) {
838
15
      const OutputSection *OS = P.first;
839
15
      uint64_t SecSize = 0;
840
17
      for (BaseCommand *Cmd : OS->SectionCommands) {
841
17
        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
842
25
          
for (InputSection *IS : ISD->Sections)16
{
843
25
            uint64_t Off = alignTo(SecSize, IS->Alignment);
844
25
            SecSize = Off + IS->getSize();
845
25
          }
846
17
      }
847
15
      P.second.Count = getMipsPageCount(SecSize);
848
15
    }
849
55
  }
850
53
851
53
  // Merge GOTs. Try to join as much as possible GOTs but do not
852
53
  // exceed maximum GOT size. In case of overflow create new GOT
853
53
  // and continue merging.
854
55
  for (FileGot &SrcGot : Gots) {
855
55
    FileGot &DstGot = MergedGots.back();
856
55
    InputFile *File = SrcGot.File;
857
55
    if (!tryMergeGots(DstGot, SrcGot, &DstGot == PrimGot)) {
858
3
      MergedGots.emplace_back();
859
3
      std::swap(MergedGots.back(), SrcGot);
860
3
    }
861
55
    File->MipsGotIndex = MergedGots.size() - 1;
862
55
  }
863
53
  std::swap(Gots, MergedGots);
864
53
865
53
  // Reduce number of "reloc-only" entries in the primary GOT
866
53
  // by substracting "global" entries exist in the primary GOT.
867
53
  PrimGot = &Gots.front();
868
53
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
39
    return PrimGot->Global.count(P.first);
870
39
  });
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda1'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
868
4
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
4
    return PrimGot->Global.count(P.first);
870
4
  });
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda1'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
868
28
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
28
    return PrimGot->Global.count(P.first);
870
28
  });
Unexecuted instantiation: void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda1'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda1'(std::__1::pair<lld::elf::Symbol*, unsigned long> const&)::operator()(std::__1::pair<lld::elf::Symbol*, unsigned long> const&) const
Line
Count
Source
868
7
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
7
    return PrimGot->Global.count(P.first);
870
7
  });
871
53
872
53
  // Calculate indexes for each GOT entry.
873
53
  size_t Index = HeaderEntriesNum;
874
56
  for (FileGot &Got : Gots) {
875
56
    Got.StartIndex = &Got == PrimGot ? 
053
:
Index3
;
876
56
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
877
56
         Got.PagesMap) {
878
15
      // For each output section referenced by GOT page relocations calculate
879
15
      // and save into PagesMap an upper bound of MIPS GOT entries required
880
15
      // to store page addresses of local symbols. We assume the worst case -
881
15
      // each 64kb page of the output section has at least one GOT relocation
882
15
      // against it. And take in account the case when the section intersects
883
15
      // page boundaries.
884
15
      P.second.FirstIndex = Index;
885
15
      Index += P.second.Count;
886
15
    }
887
56
    for (auto &P: Got.Local16)
888
10.0k
      P.second = Index++;
889
56
    for (auto &P: Got.Global)
890
36
      P.second = Index++;
891
56
    for (auto &P: Got.Relocs)
892
6
      P.second = Index++;
893
56
    for (auto &P: Got.Tls)
894
12
      P.second = Index++;
895
56
    for (auto &P: Got.DynTlsSymbols) {
896
18
      P.second = Index;
897
18
      Index += 2;
898
18
    }
899
56
  }
900
53
901
53
  // Update Symbol::GotIndex field to use this
902
53
  // value later in the `sortMipsSymbols` function.
903
53
  for (auto &P : PrimGot->Global)
904
33
    P.first->GotIndex = P.second;
905
53
  for (auto &P : PrimGot->Relocs)
906
6
    P.first->GotIndex = P.second;
907
53
908
53
  // Create dynamic relocations.
909
56
  for (FileGot &Got : Gots) {
910
56
    // Create dynamic relocations for TLS entries.
911
56
    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
912
12
      Symbol *S = P.first;
913
12
      uint64_t Offset = P.second * Config->Wordsize;
914
12
      if (S->IsPreemptible)
915
9
        InX::RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
916
12
    }
917
56
    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
918
18
      Symbol *S = P.first;
919
18
      uint64_t Offset = P.second * Config->Wordsize;
920
18
      if (S == nullptr) {
921
6
        if (!Config->Pic)
922
3
          continue;
923
3
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
924
12
      } else {
925
12
        // When building a shared library we still need a dynamic relocation
926
12
        // for the module index. Therefore only checking for
927
12
        // S->IsPreemptible is not sufficient (this happens e.g. for
928
12
        // thread-locals that have been marked as local through a linker script)
929
12
        if (!S->IsPreemptible && 
!Config->Pic4
)
930
3
          continue;
931
9
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
932
9
        // However, we can skip writing the TLS offset reloc for non-preemptible
933
9
        // symbols since it is known even in shared libraries
934
9
        if (!S->IsPreemptible)
935
1
          continue;
936
8
        Offset += Config->Wordsize;
937
8
        InX::RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
938
8
      }
939
18
    }
940
56
941
56
    // Do not create dynamic relocations for non-TLS
942
56
    // entries in the primary GOT.
943
56
    if (&Got == PrimGot)
944
53
      continue;
945
3
946
3
    // Dynamic relocations for "global" entries.
947
3
    for (const std::pair<Symbol *, size_t> &P : Got.Global) {
948
3
      uint64_t Offset = P.second * Config->Wordsize;
949
3
      InX::RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
950
3
    }
951
3
    if (!Config->Pic)
952
1
      continue;
953
2
    // Dynamic relocations for "local" entries in case of PIC.
954
2
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
955
2
         Got.PagesMap) {
956
2
      size_t PageCount = L.second.Count;
957
14
      for (size_t PI = 0; PI < PageCount; 
++PI12
) {
958
12
        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
959
12
        InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
960
12
                                int64_t(PI * 0x10000)});
961
12
      }
962
2
    }
963
2
    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
964
0
      uint64_t Offset = P.second * Config->Wordsize;
965
0
      InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
966
0
                              P.first.first, P.first.second});
967
0
    }
968
2
  }
969
53
}
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
795
17
template <class ELFT> void MipsGotSection::build() {
796
17
  if (Gots.empty())
797
12
    return;
798
5
799
5
  std::vector<FileGot> MergedGots(1);
800
5
801
5
  // For each GOT move non-preemptible symbols from the `Global`
802
5
  // to `Local16` list. Preemptible symbol might become non-preemptible
803
5
  // one if, for example, it gets a related copy relocation.
804
5
  for (FileGot &Got : Gots) {
805
5
    for (auto &P: Got.Global)
806
3
      if (!P.first->IsPreemptible)
807
0
        Got.Local16.insert({{P.first, 0}, 0});
808
5
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
5
      return !P.first->IsPreemptible;
810
5
    });
811
5
  }
812
5
813
5
  // For each GOT remove "reloc-only" entry if there is "global"
814
5
  // entry for the same symbol. And add local entries which indexed
815
5
  // using 32-bit value at the end of 16-bit entries.
816
5
  for (FileGot &Got : Gots) {
817
5
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
5
      return Got.Global.count(P.first);
819
5
    });
820
5
    set_union(Got.Local16, Got.Local32);
821
5
    Got.Local32.clear();
822
5
  }
823
5
824
5
  // Evaluate number of "reloc-only" entries in the resulting GOT.
825
5
  // To do that put all unique "reloc-only" and "global" entries
826
5
  // from all GOTs to the future primary GOT.
827
5
  FileGot *PrimGot = &MergedGots.front();
828
5
  for (FileGot &Got : Gots) {
829
5
    set_union(PrimGot->Relocs, Got.Global);
830
5
    set_union(PrimGot->Relocs, Got.Relocs);
831
5
    Got.Relocs.clear();
832
5
  }
833
5
834
5
  // Evaluate number of "page" entries in each GOT.
835
5
  for (FileGot &Got : Gots) {
836
5
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
837
5
         Got.PagesMap) {
838
0
      const OutputSection *OS = P.first;
839
0
      uint64_t SecSize = 0;
840
0
      for (BaseCommand *Cmd : OS->SectionCommands) {
841
0
        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
842
0
          for (InputSection *IS : ISD->Sections) {
843
0
            uint64_t Off = alignTo(SecSize, IS->Alignment);
844
0
            SecSize = Off + IS->getSize();
845
0
          }
846
0
      }
847
0
      P.second.Count = getMipsPageCount(SecSize);
848
0
    }
849
5
  }
850
5
851
5
  // Merge GOTs. Try to join as much as possible GOTs but do not
852
5
  // exceed maximum GOT size. In case of overflow create new GOT
853
5
  // and continue merging.
854
5
  for (FileGot &SrcGot : Gots) {
855
5
    FileGot &DstGot = MergedGots.back();
856
5
    InputFile *File = SrcGot.File;
857
5
    if (!tryMergeGots(DstGot, SrcGot, &DstGot == PrimGot)) {
858
0
      MergedGots.emplace_back();
859
0
      std::swap(MergedGots.back(), SrcGot);
860
0
    }
861
5
    File->MipsGotIndex = MergedGots.size() - 1;
862
5
  }
863
5
  std::swap(Gots, MergedGots);
864
5
865
5
  // Reduce number of "reloc-only" entries in the primary GOT
866
5
  // by substracting "global" entries exist in the primary GOT.
867
5
  PrimGot = &Gots.front();
868
5
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
5
    return PrimGot->Global.count(P.first);
870
5
  });
871
5
872
5
  // Calculate indexes for each GOT entry.
873
5
  size_t Index = HeaderEntriesNum;
874
5
  for (FileGot &Got : Gots) {
875
5
    Got.StartIndex = &Got == PrimGot ? 0 : 
Index0
;
876
5
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
877
5
         Got.PagesMap) {
878
0
      // For each output section referenced by GOT page relocations calculate
879
0
      // and save into PagesMap an upper bound of MIPS GOT entries required
880
0
      // to store page addresses of local symbols. We assume the worst case -
881
0
      // each 64kb page of the output section has at least one GOT relocation
882
0
      // against it. And take in account the case when the section intersects
883
0
      // page boundaries.
884
0
      P.second.FirstIndex = Index;
885
0
      Index += P.second.Count;
886
0
    }
887
5
    for (auto &P: Got.Local16)
888
2
      P.second = Index++;
889
5
    for (auto &P: Got.Global)
890
3
      P.second = Index++;
891
5
    for (auto &P: Got.Relocs)
892
1
      P.second = Index++;
893
5
    for (auto &P: Got.Tls)
894
0
      P.second = Index++;
895
5
    for (auto &P: Got.DynTlsSymbols) {
896
0
      P.second = Index;
897
0
      Index += 2;
898
0
    }
899
5
  }
900
5
901
5
  // Update Symbol::GotIndex field to use this
902
5
  // value later in the `sortMipsSymbols` function.
903
5
  for (auto &P : PrimGot->Global)
904
3
    P.first->GotIndex = P.second;
905
5
  for (auto &P : PrimGot->Relocs)
906
1
    P.first->GotIndex = P.second;
907
5
908
5
  // Create dynamic relocations.
909
5
  for (FileGot &Got : Gots) {
910
5
    // Create dynamic relocations for TLS entries.
911
5
    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
912
0
      Symbol *S = P.first;
913
0
      uint64_t Offset = P.second * Config->Wordsize;
914
0
      if (S->IsPreemptible)
915
0
        InX::RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
916
0
    }
917
5
    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
918
0
      Symbol *S = P.first;
919
0
      uint64_t Offset = P.second * Config->Wordsize;
920
0
      if (S == nullptr) {
921
0
        if (!Config->Pic)
922
0
          continue;
923
0
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
924
0
      } else {
925
0
        // When building a shared library we still need a dynamic relocation
926
0
        // for the module index. Therefore only checking for
927
0
        // S->IsPreemptible is not sufficient (this happens e.g. for
928
0
        // thread-locals that have been marked as local through a linker script)
929
0
        if (!S->IsPreemptible && !Config->Pic)
930
0
          continue;
931
0
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
932
0
        // However, we can skip writing the TLS offset reloc for non-preemptible
933
0
        // symbols since it is known even in shared libraries
934
0
        if (!S->IsPreemptible)
935
0
          continue;
936
0
        Offset += Config->Wordsize;
937
0
        InX::RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
938
0
      }
939
0
    }
940
5
941
5
    // Do not create dynamic relocations for non-TLS
942
5
    // entries in the primary GOT.
943
5
    if (&Got == PrimGot)
944
5
      continue;
945
0
946
0
    // Dynamic relocations for "global" entries.
947
0
    for (const std::pair<Symbol *, size_t> &P : Got.Global) {
948
0
      uint64_t Offset = P.second * Config->Wordsize;
949
0
      InX::RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
950
0
    }
951
0
    if (!Config->Pic)
952
0
      continue;
953
0
    // Dynamic relocations for "local" entries in case of PIC.
954
0
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
955
0
         Got.PagesMap) {
956
0
      size_t PageCount = L.second.Count;
957
0
      for (size_t PI = 0; PI < PageCount; ++PI) {
958
0
        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
959
0
        InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
960
0
                                int64_t(PI * 0x10000)});
961
0
      }
962
0
    }
963
0
    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
964
0
      uint64_t Offset = P.second * Config->Wordsize;
965
0
      InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
966
0
                              P.first.first, P.first.second});
967
0
    }
968
0
  }
969
5
}
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
795
113
template <class ELFT> void MipsGotSection::build() {
796
113
  if (Gots.empty())
797
78
    return;
798
35
799
35
  std::vector<FileGot> MergedGots(1);
800
35
801
35
  // For each GOT move non-preemptible symbols from the `Global`
802
35
  // to `Local16` list. Preemptible symbol might become non-preemptible
803
35
  // one if, for example, it gets a related copy relocation.
804
37
  for (FileGot &Got : Gots) {
805
37
    for (auto &P: Got.Global)
806
28
      if (!P.first->IsPreemptible)
807
0
        Got.Local16.insert({{P.first, 0}, 0});
808
37
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
37
      return !P.first->IsPreemptible;
810
37
    });
811
37
  }
812
35
813
35
  // For each GOT remove "reloc-only" entry if there is "global"
814
35
  // entry for the same symbol. And add local entries which indexed
815
35
  // using 32-bit value at the end of 16-bit entries.
816
37
  for (FileGot &Got : Gots) {
817
37
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
37
      return Got.Global.count(P.first);
819
37
    });
820
37
    set_union(Got.Local16, Got.Local32);
821
37
    Got.Local32.clear();
822
37
  }
823
35
824
35
  // Evaluate number of "reloc-only" entries in the resulting GOT.
825
35
  // To do that put all unique "reloc-only" and "global" entries
826
35
  // from all GOTs to the future primary GOT.
827
35
  FileGot *PrimGot = &MergedGots.front();
828
37
  for (FileGot &Got : Gots) {
829
37
    set_union(PrimGot->Relocs, Got.Global);
830
37
    set_union(PrimGot->Relocs, Got.Relocs);
831
37
    Got.Relocs.clear();
832
37
  }
833
35
834
35
  // Evaluate number of "page" entries in each GOT.
835
37
  for (FileGot &Got : Gots) {
836
37
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
837
37
         Got.PagesMap) {
838
10
      const OutputSection *OS = P.first;
839
10
      uint64_t SecSize = 0;
840
12
      for (BaseCommand *Cmd : OS->SectionCommands) {
841
12
        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
842
18
          
for (InputSection *IS : ISD->Sections)11
{
843
18
            uint64_t Off = alignTo(SecSize, IS->Alignment);
844
18
            SecSize = Off + IS->getSize();
845
18
          }
846
12
      }
847
10
      P.second.Count = getMipsPageCount(SecSize);
848
10
    }
849
37
  }
850
35
851
35
  // Merge GOTs. Try to join as much as possible GOTs but do not
852
35
  // exceed maximum GOT size. In case of overflow create new GOT
853
35
  // and continue merging.
854
37
  for (FileGot &SrcGot : Gots) {
855
37
    FileGot &DstGot = MergedGots.back();
856
37
    InputFile *File = SrcGot.File;
857
37
    if (!tryMergeGots(DstGot, SrcGot, &DstGot == PrimGot)) {
858
2
      MergedGots.emplace_back();
859
2
      std::swap(MergedGots.back(), SrcGot);
860
2
    }
861
37
    File->MipsGotIndex = MergedGots.size() - 1;
862
37
  }
863
35
  std::swap(Gots, MergedGots);
864
35
865
35
  // Reduce number of "reloc-only" entries in the primary GOT
866
35
  // by substracting "global" entries exist in the primary GOT.
867
35
  PrimGot = &Gots.front();
868
35
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
35
    return PrimGot->Global.count(P.first);
870
35
  });
871
35
872
35
  // Calculate indexes for each GOT entry.
873
35
  size_t Index = HeaderEntriesNum;
874
37
  for (FileGot &Got : Gots) {
875
37
    Got.StartIndex = &Got == PrimGot ? 
035
:
Index2
;
876
37
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
877
37
         Got.PagesMap) {
878
10
      // For each output section referenced by GOT page relocations calculate
879
10
      // and save into PagesMap an upper bound of MIPS GOT entries required
880
10
      // to store page addresses of local symbols. We assume the worst case -
881
10
      // each 64kb page of the output section has at least one GOT relocation
882
10
      // against it. And take in account the case when the section intersects
883
10
      // page boundaries.
884
10
      P.second.FirstIndex = Index;
885
10
      Index += P.second.Count;
886
10
    }
887
37
    for (auto &P: Got.Local16)
888
16
      P.second = Index++;
889
37
    for (auto &P: Got.Global)
890
28
      P.second = Index++;
891
37
    for (auto &P: Got.Relocs)
892
3
      P.second = Index++;
893
37
    for (auto &P: Got.Tls)
894
8
      P.second = Index++;
895
37
    for (auto &P: Got.DynTlsSymbols) {
896
11
      P.second = Index;
897
11
      Index += 2;
898
11
    }
899
37
  }
900
35
901
35
  // Update Symbol::GotIndex field to use this
902
35
  // value later in the `sortMipsSymbols` function.
903
35
  for (auto &P : PrimGot->Global)
904
25
    P.first->GotIndex = P.second;
905
35
  for (auto &P : PrimGot->Relocs)
906
3
    P.first->GotIndex = P.second;
907
35
908
35
  // Create dynamic relocations.
909
37
  for (FileGot &Got : Gots) {
910
37
    // Create dynamic relocations for TLS entries.
911
37
    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
912
8
      Symbol *S = P.first;
913
8
      uint64_t Offset = P.second * Config->Wordsize;
914
8
      if (S->IsPreemptible)
915
6
        InX::RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
916
8
    }
917
37
    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
918
11
      Symbol *S = P.first;
919
11
      uint64_t Offset = P.second * Config->Wordsize;
920
11
      if (S == nullptr) {
921
4
        if (!Config->Pic)
922
2
          continue;
923
2
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
924
7
      } else {
925
7
        // When building a shared library we still need a dynamic relocation
926
7
        // for the module index. Therefore only checking for
927
7
        // S->IsPreemptible is not sufficient (this happens e.g. for
928
7
        // thread-locals that have been marked as local through a linker script)
929
7
        if (!S->IsPreemptible && 
!Config->Pic2
)
930
2
          continue;
931
5
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
932
5
        // However, we can skip writing the TLS offset reloc for non-preemptible
933
5
        // symbols since it is known even in shared libraries
934
5
        if (!S->IsPreemptible)
935
0
          continue;
936
5
        Offset += Config->Wordsize;
937
5
        InX::RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
938
5
      }
939
11
    }
940
37
941
37
    // Do not create dynamic relocations for non-TLS
942
37
    // entries in the primary GOT.
943
37
    if (&Got == PrimGot)
944
35
      continue;
945
2
946
2
    // Dynamic relocations for "global" entries.
947
3
    
for (const std::pair<Symbol *, size_t> &P : Got.Global)2
{
948
3
      uint64_t Offset = P.second * Config->Wordsize;
949
3
      InX::RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
950
3
    }
951
2
    if (!Config->Pic)
952
0
      continue;
953
2
    // Dynamic relocations for "local" entries in case of PIC.
954
2
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
955
2
         Got.PagesMap) {
956
2
      size_t PageCount = L.second.Count;
957
14
      for (size_t PI = 0; PI < PageCount; 
++PI12
) {
958
12
        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
959
12
        InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
960
12
                                int64_t(PI * 0x10000)});
961
12
      }
962
2
    }
963
2
    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
964
0
      uint64_t Offset = P.second * Config->Wordsize;
965
0
      InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
966
0
                              P.first.first, P.first.second});
967
0
    }
968
2
  }
969
35
}
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
795
3
template <class ELFT> void MipsGotSection::build() {
796
3
  if (Gots.empty())
797
3
    return;
798
0
799
0
  std::vector<FileGot> MergedGots(1);
800
0
801
0
  // For each GOT move non-preemptible symbols from the `Global`
802
0
  // to `Local16` list. Preemptible symbol might become non-preemptible
803
0
  // one if, for example, it gets a related copy relocation.
804
0
  for (FileGot &Got : Gots) {
805
0
    for (auto &P: Got.Global)
806
0
      if (!P.first->IsPreemptible)
807
0
        Got.Local16.insert({{P.first, 0}, 0});
808
0
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
0
      return !P.first->IsPreemptible;
810
0
    });
811
0
  }
812
0
813
0
  // For each GOT remove "reloc-only" entry if there is "global"
814
0
  // entry for the same symbol. And add local entries which indexed
815
0
  // using 32-bit value at the end of 16-bit entries.
816
0
  for (FileGot &Got : Gots) {
817
0
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
0
      return Got.Global.count(P.first);
819
0
    });
820
0
    set_union(Got.Local16, Got.Local32);
821
0
    Got.Local32.clear();
822
0
  }
823
0
824
0
  // Evaluate number of "reloc-only" entries in the resulting GOT.
825
0
  // To do that put all unique "reloc-only" and "global" entries
826
0
  // from all GOTs to the future primary GOT.
827
0
  FileGot *PrimGot = &MergedGots.front();
828
0
  for (FileGot &Got : Gots) {
829
0
    set_union(PrimGot->Relocs, Got.Global);
830
0
    set_union(PrimGot->Relocs, Got.Relocs);
831
0
    Got.Relocs.clear();
832
0
  }
833
0
834
0
  // Evaluate number of "page" entries in each GOT.
835
0
  for (FileGot &Got : Gots) {
836
0
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
837
0
         Got.PagesMap) {
838
0
      const OutputSection *OS = P.first;
839
0
      uint64_t SecSize = 0;
840
0
      for (BaseCommand *Cmd : OS->SectionCommands) {
841
0
        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
842
0
          for (InputSection *IS : ISD->Sections) {
843
0
            uint64_t Off = alignTo(SecSize, IS->Alignment);
844
0
            SecSize = Off + IS->getSize();
845
0
          }
846
0
      }
847
0
      P.second.Count = getMipsPageCount(SecSize);
848
0
    }
849
0
  }
850
0
851
0
  // Merge GOTs. Try to join as much as possible GOTs but do not
852
0
  // exceed maximum GOT size. In case of overflow create new GOT
853
0
  // and continue merging.
854
0
  for (FileGot &SrcGot : Gots) {
855
0
    FileGot &DstGot = MergedGots.back();
856
0
    InputFile *File = SrcGot.File;
857
0
    if (!tryMergeGots(DstGot, SrcGot, &DstGot == PrimGot)) {
858
0
      MergedGots.emplace_back();
859
0
      std::swap(MergedGots.back(), SrcGot);
860
0
    }
861
0
    File->MipsGotIndex = MergedGots.size() - 1;
862
0
  }
863
0
  std::swap(Gots, MergedGots);
864
0
865
0
  // Reduce number of "reloc-only" entries in the primary GOT
866
0
  // by substracting "global" entries exist in the primary GOT.
867
0
  PrimGot = &Gots.front();
868
0
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
0
    return PrimGot->Global.count(P.first);
870
0
  });
871
0
872
0
  // Calculate indexes for each GOT entry.
873
0
  size_t Index = HeaderEntriesNum;
874
0
  for (FileGot &Got : Gots) {
875
0
    Got.StartIndex = &Got == PrimGot ? 0 : Index;
876
0
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
877
0
         Got.PagesMap) {
878
0
      // For each output section referenced by GOT page relocations calculate
879
0
      // and save into PagesMap an upper bound of MIPS GOT entries required
880
0
      // to store page addresses of local symbols. We assume the worst case -
881
0
      // each 64kb page of the output section has at least one GOT relocation
882
0
      // against it. And take in account the case when the section intersects
883
0
      // page boundaries.
884
0
      P.second.FirstIndex = Index;
885
0
      Index += P.second.Count;
886
0
    }
887
0
    for (auto &P: Got.Local16)
888
0
      P.second = Index++;
889
0
    for (auto &P: Got.Global)
890
0
      P.second = Index++;
891
0
    for (auto &P: Got.Relocs)
892
0
      P.second = Index++;
893
0
    for (auto &P: Got.Tls)
894
0
      P.second = Index++;
895
0
    for (auto &P: Got.DynTlsSymbols) {
896
0
      P.second = Index;
897
0
      Index += 2;
898
0
    }
899
0
  }
900
0
901
0
  // Update Symbol::GotIndex field to use this
902
0
  // value later in the `sortMipsSymbols` function.
903
0
  for (auto &P : PrimGot->Global)
904
0
    P.first->GotIndex = P.second;
905
0
  for (auto &P : PrimGot->Relocs)
906
0
    P.first->GotIndex = P.second;
907
0
908
0
  // Create dynamic relocations.
909
0
  for (FileGot &Got : Gots) {
910
0
    // Create dynamic relocations for TLS entries.
911
0
    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
912
0
      Symbol *S = P.first;
913
0
      uint64_t Offset = P.second * Config->Wordsize;
914
0
      if (S->IsPreemptible)
915
0
        InX::RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
916
0
    }
917
0
    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
918
0
      Symbol *S = P.first;
919
0
      uint64_t Offset = P.second * Config->Wordsize;
920
0
      if (S == nullptr) {
921
0
        if (!Config->Pic)
922
0
          continue;
923
0
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
924
0
      } else {
925
0
        // When building a shared library we still need a dynamic relocation
926
0
        // for the module index. Therefore only checking for
927
0
        // S->IsPreemptible is not sufficient (this happens e.g. for
928
0
        // thread-locals that have been marked as local through a linker script)
929
0
        if (!S->IsPreemptible && !Config->Pic)
930
0
          continue;
931
0
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
932
0
        // However, we can skip writing the TLS offset reloc for non-preemptible
933
0
        // symbols since it is known even in shared libraries
934
0
        if (!S->IsPreemptible)
935
0
          continue;
936
0
        Offset += Config->Wordsize;
937
0
        InX::RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
938
0
      }
939
0
    }
940
0
941
0
    // Do not create dynamic relocations for non-TLS
942
0
    // entries in the primary GOT.
943
0
    if (&Got == PrimGot)
944
0
      continue;
945
0
946
0
    // Dynamic relocations for "global" entries.
947
0
    for (const std::pair<Symbol *, size_t> &P : Got.Global) {
948
0
      uint64_t Offset = P.second * Config->Wordsize;
949
0
      InX::RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
950
0
    }
951
0
    if (!Config->Pic)
952
0
      continue;
953
0
    // Dynamic relocations for "local" entries in case of PIC.
954
0
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
955
0
         Got.PagesMap) {
956
0
      size_t PageCount = L.second.Count;
957
0
      for (size_t PI = 0; PI < PageCount; ++PI) {
958
0
        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
959
0
        InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
960
0
                                int64_t(PI * 0x10000)});
961
0
      }
962
0
    }
963
0
    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
964
0
      uint64_t Offset = P.second * Config->Wordsize;
965
0
      InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
966
0
                              P.first.first, P.first.second});
967
0
    }
968
0
  }
969
0
}
void lld::elf::MipsGotSection::build<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
795
38
template <class ELFT> void MipsGotSection::build() {
796
38
  if (Gots.empty())
797
25
    return;
798
13
799
13
  std::vector<FileGot> MergedGots(1);
800
13
801
13
  // For each GOT move non-preemptible symbols from the `Global`
802
13
  // to `Local16` list. Preemptible symbol might become non-preemptible
803
13
  // one if, for example, it gets a related copy relocation.
804
13
  for (FileGot &Got : Gots) {
805
13
    for (auto &P: Got.Global)
806
5
      if (!P.first->IsPreemptible)
807
0
        Got.Local16.insert({{P.first, 0}, 0});
808
13
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
809
13
      return !P.first->IsPreemptible;
810
13
    });
811
13
  }
812
13
813
13
  // For each GOT remove "reloc-only" entry if there is "global"
814
13
  // entry for the same symbol. And add local entries which indexed
815
13
  // using 32-bit value at the end of 16-bit entries.
816
13
  for (FileGot &Got : Gots) {
817
13
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
818
13
      return Got.Global.count(P.first);
819
13
    });
820
13
    set_union(Got.Local16, Got.Local32);
821
13
    Got.Local32.clear();
822
13
  }
823
13
824
13
  // Evaluate number of "reloc-only" entries in the resulting GOT.
825
13
  // To do that put all unique "reloc-only" and "global" entries
826
13
  // from all GOTs to the future primary GOT.
827
13
  FileGot *PrimGot = &MergedGots.front();
828
13
  for (FileGot &Got : Gots) {
829
13
    set_union(PrimGot->Relocs, Got.Global);
830
13
    set_union(PrimGot->Relocs, Got.Relocs);
831
13
    Got.Relocs.clear();
832
13
  }
833
13
834
13
  // Evaluate number of "page" entries in each GOT.
835
13
  for (FileGot &Got : Gots) {
836
13
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
837
13
         Got.PagesMap) {
838
5
      const OutputSection *OS = P.first;
839
5
      uint64_t SecSize = 0;
840
5
      for (BaseCommand *Cmd : OS->SectionCommands) {
841
5
        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
842
7
          
for (InputSection *IS : ISD->Sections)5
{
843
7
            uint64_t Off = alignTo(SecSize, IS->Alignment);
844
7
            SecSize = Off + IS->getSize();
845
7
          }
846
5
      }
847
5
      P.second.Count = getMipsPageCount(SecSize);
848
5
    }
849
13
  }
850
13
851
13
  // Merge GOTs. Try to join as much as possible GOTs but do not
852
13
  // exceed maximum GOT size. In case of overflow create new GOT
853
13
  // and continue merging.
854
13
  for (FileGot &SrcGot : Gots) {
855
13
    FileGot &DstGot = MergedGots.back();
856
13
    InputFile *File = SrcGot.File;
857
13
    if (!tryMergeGots(DstGot, SrcGot, &DstGot == PrimGot)) {
858
1
      MergedGots.emplace_back();
859
1
      std::swap(MergedGots.back(), SrcGot);
860
1
    }
861
13
    File->MipsGotIndex = MergedGots.size() - 1;
862
13
  }
863
13
  std::swap(Gots, MergedGots);
864
13
865
13
  // Reduce number of "reloc-only" entries in the primary GOT
866
13
  // by substracting "global" entries exist in the primary GOT.
867
13
  PrimGot = &Gots.front();
868
13
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
869
13
    return PrimGot->Global.count(P.first);
870
13
  });
871
13
872
13
  // Calculate indexes for each GOT entry.
873
13
  size_t Index = HeaderEntriesNum;
874
14
  for (FileGot &Got : Gots) {
875
14
    Got.StartIndex = &Got == PrimGot ? 
013
:
Index1
;
876
14
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
877
14
         Got.PagesMap) {
878
5
      // For each output section referenced by GOT page relocations calculate
879
5
      // and save into PagesMap an upper bound of MIPS GOT entries required
880
5
      // to store page addresses of local symbols. We assume the worst case -
881
5
      // each 64kb page of the output section has at least one GOT relocation
882
5
      // against it. And take in account the case when the section intersects
883
5
      // page boundaries.
884
5
      P.second.FirstIndex = Index;
885
5
      Index += P.second.Count;
886
5
    }
887
14
    for (auto &P: Got.Local16)
888
10.0k
      P.second = Index++;
889
14
    for (auto &P: Got.Global)
890
5
      P.second = Index++;
891
14
    for (auto &P: Got.Relocs)
892
2
      P.second = Index++;
893
14
    for (auto &P: Got.Tls)
894
4
      P.second = Index++;
895
14
    for (auto &P: Got.DynTlsSymbols) {
896
7
      P.second = Index;
897
7
      Index += 2;
898
7
    }
899
14
  }
900
13
901
13
  // Update Symbol::GotIndex field to use this
902
13
  // value later in the `sortMipsSymbols` function.
903
13
  for (auto &P : PrimGot->Global)
904
5
    P.first->GotIndex = P.second;
905
13
  for (auto &P : PrimGot->Relocs)
906
2
    P.first->GotIndex = P.second;
907
13
908
13
  // Create dynamic relocations.
909
14
  for (FileGot &Got : Gots) {
910
14
    // Create dynamic relocations for TLS entries.
911
14
    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
912
4
      Symbol *S = P.first;
913
4
      uint64_t Offset = P.second * Config->Wordsize;
914
4
      if (S->IsPreemptible)
915
3
        InX::RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
916
4
    }
917
14
    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
918
7
      Symbol *S = P.first;
919
7
      uint64_t Offset = P.second * Config->Wordsize;
920
7
      if (S == nullptr) {
921
2
        if (!Config->Pic)
922
1
          continue;
923
1
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
924
5
      } else {
925
5
        // When building a shared library we still need a dynamic relocation
926
5
        // for the module index. Therefore only checking for
927
5
        // S->IsPreemptible is not sufficient (this happens e.g. for
928
5
        // thread-locals that have been marked as local through a linker script)
929
5
        if (!S->IsPreemptible && 
!Config->Pic2
)
930
1
          continue;
931
4
        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
932
4
        // However, we can skip writing the TLS offset reloc for non-preemptible
933
4
        // symbols since it is known even in shared libraries
934
4
        if (!S->IsPreemptible)
935
1
          continue;
936
3
        Offset += Config->Wordsize;
937
3
        InX::RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
938
3
      }
939
7
    }
940
14
941
14
    // Do not create dynamic relocations for non-TLS
942
14
    // entries in the primary GOT.
943
14
    if (&Got == PrimGot)
944
13
      continue;
945
1
946
1
    // Dynamic relocations for "global" entries.
947
1
    for (const std::pair<Symbol *, size_t> &P : Got.Global) {
948
0
      uint64_t Offset = P.second * Config->Wordsize;
949
0
      InX::RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
950
0
    }
951
1
    if (!Config->Pic)
952
1
      continue;
953
0
    // Dynamic relocations for "local" entries in case of PIC.
954
0
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
955
0
         Got.PagesMap) {
956
0
      size_t PageCount = L.second.Count;
957
0
      for (size_t PI = 0; PI < PageCount; ++PI) {
958
0
        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
959
0
        InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
960
0
                                int64_t(PI * 0x10000)});
961
0
      }
962
0
    }
963
0
    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
964
0
      uint64_t Offset = P.second * Config->Wordsize;
965
0
      InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
966
0
                              P.first.first, P.first.second});
967
0
    }
968
0
  }
969
13
}
970
971
672
bool MipsGotSection::empty() const {
972
672
  // We add the .got section to the result for dynamic MIPS target because
973
672
  // its address and properties are mentioned in the .dynamic section.
974
672
  return Config->Relocatable;
975
672
}
976
977
10.3k
uint64_t MipsGotSection::getGp(const InputFile *F) const {
978
10.3k
  // For files without related GOT or files refer a primary GOT
979
10.3k
  // returns "common" _gp value. For secondary GOTs calculate
980
10.3k
  // individual _gp values.
981
10.3k
  if (!F || 
!F->MipsGotIndex.hasValue()10.1k
||
*F->MipsGotIndex == 010.1k
)
982
322
    return ElfSym::MipsGp->getVA(0);
983
10.0k
  return getVA() + Gots[*F->MipsGotIndex].StartIndex * Config->Wordsize +
984
10.0k
         0x7ff0;
985
10.0k
}
986
987
159
void MipsGotSection::writeTo(uint8_t *Buf) {
988
159
  // Set the MSB of the second GOT slot. This is not required by any
989
159
  // MIPS ABI documentation, though.
990
159
  //
991
159
  // There is a comment in glibc saying that "The MSB of got[1] of a
992
159
  // gnu object is set to identify gnu objects," and in GNU gold it
993
159
  // says "the second entry will be used by some runtime loaders".
994
159
  // But how this field is being used is unclear.
995
159
  //
996
159
  // We are not really willing to mimic other linkers behaviors
997
159
  // without understanding why they do that, but because all files
998
159
  // generated by GNU tools have this special GOT value, and because
999
159
  // we've been doing this for years, it is probably a safe bet to
1000
159
  // keep doing this for now. We really need to revisit this to see
1001
159
  // if we had to do this.
1002
159
  writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
1003
159
  for (const FileGot &G : Gots) {
1004
10.1k
    auto Write = [&](size_t I, const Symbol *S, int64_t A) {
1005
10.1k
      uint64_t VA = A;
1006
10.1k
      if (S) {
1007
10.0k
        VA = S->getVA(A);
1008
10.0k
        if (S->StOther & STO_MIPS_MICROMIPS)
1009
3
          VA |= 1;
1010
10.0k
      }
1011
10.1k
      writeUint(Buf + I * Config->Wordsize, VA);
1012
10.1k
    };
1013
56
    // Write 'page address' entries to the local part of the GOT.
1014
56
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
1015
56
         G.PagesMap) {
1016
15
      size_t PageCount = L.second.Count;
1017
15
      uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
1018
59
      for (size_t PI = 0; PI < PageCount; 
++PI44
)
1019
44
        Write(L.second.FirstIndex + PI, nullptr, FirstPageAddr + PI * 0x10000);
1020
15
    }
1021
56
    // Local, global, TLS, reloc-only  entries.
1022
56
    // If TLS entry has a corresponding dynamic relocations, leave it
1023
56
    // initialized by zero. Write down adjusted TLS symbol's values otherwise.
1024
56
    // To calculate the adjustments use offsets for thread-local storage.
1025
56
    // https://www.linux-mips.org/wiki/NPTL
1026
56
    for (const std::pair<GotEntry, size_t> &P : G.Local16)
1027
10.0k
      Write(P.second, P.first.first, P.first.second);
1028
56
    // Write VA to the primary GOT only. For secondary GOTs that
1029
56
    // will be done by REL32 dynamic relocations.
1030
56
    if (&G == &Gots.front())
1031
53
      for (const std::pair<const Symbol *, size_t> &P : G.Global)
1032
33
        Write(P.second, P.first, 0);
1033
56
    for (const std::pair<Symbol *, size_t> &P : G.Relocs)
1034
6
      Write(P.second, P.first, 0);
1035
56
    for (const std::pair<Symbol *, size_t> &P : G.Tls)
1036
12
      Write(P.second, P.first, P.first->IsPreemptible ? 
09
:
-0x70003
);
1037
56
    for (const std::pair<Symbol *, size_t> &P : G.DynTlsSymbols) {
1038
18
      if (P.first == nullptr && 
!Config->Pic6
)
1039
3
        Write(P.second, nullptr, 1);
1040
15
      else if (P.first && 
!P.first->IsPreemptible12
) {
1041
4
        // If we are emitting PIC code with relocations we mustn't write
1042
4
        // anything to the GOT here. When using Elf_Rel relocations the value
1043
4
        // one will be treated as an addend and will cause crashes at runtime
1044
4
        if (!Config->Pic)
1045
3
          Write(P.second, nullptr, 1);
1046
4
        Write(P.second + 1, P.first, -0x8000);
1047
4
      }
1048
18
    }
1049
56
  }
1050
159
}
1051
1052
// On PowerPC the .plt section is used to hold the table of function addresses
1053
// instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
1054
// section. I don't know why we have a BSS style type for the section but it is
1055
// consitent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
1056
GotPltSection::GotPltSection()
1057
    : SyntheticSection(SHF_ALLOC | SHF_WRITE,
1058
                       Config->EMachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1059
                       Target->GotPltEntrySize,
1060
2.13k
                       Config->EMachine == EM_PPC64 ? ".plt" : ".got.plt") {}
1061
1062
279
void GotPltSection::addEntry(Symbol &Sym) {
1063
279
  assert(Sym.PltIndex == Entries.size());
1064
279
  Entries.push_back(&Sym);
1065
279
}
1066
1067
648
size_t GotPltSection::getSize() const {
1068
648
  return (Target->GotPltHeaderEntriesNum + Entries.size()) *
1069
648
         Target->GotPltEntrySize;
1070
648
}
1071
1072
206
void GotPltSection::writeTo(uint8_t *Buf) {
1073
206
  Target->writeGotPltHeader(Buf);
1074
206
  Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
1075
257
  for (const Symbol *B : Entries) {
1076
257
    Target->writeGotPlt(Buf, *B);
1077
257
    Buf += Config->Wordsize;
1078
257
  }
1079
206
}
1080
1081
4.38k
bool GotPltSection::empty() const {
1082
4.38k
  // We need to emit a GOT.PLT even if it's empty if there's a symbol that
1083
4.38k
  // references the _GLOBAL_OFFSET_TABLE_ and the Target defines the symbol
1084
4.38k
  // relative to the .got.plt section.
1085
4.38k
  return Entries.empty() &&
1086
4.38k
         
!(3.68k
ElfSym::GlobalOffsetTable3.68k
&&
Target->GotBaseSymInGotPlt103
);
1087
4.38k
}
1088
1089
2.13k
static StringRef getIgotPltName() {
1090
2.13k
  // On ARM the IgotPltSection is part of the GotSection.
1091
2.13k
  if (Config->EMachine == EM_ARM)
1092
120
    return ".got";
1093
2.01k
1094
2.01k
  // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection
1095
2.01k
  // needs to be named the same.
1096
2.01k
  if (Config->EMachine == EM_PPC64)
1097
73
    return ".plt";
1098
1.93k
1099
1.93k
  return ".got.plt";
1100
1.93k
}
1101
1102
// On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1103
// with the IgotPltSection.
1104
IgotPltSection::IgotPltSection()
1105
    : SyntheticSection(SHF_ALLOC | SHF_WRITE,
1106
                       Config->EMachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1107
2.13k
                       Target->GotPltEntrySize, getIgotPltName()) {}
1108
1109
32
void IgotPltSection::addEntry(Symbol &Sym) {
1110
32
  Sym.IsInIgot = true;
1111
32
  assert(Sym.PltIndex == Entries.size());
1112
32
  Entries.push_back(&Sym);
1113
32
}
1114
1115
61
size_t IgotPltSection::getSize() const {
1116
61
  return Entries.size() * Target->GotPltEntrySize;
1117
61
}
1118
1119
17
void IgotPltSection::writeTo(uint8_t *Buf) {
1120
30
  for (const Symbol *B : Entries) {
1121
30
    Target->writeIgotPlt(Buf, *B);
1122
30
    Buf += Config->Wordsize;
1123
30
  }
1124
17
}
1125
1126
StringTableSection::StringTableSection(StringRef Name, bool Dynamic)
1127
    : SyntheticSection(Dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name),
1128
6.39k
      Dynamic(Dynamic) {
1129
6.39k
  // ELF string tables start with a NUL byte.
1130
6.39k
  addString("");
1131
6.39k
}
1132
1133
// Adds a string to the string table. If HashIt is true we hash and check for
1134
// duplicates. It is optional because the name of global symbols are already
1135
// uniqued and hashing them again has a big cost for a small value: uniquing
1136
// them with some other string that happens to be the same.
1137
241k
unsigned StringTableSection::addString(StringRef S, bool HashIt) {
1138
241k
  if (HashIt) {
1139
223k
    auto R = StringMap.insert(std::make_pair(S, this->Size));
1140
223k
    if (!R.second)
1141
489
      return R.first->second;
1142
241k
  }
1143
241k
  unsigned Ret = this->Size;
1144
241k
  this->Size = this->Size + S.size() + 1;
1145
241k
  Strings.push_back(S);
1146
241k
  return Ret;
1147
241k
}
1148
1149
5.02k
void StringTableSection::writeTo(uint8_t *Buf) {
1150
239k
  for (StringRef S : Strings) {
1151
239k
    memcpy(Buf, S.data(), S.size());
1152
239k
    Buf[S.size()] = '\0';
1153
239k
    Buf += S.size() + 1;
1154
239k
  }
1155
5.02k
}
1156
1157
// Returns the number of version definition entries. Because the first entry
1158
// is for the version definition itself, it is the number of versioned symbols
1159
// plus one. Note that we don't support multiple versions yet.
1160
1.29k
static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
1161
1162
template <class ELFT>
1163
DynamicSection<ELFT>::DynamicSection()
1164
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, Config->Wordsize,
1165
2.13k
                       ".dynamic") {
1166
2.13k
  this->Entsize = ELFT::Is64Bits ? 
161.75k
:
8375
;
1167
2.13k
1168
2.13k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1169
2.13k
  // which passes -z rodynamic.
1170
2.13k
  // See "Special Section" in Chapter 4 in the following document:
1171
2.13k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1172
2.13k
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic1.95k
)
1173
178
    this->Flags = SHF_ALLOC;
1174
2.13k
1175
2.13k
  // Add strings to .dynstr early so that .dynstr's size will be
1176
2.13k
  // fixed early.
1177
2.13k
  for (StringRef S : Config->FilterList)
1178
6
    addInt(DT_FILTER, InX::DynStrTab->addString(S));
1179
2.13k
  for (StringRef S : Config->AuxiliaryList)
1180
4
    addInt(DT_AUXILIARY, InX::DynStrTab->addString(S));
1181
2.13k
1182
2.13k
  if (!Config->Rpath.empty())
1183
8
    addInt(Config->EnableNewDtags ? 
DT_RUNPATH7
:
DT_RPATH1
,
1184
8
           InX::DynStrTab->addString(Config->Rpath));
1185
2.13k
1186
2.13k
  for (InputFile *File : SharedFiles) {
1187
318
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1188
318
    if (F->IsNeeded)
1189
303
      addInt(DT_NEEDED, InX::DynStrTab->addString(F->SoName));
1190
318
  }
1191
2.13k
  if (!Config->SoName.empty())
1192
26
    addInt(DT_SONAME, InX::DynStrTab->addString(Config->SoName));
1193
2.13k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::DynamicSection()
Line
Count
Source
1165
256
                       ".dynamic") {
1166
256
  this->Entsize = ELFT::Is64Bits ? 
160
: 8;
1167
256
1168
256
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1169
256
  // which passes -z rodynamic.
1170
256
  // See "Special Section" in Chapter 4 in the following document:
1171
256
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1172
256
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic239
)
1173
17
    this->Flags = SHF_ALLOC;
1174
256
1175
256
  // Add strings to .dynstr early so that .dynstr's size will be
1176
256
  // fixed early.
1177
256
  for (StringRef S : Config->FilterList)
1178
0
    addInt(DT_FILTER, InX::DynStrTab->addString(S));
1179
256
  for (StringRef S : Config->AuxiliaryList)
1180
0
    addInt(DT_AUXILIARY, InX::DynStrTab->addString(S));
1181
256
1182
256
  if (!Config->Rpath.empty())
1183
2
    addInt(Config->EnableNewDtags ? DT_RUNPATH : 
DT_RPATH0
,
1184
2
           InX::DynStrTab->addString(Config->Rpath));
1185
256
1186
256
  for (InputFile *File : SharedFiles) {
1187
51
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1188
51
    if (F->IsNeeded)
1189
45
      addInt(DT_NEEDED, InX::DynStrTab->addString(F->SoName));
1190
51
  }
1191
256
  if (!Config->SoName.empty())
1192
4
    addInt(DT_SONAME, InX::DynStrTab->addString(Config->SoName));
1193
256
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::DynamicSection()
Line
Count
Source
1165
119
                       ".dynamic") {
1166
119
  this->Entsize = ELFT::Is64Bits ? 
160
: 8;
1167
119
1168
119
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1169
119
  // which passes -z rodynamic.
1170
119
  // See "Special Section" in Chapter 4 in the following document:
1171
119
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1172
119
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic3
)
1173
116
    this->Flags = SHF_ALLOC;
1174
119
1175
119
  // Add strings to .dynstr early so that .dynstr's size will be
1176
119
  // fixed early.
1177
119
  for (StringRef S : Config->FilterList)
1178
0
    addInt(DT_FILTER, InX::DynStrTab->addString(S));
1179
119
  for (StringRef S : Config->AuxiliaryList)
1180
0
    addInt(DT_AUXILIARY, InX::DynStrTab->addString(S));
1181
119
1182
119
  if (!Config->Rpath.empty())
1183
0
    addInt(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
1184
0
           InX::DynStrTab->addString(Config->Rpath));
1185
119
1186
119
  for (InputFile *File : SharedFiles) {
1187
24
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1188
24
    if (F->IsNeeded)
1189
24
      addInt(DT_NEEDED, InX::DynStrTab->addString(F->SoName));
1190
24
  }
1191
119
  if (!Config->SoName.empty())
1192
0
    addInt(DT_SONAME, InX::DynStrTab->addString(Config->SoName));
1193
119
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::DynamicSection()
Line
Count
Source
1165
1.68k
                       ".dynamic") {
1166
1.68k
  this->Entsize = ELFT::Is64Bits ? 16 : 
80
;
1167
1.68k
1168
1.68k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1169
1.68k
  // which passes -z rodynamic.
1170
1.68k
  // See "Special Section" in Chapter 4 in the following document:
1171
1.68k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1172
1.68k
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic1.67k
)
1173
6
    this->Flags = SHF_ALLOC;
1174
1.68k
1175
1.68k
  // Add strings to .dynstr early so that .dynstr's size will be
1176
1.68k
  // fixed early.
1177
1.68k
  for (StringRef S : Config->FilterList)
1178
6
    addInt(DT_FILTER, InX::DynStrTab->addString(S));
1179
1.68k
  for (StringRef S : Config->AuxiliaryList)
1180
4
    addInt(DT_AUXILIARY, InX::DynStrTab->addString(S));
1181
1.68k
1182
1.68k
  if (!Config->Rpath.empty())
1183
5
    addInt(Config->EnableNewDtags ? 
DT_RUNPATH4
:
DT_RPATH1
,
1184
5
           InX::DynStrTab->addString(Config->Rpath));
1185
1.68k
1186
1.68k
  for (InputFile *File : SharedFiles) {
1187
227
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1188
227
    if (F->IsNeeded)
1189
218
      addInt(DT_NEEDED, InX::DynStrTab->addString(F->SoName));
1190
227
  }
1191
1.68k
  if (!Config->SoName.empty())
1192
22
    addInt(DT_SONAME, InX::DynStrTab->addString(Config->SoName));
1193
1.68k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::DynamicSection()
Line
Count
Source
1165
76
                       ".dynamic") {
1166
76
  this->Entsize = ELFT::Is64Bits ? 16 : 
80
;
1167
76
1168
76
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1169
76
  // which passes -z rodynamic.
1170
76
  // See "Special Section" in Chapter 4 in the following document:
1171
76
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1172
76
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic37
)
1173
39
    this->Flags = SHF_ALLOC;
1174
76
1175
76
  // Add strings to .dynstr early so that .dynstr's size will be
1176
76
  // fixed early.
1177
76
  for (StringRef S : Config->FilterList)
1178
0
    addInt(DT_FILTER, InX::DynStrTab->addString(S));
1179
76
  for (StringRef S : Config->AuxiliaryList)
1180
0
    addInt(DT_AUXILIARY, InX::DynStrTab->addString(S));
1181
76
1182
76
  if (!Config->Rpath.empty())
1183
1
    addInt(Config->EnableNewDtags ? DT_RUNPATH : 
DT_RPATH0
,
1184
1
           InX::DynStrTab->addString(Config->Rpath));
1185
76
1186
76
  for (InputFile *File : SharedFiles) {
1187
16
    SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
1188
16
    if (F->IsNeeded)
1189
16
      addInt(DT_NEEDED, InX::DynStrTab->addString(F->SoName));
1190
16
  }
1191
76
  if (!Config->SoName.empty())
1192
0
    addInt(DT_SONAME, InX::DynStrTab->addString(Config->SoName));
1193
76
}
1194
1195
template <class ELFT>
1196
100
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1197
100
  Entries.push_back({Tag, Fn});
1198
100
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1196
8
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1197
8
  Entries.push_back({Tag, Fn});
1198
8
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1196
69
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1197
69
  Entries.push_back({Tag, Fn});
1198
69
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::add(int, std::__1::function<unsigned long long ()>)
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1196
23
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1197
23
  Entries.push_back({Tag, Fn});
1198
23
}
1199
1200
template <class ELFT>
1201
4.78k
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1202
4.78k
  Entries.push_back({Tag, [=] 
{ return Val; }4.72k
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1202
631
  Entries.push_back({Tag, [=] { return Val; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1202
624
  Entries.push_back({Tag, [=] { return Val; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1202
3.14k
  Entries.push_back({Tag, [=] { return Val; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1202
325
  Entries.push_back({Tag, [=] { return Val; }});
1203
4.78k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInt(int, unsigned long long)
Line
Count
Source
1201
632
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1202
632
  Entries.push_back({Tag, [=] { return Val; }});
1203
632
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInt(int, unsigned long long)
Line
Count
Source
1201
624
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1202
624
  Entries.push_back({Tag, [=] { return Val; }});
1203
624
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInt(int, unsigned long long)
Line
Count
Source
1201
3.20k
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1202
3.20k
  Entries.push_back({Tag, [=] { return Val; }});
1203
3.20k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInt(int, unsigned long long)
Line
Count
Source
1201
325
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1202
325
  Entries.push_back({Tag, [=] { return Val; }});
1203
325
}
1204
1205
template <class ELFT>
1206
4.77k
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1207
4.77k
  Entries.push_back({Tag, [=] 
{ return Sec->getVA(0); }4.73k
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1207
624
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1207
334
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1207
3.54k
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1207
237
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1208
4.77k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1206
624
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1207
624
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1208
624
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1206
334
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1207
334
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1208
334
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1206
3.58k
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1207
3.58k
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1208
3.58k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1206
237
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1207
237
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1208
237
}
1209
1210
template <class ELFT>
1211
30
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1212
30
  size_t TagOffset = Entries.size() * Entsize;
1213
30
  Entries.push_back(
1214
30
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSecRelative(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1214
2
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSecRelative(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1214
21
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSecRelative(int, lld::elf::InputSection*)::'lambda'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInSecRelative(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1214
7
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1215
30
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1211
2
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1212
2
  size_t TagOffset = Entries.size() * Entsize;
1213
2
  Entries.push_back(
1214
2
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1215
2
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1211
21
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1212
21
  size_t TagOffset = Entries.size() * Entsize;
1213
21
  Entries.push_back(
1214
21
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1215
21
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSecRelative(int, lld::elf::InputSection*)
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1211
7
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1212
7
  size_t TagOffset = Entries.size() * Entsize;
1213
7
  Entries.push_back(
1214
7
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1215
7
}
1216
1217
template <class ELFT>
1218
8
void DynamicSection<ELFT>::addOutSec(int32_t Tag, OutputSection *Sec) {
1219
8
  Entries.push_back({Tag, [=] { return Sec->Addr; }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1219
8
  Entries.push_back({Tag, [=] { return Sec->Addr; }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
1220
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addOutSec(int, lld::elf::OutputSection*)
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addOutSec(int, lld::elf::OutputSection*)
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addOutSec(int, lld::elf::OutputSection*)
Line
Count
Source
1218
8
void DynamicSection<ELFT>::addOutSec(int32_t Tag, OutputSection *Sec) {
1219
8
  Entries.push_back({Tag, [=] { return Sec->Addr; }});
1220
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addOutSec(int, lld::elf::OutputSection*)
1221
1222
template <class ELFT>
1223
420
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1224
420
  Entries.push_back({Tag, [=] 
{ return Sec->Size; }416
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSize(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1224
80
  Entries.push_back({Tag, [=] { return Sec->Size; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSize(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1224
19
  Entries.push_back({Tag, [=] { return Sec->Size; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSize(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1224
290
  Entries.push_back({Tag, [=] { return Sec->Size; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSize(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1224
27
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1225
420
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1223
80
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1224
80
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1225
80
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1223
19
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1224
19
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1225
19
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1223
294
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1224
294
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1225
294
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1223
27
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1224
27
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1225
27
}
1226
1227
template <class ELFT>
1228
8
void DynamicSection<ELFT>::addSym(int32_t Tag, Symbol *Sym) {
1229
8
  Entries.push_back({Tag, [=] { return Sym->getVA(); }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSym(int, lld::elf::Symbol*)::'lambda'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSym(int, lld::elf::Symbol*)::'lambda'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSym(int, lld::elf::Symbol*)::'lambda'()::operator()() const
Line
Count
Source
1229
8
  Entries.push_back({Tag, [=] { return Sym->getVA(); }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSym(int, lld::elf::Symbol*)::'lambda'()::operator()() const
1230
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSym(int, lld::elf::Symbol*)
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSym(int, lld::elf::Symbol*)
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSym(int, lld::elf::Symbol*)
Line
Count
Source
1228
8
void DynamicSection<ELFT>::addSym(int32_t Tag, Symbol *Sym) {
1229
8
  Entries.push_back({Tag, [=] { return Sym->getVA(); }});
1230
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSym(int, lld::elf::Symbol*)
1231
1232
// Add remaining entries to complete .dynamic contents.
1233
1.04k
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1234
1.04k
  if (this->Size)
1235
0
    return; // Already finalized.
1236
1.04k
1237
1.04k
  // Set DT_FLAGS and DT_FLAGS_1.
1238
1.04k
  uint32_t DtFlags = 0;
1239
1.04k
  uint32_t DtFlags1 = 0;
1240
1.04k
  if (Config->Bsymbolic)
1241
6
    DtFlags |= DF_SYMBOLIC;
1242
1.04k
  if (Config->ZNodelete)
1243
1
    DtFlags1 |= DF_1_NODELETE;
1244
1.04k
  if (Config->ZNodlopen)
1245
1
    DtFlags1 |= DF_1_NOOPEN;
1246
1.04k
  if (Config->ZNow) {
1247
4
    DtFlags |= DF_BIND_NOW;
1248
4
    DtFlags1 |= DF_1_NOW;
1249
4
  }
1250
1.04k
  if (Config->ZOrigin) {
1251
1
    DtFlags |= DF_ORIGIN;
1252
1
    DtFlags1 |= DF_1_ORIGIN;
1253
1
  }
1254
1.04k
  if (!Config->ZText)
1255
9
    DtFlags |= DF_TEXTREL;
1256
1.04k
1257
1.04k
  if (DtFlags)
1258
18
    addInt(DT_FLAGS, DtFlags);
1259
1.04k
  if (DtFlags1)
1260
4
    addInt(DT_FLAGS_1, DtFlags1);
1261
1.04k
1262
1.04k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1263
1.04k
  // need it for each process, so we don't write it for DSOs. The loader writes
1264
1.04k
  // the pointer into this entry.
1265
1.04k
  //
1266
1.04k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1267
1.04k
  // systems (currently only Fuchsia OS) provide other means to give the
1268
1.04k
  // debugger this information. Such systems may choose make .dynamic read-only.
1269
1.04k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1270
1.04k
  if (!Config->Shared && 
!Config->Relocatable281
&&
!Config->ZRodynamic279
)
1271
278
    addInt(DT_DEBUG, 0);
1272
1.04k
1273
1.04k
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1274
1.04k
  if (!InX::RelaDyn->empty()) {
1275
206
    addInSec(InX::RelaDyn->DynamicTag, InX::RelaDyn);
1276
206
    addSize(InX::RelaDyn->SizeDynamicTag, InX::RelaDyn->getParent());
1277
206
1278
206
    bool IsRela = Config->IsRela;
1279
206
    addInt(IsRela ? 
DT_RELAENT147
:
DT_RELENT59
,
1280
206
           IsRela ? 
sizeof(Elf_Rela)147
:
sizeof(Elf_Rel)59
);
1281
206
1282
206
    // MIPS dynamic loader does not support RELCOUNT tag.
1283
206
    // The problem is in the tight relation between dynamic
1284
206
    // relocations and GOT. So do not emit this tag on MIPS.
1285
206
    if (Config->EMachine != EM_MIPS) {
1286
191
      size_t NumRelativeRels = InX::RelaDyn->getRelativeRelocCount();
1287
191
      if (Config->ZCombreloc && 
NumRelativeRels190
)
1288
40
        addInt(IsRela ? 
DT_RELACOUNT28
:
DT_RELCOUNT12
, NumRelativeRels);
1289
191
    }
1290
206
  }
1291
1.04k
  // .rel[a].plt section usually consists of two parts, containing plt and
1292
1.04k
  // iplt relocations. It is possible to have only iplt relocations in the
1293
1.04k
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1294
1.04k
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1295
1.04k
  // case, so here we always use RelaPlt as marker for the begining of
1296
1.04k
  // .rel[a].plt section.
1297
1.04k
  if (InX::RelaPlt->getParent()->Live) {
1298
206
    addInSec(DT_JMPREL, InX::RelaPlt);
1299
206
    addSize(DT_PLTRELSZ, InX::RelaPlt->getParent());
1300
206
    switch (Config->EMachine) {
1301
206
    case EM_MIPS:
1302
15
      addInSec(DT_MIPS_PLTGOT, InX::GotPlt);
1303
15
      break;
1304
206
    case EM_SPARCV9:
1305
0
      addInSec(DT_PLTGOT, InX::Plt);
1306
0
      break;
1307
206
    default:
1308
191
      addInSec(DT_PLTGOT, InX::GotPlt);
1309
191
      break;
1310
206
    }
1311
206
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA159
:
DT_REL47
);
1312
206
  }
1313
1.04k
1314
1.04k
  addInSec(DT_SYMTAB, InX::DynSymTab);
1315
1.04k
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1316
1.04k
  addInSec(DT_STRTAB, InX::DynStrTab);
1317
1.04k
  addInt(DT_STRSZ, InX::DynStrTab->getSize());
1318
1.04k
  if (!Config->ZText)
1319
9
    addInt(DT_TEXTREL, 0);
1320
1.04k
  if (InX::GnuHashTab)
1321
808
    addInSec(DT_GNU_HASH, InX::GnuHashTab);
1322
1.04k
  if (InX::HashTab)
1323
1.03k
    addInSec(DT_HASH, InX::HashTab);
1324
1.04k
1325
1.04k
  if (Out::PreinitArray) {
1326
3
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1327
3
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1328
3
  }
1329
1.04k
  if (Out::InitArray) {
1330
3
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1331
3
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1332
3
  }
1333
1.04k
  if (Out::FiniArray) {
1334
2
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1335
2
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1336
2
  }
1337
1.04k
1338
1.04k
  if (Symbol *B = Symtab->find(Config->Init))
1339
6
    if (B->isDefined())
1340
4
      addSym(DT_INIT, B);
1341
1.04k
  if (Symbol *B = Symtab->find(Config->Fini))
1342
6
    if (B->isDefined())
1343
4
      addSym(DT_FINI, B);
1344
1.04k
1345
1.04k
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1346
1.04k
  if (HasVerNeed || 
In<ELFT>::VerDef1.03k
)
1347
56
    addInSec(DT_VERSYM, In<ELFT>::VerSym);
1348
1.04k
  if (In<ELFT>::VerDef) {
1349
49
    addInSec(DT_VERDEF, In<ELFT>::VerDef);
1350
49
    addInt(DT_VERDEFNUM, getVerDefNum());
1351
49
  }
1352
1.04k
  if (HasVerNeed) {
1353
8
    addInSec(DT_VERNEED, In<ELFT>::VerNeed);
1354
8
    addInt(DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum());
1355
8
  }
1356
1.04k
1357
1.04k
  if (Config->EMachine == EM_MIPS) {
1358
100
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
100
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
100
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
100
    addInt(DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols());
1362
100
1363
100
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()::'lambda'()::operator()() const
Line
Count
Source
1363
8
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()::'lambda'()::operator()() const
Line
Count
Source
1363
69
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()::'lambda'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()::'lambda'()::operator()() const
Line
Count
Source
1363
23
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
1364
100
1365
100
    if (const Symbol *B = InX::MipsGot->getFirstGlobalEntry())
1366
33
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
67
    else
1368
67
      addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols());
1369
100
    addInSec(DT_PLTGOT, InX::MipsGot);
1370
100
    if (InX::MipsRldMap) {
1371
30
      if (!Config->Pie)
1372
27
        addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
1373
30
      // Store the offset to the .rld_map section
1374
30
      // relative to the address of the tag.
1375
30
      addInSecRelative(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap);
1376
30
    }
1377
100
  }
1378
1.04k
1379
1.04k
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
1.04k
  if (Config->EMachine == EM_PPC64 && 
!InX::Plt->empty()51
) {
1381
20
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
20
    // stub, which starts directly after the header.
1383
20
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
20
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
20
                         return InX::Plt->getVA(0) + Offset;
1386
20
                       }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()::'lambda0'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()::'lambda0'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()::'lambda0'()::operator()() const
Line
Count
Source
1383
10
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
10
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
10
                         return InX::Plt->getVA(0) + Offset;
1386
10
                       }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()::'lambda0'()::operator()() const
Line
Count
Source
1383
10
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
10
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
10
                         return InX::Plt->getVA(0) + Offset;
1386
10
                       }});
1387
20
  }
1388
1.04k
1389
1.04k
  addInt(DT_NULL, 0);
1390
1.04k
1391
1.04k
  getParent()->Link = this->Link;
1392
1.04k
  this->Size = Entries.size() * this->Entsize;
1393
1.04k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1233
136
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1234
136
  if (this->Size)
1235
0
    return; // Already finalized.
1236
136
1237
136
  // Set DT_FLAGS and DT_FLAGS_1.
1238
136
  uint32_t DtFlags = 0;
1239
136
  uint32_t DtFlags1 = 0;
1240
136
  if (Config->Bsymbolic)
1241
2
    DtFlags |= DF_SYMBOLIC;
1242
136
  if (Config->ZNodelete)
1243
0
    DtFlags1 |= DF_1_NODELETE;
1244
136
  if (Config->ZNodlopen)
1245
0
    DtFlags1 |= DF_1_NOOPEN;
1246
136
  if (Config->ZNow) {
1247
0
    DtFlags |= DF_BIND_NOW;
1248
0
    DtFlags1 |= DF_1_NOW;
1249
0
  }
1250
136
  if (Config->ZOrigin) {
1251
0
    DtFlags |= DF_ORIGIN;
1252
0
    DtFlags1 |= DF_1_ORIGIN;
1253
0
  }
1254
136
  if (!Config->ZText)
1255
0
    DtFlags |= DF_TEXTREL;
1256
136
1257
136
  if (DtFlags)
1258
2
    addInt(DT_FLAGS, DtFlags);
1259
136
  if (DtFlags1)
1260
0
    addInt(DT_FLAGS_1, DtFlags1);
1261
136
1262
136
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1263
136
  // need it for each process, so we don't write it for DSOs. The loader writes
1264
136
  // the pointer into this entry.
1265
136
  //
1266
136
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1267
136
  // systems (currently only Fuchsia OS) provide other means to give the
1268
136
  // debugger this information. Such systems may choose make .dynamic read-only.
1269
136
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1270
136
  if (!Config->Shared && 
!Config->Relocatable39
&&
!Config->ZRodynamic39
)
1271
39
    addInt(DT_DEBUG, 0);
1272
136
1273
136
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1274
136
  if (!InX::RelaDyn->empty()) {
1275
45
    addInSec(InX::RelaDyn->DynamicTag, InX::RelaDyn);
1276
45
    addSize(InX::RelaDyn->SizeDynamicTag, InX::RelaDyn->getParent());
1277
45
1278
45
    bool IsRela = Config->IsRela;
1279
45
    addInt(IsRela ? 
DT_RELAENT0
: DT_RELENT,
1280
45
           IsRela ? 
sizeof(Elf_Rela)0
: sizeof(Elf_Rel));
1281
45
1282
45
    // MIPS dynamic loader does not support RELCOUNT tag.
1283
45
    // The problem is in the tight relation between dynamic
1284
45
    // relocations and GOT. So do not emit this tag on MIPS.
1285
45
    if (Config->EMachine != EM_MIPS) {
1286
44
      size_t NumRelativeRels = InX::RelaDyn->getRelativeRelocCount();
1287
44
      if (Config->ZCombreloc && NumRelativeRels)
1288
12
        addInt(IsRela ? 
DT_RELACOUNT0
: DT_RELCOUNT, NumRelativeRels);
1289
44
    }
1290
45
  }
1291
136
  // .rel[a].plt section usually consists of two parts, containing plt and
1292
136
  // iplt relocations. It is possible to have only iplt relocations in the
1293
136
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1294
136
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1295
136
  // case, so here we always use RelaPlt as marker for the begining of
1296
136
  // .rel[a].plt section.
1297
136
  if (InX::RelaPlt->getParent()->Live) {
1298
35
    addInSec(DT_JMPREL, InX::RelaPlt);
1299
35
    addSize(DT_PLTRELSZ, InX::RelaPlt->getParent());
1300
35
    switch (Config->EMachine) {
1301
35
    case EM_MIPS:
1302
2
      addInSec(DT_MIPS_PLTGOT, InX::GotPlt);
1303
2
      break;
1304
35
    case EM_SPARCV9:
1305
0
      addInSec(DT_PLTGOT, InX::Plt);
1306
0
      break;
1307
35
    default:
1308
33
      addInSec(DT_PLTGOT, InX::GotPlt);
1309
33
      break;
1310
35
    }
1311
35
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA1
:
DT_REL34
);
1312
35
  }
1313
136
1314
136
  addInSec(DT_SYMTAB, InX::DynSymTab);
1315
136
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1316
136
  addInSec(DT_STRTAB, InX::DynStrTab);
1317
136
  addInt(DT_STRSZ, InX::DynStrTab->getSize());
1318
136
  if (!Config->ZText)
1319
0
    addInt(DT_TEXTREL, 0);
1320
136
  if (InX::GnuHashTab)
1321
93
    addInSec(DT_GNU_HASH, InX::GnuHashTab);
1322
136
  if (InX::HashTab)
1323
134
    addInSec(DT_HASH, InX::HashTab);
1324
136
1325
136
  if (Out::PreinitArray) {
1326
0
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1327
0
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1328
0
  }
1329
136
  if (Out::InitArray) {
1330
0
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1331
0
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1332
0
  }
1333
136
  if (Out::FiniArray) {
1334
0
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1335
0
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1336
0
  }
1337
136
1338
136
  if (Symbol *B = Symtab->find(Config->Init))
1339
0
    if (B->isDefined())
1340
0
      addSym(DT_INIT, B);
1341
136
  if (Symbol *B = Symtab->find(Config->Fini))
1342
0
    if (B->isDefined())
1343
0
      addSym(DT_FINI, B);
1344
136
1345
136
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1346
136
  if (HasVerNeed || In<ELFT>::VerDef)
1347
0
    addInSec(DT_VERSYM, In<ELFT>::VerSym);
1348
136
  if (In<ELFT>::VerDef) {
1349
0
    addInSec(DT_VERDEF, In<ELFT>::VerDef);
1350
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1351
0
  }
1352
136
  if (HasVerNeed) {
1353
0
    addInSec(DT_VERNEED, In<ELFT>::VerNeed);
1354
0
    addInt(DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum());
1355
0
  }
1356
136
1357
136
  if (Config->EMachine == EM_MIPS) {
1358
8
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
8
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
8
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
8
    addInt(DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols());
1362
8
1363
8
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
1364
8
1365
8
    if (const Symbol *B = InX::MipsGot->getFirstGlobalEntry())
1366
3
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
5
    else
1368
5
      addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols());
1369
8
    addInSec(DT_PLTGOT, InX::MipsGot);
1370
8
    if (InX::MipsRldMap) {
1371
2
      if (!Config->Pie)
1372
2
        addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
1373
2
      // Store the offset to the .rld_map section
1374
2
      // relative to the address of the tag.
1375
2
      addInSecRelative(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap);
1376
2
    }
1377
8
  }
1378
136
1379
136
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
136
  if (Config->EMachine == EM_PPC64 && 
!InX::Plt->empty()0
) {
1381
0
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
0
    // stub, which starts directly after the header.
1383
0
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
0
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
0
                         return InX::Plt->getVA(0) + Offset;
1386
0
                       }});
1387
0
  }
1388
136
1389
136
  addInt(DT_NULL, 0);
1390
136
1391
136
  getParent()->Link = this->Link;
1392
136
  this->Size = Entries.size() * this->Entsize;
1393
136
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1233
71
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1234
71
  if (this->Size)
1235
0
    return; // Already finalized.
1236
71
1237
71
  // Set DT_FLAGS and DT_FLAGS_1.
1238
71
  uint32_t DtFlags = 0;
1239
71
  uint32_t DtFlags1 = 0;
1240
71
  if (Config->Bsymbolic)
1241
1
    DtFlags |= DF_SYMBOLIC;
1242
71
  if (Config->ZNodelete)
1243
0
    DtFlags1 |= DF_1_NODELETE;
1244
71
  if (Config->ZNodlopen)
1245
0
    DtFlags1 |= DF_1_NOOPEN;
1246
71
  if (Config->ZNow) {
1247
0
    DtFlags |= DF_BIND_NOW;
1248
0
    DtFlags1 |= DF_1_NOW;
1249
0
  }
1250
71
  if (Config->ZOrigin) {
1251
0
    DtFlags |= DF_ORIGIN;
1252
0
    DtFlags1 |= DF_1_ORIGIN;
1253
0
  }
1254
71
  if (!Config->ZText)
1255
0
    DtFlags |= DF_TEXTREL;
1256
71
1257
71
  if (DtFlags)
1258
1
    addInt(DT_FLAGS, DtFlags);
1259
71
  if (DtFlags1)
1260
0
    addInt(DT_FLAGS_1, DtFlags1);
1261
71
1262
71
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1263
71
  // need it for each process, so we don't write it for DSOs. The loader writes
1264
71
  // the pointer into this entry.
1265
71
  //
1266
71
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1267
71
  // systems (currently only Fuchsia OS) provide other means to give the
1268
71
  // debugger this information. Such systems may choose make .dynamic read-only.
1269
71
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1270
71
  if (!Config->Shared && 
!Config->Relocatable21
&&
!Config->ZRodynamic21
)
1271
21
    addInt(DT_DEBUG, 0);
1272
71
1273
71
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1274
71
  if (!InX::RelaDyn->empty()) {
1275
8
    addInSec(InX::RelaDyn->DynamicTag, InX::RelaDyn);
1276
8
    addSize(InX::RelaDyn->SizeDynamicTag, InX::RelaDyn->getParent());
1277
8
1278
8
    bool IsRela = Config->IsRela;
1279
8
    addInt(IsRela ? 
DT_RELAENT1
:
DT_RELENT7
,
1280
8
           IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)7
);
1281
8
1282
8
    // MIPS dynamic loader does not support RELCOUNT tag.
1283
8
    // The problem is in the tight relation between dynamic
1284
8
    // relocations and GOT. So do not emit this tag on MIPS.
1285
8
    if (Config->EMachine != EM_MIPS) {
1286
1
      size_t NumRelativeRels = InX::RelaDyn->getRelativeRelocCount();
1287
1
      if (Config->ZCombreloc && NumRelativeRels)
1288
0
        addInt(IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels);
1289
1
    }
1290
8
  }
1291
71
  // .rel[a].plt section usually consists of two parts, containing plt and
1292
71
  // iplt relocations. It is possible to have only iplt relocations in the
1293
71
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1294
71
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1295
71
  // case, so here we always use RelaPlt as marker for the begining of
1296
71
  // .rel[a].plt section.
1297
71
  if (InX::RelaPlt->getParent()->Live) {
1298
11
    addInSec(DT_JMPREL, InX::RelaPlt);
1299
11
    addSize(DT_PLTRELSZ, InX::RelaPlt->getParent());
1300
11
    switch (Config->EMachine) {
1301
11
    case EM_MIPS:
1302
11
      addInSec(DT_MIPS_PLTGOT, InX::GotPlt);
1303
11
      break;
1304
11
    case EM_SPARCV9:
1305
0
      addInSec(DT_PLTGOT, InX::Plt);
1306
0
      break;
1307
11
    default:
1308
0
      addInSec(DT_PLTGOT, InX::GotPlt);
1309
0
      break;
1310
11
    }
1311
11
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA0
: DT_REL);
1312
11
  }
1313
71
1314
71
  addInSec(DT_SYMTAB, InX::DynSymTab);
1315
71
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1316
71
  addInSec(DT_STRTAB, InX::DynStrTab);
1317
71
  addInt(DT_STRSZ, InX::DynStrTab->getSize());
1318
71
  if (!Config->ZText)
1319
0
    addInt(DT_TEXTREL, 0);
1320
71
  if (InX::GnuHashTab)
1321
1
    addInSec(DT_GNU_HASH, InX::GnuHashTab);
1322
71
  if (InX::HashTab)
1323
71
    addInSec(DT_HASH, InX::HashTab);
1324
71
1325
71
  if (Out::PreinitArray) {
1326
0
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1327
0
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1328
0
  }
1329
71
  if (Out::InitArray) {
1330
0
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1331
0
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1332
0
  }
1333
71
  if (Out::FiniArray) {
1334
0
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1335
0
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1336
0
  }
1337
71
1338
71
  if (Symbol *B = Symtab->find(Config->Init))
1339
0
    if (B->isDefined())
1340
0
      addSym(DT_INIT, B);
1341
71
  if (Symbol *B = Symtab->find(Config->Fini))
1342
0
    if (B->isDefined())
1343
0
      addSym(DT_FINI, B);
1344
71
1345
71
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1346
71
  if (HasVerNeed || 
In<ELFT>::VerDef70
)
1347
1
    addInSec(DT_VERSYM, In<ELFT>::VerSym);
1348
71
  if (In<ELFT>::VerDef) {
1349
0
    addInSec(DT_VERDEF, In<ELFT>::VerDef);
1350
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1351
0
  }
1352
71
  if (HasVerNeed) {
1353
1
    addInSec(DT_VERNEED, In<ELFT>::VerNeed);
1354
1
    addInt(DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum());
1355
1
  }
1356
71
1357
71
  if (Config->EMachine == EM_MIPS) {
1358
69
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
69
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
69
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
69
    addInt(DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols());
1362
69
1363
69
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
1364
69
1365
69
    if (const Symbol *B = InX::MipsGot->getFirstGlobalEntry())
1366
23
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
46
    else
1368
46
      addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols());
1369
69
    addInSec(DT_PLTGOT, InX::MipsGot);
1370
69
    if (InX::MipsRldMap) {
1371
21
      if (!Config->Pie)
1372
19
        addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
1373
21
      // Store the offset to the .rld_map section
1374
21
      // relative to the address of the tag.
1375
21
      addInSecRelative(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap);
1376
21
    }
1377
69
  }
1378
71
1379
71
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
71
  if (Config->EMachine == EM_PPC64 && 
!InX::Plt->empty()0
) {
1381
0
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
0
    // stub, which starts directly after the header.
1383
0
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
0
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
0
                         return InX::Plt->getVA(0) + Offset;
1386
0
                       }});
1387
0
  }
1388
71
1389
71
  addInt(DT_NULL, 0);
1390
71
1391
71
  getParent()->Link = this->Link;
1392
71
  this->Size = Entries.size() * this->Entsize;
1393
71
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1233
785
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1234
785
  if (this->Size)
1235
0
    return; // Already finalized.
1236
785
1237
785
  // Set DT_FLAGS and DT_FLAGS_1.
1238
785
  uint32_t DtFlags = 0;
1239
785
  uint32_t DtFlags1 = 0;
1240
785
  if (Config->Bsymbolic)
1241
3
    DtFlags |= DF_SYMBOLIC;
1242
785
  if (Config->ZNodelete)
1243
1
    DtFlags1 |= DF_1_NODELETE;
1244
785
  if (Config->ZNodlopen)
1245
1
    DtFlags1 |= DF_1_NOOPEN;
1246
785
  if (Config->ZNow) {
1247
4
    DtFlags |= DF_BIND_NOW;
1248
4
    DtFlags1 |= DF_1_NOW;
1249
4
  }
1250
785
  if (Config->ZOrigin) {
1251
1
    DtFlags |= DF_ORIGIN;
1252
1
    DtFlags1 |= DF_1_ORIGIN;
1253
1
  }
1254
785
  if (!Config->ZText)
1255
6
    DtFlags |= DF_TEXTREL;
1256
785
1257
785
  if (DtFlags)
1258
12
    addInt(DT_FLAGS, DtFlags);
1259
785
  if (DtFlags1)
1260
4
    addInt(DT_FLAGS_1, DtFlags1);
1261
785
1262
785
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1263
785
  // need it for each process, so we don't write it for DSOs. The loader writes
1264
785
  // the pointer into this entry.
1265
785
  //
1266
785
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1267
785
  // systems (currently only Fuchsia OS) provide other means to give the
1268
785
  // debugger this information. Such systems may choose make .dynamic read-only.
1269
785
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1270
785
  if (!Config->Shared && 
!Config->Relocatable206
&&
!Config->ZRodynamic204
)
1271
203
    addInt(DT_DEBUG, 0);
1272
785
1273
785
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1274
785
  if (!InX::RelaDyn->empty()) {
1275
138
    addInSec(InX::RelaDyn->DynamicTag, InX::RelaDyn);
1276
138
    addSize(InX::RelaDyn->SizeDynamicTag, InX::RelaDyn->getParent());
1277
138
1278
138
    bool IsRela = Config->IsRela;
1279
138
    addInt(IsRela ? DT_RELAENT : 
DT_RELENT0
,
1280
138
           IsRela ? sizeof(Elf_Rela) : 
sizeof(Elf_Rel)0
);
1281
138
1282
138
    // MIPS dynamic loader does not support RELCOUNT tag.
1283
138
    // The problem is in the tight relation between dynamic
1284
138
    // relocations and GOT. So do not emit this tag on MIPS.
1285
138
    if (Config->EMachine != EM_MIPS) {
1286
138
      size_t NumRelativeRels = InX::RelaDyn->getRelativeRelocCount();
1287
138
      if (Config->ZCombreloc && 
NumRelativeRels137
)
1288
27
        addInt(IsRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, NumRelativeRels);
1289
138
    }
1290
138
  }
1291
785
  // .rel[a].plt section usually consists of two parts, containing plt and
1292
785
  // iplt relocations. It is possible to have only iplt relocations in the
1293
785
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1294
785
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1295
785
  // case, so here we always use RelaPlt as marker for the begining of
1296
785
  // .rel[a].plt section.
1297
785
  if (InX::RelaPlt->getParent()->Live) {
1298
148
    addInSec(DT_JMPREL, InX::RelaPlt);
1299
148
    addSize(DT_PLTRELSZ, InX::RelaPlt->getParent());
1300
148
    switch (Config->EMachine) {
1301
148
    case EM_MIPS:
1302
0
      addInSec(DT_MIPS_PLTGOT, InX::GotPlt);
1303
0
      break;
1304
148
    case EM_SPARCV9:
1305
0
      addInSec(DT_PLTGOT, InX::Plt);
1306
0
      break;
1307
148
    default:
1308
148
      addInSec(DT_PLTGOT, InX::GotPlt);
1309
148
      break;
1310
148
    }
1311
148
    addInt(DT_PLTREL, Config->IsRela ? DT_RELA : 
DT_REL0
);
1312
148
  }
1313
785
1314
785
  addInSec(DT_SYMTAB, InX::DynSymTab);
1315
785
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1316
785
  addInSec(DT_STRTAB, InX::DynStrTab);
1317
785
  addInt(DT_STRSZ, InX::DynStrTab->getSize());
1318
785
  if (!Config->ZText)
1319
6
    addInt(DT_TEXTREL, 0);
1320
785
  if (InX::GnuHashTab)
1321
689
    addInSec(DT_GNU_HASH, InX::GnuHashTab);
1322
785
  if (InX::HashTab)
1323
777
    addInSec(DT_HASH, InX::HashTab);
1324
785
1325
785
  if (Out::PreinitArray) {
1326
3
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1327
3
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1328
3
  }
1329
785
  if (Out::InitArray) {
1330
3
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1331
3
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1332
3
  }
1333
785
  if (Out::FiniArray) {
1334
2
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1335
2
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1336
2
  }
1337
785
1338
785
  if (Symbol *B = Symtab->find(Config->Init))
1339
6
    if (B->isDefined())
1340
4
      addSym(DT_INIT, B);
1341
785
  if (Symbol *B = Symtab->find(Config->Fini))
1342
6
    if (B->isDefined())
1343
4
      addSym(DT_FINI, B);
1344
785
1345
785
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1346
785
  if (HasVerNeed || 
In<ELFT>::VerDef778
)
1347
55
    addInSec(DT_VERSYM, In<ELFT>::VerSym);
1348
785
  if (In<ELFT>::VerDef) {
1349
49
    addInSec(DT_VERDEF, In<ELFT>::VerDef);
1350
49
    addInt(DT_VERDEFNUM, getVerDefNum());
1351
49
  }
1352
785
  if (HasVerNeed) {
1353
7
    addInSec(DT_VERNEED, In<ELFT>::VerNeed);
1354
7
    addInt(DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum());
1355
7
  }
1356
785
1357
785
  if (Config->EMachine == EM_MIPS) {
1358
0
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
0
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
0
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
0
    addInt(DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols());
1362
0
1363
0
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
1364
0
1365
0
    if (const Symbol *B = InX::MipsGot->getFirstGlobalEntry())
1366
0
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
0
    else
1368
0
      addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols());
1369
0
    addInSec(DT_PLTGOT, InX::MipsGot);
1370
0
    if (InX::MipsRldMap) {
1371
0
      if (!Config->Pie)
1372
0
        addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
1373
0
      // Store the offset to the .rld_map section
1374
0
      // relative to the address of the tag.
1375
0
      addInSecRelative(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap);
1376
0
    }
1377
0
  }
1378
785
1379
785
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
785
  if (Config->EMachine == EM_PPC64 && 
!InX::Plt->empty()26
) {
1381
10
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
10
    // stub, which starts directly after the header.
1383
10
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
10
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
10
                         return InX::Plt->getVA(0) + Offset;
1386
10
                       }});
1387
10
  }
1388
785
1389
785
  addInt(DT_NULL, 0);
1390
785
1391
785
  getParent()->Link = this->Link;
1392
785
  this->Size = Entries.size() * this->Entsize;
1393
785
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1233
48
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1234
48
  if (this->Size)
1235
0
    return; // Already finalized.
1236
48
1237
48
  // Set DT_FLAGS and DT_FLAGS_1.
1238
48
  uint32_t DtFlags = 0;
1239
48
  uint32_t DtFlags1 = 0;
1240
48
  if (Config->Bsymbolic)
1241
0
    DtFlags |= DF_SYMBOLIC;
1242
48
  if (Config->ZNodelete)
1243
0
    DtFlags1 |= DF_1_NODELETE;
1244
48
  if (Config->ZNodlopen)
1245
0
    DtFlags1 |= DF_1_NOOPEN;
1246
48
  if (Config->ZNow) {
1247
0
    DtFlags |= DF_BIND_NOW;
1248
0
    DtFlags1 |= DF_1_NOW;
1249
0
  }
1250
48
  if (Config->ZOrigin) {
1251
0
    DtFlags |= DF_ORIGIN;
1252
0
    DtFlags1 |= DF_1_ORIGIN;
1253
0
  }
1254
48
  if (!Config->ZText)
1255
3
    DtFlags |= DF_TEXTREL;
1256
48
1257
48
  if (DtFlags)
1258
3
    addInt(DT_FLAGS, DtFlags);
1259
48
  if (DtFlags1)
1260
0
    addInt(DT_FLAGS_1, DtFlags1);
1261
48
1262
48
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1263
48
  // need it for each process, so we don't write it for DSOs. The loader writes
1264
48
  // the pointer into this entry.
1265
48
  //
1266
48
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1267
48
  // systems (currently only Fuchsia OS) provide other means to give the
1268
48
  // debugger this information. Such systems may choose make .dynamic read-only.
1269
48
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1270
48
  if (!Config->Shared && 
!Config->Relocatable15
&&
!Config->ZRodynamic15
)
1271
15
    addInt(DT_DEBUG, 0);
1272
48
1273
48
  this->Link = InX::DynStrTab->getParent()->SectionIndex;
1274
48
  if (!InX::RelaDyn->empty()) {
1275
15
    addInSec(InX::RelaDyn->DynamicTag, InX::RelaDyn);
1276
15
    addSize(InX::RelaDyn->SizeDynamicTag, InX::RelaDyn->getParent());
1277
15
1278
15
    bool IsRela = Config->IsRela;
1279
15
    addInt(IsRela ? 
DT_RELAENT8
:
DT_RELENT7
,
1280
15
           IsRela ? 
sizeof(Elf_Rela)8
:
sizeof(Elf_Rel)7
);
1281
15
1282
15
    // MIPS dynamic loader does not support RELCOUNT tag.
1283
15
    // The problem is in the tight relation between dynamic
1284
15
    // relocations and GOT. So do not emit this tag on MIPS.
1285
15
    if (Config->EMachine != EM_MIPS) {
1286
8
      size_t NumRelativeRels = InX::RelaDyn->getRelativeRelocCount();
1287
8
      if (Config->ZCombreloc && NumRelativeRels)
1288
1
        addInt(IsRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, NumRelativeRels);
1289
8
    }
1290
15
  }
1291
48
  // .rel[a].plt section usually consists of two parts, containing plt and
1292
48
  // iplt relocations. It is possible to have only iplt relocations in the
1293
48
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1294
48
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1295
48
  // case, so here we always use RelaPlt as marker for the begining of
1296
48
  // .rel[a].plt section.
1297
48
  if (InX::RelaPlt->getParent()->Live) {
1298
12
    addInSec(DT_JMPREL, InX::RelaPlt);
1299
12
    addSize(DT_PLTRELSZ, InX::RelaPlt->getParent());
1300
12
    switch (Config->EMachine) {
1301
12
    case EM_MIPS:
1302
2
      addInSec(DT_MIPS_PLTGOT, InX::GotPlt);
1303
2
      break;
1304
12
    case EM_SPARCV9:
1305
0
      addInSec(DT_PLTGOT, InX::Plt);
1306
0
      break;
1307
12
    default:
1308
10
      addInSec(DT_PLTGOT, InX::GotPlt);
1309
10
      break;
1310
12
    }
1311
12
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA10
:
DT_REL2
);
1312
12
  }
1313
48
1314
48
  addInSec(DT_SYMTAB, InX::DynSymTab);
1315
48
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1316
48
  addInSec(DT_STRTAB, InX::DynStrTab);
1317
48
  addInt(DT_STRSZ, InX::DynStrTab->getSize());
1318
48
  if (!Config->ZText)
1319
3
    addInt(DT_TEXTREL, 0);
1320
48
  if (InX::GnuHashTab)
1321
25
    addInSec(DT_GNU_HASH, InX::GnuHashTab);
1322
48
  if (InX::HashTab)
1323
48
    addInSec(DT_HASH, InX::HashTab);
1324
48
1325
48
  if (Out::PreinitArray) {
1326
0
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1327
0
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1328
0
  }
1329
48
  if (Out::InitArray) {
1330
0
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1331
0
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1332
0
  }
1333
48
  if (Out::FiniArray) {
1334
0
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1335
0
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1336
0
  }
1337
48
1338
48
  if (Symbol *B = Symtab->find(Config->Init))
1339
0
    if (B->isDefined())
1340
0
      addSym(DT_INIT, B);
1341
48
  if (Symbol *B = Symtab->find(Config->Fini))
1342
0
    if (B->isDefined())
1343
0
      addSym(DT_FINI, B);
1344
48
1345
48
  bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1346
48
  if (HasVerNeed || In<ELFT>::VerDef)
1347
0
    addInSec(DT_VERSYM, In<ELFT>::VerSym);
1348
48
  if (In<ELFT>::VerDef) {
1349
0
    addInSec(DT_VERDEF, In<ELFT>::VerDef);
1350
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1351
0
  }
1352
48
  if (HasVerNeed) {
1353
0
    addInSec(DT_VERNEED, In<ELFT>::VerNeed);
1354
0
    addInt(DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum());
1355
0
  }
1356
48
1357
48
  if (Config->EMachine == EM_MIPS) {
1358
23
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
23
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
23
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
23
    addInt(DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols());
1362
23
1363
23
    add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); });
1364
23
1365
23
    if (const Symbol *B = InX::MipsGot->getFirstGlobalEntry())
1366
7
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
16
    else
1368
16
      addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols());
1369
23
    addInSec(DT_PLTGOT, InX::MipsGot);
1370
23
    if (InX::MipsRldMap) {
1371
7
      if (!Config->Pie)
1372
6
        addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap);
1373
7
      // Store the offset to the .rld_map section
1374
7
      // relative to the address of the tag.
1375
7
      addInSecRelative(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap);
1376
7
    }
1377
23
  }
1378
48
1379
48
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
48
  if (Config->EMachine == EM_PPC64 && 
!InX::Plt->empty()25
) {
1381
10
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
10
    // stub, which starts directly after the header.
1383
10
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
10
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
10
                         return InX::Plt->getVA(0) + Offset;
1386
10
                       }});
1387
10
  }
1388
48
1389
48
  addInt(DT_NULL, 0);
1390
48
1391
48
  getParent()->Link = this->Link;
1392
48
  this->Size = Entries.size() * this->Entsize;
1393
48
}
1394
1395
1.03k
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
1.03k
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
1.03k
1398
10.0k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
10.0k
    P->d_tag = KV.first;
1400
10.0k
    P->d_un.d_val = KV.second();
1401
10.0k
    ++P;
1402
10.0k
  }
1403
1.03k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1395
136
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
136
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
136
1398
1.34k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
1.34k
    P->d_tag = KV.first;
1400
1.34k
    P->d_un.d_val = KV.second();
1401
1.34k
    ++P;
1402
1.34k
  }
1403
136
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1395
71
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
71
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
71
1398
1.06k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
1.06k
    P->d_tag = KV.first;
1400
1.06k
    P->d_un.d_val = KV.second();
1401
1.06k
    ++P;
1402
1.06k
  }
1403
71
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1395
776
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
776
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
776
1398
7.00k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
7.00k
    P->d_tag = KV.first;
1400
7.00k
    P->d_un.d_val = KV.second();
1401
7.00k
    ++P;
1402
7.00k
  }
1403
776
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1395
48
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
48
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
48
1398
629
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
629
    P->d_tag = KV.first;
1400
629
    P->d_un.d_val = KV.second();
1401
629
    ++P;
1402
629
  }
1403
48
}
1404
1405
892
uint64_t DynamicReloc::getOffset() const {
1406
892
  return InputSec->getVA(OffsetInSec);
1407
892
}
1408
1409
564
int64_t DynamicReloc::computeAddend() const {
1410
564
  if (UseSymVA)
1411
144
    return Sym->getVA(Addend);
1412
420
  if (!OutputSec)
1413
420
    return Addend;
1414
0
  // See the comment in the DynamicReloc ctor.
1415
0
  return getMipsPageAddr(OutputSec->Addr) + Addend;
1416
0
}
1417
1418
1.58k
uint32_t DynamicReloc::getSymIndex() const {
1419
1.58k
  if (Sym && 
!UseSymVA1.49k
)
1420
1.07k
    return Sym->DynsymIndex;
1421
504
  return 0;
1422
504
}
1423
1424
RelocationBaseSection::RelocationBaseSection(StringRef Name, uint32_t Type,
1425
                                             int32_t DynamicTag,
1426
                                             int32_t SizeDynamicTag)
1427
    : SyntheticSection(SHF_ALLOC, Type, Config->Wordsize, Name),
1428
6.39k
      DynamicTag(DynamicTag), SizeDynamicTag(SizeDynamicTag) {}
1429
1430
void RelocationBaseSection::addReloc(RelType DynType, InputSectionBase *IS,
1431
132
                                     uint64_t OffsetInSec, Symbol *Sym) {
1432
132
  addReloc({DynType, IS, OffsetInSec, false, Sym, 0});
1433
132
}
1434
1435
void RelocationBaseSection::addReloc(RelType DynType,
1436
                                     InputSectionBase *InputSec,
1437
                                     uint64_t OffsetInSec, Symbol *Sym,
1438
                                     int64_t Addend, RelExpr Expr,
1439
395
                                     RelType Type) {
1440
395
  // Write the addends to the relocated address if required. We skip
1441
395
  // it if the written value would be zero.
1442
395
  if (Config->WriteAddends && 
(138
Expr != R_ADDEND138
||
Addend != 060
))
1443
91
    InputSec->Relocations.push_back({Expr, Type, OffsetInSec, Addend, Sym});
1444
395
  addReloc({DynType, InputSec, OffsetInSec, Expr != R_ADDEND, Sym, Addend});
1445
395
}
1446
1447
854
void RelocationBaseSection::addReloc(const DynamicReloc &Reloc) {
1448
854
  if (Reloc.Type == Target->RelativeRel)
1449
187
    ++NumRelativeRelocs;
1450
854
  Relocs.push_back(Reloc);
1451
854
}
1452
1453
428
void RelocationBaseSection::finalizeContents() {
1454
428
  // If all relocations are R_*_RELATIVE they don't refer to any
1455
428
  // dynamic symbol and we don't need a dynamic symbol table. If that
1456
428
  // is the case, just use 0 as the link.
1457
428
  Link = InX::DynSymTab ? 
InX::DynSymTab->getParent()->SectionIndex419
:
09
;
1458
428
1459
428
  // Set required output section properties.
1460
428
  getParent()->Link = Link;
1461
428
}
1462
1463
template <class ELFT>
1464
static void encodeDynamicReloc(typename ELFT::Rela *P,
1465
892
                               const DynamicReloc &Rel) {
1466
892
  if (Config->IsRela)
1467
564
    P->r_addend = Rel.computeAddend();
1468
892
  P->r_offset = Rel.getOffset();
1469
892
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1470
892
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::object::ELFType<(llvm::support::endianness)1, false>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1465
258
                               const DynamicReloc &Rel) {
1466
258
  if (Config->IsRela)
1467
1
    P->r_addend = Rel.computeAddend();
1468
258
  P->r_offset = Rel.getOffset();
1469
258
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1470
258
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1465
54
                               const DynamicReloc &Rel) {
1466
54
  if (Config->IsRela)
1467
1
    P->r_addend = Rel.computeAddend();
1468
54
  P->r_offset = Rel.getOffset();
1469
54
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1470
54
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::object::ELFType<(llvm::support::endianness)1, true>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1465
530
                               const DynamicReloc &Rel) {
1466
530
  if (Config->IsRela)
1467
530
    P->r_addend = Rel.computeAddend();
1468
530
  P->r_offset = Rel.getOffset();
1469
530
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1470
530
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1465
50
                               const DynamicReloc &Rel) {
1466
50
  if (Config->IsRela)
1467
32
    P->r_addend = Rel.computeAddend();
1468
50
  P->r_offset = Rel.getOffset();
1469
50
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1470
50
}
1471
1472
template <class ELFT>
1473
RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
1474
    : RelocationBaseSection(Name, Config->IsRela ? SHT_RELA : SHT_REL,
1475
                            Config->IsRela ? DT_RELA : DT_REL,
1476
                            Config->IsRela ? DT_RELASZ : DT_RELSZ),
1477
6.39k
      Sort(Sort) {
1478
6.39k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)5.16k
:
sizeof(Elf_Rel)1.23k
;
1479
6.39k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1477
767
      Sort(Sort) {
1478
767
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)9
:
sizeof(Elf_Rel)758
;
1479
767
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1477
357
      Sort(Sort) {
1478
357
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)9
:
sizeof(Elf_Rel)348
;
1479
357
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1477
5.04k
      Sort(Sort) {
1478
5.04k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)5.03k
:
sizeof(Elf_Rel)9
;
1479
5.04k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1477
228
      Sort(Sort) {
1478
228
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)111
:
sizeof(Elf_Rel)117
;
1479
228
}
1480
1481
542
static bool compRelocations(const DynamicReloc &A, const DynamicReloc &B) {
1482
542
  bool AIsRel = A.Type == Target->RelativeRel;
1483
542
  bool BIsRel = B.Type == Target->RelativeRel;
1484
542
  if (AIsRel != BIsRel)
1485
197
    return AIsRel;
1486
345
  return A.getSymIndex() < B.getSymIndex();
1487
345
}
1488
1489
422
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1490
422
  if (Sort)
1491
200
    std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations);
1492
422
1493
788
  for (const DynamicReloc &Rel : Relocs) {
1494
788
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1495
788
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)512
:
sizeof(Elf_Rel)276
;
1496
788
  }
1497
422
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1489
85
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1490
85
  if (Sort)
1491
44
    std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations);
1492
85
1493
206
  for (const DynamicReloc &Rel : Relocs) {
1494
206
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1495
206
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)205
;
1496
206
  }
1497
85
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1489
19
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1490
19
  if (Sort)
1491
8
    std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations);
1492
19
1493
54
  for (const DynamicReloc &Rel : Relocs) {
1494
54
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1495
54
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)53
;
1496
54
  }
1497
19
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1489
290
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1490
290
  if (Sort)
1491
133
    std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations);
1492
290
1493
478
  for (const DynamicReloc &Rel : Relocs) {
1494
478
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1495
478
    Buf += Config->IsRela ? sizeof(Elf_Rela) : 
sizeof(Elf_Rel)0
;
1496
478
  }
1497
290
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1489
28
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1490
28
  if (Sort)
1491
15
    std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations);
1492
28
1493
50
  for (const DynamicReloc &Rel : Relocs) {
1494
50
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1495
50
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)32
:
sizeof(Elf_Rel)18
;
1496
50
  }
1497
28
}
1498
1499
311
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1500
311
  return this->Entsize * Relocs.size();
1501
311
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getRelocOffset()
Line
Count
Source
1499
80
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1500
80
  return this->Entsize * Relocs.size();
1501
80
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getRelocOffset()
Line
Count
Source
1499
13
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1500
13
  return this->Entsize * Relocs.size();
1501
13
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getRelocOffset()
Line
Count
Source
1499
205
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1500
205
  return this->Entsize * Relocs.size();
1501
205
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getRelocOffset()
Line
Count
Source
1499
13
template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1500
13
  return this->Entsize * Relocs.size();
1501
13
}
1502
1503
template <class ELFT>
1504
AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1505
    StringRef Name)
1506
    : RelocationBaseSection(
1507
          Name, Config->IsRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1508
          Config->IsRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1509
2
          Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1510
2
  this->Entsize = 1;
1511
2
}
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::AndroidPackedRelocationSection(llvm::StringRef)
Line
Count
Source
1509
1
          Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1510
1
  this->Entsize = 1;
1511
1
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::AndroidPackedRelocationSection(llvm::StringRef)
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::AndroidPackedRelocationSection(llvm::StringRef)
Line
Count
Source
1509
1
          Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1510
1
  this->Entsize = 1;
1511
1
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::AndroidPackedRelocationSection(llvm::StringRef)
1512
1513
template <class ELFT>
1514
4
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1515
4
  // This function computes the contents of an Android-format packed relocation
1516
4
  // section.
1517
4
  //
1518
4
  // This format compresses relocations by using relocation groups to factor out
1519
4
  // fields that are common between relocations and storing deltas from previous
1520
4
  // relocations in SLEB128 format (which has a short representation for small
1521
4
  // numbers). A good example of a relocation type with common fields is
1522
4
  // R_*_RELATIVE, which is normally used to represent function pointers in
1523
4
  // vtables. In the REL format, each relative relocation has the same r_info
1524
4
  // field, and is only different from other relative relocations in terms of
1525
4
  // the r_offset field. By sorting relocations by offset, grouping them by
1526
4
  // r_info and representing each relocation with only the delta from the
1527
4
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1528
4
  // byte (or less with run-length encoding). This relocation packer was able to
1529
4
  // reduce the size of the relocation section in an Android Chromium DSO from
1530
4
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1531
4
  //
1532
4
  // A relocation section consists of a header containing the literal bytes
1533
4
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1534
4
  // elements are the total number of relocations in the section and an initial
1535
4
  // r_offset value. The remaining elements define a sequence of relocation
1536
4
  // groups. Each relocation group starts with a header consisting of the
1537
4
  // following elements:
1538
4
  //
1539
4
  // - the number of relocations in the relocation group
1540
4
  // - flags for the relocation group
1541
4
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1542
4
  //   for each relocation in the group.
1543
4
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1544
4
  //   field for each relocation in the group.
1545
4
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1546
4
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1547
4
  //   each relocation in the group.
1548
4
  //
1549
4
  // Following the relocation group header are descriptions of each of the
1550
4
  // relocations in the group. They consist of the following elements:
1551
4
  //
1552
4
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1553
4
  //   delta for this relocation.
1554
4
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1555
4
  //   field for this relocation.
1556
4
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1557
4
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1558
4
  //   this relocation.
1559
4
1560
4
  size_t OldSize = RelocData.size();
1561
4
1562
4
  RelocData = {'A', 'P', 'S', '2'};
1563
4
  raw_svector_ostream OS(RelocData);
1564
188
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()::'lambda'(long long)::operator()(long long) const
Line
Count
Source
1564
68
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::updateAllocSize()::'lambda'(long long)::operator()(long long) const
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::updateAllocSize()::'lambda'(long long)::operator()(long long) const
Line
Count
Source
1564
120
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()::'lambda'(long long)::operator()(long long) const
1565
4
1566
4
  // The format header includes the number of relocations and the initial
1567
4
  // offset (we set this to zero because the first relocation group will
1568
4
  // perform the initial adjustment).
1569
4
  Add(Relocs.size());
1570
4
  Add(0);
1571
4
1572
4
  std::vector<Elf_Rela> Relatives, NonRelatives;
1573
4
1574
104
  for (const DynamicReloc &Rel : Relocs) {
1575
104
    Elf_Rela R;
1576
104
    encodeDynamicReloc<ELFT>(&R, Rel);
1577
104
1578
104
    if (R.getType(Config->IsMips64EL) == Target->RelativeRel)
1579
96
      Relatives.push_back(R);
1580
8
    else
1581
8
      NonRelatives.push_back(R);
1582
104
  }
1583
4
1584
4
  llvm::sort(Relatives.begin(), Relatives.end(),
1585
92
             [](const Elf_Rel &A, const Elf_Rel &B) {
1586
92
               return A.r_offset < B.r_offset;
1587
92
             });
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
Line
Count
Source
1585
46
             [](const Elf_Rel &A, const Elf_Rel &B) {
1586
46
               return A.r_offset < B.r_offset;
1587
46
             });
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
Line
Count
Source
1585
46
             [](const Elf_Rel &A, const Elf_Rel &B) {
1586
46
               return A.r_offset < B.r_offset;
1587
46
             });
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
1588
4
1589
4
  // Try to find groups of relative relocations which are spaced one word
1590
4
  // apart from one another. These generally correspond to vtable entries. The
1591
4
  // format allows these groups to be encoded using a sort of run-length
1592
4
  // encoding, but each group will cost 7 bytes in addition to the offset from
1593
4
  // the previous group, so it is only profitable to do this for groups of
1594
4
  // size 8 or larger.
1595
4
  std::vector<Elf_Rela> UngroupedRelatives;
1596
4
  std::vector<std::vector<Elf_Rela>> RelativeGroups;
1597
16
  for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) {
1598
12
    std::vector<Elf_Rela> Group;
1599
96
    do {
1600
96
      Group.push_back(*I++);
1601
96
    } while (I != E && 
(I - 1)->r_offset + Config->Wordsize == I->r_offset92
);
1602
12
1603
12
    if (Group.size() < 8)
1604
4
      UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(),
1605
4
                                Group.end());
1606
8
    else
1607
8
      RelativeGroups.emplace_back(std::move(Group));
1608
12
  }
1609
4
1610
4
  unsigned HasAddendIfRela =
1611
4
      Config->IsRela ? 
RELOCATION_GROUP_HAS_ADDEND_FLAG2
:
02
;
1612
4
1613
4
  uint64_t Offset = 0;
1614
4
  uint64_t Addend = 0;
1615
4
1616
4
  // Emit the run-length encoding for the groups of adjacent relative
1617
4
  // relocations. Each group is represented using two groups in the packed
1618
4
  // format. The first is used to set the current offset to the start of the
1619
4
  // group (and also encodes the first relocation), and the second encodes the
1620
4
  // remaining relocations.
1621
8
  for (std::vector<Elf_Rela> &G : RelativeGroups) {
1622
8
    // The first relocation in the group.
1623
8
    Add(1);
1624
8
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1625
8
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1626
8
    Add(G[0].r_offset - Offset);
1627
8
    Add(Target->RelativeRel);
1628
8
    if (Config->IsRela) {
1629
4
      Add(G[0].r_addend - Addend);
1630
4
      Addend = G[0].r_addend;
1631
4
    }
1632
8
1633
8
    // The remaining relocations.
1634
8
    Add(G.size() - 1);
1635
8
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1636
8
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1637
8
    Add(Config->Wordsize);
1638
8
    Add(Target->RelativeRel);
1639
8
    if (Config->IsRela) {
1640
34
      for (auto I = G.begin() + 1, E = G.end(); I != E; 
++I30
) {
1641
30
        Add(I->r_addend - Addend);
1642
30
        Addend = I->r_addend;
1643
30
      }
1644
4
    }
1645
8
1646
8
    Offset = G.back().r_offset;
1647
8
  }
1648
4
1649
4
  // Now the ungrouped relatives.
1650
4
  if (!UngroupedRelatives.empty()) {
1651
4
    Add(UngroupedRelatives.size());
1652
4
    Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1653
4
    Add(Target->RelativeRel);
1654
28
    for (Elf_Rela &R : UngroupedRelatives) {
1655
28
      Add(R.r_offset - Offset);
1656
28
      Offset = R.r_offset;
1657
28
      if (Config->IsRela) {
1658
14
        Add(R.r_addend - Addend);
1659
14
        Addend = R.r_addend;
1660
14
      }
1661
28
    }
1662
4
  }
1663
4
1664
4
  // Finally the non-relative relocations.
1665
4
  llvm::sort(NonRelatives.begin(), NonRelatives.end(),
1666
4
             [](const Elf_Rela &A, const Elf_Rela &B) {
1667
4
               return A.r_offset < B.r_offset;
1668
4
             });
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
Line
Count
Source
1666
2
             [](const Elf_Rela &A, const Elf_Rela &B) {
1667
2
               return A.r_offset < B.r_offset;
1668
2
             });
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
Line
Count
Source
1666
2
             [](const Elf_Rela &A, const Elf_Rela &B) {
1667
2
               return A.r_offset < B.r_offset;
1668
2
             });
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()::'lambda'(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&)::operator()(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&) const
1669
4
  if (!NonRelatives.empty()) {
1670
4
    Add(NonRelatives.size());
1671
4
    Add(HasAddendIfRela);
1672
8
    for (Elf_Rela &R : NonRelatives) {
1673
8
      Add(R.r_offset - Offset);
1674
8
      Offset = R.r_offset;
1675
8
      Add(R.r_info);
1676
8
      if (Config->IsRela) {
1677
4
        Add(R.r_addend - Addend);
1678
4
        Addend = R.r_addend;
1679
4
      }
1680
8
    }
1681
4
  }
1682
4
1683
4
  // Returns whether the section size changed. We need to keep recomputing both
1684
4
  // section layout and the contents of this section until the size converges
1685
4
  // because changing this section's size can affect section layout, which in
1686
4
  // turn can affect the sizes of the LEB-encoded integers stored in this
1687
4
  // section.
1688
4
  return RelocData.size() != OldSize;
1689
4
}
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()
Line
Count
Source
1514
2
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1515
2
  // This function computes the contents of an Android-format packed relocation
1516
2
  // section.
1517
2
  //
1518
2
  // This format compresses relocations by using relocation groups to factor out
1519
2
  // fields that are common between relocations and storing deltas from previous
1520
2
  // relocations in SLEB128 format (which has a short representation for small
1521
2
  // numbers). A good example of a relocation type with common fields is
1522
2
  // R_*_RELATIVE, which is normally used to represent function pointers in
1523
2
  // vtables. In the REL format, each relative relocation has the same r_info
1524
2
  // field, and is only different from other relative relocations in terms of
1525
2
  // the r_offset field. By sorting relocations by offset, grouping them by
1526
2
  // r_info and representing each relocation with only the delta from the
1527
2
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1528
2
  // byte (or less with run-length encoding). This relocation packer was able to
1529
2
  // reduce the size of the relocation section in an Android Chromium DSO from
1530
2
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1531
2
  //
1532
2
  // A relocation section consists of a header containing the literal bytes
1533
2
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1534
2
  // elements are the total number of relocations in the section and an initial
1535
2
  // r_offset value. The remaining elements define a sequence of relocation
1536
2
  // groups. Each relocation group starts with a header consisting of the
1537
2
  // following elements:
1538
2
  //
1539
2
  // - the number of relocations in the relocation group
1540
2
  // - flags for the relocation group
1541
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1542
2
  //   for each relocation in the group.
1543
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1544
2
  //   field for each relocation in the group.
1545
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1546
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1547
2
  //   each relocation in the group.
1548
2
  //
1549
2
  // Following the relocation group header are descriptions of each of the
1550
2
  // relocations in the group. They consist of the following elements:
1551
2
  //
1552
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1553
2
  //   delta for this relocation.
1554
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1555
2
  //   field for this relocation.
1556
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1557
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1558
2
  //   this relocation.
1559
2
1560
2
  size_t OldSize = RelocData.size();
1561
2
1562
2
  RelocData = {'A', 'P', 'S', '2'};
1563
2
  raw_svector_ostream OS(RelocData);
1564
2
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
1565
2
1566
2
  // The format header includes the number of relocations and the initial
1567
2
  // offset (we set this to zero because the first relocation group will
1568
2
  // perform the initial adjustment).
1569
2
  Add(Relocs.size());
1570
2
  Add(0);
1571
2
1572
2
  std::vector<Elf_Rela> Relatives, NonRelatives;
1573
2
1574
52
  for (const DynamicReloc &Rel : Relocs) {
1575
52
    Elf_Rela R;
1576
52
    encodeDynamicReloc<ELFT>(&R, Rel);
1577
52
1578
52
    if (R.getType(Config->IsMips64EL) == Target->RelativeRel)
1579
48
      Relatives.push_back(R);
1580
4
    else
1581
4
      NonRelatives.push_back(R);
1582
52
  }
1583
2
1584
2
  llvm::sort(Relatives.begin(), Relatives.end(),
1585
2
             [](const Elf_Rel &A, const Elf_Rel &B) {
1586
2
               return A.r_offset < B.r_offset;
1587
2
             });
1588
2
1589
2
  // Try to find groups of relative relocations which are spaced one word
1590
2
  // apart from one another. These generally correspond to vtable entries. The
1591
2
  // format allows these groups to be encoded using a sort of run-length
1592
2
  // encoding, but each group will cost 7 bytes in addition to the offset from
1593
2
  // the previous group, so it is only profitable to do this for groups of
1594
2
  // size 8 or larger.
1595
2
  std::vector<Elf_Rela> UngroupedRelatives;
1596
2
  std::vector<std::vector<Elf_Rela>> RelativeGroups;
1597
8
  for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) {
1598
6
    std::vector<Elf_Rela> Group;
1599
48
    do {
1600
48
      Group.push_back(*I++);
1601
48
    } while (I != E && 
(I - 1)->r_offset + Config->Wordsize == I->r_offset46
);
1602
6
1603
6
    if (Group.size() < 8)
1604
2
      UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(),
1605
2
                                Group.end());
1606
4
    else
1607
4
      RelativeGroups.emplace_back(std::move(Group));
1608
6
  }
1609
2
1610
2
  unsigned HasAddendIfRela =
1611
2
      Config->IsRela ? 
RELOCATION_GROUP_HAS_ADDEND_FLAG0
: 0;
1612
2
1613
2
  uint64_t Offset = 0;
1614
2
  uint64_t Addend = 0;
1615
2
1616
2
  // Emit the run-length encoding for the groups of adjacent relative
1617
2
  // relocations. Each group is represented using two groups in the packed
1618
2
  // format. The first is used to set the current offset to the start of the
1619
2
  // group (and also encodes the first relocation), and the second encodes the
1620
2
  // remaining relocations.
1621
4
  for (std::vector<Elf_Rela> &G : RelativeGroups) {
1622
4
    // The first relocation in the group.
1623
4
    Add(1);
1624
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1625
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1626
4
    Add(G[0].r_offset - Offset);
1627
4
    Add(Target->RelativeRel);
1628
4
    if (Config->IsRela) {
1629
0
      Add(G[0].r_addend - Addend);
1630
0
      Addend = G[0].r_addend;
1631
0
    }
1632
4
1633
4
    // The remaining relocations.
1634
4
    Add(G.size() - 1);
1635
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1636
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1637
4
    Add(Config->Wordsize);
1638
4
    Add(Target->RelativeRel);
1639
4
    if (Config->IsRela) {
1640
0
      for (auto I = G.begin() + 1, E = G.end(); I != E; ++I) {
1641
0
        Add(I->r_addend - Addend);
1642
0
        Addend = I->r_addend;
1643
0
      }
1644
0
    }
1645
4
1646
4
    Offset = G.back().r_offset;
1647
4
  }
1648
2
1649
2
  // Now the ungrouped relatives.
1650
2
  if (!UngroupedRelatives.empty()) {
1651
2
    Add(UngroupedRelatives.size());
1652
2
    Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1653
2
    Add(Target->RelativeRel);
1654
14
    for (Elf_Rela &R : UngroupedRelatives) {
1655
14
      Add(R.r_offset - Offset);
1656
14
      Offset = R.r_offset;
1657
14
      if (Config->IsRela) {
1658
0
        Add(R.r_addend - Addend);
1659
0
        Addend = R.r_addend;
1660
0
      }
1661
14
    }
1662
2
  }
1663
2
1664
2
  // Finally the non-relative relocations.
1665
2
  llvm::sort(NonRelatives.begin(), NonRelatives.end(),
1666
2
             [](const Elf_Rela &A, const Elf_Rela &B) {
1667
2
               return A.r_offset < B.r_offset;
1668
2
             });
1669
2
  if (!NonRelatives.empty()) {
1670
2
    Add(NonRelatives.size());
1671
2
    Add(HasAddendIfRela);
1672
4
    for (Elf_Rela &R : NonRelatives) {
1673
4
      Add(R.r_offset - Offset);
1674
4
      Offset = R.r_offset;
1675
4
      Add(R.r_info);
1676
4
      if (Config->IsRela) {
1677
0
        Add(R.r_addend - Addend);
1678
0
        Addend = R.r_addend;
1679
0
      }
1680
4
    }
1681
2
  }
1682
2
1683
2
  // Returns whether the section size changed. We need to keep recomputing both
1684
2
  // section layout and the contents of this section until the size converges
1685
2
  // because changing this section's size can affect section layout, which in
1686
2
  // turn can affect the sizes of the LEB-encoded integers stored in this
1687
2
  // section.
1688
2
  return RelocData.size() != OldSize;
1689
2
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::updateAllocSize()
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::updateAllocSize()
Line
Count
Source
1514
2
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1515
2
  // This function computes the contents of an Android-format packed relocation
1516
2
  // section.
1517
2
  //
1518
2
  // This format compresses relocations by using relocation groups to factor out
1519
2
  // fields that are common between relocations and storing deltas from previous
1520
2
  // relocations in SLEB128 format (which has a short representation for small
1521
2
  // numbers). A good example of a relocation type with common fields is
1522
2
  // R_*_RELATIVE, which is normally used to represent function pointers in
1523
2
  // vtables. In the REL format, each relative relocation has the same r_info
1524
2
  // field, and is only different from other relative relocations in terms of
1525
2
  // the r_offset field. By sorting relocations by offset, grouping them by
1526
2
  // r_info and representing each relocation with only the delta from the
1527
2
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1528
2
  // byte (or less with run-length encoding). This relocation packer was able to
1529
2
  // reduce the size of the relocation section in an Android Chromium DSO from
1530
2
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1531
2
  //
1532
2
  // A relocation section consists of a header containing the literal bytes
1533
2
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1534
2
  // elements are the total number of relocations in the section and an initial
1535
2
  // r_offset value. The remaining elements define a sequence of relocation
1536
2
  // groups. Each relocation group starts with a header consisting of the
1537
2
  // following elements:
1538
2
  //
1539
2
  // - the number of relocations in the relocation group
1540
2
  // - flags for the relocation group
1541
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1542
2
  //   for each relocation in the group.
1543
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1544
2
  //   field for each relocation in the group.
1545
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1546
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1547
2
  //   each relocation in the group.
1548
2
  //
1549
2
  // Following the relocation group header are descriptions of each of the
1550
2
  // relocations in the group. They consist of the following elements:
1551
2
  //
1552
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1553
2
  //   delta for this relocation.
1554
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1555
2
  //   field for this relocation.
1556
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1557
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1558
2
  //   this relocation.
1559
2
1560
2
  size_t OldSize = RelocData.size();
1561
2
1562
2
  RelocData = {'A', 'P', 'S', '2'};
1563
2
  raw_svector_ostream OS(RelocData);
1564
2
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
1565
2
1566
2
  // The format header includes the number of relocations and the initial
1567
2
  // offset (we set this to zero because the first relocation group will
1568
2
  // perform the initial adjustment).
1569
2
  Add(Relocs.size());
1570
2
  Add(0);
1571
2
1572
2
  std::vector<Elf_Rela> Relatives, NonRelatives;
1573
2
1574
52
  for (const DynamicReloc &Rel : Relocs) {
1575
52
    Elf_Rela R;
1576
52
    encodeDynamicReloc<ELFT>(&R, Rel);
1577
52
1578
52
    if (R.getType(Config->IsMips64EL) == Target->RelativeRel)
1579
48
      Relatives.push_back(R);
1580
4
    else
1581
4
      NonRelatives.push_back(R);
1582
52
  }
1583
2
1584
2
  llvm::sort(Relatives.begin(), Relatives.end(),
1585
2
             [](const Elf_Rel &A, const Elf_Rel &B) {
1586
2
               return A.r_offset < B.r_offset;
1587
2
             });
1588
2
1589
2
  // Try to find groups of relative relocations which are spaced one word
1590
2
  // apart from one another. These generally correspond to vtable entries. The
1591
2
  // format allows these groups to be encoded using a sort of run-length
1592
2
  // encoding, but each group will cost 7 bytes in addition to the offset from
1593
2
  // the previous group, so it is only profitable to do this for groups of
1594
2
  // size 8 or larger.
1595
2
  std::vector<Elf_Rela> UngroupedRelatives;
1596
2
  std::vector<std::vector<Elf_Rela>> RelativeGroups;
1597
8
  for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) {
1598
6
    std::vector<Elf_Rela> Group;
1599
48
    do {
1600
48
      Group.push_back(*I++);
1601
48
    } while (I != E && 
(I - 1)->r_offset + Config->Wordsize == I->r_offset46
);
1602
6
1603
6
    if (Group.size() < 8)
1604
2
      UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(),
1605
2
                                Group.end());
1606
4
    else
1607
4
      RelativeGroups.emplace_back(std::move(Group));
1608
6
  }
1609
2
1610
2
  unsigned HasAddendIfRela =
1611
2
      Config->IsRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 
00
;
1612
2
1613
2
  uint64_t Offset = 0;
1614
2
  uint64_t Addend = 0;
1615
2
1616
2
  // Emit the run-length encoding for the groups of adjacent relative
1617
2
  // relocations. Each group is represented using two groups in the packed
1618
2
  // format. The first is used to set the current offset to the start of the
1619
2
  // group (and also encodes the first relocation), and the second encodes the
1620
2
  // remaining relocations.
1621
4
  for (std::vector<Elf_Rela> &G : RelativeGroups) {
1622
4
    // The first relocation in the group.
1623
4
    Add(1);
1624
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1625
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1626
4
    Add(G[0].r_offset - Offset);
1627
4
    Add(Target->RelativeRel);
1628
4
    if (Config->IsRela) {
1629
4
      Add(G[0].r_addend - Addend);
1630
4
      Addend = G[0].r_addend;
1631
4
    }
1632
4
1633
4
    // The remaining relocations.
1634
4
    Add(G.size() - 1);
1635
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1636
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1637
4
    Add(Config->Wordsize);
1638
4
    Add(Target->RelativeRel);
1639
4
    if (Config->IsRela) {
1640
34
      for (auto I = G.begin() + 1, E = G.end(); I != E; 
++I30
) {
1641
30
        Add(I->r_addend - Addend);
1642
30
        Addend = I->r_addend;
1643
30
      }
1644
4
    }
1645
4
1646
4
    Offset = G.back().r_offset;
1647
4
  }
1648
2
1649
2
  // Now the ungrouped relatives.
1650
2
  if (!UngroupedRelatives.empty()) {
1651
2
    Add(UngroupedRelatives.size());
1652
2
    Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1653
2
    Add(Target->RelativeRel);
1654
14
    for (Elf_Rela &R : UngroupedRelatives) {
1655
14
      Add(R.r_offset - Offset);
1656
14
      Offset = R.r_offset;
1657
14
      if (Config->IsRela) {
1658
14
        Add(R.r_addend - Addend);
1659
14
        Addend = R.r_addend;
1660
14
      }
1661
14
    }
1662
2
  }
1663
2
1664
2
  // Finally the non-relative relocations.
1665
2
  llvm::sort(NonRelatives.begin(), NonRelatives.end(),
1666
2
             [](const Elf_Rela &A, const Elf_Rela &B) {
1667
2
               return A.r_offset < B.r_offset;
1668
2
             });
1669
2
  if (!NonRelatives.empty()) {
1670
2
    Add(NonRelatives.size());
1671
2
    Add(HasAddendIfRela);
1672
4
    for (Elf_Rela &R : NonRelatives) {
1673
4
      Add(R.r_offset - Offset);
1674
4
      Offset = R.r_offset;
1675
4
      Add(R.r_info);
1676
4
      if (Config->IsRela) {
1677
4
        Add(R.r_addend - Addend);
1678
4
        Addend = R.r_addend;
1679
4
      }
1680
4
    }
1681
2
  }
1682
2
1683
2
  // Returns whether the section size changed. We need to keep recomputing both
1684
2
  // section layout and the contents of this section until the size converges
1685
2
  // because changing this section's size can affect section layout, which in
1686
2
  // turn can affect the sizes of the LEB-encoded integers stored in this
1687
2
  // section.
1688
2
  return RelocData.size() != OldSize;
1689
2
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<l