Coverage Report

Created: 2018-10-20 12:32

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