Coverage Report

Created: 2019-07-24 05:18

/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
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains linker-synthesized sections. Currently,
10
// synthetic sections are created either output sections or input sections,
11
// but we are rewriting code so that all synthetic sections are created as
12
// input sections.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "SyntheticSections.h"
17
#include "Config.h"
18
#include "InputFiles.h"
19
#include "LinkerScript.h"
20
#include "OutputSections.h"
21
#include "SymbolTable.h"
22
#include "Symbols.h"
23
#include "Target.h"
24
#include "Writer.h"
25
#include "lld/Common/ErrorHandler.h"
26
#include "lld/Common/Memory.h"
27
#include "lld/Common/Strings.h"
28
#include "lld/Common/Threads.h"
29
#include "lld/Common/Version.h"
30
#include "llvm/ADT/SetOperations.h"
31
#include "llvm/ADT/StringExtras.h"
32
#include "llvm/BinaryFormat/Dwarf.h"
33
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
34
#include "llvm/Object/ELFObjectFile.h"
35
#include "llvm/Support/Compression.h"
36
#include "llvm/Support/Endian.h"
37
#include "llvm/Support/LEB128.h"
38
#include "llvm/Support/MD5.h"
39
#include <cstdlib>
40
#include <thread>
41
42
using namespace llvm;
43
using namespace llvm::dwarf;
44
using namespace llvm::ELF;
45
using namespace llvm::object;
46
using namespace llvm::support;
47
48
using namespace lld;
49
using namespace lld::elf;
50
51
using llvm::support::endian::read32le;
52
using llvm::support::endian::write32le;
53
using llvm::support::endian::write64le;
54
55
constexpr size_t MergeNoTailSection::numShards;
56
57
1.23k
static uint64_t readUint(uint8_t *buf) {
58
1.23k
  return config->is64 ? 
read64(buf)1.04k
:
read32(buf)196
;
59
1.23k
}
60
61
11.5k
static void writeUint(uint8_t *buf, uint64_t val) {
62
11.5k
  if (config->is64)
63
11.1k
    write64(buf, val);
64
419
  else
65
419
    write32(buf, val);
66
11.5k
}
67
68
// Returns an LLD version string.
69
2.66k
static ArrayRef<uint8_t> getVersion() {
70
2.66k
  // Check LLD_VERSION first for ease of testing.
71
2.66k
  // You can get consistent output by using the environment variable.
72
2.66k
  // This is only for testing.
73
2.66k
  StringRef s = getenv("LLD_VERSION");
74
2.66k
  if (s.empty())
75
0
    s = saver.save(Twine("Linker: ") + getLLDVersion());
76
2.66k
77
2.66k
  // +1 to include the terminating '\0'.
78
2.66k
  return {(const uint8_t *)s.data(), s.size() + 1};
79
2.66k
}
80
81
// Creates a .comment section containing LLD version info.
82
// With this feature, you can identify LLD-generated binaries easily
83
// by "readelf --string-dump .comment <file>".
84
// The returned object is a mergeable string section.
85
2.66k
MergeInputSection *elf::createCommentSection() {
86
2.66k
  return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,
87
2.66k
                                 getVersion(), ".comment");
88
2.66k
}
89
90
// .MIPS.abiflags section.
91
template <class ELFT>
92
MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags flags)
93
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
94
192
      flags(flags) {
95
192
  this->entsize = sizeof(Elf_Mips_ABIFlags);
96
192
}
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
94
23
      flags(flags) {
95
23
  this->entsize = sizeof(Elf_Mips_ABIFlags);
96
23
}
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
94
123
      flags(flags) {
95
123
  this->entsize = sizeof(Elf_Mips_ABIFlags);
96
123
}
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
94
3
      flags(flags) {
95
3
  this->entsize = sizeof(Elf_Mips_ABIFlags);
96
3
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::MipsAbiFlagsSection(llvm::object::Elf_Mips_ABIFlags<llvm::object::ELFType<(llvm::support::endianness)0, true> >)
Line
Count
Source
94
43
      flags(flags) {
95
43
  this->entsize = sizeof(Elf_Mips_ABIFlags);
96
43
}
97
98
185
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
99
185
  memcpy(buf, &flags, sizeof(flags));
100
185
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
98
22
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
99
22
  memcpy(buf, &flags, sizeof(flags));
100
22
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
98
122
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
99
122
  memcpy(buf, &flags, sizeof(flags));
100
122
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
98
3
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
99
3
  memcpy(buf, &flags, sizeof(flags));
100
3
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
98
38
template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
99
38
  memcpy(buf, &flags, sizeof(flags));
100
38
}
101
102
template <class ELFT>
103
198
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
198
  Elf_Mips_ABIFlags flags = {};
105
198
  bool create = false;
106
198
107
1.93k
  for (InputSectionBase *sec : inputSections) {
108
1.93k
    if (sec->type != SHT_MIPS_ABIFLAGS)
109
1.68k
      continue;
110
254
    sec->markDead();
111
254
    create = true;
112
254
113
254
    std::string filename = toString(sec->file);
114
254
    const size_t size = sec->data().size();
115
254
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
254
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
254
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
254
    if (size < sizeof(Elf_Mips_ABIFlags)) {
119
0
      error(filename + ": invalid size of .MIPS.abiflags section: got " +
120
0
            Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
121
0
      return nullptr;
122
0
    }
123
254
    auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124
254
    if (s->version != 0) {
125
0
      error(filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(s->version));
127
0
      return nullptr;
128
0
    }
129
254
130
254
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
254
    // select the highest number of ISA/Rev/Ext.
132
254
    flags.isa_level = std::max(flags.isa_level, s->isa_level);
133
254
    flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
134
254
    flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
135
254
    flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
136
254
    flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
137
254
    flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
138
254
    flags.ases |= s->ases;
139
254
    flags.flags1 |= s->flags1;
140
254
    flags.flags2 |= s->flags2;
141
254
    flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);
142
254
  };
143
198
144
198
  if (create)
145
192
    return make<MipsAbiFlagsSection<ELFT>>(flags);
146
6
  return nullptr;
147
6
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
103
24
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
24
  Elf_Mips_ABIFlags flags = {};
105
24
  bool create = false;
106
24
107
218
  for (InputSectionBase *sec : inputSections) {
108
218
    if (sec->type != SHT_MIPS_ABIFLAGS)
109
190
      continue;
110
28
    sec->markDead();
111
28
    create = true;
112
28
113
28
    std::string filename = toString(sec->file);
114
28
    const size_t size = sec->data().size();
115
28
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
28
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
28
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
28
    if (size < sizeof(Elf_Mips_ABIFlags)) {
119
0
      error(filename + ": invalid size of .MIPS.abiflags section: got " +
120
0
            Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
121
0
      return nullptr;
122
0
    }
123
28
    auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124
28
    if (s->version != 0) {
125
0
      error(filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(s->version));
127
0
      return nullptr;
128
0
    }
129
28
130
28
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
28
    // select the highest number of ISA/Rev/Ext.
132
28
    flags.isa_level = std::max(flags.isa_level, s->isa_level);
133
28
    flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
134
28
    flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
135
28
    flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
136
28
    flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
137
28
    flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
138
28
    flags.ases |= s->ases;
139
28
    flags.flags1 |= s->flags1;
140
28
    flags.flags2 |= s->flags2;
141
28
    flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);
142
28
  };
143
24
144
24
  if (create)
145
23
    return make<MipsAbiFlagsSection<ELFT>>(flags);
146
1
  return nullptr;
147
1
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
103
126
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
126
  Elf_Mips_ABIFlags flags = {};
105
126
  bool create = false;
106
126
107
1.28k
  for (InputSectionBase *sec : inputSections) {
108
1.28k
    if (sec->type != SHT_MIPS_ABIFLAGS)
109
1.11k
      continue;
110
168
    sec->markDead();
111
168
    create = true;
112
168
113
168
    std::string filename = toString(sec->file);
114
168
    const size_t size = sec->data().size();
115
168
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
168
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
168
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
168
    if (size < sizeof(Elf_Mips_ABIFlags)) {
119
0
      error(filename + ": invalid size of .MIPS.abiflags section: got " +
120
0
            Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
121
0
      return nullptr;
122
0
    }
123
168
    auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124
168
    if (s->version != 0) {
125
0
      error(filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(s->version));
127
0
      return nullptr;
128
0
    }
129
168
130
168
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
168
    // select the highest number of ISA/Rev/Ext.
132
168
    flags.isa_level = std::max(flags.isa_level, s->isa_level);
133
168
    flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
134
168
    flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
135
168
    flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
136
168
    flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
137
168
    flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
138
168
    flags.ases |= s->ases;
139
168
    flags.flags1 |= s->flags1;
140
168
    flags.flags2 |= s->flags2;
141
168
    flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);
142
168
  };
143
126
144
126
  if (create)
145
123
    return make<MipsAbiFlagsSection<ELFT>>(flags);
146
3
  return nullptr;
147
3
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
103
3
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
3
  Elf_Mips_ABIFlags flags = {};
105
3
  bool create = false;
106
3
107
24
  for (InputSectionBase *sec : inputSections) {
108
24
    if (sec->type != SHT_MIPS_ABIFLAGS)
109
21
      continue;
110
3
    sec->markDead();
111
3
    create = true;
112
3
113
3
    std::string filename = toString(sec->file);
114
3
    const size_t size = sec->data().size();
115
3
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
3
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
3
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
3
    if (size < sizeof(Elf_Mips_ABIFlags)) {
119
0
      error(filename + ": invalid size of .MIPS.abiflags section: got " +
120
0
            Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
121
0
      return nullptr;
122
0
    }
123
3
    auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124
3
    if (s->version != 0) {
125
0
      error(filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(s->version));
127
0
      return nullptr;
128
0
    }
129
3
130
3
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
3
    // select the highest number of ISA/Rev/Ext.
132
3
    flags.isa_level = std::max(flags.isa_level, s->isa_level);
133
3
    flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
134
3
    flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
135
3
    flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
136
3
    flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
137
3
    flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
138
3
    flags.ases |= s->ases;
139
3
    flags.flags1 |= s->flags1;
140
3
    flags.flags2 |= s->flags2;
141
3
    flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);
142
3
  };
143
3
144
3
  if (create)
145
3
    return make<MipsAbiFlagsSection<ELFT>>(flags);
146
0
  return nullptr;
147
0
}
lld::elf::MipsAbiFlagsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
103
45
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
45
  Elf_Mips_ABIFlags flags = {};
105
45
  bool create = false;
106
45
107
409
  for (InputSectionBase *sec : inputSections) {
108
409
    if (sec->type != SHT_MIPS_ABIFLAGS)
109
354
      continue;
110
55
    sec->markDead();
111
55
    create = true;
112
55
113
55
    std::string filename = toString(sec->file);
114
55
    const size_t size = sec->data().size();
115
55
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
55
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
55
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
55
    if (size < sizeof(Elf_Mips_ABIFlags)) {
119
0
      error(filename + ": invalid size of .MIPS.abiflags section: got " +
120
0
            Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
121
0
      return nullptr;
122
0
    }
123
55
    auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124
55
    if (s->version != 0) {
125
0
      error(filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(s->version));
127
0
      return nullptr;
128
0
    }
129
55
130
55
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
55
    // select the highest number of ISA/Rev/Ext.
132
55
    flags.isa_level = std::max(flags.isa_level, s->isa_level);
133
55
    flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
134
55
    flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
135
55
    flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
136
55
    flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
137
55
    flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
138
55
    flags.ases |= s->ases;
139
55
    flags.flags1 |= s->flags1;
140
55
    flags.flags2 |= s->flags2;
141
55
    flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);
142
55
  };
143
45
144
45
  if (create)
145
43
    return make<MipsAbiFlagsSection<ELFT>>(flags);
146
2
  return nullptr;
147
2
}
148
149
// .MIPS.options section.
150
template <class ELFT>
151
MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo reginfo)
152
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
153
42
      reginfo(reginfo) {
154
42
  this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
155
42
}
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, false> >)
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, false> >)
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)1, true> >)
Line
Count
Source
153
3
      reginfo(reginfo) {
154
3
  this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
155
3
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::MipsOptionsSection(llvm::object::Elf_Mips_RegInfo<llvm::object::ELFType<(llvm::support::endianness)0, true> >)
Line
Count
Source
153
39
      reginfo(reginfo) {
154
39
  this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
155
39
}
156
157
41
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {
158
41
  auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
159
41
  options->kind = ODK_REGINFO;
160
41
  options->size = getSize();
161
41
162
41
  if (!config->relocatable)
163
39
    reginfo.ri_gp_value = in.mipsGot->getGp();
164
41
  memcpy(buf + sizeof(Elf_Mips_Options), &reginfo, sizeof(reginfo));
165
41
}
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Unexecuted instantiation: lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
157
3
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {
158
3
  auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
159
3
  options->kind = ODK_REGINFO;
160
3
  options->size = getSize();
161
3
162
3
  if (!config->relocatable)
163
3
    reginfo.ri_gp_value = in.mipsGot->getGp();
164
3
  memcpy(buf + sizeof(Elf_Mips_Options), &reginfo, sizeof(reginfo));
165
3
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
157
38
template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {
158
38
  auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
159
38
  options->kind = ODK_REGINFO;
160
38
  options->size = getSize();
161
38
162
38
  if (!config->relocatable)
163
36
    reginfo.ri_gp_value = in.mipsGot->getGp();
164
38
  memcpy(buf + sizeof(Elf_Mips_Options), &reginfo, sizeof(reginfo));
165
38
}
166
167
template <class ELFT>
168
198
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
198
  // N64 ABI only.
170
198
  if (!ELFT::Is64Bits)
171
150
    return nullptr;
172
48
173
48
  std::vector<InputSectionBase *> sections;
174
48
  for (InputSectionBase *sec : inputSections)
175
479
    if (sec->type == SHT_MIPS_OPTIONS)
176
50
      sections.push_back(sec);
177
48
178
48
  if (sections.empty())
179
6
    return nullptr;
180
42
181
42
  Elf_Mips_RegInfo reginfo = {};
182
50
  for (InputSectionBase *sec : sections) {
183
50
    sec->markDead();
184
50
185
50
    std::string filename = toString(sec->file);
186
50
    ArrayRef<uint8_t> d = sec->data();
187
50
188
50
    while (!d.empty()) {
189
50
      if (d.size() < sizeof(Elf_Mips_Options)) {
190
0
        error(filename + ": invalid size of .MIPS.options section");
191
0
        break;
192
0
      }
193
50
194
50
      auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
195
50
      if (opt->kind == ODK_REGINFO) {
196
50
        reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
197
50
        sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
198
50
        break;
199
50
      }
200
0
201
0
      if (!opt->size)
202
0
        fatal(filename + ": zero option descriptor size");
203
0
      d = d.slice(opt->size);
204
0
    }
205
50
  };
206
42
207
42
  return make<MipsOptionsSection<ELFT>>(reginfo);
208
42
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
168
24
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
24
  // N64 ABI only.
170
24
  if (!ELFT::Is64Bits)
171
24
    return nullptr;
172
0
173
0
  std::vector<InputSectionBase *> sections;
174
0
  for (InputSectionBase *sec : inputSections)
175
0
    if (sec->type == SHT_MIPS_OPTIONS)
176
0
      sections.push_back(sec);
177
0
178
0
  if (sections.empty())
179
0
    return nullptr;
180
0
181
0
  Elf_Mips_RegInfo reginfo = {};
182
0
  for (InputSectionBase *sec : sections) {
183
0
    sec->markDead();
184
0
185
0
    std::string filename = toString(sec->file);
186
0
    ArrayRef<uint8_t> d = sec->data();
187
0
188
0
    while (!d.empty()) {
189
0
      if (d.size() < sizeof(Elf_Mips_Options)) {
190
0
        error(filename + ": invalid size of .MIPS.options section");
191
0
        break;
192
0
      }
193
0
194
0
      auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
195
0
      if (opt->kind == ODK_REGINFO) {
196
0
        reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
197
0
        sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
198
0
        break;
199
0
      }
200
0
201
0
      if (!opt->size)
202
0
        fatal(filename + ": zero option descriptor size");
203
0
      d = d.slice(opt->size);
204
0
    }
205
0
  };
206
0
207
0
  return make<MipsOptionsSection<ELFT>>(reginfo);
208
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
168
126
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
126
  // N64 ABI only.
170
126
  if (!ELFT::Is64Bits)
171
126
    return nullptr;
172
0
173
0
  std::vector<InputSectionBase *> sections;
174
0
  for (InputSectionBase *sec : inputSections)
175
0
    if (sec->type == SHT_MIPS_OPTIONS)
176
0
      sections.push_back(sec);
177
0
178
0
  if (sections.empty())
179
0
    return nullptr;
180
0
181
0
  Elf_Mips_RegInfo reginfo = {};
182
0
  for (InputSectionBase *sec : sections) {
183
0
    sec->markDead();
184
0
185
0
    std::string filename = toString(sec->file);
186
0
    ArrayRef<uint8_t> d = sec->data();
187
0
188
0
    while (!d.empty()) {
189
0
      if (d.size() < sizeof(Elf_Mips_Options)) {
190
0
        error(filename + ": invalid size of .MIPS.options section");
191
0
        break;
192
0
      }
193
0
194
0
      auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
195
0
      if (opt->kind == ODK_REGINFO) {
196
0
        reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
197
0
        sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
198
0
        break;
199
0
      }
200
0
201
0
      if (!opt->size)
202
0
        fatal(filename + ": zero option descriptor size");
203
0
      d = d.slice(opt->size);
204
0
    }
205
0
  };
206
0
207
0
  return make<MipsOptionsSection<ELFT>>(reginfo);
208
0
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
168
3
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
3
  // N64 ABI only.
170
3
  if (!ELFT::Is64Bits)
171
0
    return nullptr;
172
3
173
3
  std::vector<InputSectionBase *> sections;
174
3
  for (InputSectionBase *sec : inputSections)
175
27
    if (sec->type == SHT_MIPS_OPTIONS)
176
3
      sections.push_back(sec);
177
3
178
3
  if (sections.empty())
179
0
    return nullptr;
180
3
181
3
  Elf_Mips_RegInfo reginfo = {};
182
3
  for (InputSectionBase *sec : sections) {
183
3
    sec->markDead();
184
3
185
3
    std::string filename = toString(sec->file);
186
3
    ArrayRef<uint8_t> d = sec->data();
187
3
188
3
    while (!d.empty()) {
189
3
      if (d.size() < sizeof(Elf_Mips_Options)) {
190
0
        error(filename + ": invalid size of .MIPS.options section");
191
0
        break;
192
0
      }
193
3
194
3
      auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
195
3
      if (opt->kind == ODK_REGINFO) {
196
3
        reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
197
3
        sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
198
3
        break;
199
3
      }
200
0
201
0
      if (!opt->size)
202
0
        fatal(filename + ": zero option descriptor size");
203
0
      d = d.slice(opt->size);
204
0
    }
205
3
  };
206
3
207
3
  return make<MipsOptionsSection<ELFT>>(reginfo);
208
3
}
lld::elf::MipsOptionsSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
168
45
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
45
  // N64 ABI only.
170
45
  if (!ELFT::Is64Bits)
171
0
    return nullptr;
172
45
173
45
  std::vector<InputSectionBase *> sections;
174
45
  for (InputSectionBase *sec : inputSections)
175
452
    if (sec->type == SHT_MIPS_OPTIONS)
176
47
      sections.push_back(sec);
177
45
178
45
  if (sections.empty())
179
6
    return nullptr;
180
39
181
39
  Elf_Mips_RegInfo reginfo = {};
182
47
  for (InputSectionBase *sec : sections) {
183
47
    sec->markDead();
184
47
185
47
    std::string filename = toString(sec->file);
186
47
    ArrayRef<uint8_t> d = sec->data();
187
47
188
47
    while (!d.empty()) {
189
47
      if (d.size() < sizeof(Elf_Mips_Options)) {
190
0
        error(filename + ": invalid size of .MIPS.options section");
191
0
        break;
192
0
      }
193
47
194
47
      auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
195
47
      if (opt->kind == ODK_REGINFO) {
196
47
        reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
197
47
        sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
198
47
        break;
199
47
      }
200
0
201
0
      if (!opt->size)
202
0
        fatal(filename + ": zero option descriptor size");
203
0
      d = d.slice(opt->size);
204
0
    }
205
47
  };
206
39
207
39
  return make<MipsOptionsSection<ELFT>>(reginfo);
208
39
}
209
210
// MIPS .reginfo section.
211
template <class ELFT>
212
MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo reginfo)
213
    : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
214
146
      reginfo(reginfo) {
215
146
  this->entsize = sizeof(Elf_Mips_RegInfo);
216
146
}
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
214
23
      reginfo(reginfo) {
215
23
  this->entsize = sizeof(Elf_Mips_RegInfo);
216
23
}
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
214
123
      reginfo(reginfo) {
215
123
  this->entsize = sizeof(Elf_Mips_RegInfo);
216
123
}
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> >)
217
218
144
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {
219
144
  if (!config->relocatable)
220
132
    reginfo.ri_gp_value = in.mipsGot->getGp();
221
144
  memcpy(buf, &reginfo, sizeof(reginfo));
222
144
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
218
22
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {
219
22
  if (!config->relocatable)
220
21
    reginfo.ri_gp_value = in.mipsGot->getGp();
221
22
  memcpy(buf, &reginfo, sizeof(reginfo));
222
22
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
218
122
template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {
219
122
  if (!config->relocatable)
220
111
    reginfo.ri_gp_value = in.mipsGot->getGp();
221
122
  memcpy(buf, &reginfo, sizeof(reginfo));
222
122
}
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*)
223
224
template <class ELFT>
225
198
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
198
  // Section should be alive for O32 and N32 ABIs only.
227
198
  if (ELFT::Is64Bits)
228
48
    return nullptr;
229
150
230
150
  std::vector<InputSectionBase *> sections;
231
150
  for (InputSectionBase *sec : inputSections)
232
1.65k
    if (sec->type == SHT_MIPS_REGINFO)
233
196
      sections.push_back(sec);
234
150
235
150
  if (sections.empty())
236
4
    return nullptr;
237
146
238
146
  Elf_Mips_RegInfo reginfo = {};
239
196
  for (InputSectionBase *sec : sections) {
240
196
    sec->markDead();
241
196
242
196
    if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243
0
      error(toString(sec->file) + ": invalid size of .reginfo section");
244
0
      return nullptr;
245
0
    }
246
196
247
196
    auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248
196
    reginfo.ri_gprmask |= r->ri_gprmask;
249
196
    sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
250
196
  };
251
146
252
146
  return make<MipsReginfoSection<ELFT>>(reginfo);
253
146
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
225
24
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
24
  // Section should be alive for O32 and N32 ABIs only.
227
24
  if (ELFT::Is64Bits)
228
0
    return nullptr;
229
24
230
24
  std::vector<InputSectionBase *> sections;
231
24
  for (InputSectionBase *sec : inputSections)
232
241
    if (sec->type == SHT_MIPS_REGINFO)
233
28
      sections.push_back(sec);
234
24
235
24
  if (sections.empty())
236
1
    return nullptr;
237
23
238
23
  Elf_Mips_RegInfo reginfo = {};
239
28
  for (InputSectionBase *sec : sections) {
240
28
    sec->markDead();
241
28
242
28
    if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243
0
      error(toString(sec->file) + ": invalid size of .reginfo section");
244
0
      return nullptr;
245
0
    }
246
28
247
28
    auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248
28
    reginfo.ri_gprmask |= r->ri_gprmask;
249
28
    sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
250
28
  };
251
23
252
23
  return make<MipsReginfoSection<ELFT>>(reginfo);
253
23
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::create()
Line
Count
Source
225
126
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
126
  // Section should be alive for O32 and N32 ABIs only.
227
126
  if (ELFT::Is64Bits)
228
0
    return nullptr;
229
126
230
126
  std::vector<InputSectionBase *> sections;
231
126
  for (InputSectionBase *sec : inputSections)
232
1.41k
    if (sec->type == SHT_MIPS_REGINFO)
233
168
      sections.push_back(sec);
234
126
235
126
  if (sections.empty())
236
3
    return nullptr;
237
123
238
123
  Elf_Mips_RegInfo reginfo = {};
239
168
  for (InputSectionBase *sec : sections) {
240
168
    sec->markDead();
241
168
242
168
    if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243
0
      error(toString(sec->file) + ": invalid size of .reginfo section");
244
0
      return nullptr;
245
0
    }
246
168
247
168
    auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248
168
    reginfo.ri_gprmask |= r->ri_gprmask;
249
168
    sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
250
168
  };
251
123
252
123
  return make<MipsReginfoSection<ELFT>>(reginfo);
253
123
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::create()
Line
Count
Source
225
3
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
3
  // Section should be alive for O32 and N32 ABIs only.
227
3
  if (ELFT::Is64Bits)
228
3
    return nullptr;
229
0
230
0
  std::vector<InputSectionBase *> sections;
231
0
  for (InputSectionBase *sec : inputSections)
232
0
    if (sec->type == SHT_MIPS_REGINFO)
233
0
      sections.push_back(sec);
234
0
235
0
  if (sections.empty())
236
0
    return nullptr;
237
0
238
0
  Elf_Mips_RegInfo reginfo = {};
239
0
  for (InputSectionBase *sec : sections) {
240
0
    sec->markDead();
241
0
242
0
    if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243
0
      error(toString(sec->file) + ": invalid size of .reginfo section");
244
0
      return nullptr;
245
0
    }
246
0
247
0
    auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248
0
    reginfo.ri_gprmask |= r->ri_gprmask;
249
0
    sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
250
0
  };
251
0
252
0
  return make<MipsReginfoSection<ELFT>>(reginfo);
253
0
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::create()
Line
Count
Source
225
45
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
45
  // Section should be alive for O32 and N32 ABIs only.
227
45
  if (ELFT::Is64Bits)
228
45
    return nullptr;
229
0
230
0
  std::vector<InputSectionBase *> sections;
231
0
  for (InputSectionBase *sec : inputSections)
232
0
    if (sec->type == SHT_MIPS_REGINFO)
233
0
      sections.push_back(sec);
234
0
235
0
  if (sections.empty())
236
0
    return nullptr;
237
0
238
0
  Elf_Mips_RegInfo reginfo = {};
239
0
  for (InputSectionBase *sec : sections) {
240
0
    sec->markDead();
241
0
242
0
    if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243
0
      error(toString(sec->file) + ": invalid size of .reginfo section");
244
0
      return nullptr;
245
0
    }
246
0
247
0
    auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248
0
    reginfo.ri_gprmask |= r->ri_gprmask;
249
0
    sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
250
0
  };
251
0
252
0
  return make<MipsReginfoSection<ELFT>>(reginfo);
253
0
}
254
255
8
InputSection *elf::createInterpSection() {
256
8
  // StringSaver guarantees that the returned string ends with '\0'.
257
8
  StringRef s = saver.save(config->dynamicLinker);
258
8
  ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
259
8
260
8
  auto *sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents,
261
8
                                 ".interp");
262
8
  sec->markLive();
263
8
  return sec;
264
8
}
265
266
Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
267
567
                                uint64_t size, InputSectionBase &section) {
268
567
  auto *s = make<Defined>(section.file, name, STB_LOCAL, STV_DEFAULT, type,
269
567
                          value, size, &section);
270
567
  if (in.symTab)
271
567
    in.symTab->addSymbol(s);
272
567
  return s;
273
567
}
274
275
19
static size_t getHashSize() {
276
19
  switch (config->buildId) {
277
19
  case BuildIdKind::Fast:
278
6
    return 8;
279
19
  case BuildIdKind::Md5:
280
4
  case BuildIdKind::Uuid:
281
4
    return 16;
282
6
  case BuildIdKind::Sha1:
283
6
    return 20;
284
4
  case BuildIdKind::Hexstring:
285
3
    return config->buildIdVector.size();
286
4
  default:
287
0
    llvm_unreachable("unknown BuildIdKind");
288
19
  }
289
19
}
290
291
// This class represents a linker-synthesized .note.gnu.property section.
292
//
293
// In x86 and AArch64, object files may contain feature flags indicating the
294
// features that they have used. The flags are stored in a .note.gnu.property
295
// section.
296
//
297
// lld reads the sections from input files and merges them by computing AND of
298
// the flags. The result is written as a new .note.gnu.property section.
299
//
300
// If the flag is zero (which indicates that the intersection of the feature
301
// sets is empty, or some input files didn't have .note.gnu.property sections),
302
// we don't create this section.
303
GnuPropertySection::GnuPropertySection()
304
    : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, 4,
305
24
                       ".note.gnu.property") {}
306
307
24
void GnuPropertySection::writeTo(uint8_t *buf) {
308
24
  uint32_t featureAndType = config->emachine == EM_AARCH64
309
24
                                ? 
GNU_PROPERTY_AARCH64_FEATURE_1_AND11
310
24
                                : 
GNU_PROPERTY_X86_FEATURE_1_AND13
;
311
24
312
24
  write32(buf, 4);                                   // Name size
313
24
  write32(buf + 4, config->is64 ? 
1621
:
123
); // Content size
314
24
  write32(buf + 8, NT_GNU_PROPERTY_TYPE_0);          // Type
315
24
  memcpy(buf + 12, "GNU", 4);                        // Name string
316
24
  write32(buf + 16, featureAndType);                 // Feature type
317
24
  write32(buf + 20, 4);                              // Feature size
318
24
  write32(buf + 24, config->andFeatures);            // Feature flags
319
24
  if (config->is64)
320
21
    write32(buf + 28, 0); // Padding
321
24
}
322
323
96
size_t GnuPropertySection::getSize() const { return config->is64 ? 
3284
:
2812
; }
324
325
BuildIdSection::BuildIdSection()
326
    : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"),
327
19
      hashSize(getHashSize()) {}
328
329
19
void BuildIdSection::writeTo(uint8_t *buf) {
330
19
  write32(buf, 4);                      // Name size
331
19
  write32(buf + 4, hashSize);           // Content size
332
19
  write32(buf + 8, NT_GNU_BUILD_ID);    // Type
333
19
  memcpy(buf + 12, "GNU", 4);           // Name string
334
19
  hashBuf = buf + 16;
335
19
}
336
337
19
void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) {
338
19
  assert(buf.size() == hashSize);
339
19
  memcpy(hashBuf, buf.data(), hashSize);
340
19
}
341
342
BssSection::BssSection(StringRef name, uint64_t size, uint32_t alignment)
343
5.67k
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, alignment, name) {
344
5.67k
  this->bss = true;
345
5.67k
  this->size = size;
346
5.67k
}
347
348
EhFrameSection::EhFrameSection()
349
2.68k
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
350
351
// Search for an existing CIE record or create a new one.
352
// CIE records from input object files are uniquified by their contents
353
// and where their relocations point to.
354
template <class ELFT, class RelTy>
355
108
CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
356
108
  Symbol *personality = nullptr;
357
108
  unsigned firstRelI = cie.firstRelocation;
358
108
  if (firstRelI != (unsigned)-1)
359
11
    personality =
360
11
        &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
361
108
362
108
  // Search for an existing CIE by CIE contents/relocation target pair.
363
108
  CieRecord *&rec = cieMap[{cie.data(), personality}];
364
108
365
108
  // If not found, create a new one.
366
108
  if (!rec) {
367
95
    rec = make<CieRecord>();
368
95
    rec->cie = &cie;
369
95
    cieRecords.push_back(rec);
370
95
  }
371
108
  return rec;
372
108
}
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
355
4
CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
356
4
  Symbol *personality = nullptr;
357
4
  unsigned firstRelI = cie.firstRelocation;
358
4
  if (firstRelI != (unsigned)-1)
359
2
    personality =
360
2
        &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
361
4
362
4
  // Search for an existing CIE by CIE contents/relocation target pair.
363
4
  CieRecord *&rec = cieMap[{cie.data(), personality}];
364
4
365
4
  // If not found, create a new one.
366
4
  if (!rec) {
367
3
    rec = make<CieRecord>();
368
3
    rec->cie = &cie;
369
3
    cieRecords.push_back(rec);
370
3
  }
371
4
  return rec;
372
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> >)
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>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
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
355
93
CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
356
93
  Symbol *personality = nullptr;
357
93
  unsigned firstRelI = cie.firstRelocation;
358
93
  if (firstRelI != (unsigned)-1)
359
8
    personality =
360
8
        &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
361
93
362
93
  // Search for an existing CIE by CIE contents/relocation target pair.
363
93
  CieRecord *&rec = cieMap[{cie.data(), personality}];
364
93
365
93
  // If not found, create a new one.
366
93
  if (!rec) {
367
81
    rec = make<CieRecord>();
368
81
    rec->cie = &cie;
369
81
    cieRecords.push_back(rec);
370
81
  }
371
93
  return rec;
372
93
}
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
355
7
CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
356
7
  Symbol *personality = nullptr;
357
7
  unsigned firstRelI = cie.firstRelocation;
358
7
  if (firstRelI != (unsigned)-1)
359
0
    personality =
360
0
        &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
361
7
362
7
  // Search for an existing CIE by CIE contents/relocation target pair.
363
7
  CieRecord *&rec = cieMap[{cie.data(), personality}];
364
7
365
7
  // If not found, create a new one.
366
7
  if (!rec) {
367
7
    rec = make<CieRecord>();
368
7
    rec->cie = &cie;
369
7
    cieRecords.push_back(rec);
370
7
  }
371
7
  return rec;
372
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
355
3
CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
356
3
  Symbol *personality = nullptr;
357
3
  unsigned firstRelI = cie.firstRelocation;
358
3
  if (firstRelI != (unsigned)-1)
359
1
    personality =
360
1
        &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
361
3
362
3
  // Search for an existing CIE by CIE contents/relocation target pair.
363
3
  CieRecord *&rec = cieMap[{cie.data(), personality}];
364
3
365
3
  // If not found, create a new one.
366
3
  if (!rec) {
367
3
    rec = make<CieRecord>();
368
3
    rec->cie = &cie;
369
3
    cieRecords.push_back(rec);
370
3
  }
371
3
  return rec;
372
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
355
1
CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
356
1
  Symbol *personality = nullptr;
357
1
  unsigned firstRelI = cie.firstRelocation;
358
1
  if (firstRelI != (unsigned)-1)
359
0
    personality =
360
0
        &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
361
1
362
1
  // Search for an existing CIE by CIE contents/relocation target pair.
363
1
  CieRecord *&rec = cieMap[{cie.data(), personality}];
364
1
365
1
  // If not found, create a new one.
366
1
  if (!rec) {
367
1
    rec = make<CieRecord>();
368
1
    rec->cie = &cie;
369
1
    cieRecords.push_back(rec);
370
1
  }
371
1
  return rec;
372
1
}
373
374
// There is one FDE per function. Returns true if a given FDE
375
// points to a live function.
376
template <class ELFT, class RelTy>
377
136
bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
378
136
  auto *sec = cast<EhInputSection>(fde.sec);
379
136
  unsigned firstRelI = fde.firstRelocation;
380
136
381
136
  // An FDE should point to some function because FDEs are to describe
382
136
  // functions. That's however not always the case due to an issue of
383
136
  // ld.gold with -r. ld.gold may discard only functions and leave their
384
136
  // corresponding FDEs, which results in creating bad .eh_frame sections.
385
136
  // To deal with that, we ignore such FDEs.
386
136
  if (firstRelI == (unsigned)-1)
387
1
    return false;
388
135
389
135
  const RelTy &rel = rels[firstRelI];
390
135
  Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
391
135
392
135
  // FDEs for garbage-collected or merged-by-ICF sections, or sections in
393
135
  // another partition, are dead.
394
135
  if (auto *d = dyn_cast<Defined>(&b))
395
133
    if (SectionBase *sec = d->section)
396
133
      return sec->partition == partition;
397
2
  return false;
398
2
}
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
377
4
bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
378
4
  auto *sec = cast<EhInputSection>(fde.sec);
379
4
  unsigned firstRelI = fde.firstRelocation;
380
4
381
4
  // An FDE should point to some function because FDEs are to describe
382
4
  // functions. That's however not always the case due to an issue of
383
4
  // ld.gold with -r. ld.gold may discard only functions and leave their
384
4
  // corresponding FDEs, which results in creating bad .eh_frame sections.
385
4
  // To deal with that, we ignore such FDEs.
386
4
  if (firstRelI == (unsigned)-1)
387
0
    return false;
388
4
389
4
  const RelTy &rel = rels[firstRelI];
390
4
  Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
391
4
392
4
  // FDEs for garbage-collected or merged-by-ICF sections, or sections in
393
4
  // another partition, are dead.
394
4
  if (auto *d = dyn_cast<Defined>(&b))
395
4
    if (SectionBase *sec = d->section)
396
4
      return sec->partition == partition;
397
0
  return false;
398
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> >)
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>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
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
377
128
bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
378
128
  auto *sec = cast<EhInputSection>(fde.sec);
379
128
  unsigned firstRelI = fde.firstRelocation;
380
128
381
128
  // An FDE should point to some function because FDEs are to describe
382
128
  // functions. That's however not always the case due to an issue of
383
128
  // ld.gold with -r. ld.gold may discard only functions and leave their
384
128
  // corresponding FDEs, which results in creating bad .eh_frame sections.
385
128
  // To deal with that, we ignore such FDEs.
386
128
  if (firstRelI == (unsigned)-1)
387
0
    return false;
388
128
389
128
  const RelTy &rel = rels[firstRelI];
390
128
  Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
391
128
392
128
  // FDEs for garbage-collected or merged-by-ICF sections, or sections in
393
128
  // another partition, are dead.
394
128
  if (auto *d = dyn_cast<Defined>(&b))
395
126
    if (SectionBase *sec = d->section)
396
126
      return sec->partition == partition;
397
2
  return false;
398
2
}
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
377
1
bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
378
1
  auto *sec = cast<EhInputSection>(fde.sec);
379
1
  unsigned firstRelI = fde.firstRelocation;
380
1
381
1
  // An FDE should point to some function because FDEs are to describe
382
1
  // functions. That's however not always the case due to an issue of
383
1
  // ld.gold with -r. ld.gold may discard only functions and leave their
384
1
  // corresponding FDEs, which results in creating bad .eh_frame sections.
385
1
  // To deal with that, we ignore such FDEs.
386
1
  if (firstRelI == (unsigned)-1)
387
1
    return false;
388
0
389
0
  const RelTy &rel = rels[firstRelI];
390
0
  Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
391
0
392
0
  // FDEs for garbage-collected or merged-by-ICF sections, or sections in
393
0
  // another partition, are dead.
394
0
  if (auto *d = dyn_cast<Defined>(&b))
395
0
    if (SectionBase *sec = d->section)
396
0
      return sec->partition == partition;
397
0
  return false;
398
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
377
3
bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
378
3
  auto *sec = cast<EhInputSection>(fde.sec);
379
3
  unsigned firstRelI = fde.firstRelocation;
380
3
381
3
  // An FDE should point to some function because FDEs are to describe
382
3
  // functions. That's however not always the case due to an issue of
383
3
  // ld.gold with -r. ld.gold may discard only functions and leave their
384
3
  // corresponding FDEs, which results in creating bad .eh_frame sections.
385
3
  // To deal with that, we ignore such FDEs.
386
3
  if (firstRelI == (unsigned)-1)
387
0
    return false;
388
3
389
3
  const RelTy &rel = rels[firstRelI];
390
3
  Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
391
3
392
3
  // FDEs for garbage-collected or merged-by-ICF sections, or sections in
393
3
  // another partition, are dead.
394
3
  if (auto *d = dyn_cast<Defined>(&b))
395
3
    if (SectionBase *sec = d->section)
396
3
      return sec->partition == partition;
397
0
  return false;
398
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> >)
399
400
// .eh_frame is a sequence of CIE or FDE records. In general, there
401
// is one CIE record per input object file which is followed by
402
// a list of FDEs. This function searches an existing CIE or create a new
403
// one and associates FDEs to the CIE.
404
template <class ELFT, class RelTy>
405
114
void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) {
406
114
  offsetToCie.clear();
407
251
  for (EhSectionPiece &piece : sec->pieces) {
408
251
    // The empty record is the end marker.
409
251
    if (piece.size == 4)
410
7
      return;
411
244
412
244
    size_t offset = piece.inputOff;
413
244
    uint32_t id = read32(piece.data().data() + 4);
414
244
    if (id == 0) {
415
108
      offsetToCie[offset] = addCie<ELFT>(piece, rels);
416
108
      continue;
417
108
    }
418
136
419
136
    uint32_t cieOffset = offset + 4 - id;
420
136
    CieRecord *rec = offsetToCie[cieOffset];
421
136
    if (!rec)
422
0
      fatal(toString(sec) + ": invalid CIE reference");
423
136
424
136
    if (!isFdeLive<ELFT>(piece, rels))
425
11
      continue;
426
125
    rec->fdes.push_back(&piece);
427
125
    numFdes++;
428
125
  }
429
114
}
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
405
4
void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) {
406
4
  offsetToCie.clear();
407
8
  for (EhSectionPiece &piece : sec->pieces) {
408
8
    // The empty record is the end marker.
409
8
    if (piece.size == 4)
410
0
      return;
411
8
412
8
    size_t offset = piece.inputOff;
413
8
    uint32_t id = read32(piece.data().data() + 4);
414
8
    if (id == 0) {
415
4
      offsetToCie[offset] = addCie<ELFT>(piece, rels);
416
4
      continue;
417
4
    }
418
4
419
4
    uint32_t cieOffset = offset + 4 - id;
420
4
    CieRecord *rec = offsetToCie[cieOffset];
421
4
    if (!rec)
422
0
      fatal(toString(sec) + ": invalid CIE reference");
423
4
424
4
    if (!isFdeLive<ELFT>(piece, rels))
425
0
      continue;
426
4
    rec->fdes.push_back(&piece);
427
4
    numFdes++;
428
4
  }
429
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> >)
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>, false> >(lld::elf::EhInputSection*, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >)
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
405
93
void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) {
406
93
  offsetToCie.clear();
407
222
  for (EhSectionPiece &piece : sec->pieces) {
408
222
    // The empty record is the end marker.
409
222
    if (piece.size == 4)
410
1
      return;
411
221
412
221
    size_t offset = piece.inputOff;
413
221
    uint32_t id = read32(piece.data().data() + 4);
414
221
    if (id == 0) {
415
93
      offsetToCie[offset] = addCie<ELFT>(piece, rels);
416
93
      continue;
417
93
    }
418
128
419
128
    uint32_t cieOffset = offset + 4 - id;
420
128
    CieRecord *rec = offsetToCie[cieOffset];
421
128
    if (!rec)
422
0
      fatal(toString(sec) + ": invalid CIE reference");
423
128
424
128
    if (!isFdeLive<ELFT>(piece, rels))
425
10
      continue;
426
118
    rec->fdes.push_back(&piece);
427
118
    numFdes++;
428
118
  }
429
93
}
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
405
13
void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) {
406
13
  offsetToCie.clear();
407
14
  for (EhSectionPiece &piece : sec->pieces) {
408
14
    // The empty record is the end marker.
409
14
    if (piece.size == 4)
410
6
      return;
411
8
412
8
    size_t offset = piece.inputOff;
413
8
    uint32_t id = read32(piece.data().data() + 4);
414
8
    if (id == 0) {
415
7
      offsetToCie[offset] = addCie<ELFT>(piece, rels);
416
7
      continue;
417
7
    }
418
1
419
1
    uint32_t cieOffset = offset + 4 - id;
420
1
    CieRecord *rec = offsetToCie[cieOffset];
421
1
    if (!rec)
422
0
      fatal(toString(sec) + ": invalid CIE reference");
423
1
424
1
    if (!isFdeLive<ELFT>(piece, rels))
425
1
      continue;
426
0
    rec->fdes.push_back(&piece);
427
0
    numFdes++;
428
0
  }
429
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
405
3
void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) {
406
3
  offsetToCie.clear();
407
6
  for (EhSectionPiece &piece : sec->pieces) {
408
6
    // The empty record is the end marker.
409
6
    if (piece.size == 4)
410
0
      return;
411
6
412
6
    size_t offset = piece.inputOff;
413
6
    uint32_t id = read32(piece.data().data() + 4);
414
6
    if (id == 0) {
415
3
      offsetToCie[offset] = addCie<ELFT>(piece, rels);
416
3
      continue;
417
3
    }
418
3
419
3
    uint32_t cieOffset = offset + 4 - id;
420
3
    CieRecord *rec = offsetToCie[cieOffset];
421
3
    if (!rec)
422
0
      fatal(toString(sec) + ": invalid CIE reference");
423
3
424
3
    if (!isFdeLive<ELFT>(piece, rels))
425
0
      continue;
426
3
    rec->fdes.push_back(&piece);
427
3
    numFdes++;
428
3
  }
429
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
405
1
void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) {
406
1
  offsetToCie.clear();
407
1
  for (EhSectionPiece &piece : sec->pieces) {
408
1
    // The empty record is the end marker.
409
1
    if (piece.size == 4)
410
0
      return;
411
1
412
1
    size_t offset = piece.inputOff;
413
1
    uint32_t id = read32(piece.data().data() + 4);
414
1
    if (id == 0) {
415
1
      offsetToCie[offset] = addCie<ELFT>(piece, rels);
416
1
      continue;
417
1
    }
418
0
419
0
    uint32_t cieOffset = offset + 4 - id;
420
0
    CieRecord *rec = offsetToCie[cieOffset];
421
0
    if (!rec)
422
0
      fatal(toString(sec) + ": invalid CIE reference");
423
0
424
0
    if (!isFdeLive<ELFT>(piece, rels))
425
0
      continue;
426
0
    rec->fdes.push_back(&piece);
427
0
    numFdes++;
428
0
  }
429
1
}
430
431
117
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *c) {
432
117
  auto *sec = cast<EhInputSection>(c);
433
117
  sec->parent = this;
434
117
435
117
  alignment = std::max(alignment, sec->alignment);
436
117
  sections.push_back(sec);
437
117
438
117
  for (auto *ds : sec->dependentSections)
439
3
    dependentSections.push_back(ds);
440
117
441
117
  if (sec->pieces.empty())
442
3
    return;
443
114
444
114
  if (sec->areRelocsRela)
445
96
    addSectionAux<ELFT>(sec, sec->template relas<ELFT>());
446
18
  else
447
18
    addSectionAux<ELFT>(sec, sec->template rels<ELFT>());
448
114
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
431
4
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *c) {
432
4
  auto *sec = cast<EhInputSection>(c);
433
4
  sec->parent = this;
434
4
435
4
  alignment = std::max(alignment, sec->alignment);
436
4
  sections.push_back(sec);
437
4
438
4
  for (auto *ds : sec->dependentSections)
439
0
    dependentSections.push_back(ds);
440
4
441
4
  if (sec->pieces.empty())
442
0
    return;
443
4
444
4
  if (sec->areRelocsRela)
445
0
    addSectionAux<ELFT>(sec, sec->template relas<ELFT>());
446
4
  else
447
4
    addSectionAux<ELFT>(sec, sec->template rels<ELFT>());
448
4
}
Unexecuted instantiation: void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::InputSectionBase*)
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
431
109
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *c) {
432
109
  auto *sec = cast<EhInputSection>(c);
433
109
  sec->parent = this;
434
109
435
109
  alignment = std::max(alignment, sec->alignment);
436
109
  sections.push_back(sec);
437
109
438
109
  for (auto *ds : sec->dependentSections)
439
3
    dependentSections.push_back(ds);
440
109
441
109
  if (sec->pieces.empty())
442
3
    return;
443
106
444
106
  if (sec->areRelocsRela)
445
93
    addSectionAux<ELFT>(sec, sec->template relas<ELFT>());
446
13
  else
447
13
    addSectionAux<ELFT>(sec, sec->template rels<ELFT>());
448
106
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
431
4
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *c) {
432
4
  auto *sec = cast<EhInputSection>(c);
433
4
  sec->parent = this;
434
4
435
4
  alignment = std::max(alignment, sec->alignment);
436
4
  sections.push_back(sec);
437
4
438
4
  for (auto *ds : sec->dependentSections)
439
0
    dependentSections.push_back(ds);
440
4
441
4
  if (sec->pieces.empty())
442
0
    return;
443
4
444
4
  if (sec->areRelocsRela)
445
3
    addSectionAux<ELFT>(sec, sec->template relas<ELFT>());
446
1
  else
447
1
    addSectionAux<ELFT>(sec, sec->template rels<ELFT>());
448
4
}
449
450
210
static void writeCieFde(uint8_t *buf, ArrayRef<uint8_t> d) {
451
210
  memcpy(buf, d.data(), d.size());
452
210
453
210
  size_t aligned = alignTo(d.size(), config->wordsize);
454
210
455
210
  // Zero-clear trailing padding if it exists.
456
210
  memset(buf + d.size(), 0, aligned - d.size());
457
210
458
210
  // Fix the size field. -4 since size does not include the size field itself.
459
210
  write32(buf, aligned - 4);
460
210
}
461
462
100
void EhFrameSection::finalizeContents() {
463
100
  assert(!this->size); // Not finalized.
464
100
  size_t off = 0;
465
100
  for (CieRecord *rec : cieRecords) {
466
93
    rec->cie->outputOff = off;
467
93
    off += alignTo(rec->cie->size, config->wordsize);
468
93
469
123
    for (EhSectionPiece *fde : rec->fdes) {
470
123
      fde->outputOff = off;
471
123
      off += alignTo(fde->size, config->wordsize);
472
123
    }
473
93
  }
474
100
475
100
  // The LSB standard does not allow a .eh_frame section with zero
476
100
  // Call Frame Information records. glibc unwind-dw2-fde.c
477
100
  // classify_object_over_fdes expects there is a CIE record length 0 as a
478
100
  // terminator. Thus we add one unconditionally.
479
100
  off += 4;
480
100
481
100
  this->size = off;
482
100
}
483
484
// Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table
485
// to get an FDE from an address to which FDE is applied. This function
486
// returns a list of such pairs.
487
30
std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const {
488
30
  uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;
489
30
  std::vector<FdeData> ret;
490
30
491
30
  uint64_t va = getPartition().ehFrameHdr->getVA();
492
30
  for (CieRecord *rec : cieRecords) {
493
30
    uint8_t enc = getFdeEncoding(rec->cie);
494
30
    for (EhSectionPiece *fde : rec->fdes) {
495
26
      uint64_t pc = getFdePc(buf, fde->outputOff, enc);
496
26
      uint64_t fdeVA = getParent()->addr + fde->outputOff;
497
26
      if (!isInt<32>(pc - va))
498
0
        fatal(toString(fde->sec) + ": PC offset is too large: 0x" +
499
0
              Twine::utohexstr(pc - va));
500
26
      ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)});
501
26
    }
502
30
  }
503
30
504
30
  // Sort the FDE list by their PC and uniqueify. Usually there is only
505
30
  // one FDE for a PC (i.e. function), but if ICF merges two functions
506
30
  // into one, there can be more than one FDEs pointing to the address.
507
30
  auto less = [](const FdeData &a, const FdeData &b) {
508
4
    return a.pcRel < b.pcRel;
509
4
  };
510
30
  llvm::stable_sort(ret, less);
511
30
  auto eq = [](const FdeData &a, const FdeData &b) {
512
4
    return a.pcRel == b.pcRel;
513
4
  };
514
30
  ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end());
515
30
516
30
  return ret;
517
30
}
518
519
26
static uint64_t readFdeAddr(uint8_t *buf, int size) {
520
26
  switch (size) {
521
26
  case DW_EH_PE_udata2:
522
1
    return read16(buf);
523
26
  case DW_EH_PE_sdata2:
524
1
    return (int16_t)read16(buf);
525
26
  case DW_EH_PE_udata4:
526
0
    return read32(buf);
527
26
  case DW_EH_PE_sdata4:
528
19
    return (int32_t)read32(buf);
529
26
  case DW_EH_PE_udata8:
530
3
  case DW_EH_PE_sdata8:
531
3
    return read64(buf);
532
3
  case DW_EH_PE_absptr:
533
2
    return readUint(buf);
534
0
  }
535
0
  fatal("unknown FDE size encoding");
536
0
}
537
538
// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
539
// We need it to create .eh_frame_hdr section.
540
uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff,
541
26
                                  uint8_t enc) const {
542
26
  // The starting address to which this FDE applies is
543
26
  // stored at FDE + 8 byte.
544
26
  size_t off = fdeOff + 8;
545
26
  uint64_t addr = readFdeAddr(buf + off, enc & 0xf);
546
26
  if ((enc & 0x70) == DW_EH_PE_absptr)
547
5
    return addr;
548
21
  if ((enc & 0x70) == DW_EH_PE_pcrel)
549
21
    return addr + getParent()->addr + off;
550
0
  fatal("unknown FDE size relative encoding");
551
0
}
552
553
97
void EhFrameSection::writeTo(uint8_t *buf) {
554
97
  // Write CIE and FDE records.
555
97
  for (CieRecord *rec : cieRecords) {
556
90
    size_t cieOffset = rec->cie->outputOff;
557
90
    writeCieFde(buf + cieOffset, rec->cie->data());
558
90
559
120
    for (EhSectionPiece *fde : rec->fdes) {
560
120
      size_t off = fde->outputOff;
561
120
      writeCieFde(buf + off, fde->data());
562
120
563
120
      // FDE's second word should have the offset to an associated CIE.
564
120
      // Write it.
565
120
      write32(buf + off + 4, off + 4 - cieOffset);
566
120
    }
567
90
  }
568
97
569
97
  // Apply relocations. .eh_frame section contents are not contiguous
570
97
  // in the output buffer, but relocateAlloc() still works because
571
97
  // getOffset() takes care of discontiguous section pieces.
572
97
  for (EhInputSection *s : sections)
573
111
    s->relocateAlloc(buf, nullptr);
574
97
575
97
  if (getPartition().ehFrameHdr && 
getPartition().ehFrameHdr->getParent()30
)
576
30
    getPartition().ehFrameHdr->write();
577
97
}
578
579
GotSection::GotSection()
580
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,
581
2.55k
                       ".got") {
582
2.55k
  // If ElfSym::globalOffsetTable is relative to .got and is referenced,
583
2.55k
  // increase numEntries by the number of entries used to emit
584
2.55k
  // ElfSym::globalOffsetTable.
585
2.55k
  if (ElfSym::globalOffsetTable && 
!target->gotBaseSymInGotPlt118
)
586
87
    numEntries += target->gotHeaderEntriesNum;
587
2.55k
}
588
589
201
void GotSection::addEntry(Symbol &sym) {
590
201
  sym.gotIndex = numEntries;
591
201
  ++numEntries;
592
201
}
593
594
78
bool GotSection::addDynTlsEntry(Symbol &sym) {
595
78
  if (sym.globalDynIndex != -1U)
596
24
    return false;
597
54
  sym.globalDynIndex = numEntries;
598
54
  // Global Dynamic TLS entries take two GOT slots.
599
54
  numEntries += 2;
600
54
  return true;
601
54
}
602
603
// Reserves TLS entries for a TLS module ID and a TLS block offset.
604
// In total it takes two GOT slots.
605
31
bool GotSection::addTlsIndex() {
606
31
  if (tlsIndexOff != uint32_t(-1))
607
19
    return false;
608
12
  tlsIndexOff = numEntries * config->wordsize;
609
12
  numEntries += 2;
610
12
  return true;
611
12
}
612
613
74
uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const {
614
74
  return this->getVA() + b.globalDynIndex * config->wordsize;
615
74
}
616
617
64
uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const {
618
64
  return b.globalDynIndex * config->wordsize;
619
64
}
620
621
246
void GotSection::finalizeContents() {
622
246
  size = numEntries * config->wordsize;
623
246
}
624
625
4.84k
bool GotSection::isNeeded() const {
626
4.84k
  // We need to emit a GOT even if it's empty if there's a relocation that is
627
4.84k
  // relative to GOT(such as GOTOFFREL).
628
4.84k
  return numEntries || 
hasGotOffRel4.37k
;
629
4.84k
}
630
631
245
void GotSection::writeTo(uint8_t *buf) {
632
245
  // Buf points to the start of this section's buffer,
633
245
  // whereas InputSectionBase::relocateAlloc() expects its argument
634
245
  // to point to the start of the output section.
635
245
  target->writeGotHeader(buf);
636
245
  relocateAlloc(buf - outSecOff, buf - outSecOff + size);
637
245
}
638
639
71
static uint64_t getMipsPageAddr(uint64_t addr) {
640
71
  return (addr + 0x8000) & ~0xffff;
641
71
}
642
643
14
static uint64_t getMipsPageCount(uint64_t size) {
644
14
  return (size + 0xfffe) / 0xffff + 1;
645
14
}
646
647
MipsGotSection::MipsGotSection()
648
    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
649
198
                       ".got") {}
650
651
void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend,
652
10.1k
                              RelExpr expr) {
653
10.1k
  FileGot &g = getGot(file);
654
10.1k
  if (expr == R_MIPS_GOT_LOCAL_PAGE) {
655
29
    if (const OutputSection *os = sym.getOutputSection())
656
25
      g.pagesMap.insert({os, {}});
657
4
    else
658
4
      g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(addend))}, 0});
659
10.0k
  } else if (sym.isTls())
660
13
    g.tls.insert({&sym, 0});
661
10.0k
  else if (sym.isPreemptible && 
expr == R_ABS41
)
662
4
    g.relocs.insert({&sym, 0});
663
10.0k
  else if (sym.isPreemptible)
664
37
    g.global.insert({&sym, 0});
665
10.0k
  else if (expr == R_MIPS_GOT_OFF32)
666
12
    g.local32.insert({{&sym, addend}, 0});
667
10.0k
  else
668
10.0k
    g.local16.insert({{&sym, addend}, 0});
669
10.1k
}
670
671
12
void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) {
672
12
  getGot(file).dynTlsSymbols.insert({&sym, 0});
673
12
}
674
675
6
void MipsGotSection::addTlsIndex(InputFile &file) {
676
6
  getGot(file).dynTlsSymbols.insert({nullptr, 0});
677
6
}
678
679
114
size_t MipsGotSection::FileGot::getEntriesNum() const {
680
114
  return getPageEntriesNum() + local16.size() + global.size() + relocs.size() +
681
114
         tls.size() + dynTlsSymbols.size() * 2;
682
114
}
683
684
218
size_t MipsGotSection::FileGot::getPageEntriesNum() const {
685
218
  size_t num = 0;
686
218
  for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap)
687
52
    num += p.second.count;
688
218
  return num;
689
218
}
690
691
59
size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
692
59
  size_t count = getPageEntriesNum() + local16.size() + global.size();
693
59
  // If there are relocation-only entries in the GOT, TLS entries
694
59
  // are allocated after them. TLS entries should be addressable
695
59
  // by 16-bit index so count both reloc-only and TLS entries.
696
59
  if (!tls.empty() || 
!dynTlsSymbols.empty()50
)
697
10
    count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2;
698
59
  return count;
699
59
}
700
701
10.1k
MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) {
702
10.1k
  if (!f.mipsGotIndex.hasValue()) {
703
58
    gots.emplace_back();
704
58
    gots.back().file = &f;
705
58
    f.mipsGotIndex = gots.size() - 1;
706
58
  }
707
10.1k
  return gots[*f.mipsGotIndex];
708
10.1k
}
709
710
uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f,
711
                                            const Symbol &sym,
712
29
                                            int64_t addend) const {
713
29
  const FileGot &g = gots[*f->mipsGotIndex];
714
29
  uint64_t index = 0;
715
29
  if (const OutputSection *outSec = sym.getOutputSection()) {
716
25
    uint64_t secAddr = getMipsPageAddr(outSec->addr);
717
25
    uint64_t symAddr = getMipsPageAddr(sym.getVA(addend));
718
25
    index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff;
719
25
  } else {
720
4
    index = g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(addend))});
721
4
  }
722
29
  return index * config->wordsize;
723
29
}
724
725
uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s,
726
10.0k
                                           int64_t addend) const {
727
10.0k
  const FileGot &g = gots[*f->mipsGotIndex];
728
10.0k
  Symbol *sym = const_cast<Symbol *>(&s);
729
10.0k
  if (sym->isTls())
730
13
    return g.tls.lookup(sym) * config->wordsize;
731
10.0k
  if (sym->isPreemptible)
732
37
    return g.global.lookup(sym) * config->wordsize;
733
10.0k
  return g.local16.lookup({sym, addend}) * config->wordsize;
734
10.0k
}
735
736
6
uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const {
737
6
  const FileGot &g = gots[*f->mipsGotIndex];
738
6
  return g.dynTlsSymbols.lookup(nullptr) * config->wordsize;
739
6
}
740
741
uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f,
742
12
                                            const Symbol &s) const {
743
12
  const FileGot &g = gots[*f->mipsGotIndex];
744
12
  Symbol *sym = const_cast<Symbol *>(&s);
745
12
  return g.dynTlsSymbols.lookup(sym) * config->wordsize;
746
12
}
747
748
100
const Symbol *MipsGotSection::getFirstGlobalEntry() const {
749
100
  if (gots.empty())
750
55
    return nullptr;
751
45
  const FileGot &primGot = gots.front();
752
45
  if (!primGot.global.empty())
753
27
    return primGot.global.front().first;
754
18
  if (!primGot.relocs.empty())
755
5
    return primGot.relocs.front().first;
756
13
  return nullptr;
757
13
}
758
759
100
unsigned MipsGotSection::getLocalEntriesNum() const {
760
100
  if (gots.empty())
761
55
    return headerEntriesNum;
762
45
  return headerEntriesNum + gots.front().getPageEntriesNum() +
763
45
         gots.front().local16.size();
764
45
}
765
766
59
bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) {
767
59
  FileGot tmp = dst;
768
59
  set_union(tmp.pagesMap, src.pagesMap);
769
59
  set_union(tmp.local16, src.local16);
770
59
  set_union(tmp.global, src.global);
771
59
  set_union(tmp.relocs, src.relocs);
772
59
  set_union(tmp.tls, src.tls);
773
59
  set_union(tmp.dynTlsSymbols, src.dynTlsSymbols);
774
59
775
59
  size_t count = isPrimary ? 
headerEntriesNum58
:
01
;
776
59
  count += tmp.getIndexedEntriesNum();
777
59
778
59
  if (count * config->wordsize > config->mipsGotSize)
779
4
    return false;
780
55
781
55
  std::swap(tmp, dst);
782
55
  return true;
783
55
}
784
785
173
void MipsGotSection::finalizeContents() { updateAllocSize(); }
786
787
375
bool MipsGotSection::updateAllocSize() {
788
375
  size = headerEntriesNum * config->wordsize;
789
375
  for (const FileGot &g : gots)
790
114
    size += g.getEntriesNum() * config->wordsize;
791
375
  return false;
792
375
}
793
794
187
void MipsGotSection::build() {
795
187
  if (gots.empty())
796
133
    return;
797
54
798
54
  std::vector<FileGot> mergedGots(1);
799
54
800
54
  // For each GOT move non-preemptible symbols from the `Global`
801
54
  // to `Local16` list. Preemptible symbol might become non-preemptible
802
54
  // one if, for example, it gets a related copy relocation.
803
58
  for (FileGot &got : gots) {
804
58
    for (auto &p: got.global)
805
35
      if (!p.first->isPreemptible)
806
0
        got.local16.insert({{p.first, 0}, 0});
807
58
    got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) {
808
35
      return !p.first->isPreemptible;
809
35
    });
810
58
  }
811
54
812
54
  // For each GOT remove "reloc-only" entry if there is "global"
813
54
  // entry for the same symbol. And add local entries which indexed
814
54
  // using 32-bit value at the end of 16-bit entries.
815
58
  for (FileGot &got : gots) {
816
58
    got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
817
4
      return got.global.count(p.first);
818
4
    });
819
58
    set_union(got.local16, got.local32);
820
58
    got.local32.clear();
821
58
  }
822
54
823
54
  // Evaluate number of "reloc-only" entries in the resulting GOT.
824
54
  // To do that put all unique "reloc-only" and "global" entries
825
54
  // from all GOTs to the future primary GOT.
826
54
  FileGot *primGot = &mergedGots.front();
827
58
  for (FileGot &got : gots) {
828
58
    set_union(primGot->relocs, got.global);
829
58
    set_union(primGot->relocs, got.relocs);
830
58
    got.relocs.clear();
831
58
  }
832
54
833
54
  // Evaluate number of "page" entries in each GOT.
834
58
  for (FileGot &got : gots) {
835
58
    for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
836
58
         got.pagesMap) {
837
14
      const OutputSection *os = p.first;
838
14
      uint64_t secSize = 0;
839
16
      for (BaseCommand *cmd : os->sectionCommands) {
840
16
        if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
841
24
          
for (InputSection *isec : isd->sections)15
{
842
24
            uint64_t off = alignTo(secSize, isec->alignment);
843
24
            secSize = off + isec->getSize();
844
24
          }
845
16
      }
846
14
      p.second.count = getMipsPageCount(secSize);
847
14
    }
848
58
  }
849
54
850
54
  // Merge GOTs. Try to join as much as possible GOTs but do not exceed
851
54
  // maximum GOT size. At first, try to fill the primary GOT because
852
54
  // the primary GOT can be accessed in the most effective way. If it
853
54
  // is not possible, try to fill the last GOT in the list, and finally
854
54
  // create a new GOT if both attempts failed.
855
58
  for (FileGot &srcGot : gots) {
856
58
    InputFile *file = srcGot.file;
857
58
    if (tryMergeGots(mergedGots.front(), srcGot, true)) {
858
54
      file->mipsGotIndex = 0;
859
54
    } else {
860
4
      // If this is the first time we failed to merge with the primary GOT,
861
4
      // MergedGots.back() will also be the primary GOT. We must make sure not
862
4
      // to try to merge again with isPrimary=false, as otherwise, if the
863
4
      // inputs are just right, we could allow the primary GOT to become 1 or 2
864
4
      // words bigger due to ignoring the header size.
865
4
      if (mergedGots.size() == 1 ||
866
4
          
!tryMergeGots(mergedGots.back(), srcGot, false)1
) {
867
3
        mergedGots.emplace_back();
868
3
        std::swap(mergedGots.back(), srcGot);
869
3
      }
870
4
      file->mipsGotIndex = mergedGots.size() - 1;
871
4
    }
872
58
  }
873
54
  std::swap(gots, mergedGots);
874
54
875
54
  // Reduce number of "reloc-only" entries in the primary GOT
876
54
  // by substracting "global" entries exist in the primary GOT.
877
54
  primGot = &gots.front();
878
54
  primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
879
38
    return primGot->global.count(p.first);
880
38
  });
881
54
882
54
  // Calculate indexes for each GOT entry.
883
54
  size_t index = headerEntriesNum;
884
57
  for (FileGot &got : gots) {
885
57
    got.startIndex = &got == primGot ? 
054
:
index3
;
886
57
    for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
887
57
         got.pagesMap) {
888
13
      // For each output section referenced by GOT page relocations calculate
889
13
      // and save into pagesMap an upper bound of MIPS GOT entries required
890
13
      // to store page addresses of local symbols. We assume the worst case -
891
13
      // each 64kb page of the output section has at least one GOT relocation
892
13
      // against it. And take in account the case when the section intersects
893
13
      // page boundaries.
894
13
      p.second.firstIndex = index;
895
13
      index += p.second.count;
896
13
    }
897
57
    for (auto &p: got.local16)
898
10.0k
      p.second = index++;
899
57
    for (auto &p: got.global)
900
34
      p.second = index++;
901
57
    for (auto &p: got.relocs)
902
6
      p.second = index++;
903
57
    for (auto &p: got.tls)
904
12
      p.second = index++;
905
57
    for (auto &p: got.dynTlsSymbols) {
906
17
      p.second = index;
907
17
      index += 2;
908
17
    }
909
57
  }
910
54
911
54
  // Update Symbol::gotIndex field to use this
912
54
  // value later in the `sortMipsSymbols` function.
913
54
  for (auto &p : primGot->global)
914
32
    p.first->gotIndex = p.second;
915
54
  for (auto &p : primGot->relocs)
916
6
    p.first->gotIndex = p.second;
917
54
918
54
  // Create dynamic relocations.
919
57
  for (FileGot &got : gots) {
920
57
    // Create dynamic relocations for TLS entries.
921
57
    for (std::pair<Symbol *, size_t> &p : got.tls) {
922
12
      Symbol *s = p.first;
923
12
      uint64_t offset = p.second * config->wordsize;
924
12
      if (s->isPreemptible)
925
9
        mainPart->relaDyn->addReloc(target->tlsGotRel, this, offset, s);
926
12
    }
927
57
    for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
928
17
      Symbol *s = p.first;
929
17
      uint64_t offset = p.second * config->wordsize;
930
17
      if (s == nullptr) {
931
6
        if (!config->isPic)
932
3
          continue;
933
3
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s);
934
11
      } else {
935
11
        // When building a shared library we still need a dynamic relocation
936
11
        // for the module index. Therefore only checking for
937
11
        // S->isPreemptible is not sufficient (this happens e.g. for
938
11
        // thread-locals that have been marked as local through a linker script)
939
11
        if (!s->isPreemptible && 
!config->isPic4
)
940
3
          continue;
941
8
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s);
942
8
        // However, we can skip writing the TLS offset reloc for non-preemptible
943
8
        // symbols since it is known even in shared libraries
944
8
        if (!s->isPreemptible)
945
1
          continue;
946
7
        offset += config->wordsize;
947
7
        mainPart->relaDyn->addReloc(target->tlsOffsetRel, this, offset, s);
948
7
      }
949
17
    }
950
57
951
57
    // Do not create dynamic relocations for non-TLS
952
57
    // entries in the primary GOT.
953
57
    if (&got == primGot)
954
54
      continue;
955
3
956
3
    // Dynamic relocations for "global" entries.
957
3
    for (const std::pair<Symbol *, size_t> &p : got.global) {
958
2
      uint64_t offset = p.second * config->wordsize;
959
2
      mainPart->relaDyn->addReloc(target->relativeRel, this, offset, p.first);
960
2
    }
961
3
    if (!config->isPic)
962
1
      continue;
963
2
    // Dynamic relocations for "local" entries in case of PIC.
964
2
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
965
2
         got.pagesMap) {
966
1
      size_t pageCount = l.second.count;
967
7
      for (size_t pi = 0; pi < pageCount; 
++pi6
) {
968
6
        uint64_t offset = (l.second.firstIndex + pi) * config->wordsize;
969
6
        mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first,
970
6
                                 int64_t(pi * 0x10000)});
971
6
      }
972
1
    }
973
2
    for (const std::pair<GotEntry, size_t> &p : got.local16) {
974
1
      uint64_t offset = p.second * config->wordsize;
975
1
      mainPart->relaDyn->addReloc({target->relativeRel, this, offset, true,
976
1
                               p.first.first, p.first.second});
977
1
    }
978
2
  }
979
54
}
980
981
374
bool MipsGotSection::isNeeded() const {
982
374
  // We add the .got section to the result for dynamic MIPS target because
983
374
  // its address and properties are mentioned in the .dynamic section.
984
374
  return !config->relocatable;
985
374
}
986
987
10.3k
uint64_t MipsGotSection::getGp(const InputFile *f) const {
988
10.3k
  // For files without related GOT or files refer a primary GOT
989
10.3k
  // returns "common" _gp value. For secondary GOTs calculate
990
10.3k
  // individual _gp values.
991
10.3k
  if (!f || 
!f->mipsGotIndex.hasValue()10.1k
||
*f->mipsGotIndex == 010.1k
)
992
338
    return ElfSym::mipsGp->getVA(0);
993
10.0k
  return getVA() + gots[*f->mipsGotIndex].startIndex * config->wordsize +
994
10.0k
         0x7ff0;
995
10.0k
}
996
997
173
void MipsGotSection::writeTo(uint8_t *buf) {
998
173
  // Set the MSB of the second GOT slot. This is not required by any
999
173
  // MIPS ABI documentation, though.
1000
173
  //
1001
173
  // There is a comment in glibc saying that "The MSB of got[1] of a
1002
173
  // gnu object is set to identify gnu objects," and in GNU gold it
1003
173
  // says "the second entry will be used by some runtime loaders".
1004
173
  // But how this field is being used is unclear.
1005
173
  //
1006
173
  // We are not really willing to mimic other linkers behaviors
1007
173
  // without understanding why they do that, but because all files
1008
173
  // generated by GNU tools have this special GOT value, and because
1009
173
  // we've been doing this for years, it is probably a safe bet to
1010
173
  // keep doing this for now. We really need to revisit this to see
1011
173
  // if we had to do this.
1012
173
  writeUint(buf + config->wordsize, (uint64_t)1 << (config->wordsize * 8 - 1));
1013
173
  for (const FileGot &g : gots) {
1014
10.1k
    auto write = [&](size_t i, const Symbol *s, int64_t a) {
1015
10.1k
      uint64_t va = a;
1016
10.1k
      if (s)
1017
10.0k
        va = s->getVA(a);
1018
10.1k
      writeUint(buf + i * config->wordsize, va);
1019
10.1k
    };
1020
57
    // Write 'page address' entries to the local part of the GOT.
1021
57
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
1022
57
         g.pagesMap) {
1023
13
      size_t pageCount = l.second.count;
1024
13
      uint64_t firstPageAddr = getMipsPageAddr(l.first->addr);
1025
49
      for (size_t pi = 0; pi < pageCount; 
++pi36
)
1026
36
        write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000);
1027
13
    }
1028
57
    // Local, global, TLS, reloc-only  entries.
1029
57
    // If TLS entry has a corresponding dynamic relocations, leave it
1030
57
    // initialized by zero. Write down adjusted TLS symbol's values otherwise.
1031
57
    // To calculate the adjustments use offsets for thread-local storage.
1032
57
    // https://www.linux-mips.org/wiki/NPTL
1033
57
    for (const std::pair<GotEntry, size_t> &p : g.local16)
1034
10.0k
      write(p.second, p.first.first, p.first.second);
1035
57
    // Write VA to the primary GOT only. For secondary GOTs that
1036
57
    // will be done by REL32 dynamic relocations.
1037
57
    if (&g == &gots.front())
1038
54
      for (const std::pair<const Symbol *, size_t> &p : g.global)
1039
32
        write(p.second, p.first, 0);
1040
57
    for (const std::pair<Symbol *, size_t> &p : g.relocs)
1041
6
      write(p.second, p.first, 0);
1042
57
    for (const std::pair<Symbol *, size_t> &p : g.tls)
1043
12
      write(p.second, p.first, p.first->isPreemptible ? 
09
:
-0x70003
);
1044
57
    for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) {
1045
17
      if (p.first == nullptr && 
!config->isPic6
)
1046
3
        write(p.second, nullptr, 1);
1047
14
      else if (p.first && 
!p.first->isPreemptible11
) {
1048
4
        // If we are emitting PIC code with relocations we mustn't write
1049
4
        // anything to the GOT here. When using Elf_Rel relocations the value
1050
4
        // one will be treated as an addend and will cause crashes at runtime
1051
4
        if (!config->isPic)
1052
3
          write(p.second, nullptr, 1);
1053
4
        write(p.second + 1, p.first, -0x8000);
1054
4
      }
1055
17
    }
1056
57
  }
1057
173
}
1058
1059
// On PowerPC the .plt section is used to hold the table of function addresses
1060
// instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
1061
// section. I don't know why we have a BSS style type for the section but it is
1062
// consitent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
1063
GotPltSection::GotPltSection()
1064
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,
1065
2.75k
                       ".got.plt") {
1066
2.75k
  if (config->emachine == EM_PPC) {
1067
31
    name = ".plt";
1068
2.72k
  } else if (config->emachine == EM_PPC64) {
1069
152
    type = SHT_NOBITS;
1070
152
    name = ".plt";
1071
152
  }
1072
2.75k
}
1073
1074
359
void GotPltSection::addEntry(Symbol &sym) {
1075
359
  assert(sym.pltIndex == entries.size());
1076
359
  entries.push_back(&sym);
1077
359
}
1078
1079
1.28k
size_t GotPltSection::getSize() const {
1080
1.28k
  return (target->gotPltHeaderEntriesNum + entries.size()) * config->wordsize;
1081
1.28k
}
1082
1083
276
void GotPltSection::writeTo(uint8_t *buf) {
1084
276
  target->writeGotPltHeader(buf);
1085
276
  buf += target->gotPltHeaderEntriesNum * config->wordsize;
1086
332
  for (const Symbol *b : entries) {
1087
332
    target->writeGotPlt(buf, *b);
1088
332
    buf += config->wordsize;
1089
332
  }
1090
276
}
1091
1092
5.22k
bool GotPltSection::isNeeded() const {
1093
5.22k
  // We need to emit GOTPLT even if it's empty if there's a relocation relative
1094
5.22k
  // to it.
1095
5.22k
  return !entries.empty() || 
hasGotPltOffRel4.70k
;
1096
5.22k
}
1097
1098
2.75k
static StringRef getIgotPltName() {
1099
2.75k
  // On ARM the IgotPltSection is part of the GotSection.
1100
2.75k
  if (config->emachine == EM_ARM)
1101
159
    return ".got";
1102
2.59k
1103
2.59k
  // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection
1104
2.59k
  // needs to be named the same.
1105
2.59k
  if (config->emachine == EM_PPC64)
1106
152
    return ".plt";
1107
2.44k
1108
2.44k
  return ".got.plt";
1109
2.44k
}
1110
1111
// On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1112
// with the IgotPltSection.
1113
IgotPltSection::IgotPltSection()
1114
    : SyntheticSection(SHF_ALLOC | SHF_WRITE,
1115
                       config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1116
2.75k
                       config->wordsize, getIgotPltName()) {}
1117
1118
62
void IgotPltSection::addEntry(Symbol &sym) {
1119
62
  assert(sym.pltIndex == entries.size());
1120
62
  entries.push_back(&sym);
1121
62
}
1122
1123
194
size_t IgotPltSection::getSize() const {
1124
194
  return entries.size() * config->wordsize;
1125
194
}
1126
1127
44
void IgotPltSection::writeTo(uint8_t *buf) {
1128
58
  for (const Symbol *b : entries) {
1129
58
    target->writeIgotPlt(buf, *b);
1130
58
    buf += config->wordsize;
1131
58
  }
1132
44
}
1133
1134
StringTableSection::StringTableSection(StringRef name, bool dynamic)
1135
    : SyntheticSection(dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, name),
1136
8.27k
      dynamic(dynamic) {
1137
8.27k
  // ELF string tables start with a NUL byte.
1138
8.27k
  addString("");
1139
8.27k
}
1140
1141
// Adds a string to the string table. If `hashIt` is true we hash and check for
1142
// duplicates. It is optional because the name of global symbols are already
1143
// uniqued and hashing them again has a big cost for a small value: uniquing
1144
// them with some other string that happens to be the same.
1145
450k
unsigned StringTableSection::addString(StringRef s, bool hashIt) {
1146
450k
  if (hashIt) {
1147
430k
    auto r = stringMap.insert(std::make_pair(s, this->size));
1148
430k
    if (!r.second)
1149
66.2k
      return r.first->second;
1150
384k
  }
1151
384k
  unsigned ret = this->size;
1152
384k
  this->size = this->size + s.size() + 1;
1153
384k
  strings.push_back(s);
1154
384k
  return ret;
1155
384k
}
1156
1157
6.39k
void StringTableSection::writeTo(uint8_t *buf) {
1158
381k
  for (StringRef s : strings) {
1159
381k
    memcpy(buf, s.data(), s.size());
1160
381k
    buf[s.size()] = '\0';
1161
381k
    buf += s.size() + 1;
1162
381k
  }
1163
6.39k
}
1164
1165
// Returns the number of version definition entries. Because the first entry
1166
// is for the version definition itself, it is the number of versioned symbols
1167
// plus one. Note that we don't support multiple versions yet.
1168
376
static unsigned getVerDefNum() { return config->versionDefinitions.size() + 1; }
1169
1170
template <class ELFT>
1171
DynamicSection<ELFT>::DynamicSection()
1172
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, config->wordsize,
1173
2.77k
                       ".dynamic") {
1174
2.77k
  this->entsize = ELFT::Is64Bits ? 
162.23k
:
8537
;
1175
2.77k
1176
2.77k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1177
2.77k
  // which passes -z rodynamic.
1178
2.77k
  // See "Special Section" in Chapter 4 in the following document:
1179
2.77k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1180
2.77k
  if (config->emachine == EM_MIPS || 
config->zRodynamic2.57k
)
1181
202
    this->flags = SHF_ALLOC;
1182
2.77k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::DynamicSection()
Line
Count
Source
1173
380
                       ".dynamic") {
1174
380
  this->entsize = ELFT::Is64Bits ? 
160
: 8;
1175
380
1176
380
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1177
380
  // which passes -z rodynamic.
1178
380
  // See "Special Section" in Chapter 4 in the following document:
1179
380
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1180
380
  if (config->emachine == EM_MIPS || 
config->zRodynamic355
)
1181
25
    this->flags = SHF_ALLOC;
1182
380
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::DynamicSection()
Line
Count
Source
1173
157
                       ".dynamic") {
1174
157
  this->entsize = ELFT::Is64Bits ? 
160
: 8;
1175
157
1176
157
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1177
157
  // which passes -z rodynamic.
1178
157
  // See "Special Section" in Chapter 4 in the following document:
1179
157
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1180
157
  if (config->emachine == EM_MIPS || 
config->zRodynamic31
)
1181
126
    this->flags = SHF_ALLOC;
1182
157
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::DynamicSection()
Line
Count
Source
1173
2.12k
                       ".dynamic") {
1174
2.12k
  this->entsize = ELFT::Is64Bits ? 16 : 
80
;
1175
2.12k
1176
2.12k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1177
2.12k
  // which passes -z rodynamic.
1178
2.12k
  // See "Special Section" in Chapter 4 in the following document:
1179
2.12k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1180
2.12k
  if (config->emachine == EM_MIPS || 
config->zRodynamic2.12k
)
1181
6
    this->flags = SHF_ALLOC;
1182
2.12k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::DynamicSection()
Line
Count
Source
1173
108
                       ".dynamic") {
1174
108
  this->entsize = ELFT::Is64Bits ? 16 : 
80
;
1175
108
1176
108
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1177
108
  // which passes -z rodynamic.
1178
108
  // See "Special Section" in Chapter 4 in the following document:
1179
108
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1180
108
  if (config->emachine == EM_MIPS || 
config->zRodynamic63
)
1181
45
    this->flags = SHF_ALLOC;
1182
108
}
1183
1184
template <class ELFT>
1185
116
void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) {
1186
116
  entries.push_back({tag, fn});
1187
116
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1185
8
void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) {
1186
8
  entries.push_back({tag, fn});
1187
8
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1185
85
void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) {
1186
85
  entries.push_back({tag, fn});
1187
85
}
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
1185
23
void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) {
1186
23
  entries.push_back({tag, fn});
1187
23
}
1188
1189
template <class ELFT>
1190
5.85k
void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) {
1191
5.85k
  entries.push_back({tag, [=] 
{ return val; }5.81k
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1191
833
  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
1191
694
  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
1191
3.96k
  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
1191
330
  entries.push_back({tag, [=] { return val; }});
1192
5.85k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInt(int, unsigned long long)
Line
Count
Source
1190
833
void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) {
1191
833
  entries.push_back({tag, [=] { return val; }});
1192
833
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInt(int, unsigned long long)
Line
Count
Source
1190
694
void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) {
1191
694
  entries.push_back({tag, [=] { return val; }});
1192
694
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInt(int, unsigned long long)
Line
Count
Source
1190
3.99k
void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) {
1191
3.99k
  entries.push_back({tag, [=] { return val; }});
1192
3.99k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInt(int, unsigned long long)
Line
Count
Source
1190
330
void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) {
1191
330
  entries.push_back({tag, [=] { return val; }});
1192
330
}
1193
1194
template <class ELFT>
1195
5.89k
void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) {
1196
5.89k
  entries.push_back({tag, [=] 
{ return sec->getVA(0); }5.85k
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1196
821
  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
1196
411
  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
1196
4.37k
  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
1196
245
  entries.push_back({tag, [=] { return sec->getVA(0); }});
1197
5.89k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1195
821
void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) {
1196
821
  entries.push_back({tag, [=] { return sec->getVA(0); }});
1197
821
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1195
411
void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) {
1196
411
  entries.push_back({tag, [=] { return sec->getVA(0); }});
1197
411
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1195
4.42k
void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) {
1196
4.42k
  entries.push_back({tag, [=] { return sec->getVA(0); }});
1197
4.42k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1195
245
void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) {
1196
245
  entries.push_back({tag, [=] { return sec->getVA(0); }});
1197
245
}
1198
1199
template <class ELFT>
1200
29
void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) {
1201
29
  size_t tagOffset = entries.size() * entsize;
1202
29
  entries.push_back(
1203
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
1203
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
1203
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
1203
6
      {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }});
1204
29
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1200
2
void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) {
1201
2
  size_t tagOffset = entries.size() * entsize;
1202
2
  entries.push_back(
1203
2
      {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }});
1204
2
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1200
21
void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) {
1201
21
  size_t tagOffset = entries.size() * entsize;
1202
21
  entries.push_back(
1203
21
      {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }});
1204
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
1200
6
void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) {
1201
6
  size_t tagOffset = entries.size() * entsize;
1202
6
  entries.push_back(
1203
6
      {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }});
1204
6
}
1205
1206
template <class ELFT>
1207
9
void DynamicSection<ELFT>::addOutSec(int32_t tag, OutputSection *sec) {
1208
9
  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
1208
9
  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
1209
9
}
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
1207
9
void DynamicSection<ELFT>::addOutSec(int32_t tag, OutputSection *sec) {
1208
9
  entries.push_back({tag, [=] { return sec->addr; }});
1209
9
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addOutSec(int, lld::elf::OutputSection*)
1210
1211
template <class ELFT>
1212
295
void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) {
1213
295
  entries.push_back({tag, [=] 
{ return sec->size; }292
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSize(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1213
63
  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
1213
16
  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
1213
194
  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
1213
19
  entries.push_back({tag, [=] { return sec->size; }});
1214
295
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1212
63
void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) {
1213
63
  entries.push_back({tag, [=] { return sec->size; }});
1214
63
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1212
16
void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) {
1213
16
  entries.push_back({tag, [=] { return sec->size; }});
1214
16
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1212
197
void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) {
1213
197
  entries.push_back({tag, [=] { return sec->size; }});
1214
197
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1212
19
void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) {
1213
19
  entries.push_back({tag, [=] { return sec->size; }});
1214
19
}
1215
1216
template <class ELFT>
1217
8
void DynamicSection<ELFT>::addSym(int32_t tag, Symbol *sym) {
1218
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
1218
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
1219
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
1217
8
void DynamicSection<ELFT>::addSym(int32_t tag, Symbol *sym) {
1218
8
  entries.push_back({tag, [=] { return sym->getVA(); }});
1219
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSym(int, lld::elf::Symbol*)
1220
1221
// A Linker script may assign the RELA relocation sections to the same
1222
// output section. When this occurs we cannot just use the OutputSection
1223
// Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
1224
// overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1225
269
static uint64_t addPltRelSz() {
1226
269
  size_t size = in.relaPlt->getSize();
1227
269
  if (in.relaIplt->getParent() == in.relaPlt->getParent() &&
1228
269
      
in.relaIplt->name == in.relaPlt->name249
)
1229
248
    size += in.relaIplt->getSize();
1230
269
  return size;
1231
269
}
1232
1233
// Add remaining entries to complete .dynamic contents.
1234
1.26k
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1235
1.26k
  elf::Partition &part = getPartition();
1236
1.26k
  bool isMain = part.name.empty();
1237
1.26k
1238
1.26k
  for (StringRef s : config->filterList)
1239
6
    addInt(DT_FILTER, part.dynStrTab->addString(s));
1240
1.26k
  for (StringRef s : config->auxiliaryList)
1241
4
    addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1242
1.26k
1243
1.26k
  if (!config->rpath.empty())
1244
7
    addInt(config->enableNewDtags ? 
DT_RUNPATH6
:
DT_RPATH1
,
1245
7
           part.dynStrTab->addString(config->rpath));
1246
1.26k
1247
1.26k
  for (SharedFile *file : sharedFiles)
1248
377
    if (file->isNeeded)
1249
360
      addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1250
1.26k
1251
1.26k
  if (isMain) {
1252
1.25k
    if (!config->soName.empty())
1253
46
      addInt(DT_SONAME, part.dynStrTab->addString(config->soName));
1254
1.25k
  } else {
1255
11
    if (!config->soName.empty())
1256
1
      addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));
1257
11
    addInt(DT_SONAME, part.dynStrTab->addString(part.name));
1258
11
  }
1259
1.26k
1260
1.26k
  // Set DT_FLAGS and DT_FLAGS_1.
1261
1.26k
  uint32_t dtFlags = 0;
1262
1.26k
  uint32_t dtFlags1 = 0;
1263
1.26k
  if (config->bsymbolic)
1264
8
    dtFlags |= DF_SYMBOLIC;
1265
1.26k
  if (config->zGlobal)
1266
1
    dtFlags1 |= DF_1_GLOBAL;
1267
1.26k
  if (config->zInitfirst)
1268
1
    dtFlags1 |= DF_1_INITFIRST;
1269
1.26k
  if (config->zInterpose)
1270
1
    dtFlags1 |= DF_1_INTERPOSE;
1271
1.26k
  if (config->zNodefaultlib)
1272
1
    dtFlags1 |= DF_1_NODEFLIB;
1273
1.26k
  if (config->zNodelete)
1274
1
    dtFlags1 |= DF_1_NODELETE;
1275
1.26k
  if (config->zNodlopen)
1276
1
    dtFlags1 |= DF_1_NOOPEN;
1277
1.26k
  if (config->zNow) {
1278
4
    dtFlags |= DF_BIND_NOW;
1279
4
    dtFlags1 |= DF_1_NOW;
1280
4
  }
1281
1.26k
  if (config->zOrigin) {
1282
1
    dtFlags |= DF_ORIGIN;
1283
1
    dtFlags1 |= DF_1_ORIGIN;
1284
1
  }
1285
1.26k
  if (!config->zText)
1286
11
    dtFlags |= DF_TEXTREL;
1287
1.26k
  if (config->hasStaticTlsModel)
1288
18
    dtFlags |= DF_STATIC_TLS;
1289
1.26k
1290
1.26k
  if (dtFlags)
1291
40
    addInt(DT_FLAGS, dtFlags);
1292
1.26k
  if (dtFlags1)
1293
4
    addInt(DT_FLAGS_1, dtFlags1);
1294
1.26k
1295
1.26k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1296
1.26k
  // need it for each process, so we don't write it for DSOs. The loader writes
1297
1.26k
  // the pointer into this entry.
1298
1.26k
  //
1299
1.26k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1300
1.26k
  // systems (currently only Fuchsia OS) provide other means to give the
1301
1.26k
  // debugger this information. Such systems may choose make .dynamic read-only.
1302
1.26k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1303
1.26k
  if (!config->shared && 
!config->relocatable382
&&
!config->zRodynamic380
)
1304
379
    addInt(DT_DEBUG, 0);
1305
1.26k
1306
1.26k
  if (OutputSection *sec = part.dynStrTab->getParent())
1307
1.26k
    this->link = sec->sectionIndex;
1308
1.26k
1309
1.26k
  if (part.relaDyn->isNeeded()) {
1310
281
    addInSec(part.relaDyn->dynamicTag, part.relaDyn);
1311
281
    addSize(part.relaDyn->sizeDynamicTag, part.relaDyn->getParent());
1312
281
1313
281
    bool isRela = config->isRela;
1314
281
    addInt(isRela ? 
DT_RELAENT213
:
DT_RELENT68
,
1315
281
           isRela ? 
sizeof(Elf_Rela)213
:
sizeof(Elf_Rel)68
);
1316
281
1317
281
    // MIPS dynamic loader does not support RELCOUNT tag.
1318
281
    // The problem is in the tight relation between dynamic
1319
281
    // relocations and GOT. So do not emit this tag on MIPS.
1320
281
    if (config->emachine != EM_MIPS) {
1321
265
      size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1322
265
      if (config->zCombreloc && 
numRelativeRels264
)
1323
59
        addInt(isRela ? 
DT_RELACOUNT45
:
DT_RELCOUNT14
, numRelativeRels);
1324
265
    }
1325
281
  }
1326
1.26k
  if (part.relrDyn && 
!part.relrDyn->relocs.empty()6
) {
1327
5
    addInSec(config->useAndroidRelrTags ? 
DT_ANDROID_RELR0
: DT_RELR,
1328
5
             part.relrDyn);
1329
5
    addSize(config->useAndroidRelrTags ? 
DT_ANDROID_RELRSZ0
: DT_RELRSZ,
1330
5
            part.relrDyn->getParent());
1331
5
    addInt(config->useAndroidRelrTags ? 
DT_ANDROID_RELRENT0
: DT_RELRENT,
1332
5
           sizeof(Elf_Relr));
1333
5
  }
1334
1.26k
  // .rel[a].plt section usually consists of two parts, containing plt and
1335
1.26k
  // iplt relocations. It is possible to have only iplt relocations in the
1336
1.26k
  // output. In that case relaPlt is empty and have zero offset, the same offset
1337
1.26k
  // as relaIplt has. And we still want to emit proper dynamic tags for that
1338
1.26k
  // case, so here we always use relaPlt as marker for the begining of
1339
1.26k
  // .rel[a].plt section.
1340
1.26k
  if (isMain && 
(1.25k
in.relaPlt->isNeeded()1.25k
||
in.relaIplt->isNeeded()992
)) {
1341
270
    addInSec(DT_JMPREL, in.relaPlt);
1342
270
    entries.push_back({DT_PLTRELSZ, addPltRelSz});
1343
270
    switch (config->emachine) {
1344
270
    case EM_MIPS:
1345
15
      addInSec(DT_MIPS_PLTGOT, in.gotPlt);
1346
15
      break;
1347
270
    case EM_SPARCV9:
1348
0
      addInSec(DT_PLTGOT, in.plt);
1349
0
      break;
1350
270
    default:
1351
255
      addInSec(DT_PLTGOT, in.gotPlt);
1352
255
      break;
1353
270
    }
1354
270
    addInt(DT_PLTREL, config->isRela ? 
DT_RELA219
:
DT_REL51
);
1355
270
  }
1356
1.26k
1357
1.26k
  if (config->emachine == EM_AARCH64) {
1358
53
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1359
8
      addInt(DT_AARCH64_BTI_PLT, 0);
1360
53
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
1361
5
      addInt(DT_AARCH64_PAC_PLT, 0);
1362
53
  }
1363
1.26k
1364
1.26k
  addInSec(DT_SYMTAB, part.dynSymTab);
1365
1.26k
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1366
1.26k
  addInSec(DT_STRTAB, part.dynStrTab);
1367
1.26k
  addInt(DT_STRSZ, part.dynStrTab->getSize());
1368
1.26k
  if (!config->zText)
1369
11
    addInt(DT_TEXTREL, 0);
1370
1.26k
  if (part.gnuHashTab)
1371
1.02k
    addInSec(DT_GNU_HASH, part.gnuHashTab);
1372
1.26k
  if (part.hashTab)
1373
1.25k
    addInSec(DT_HASH, part.hashTab);
1374
1.26k
1375
1.26k
  if (isMain) {
1376
1.25k
    if (Out::preinitArray) {
1377
3
      addOutSec(DT_PREINIT_ARRAY, Out::preinitArray);
1378
3
      addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray);
1379
3
    }
1380
1.25k
    if (Out::initArray) {
1381
4
      addOutSec(DT_INIT_ARRAY, Out::initArray);
1382
4
      addSize(DT_INIT_ARRAYSZ, Out::initArray);
1383
4
    }
1384
1.25k
    if (Out::finiArray) {
1385
2
      addOutSec(DT_FINI_ARRAY, Out::finiArray);
1386
2
      addSize(DT_FINI_ARRAYSZ, Out::finiArray);
1387
2
    }
1388
1.25k
1389
1.25k
    if (Symbol *b = symtab->find(config->init))
1390
6
      if (b->isDefined())
1391
4
        addSym(DT_INIT, b);
1392
1.25k
    if (Symbol *b = symtab->find(config->fini))
1393
6
      if (b->isDefined())
1394
4
        addSym(DT_FINI, b);
1395
1.25k
  }
1396
1.26k
1397
1.26k
  bool hasVerNeed = SharedFile::vernauxNum != 0;
1398
1.26k
  if (hasVerNeed || 
part.verDef1.25k
)
1399
67
    addInSec(DT_VERSYM, part.verSym);
1400
1.26k
  if (part.verDef) {
1401
60
    addInSec(DT_VERDEF, part.verDef);
1402
60
    addInt(DT_VERDEFNUM, getVerDefNum());
1403
60
  }
1404
1.26k
  if (hasVerNeed) {
1405
10
    addInSec(DT_VERNEED, part.verNeed);
1406
10
    unsigned needNum = 0;
1407
10
    for (SharedFile *f : sharedFiles)
1408
11
      if (!f->vernauxs.empty())
1409
11
        ++needNum;
1410
10
    addInt(DT_VERNEEDNUM, needNum);
1411
10
  }
1412
1.26k
1413
1.26k
  if (config->emachine == EM_MIPS) {
1414
100
    addInt(DT_MIPS_RLD_VERSION, 1);
1415
100
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1416
100
    addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());
1417
100
    addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1418
100
1419
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
1419
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
1419
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
1419
23
    add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); });
1420
100
1421
100
    if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())
1422
32
      addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1423
68
    else
1424
68
      addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1425
100
    addInSec(DT_PLTGOT, in.mipsGot);
1426
100
    if (in.mipsRldMap) {
1427
29
      if (!config->pie)
1428
26
        addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap);
1429
29
      // Store the offset to the .rld_map section
1430
29
      // relative to the address of the tag.
1431
29
      addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap);
1432
29
    }
1433
100
  }
1434
1.26k
1435
1.26k
  // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1436
1.26k
  // glibc assumes the old-style BSS PLT layout which we don't support.
1437
1.26k
  if (config->emachine == EM_PPC)
1438
16
    add(DT_PPC_GOT, [] { return in.got->getVA(); });
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()::'lambda0'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()::'lambda0'()::operator()() const
Line
Count
Source
1438
16
    add(DT_PPC_GOT, [] { return in.got->getVA(); });
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()::'lambda0'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()::'lambda0'()::operator()() const
1439
1.26k
1440
1.26k
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1441
1.26k
  if (config->emachine == EM_PPC64 && 
in.plt->isNeeded()68
) {
1442
21
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1443
21
    // stub, which starts directly after the header.
1444
21
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
21
                         unsigned offset = target->pltHeaderSize - 32;
1446
21
                         return in.plt->getVA(0) + offset;
1447
21
                       }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()::'lambda1'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()::'lambda1'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()::'lambda1'()::operator()() const
Line
Count
Source
1444
12
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
12
                         unsigned offset = target->pltHeaderSize - 32;
1446
12
                         return in.plt->getVA(0) + offset;
1447
12
                       }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()::'lambda1'()::operator()() const
Line
Count
Source
1444
9
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
9
                         unsigned offset = target->pltHeaderSize - 32;
1446
9
                         return in.plt->getVA(0) + offset;
1447
9
                       }});
1448
21
  }
1449
1.26k
1450
1.26k
  addInt(DT_NULL, 0);
1451
1.26k
1452
1.26k
  getParent()->link = this->link;
1453
1.26k
  this->size = entries.size() * this->entsize;
1454
1.26k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1234
177
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1235
177
  elf::Partition &part = getPartition();
1236
177
  bool isMain = part.name.empty();
1237
177
1238
177
  for (StringRef s : config->filterList)
1239
0
    addInt(DT_FILTER, part.dynStrTab->addString(s));
1240
177
  for (StringRef s : config->auxiliaryList)
1241
0
    addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1242
177
1243
177
  if (!config->rpath.empty())
1244
2
    addInt(config->enableNewDtags ? DT_RUNPATH : 
DT_RPATH0
,
1245
2
           part.dynStrTab->addString(config->rpath));
1246
177
1247
177
  for (SharedFile *file : sharedFiles)
1248
59
    if (file->isNeeded)
1249
53
      addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1250
177
1251
177
  if (isMain) {
1252
174
    if (!config->soName.empty())
1253
7
      addInt(DT_SONAME, part.dynStrTab->addString(config->soName));
1254
174
  } else {
1255
3
    if (!config->soName.empty())
1256
0
      addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));
1257
3
    addInt(DT_SONAME, part.dynStrTab->addString(part.name));
1258
3
  }
1259
177
1260
177
  // Set DT_FLAGS and DT_FLAGS_1.
1261
177
  uint32_t dtFlags = 0;
1262
177
  uint32_t dtFlags1 = 0;
1263
177
  if (config->bsymbolic)
1264
2
    dtFlags |= DF_SYMBOLIC;
1265
177
  if (config->zGlobal)
1266
0
    dtFlags1 |= DF_1_GLOBAL;
1267
177
  if (config->zInitfirst)
1268
0
    dtFlags1 |= DF_1_INITFIRST;
1269
177
  if (config->zInterpose)
1270
0
    dtFlags1 |= DF_1_INTERPOSE;
1271
177
  if (config->zNodefaultlib)
1272
0
    dtFlags1 |= DF_1_NODEFLIB;
1273
177
  if (config->zNodelete)
1274
0
    dtFlags1 |= DF_1_NODELETE;
1275
177
  if (config->zNodlopen)
1276
0
    dtFlags1 |= DF_1_NOOPEN;
1277
177
  if (config->zNow) {
1278
0
    dtFlags |= DF_BIND_NOW;
1279
0
    dtFlags1 |= DF_1_NOW;
1280
0
  }
1281
177
  if (config->zOrigin) {
1282
0
    dtFlags |= DF_ORIGIN;
1283
0
    dtFlags1 |= DF_1_ORIGIN;
1284
0
  }
1285
177
  if (!config->zText)
1286
1
    dtFlags |= DF_TEXTREL;
1287
177
  if (config->hasStaticTlsModel)
1288
11
    dtFlags |= DF_STATIC_TLS;
1289
177
1290
177
  if (dtFlags)
1291
14
    addInt(DT_FLAGS, dtFlags);
1292
177
  if (dtFlags1)
1293
0
    addInt(DT_FLAGS_1, dtFlags1);
1294
177
1295
177
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1296
177
  // need it for each process, so we don't write it for DSOs. The loader writes
1297
177
  // the pointer into this entry.
1298
177
  //
1299
177
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1300
177
  // systems (currently only Fuchsia OS) provide other means to give the
1301
177
  // debugger this information. Such systems may choose make .dynamic read-only.
1302
177
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1303
177
  if (!config->shared && 
!config->relocatable60
&&
!config->zRodynamic60
)
1304
60
    addInt(DT_DEBUG, 0);
1305
177
1306
177
  if (OutputSection *sec = part.dynStrTab->getParent())
1307
177
    this->link = sec->sectionIndex;
1308
177
1309
177
  if (part.relaDyn->isNeeded()) {
1310
61
    addInSec(part.relaDyn->dynamicTag, part.relaDyn);
1311
61
    addSize(part.relaDyn->sizeDynamicTag, part.relaDyn->getParent());
1312
61
1313
61
    bool isRela = config->isRela;
1314
61
    addInt(isRela ? 
DT_RELAENT8
:
DT_RELENT53
,
1315
61
           isRela ? 
sizeof(Elf_Rela)8
:
sizeof(Elf_Rel)53
);
1316
61
1317
61
    // MIPS dynamic loader does not support RELCOUNT tag.
1318
61
    // The problem is in the tight relation between dynamic
1319
61
    // relocations and GOT. So do not emit this tag on MIPS.
1320
61
    if (config->emachine != EM_MIPS) {
1321
60
      size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1322
60
      if (config->zCombreloc && numRelativeRels)
1323
16
        addInt(isRela ? 
DT_RELACOUNT2
:
DT_RELCOUNT14
, numRelativeRels);
1324
60
    }
1325
61
  }
1326
177
  if (part.relrDyn && 
!part.relrDyn->relocs.empty()2
) {
1327
2
    addInSec(config->useAndroidRelrTags ? 
DT_ANDROID_RELR0
: DT_RELR,
1328
2
             part.relrDyn);
1329
2
    addSize(config->useAndroidRelrTags ? 
DT_ANDROID_RELRSZ0
: DT_RELRSZ,
1330
2
            part.relrDyn->getParent());
1331
2
    addInt(config->useAndroidRelrTags ? 
DT_ANDROID_RELRENT0
: DT_RELRENT,
1332
2
           sizeof(Elf_Relr));
1333
2
  }
1334
177
  // .rel[a].plt section usually consists of two parts, containing plt and
1335
177
  // iplt relocations. It is possible to have only iplt relocations in the
1336
177
  // output. In that case relaPlt is empty and have zero offset, the same offset
1337
177
  // as relaIplt has. And we still want to emit proper dynamic tags for that
1338
177
  // case, so here we always use relaPlt as marker for the begining of
1339
177
  // .rel[a].plt section.
1340
177
  if (isMain && 
(174
in.relaPlt->isNeeded()174
||
in.relaIplt->isNeeded()131
)) {
1341
43
    addInSec(DT_JMPREL, in.relaPlt);
1342
43
    entries.push_back({DT_PLTRELSZ, addPltRelSz});
1343
43
    switch (config->emachine) {
1344
43
    case EM_MIPS:
1345
2
      addInSec(DT_MIPS_PLTGOT, in.gotPlt);
1346
2
      break;
1347
43
    case EM_SPARCV9:
1348
0
      addInSec(DT_PLTGOT, in.plt);
1349
0
      break;
1350
43
    default:
1351
41
      addInSec(DT_PLTGOT, in.gotPlt);
1352
41
      break;
1353
43
    }
1354
43
    addInt(DT_PLTREL, config->isRela ? 
DT_RELA5
:
DT_REL38
);
1355
43
  }
1356
177
1357
177
  if (config->emachine == EM_AARCH64) {
1358
0
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1359
0
      addInt(DT_AARCH64_BTI_PLT, 0);
1360
0
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
1361
0
      addInt(DT_AARCH64_PAC_PLT, 0);
1362
0
  }
1363
177
1364
177
  addInSec(DT_SYMTAB, part.dynSymTab);
1365
177
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1366
177
  addInSec(DT_STRTAB, part.dynStrTab);
1367
177
  addInt(DT_STRSZ, part.dynStrTab->getSize());
1368
177
  if (!config->zText)
1369
1
    addInt(DT_TEXTREL, 0);
1370
177
  if (part.gnuHashTab)
1371
133
    addInSec(DT_GNU_HASH, part.gnuHashTab);
1372
177
  if (part.hashTab)
1373
175
    addInSec(DT_HASH, part.hashTab);
1374
177
1375
177
  if (isMain) {
1376
174
    if (Out::preinitArray) {
1377
0
      addOutSec(DT_PREINIT_ARRAY, Out::preinitArray);
1378
0
      addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray);
1379
0
    }
1380
174
    if (Out::initArray) {
1381
0
      addOutSec(DT_INIT_ARRAY, Out::initArray);
1382
0
      addSize(DT_INIT_ARRAYSZ, Out::initArray);
1383
0
    }
1384
174
    if (Out::finiArray) {
1385
0
      addOutSec(DT_FINI_ARRAY, Out::finiArray);
1386
0
      addSize(DT_FINI_ARRAYSZ, Out::finiArray);
1387
0
    }
1388
174
1389
174
    if (Symbol *b = symtab->find(config->init))
1390
0
      if (b->isDefined())
1391
0
        addSym(DT_INIT, b);
1392
174
    if (Symbol *b = symtab->find(config->fini))
1393
0
      if (b->isDefined())
1394
0
        addSym(DT_FINI, b);
1395
174
  }
1396
177
1397
177
  bool hasVerNeed = SharedFile::vernauxNum != 0;
1398
177
  if (hasVerNeed || part.verDef)
1399
0
    addInSec(DT_VERSYM, part.verSym);
1400
177
  if (part.verDef) {
1401
0
    addInSec(DT_VERDEF, part.verDef);
1402
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1403
0
  }
1404
177
  if (hasVerNeed) {
1405
0
    addInSec(DT_VERNEED, part.verNeed);
1406
0
    unsigned needNum = 0;
1407
0
    for (SharedFile *f : sharedFiles)
1408
0
      if (!f->vernauxs.empty())
1409
0
        ++needNum;
1410
0
    addInt(DT_VERNEEDNUM, needNum);
1411
0
  }
1412
177
1413
177
  if (config->emachine == EM_MIPS) {
1414
8
    addInt(DT_MIPS_RLD_VERSION, 1);
1415
8
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1416
8
    addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());
1417
8
    addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1418
8
1419
8
    add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); });
1420
8
1421
8
    if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())
1422
3
      addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1423
5
    else
1424
5
      addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1425
8
    addInSec(DT_PLTGOT, in.mipsGot);
1426
8
    if (in.mipsRldMap) {
1427
2
      if (!config->pie)
1428
2
        addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap);
1429
2
      // Store the offset to the .rld_map section
1430
2
      // relative to the address of the tag.
1431
2
      addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap);
1432
2
    }
1433
8
  }
1434
177
1435
177
  // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1436
177
  // glibc assumes the old-style BSS PLT layout which we don't support.
1437
177
  if (config->emachine == EM_PPC)
1438
0
    add(DT_PPC_GOT, [] { return in.got->getVA(); });
1439
177
1440
177
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1441
177
  if (config->emachine == EM_PPC64 && 
in.plt->isNeeded()0
) {
1442
0
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1443
0
    // stub, which starts directly after the header.
1444
0
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
0
                         unsigned offset = target->pltHeaderSize - 32;
1446
0
                         return in.plt->getVA(0) + offset;
1447
0
                       }});
1448
0
  }
1449
177
1450
177
  addInt(DT_NULL, 0);
1451
177
1452
177
  getParent()->link = this->link;
1453
177
  this->size = entries.size() * this->entsize;
1454
177
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1234
85
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1235
85
  elf::Partition &part = getPartition();
1236
85
  bool isMain = part.name.empty();
1237
85
1238
85
  for (StringRef s : config->filterList)
1239
0
    addInt(DT_FILTER, part.dynStrTab->addString(s));
1240
85
  for (StringRef s : config->auxiliaryList)
1241
0
    addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1242
85
1243
85
  if (!config->rpath.empty())
1244
0
    addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH,
1245
0
           part.dynStrTab->addString(config->rpath));
1246
85
1247
85
  for (SharedFile *file : sharedFiles)
1248
29
    if (file->isNeeded)
1249
29
      addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1250
85
1251
85
  if (isMain) {
1252
85
    if (!config->soName.empty())
1253
3
      addInt(DT_SONAME, part.dynStrTab->addString(config->soName));
1254
85
  } else {
1255
0
    if (!config->soName.empty())
1256
0
      addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));
1257
0
    addInt(DT_SONAME, part.dynStrTab->addString(part.name));
1258
0
  }
1259
85
1260
85
  // Set DT_FLAGS and DT_FLAGS_1.
1261
85
  uint32_t dtFlags = 0;
1262
85
  uint32_t dtFlags1 = 0;
1263
85
  if (config->bsymbolic)
1264
1
    dtFlags |= DF_SYMBOLIC;
1265
85
  if (config->zGlobal)
1266
0
    dtFlags1 |= DF_1_GLOBAL;
1267
85
  if (config->zInitfirst)
1268
0
    dtFlags1 |= DF_1_INITFIRST;
1269
85
  if (config->zInterpose)
1270
0
    dtFlags1 |= DF_1_INTERPOSE;
1271
85
  if (config->zNodefaultlib)
1272
0
    dtFlags1 |= DF_1_NODEFLIB;
1273
85
  if (config->zNodelete)
1274
0
    dtFlags1 |= DF_1_NODELETE;
1275
85
  if (config->zNodlopen)
1276
0
    dtFlags1 |= DF_1_NOOPEN;
1277
85
  if (config->zNow) {
1278
0
    dtFlags |= DF_BIND_NOW;
1279
0
    dtFlags1 |= DF_1_NOW;
1280
0
  }
1281
85
  if (config->zOrigin) {
1282
0
    dtFlags |= DF_ORIGIN;
1283
0
    dtFlags1 |= DF_1_ORIGIN;
1284
0
  }
1285
85
  if (!config->zText)
1286
0
    dtFlags |= DF_TEXTREL;
1287
85
  if (config->hasStaticTlsModel)
1288
0
    dtFlags |= DF_STATIC_TLS;
1289
85
1290
85
  if (dtFlags)
1291
1
    addInt(DT_FLAGS, dtFlags);
1292
85
  if (dtFlags1)
1293
0
    addInt(DT_FLAGS_1, dtFlags1);
1294
85
1295
85
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1296
85
  // need it for each process, so we don't write it for DSOs. The loader writes
1297
85
  // the pointer into this entry.
1298
85
  //
1299
85
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1300
85
  // systems (currently only Fuchsia OS) provide other means to give the
1301
85
  // debugger this information. Such systems may choose make .dynamic read-only.
1302
85
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1303
85
  if (!config->shared && 
!config->relocatable26
&&
!config->zRodynamic26
)
1304
26
    addInt(DT_DEBUG, 0);
1305
85
1306
85
  if (OutputSection *sec = part.dynStrTab->getParent())
1307
85
    this->link = sec->sectionIndex;
1308
85
1309
85
  if (part.relaDyn->isNeeded()) {
1310
16
    addInSec(part.relaDyn->dynamicTag, part.relaDyn);
1311
16
    addSize(part.relaDyn->sizeDynamicTag, part.relaDyn->getParent());
1312
16
1313
16
    bool isRela = config->isRela;
1314
16
    addInt(isRela ? 
DT_RELAENT9
:
DT_RELENT7
,
1315
16
           isRela ? 
sizeof(Elf_Rela)9
:
sizeof(Elf_Rel)7
);
1316
16
1317
16
    // MIPS dynamic loader does not support RELCOUNT tag.
1318
16
    // The problem is in the tight relation between dynamic
1319
16
    // relocations and GOT. So do not emit this tag on MIPS.
1320
16
    if (config->emachine != EM_MIPS) {
1321
9
      size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1322
9
      if (config->zCombreloc && numRelativeRels)
1323
1
        addInt(isRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, numRelativeRels);
1324
9
    }
1325
16
  }
1326
85
  if (part.relrDyn && 
!part.relrDyn->relocs.empty()0
) {
1327
0
    addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
1328
0
             part.relrDyn);
1329
0
    addSize(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
1330
0
            part.relrDyn->getParent());
1331
0
    addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
1332
0
           sizeof(Elf_Relr));
1333
0
  }
1334
85
  // .rel[a].plt section usually consists of two parts, containing plt and
1335
85
  // iplt relocations. It is possible to have only iplt relocations in the
1336
85
  // output. In that case relaPlt is empty and have zero offset, the same offset
1337
85
  // as relaIplt has. And we still want to emit proper dynamic tags for that
1338
85
  // case, so here we always use relaPlt as marker for the begining of
1339
85
  // .rel[a].plt section.
1340
85
  if (isMain && (in.relaPlt->isNeeded() || 
in.relaIplt->isNeeded()68
)) {
1341
17
    addInSec(DT_JMPREL, in.relaPlt);
1342
17
    entries.push_back({DT_PLTRELSZ, addPltRelSz});
1343
17
    switch (config->emachine) {
1344
17
    case EM_MIPS:
1345
11
      addInSec(DT_MIPS_PLTGOT, in.gotPlt);
1346
11
      break;
1347
17
    case EM_SPARCV9:
1348
0
      addInSec(DT_PLTGOT, in.plt);
1349
0
      break;
1350
17
    default:
1351
6
      addInSec(DT_PLTGOT, in.gotPlt);
1352
6
      break;
1353
17
    }
1354
17
    addInt(DT_PLTREL, config->isRela ? 
DT_RELA6
:
DT_REL11
);
1355
17
  }
1356
85
1357
85
  if (config->emachine == EM_AARCH64) {
1358
0
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1359
0
      addInt(DT_AARCH64_BTI_PLT, 0);
1360
0
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
1361
0
      addInt(DT_AARCH64_PAC_PLT, 0);
1362
0
  }
1363
85
1364
85
  addInSec(DT_SYMTAB, part.dynSymTab);
1365
85
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1366
85
  addInSec(DT_STRTAB, part.dynStrTab);
1367
85
  addInt(DT_STRSZ, part.dynStrTab->getSize());
1368
85
  if (!config->zText)
1369
0
    addInt(DT_TEXTREL, 0);
1370
85
  if (part.gnuHashTab)
1371
16
    addInSec(DT_GNU_HASH, part.gnuHashTab);
1372
85
  if (part.hashTab)
1373
85
    addInSec(DT_HASH, part.hashTab);
1374
85
1375
85
  if (isMain) {
1376
85
    if (Out::preinitArray) {
1377
0
      addOutSec(DT_PREINIT_ARRAY, Out::preinitArray);
1378
0
      addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray);
1379
0
    }
1380
85
    if (Out::initArray) {
1381
0
      addOutSec(DT_INIT_ARRAY, Out::initArray);
1382
0
      addSize(DT_INIT_ARRAYSZ, Out::initArray);
1383
0
    }
1384
85
    if (Out::finiArray) {
1385
0
      addOutSec(DT_FINI_ARRAY, Out::finiArray);
1386
0
      addSize(DT_FINI_ARRAYSZ, Out::finiArray);
1387
0
    }
1388
85
1389
85
    if (Symbol *b = symtab->find(config->init))
1390
0
      if (b->isDefined())
1391
0
        addSym(DT_INIT, b);
1392
85
    if (Symbol *b = symtab->find(config->fini))
1393
0
      if (b->isDefined())
1394
0
        addSym(DT_FINI, b);
1395
85
  }
1396
85
1397
85
  bool hasVerNeed = SharedFile::vernauxNum != 0;
1398
85
  if (hasVerNeed || 
part.verDef84
)
1399
1
    addInSec(DT_VERSYM, part.verSym);
1400
85
  if (part.verDef) {
1401
0
    addInSec(DT_VERDEF, part.verDef);
1402
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1403
0
  }
1404
85
  if (hasVerNeed) {
1405
1
    addInSec(DT_VERNEED, part.verNeed);
1406
1
    unsigned needNum = 0;
1407
1
    for (SharedFile *f : sharedFiles)
1408
1
      if (!f->vernauxs.empty())
1409
1
        ++needNum;
1410
1
    addInt(DT_VERNEEDNUM, needNum);
1411
1
  }
1412
85
1413
85
  if (config->emachine == EM_MIPS) {
1414
69
    addInt(DT_MIPS_RLD_VERSION, 1);
1415
69
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1416
69
    addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());
1417
69
    addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1418
69
1419
69
    add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); });
1420
69
1421
69
    if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())
1422
23
      addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1423
46
    else
1424
46
      addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1425
69
    addInSec(DT_PLTGOT, in.mipsGot);
1426
69
    if (in.mipsRldMap) {
1427
21
      if (!config->pie)
1428
19
        addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap);
1429
21
      // Store the offset to the .rld_map section
1430
21
      // relative to the address of the tag.
1431
21
      addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap);
1432
21
    }
1433
69
  }
1434
85
1435
85
  // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1436
85
  // glibc assumes the old-style BSS PLT layout which we don't support.
1437
85
  if (config->emachine == EM_PPC)
1438
16
    add(DT_PPC_GOT, [] { return in.got->getVA(); });
1439
85
1440
85
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1441
85
  if (config->emachine == EM_PPC64 && 
in.plt->isNeeded()0
) {
1442
0
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1443
0
    // stub, which starts directly after the header.
1444
0
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
0
                         unsigned offset = target->pltHeaderSize - 32;
1446
0
                         return in.plt->getVA(0) + offset;
1447
0
                       }});
1448
0
  }
1449
85
1450
85
  addInt(DT_NULL, 0);
1451
85
1452
85
  getParent()->link = this->link;
1453
85
  this->size = entries.size() * this->entsize;
1454
85
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1234
951
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1235
951
  elf::Partition &part = getPartition();
1236
951
  bool isMain = part.name.empty();
1237
951
1238
951
  for (StringRef s : config->filterList)
1239
6
    addInt(DT_FILTER, part.dynStrTab->addString(s));
1240
951
  for (StringRef s : config->auxiliaryList)
1241
4
    addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1242
951
1243
951
  if (!config->rpath.empty())
1244
4
    addInt(config->enableNewDtags ? 
DT_RUNPATH3
:
DT_RPATH1
,
1245
4
           part.dynStrTab->addString(config->rpath));
1246
951
1247
951
  for (SharedFile *file : sharedFiles)
1248
275
    if (file->isNeeded)
1249
264
      addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1250
951
1251
951
  if (isMain) {
1252
943
    if (!config->soName.empty())
1253
35
      addInt(DT_SONAME, part.dynStrTab->addString(config->soName));
1254
943
  } else {
1255
8
    if (!config->soName.empty())
1256
1
      addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));
1257
8
    addInt(DT_SONAME, part.dynStrTab->addString(part.name));
1258
8
  }
1259
951
1260
951
  // Set DT_FLAGS and DT_FLAGS_1.
1261
951
  uint32_t dtFlags = 0;
1262
951
  uint32_t dtFlags1 = 0;
1263
951
  if (config->bsymbolic)
1264
4
    dtFlags |= DF_SYMBOLIC;
1265
951
  if (config->zGlobal)
1266
1
    dtFlags1 |= DF_1_GLOBAL;
1267
951
  if (config->zInitfirst)
1268
1
    dtFlags1 |= DF_1_INITFIRST;
1269
951
  if (config->zInterpose)
1270
1
    dtFlags1 |= DF_1_INTERPOSE;
1271
951
  if (config->zNodefaultlib)
1272
1
    dtFlags1 |= DF_1_NODEFLIB;
1273
951
  if (config->zNodelete)
1274
1
    dtFlags1 |= DF_1_NODELETE;
1275
951
  if (config->zNodlopen)
1276
1
    dtFlags1 |= DF_1_NOOPEN;
1277
951
  if (config->zNow) {
1278
4
    dtFlags |= DF_BIND_NOW;
1279
4
    dtFlags1 |= DF_1_NOW;
1280
4
  }
1281
951
  if (config->zOrigin) {
1282
1
    dtFlags |= DF_ORIGIN;
1283
1
    dtFlags1 |= DF_1_ORIGIN;
1284
1
  }
1285
951
  if (!config->zText)
1286
8
    dtFlags |= DF_TEXTREL;
1287
951
  if (config->hasStaticTlsModel)
1288
7
    dtFlags |= DF_STATIC_TLS;
1289
951
1290
951
  if (dtFlags)
1291
22
    addInt(DT_FLAGS, dtFlags);
1292
951
  if (dtFlags1)
1293
4
    addInt(DT_FLAGS_1, dtFlags1);
1294
951
1295
951
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1296
951
  // need it for each process, so we don't write it for DSOs. The loader writes
1297
951
  // the pointer into this entry.
1298
951
  //
1299
951
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1300
951
  // systems (currently only Fuchsia OS) provide other means to give the
1301
951
  // debugger this information. Such systems may choose make .dynamic read-only.
1302
951
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1303
951
  if (!config->shared && 
!config->relocatable284
&&
!config->zRodynamic282
)
1304
281
    addInt(DT_DEBUG, 0);
1305
951
1306
951
  if (OutputSection *sec = part.dynStrTab->getParent())
1307
950
    this->link = sec->sectionIndex;
1308
951
1309
951
  if (part.relaDyn->isNeeded()) {
1310
185
    addInSec(part.relaDyn->dynamicTag, part.relaDyn);
1311
185
    addSize(part.relaDyn->sizeDynamicTag, part.relaDyn->getParent());
1312
185
1313
185
    bool isRela = config->isRela;
1314
185
    addInt(isRela ? DT_RELAENT : 
DT_RELENT0
,
1315
185
           isRela ? sizeof(Elf_Rela) : 
sizeof(Elf_Rel)0
);
1316
185
1317
185
    // MIPS dynamic loader does not support RELCOUNT tag.
1318
185
    // The problem is in the tight relation between dynamic
1319
185
    // relocations and GOT. So do not emit this tag on MIPS.
1320
185
    if (config->emachine != EM_MIPS) {
1321
185
      size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1322
185
      if (config->zCombreloc && 
numRelativeRels184
)
1323
40
        addInt(isRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, numRelativeRels);
1324
185
    }
1325
185
  }
1326
951
  if (part.relrDyn && 
!part.relrDyn->relocs.empty()4
) {
1327
3
    addInSec(config->useAndroidRelrTags ? 
DT_ANDROID_RELR0
: DT_RELR,
1328
3
             part.relrDyn);
1329
3
    addSize(config->useAndroidRelrTags ? 
DT_ANDROID_RELRSZ0
: DT_RELRSZ,
1330
3
            part.relrDyn->getParent());
1331
3
    addInt(config->useAndroidRelrTags ? 
DT_ANDROID_RELRENT0
: DT_RELRENT,
1332
3
           sizeof(Elf_Relr));
1333
3
  }
1334
951
  // .rel[a].plt section usually consists of two parts, containing plt and
1335
951
  // iplt relocations. It is possible to have only iplt relocations in the
1336
951
  // output. In that case relaPlt is empty and have zero offset, the same offset
1337
951
  // as relaIplt has. And we still want to emit proper dynamic tags for that
1338
951
  // case, so here we always use relaPlt as marker for the begining of
1339
951
  // .rel[a].plt section.
1340
951
  if (isMain && 
(943
in.relaPlt->isNeeded()943
||
in.relaIplt->isNeeded()754
)) {
1341
199
    addInSec(DT_JMPREL, in.relaPlt);
1342
199
    entries.push_back({DT_PLTRELSZ, addPltRelSz});
1343
199
    switch (config->emachine) {
1344
199
    case EM_MIPS:
1345
0
      addInSec(DT_MIPS_PLTGOT, in.gotPlt);
1346
0
      break;
1347
199
    case EM_SPARCV9:
1348
0
      addInSec(DT_PLTGOT, in.plt);
1349
0
      break;
1350
199
    default:
1351
199
      addInSec(DT_PLTGOT, in.gotPlt);
1352
199
      break;
1353
199
    }
1354
199
    addInt(DT_PLTREL, config->isRela ? DT_RELA : 
DT_REL0
);
1355
199
  }
1356
951
1357
951
  if (config->emachine == EM_AARCH64) {
1358
53
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1359
8
      addInt(DT_AARCH64_BTI_PLT, 0);
1360
53
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
1361
5
      addInt(DT_AARCH64_PAC_PLT, 0);
1362
53
  }
1363
951
1364
951
  addInSec(DT_SYMTAB, part.dynSymTab);
1365
951
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1366
951
  addInSec(DT_STRTAB, part.dynStrTab);
1367
951
  addInt(DT_STRSZ, part.dynStrTab->getSize());
1368
951
  if (!config->zText)
1369
8
    addInt(DT_TEXTREL, 0);
1370
951
  if (part.gnuHashTab)
1371
853
    addInSec(DT_GNU_HASH, part.gnuHashTab);
1372
951
  if (part.hashTab)
1373
944
    addInSec(DT_HASH, part.hashTab);
1374
951
1375
951
  if (isMain) {
1376
943
    if (Out::preinitArray) {
1377
3
      addOutSec(DT_PREINIT_ARRAY, Out::preinitArray);
1378
3
      addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray);
1379
3
    }
1380
943
    if (Out::initArray) {
1381
4
      addOutSec(DT_INIT_ARRAY, Out::initArray);
1382
4
      addSize(DT_INIT_ARRAYSZ, Out::initArray);
1383
4
    }
1384
943
    if (Out::finiArray) {
1385
2
      addOutSec(DT_FINI_ARRAY, Out::finiArray);
1386
2
      addSize(DT_FINI_ARRAYSZ, Out::finiArray);
1387
2
    }
1388
943
1389
943
    if (Symbol *b = symtab->find(config->init))
1390
6
      if (b->isDefined())
1391
4
        addSym(DT_INIT, b);
1392
943
    if (Symbol *b = symtab->find(config->fini))
1393
6
      if (b->isDefined())
1394
4
        addSym(DT_FINI, b);
1395
943
  }
1396
951
1397
951
  bool hasVerNeed = SharedFile::vernauxNum != 0;
1398
951
  if (hasVerNeed || 
part.verDef942
)
1399
66
    addInSec(DT_VERSYM, part.verSym);
1400
951
  if (part.verDef) {
1401
60
    addInSec(DT_VERDEF, part.verDef);
1402
60
    addInt(DT_VERDEFNUM, getVerDefNum());
1403
60
  }
1404
951
  if (hasVerNeed) {
1405
9
    addInSec(DT_VERNEED, part.verNeed);
1406
9
    unsigned needNum = 0;
1407
9
    for (SharedFile *f : sharedFiles)
1408
10
      if (!f->vernauxs.empty())
1409
10
        ++needNum;
1410
9
    addInt(DT_VERNEEDNUM, needNum);
1411
9
  }
1412
951
1413
951
  if (config->emachine == EM_MIPS) {
1414
0
    addInt(DT_MIPS_RLD_VERSION, 1);
1415
0
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1416
0
    addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());
1417
0
    addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1418
0
1419
0
    add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); });
1420
0
1421
0
    if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())
1422
0
      addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1423
0
    else
1424
0
      addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1425
0
    addInSec(DT_PLTGOT, in.mipsGot);
1426
0
    if (in.mipsRldMap) {
1427
0
      if (!config->pie)
1428
0
        addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap);
1429
0
      // Store the offset to the .rld_map section
1430
0
      // relative to the address of the tag.
1431
0
      addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap);
1432
0
    }
1433
0
  }
1434
951
1435
951
  // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1436
951
  // glibc assumes the old-style BSS PLT layout which we don't support.
1437
951
  if (config->emachine == EM_PPC)
1438
0
    add(DT_PPC_GOT, [] { return in.got->getVA(); });
1439
951
1440
951
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1441
951
  if (config->emachine == EM_PPC64 && 
in.plt->isNeeded()41
) {
1442
12
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1443
12
    // stub, which starts directly after the header.
1444
12
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
12
                         unsigned offset = target->pltHeaderSize - 32;
1446
12
                         return in.plt->getVA(0) + offset;
1447
12
                       }});
1448
12
  }
1449
951
1450
951
  addInt(DT_NULL, 0);
1451
951
1452
951
  getParent()->link = this->link;
1453
951
  this->size = entries.size() * this->entsize;
1454
951
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1234
50
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1235
50
  elf::Partition &part = getPartition();
1236
50
  bool isMain = part.name.empty();
1237
50
1238
50
  for (StringRef s : config->filterList)
1239
0
    addInt(DT_FILTER, part.dynStrTab->addString(s));
1240
50
  for (StringRef s : config->auxiliaryList)
1241
0
    addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1242
50
1243
50
  if (!config->rpath.empty())
1244
1
    addInt(config->enableNewDtags ? DT_RUNPATH : 
DT_RPATH0
,
1245
1
           part.dynStrTab->addString(config->rpath));
1246
50
1247
50
  for (SharedFile *file : sharedFiles)
1248
14
    if (file->isNeeded)
1249
14
      addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1250
50
1251
50
  if (isMain) {
1252
50
    if (!config->soName.empty())
1253
1
      addInt(DT_SONAME, part.dynStrTab->addString(config->soName));
1254
50
  } else {
1255
0
    if (!config->soName.empty())
1256
0
      addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));
1257
0
    addInt(DT_SONAME, part.dynStrTab->addString(part.name));
1258
0
  }
1259
50
1260
50
  // Set DT_FLAGS and DT_FLAGS_1.
1261
50
  uint32_t dtFlags = 0;
1262
50
  uint32_t dtFlags1 = 0;
1263
50
  if (config->bsymbolic)
1264
1
    dtFlags |= DF_SYMBOLIC;
1265
50
  if (config->zGlobal)
1266
0
    dtFlags1 |= DF_1_GLOBAL;
1267
50
  if (config->zInitfirst)
1268
0
    dtFlags1 |= DF_1_INITFIRST;
1269
50
  if (config->zInterpose)
1270
0
    dtFlags1 |= DF_1_INTERPOSE;
1271
50
  if (config->zNodefaultlib)
1272
0
    dtFlags1 |= DF_1_NODEFLIB;
1273
50
  if (config->zNodelete)
1274
0
    dtFlags1 |= DF_1_NODELETE;
1275
50
  if (config->zNodlopen)
1276
0
    dtFlags1 |= DF_1_NOOPEN;
1277
50
  if (config->zNow) {
1278
0
    dtFlags |= DF_BIND_NOW;
1279
0
    dtFlags1 |= DF_1_NOW;
1280
0
  }
1281
50
  if (config->zOrigin) {
1282
0
    dtFlags |= DF_ORIGIN;
1283
0
    dtFlags1 |= DF_1_ORIGIN;
1284
0
  }
1285
50
  if (!config->zText)
1286
2
    dtFlags |= DF_TEXTREL;
1287
50
  if (config->hasStaticTlsModel)
1288
0
    dtFlags |= DF_STATIC_TLS;
1289
50
1290
50
  if (dtFlags)
1291
3
    addInt(DT_FLAGS, dtFlags);
1292
50
  if (dtFlags1)
1293
0
    addInt(DT_FLAGS_1, dtFlags1);
1294
50
1295
50
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1296
50
  // need it for each process, so we don't write it for DSOs. The loader writes
1297
50
  // the pointer into this entry.
1298
50
  //
1299
50
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1300
50
  // systems (currently only Fuchsia OS) provide other means to give the
1301
50
  // debugger this information. Such systems may choose make .dynamic read-only.
1302
50
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1303
50
  if (!config->shared && 
!config->relocatable12
&&
!config->zRodynamic12
)
1304
12
    addInt(DT_DEBUG, 0);
1305
50
1306
50
  if (OutputSection *sec = part.dynStrTab->getParent())
1307
50
    this->link = sec->sectionIndex;
1308
50
1309
50
  if (part.relaDyn->isNeeded()) {
1310
19
    addInSec(part.relaDyn->dynamicTag, part.relaDyn);
1311
19
    addSize(part.relaDyn->sizeDynamicTag, part.relaDyn->getParent());
1312
19
1313
19
    bool isRela = config->isRela;
1314
19
    addInt(isRela ? 
DT_RELAENT11
:
DT_RELENT8
,
1315
19
           isRela ? 
sizeof(Elf_Rela)11
:
sizeof(Elf_Rel)8
);
1316
19
1317
19
    // MIPS dynamic loader does not support RELCOUNT tag.
1318
19
    // The problem is in the tight relation between dynamic
1319
19
    // relocations and GOT. So do not emit this tag on MIPS.
1320
19
    if (config->emachine != EM_MIPS) {
1321
11
      size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1322
11
      if (config->zCombreloc && numRelativeRels)
1323
2
        addInt(isRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, numRelativeRels);
1324
11
    }
1325
19
  }
1326
50
  if (part.relrDyn && 
!part.relrDyn->relocs.empty()0
) {
1327
0
    addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
1328
0
             part.relrDyn);
1329
0
    addSize(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
1330
0
            part.relrDyn->getParent());
1331
0
    addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
1332
0
           sizeof(Elf_Relr));
1333
0
  }
1334
50
  // .rel[a].plt section usually consists of two parts, containing plt and
1335
50
  // iplt relocations. It is possible to have only iplt relocations in the
1336
50
  // output. In that case relaPlt is empty and have zero offset, the same offset
1337
50
  // as relaIplt has. And we still want to emit proper dynamic tags for that
1338
50
  // case, so here we always use relaPlt as marker for the begining of
1339
50
  // .rel[a].plt section.
1340
50
  if (isMain && (in.relaPlt->isNeeded() || 
in.relaIplt->isNeeded()39
)) {
1341
11
    addInSec(DT_JMPREL, in.relaPlt);
1342
11
    entries.push_back({DT_PLTRELSZ, addPltRelSz});
1343
11
    switch (config->emachine) {
1344
11
    case EM_MIPS:
1345
2
      addInSec(DT_MIPS_PLTGOT, in.gotPlt);
1346
2
      break;
1347
11
    case EM_SPARCV9:
1348
0
      addInSec(DT_PLTGOT, in.plt);
1349
0
      break;
1350
11
    default:
1351
9
      addInSec(DT_PLTGOT, in.gotPlt);
1352
9
      break;
1353
11
    }
1354
11
    addInt(DT_PLTREL, config->isRela ? 
DT_RELA9
:
DT_REL2
);
1355
11
  }
1356
50
1357
50
  if (config->emachine == EM_AARCH64) {
1358
0
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1359
0
      addInt(DT_AARCH64_BTI_PLT, 0);
1360
0
    if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
1361
0
      addInt(DT_AARCH64_PAC_PLT, 0);
1362
0
  }
1363
50
1364
50
  addInSec(DT_SYMTAB, part.dynSymTab);
1365
50
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1366
50
  addInSec(DT_STRTAB, part.dynStrTab);
1367
50
  addInt(DT_STRSZ, part.dynStrTab->getSize());
1368
50
  if (!config->zText)
1369
2
    addInt(DT_TEXTREL, 0);
1370
50
  if (part.gnuHashTab)
1371
26
    addInSec(DT_GNU_HASH, part.gnuHashTab);
1372
50
  if (part.hashTab)
1373
50
    addInSec(DT_HASH, part.hashTab);
1374
50
1375
50
  if (isMain) {
1376
50
    if (Out::preinitArray) {
1377
0
      addOutSec(DT_PREINIT_ARRAY, Out::preinitArray);
1378
0
      addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray);
1379
0
    }
1380
50
    if (Out::initArray) {
1381
0
      addOutSec(DT_INIT_ARRAY, Out::initArray);
1382
0
      addSize(DT_INIT_ARRAYSZ, Out::initArray);
1383
0
    }
1384
50
    if (Out::finiArray) {
1385
0
      addOutSec(DT_FINI_ARRAY, Out::finiArray);
1386
0
      addSize(DT_FINI_ARRAYSZ, Out::finiArray);
1387
0
    }
1388
50
1389
50
    if (Symbol *b = symtab->find(config->init))
1390
0
      if (b->isDefined())
1391
0
        addSym(DT_INIT, b);
1392
50
    if (Symbol *b = symtab->find(config->fini))
1393
0
      if (b->isDefined())
1394
0
        addSym(DT_FINI, b);
1395
50
  }
1396
50
1397
50
  bool hasVerNeed = SharedFile::vernauxNum != 0;
1398
50
  if (hasVerNeed || part.verDef)
1399
0
    addInSec(DT_VERSYM, part.verSym);
1400
50
  if (part.verDef) {
1401
0
    addInSec(DT_VERDEF, part.verDef);
1402
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1403
0
  }
1404
50
  if (hasVerNeed) {
1405
0
    addInSec(DT_VERNEED, part.verNeed);
1406
0
    unsigned needNum = 0;
1407
0
    for (SharedFile *f : sharedFiles)
1408
0
      if (!f->vernauxs.empty())
1409
0
        ++needNum;
1410
0
    addInt(DT_VERNEEDNUM, needNum);
1411
0
  }
1412
50
1413
50
  if (config->emachine == EM_MIPS) {
1414
23
    addInt(DT_MIPS_RLD_VERSION, 1);
1415
23
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1416
23
    addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());
1417
23
    addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1418
23
1419
23
    add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); });
1420
23
1421
23
    if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())
1422
6
      addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1423
17
    else
1424
17
      addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1425
23
    addInSec(DT_PLTGOT, in.mipsGot);
1426
23
    if (in.mipsRldMap) {
1427
6
      if (!config->pie)
1428
5
        addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap);
1429
6
      // Store the offset to the .rld_map section
1430
6
      // relative to the address of the tag.
1431
6
      addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap);
1432
6
    }
1433
23
  }
1434
50
1435
50
  // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1436
50
  // glibc assumes the old-style BSS PLT layout which we don't support.
1437
50
  if (config->emachine == EM_PPC)
1438
0
    add(DT_PPC_GOT, [] { return in.got->getVA(); });
1439
50
1440
50
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1441
50
  if (config->emachine == EM_PPC64 && 
in.plt->isNeeded()27
) {
1442
9
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1443
9
    // stub, which starts directly after the header.
1444
9
    entries.push_back({DT_PPC64_GLINK, [=] {
1445
9
                         unsigned offset = target->pltHeaderSize - 32;
1446
9
                         return in.plt->getVA(0) + offset;
1447
9
                       }});
1448
9
  }
1449
50
1450
50
  addInt(DT_NULL, 0);
1451
50
1452
50
  getParent()->link = this->link;
1453
50
  this->size = entries.size() * this->entsize;
1454
50
}
1455
1456
1.25k
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1457
1.25k
  auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1458
1.25k
1459
12.4k
  for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) {
1460
12.4k
    p->d_tag = kv.first;
1461
12.4k
    p->d_un.d_val = kv.second();
1462
12.4k
    ++p;
1463
12.4k
  }
1464
1.25k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1456
177
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1457
177
  auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1458
177
1459
1.77k
  for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) {
1460
1.77k
    p->d_tag = kv.first;
1461
1.77k
    p->d_un.d_val = kv.second();
1462
1.77k
    ++p;
1463
1.77k
  }
1464
177
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1456
85
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1457
85
  auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1458
85
1459
1.24k
  for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) {
1460
1.24k
    p->d_tag = kv.first;
1461
1.24k
    p->d_un.d_val = kv.second();
1462
1.24k
    ++p;
1463
1.24k
  }
1464
85
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1456
942
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1457
942
  auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1458
942
1459
8.76k
  for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) {
1460
8.76k
    p->d_tag = kv.first;
1461
8.76k
    p->d_un.d_val = kv.second();
1462
8.76k
    ++p;
1463
8.76k
  }
1464
942
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1456
50
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1457
50
  auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1458
50
1459
643
  for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) {
1460
643
    p->d_tag = kv.first;
1461
643
    p->d_un.d_val = kv.second();
1462
643
    ++p;
1463
643
  }
1464
50
}
1465
1466
2.24k
uint64_t DynamicReloc::getOffset() const {
1467
2.24k
  return inputSec->getVA(offsetInSec);
1468
2.24k
}
1469
1470
799
int64_t DynamicReloc::computeAddend() const {
1471
799
  if (useSymVA)
1472
213
    return sym->getVA(addend);
1473
586
  if (!outputSec)
1474
586
    return addend;
1475
0
  // See the comment in the DynamicReloc ctor.
1476
0
  return getMipsPageAddr(outputSec->addr) + addend;
1477
0
}
1478
1479
2.24k
uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
1480
2.24k
  if (sym && 
!useSymVA2.13k
)
1481
1.49k
    return symTab->getSymbolIndex(sym);
1482
744
  return 0;
1483
744
}
1484
1485
RelocationBaseSection::RelocationBaseSection(StringRef name, uint32_t type,
1486
                                             int32_t dynamicTag,
1487
                                             int32_t sizeDynamicTag)
1488
    : SyntheticSection(SHF_ALLOC, type, config->wordsize, name),
1489
8.27k
      dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag) {}
1490
1491
void RelocationBaseSection::addReloc(RelType dynType, InputSectionBase *isec,
1492
157
                                     uint64_t offsetInSec, Symbol *sym) {
1493
157
  addReloc({dynType, isec, offsetInSec, false, sym, 0});
1494
157
}
1495
1496
void RelocationBaseSection::addReloc(RelType dynType,
1497
                                     InputSectionBase *inputSec,
1498
                                     uint64_t offsetInSec, Symbol *sym,
1499
                                     int64_t addend, RelExpr expr,
1500
498
                                     RelType type) {
1501
498
  // Write the addends to the relocated address if required. We skip
1502
498
  // it if the written value would be zero.
1503
498
  if (config->writeAddends && 
(149
expr != R_ADDEND149
||
addend != 066
))
1504
96
    inputSec->relocations.push_back({expr, type, offsetInSec, addend, sym});
1505
498
  addReloc({dynType, inputSec, offsetInSec, expr != R_ADDEND, sym, addend});
1506
498
}
1507
1508
1.09k
void RelocationBaseSection::addReloc(const DynamicReloc &reloc) {
1509
1.09k
  if (reloc.type == target->relativeRel)
1510
211
    ++numRelativeRelocs;
1511
1.09k
  relocs.push_back(reloc);
1512
1.09k
}
1513
1514
587
void RelocationBaseSection::finalizeContents() {
1515
587
  SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1516
587
1517
587
  // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1518
587
  // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1519
587
  // case.
1520
587
  if (symTab && symTab->getParent())
1521
558
    getParent()->link = symTab->getParent()->sectionIndex;
1522
29
  else
1523
29
    getParent()->link = 0;
1524
587
1525
587
  if (in.relaPlt == this)
1526
260
    getParent()->info = in.gotPlt->getParent()->sectionIndex;
1527
587
  if (in.relaIplt == this)
1528
46
    getParent()->info = in.igotPlt->getParent()->sectionIndex;
1529
587
}
1530
1531
RelrBaseSection::RelrBaseSection()
1532
    : SyntheticSection(SHF_ALLOC,
1533
                       config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR,
1534
6
                       config->wordsize, ".relr.dyn") {}
1535
1536
template <class ELFT>
1537
static void encodeDynamicReloc(SymbolTableBaseSection *symTab,
1538
                               typename ELFT::Rela *p,
1539
1.13k
                               const DynamicReloc &rel) {
1540
1.13k
  if (config->isRela)
1541
799
    p->r_addend = rel.computeAddend();
1542
1.13k
  p->r_offset = rel.getOffset();
1543
1.13k
  p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL);
1544
1.13k
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SymbolTableBaseSection*, llvm::object::ELFType<(llvm::support::endianness)1, false>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1539
298
                               const DynamicReloc &rel) {
1540
298
  if (config->isRela)
1541
21
    p->r_addend = rel.computeAddend();
1542
298
  p->r_offset = rel.getOffset();
1543
298
  p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL);
1544
298
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SymbolTableBaseSection*, llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1539
78
                               const DynamicReloc &rel) {
1540
78
  if (config->isRela)
1541
34
    p->r_addend = rel.computeAddend();
1542
78
  p->r_offset = rel.getOffset();
1543
78
  p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL);
1544
78
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SymbolTableBaseSection*, llvm::object::ELFType<(llvm::support::endianness)1, true>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1539
711
                               const DynamicReloc &rel) {
1540
711
  if (config->isRela)
1541
711
    p->r_addend = rel.computeAddend();
1542
711
  p->r_offset = rel.getOffset();
1543
711
  p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL);
1544
711
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SymbolTableBaseSection*, llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1539
52
                               const DynamicReloc &rel) {
1540
52
  if (config->isRela)
1541
33
    p->r_addend = rel.computeAddend();
1542
52
  p->r_offset = rel.getOffset();
1543
52
  p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL);
1544
52
}
1545
1546
template <class ELFT>
1547
RelocationSection<ELFT>::RelocationSection(StringRef name, bool sort)
1548
    : RelocationBaseSection(name, config->isRela ? SHT_RELA : SHT_REL,
1549
                            config->isRela ? DT_RELA : DT_REL,
1550
                            config->isRela ? DT_RELASZ : DT_RELSZ),
1551
8.27k
      sort(sort) {
1552
8.27k
  this->entsize = config->isRela ? 
sizeof(Elf_Rela)6.77k
:
sizeof(Elf_Rel)1.49k
;
1553
8.27k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1551
1.13k
      sort(sort) {
1552
1.13k
  this->entsize = config->isRela ? 
sizeof(Elf_Rela)159
:
sizeof(Elf_Rel)972
;
1553
1.13k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1551
471
      sort(sort) {
1552
471
  this->entsize = config->isRela ? 
sizeof(Elf_Rela)93
:
sizeof(Elf_Rel)378
;
1553
471
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1551
6.34k
      sort(sort) {
1552
6.34k
  this->entsize = config->isRela ? 
sizeof(Elf_Rela)6.33k
:
sizeof(Elf_Rel)9
;
1553
6.34k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1551
324
      sort(sort) {
1552
324
  this->entsize = config->isRela ? 
sizeof(Elf_Rela)186
:
sizeof(Elf_Rel)138
;
1553
324
}
1554
1555
575
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1556
575
  SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1557
575
1558
575
  // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1559
575
  // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1560
575
  // is to make results easier to read.
1561
575
  if (sort)
1562
270
    llvm::stable_sort(
1563
551
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
551
          return std::make_tuple(a.type != target->relativeRel,
1565
551
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
551
                 std::make_tuple(b.type != target->relativeRel,
1567
551
                                 b.getSymIndex(symTab), b.getOffset());
1568
551
        });
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)::'lambda'(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&)::operator()(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&) const
Line
Count
Source
1563
150
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
150
          return std::make_tuple(a.type != target->relativeRel,
1565
150
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
150
                 std::make_tuple(b.type != target->relativeRel,
1567
150
                                 b.getSymIndex(symTab), b.getOffset());
1568
150
        });
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)::'lambda'(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&)::operator()(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&) const
Line
Count
Source
1563
110
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
110
          return std::make_tuple(a.type != target->relativeRel,
1565
110
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
110
                 std::make_tuple(b.type != target->relativeRel,
1567
110
                                 b.getSymIndex(symTab), b.getOffset());
1568
110
        });
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)::'lambda'(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&)::operator()(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&) const
Line
Count
Source
1563
261
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
261
          return std::make_tuple(a.type != target->relativeRel,
1565
261
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
261
                 std::make_tuple(b.type != target->relativeRel,
1567
261
                                 b.getSymIndex(symTab), b.getOffset());
1568
261
        });
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)::'lambda'(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&)::operator()(lld::elf::DynamicReloc const&, lld::elf::DynamicReloc const&) const
Line
Count
Source
1563
30
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
30
          return std::make_tuple(a.type != target->relativeRel,
1565
30
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
30
                 std::make_tuple(b.type != target->relativeRel,
1567
30
                                 b.getSymIndex(symTab), b.getOffset());
1568
30
        });
1569
575
1570
1.01k
  for (const DynamicReloc &rel : relocs) {
1571
1.01k
    encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel);
1572
1.01k
    buf += config->isRela ? 
sizeof(Elf_Rela)729
:
sizeof(Elf_Rel)286
;
1573
1.01k
  }
1574
575
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1555
109
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1556
109
  SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1557
109
1558
109
  // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1559
109
  // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1560
109
  // is to make results easier to read.
1561
109
  if (sort)
1562
60
    llvm::stable_sort(
1563
60
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
60
          return std::make_tuple(a.type != target->relativeRel,
1565
60
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
60
                 std::make_tuple(b.type != target->relativeRel,
1567
60
                                 b.getSymIndex(symTab), b.getOffset());
1568
60
        });
1569
109
1570
244
  for (const DynamicReloc &rel : relocs) {
1571
244
    encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel);
1572
244
    buf += config->isRela ? 
sizeof(Elf_Rela)21
:
sizeof(Elf_Rel)223
;
1573
244
  }
1574
109
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1555
35
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1556
35
  SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1557
35
1558
35
  // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1559
35
  // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1560
35
  // is to make results easier to read.
1561
35
  if (sort)
1562
16
    llvm::stable_sort(
1563
16
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
16
          return std::make_tuple(a.type != target->relativeRel,
1565
16
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
16
                 std::make_tuple(b.type != target->relativeRel,
1567
16
                                 b.getSymIndex(symTab), b.getOffset());
1568
16
        });
1569
35
1570
78
  for (const DynamicReloc &rel : relocs) {
1571
78
    encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel);
1572
78
    buf += config->isRela ? 
sizeof(Elf_Rela)34
:
sizeof(Elf_Rel)44
;
1573
78
  }
1574
35
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1555
400
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1556
400
  SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1557
400
1558
400
  // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1559
400
  // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1560
400
  // is to make results easier to read.
1561
400
  if (sort)
1562
175
    llvm::stable_sort(
1563
175
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
175
          return std::make_tuple(a.type != target->relativeRel,
1565
175
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
175
                 std::make_tuple(b.type != target->relativeRel,
1567
175
                                 b.getSymIndex(symTab), b.getOffset());
1568
175
        });
1569
400
1570
641
  for (const DynamicReloc &rel : relocs) {
1571
641
    encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel);
1572
641
    buf += config->isRela ? sizeof(Elf_Rela) : 
sizeof(Elf_Rel)0
;
1573
641
  }
1574
400
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1555
31
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1556
31
  SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1557
31
1558
31
  // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1559
31
  // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1560
31
  // is to make results easier to read.
1561
31
  if (sort)
1562
19
    llvm::stable_sort(
1563
19
        relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1564
19
          return std::make_tuple(a.type != target->relativeRel,
1565
19
                                 a.getSymIndex(symTab), a.getOffset()) <
1566
19
                 std::make_tuple(b.type != target->relativeRel,
1567
19
                                 b.getSymIndex(symTab), b.getOffset());
1568
19
        });
1569
31
1570
52
  for (const DynamicReloc &rel : relocs) {
1571
52
    encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel);
1572
52
    buf += config->isRela ? 
sizeof(Elf_Rela)33
:
sizeof(Elf_Rel)19
;
1573
52
  }
1574
31
}
1575
1576
template <class ELFT>
1577
AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1578
    StringRef name)
1579
    : RelocationBaseSection(
1580
          name, config->isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1581
          config->isRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1582
7
          config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1583
7
  this->entsize = 1;
1584
7
}
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::AndroidPackedRelocationSection(llvm::StringRef)
Line
Count
Source
1582
1
          config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1583
1
  this->entsize = 1;
1584
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
1582
6
          config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1583
6
  this->entsize = 1;
1584
6
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::AndroidPackedRelocationSection(llvm::StringRef)
1585
1586
template <class ELFT>
1587
14
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1588
14
  // This function computes the contents of an Android-format packed relocation
1589
14
  // section.
1590
14
  //
1591
14
  // This format compresses relocations by using relocation groups to factor out
1592
14
  // fields that are common between relocations and storing deltas from previous
1593
14
  // relocations in SLEB128 format (which has a short representation for small
1594
14
  // numbers). A good example of a relocation type with common fields is
1595
14
  // R_*_RELATIVE, which is normally used to represent function pointers in
1596
14
  // vtables. In the REL format, each relative relocation has the same r_info
1597
14
  // field, and is only different from other relative relocations in terms of
1598
14
  // the r_offset field. By sorting relocations by offset, grouping them by
1599
14
  // r_info and representing each relocation with only the delta from the
1600
14
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1601
14
  // byte (or less with run-length encoding). This relocation packer was able to
1602
14
  // reduce the size of the relocation section in an Android Chromium DSO from
1603
14
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1604
14
  //
1605
14
  // A relocation section consists of a header containing the literal bytes
1606
14
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1607
14
  // elements are the total number of relocations in the section and an initial
1608
14
  // r_offset value. The remaining elements define a sequence of relocation
1609
14
  // groups. Each relocation group starts with a header consisting of the
1610
14
  // following elements:
1611
14
  //
1612
14
  // - the number of relocations in the relocation group
1613
14
  // - flags for the relocation group
1614
14
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1615
14
  //   for each relocation in the group.
1616
14
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1617
14
  //   field for each relocation in the group.
1618
14
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1619
14
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1620
14
  //   each relocation in the group.
1621
14
  //
1622
14
  // Following the relocation group header are descriptions of each of the
1623
14
  // relocations in the group. They consist of the following elements:
1624
14
  //
1625
14
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1626
14
  //   delta for this relocation.
1627
14
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1628
14
  //   field for this relocation.
1629
14
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1630
14
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1631
14
  //   this relocation.
1632
14
1633
14
  size_t oldSize = relocData.size();
1634
14
1635
14
  relocData = {'A', 'P', 'S', '2'};
1636
14
  raw_svector_ostream os(relocData);
1637
278
  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
1637
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
1637
208
  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
1638
14
1639
14
  // The format header includes the number of relocations and the initial
1640
14
  // offset (we set this to zero because the first relocation group will
1641
14
  // perform the initial adjustment).
1642
14
  add(relocs.size());
1643
14
  add(0);
1644
14
1645
14
  std::vector<Elf_Rela> relatives, nonRelatives;
1646
14
1647
124
  for (const DynamicReloc &rel : relocs) {
1648
124
    Elf_Rela r;
1649
124
    encodeDynamicReloc<ELFT>(getPartition().dynSymTab, &r, rel);
1650
124
1651
124
    if (r.getType(config->isMips64EL) == target->relativeRel)
1652
106
      relatives.push_back(r);
1653
18
    else
1654
18
      nonRelatives.push_back(r);
1655
124
  }
1656
14
1657
100
  llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1658
100
    return a.r_offset < b.r_offset;
1659
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
1657
48
  llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1658
48
    return a.r_offset < b.r_offset;
1659
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
1657
52
  llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1658
52
    return a.r_offset < b.r_offset;
1659
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
1660
14
1661
14
  // Try to find groups of relative relocations which are spaced one word
1662
14
  // apart from one another. These generally correspond to vtable entries. The
1663
14
  // format allows these groups to be encoded using a sort of run-length
1664
14
  // encoding, but each group will cost 7 bytes in addition to the offset from
1665
14
  // the previous group, so it is only profitable to do this for groups of
1666
14
  // size 8 or larger.
1667
14
  std::vector<Elf_Rela> ungroupedRelatives;
1668
14
  std::vector<std::vector<Elf_Rela>> relativeGroups;
1669
32
  for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1670
18
    std::vector<Elf_Rela> group;
1671
106
    do {
1672
106
      group.push_back(*i++);
1673
106
    } while (i != e && 
(i - 1)->r_offset + config->wordsize == i->r_offset100
);
1674
18
1675
18
    if (group.size() < 8)
1676
10
      ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1677
10
                                group.end());
1678
8
    else
1679
8
      relativeGroups.emplace_back(std::move(group));
1680
18
  }
1681
14
1682
14
  unsigned hasAddendIfRela =
1683
14
      config->isRela ? 
RELOCATION_GROUP_HAS_ADDEND_FLAG12
:
02
;
1684
14
1685
14
  uint64_t offset = 0;
1686
14
  uint64_t addend = 0;
1687
14
1688
14
  // Emit the run-length encoding for the groups of adjacent relative
1689
14
  // relocations. Each group is represented using two groups in the packed
1690
14
  // format. The first is used to set the current offset to the start of the
1691
14
  // group (and also encodes the first relocation), and the second encodes the
1692
14
  // remaining relocations.
1693
14
  for (std::vector<Elf_Rela> &g : relativeGroups) {
1694
8
    // The first relocation in the group.
1695
8
    add(1);
1696
8
    add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1697
8
        RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1698
8
    add(g[0].r_offset - offset);
1699
8
    add(target->relativeRel);
1700
8
    if (config->isRela) {
1701
4
      add(g[0].r_addend - addend);
1702
4
      addend = g[0].r_addend;
1703
4
    }
1704
8
1705
8
    // The remaining relocations.
1706
8
    add(g.size() - 1);
1707
8
    add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1708
8
        RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1709
8
    add(config->wordsize);
1710
8
    add(target->relativeRel);
1711
8
    if (config->isRela) {
1712
34
      for (auto i = g.begin() + 1, e = g.end(); i != e; 
++i30
) {
1713
30
        add(i->r_addend - addend);
1714
30
        addend = i->r_addend;
1715
30
      }
1716
4
    }
1717
8
1718
8
    offset = g.back().r_offset;
1719
8
  }
1720
14
1721
14
  // Now the ungrouped relatives.
1722
14
  if (!ungroupedRelatives.empty()) {
1723
6
    add(ungroupedRelatives.size());
1724
6
    add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1725
6
    add(target->relativeRel);
1726
38
    for (Elf_Rela &r : ungroupedRelatives) {
1727
38
      add(r.r_offset - offset);
1728
38
      offset = r.r_offset;
1729
38
      if (config->isRela) {
1730
22
        add(r.r_addend - addend);
1731
22
        addend = r.r_addend;
1732
22
      }
1733
38
    }
1734
6
  }
1735
14
1736
14
  // Finally the non-relative relocations.
1737
14
  llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1738
6
    return a.r_offset < b.r_offset;
1739
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
1737
2
  llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1738
2
    return a.r_offset < b.r_offset;
1739
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
1737
4
  llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1738
4
    return a.r_offset < b.r_offset;
1739
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
1740
14
  if (!nonRelatives.empty()) {
1741
12
    add(nonRelatives.size());
1742
12
    add(hasAddendIfRela);
1743
18
    for (Elf_Rela &r : nonRelatives) {
1744
18
      add(r.r_offset - offset);
1745
18
      offset = r.r_offset;
1746
18
      add(r.r_info);
1747
18
      if (config->isRela) {
1748
14
        add(r.r_addend - addend);
1749
14
        addend = r.r_addend;
1750
14
      }
1751
18
    }
1752
12
  }
1753
14
1754
14
  // Don't allow the section to shrink; otherwise the size of the section can
1755
14
  // oscillate infinitely.
1756
14
  if (relocData.size() < oldSize)
1757
1
    relocData.append(oldSize - relocData.size(), 0);
1758
14
1759
14
  // Returns whether the section size changed. We need to keep recomputing both
1760
14
  // section layout and the contents of this section until the size converges
1761
14
  // because changing this section's size can affect section layout, which in
1762
14
  // turn can affect the sizes of the LEB-encoded integers stored in this
1763
14
  // section.
1764
14
  return relocData.size() != oldSize;
1765
14
}
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()
Line
Count
Source
1587
2
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1588
2
  // This function computes the contents of an Android-format packed relocation
1589
2
  // section.
1590
2
  //
1591
2
  // This format compresses relocations by using relocation groups to factor out
1592
2
  // fields that are common between relocations and storing deltas from previous
1593
2
  // relocations in SLEB128 format (which has a short representation for small
1594
2
  // numbers). A good example of a relocation type with common fields is
1595
2
  // R_*_RELATIVE, which is normally used to represent function pointers in
1596
2
  // vtables. In the REL format, each relative relocation has the same r_info
1597
2
  // field, and is only different from other relative relocations in terms of
1598
2
  // the r_offset field. By sorting relocations by offset, grouping them by
1599
2
  // r_info and representing each relocation with only the delta from the
1600
2
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1601
2
  // byte (or less with run-length encoding). This relocation packer was able to
1602
2
  // reduce the size of the relocation section in an Android Chromium DSO from
1603
2
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1604
2
  //
1605
2
  // A relocation section consists of a header containing the literal bytes
1606
2
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1607
2
  // elements are the total number of relocations in the section and an initial
1608
2
  // r_offset value. The remaining elements define a sequence of relocation
1609
2
  // groups. Each relocation group starts with a header consisting of the
1610
2
  // following elements:
1611
2
  //
1612
2
  // - the number of relocations in the relocation group
1613
2
  // - flags for the relocation group
1614
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1615
2
  //   for each relocation in the group.
1616
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1617
2
  //   field for each relocation in the group.
1618
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1619
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1620
2
  //   each relocation in the group.
1621
2
  //
1622
2
  // Following the relocation group header are descriptions of each of the
1623
2
  // relocations in the group. They consist of the following elements:
1624
2
  //
1625
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1626
2
  //   delta for this relocation.
1627
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1628
2
  //   field for this relocation.
1629
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1630
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1631
2
  //   this relocation.
1632
2
1633
2
  size_t oldSize = relocData.size();
1634
2
1635
2
  relocData = {'A', 'P', 'S', '2'};
1636
2
  raw_svector_ostream os(relocData);
1637
2
  auto add = [&](int64_t v) { encodeSLEB128(v, os); };
1638
2
1639
2
  // The format header includes the number of relocations and the initial
1640
2
  // offset (we set this to zero because the first relocation group will
1641
2
  // perform the initial adjustment).
1642
2
  add(relocs.size());
1643
2
  add(0);
1644
2
1645
2
  std::vector<Elf_Rela> relatives, nonRelatives;
1646
2
1647
54
  for (const DynamicReloc &rel : relocs) {
1648
54
    Elf_Rela r;
1649
54
    encodeDynamicReloc<ELFT>(getPartition().dynSymTab, &r, rel);
1650
54
1651
54
    if (r.getType(config->isMips64EL) == target->relativeRel)
1652
50
      relatives.push_back(r);
1653
4
    else
1654
4
      nonRelatives.push_back(r);
1655
54
  }
1656
2
1657
2
  llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1658
2
    return a.r_offset < b.r_offset;
1659
2
  });
1660
2
1661
2
  // Try to find groups of relative relocations which are spaced one word
1662
2
  // apart from one another. These generally correspond to vtable entries. The
1663
2
  // format allows these groups to be encoded using a sort of run-length
1664
2
  // encoding, but each group will cost 7 bytes in addition to the offset from
1665
2
  // the previous group, so it is only profitable to do this for groups of
1666
2
  // size 8 or larger.
1667
2
  std::vector<Elf_Rela> ungroupedRelatives;
1668
2
  std::vector<std::vector<Elf_Rela>> relativeGroups;
1669
10
  for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1670
8
    std::vector<Elf_Rela> group;
1671
50
    do {
1672
50
      group.push_back(*i++);
1673
50
    } while (i != e && 
(i - 1)->r_offset + config->wordsize == i->r_offset48
);
1674
8
1675
8
    if (group.size() < 8)
1676
4
      ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1677
4
                                group.end());
1678
4
    else
1679
4
      relativeGroups.emplace_back(std::move(group));
1680
8
  }
1681
2
1682
2
  unsigned hasAddendIfRela =
1683
2
      config->isRela ? 
RELOCATION_GROUP_HAS_ADDEND_FLAG0
: 0;
1684
2
1685
2
  uint64_t offset = 0;
1686
2
  uint64_t addend = 0;
1687
2
1688
2
  // Emit the run-length encoding for the groups of adjacent relative
1689
2
  // relocations. Each group is represented using two groups in the packed
1690
2
  // format. The first is used to set the current offset to the start of the
1691
2
  // group (and also encodes the first relocation), and the second encodes the
1692
2
  // remaining relocations.
1693
4
  for (std::vector<Elf_Rela> &g : relativeGroups) {
1694
4
    // The first relocation in the group.
1695
4
    add(1);
1696
4
    add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1697
4
        RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1698
4
    add(g[0].r_offset - offset);
1699
4
    add(target->relativeRel);
1700
4
    if (config->isRela) {
1701
0
      add(g[0].r_addend - addend);
1702
0
      addend = g[0].r_addend;
1703
0
    }
1704
4
1705
4
    // The remaining relocations.
1706
4
    add(g.size() - 1);
1707
4
    add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1708
4
        RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1709
4
    add(config->wordsize);
1710
4
    add(target->relativeRel);
1711
4
    if (config->isRela) {
1712
0
      for (auto i = g.begin() + 1, e = g.end(); i != e; ++i) {
1713
0
        add(i->r_addend - addend);
1714
0
        addend = i->r_addend;
1715
0
      }
1716
0
    }
1717
4
1718
4
    offset = g.back().r_offset;
1719
4
  }
1720
2
1721
2
  // Now the ungrouped relatives.
1722
2
  if (!ungroupedRelatives.empty()) {
1723
2
    add(ungroupedRelatives.size());
1724
2
    add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1725
2
    add(target->relativeRel);
1726
16
    for (Elf_Rela &r : ungroupedRelatives) {
1727
16
      add(r.r_offset - offset);
1728
16
      offset = r.r_offset;
1729
16
      if (config->isRela) {
1730
0
        add(r.r_addend - addend);
1731
0
        addend = r.r_addend;
1732
0
      }
1733
16
    }
1734
2
  }
1735
2
1736
2
  // Finally the non-relative relocations.
1737
2
  llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1738
2
    return a.r_offset < b.r_offset;
1739
2
  });
1740
2
  if (!nonRelatives.empty()) {
1741
2
    add(nonRelatives.size());
1742
2
    add(hasAddendIfRela);
1743
4
    for (Elf_Rela &r : nonRelatives) {
1744
4
      add(r.r_offset - offset);
1745
4
      offset = r.r_offset;
1746
4
      add(r.r_info);
1747
4
      if (config->isRela) {
1748
0
        add(r.r_addend - addend);
1749
0
        addend = r.r_addend;
1750
0
      }
1751
4
    }
1752
2
  }
1753
2
1754
2
  // Don't allow the section to shrink; otherwise the size of the section can
1755
2
  // oscillate infinitely.
1756
2
  if (relocData.size() < oldSize)
1757
0
    relocData.append(oldSize - relocData.size(), 0);
1758
2
1759
2
  // Returns whether the section size changed. We need to keep recomputing both
1760
2
  // section layout and the contents of this section until the size converges
1761
2
  // because changing this section's size can affect section layout, which in
1762
2
  // turn can affect the sizes of the LEB-encoded integers stored in this
1763
2
  // section.
1764
2
  return relocData.size() != oldSize;
1765
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
1587
12
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1588
12
  // This function computes the contents of an Android-format packed relocation
1589
12
  // section.
1590
12
  //
1591
12
  // This format compresses relocations by using relocation groups to factor out
1592
12
  // fields that are common between relocations and storing deltas from previous
1593
12
  // relocations in SLEB128 format (which has a short representation for small
1594
12
  // numbers). A good example of a relocation type with common fields is
1595
12
  // R_*_RELATIVE, which is normally used to represent function pointers in
1596
12
  // vtables. In the REL format, each relative relocation has the same r_info
1597
12
  // field, and is only different from other relative relocations in terms of
1598
12
  // the r_offset field. By sorting relocations by offset, grouping them by
1599
12
  // r_info and representing each relocation with only the delta from the
1600
12
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1601
12
  // byte (or less with run-length encoding). This relocation packer was able to
1602
12
  // reduce the size of the relocation section in an Android Chromium DSO from
1603
12
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1604
12
  //
1605
12
  // A relocation section consists of a header containing the literal bytes
1606
12
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1607
12
  // elements are the total number of relocations in the section and an initial
1608
12
  // r_offset value. The remaining elements define a sequence of relocation
1609
12
  // groups. Each relocation group starts with a header consisting of the
1610
12
  // following elements:
1611
12
  //
1612
12
  // - the number of relocations in the relocation group
1613
12
  // - flags for the relocation group
1614
12
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1615
12
  //   for each relocation in the group.
1616
12
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1617
12
  //   field for each relocation in the group.
1618
12
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1619
12
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1620
12
  //   each relocation in the group.
1621
12
  //
1622
12
  // Following the relocation group header are descriptions of each of the
1623
12
  // relocations in the group. They consist of the following elements:
1624
12
  //
1625
12
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1626
12
  //   delta for this relocation.
1627
12
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1628
12
  //   field for this relocation.
1629
12
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1630
12
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1631
12
  //   this relocation.
1632
12
1633
12
  size_t oldSize = relocData.size();
1634
12
1635
12
  relocData = {'A', 'P', 'S', '2'};
1636
12
  raw_svector_ostream os(relocData);
1637
12
  auto add = [&](int64_t v) { encodeSLEB128(v, os); };
1638
12
1639
12
  // The format header includes the number of relocations and the initial
1640
12
  // offset (we set this to zero because the first relocation group will
1641
12
  // perform the initial adjustment).
1642
12
  add(relocs.size());
1643
12
  add(0);
1644
12
1645
12
  std::vector<Elf_Rela> relatives, nonRelatives;
1646
12
1647
70
  for (const DynamicReloc &rel : relocs) {
1648
70
    Elf_Rela r;
1649
70
    encodeDynamicReloc<ELFT>(getPartition().dynSymTab, &r, rel);
1650
70
1651
70
    if (r.getType(config->isMips64EL) == target->relativeRel)
1652
56
      relatives.push_back(r);
1653
14
    else
1654
14
      nonRelatives.push_back(r);
1655
70
  }
1656
12
1657
12
  llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1658
12
    return a.r_offset < b.r_offset;
1659
12
  });
1660
12
1661
12
  // Try to find groups of relative relocations which are spaced one word
1662
12
  // apart from one another. These generally correspond to vtable entries. The
1663
12
  // format allows these groups to be encoded using a sort of run-length
1664
12
  // encoding, but each group will cost 7 bytes in addition to the offset from
1665
12
  // the previous group, so it is only profitable to do this for groups of
1666
12
  // size 8 or larger.
1667
12
  std::vector<Elf_Rela> ungroupedRelatives;
1668
12
  std::vector<std::vector<Elf_Rela>> relativeGroups;
1669
22
  for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1670
10
    std::vector<Elf_Rela> group;
1671
56
    do {
1672
56
      group.push_back(*i++);
1673
56
    } while (i != e && 
(i - 1)->r_offset + config->wordsize == i->r_offset52
);
1674
10
1675
10
    if (group.size() < 8)
1676
6
      ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1677
6
                                group.end());
1678
4
    else
1679
4
      relativeGroups.emplace_back(std::move(group));
1680
10
  }
1681
12
1682
12
  unsigned hasAddendIfRela =
1683
12
      config->isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 
00
;
1684
12
1685
12
  uint64_t offset = 0;
1686
12
  uint64_t addend = 0;
1687
12
1688
12
  // Emit the run-length encoding for the groups of adjacent relative
1689
12
  // relocations. Each group is represented using two groups in the packed
1690
12
  // format. The first is used to set the current offset to the start of the
1691
12
  // group (and also encodes the first relocation), and the second encodes the
1692
12
  // remaining relocations.
1693
12
  for (std::vector<Elf_Rela> &g : relativeGroups) {
1694
4
    // The first relocation in the group.
1695
4
    add(1);
1696
4
    add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1697
4
        RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1698
4
    add(g[0].r_offset - offset);
1699
4
    add(target->relativeRel);
1700
4
    if (config->isRela) {
1701
4
      add(g[0].r_addend - addend);
1702
4
      addend = g[0].r_addend;
1703
4
    }
1704
4
1705
4
    // The remaining relocations.
1706
4
    add(g.size() - 1);
1707
4
    add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1708
4
        RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1709
4
    add(config->wordsize);
1710
4
    add(target->relativeRel);
1711
4
    if (config->isRela) {
1712
34
      for (auto i = g.begin() + 1, e = g.end(); i != e; 
++i30
) {
1713
30
        add(i->r_addend - addend);
1714
30
        addend = i->r_addend;
1715
30
      }
1716
4
    }
1717
4
1718
4
    offset = g.back().r_offset;
1719
4
  }
1720
12
1721
12
  // Now the ungrouped relatives.
1722
12
  if (!ungroupedRelatives.empty()) {
1723
4
    add(ungroupedRelatives.size());
1724
4
    add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1725
4
    add(target->relativeRel);
1726
22
    for (Elf_Rela &r : ungroupedRelatives) {
1727
22
      add(r.r_offset - offset);
1728
22
      offset = r.r_offset;
1729
22
      if (config->isRela) {
1730
22
        add(r.r_addend - addend);
1731
22
        addend = r.r_addend;
1732
22
      }
1733
22
    }
1734
4
  }
1735
12
1736
12
  // Finally the non-relative relocations.
1737
12
  llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1738
12
    return a.r_offset < b.r_offset;
1739
12
  });
1740
12
  if (!nonRelatives.empty()) {
1741
10
    add(nonRelatives.size());
1742
10
    add(hasAddendIfRela);
1743
14
    for (Elf_Rela &r : nonRelatives) {
1744
14
      add(r.r_offset - offset);
1745
14
      offset = r.r_offset;
1746
14
      add(r.r_info);
1747
14
      if (config->isRela) {
1748
14
        add(r.r_addend - addend);
1749
14
        addend = r.r_addend;
1750
14
      }
1751
14
    }
1752
10
  }
1753
12
1754
12
  // Don't allow the section to shrink; otherwise the size of the section can
1755
12
  // oscillate infinitely.
1756
12
  if (relocData.size() < oldSize)
1757
1
    relocData.append(oldSize - relocData.size(), 0);
1758
12
1759
12
  // Returns whether the section size changed. We need to keep recomputing both
1760
12
  // section layout and the contents of this section until the size converges
1761
12
  // because changing this section's size can affect section layout, which in
1762
12
  // turn can affect the sizes of the LEB-encoded integers stored in this
1763
12
  // section.
1764
12
  return relocData.size() != oldSize;
1765
12
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()
1766
1767
6
template <class ELFT> RelrSection<ELFT>::RelrSection() {
1768
6
  this->entsize = config->wordsize;
1769
6
}
lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelrSection()
Line
Count
Source
1767
2
template <class ELFT> RelrSection<ELFT>::RelrSection() {
1768
2
  this->entsize = config->wordsize;
1769
2
}
Unexecuted instantiation: lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::RelrSection()
lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RelrSection()
Line
Count
Source
1767
4
template <class ELFT> RelrSection<ELFT>::RelrSection() {
1768
4
  this->entsize = config->wordsize;
1769
4
}
Unexecuted instantiation: lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelrSection()
1770
1771
11
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1772
11
  // This function computes the contents of an SHT_RELR packed relocation
1773
11
  // section.
1774
11
  //
1775
11
  // Proposal for adding SHT_RELR sections to generic-abi is here:
1776
11
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1777
11
  //
1778
11
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1779
11
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1780
11
  //
1781
11
  // i.e. start with an address, followed by any number of bitmaps. The address
1782
11
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1783
11
  // relocations each, at subsequent offsets following the last address entry.
1784
11
  //
1785
11
  // The bitmap entries must have 1 in the least significant bit. The assumption
1786
11
  // here is that an address cannot have 1 in lsb. Odd addresses are not
1787
11
  // supported.
1788
11
  //
1789
11
  // Excluding the least significant bit in the bitmap, each non-zero bit in
1790
11
  // the bitmap represents a relocation to be applied to a corresponding machine
1791
11
  // word that follows the base address word. The second least significant bit
1792
11
  // represents the machine word immediately following the initial address, and
1793
11
  // each bit that follows represents the next word, in linear order. As such,
1794
11
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1795
11
  // 63 relocations in a 64-bit object.
1796
11
  //
1797
11
  // This encoding has a couple of interesting properties:
1798
11
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1799
11
  //    even means address, odd means bitmap.
1800
11
  // 2. Just a simple list of addresses is a valid encoding.
1801
11
1802
11
  size_t oldSize = relrRelocs.size();
1803
11
  relrRelocs.clear();
1804
11
1805
11
  // Same as Config->Wordsize but faster because this is a compile-time
1806
11
  // constant.
1807
11
  const size_t wordsize = sizeof(typename ELFT::uint);
1808
11
1809
11
  // Number of bits to use for the relocation offsets bitmap.
1810
11
  // Must be either 63 or 31.
1811
11
  const size_t nBits = wordsize * 8 - 1;
1812
11
1813
11
  // Get offsets for all relative relocations and sort them.
1814
11
  std::vector<uint64_t> offsets;
1815
11
  for (const RelativeReloc &rel : relocs)
1816
168
    offsets.push_back(rel.getOffset());
1817
11
  llvm::sort(offsets);
1818
11
1819
11
  // For each leading relocation, find following ones that can be folded
1820
11
  // as a bitmap and fold them.
1821
21
  for (size_t i = 0, e = offsets.size(); i < e;) {
1822
10
    // Add a leading relocation.
1823
10
    relrRelocs.push_back(Elf_Relr(offsets[i]));
1824
10
    uint64_t base = offsets[i] + wordsize;
1825
10
    ++i;
1826
10
1827
10
    // Find foldable relocations to construct bitmaps.
1828
18
    while (i < e) {
1829
8
      uint64_t bitmap = 0;
1830
8
1831
166
      while (i < e) {
1832
160
        uint64_t delta = offsets[i] - base;
1833
160
1834
160
        // If it is too far, it cannot be folded.
1835
160
        if (delta >= nBits * wordsize)
1836
2
          break;
1837
158
1838
158
        // If it is not a multiple of wordsize away, it cannot be folded.
1839
158
        if (delta % wordsize)
1840
0
          break;
1841
158
1842
158
        // Fold it.
1843
158
        bitmap |= 1ULL << (delta / wordsize);
1844
158
        ++i;
1845
158
      }
1846
8
1847
8
      if (!bitmap)
1848
0
        break;
1849
8
1850
8
      relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
1851
8
      base += nBits * wordsize;
1852
8
    }
1853
10
  }
1854
11
1855
11
  return relrRelocs.size() != oldSize;
1856
11
}
lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()
Line
Count
Source
1771
4
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1772
4
  // This function computes the contents of an SHT_RELR packed relocation
1773
4
  // section.
1774
4
  //
1775
4
  // Proposal for adding SHT_RELR sections to generic-abi is here:
1776
4
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1777
4
  //
1778
4
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1779
4
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1780
4
  //
1781
4
  // i.e. start with an address, followed by any number of bitmaps. The address
1782
4
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1783
4
  // relocations each, at subsequent offsets following the last address entry.
1784
4
  //
1785
4
  // The bitmap entries must have 1 in the least significant bit. The assumption
1786
4
  // here is that an address cannot have 1 in lsb. Odd addresses are not
1787
4
  // supported.
1788
4
  //
1789
4
  // Excluding the least significant bit in the bitmap, each non-zero bit in
1790
4
  // the bitmap represents a relocation to be applied to a corresponding machine
1791
4
  // word that follows the base address word. The second least significant bit
1792
4
  // represents the machine word immediately following the initial address, and
1793
4
  // each bit that follows represents the next word, in linear order. As such,
1794
4
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1795
4
  // 63 relocations in a 64-bit object.
1796
4
  //
1797
4
  // This encoding has a couple of interesting properties:
1798
4
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1799
4
  //    even means address, odd means bitmap.
1800
4
  // 2. Just a simple list of addresses is a valid encoding.
1801
4
1802
4
  size_t oldSize = relrRelocs.size();
1803
4
  relrRelocs.clear();
1804
4
1805
4
  // Same as Config->Wordsize but faster because this is a compile-time
1806
4
  // constant.
1807
4
  const size_t wordsize = sizeof(typename ELFT::uint);
1808
4
1809
4
  // Number of bits to use for the relocation offsets bitmap.
1810
4
  // Must be either 63 or 31.
1811
4
  const size_t nBits = wordsize * 8 - 1;
1812
4
1813
4
  // Get offsets for all relative relocations and sort them.
1814
4
  std::vector<uint64_t> offsets;
1815
4
  for (const RelativeReloc &rel : relocs)
1816
116
    offsets.push_back(rel.getOffset());
1817
4
  llvm::sort(offsets);
1818
4
1819
4
  // For each leading relocation, find following ones that can be folded
1820
4
  // as a bitmap and fold them.
1821
8
  for (size_t i = 0, e = offsets.size(); i < e;) {
1822
4
    // Add a leading relocation.
1823
4
    relrRelocs.push_back(Elf_Relr(offsets[i]));
1824
4
    uint64_t base = offsets[i] + wordsize;
1825
4
    ++i;
1826
4
1827
4
    // Find foldable relocations to construct bitmaps.
1828
10
    while (i < e) {
1829
6
      uint64_t bitmap = 0;
1830
6
1831
118
      while (i < e) {
1832
114
        uint64_t delta = offsets[i] - base;
1833
114
1834
114
        // If it is too far, it cannot be folded.
1835
114
        if (delta >= nBits * wordsize)
1836
2
          break;
1837
112
1838
112
        // If it is not a multiple of wordsize away, it cannot be folded.
1839
112
        if (delta % wordsize)
1840
0
          break;
1841
112
1842
112
        // Fold it.
1843
112
        bitmap |= 1ULL << (delta / wordsize);
1844
112
        ++i;
1845
112
      }
1846
6
1847
6
      if (!bitmap)
1848
0
        break;
1849
6
1850
6
      relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
1851
6
      base += nBits * wordsize;
1852
6
    }
1853
4
  }
1854
4
1855
4
  return relrRelocs.size() != oldSize;
1856
4
}
Unexecuted instantiation: lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::updateAllocSize()
lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::updateAllocSize()
Line
Count
Source
1771
7
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1772
7
  // This function computes the contents of an SHT_RELR packed relocation
1773
7
  // section.
1774
7
  //
1775
7
  // Proposal for adding SHT_RELR sections to generic-abi is here:
1776
7
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1777
7
  //
1778
7
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1779
7
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1780
7
  //
1781
7
  // i.e. start with an address, followed by any number of bitmaps. The address
1782
7
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1783
7
  // relocations each, at subsequent offsets following the last address entry.
1784
7
  //
1785
7
  // The bitmap entries must have 1 in the least significant bit. The assumption
1786
7
  // here is that an address cannot have 1 in lsb. Odd addresses are not
1787
7
  // supported.
1788
7
  //
1789
7
  // Excluding the least significant bit in the bitmap, each non-zero bit in
1790
7
  // the bitmap represents a relocation to be applied to a corresponding machine
1791
7
  // word that follows the base address word. The second least significant bit
1792
7
  // represents the machine word immediately following the initial address, and
1793
7
  // each bit that follows represents the next word, in linear order. As such,
1794
7
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1795
7
  // 63 relocations in a 64-bit object.
1796
7
  //
1797
7
  // This encoding has a couple of interesting properties:
1798
7
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1799
7
  //    even means address, odd means bitmap.
1800
7
  // 2. Just a simple list of addresses is a valid encoding.
1801
7
1802
7
  size_t oldSize = relrRelocs.size();
1803
7
  relrRelocs.clear();
1804
7
1805
7
  // Same as Config->Wordsize but faster because this is a compile-time
1806
7
  // constant.
1807
7
  const size_t wordsize = sizeof(typename ELFT::uint);
1808
7
1809
7
  // Number of bits to use for the relocation offsets bitmap.
1810
7
  // Must be either 63 or 31.
1811
7
  const size_t nBits = wordsize * 8 - 1;
1812
7
1813
7
  // Get offsets for all relative relocations and sort them.
1814
7
  std::vector<uint64_t> offsets;
1815
7
  for (const RelativeReloc &rel : relocs)
1816
52
    offsets.push_back(rel.getOffset());
1817
7
  llvm::sort(offsets);
1818
7
1819
7
  // For each leading relocation, find following ones that can be folded
1820
7
  // as a bitmap and fold them.
1821
13
  for (size_t i = 0, e = offsets.size(); i < e;) {
1822
6
    // Add a leading relocation.
1823
6
    relrRelocs.push_back(Elf_Relr(offsets[i]));
1824
6
    uint64_t base = offsets[i] + wordsize;
1825
6
    ++i;
1826
6
1827
6
    // Find foldable relocations to construct bitmaps.
1828
8
    while (i < e) {
1829
2
      uint64_t bitmap = 0;
1830
2
1831
48
      while (i < e) {
1832
46
        uint64_t delta = offsets[i] - base;
1833
46
1834
46
        // If it is too far, it cannot be folded.
1835
46
        if (delta >= nBits * wordsize)
1836
0
          break;
1837
46
1838
46
        // If it is not a multiple of wordsize away, it cannot be folded.
1839
46
        if (delta % wordsize)
1840
0
          break;
1841
46
1842
46
        // Fold it.
1843
46
        bitmap |= 1ULL << (delta / wordsize);
1844
46
        ++i;
1845
46
      }
1846
2
1847
2
      if (!bitmap)
1848
0
        break;
1849
2
1850
2
      relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
1851
2
      base += nBits * wordsize;
1852
2
    }
1853
6
  }
1854
7
1855
7
  return relrRelocs.size() != oldSize;
1856
7
}
Unexecuted instantiation: lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()
1857
1858
SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &strTabSec)
1859
    : SyntheticSection(strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1860
                       strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1861
                       config->wordsize,
1862
                       strTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1863
6.87k
      strTabSec(strTabSec) {}
1864
1865
// Orders symbols according to their positions in the GOT,
1866
// in compliance with MIPS ABI rules.
1867
// See "Global Offset Table" in Chapter 5 in the following document
1868
// for detailed description:
1869
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1870
static bool sortMipsSymbols(const SymbolTableEntry &l,
1871
153
                            const SymbolTableEntry &r) {
1872
153
  // Sort entries related to non-local preemptible symbols by GOT indexes.
1873
153
  // All other entries go to the beginning of a dynsym in arbitrary order.
1874
153
  if (l.sym->isInGot() && 
r.sym->isInGot()21
)
1875
6
    return l.sym->gotIndex < r.sym->gotIndex;
1876
147
  if (!l.sym->isInGot() && 
!r.sym->isInGot()132
)
1877
108
    return false;
1878
39
  return !l.sym->isInGot();
1879
39
}
1880
1881
3.86k
void SymbolTableBaseSection::finalizeContents() {
1882
3.86k
  if (OutputSection *sec = strTabSec.getParent())
1883
3.86k
    getParent()->link = sec->sectionIndex;
1884
3.86k
1885
3.86k
  if (this->type != SHT_DYNSYM) {
1886
2.60k
    sortSymTabSymbols();
1887
2.60k
    return;
1888
2.60k
  }
1889
1.26k
1890
1.26k
  // If it is a .dynsym, there should be no local symbols, but we need
1891
1.26k
  // to do a few things for the dynamic linker.
1892
1.26k
1893
1.26k
  // Section's Info field has the index of the first non-local symbol.
1894
1.26k
  // Because the first symbol entry is a null entry, 1 is the first.
1895
1.26k
  getParent()->info = 1;
1896
1.26k
1897
1.26k
  if (getPartition().gnuHashTab) {
1898
1.02k
    // NB: It also sorts Symbols to meet the GNU hash table requirements.
1899
1.02k
    getPartition().gnuHashTab->addSymbols(symbols);
1900
1.02k
  } else 
if (235
config->emachine == EM_MIPS235
) {
1901
100
    llvm::stable_sort(symbols, sortMipsSymbols);
1902
100
  }
1903
1.26k
1904
1.26k
  // Only the main partition's dynsym indexes are stored in the symbols
1905
1.26k
  // themselves. All other partitions use a lookup table.
1906
1.26k
  if (this == mainPart->dynSymTab) {
1907
1.25k
    size_t i = 0;
1908
1.25k
    for (const SymbolTableEntry &s : symbols)
1909
2.14k
      s.sym->dynsymIndex = ++i;
1910
1.25k
  }
1911
1.26k
}
1912
1913
// The ELF spec requires that all local symbols precede global symbols, so we
1914
// sort symbol entries in this function. (For .dynsym, we don't do that because
1915
// symbols for dynamic linking are inherently all globals.)
1916
//
1917
// Aside from above, we put local symbols in groups starting with the STT_FILE
1918
// symbol. That is convenient for purpose of identifying where are local symbols
1919
// coming from.
1920
2.60k
void SymbolTableBaseSection::sortSymTabSymbols() {
1921
2.60k
  // Move all local symbols before global symbols.
1922
2.60k
  auto e = std::stable_partition(
1923
85.8k
      symbols.begin(), symbols.end(), [](const SymbolTableEntry &s) {
1924
85.8k
        return s.sym->isLocal() || 
s.sym->computeBinding() == STB_LOCAL17.4k
;
1925
85.8k
      });
1926
2.60k
  size_t numLocals = e - symbols.begin();
1927
2.60k
  getParent()->info = numLocals + 1;
1928
2.60k
1929
2.60k
  // We want to group the local symbols by file. For that we rebuild the local
1930
2.60k
  // part of the symbols vector. We do not need to care about the STT_FILE
1931
2.60k
  // symbols, they are already naturally placed first in each group. That
1932
2.60k
  // happens because STT_FILE is always the first symbol in the object and hence
1933
2.60k
  // precede all other local symbols we add for a file.
1934
2.60k
  MapVector<InputFile *, std::vector<SymbolTableEntry>> arr;
1935
2.60k
  for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e))
1936
70.2k
    arr[s.sym->file].push_back(s);
1937
2.60k
1938
2.60k
  auto i = symbols.begin();
1939
2.60k
  for (std::pair<InputFile *, std::vector<SymbolTableEntry>> &p : arr)
1940
2.48k
    for (SymbolTableEntry &entry : p.second)
1941
70.2k
      *i++ = entry;
1942
2.60k
}
1943
1944
88.5k
void SymbolTableBaseSection::addSymbol(Symbol *b) {
1945
88.5k
  // Adding a local symbol to a .dynsym is a bug.
1946
88.5k
  assert(this->type != SHT_DYNSYM || !b->isLocal());
1947
88.5k
1948
88.5k
  bool hashIt = b->isLocal();
1949
88.5k
  symbols.push_back({b, strTabSec.addString(b->getName(), hashIt)});
1950
88.5k
}
1951
1952
2.33k
size_t SymbolTableBaseSection::getSymbolIndex(Symbol *sym) {
1953
2.33k
  if (this == mainPart->dynSymTab)
1954
2.13k
    return sym->dynsymIndex;
1955
201
1956
201
  // Initializes symbol lookup tables lazily. This is used only for -r,
1957
201
  // -emit-relocs and dynsyms in partitions other than the main one.
1958
201
  llvm::call_once(onceFlag, [&] {
1959
80
    symbolIndexMap.reserve(symbols.size());
1960
80
    size_t i = 0;
1961
412
    for (const SymbolTableEntry &e : symbols) {
1962
412
      if (e.sym->type == STT_SECTION)
1963
203
        sectionIndexMap[e.sym->getOutputSection()] = ++i;
1964
209
      else
1965
209
        symbolIndexMap[e.sym] = ++i;
1966
412
    }
1967
80
  });
1968
201
1969
201
  // Section symbols are mapped based on their output sections
1970
201
  // to maintain their semantics.
1971
201
  if (sym->type == STT_SECTION)
1972
93
    return sectionIndexMap.lookup(sym->getOutputSection());
1973
108
  return symbolIndexMap.lookup(sym);
1974
108
}
1975
1976
template <class ELFT>
1977
SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &strTabSec)
1978
6.87k
    : SymbolTableBaseSection(strTabSec) {
1979
6.87k
  this->entsize = sizeof(Elf_Sym);
1980
6.87k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1978
941
    : SymbolTableBaseSection(strTabSec) {
1979
941
  this->entsize = sizeof(Elf_Sym);
1980
941
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1978
401
    : SymbolTableBaseSection(strTabSec) {
1979
401
  this->entsize = sizeof(Elf_Sym);
1980
401
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1978
5.26k
    : SymbolTableBaseSection(strTabSec) {
1979
5.26k
  this->entsize = sizeof(Elf_Sym);
1980
5.26k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1978
272
    : SymbolTableBaseSection(strTabSec) {
1979
272
  this->entsize = sizeof(Elf_Sym);
1980
272
}
1981
1982
241k
static BssSection *getCommonSec(Symbol *sym) {
1983
241k
  if (!config->defineCommon)
1984
197k
    if (auto *d = dyn_cast<Defined>(sym))
1985
197k
      return dyn_cast_or_null<BssSection>(d->section);
1986
44.0k
  return nullptr;
1987
44.0k
}
1988
1989
153k
static uint32_t getSymSectionIndex(Symbol *sym) {
1990
153k
  if (getCommonSec(sym))
1991
2
    return SHN_COMMON;
1992
153k
  if (!isa<Defined>(sym) || 
sym->needsPltAddr152k
)
1993
1.28k
    return SHN_UNDEF;
1994
152k
  if (const OutputSection *os = sym->getOutputSection())
1995
151k
    return os->sectionIndex >= SHN_LORESERVE ? 
(uint32_t)SHN_XINDEX522
1996
151k
                                             : 
os->sectionIndex150k
;
1997
720
  return SHN_ABS;
1998
720
}
1999
2000
// Write the internal symbol table contents to the output symbol table.
2001
3.82k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
2002
3.82k
  // The first entry is a null entry as per the ELF spec.
2003
3.82k
  memset(buf, 0, sizeof(Elf_Sym));
2004
3.82k
  buf += sizeof(Elf_Sym);
2005
3.82k
2006
3.82k
  auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2007
3.82k
2008
87.9k
  for (SymbolTableEntry &ent : symbols) {
2009
87.9k
    Symbol *sym = ent.sym;
2010
87.9k
    bool isDefinedHere = type == SHT_SYMTAB || 
sym->partition == partition2.13k
;
2011
87.9k
2012
87.9k
    // Set st_info and st_other.
2013
87.9k
    eSym->st_other = 0;
2014
87.9k
    if (sym->isLocal()) {
2015
68.3k
      eSym->setBindingAndType(STB_LOCAL, sym->type);
2016
68.3k
    } else {
2017
19.5k
      eSym->setBindingAndType(sym->computeBinding(), sym->type);
2018
19.5k
      eSym->setVisibility(sym->visibility);
2019
19.5k
    }
2020
87.9k
2021
87.9k
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
2022
87.9k
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
2023
87.9k
    if (config->emachine == EM_PPC64)
2024
814
      eSym->st_other |= sym->stOther & 0xe0;
2025
87.9k
2026
87.9k
    eSym->st_name = ent.strTabOffset;
2027
87.9k
    if (isDefinedHere)
2028
87.9k
      eSym->st_shndx = getSymSectionIndex(ent.sym);
2029
2
    else
2030
2
      eSym->st_shndx = 0;
2031
87.9k
2032
87.9k
    // Copy symbol size if it is a defined symbol. st_size is not significant
2033
87.9k
    // for undefined symbols, so whether copying it or not is up to us if that's
2034
87.9k
    // the case. We'll leave it as zero because by not setting a value, we can
2035
87.9k
    // get the exact same outputs for two sets of input files that differ only
2036
87.9k
    // in undefined symbol size in DSOs.
2037
87.9k
    if (eSym->st_shndx == SHN_UNDEF || 
!isDefinedHere86.6k
)
2038
1.28k
      eSym->st_size = 0;
2039
86.6k
    else
2040
86.6k
      eSym->st_size = sym->getSize();
2041
87.9k
2042
87.9k
    // st_value is usually an address of a symbol, but that has a
2043
87.9k
    // special meaining for uninstantiated common symbols (this can
2044
87.9k
    // occur if -r is given).
2045
87.9k
    if (BssSection *commonSec = getCommonSec(ent.sym))
2046
2
      eSym->st_value = commonSec->alignment;
2047
87.9k
    else if (isDefinedHere)
2048
87.9k
      eSym->st_value = sym->getVA();
2049
2
    else
2050
2
      eSym->st_value = 0;
2051
87.9k
2052
87.9k
    ++eSym;
2053
87.9k
  }
2054
3.82k
2055
3.82k
  // On MIPS we need to mark symbol which has a PLT entry and requires
2056
3.82k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
2057
3.82k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
2058
3.82k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
2059
3.82k
  if (config->emachine == EM_MIPS) {
2060
287
    auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2061
287
2062
11.1k
    for (SymbolTableEntry &ent : symbols) {
2063
11.1k
      Symbol *sym = ent.sym;
2064
11.1k
      if (sym->isInPlt() && 
sym->needsPltAddr34
)
2065
8
        eSym->st_other |= STO_MIPS_PLT;
2066
11.1k
      if (isMicroMips()) {
2067
147
        // We already set the less-significant bit for symbols
2068
147
        // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
2069
147
        // records. That allows us to distinguish such symbols in
2070
147
        // the `MIPS<ELFT>::relocateOne()` routine. Now we should
2071
147
        // clear that bit for non-dynamic symbol table, so tools
2072
147
        // like `objdump` will be able to deal with a correct
2073
147
        // symbol position.
2074
147
        if (sym->isDefined() &&
2075
147
            
(135
(sym->stOther & STO_MIPS_MICROMIPS)135
||
sym->needsPltAddr94
)) {
2076
43
          if (!strTabSec.isDynamic())
2077
37
            eSym->st_value &= ~1;
2078
43
          eSym->st_other |= STO_MIPS_MICROMIPS;
2079
43
        }
2080
147
      }
2081
11.1k
      if (config->relocatable)
2082
82
        if (auto *d = dyn_cast<Defined>(sym))
2083
82
          if (isMipsPIC<ELFT>(d))
2084
7
            eSym->st_other |= STO_MIPS_PIC;
2085
11.1k
      ++eSym;
2086
11.1k
    }
2087
287
  }
2088
3.82k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
2001
542
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
2002
542
  // The first entry is a null entry as per the ELF spec.
2003
542
  memset(buf, 0, sizeof(Elf_Sym));
2004
542
  buf += sizeof(Elf_Sym);
2005
542
2006
542
  auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2007
542
2008
2.84k
  for (SymbolTableEntry &ent : symbols) {
2009
2.84k
    Symbol *sym = ent.sym;
2010
2.84k
    bool isDefinedHere = type == SHT_SYMTAB || 
sym->partition == partition351
;
2011
2.84k
2012
2.84k
    // Set st_info and st_other.
2013
2.84k
    eSym->st_other = 0;
2014
2.84k
    if (sym->isLocal()) {
2015
1.14k
      eSym->setBindingAndType(STB_LOCAL, sym->type);
2016
1.70k
    } else {
2017
1.70k
      eSym->setBindingAndType(sym->computeBinding(), sym->type);
2018
1.70k
      eSym->setVisibility(sym->visibility);
2019
1.70k
    }
2020
2.84k
2021
2.84k
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
2022
2.84k
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
2023
2.84k
    if (config->emachine == EM_PPC64)
2024
0
      eSym->st_other |= sym->stOther & 0xe0;
2025
2.84k
2026
2.84k
    eSym->st_name = ent.strTabOffset;
2027
2.84k
    if (isDefinedHere)
2028
2.84k
      eSym->st_shndx = getSymSectionIndex(ent.sym);
2029
0
    else
2030
0
      eSym->st_shndx = 0;
2031
2.84k
2032
2.84k
    // Copy symbol size if it is a defined symbol. st_size is not significant
2033
2.84k
    // for undefined symbols, so whether copying it or not is up to us if that's
2034
2.84k
    // the case. We'll leave it as zero because by not setting a value, we can
2035
2.84k