Coverage Report

Created: 2019-05-19 14:56

/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.14k
static uint64_t readUint(uint8_t *Buf) {
58
1.14k
  return Config->Is64 ? 
read64(Buf)980
:
read32(Buf)163
;
59
1.14k
}
60
61
11.4k
static void writeUint(uint8_t *Buf, uint64_t Val) {
62
11.4k
  if (Config->Is64)
63
11.0k
    write64(Buf, Val);
64
386
  else
65
386
    write32(Buf, Val);
66
11.4k
}
67
68
// Returns an LLD version string.
69
2.44k
static ArrayRef<uint8_t> getVersion() {
70
2.44k
  // Check LLD_VERSION first for ease of testing.
71
2.44k
  // You can get consistent output by using the environment variable.
72
2.44k
  // This is only for testing.
73
2.44k
  StringRef S = getenv("LLD_VERSION");
74
2.44k
  if (S.empty())
75
0
    S = Saver.save(Twine("Linker: ") + getLLDVersion());
76
2.44k
77
2.44k
  // +1 to include the terminating '\0'.
78
2.44k
  return {(const uint8_t *)S.data(), S.size() + 1};
79
2.44k
}
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.44k
MergeInputSection *elf::createCommentSection() {
86
2.44k
  return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,
87
2.44k
                                 getVersion(), ".comment");
88
2.44k
}
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
191
      Flags(Flags) {
95
191
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
96
191
}
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
22
      Flags(Flags) {
95
22
  this->Entsize = sizeof(Elf_Mips_ABIFlags);
96
22
}
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
197
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
197
  Elf_Mips_ABIFlags Flags = {};
105
197
  bool Create = false;
106
197
107
1.92k
  for (InputSectionBase *Sec : InputSections) {
108
1.92k
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
109
1.67k
      continue;
110
253
    Sec->Live = false;
111
253
    Create = true;
112
253
113
253
    std::string Filename = toString(Sec->File);
114
253
    const size_t Size = Sec->data().size();
115
253
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
253
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
253
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
253
    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
253
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->data().data());
124
253
    if (S->version != 0) {
125
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(S->version));
127
0
      return nullptr;
128
0
    }
129
253
130
253
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
253
    // select the highest number of ISA/Rev/Ext.
132
253
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
133
253
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
134
253
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
135
253
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
136
253
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
137
253
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
138
253
    Flags.ases |= S->ases;
139
253
    Flags.flags1 |= S->flags1;
140
253
    Flags.flags2 |= S->flags2;
141
253
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
142
253
  };
143
197
144
197
  if (Create)
145
191
    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
23
MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104
23
  Elf_Mips_ABIFlags Flags = {};
105
23
  bool Create = false;
106
23
107
209
  for (InputSectionBase *Sec : InputSections) {
108
209
    if (Sec->Type != SHT_MIPS_ABIFLAGS)
109
182
      continue;
110
27
    Sec->Live = false;
111
27
    Create = true;
112
27
113
27
    std::string Filename = toString(Sec->File);
114
27
    const size_t Size = Sec->data().size();
115
27
    // Older version of BFD (such as the default FreeBSD linker) concatenate
116
27
    // .MIPS.abiflags instead of merging. To allow for this case (or potential
117
27
    // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118
27
    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
27
    auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->data().data());
124
27
    if (S->version != 0) {
125
0
      error(Filename + ": unexpected .MIPS.abiflags version " +
126
0
            Twine(S->version));
127
0
      return nullptr;
128
0
    }
129
27
130
27
    // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131
27
    // select the highest number of ISA/Rev/Ext.
132
27
    Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
133
27
    Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
134
27
    Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
135
27
    Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
136
27
    Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
137
27
    Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
138
27
    Flags.ases |= S->ases;
139
27
    Flags.flags1 |= S->flags1;
140
27
    Flags.flags2 |= S->flags2;
141
27
    Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
142
27
  };
143
23
144
23
  if (Create)
145
22
    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->Live = false;
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->Live = false;
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->Live = false;
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
197
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
197
  // N64 ABI only.
170
197
  if (!ELFT::Is64Bits)
171
149
    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->Live = false;
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
23
MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169
23
  // N64 ABI only.
170
23
  if (!ELFT::Is64Bits)
171
23
    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->Live = false;
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->Live = false;
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->Live = false;
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->Live = false;
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
145
      Reginfo(Reginfo) {
215
145
  this->Entsize = sizeof(Elf_Mips_RegInfo);
216
145
}
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
22
      Reginfo(Reginfo) {
215
22
  this->Entsize = sizeof(Elf_Mips_RegInfo);
216
22
}
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
197
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
197
  // Section should be alive for O32 and N32 ABIs only.
227
197
  if (ELFT::Is64Bits)
228
48
    return nullptr;
229
149
230
149
  std::vector<InputSectionBase *> Sections;
231
149
  for (InputSectionBase *Sec : InputSections)
232
1.64k
    if (Sec->Type == SHT_MIPS_REGINFO)
233
195
      Sections.push_back(Sec);
234
149
235
149
  if (Sections.empty())
236
4
    return nullptr;
237
145
238
145
  Elf_Mips_RegInfo Reginfo = {};
239
195
  for (InputSectionBase *Sec : Sections) {
240
195
    Sec->Live = false;
241
195
242
195
    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
195
247
195
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->data().data());
248
195
    Reginfo.ri_gprmask |= R->ri_gprmask;
249
195
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
250
195
  };
251
145
252
145
  return make<MipsReginfoSection<ELFT>>(Reginfo);
253
145
}
lld::elf::MipsReginfoSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::create()
Line
Count
Source
225
23
MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226
23
  // Section should be alive for O32 and N32 ABIs only.
227
23
  if (ELFT::Is64Bits)
228
0
    return nullptr;
229
23
230
23
  std::vector<InputSectionBase *> Sections;
231
23
  for (InputSectionBase *Sec : InputSections)
232
231
    if (Sec->Type == SHT_MIPS_REGINFO)
233
27
      Sections.push_back(Sec);
234
23
235
23
  if (Sections.empty())
236
1
    return nullptr;
237
22
238
22
  Elf_Mips_RegInfo Reginfo = {};
239
27
  for (InputSectionBase *Sec : Sections) {
240
27
    Sec->Live = false;
241
27
242
27
    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
27
247
27
    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->data().data());
248
27
    Reginfo.ri_gprmask |= R->ri_gprmask;
249
27
    Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
250
27
  };
251
22
252
22
  return make<MipsReginfoSection<ELFT>>(Reginfo);
253
22
}
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->Live = false;
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->Live = false;
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->Live = false;
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
10
InputSection *elf::createInterpSection() {
256
10
  // StringSaver guarantees that the returned string ends with '\0'.
257
10
  StringRef S = Saver.save(Config->DynamicLinker);
258
10
  ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1};
259
10
260
10
  auto *Sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, Contents,
261
10
                                 ".interp");
262
10
  Sec->Live = true;
263
10
  return Sec;
264
10
}
265
266
Defined *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
267
545
                                uint64_t Size, InputSectionBase &Section) {
268
545
  auto *S = make<Defined>(Section.File, Name, STB_LOCAL, STV_DEFAULT, Type,
269
545
                          Value, Size, &Section);
270
545
  if (In.SymTab)
271
545
    In.SymTab->addSymbol(S);
272
545
  return S;
273
545
}
274
275
17
static size_t getHashSize() {
276
17
  switch (Config->BuildId) {
277
17
  case BuildIdKind::Fast:
278
6
    return 8;
279
17
  case BuildIdKind::Md5:
280
4
  case BuildIdKind::Uuid:
281
4
    return 16;
282
4
  case BuildIdKind::Sha1:
283
4
    return 20;
284
4
  case BuildIdKind::Hexstring:
285
3
    return Config->BuildIdVector.size();
286
4
  default:
287
0
    llvm_unreachable("unknown BuildIdKind");
288
17
  }
289
17
}
290
291
BuildIdSection::BuildIdSection()
292
    : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"),
293
17
      HashSize(getHashSize()) {}
294
295
17
void BuildIdSection::writeTo(uint8_t *Buf) {
296
17
  write32(Buf, 4);                      // Name size
297
17
  write32(Buf + 4, HashSize);           // Content size
298
17
  write32(Buf + 8, NT_GNU_BUILD_ID);    // Type
299
17
  memcpy(Buf + 12, "GNU", 4);           // Name string
300
17
  HashBuf = Buf + 16;
301
17
}
302
303
17
void BuildIdSection::writeBuildId(ArrayRef<uint8_t> Buf) {
304
17
  assert(Buf.size() == HashSize);
305
17
  memcpy(HashBuf, Buf.data(), HashSize);
306
17
}
307
308
BssSection::BssSection(StringRef Name, uint64_t Size, uint32_t Alignment)
309
5.20k
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, Alignment, Name) {
310
5.20k
  this->Bss = true;
311
5.20k
  this->Size = Size;
312
5.20k
}
313
314
EhFrameSection::EhFrameSection()
315
2.44k
    : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
316
317
// Search for an existing CIE record or create a new one.
318
// CIE records from input object files are uniquified by their contents
319
// and where their relocations point to.
320
template <class ELFT, class RelTy>
321
108
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
322
108
  Symbol *Personality = nullptr;
323
108
  unsigned FirstRelI = Cie.FirstRelocation;
324
108
  if (FirstRelI != (unsigned)-1)
325
11
    Personality =
326
11
        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
327
108
328
108
  // Search for an existing CIE by CIE contents/relocation target pair.
329
108
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
330
108
331
108
  // If not found, create a new one.
332
108
  if (!Rec) {
333
94
    Rec = make<CieRecord>();
334
94
    Rec->Cie = &Cie;
335
94
    CieRecords.push_back(Rec);
336
94
  }
337
108
  return Rec;
338
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
321
4
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
322
4
  Symbol *Personality = nullptr;
323
4
  unsigned FirstRelI = Cie.FirstRelocation;
324
4
  if (FirstRelI != (unsigned)-1)
325
2
    Personality =
326
2
        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
327
4
328
4
  // Search for an existing CIE by CIE contents/relocation target pair.
329
4
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
330
4
331
4
  // If not found, create a new one.
332
4
  if (!Rec) {
333
3
    Rec = make<CieRecord>();
334
3
    Rec->Cie = &Cie;
335
3
    CieRecords.push_back(Rec);
336
3
  }
337
4
  return Rec;
338
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
321
93
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
322
93
  Symbol *Personality = nullptr;
323
93
  unsigned FirstRelI = Cie.FirstRelocation;
324
93
  if (FirstRelI != (unsigned)-1)
325
8
    Personality =
326
8
        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
327
93
328
93
  // Search for an existing CIE by CIE contents/relocation target pair.
329
93
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
330
93
331
93
  // If not found, create a new one.
332
93
  if (!Rec) {
333
80
    Rec = make<CieRecord>();
334
80
    Rec->Cie = &Cie;
335
80
    CieRecords.push_back(Rec);
336
80
  }
337
93
  return Rec;
338
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
321
7
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
322
7
  Symbol *Personality = nullptr;
323
7
  unsigned FirstRelI = Cie.FirstRelocation;
324
7
  if (FirstRelI != (unsigned)-1)
325
0
    Personality =
326
0
        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
327
7
328
7
  // Search for an existing CIE by CIE contents/relocation target pair.
329
7
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
330
7
331
7
  // If not found, create a new one.
332
7
  if (!Rec) {
333
7
    Rec = make<CieRecord>();
334
7
    Rec->Cie = &Cie;
335
7
    CieRecords.push_back(Rec);
336
7
  }
337
7
  return Rec;
338
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
321
3
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
322
3
  Symbol *Personality = nullptr;
323
3
  unsigned FirstRelI = Cie.FirstRelocation;
324
3
  if (FirstRelI != (unsigned)-1)
325
1
    Personality =
326
1
        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
327
3
328
3
  // Search for an existing CIE by CIE contents/relocation target pair.
329
3
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
330
3
331
3
  // If not found, create a new one.
332
3
  if (!Rec) {
333
3
    Rec = make<CieRecord>();
334
3
    Rec->Cie = &Cie;
335
3
    CieRecords.push_back(Rec);
336
3
  }
337
3
  return Rec;
338
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
321
1
CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
322
1
  Symbol *Personality = nullptr;
323
1
  unsigned FirstRelI = Cie.FirstRelocation;
324
1
  if (FirstRelI != (unsigned)-1)
325
0
    Personality =
326
0
        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
327
1
328
1
  // Search for an existing CIE by CIE contents/relocation target pair.
329
1
  CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
330
1
331
1
  // If not found, create a new one.
332
1
  if (!Rec) {
333
1
    Rec = make<CieRecord>();
334
1
    Rec->Cie = &Cie;
335
1
    CieRecords.push_back(Rec);
336
1
  }
337
1
  return Rec;
338
1
}
339
340
// There is one FDE per function. Returns true if a given FDE
341
// points to a live function.
342
template <class ELFT, class RelTy>
343
133
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
344
133
  auto *Sec = cast<EhInputSection>(Fde.Sec);
345
133
  unsigned FirstRelI = Fde.FirstRelocation;
346
133
347
133
  // An FDE should point to some function because FDEs are to describe
348
133
  // functions. That's however not always the case due to an issue of
349
133
  // ld.gold with -r. ld.gold may discard only functions and leave their
350
133
  // corresponding FDEs, which results in creating bad .eh_frame sections.
351
133
  // To deal with that, we ignore such FDEs.
352
133
  if (FirstRelI == (unsigned)-1)
353
1
    return false;
354
132
355
132
  const RelTy &Rel = Rels[FirstRelI];
356
132
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
357
132
358
132
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
359
132
  if (auto *D = dyn_cast<Defined>(&B))
360
131
    if (SectionBase *Sec = D->Section)
361
131
      return Sec->Live;
362
1
  return false;
363
1
}
Unexecuted instantiation: bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >)
bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >)
Line
Count
Source
343
4
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
344
4
  auto *Sec = cast<EhInputSection>(Fde.Sec);
345
4
  unsigned FirstRelI = Fde.FirstRelocation;
346
4
347
4
  // An FDE should point to some function because FDEs are to describe
348
4
  // functions. That's however not always the case due to an issue of
349
4
  // ld.gold with -r. ld.gold may discard only functions and leave their
350
4
  // corresponding FDEs, which results in creating bad .eh_frame sections.
351
4
  // To deal with that, we ignore such FDEs.
352
4
  if (FirstRelI == (unsigned)-1)
353
0
    return false;
354
4
355
4
  const RelTy &Rel = Rels[FirstRelI];
356
4
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
357
4
358
4
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
359
4
  if (auto *D = dyn_cast<Defined>(&B))
360
4
    if (SectionBase *Sec = D->Section)
361
4
      return Sec->Live;
362
0
  return false;
363
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
343
125
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
344
125
  auto *Sec = cast<EhInputSection>(Fde.Sec);
345
125
  unsigned FirstRelI = Fde.FirstRelocation;
346
125
347
125
  // An FDE should point to some function because FDEs are to describe
348
125
  // functions. That's however not always the case due to an issue of
349
125
  // ld.gold with -r. ld.gold may discard only functions and leave their
350
125
  // corresponding FDEs, which results in creating bad .eh_frame sections.
351
125
  // To deal with that, we ignore such FDEs.
352
125
  if (FirstRelI == (unsigned)-1)
353
0
    return false;
354
125
355
125
  const RelTy &Rel = Rels[FirstRelI];
356
125
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
357
125
358
125
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
359
125
  if (auto *D = dyn_cast<Defined>(&B))
360
124
    if (SectionBase *Sec = D->Section)
361
124
      return Sec->Live;
362
1
  return false;
363
1
}
bool lld::elf::EhFrameSection::isFdeLive<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::EhSectionPiece&, llvm::ArrayRef<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >)
Line
Count
Source
343
1
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
344
1
  auto *Sec = cast<EhInputSection>(Fde.Sec);
345
1
  unsigned FirstRelI = Fde.FirstRelocation;
346
1
347
1
  // An FDE should point to some function because FDEs are to describe
348
1
  // functions. That's however not always the case due to an issue of
349
1
  // ld.gold with -r. ld.gold may discard only functions and leave their
350
1
  // corresponding FDEs, which results in creating bad .eh_frame sections.
351
1
  // To deal with that, we ignore such FDEs.
352
1
  if (FirstRelI == (unsigned)-1)
353
1
    return false;
354
0
355
0
  const RelTy &Rel = Rels[FirstRelI];
356
0
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
357
0
358
0
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
359
0
  if (auto *D = dyn_cast<Defined>(&B))
360
0
    if (SectionBase *Sec = D->Section)
361
0
      return Sec->Live;
362
0
  return false;
363
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
343
3
bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) {
344
3
  auto *Sec = cast<EhInputSection>(Fde.Sec);
345
3
  unsigned FirstRelI = Fde.FirstRelocation;
346
3
347
3
  // An FDE should point to some function because FDEs are to describe
348
3
  // functions. That's however not always the case due to an issue of
349
3
  // ld.gold with -r. ld.gold may discard only functions and leave their
350
3
  // corresponding FDEs, which results in creating bad .eh_frame sections.
351
3
  // To deal with that, we ignore such FDEs.
352
3
  if (FirstRelI == (unsigned)-1)
353
0
    return false;
354
3
355
3
  const RelTy &Rel = Rels[FirstRelI];
356
3
  Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
357
3
358
3
  // FDEs for garbage-collected or merged-by-ICF sections are dead.
359
3
  if (auto *D = dyn_cast<Defined>(&B))
360
3
    if (SectionBase *Sec = D->Section)
361
3
      return Sec->Live;
362
0
  return false;
363
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> >)
364
365
// .eh_frame is a sequence of CIE or FDE records. In general, there
366
// is one CIE record per input object file which is followed by
367
// a list of FDEs. This function searches an existing CIE or create a new
368
// one and associates FDEs to the CIE.
369
template <class ELFT, class RelTy>
370
112
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
371
112
  OffsetToCie.clear();
372
247
  for (EhSectionPiece &Piece : Sec->Pieces) {
373
247
    // The empty record is the end marker.
374
247
    if (Piece.Size == 4)
375
6
      return;
376
241
377
241
    size_t Offset = Piece.InputOff;
378
241
    uint32_t ID = read32(Piece.data().data() + 4);
379
241
    if (ID == 0) {
380
108
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
381
108
      continue;
382
108
    }
383
133
384
133
    uint32_t CieOffset = Offset + 4 - ID;
385
133
    CieRecord *Rec = OffsetToCie[CieOffset];
386
133
    if (!Rec)
387
0
      fatal(toString(Sec) + ": invalid CIE reference");
388
133
389
133
    if (!isFdeLive<ELFT>(Piece, Rels))
390
10
      continue;
391
123
    Rec->Fdes.push_back(&Piece);
392
123
    NumFdes++;
393
123
  }
394
112
}
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
370
4
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
371
4
  OffsetToCie.clear();
372
8
  for (EhSectionPiece &Piece : Sec->Pieces) {
373
8
    // The empty record is the end marker.
374
8
    if (Piece.Size == 4)
375
0
      return;
376
8
377
8
    size_t Offset = Piece.InputOff;
378
8
    uint32_t ID = read32(Piece.data().data() + 4);
379
8
    if (ID == 0) {
380
4
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
381
4
      continue;
382
4
    }
383
4
384
4
    uint32_t CieOffset = Offset + 4 - ID;
385
4
    CieRecord *Rec = OffsetToCie[CieOffset];
386
4
    if (!Rec)
387
0
      fatal(toString(Sec) + ": invalid CIE reference");
388
4
389
4
    if (!isFdeLive<ELFT>(Piece, Rels))
390
0
      continue;
391
4
    Rec->Fdes.push_back(&Piece);
392
4
    NumFdes++;
393
4
  }
394
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
370
91
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
371
91
  OffsetToCie.clear();
372
218
  for (EhSectionPiece &Piece : Sec->Pieces) {
373
218
    // The empty record is the end marker.
374
218
    if (Piece.Size == 4)
375
0
      return;
376
218
377
218
    size_t Offset = Piece.InputOff;
378
218
    uint32_t ID = read32(Piece.data().data() + 4);
379
218
    if (ID == 0) {
380
93
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
381
93
      continue;
382
93
    }
383
125
384
125
    uint32_t CieOffset = Offset + 4 - ID;
385
125
    CieRecord *Rec = OffsetToCie[CieOffset];
386
125
    if (!Rec)
387
0
      fatal(toString(Sec) + ": invalid CIE reference");
388
125
389
125
    if (!isFdeLive<ELFT>(Piece, Rels))
390
9
      continue;
391
116
    Rec->Fdes.push_back(&Piece);
392
116
    NumFdes++;
393
116
  }
394
91
}
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
370
13
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
371
13
  OffsetToCie.clear();
372
14
  for (EhSectionPiece &Piece : Sec->Pieces) {
373
14
    // The empty record is the end marker.
374
14
    if (Piece.Size == 4)
375
6
      return;
376
8
377
8
    size_t Offset = Piece.InputOff;
378
8
    uint32_t ID = read32(Piece.data().data() + 4);
379
8
    if (ID == 0) {
380
7
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
381
7
      continue;
382
7
    }
383
1
384
1
    uint32_t CieOffset = Offset + 4 - ID;
385
1
    CieRecord *Rec = OffsetToCie[CieOffset];
386
1
    if (!Rec)
387
0
      fatal(toString(Sec) + ": invalid CIE reference");
388
1
389
1
    if (!isFdeLive<ELFT>(Piece, Rels))
390
1
      continue;
391
0
    Rec->Fdes.push_back(&Piece);
392
0
    NumFdes++;
393
0
  }
394
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
370
3
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
371
3
  OffsetToCie.clear();
372
6
  for (EhSectionPiece &Piece : Sec->Pieces) {
373
6
    // The empty record is the end marker.
374
6
    if (Piece.Size == 4)
375
0
      return;
376
6
377
6
    size_t Offset = Piece.InputOff;
378
6
    uint32_t ID = read32(Piece.data().data() + 4);
379
6
    if (ID == 0) {
380
3
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
381
3
      continue;
382
3
    }
383
3
384
3
    uint32_t CieOffset = Offset + 4 - ID;
385
3
    CieRecord *Rec = OffsetToCie[CieOffset];
386
3
    if (!Rec)
387
0
      fatal(toString(Sec) + ": invalid CIE reference");
388
3
389
3
    if (!isFdeLive<ELFT>(Piece, Rels))
390
0
      continue;
391
3
    Rec->Fdes.push_back(&Piece);
392
3
    NumFdes++;
393
3
  }
394
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
370
1
void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) {
371
1
  OffsetToCie.clear();
372
1
  for (EhSectionPiece &Piece : Sec->Pieces) {
373
1
    // The empty record is the end marker.
374
1
    if (Piece.Size == 4)
375
0
      return;
376
1
377
1
    size_t Offset = Piece.InputOff;
378
1
    uint32_t ID = read32(Piece.data().data() + 4);
379
1
    if (ID == 0) {
380
1
      OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels);
381
1
      continue;
382
1
    }
383
0
384
0
    uint32_t CieOffset = Offset + 4 - ID;
385
0
    CieRecord *Rec = OffsetToCie[CieOffset];
386
0
    if (!Rec)
387
0
      fatal(toString(Sec) + ": invalid CIE reference");
388
0
389
0
    if (!isFdeLive<ELFT>(Piece, Rels))
390
0
      continue;
391
0
    Rec->Fdes.push_back(&Piece);
392
0
    NumFdes++;
393
0
  }
394
1
}
395
396
115
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
397
115
  auto *Sec = cast<EhInputSection>(C);
398
115
  Sec->Parent = this;
399
115
400
115
  Alignment = std::max(Alignment, Sec->Alignment);
401
115
  Sections.push_back(Sec);
402
115
403
115
  for (auto *DS : Sec->DependentSections)
404
3
    DependentSections.push_back(DS);
405
115
406
115
  if (Sec->Pieces.empty())
407
3
    return;
408
112
409
112
  if (Sec->AreRelocsRela)
410
94
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
411
18
  else
412
18
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
413
112
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::InputSectionBase*)
Line
Count
Source
396
4
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
397
4
  auto *Sec = cast<EhInputSection>(C);
398
4
  Sec->Parent = this;
399
4
400
4
  Alignment = std::max(Alignment, Sec->Alignment);
401
4
  Sections.push_back(Sec);
402
4
403
4
  for (auto *DS : Sec->DependentSections)
404
0
    DependentSections.push_back(DS);
405
4
406
4
  if (Sec->Pieces.empty())
407
0
    return;
408
4
409
4
  if (Sec->AreRelocsRela)
410
0
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
411
4
  else
412
4
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
413
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
396
107
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
397
107
  auto *Sec = cast<EhInputSection>(C);
398
107
  Sec->Parent = this;
399
107
400
107
  Alignment = std::max(Alignment, Sec->Alignment);
401
107
  Sections.push_back(Sec);
402
107
403
107
  for (auto *DS : Sec->DependentSections)
404
3
    DependentSections.push_back(DS);
405
107
406
107
  if (Sec->Pieces.empty())
407
3
    return;
408
104
409
104
  if (Sec->AreRelocsRela)
410
91
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
411
13
  else
412
13
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
413
104
}
void lld::elf::EhFrameSection::addSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::InputSectionBase*)
Line
Count
Source
396
4
template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) {
397
4
  auto *Sec = cast<EhInputSection>(C);
398
4
  Sec->Parent = this;
399
4
400
4
  Alignment = std::max(Alignment, Sec->Alignment);
401
4
  Sections.push_back(Sec);
402
4
403
4
  for (auto *DS : Sec->DependentSections)
404
0
    DependentSections.push_back(DS);
405
4
406
4
  if (Sec->Pieces.empty())
407
0
    return;
408
4
409
4
  if (Sec->AreRelocsRela)
410
3
    addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>());
411
1
  else
412
1
    addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>());
413
4
}
414
415
205
static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
416
205
  memcpy(Buf, D.data(), D.size());
417
205
418
205
  size_t Aligned = alignTo(D.size(), Config->Wordsize);
419
205
420
205
  // Zero-clear trailing padding if it exists.
421
205
  memset(Buf + D.size(), 0, Aligned - D.size());
422
205
423
205
  // Fix the size field. -4 since size does not include the size field itself.
424
205
  write32(Buf, Aligned - 4);
425
205
}
426
427
98
void EhFrameSection::finalizeContents() {
428
98
  assert(!this->Size); // Not finalized.
429
98
  size_t Off = 0;
430
98
  for (CieRecord *Rec : CieRecords) {
431
92
    Rec->Cie->OutputOff = Off;
432
92
    Off += alignTo(Rec->Cie->Size, Config->Wordsize);
433
92
434
121
    for (EhSectionPiece *Fde : Rec->Fdes) {
435
121
      Fde->OutputOff = Off;
436
121
      Off += alignTo(Fde->Size, Config->Wordsize);
437
121
    }
438
92
  }
439
98
440
98
  // The LSB standard does not allow a .eh_frame section with zero
441
98
  // Call Frame Information records. glibc unwind-dw2-fde.c
442
98
  // classify_object_over_fdes expects there is a CIE record length 0 as a
443
98
  // terminator. Thus we add one unconditionally.
444
98
  Off += 4;
445
98
446
98
  this->Size = Off;
447
98
}
448
449
// Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table
450
// to get an FDE from an address to which FDE is applied. This function
451
// returns a list of such pairs.
452
28
std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const {
453
28
  uint8_t *Buf = Out::BufferStart + getParent()->Offset + OutSecOff;
454
28
  std::vector<FdeData> Ret;
455
28
456
28
  uint64_t VA = In.EhFrameHdr->getVA();
457
28
  for (CieRecord *Rec : CieRecords) {
458
28
    uint8_t Enc = getFdeEncoding(Rec->Cie);
459
28
    for (EhSectionPiece *Fde : Rec->Fdes) {
460
24
      uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
461
24
      uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
462
24
      if (!isInt<32>(Pc - VA))
463
0
        fatal(toString(Fde->Sec) + ": PC offset is too large: 0x" +
464
0
              Twine::utohexstr(Pc - VA));
465
24
      Ret.push_back({uint32_t(Pc - VA), uint32_t(FdeVA - VA)});
466
24
    }
467
28
  }
468
28
469
28
  // Sort the FDE list by their PC and uniqueify. Usually there is only
470
28
  // one FDE for a PC (i.e. function), but if ICF merges two functions
471
28
  // into one, there can be more than one FDEs pointing to the address.
472
28
  auto Less = [](const FdeData &A, const FdeData &B) {
473
4
    return A.PcRel < B.PcRel;
474
4
  };
475
28
  llvm::stable_sort(Ret, Less);
476
28
  auto Eq = [](const FdeData &A, const FdeData &B) {
477
4
    return A.PcRel == B.PcRel;
478
4
  };
479
28
  Ret.erase(std::unique(Ret.begin(), Ret.end(), Eq), Ret.end());
480
28
481
28
  return Ret;
482
28
}
483
484
24
static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
485
24
  switch (Size) {
486
24
  case DW_EH_PE_udata2:
487
1
    return read16(Buf);
488
24
  case DW_EH_PE_sdata2:
489
1
    return (int16_t)read16(Buf);
490
24
  case DW_EH_PE_udata4:
491
0
    return read32(Buf);
492
24
  case DW_EH_PE_sdata4:
493
17
    return (int32_t)read32(Buf);
494
24
  case DW_EH_PE_udata8:
495
3
  case DW_EH_PE_sdata8:
496
3
    return read64(Buf);
497
3
  case DW_EH_PE_absptr:
498
2
    return readUint(Buf);
499
0
  }
500
0
  fatal("unknown FDE size encoding");
501
0
}
502
503
// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
504
// We need it to create .eh_frame_hdr section.
505
uint64_t EhFrameSection::getFdePc(uint8_t *Buf, size_t FdeOff,
506
24
                                  uint8_t Enc) const {
507
24
  // The starting address to which this FDE applies is
508
24
  // stored at FDE + 8 byte.
509
24
  size_t Off = FdeOff + 8;
510
24
  uint64_t Addr = readFdeAddr(Buf + Off, Enc & 0xf);
511
24
  if ((Enc & 0x70) == DW_EH_PE_absptr)
512
5
    return Addr;
513
19
  if ((Enc & 0x70) == DW_EH_PE_pcrel)
514
19
    return Addr + getParent()->Addr + Off;
515
0
  fatal("unknown FDE size relative encoding");
516
0
}
517
518
94
void EhFrameSection::writeTo(uint8_t *Buf) {
519
94
  // Write CIE and FDE records.
520
94
  for (CieRecord *Rec : CieRecords) {
521
88
    size_t CieOffset = Rec->Cie->OutputOff;
522
88
    writeCieFde(Buf + CieOffset, Rec->Cie->data());
523
88
524
117
    for (EhSectionPiece *Fde : Rec->Fdes) {
525
117
      size_t Off = Fde->OutputOff;
526
117
      writeCieFde(Buf + Off, Fde->data());
527
117
528
117
      // FDE's second word should have the offset to an associated CIE.
529
117
      // Write it.
530
117
      write32(Buf + Off + 4, Off + 4 - CieOffset);
531
117
    }
532
88
  }
533
94
534
94
  // Apply relocations. .eh_frame section contents are not contiguous
535
94
  // in the output buffer, but relocateAlloc() still works because
536
94
  // getOffset() takes care of discontiguous section pieces.
537
94
  for (EhInputSection *S : Sections)
538
108
    S->relocateAlloc(Buf, nullptr);
539
94
540
94
  if (In.EhFrameHdr && 
In.EhFrameHdr->getParent()28
)
541
28
    In.EhFrameHdr->write();
542
94
}
543
544
GotSection::GotSection()
545
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
546
2.33k
                       Target->GotEntrySize, ".got") {
547
2.33k
  // PPC64 saves the ElfSym::GlobalOffsetTable .TOC. as the first entry in the
548
2.33k
  // .got. If there are no references to .TOC. in the symbol table,
549
2.33k
  // ElfSym::GlobalOffsetTable will not be defined and we won't need to save
550
2.33k
  // .TOC. in the .got. When it is defined, we increase NumEntries by the number
551
2.33k
  // of entries used to emit ElfSym::GlobalOffsetTable.
552
2.33k
  if (ElfSym::GlobalOffsetTable && 
!Target->GotBaseSymInGotPlt120
)
553
89
    NumEntries += Target->GotHeaderEntriesNum;
554
2.33k
}
555
556
167
void GotSection::addEntry(Symbol &Sym) {
557
167
  Sym.GotIndex = NumEntries;
558
167
  ++NumEntries;
559
167
}
560
561
52
bool GotSection::addDynTlsEntry(Symbol &Sym) {
562
52
  if (Sym.GlobalDynIndex != -1U)
563
20
    return false;
564
32
  Sym.GlobalDynIndex = NumEntries;
565
32
  // Global Dynamic TLS entries take two GOT slots.
566
32
  NumEntries += 2;
567
32
  return true;
568
32
}
569
570
// Reserves TLS entries for a TLS module ID and a TLS block offset.
571
// In total it takes two GOT slots.
572
29
bool GotSection::addTlsIndex() {
573
29
  if (TlsIndexOff != uint32_t(-1))
574
18
    return false;
575
11
  TlsIndexOff = NumEntries * Config->Wordsize;
576
11
  NumEntries += 2;
577
11
  return true;
578
11
}
579
580
34
uint64_t GotSection::getGlobalDynAddr(const Symbol &B) const {
581
34
  return this->getVA() + B.GlobalDynIndex * Config->Wordsize;
582
34
}
583
584
42
uint64_t GotSection::getGlobalDynOffset(const Symbol &B) const {
585
42
  return B.GlobalDynIndex * Config->Wordsize;
586
42
}
587
588
215
void GotSection::finalizeContents() {
589
215
  Size = NumEntries * Config->Wordsize;
590
215
}
591
592
4.44k
bool GotSection::isNeeded() const {
593
4.44k
  // We need to emit a GOT even if it's empty if there's a relocation that is
594
4.44k
  // relative to GOT(such as GOTOFFREL).
595
4.44k
  return NumEntries || 
HasGotOffRel4.03k
;
596
4.44k
}
597
598
214
void GotSection::writeTo(uint8_t *Buf) {
599
214
  // Buf points to the start of this section's buffer,
600
214
  // whereas InputSectionBase::relocateAlloc() expects its argument
601
214
  // to point to the start of the output section.
602
214
  Target->writeGotHeader(Buf);
603
214
  relocateAlloc(Buf - OutSecOff, Buf - OutSecOff + Size);
604
214
}
605
606
71
static uint64_t getMipsPageAddr(uint64_t Addr) {
607
71
  return (Addr + 0x8000) & ~0xffff;
608
71
}
609
610
14
static uint64_t getMipsPageCount(uint64_t Size) {
611
14
  return (Size + 0xfffe) / 0xffff + 1;
612
14
}
613
614
MipsGotSection::MipsGotSection()
615
    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
616
197
                       ".got") {}
617
618
void MipsGotSection::addEntry(InputFile &File, Symbol &Sym, int64_t Addend,
619
10.1k
                              RelExpr Expr) {
620
10.1k
  FileGot &G = getGot(File);
621
10.1k
  if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
622
29
    if (const OutputSection *OS = Sym.getOutputSection())
623
25
      G.PagesMap.insert({OS, {}});
624
4
    else
625
4
      G.Local16.insert({{nullptr, getMipsPageAddr(Sym.getVA(Addend))}, 0});
626
10.0k
  } else if (Sym.isTls())
627
13
    G.Tls.insert({&Sym, 0});
628
10.0k
  else if (Sym.IsPreemptible && 
Expr == R_ABS41
)
629
4
    G.Relocs.insert({&Sym, 0});
630
10.0k
  else if (Sym.IsPreemptible)
631
37
    G.Global.insert({&Sym, 0});
632
10.0k
  else if (Expr == R_MIPS_GOT_OFF32)
633
12
    G.Local32.insert({{&Sym, Addend}, 0});
634
10.0k
  else
635
10.0k
    G.Local16.insert({{&Sym, Addend}, 0});
636
10.1k
}
637
638
12
void MipsGotSection::addDynTlsEntry(InputFile &File, Symbol &Sym) {
639
12
  getGot(File).DynTlsSymbols.insert({&Sym, 0});
640
12
}
641
642
6
void MipsGotSection::addTlsIndex(InputFile &File) {
643
6
  getGot(File).DynTlsSymbols.insert({nullptr, 0});
644
6
}
645
646
114
size_t MipsGotSection::FileGot::getEntriesNum() const {
647
114
  return getPageEntriesNum() + Local16.size() + Global.size() + Relocs.size() +
648
114
         Tls.size() + DynTlsSymbols.size() * 2;
649
114
}
650
651
218
size_t MipsGotSection::FileGot::getPageEntriesNum() const {
652
218
  size_t Num = 0;
653
218
  for (const std::pair<const OutputSection *, FileGot::PageBlock> &P : PagesMap)
654
52
    Num += P.second.Count;
655
218
  return Num;
656
218
}
657
658
59
size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
659
59
  size_t Count = getPageEntriesNum() + Local16.size() + Global.size();
660
59
  // If there are relocation-only entries in the GOT, TLS entries
661
59
  // are allocated after them. TLS entries should be addressable
662
59
  // by 16-bit index so count both reloc-only and TLS entries.
663
59
  if (!Tls.empty() || 
!DynTlsSymbols.empty()50
)
664
10
    Count += Relocs.size() + Tls.size() + DynTlsSymbols.size() * 2;
665
59
  return Count;
666
59
}
667
668
10.1k
MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &F) {
669
10.1k
  if (!F.MipsGotIndex.hasValue()) {
670
58
    Gots.emplace_back();
671
58
    Gots.back().File = &F;
672
58
    F.MipsGotIndex = Gots.size() - 1;
673
58
  }
674
10.1k
  return Gots[*F.MipsGotIndex];
675
10.1k
}
676
677
uint64_t MipsGotSection::getPageEntryOffset(const InputFile *F,
678
                                            const Symbol &Sym,
679
29
                                            int64_t Addend) const {
680
29
  const FileGot &G = Gots[*F->MipsGotIndex];
681
29
  uint64_t Index = 0;
682
29
  if (const OutputSection *OutSec = Sym.getOutputSection()) {
683
25
    uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
684
25
    uint64_t SymAddr = getMipsPageAddr(Sym.getVA(Addend));
685
25
    Index = G.PagesMap.lookup(OutSec).FirstIndex + (SymAddr - SecAddr) / 0xffff;
686
25
  } else {
687
4
    Index = G.Local16.lookup({nullptr, getMipsPageAddr(Sym.getVA(Addend))});
688
4
  }
689
29
  return Index * Config->Wordsize;
690
29
}
691
692
uint64_t MipsGotSection::getSymEntryOffset(const InputFile *F, const Symbol &S,
693
10.0k
                                           int64_t Addend) const {
694
10.0k
  const FileGot &G = Gots[*F->MipsGotIndex];
695
10.0k
  Symbol *Sym = const_cast<Symbol *>(&S);
696
10.0k
  if (Sym->isTls())
697
13
    return G.Tls.lookup(Sym) * Config->Wordsize;
698
10.0k
  if (Sym->IsPreemptible)
699
37
    return G.Global.lookup(Sym) * Config->Wordsize;
700
10.0k
  return G.Local16.lookup({Sym, Addend}) * Config->Wordsize;
701
10.0k
}
702
703
6
uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *F) const {
704
6
  const FileGot &G = Gots[*F->MipsGotIndex];
705
6
  return G.DynTlsSymbols.lookup(nullptr) * Config->Wordsize;
706
6
}
707
708
uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *F,
709
12
                                            const Symbol &S) const {
710
12
  const FileGot &G = Gots[*F->MipsGotIndex];
711
12
  Symbol *Sym = const_cast<Symbol *>(&S);
712
12
  return G.DynTlsSymbols.lookup(Sym) * Config->Wordsize;
713
12
}
714
715
100
const Symbol *MipsGotSection::getFirstGlobalEntry() const {
716
100
  if (Gots.empty())
717
55
    return nullptr;
718
45
  const FileGot &PrimGot = Gots.front();
719
45
  if (!PrimGot.Global.empty())
720
27
    return PrimGot.Global.front().first;
721
18
  if (!PrimGot.Relocs.empty())
722
5
    return PrimGot.Relocs.front().first;
723
13
  return nullptr;
724
13
}
725
726
100
unsigned MipsGotSection::getLocalEntriesNum() const {
727
100
  if (Gots.empty())
728
55
    return HeaderEntriesNum;
729
45
  return HeaderEntriesNum + Gots.front().getPageEntriesNum() +
730
45
         Gots.front().Local16.size();
731
45
}
732
733
59
bool MipsGotSection::tryMergeGots(FileGot &Dst, FileGot &Src, bool IsPrimary) {
734
59
  FileGot Tmp = Dst;
735
59
  set_union(Tmp.PagesMap, Src.PagesMap);
736
59
  set_union(Tmp.Local16, Src.Local16);
737
59
  set_union(Tmp.Global, Src.Global);
738
59
  set_union(Tmp.Relocs, Src.Relocs);
739
59
  set_union(Tmp.Tls, Src.Tls);
740
59
  set_union(Tmp.DynTlsSymbols, Src.DynTlsSymbols);
741
59
742
59
  size_t Count = IsPrimary ? 
HeaderEntriesNum58
:
01
;
743
59
  Count += Tmp.getIndexedEntriesNum();
744
59
745
59
  if (Count * Config->Wordsize > Config->MipsGotSize)
746
4
    return false;
747
55
748
55
  std::swap(Tmp, Dst);
749
55
  return true;
750
55
}
751
752
173
void MipsGotSection::finalizeContents() { updateAllocSize(); }
753
754
375
bool MipsGotSection::updateAllocSize() {
755
375
  Size = HeaderEntriesNum * Config->Wordsize;
756
375
  for (const FileGot &G : Gots)
757
114
    Size += G.getEntriesNum() * Config->Wordsize;
758
375
  return false;
759
375
}
760
761
187
void MipsGotSection::build() {
762
187
  if (Gots.empty())
763
133
    return;
764
54
765
54
  std::vector<FileGot> MergedGots(1);
766
54
767
54
  // For each GOT move non-preemptible symbols from the `Global`
768
54
  // to `Local16` list. Preemptible symbol might become non-preemptible
769
54
  // one if, for example, it gets a related copy relocation.
770
58
  for (FileGot &Got : Gots) {
771
58
    for (auto &P: Got.Global)
772
35
      if (!P.first->IsPreemptible)
773
0
        Got.Local16.insert({{P.first, 0}, 0});
774
58
    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
775
35
      return !P.first->IsPreemptible;
776
35
    });
777
58
  }
778
54
779
54
  // For each GOT remove "reloc-only" entry if there is "global"
780
54
  // entry for the same symbol. And add local entries which indexed
781
54
  // using 32-bit value at the end of 16-bit entries.
782
58
  for (FileGot &Got : Gots) {
783
58
    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
784
4
      return Got.Global.count(P.first);
785
4
    });
786
58
    set_union(Got.Local16, Got.Local32);
787
58
    Got.Local32.clear();
788
58
  }
789
54
790
54
  // Evaluate number of "reloc-only" entries in the resulting GOT.
791
54
  // To do that put all unique "reloc-only" and "global" entries
792
54
  // from all GOTs to the future primary GOT.
793
54
  FileGot *PrimGot = &MergedGots.front();
794
58
  for (FileGot &Got : Gots) {
795
58
    set_union(PrimGot->Relocs, Got.Global);
796
58
    set_union(PrimGot->Relocs, Got.Relocs);
797
58
    Got.Relocs.clear();
798
58
  }
799
54
800
54
  // Evaluate number of "page" entries in each GOT.
801
58
  for (FileGot &Got : Gots) {
802
58
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
803
58
         Got.PagesMap) {
804
14
      const OutputSection *OS = P.first;
805
14
      uint64_t SecSize = 0;
806
16
      for (BaseCommand *Cmd : OS->SectionCommands) {
807
16
        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
808
24
          
for (InputSection *IS : ISD->Sections)15
{
809
24
            uint64_t Off = alignTo(SecSize, IS->Alignment);
810
24
            SecSize = Off + IS->getSize();
811
24
          }
812
16
      }
813
14
      P.second.Count = getMipsPageCount(SecSize);
814
14
    }
815
58
  }
816
54
817
54
  // Merge GOTs. Try to join as much as possible GOTs but do not exceed
818
54
  // maximum GOT size. At first, try to fill the primary GOT because
819
54
  // the primary GOT can be accessed in the most effective way. If it
820
54
  // is not possible, try to fill the last GOT in the list, and finally
821
54
  // create a new GOT if both attempts failed.
822
58
  for (FileGot &SrcGot : Gots) {
823
58
    InputFile *File = SrcGot.File;
824
58
    if (tryMergeGots(MergedGots.front(), SrcGot, true)) {
825
54
      File->MipsGotIndex = 0;
826
54
    } else {
827
4
      // If this is the first time we failed to merge with the primary GOT,
828
4
      // MergedGots.back() will also be the primary GOT. We must make sure not
829
4
      // to try to merge again with IsPrimary=false, as otherwise, if the
830
4
      // inputs are just right, we could allow the primary GOT to become 1 or 2
831
4
      // words too big due to ignoring the header size.
832
4
      if (MergedGots.size() == 1 ||
833
4
          
!tryMergeGots(MergedGots.back(), SrcGot, false)1
) {
834
3
        MergedGots.emplace_back();
835
3
        std::swap(MergedGots.back(), SrcGot);
836
3
      }
837
4
      File->MipsGotIndex = MergedGots.size() - 1;
838
4
    }
839
58
  }
840
54
  std::swap(Gots, MergedGots);
841
54
842
54
  // Reduce number of "reloc-only" entries in the primary GOT
843
54
  // by substracting "global" entries exist in the primary GOT.
844
54
  PrimGot = &Gots.front();
845
54
  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
846
38
    return PrimGot->Global.count(P.first);
847
38
  });
848
54
849
54
  // Calculate indexes for each GOT entry.
850
54
  size_t Index = HeaderEntriesNum;
851
57
  for (FileGot &Got : Gots) {
852
57
    Got.StartIndex = &Got == PrimGot ? 
054
:
Index3
;
853
57
    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
854
57
         Got.PagesMap) {
855
13
      // For each output section referenced by GOT page relocations calculate
856
13
      // and save into PagesMap an upper bound of MIPS GOT entries required
857
13
      // to store page addresses of local symbols. We assume the worst case -
858
13
      // each 64kb page of the output section has at least one GOT relocation
859
13
      // against it. And take in account the case when the section intersects
860
13
      // page boundaries.
861
13
      P.second.FirstIndex = Index;
862
13
      Index += P.second.Count;
863
13
    }
864
57
    for (auto &P: Got.Local16)
865
10.0k
      P.second = Index++;
866
57
    for (auto &P: Got.Global)
867
34
      P.second = Index++;
868
57
    for (auto &P: Got.Relocs)
869
6
      P.second = Index++;
870
57
    for (auto &P: Got.Tls)
871
12
      P.second = Index++;
872
57
    for (auto &P: Got.DynTlsSymbols) {
873
17
      P.second = Index;
874
17
      Index += 2;
875
17
    }
876
57
  }
877
54
878
54
  // Update Symbol::GotIndex field to use this
879
54
  // value later in the `sortMipsSymbols` function.
880
54
  for (auto &P : PrimGot->Global)
881
32
    P.first->GotIndex = P.second;
882
54
  for (auto &P : PrimGot->Relocs)
883
6
    P.first->GotIndex = P.second;
884
54
885
54
  // Create dynamic relocations.
886
57
  for (FileGot &Got : Gots) {
887
57
    // Create dynamic relocations for TLS entries.
888
57
    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
889
12
      Symbol *S = P.first;
890
12
      uint64_t Offset = P.second * Config->Wordsize;
891
12
      if (S->IsPreemptible)
892
9
        In.RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
893
12
    }
894
57
    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
895
17
      Symbol *S = P.first;
896
17
      uint64_t Offset = P.second * Config->Wordsize;
897
17
      if (S == nullptr) {
898
6
        if (!Config->Pic)
899
3
          continue;
900
3
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
901
11
      } else {
902
11
        // When building a shared library we still need a dynamic relocation
903
11
        // for the module index. Therefore only checking for
904
11
        // S->IsPreemptible is not sufficient (this happens e.g. for
905
11
        // thread-locals that have been marked as local through a linker script)
906
11
        if (!S->IsPreemptible && 
!Config->Pic4
)
907
3
          continue;
908
8
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
909
8
        // However, we can skip writing the TLS offset reloc for non-preemptible
910
8
        // symbols since it is known even in shared libraries
911
8
        if (!S->IsPreemptible)
912
1
          continue;
913
7
        Offset += Config->Wordsize;
914
7
        In.RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
915
7
      }
916
17
    }
917
57
918
57
    // Do not create dynamic relocations for non-TLS
919
57
    // entries in the primary GOT.
920
57
    if (&Got == PrimGot)
921
54
      continue;
922
3
923
3
    // Dynamic relocations for "global" entries.
924
3
    for (const std::pair<Symbol *, size_t> &P : Got.Global) {
925
2
      uint64_t Offset = P.second * Config->Wordsize;
926
2
      In.RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
927
2
    }
928
3
    if (!Config->Pic)
929
1
      continue;
930
2
    // Dynamic relocations for "local" entries in case of PIC.
931
2
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
932
2
         Got.PagesMap) {
933
1
      size_t PageCount = L.second.Count;
934
7
      for (size_t PI = 0; PI < PageCount; 
++PI6
) {
935
6
        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
936
6
        In.RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
937
6
                              int64_t(PI * 0x10000)});
938
6
      }
939
1
    }
940
2
    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
941
1
      uint64_t Offset = P.second * Config->Wordsize;
942
1
      In.RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
943
1
                            P.first.first, P.first.second});
944
1
    }
945
2
  }
946
54
}
947
948
374
bool MipsGotSection::isNeeded() const {
949
374
  // We add the .got section to the result for dynamic MIPS target because
950
374
  // its address and properties are mentioned in the .dynamic section.
951
374
  return !Config->Relocatable;
952
374
}
953
954
10.3k
uint64_t MipsGotSection::getGp(const InputFile *F) const {
955
10.3k
  // For files without related GOT or files refer a primary GOT
956
10.3k
  // returns "common" _gp value. For secondary GOTs calculate
957
10.3k
  // individual _gp values.
958
10.3k
  if (!F || 
!F->MipsGotIndex.hasValue()10.1k
||
*F->MipsGotIndex == 010.1k
)
959
338
    return ElfSym::MipsGp->getVA(0);
960
10.0k
  return getVA() + Gots[*F->MipsGotIndex].StartIndex * Config->Wordsize +
961
10.0k
         0x7ff0;
962
10.0k
}
963
964
173
void MipsGotSection::writeTo(uint8_t *Buf) {
965
173
  // Set the MSB of the second GOT slot. This is not required by any
966
173
  // MIPS ABI documentation, though.
967
173
  //
968
173
  // There is a comment in glibc saying that "The MSB of got[1] of a
969
173
  // gnu object is set to identify gnu objects," and in GNU gold it
970
173
  // says "the second entry will be used by some runtime loaders".
971
173
  // But how this field is being used is unclear.
972
173
  //
973
173
  // We are not really willing to mimic other linkers behaviors
974
173
  // without understanding why they do that, but because all files
975
173
  // generated by GNU tools have this special GOT value, and because
976
173
  // we've been doing this for years, it is probably a safe bet to
977
173
  // keep doing this for now. We really need to revisit this to see
978
173
  // if we had to do this.
979
173
  writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
980
173
  for (const FileGot &G : Gots) {
981
10.1k
    auto Write = [&](size_t I, const Symbol *S, int64_t A) {
982
10.1k
      uint64_t VA = A;
983
10.1k
      if (S)
984
10.0k
        VA = S->getVA(A);
985
10.1k
      writeUint(Buf + I * Config->Wordsize, VA);
986
10.1k
    };
987
57
    // Write 'page address' entries to the local part of the GOT.
988
57
    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
989
57
         G.PagesMap) {
990
13
      size_t PageCount = L.second.Count;
991
13
      uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
992
49
      for (size_t PI = 0; PI < PageCount; 
++PI36
)
993
36
        Write(L.second.FirstIndex + PI, nullptr, FirstPageAddr + PI * 0x10000);
994
13
    }
995
57
    // Local, global, TLS, reloc-only  entries.
996
57
    // If TLS entry has a corresponding dynamic relocations, leave it
997
57
    // initialized by zero. Write down adjusted TLS symbol's values otherwise.
998
57
    // To calculate the adjustments use offsets for thread-local storage.
999
57
    // https://www.linux-mips.org/wiki/NPTL
1000
57
    for (const std::pair<GotEntry, size_t> &P : G.Local16)
1001
10.0k
      Write(P.second, P.first.first, P.first.second);
1002
57
    // Write VA to the primary GOT only. For secondary GOTs that
1003
57
    // will be done by REL32 dynamic relocations.
1004
57
    if (&G == &Gots.front())
1005
54
      for (const std::pair<const Symbol *, size_t> &P : G.Global)
1006
32
        Write(P.second, P.first, 0);
1007
57
    for (const std::pair<Symbol *, size_t> &P : G.Relocs)
1008
6
      Write(P.second, P.first, 0);
1009
57
    for (const std::pair<Symbol *, size_t> &P : G.Tls)
1010
12
      Write(P.second, P.first, P.first->IsPreemptible ? 
09
:
-0x70003
);
1011
57
    for (const std::pair<Symbol *, size_t> &P : G.DynTlsSymbols) {
1012
17
      if (P.first == nullptr && 
!Config->Pic6
)
1013
3
        Write(P.second, nullptr, 1);
1014
14
      else if (P.first && 
!P.first->IsPreemptible11
) {
1015
4
        // If we are emitting PIC code with relocations we mustn't write
1016
4
        // anything to the GOT here. When using Elf_Rel relocations the value
1017
4
        // one will be treated as an addend and will cause crashes at runtime
1018
4
        if (!Config->Pic)
1019
3
          Write(P.second, nullptr, 1);
1020
4
        Write(P.second + 1, P.first, -0x8000);
1021
4
      }
1022
17
    }
1023
57
  }
1024
173
}
1025
1026
// On PowerPC the .plt section is used to hold the table of function addresses
1027
// instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
1028
// section. I don't know why we have a BSS style type for the section but it is
1029
// consitent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
1030
GotPltSection::GotPltSection()
1031
    : SyntheticSection(SHF_ALLOC | SHF_WRITE,
1032
                       Config->EMachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1033
                       Target->GotPltEntrySize,
1034
2.52k
                       Config->EMachine == EM_PPC64 ? ".plt" : ".got.plt") {}
1035
1036
324
void GotPltSection::addEntry(Symbol &Sym) {
1037
324
  assert(Sym.PltIndex == Entries.size());
1038
324
  Entries.push_back(&Sym);
1039
324
}
1040
1041
1.17k
size_t GotPltSection::getSize() const {
1042
1.17k
  return (Target->GotPltHeaderEntriesNum + Entries.size()) *
1043
1.17k
         Target->GotPltEntrySize;
1044
1.17k
}
1045
1046
247
void GotPltSection::writeTo(uint8_t *Buf) {
1047
247
  Target->writeGotPltHeader(Buf);
1048
247
  Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
1049
294
  for (const Symbol *B : Entries) {
1050
294
    Target->writeGotPlt(Buf, *B);
1051
294
    Buf += Config->Wordsize;
1052
294
  }
1053
247
}
1054
1055
4.82k
bool GotPltSection::isNeeded() const {
1056
4.82k
  // We need to emit GOTPLT even if it's empty if there's a relocation relative
1057
4.82k
  // to it.
1058
4.82k
  return !Entries.empty() || 
HasGotPltOffRel4.35k
;
1059
4.82k
}
1060
1061
2.52k
static StringRef getIgotPltName() {
1062
2.52k
  // On ARM the IgotPltSection is part of the GotSection.
1063
2.52k
  if (Config->EMachine == EM_ARM)
1064
155
    return ".got";
1065
2.37k
1066
2.37k
  // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection
1067
2.37k
  // needs to be named the same.
1068
2.37k
  if (Config->EMachine == EM_PPC64)
1069
156
    return ".plt";
1070
2.21k
1071
2.21k
  return ".got.plt";
1072
2.21k
}
1073
1074
// On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1075
// with the IgotPltSection.
1076
IgotPltSection::IgotPltSection()
1077
    : SyntheticSection(SHF_ALLOC | SHF_WRITE,
1078
                       Config->EMachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1079
2.52k
                       Target->GotPltEntrySize, getIgotPltName()) {}
1080
1081
57
void IgotPltSection::addEntry(Symbol &Sym) {
1082
57
  assert(Sym.PltIndex == Entries.size());
1083
57
  Entries.push_back(&Sym);
1084
57
}
1085
1086
175
size_t IgotPltSection::getSize() const {
1087
175
  return Entries.size() * Target->GotPltEntrySize;
1088
175
}
1089
1090
40
void IgotPltSection::writeTo(uint8_t *Buf) {
1091
53
  for (const Symbol *B : Entries) {
1092
53
    Target->writeIgotPlt(Buf, *B);
1093
53
    Buf += Config->Wordsize;
1094
53
  }
1095
40
}
1096
1097
StringTableSection::StringTableSection(StringRef Name, bool Dynamic)
1098
    : SyntheticSection(Dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name),
1099
7.57k
      Dynamic(Dynamic) {
1100
7.57k
  // ELF string tables start with a NUL byte.
1101
7.57k
  addString("");
1102
7.57k
}
1103
1104
// Adds a string to the string table. If HashIt is true we hash and check for
1105
// duplicates. It is optional because the name of global symbols are already
1106
// uniqued and hashing them again has a big cost for a small value: uniquing
1107
// them with some other string that happens to be the same.
1108
446k
unsigned StringTableSection::addString(StringRef S, bool HashIt) {
1109
446k
  if (HashIt) {
1110
427k
    auto R = StringMap.insert(std::make_pair(S, this->Size));
1111
427k
    if (!R.second)
1112
66.1k
      return R.first->second;
1113
380k
  }
1114
380k
  unsigned Ret = this->Size;
1115
380k
  this->Size = this->Size + S.size() + 1;
1116
380k
  Strings.push_back(S);
1117
380k
  return Ret;
1118
380k
}
1119
1120
5.89k
void StringTableSection::writeTo(uint8_t *Buf) {
1121
378k
  for (StringRef S : Strings) {
1122
378k
    memcpy(Buf, S.data(), S.size());
1123
378k
    Buf[S.size()] = '\0';
1124
378k
    Buf += S.size() + 1;
1125
378k
  }
1126
5.89k
}
1127
1128
// Returns the number of version definition entries. Because the first entry
1129
// is for the version definition itself, it is the number of versioned symbols
1130
// plus one. Note that we don't support multiple versions yet.
1131
326
static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
1132
1133
template <class ELFT>
1134
DynamicSection<ELFT>::DynamicSection()
1135
    : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, Config->Wordsize,
1136
2.52k
                       ".dynamic") {
1137
2.52k
  this->Entsize = ELFT::Is64Bits ? 
162.07k
:
8452
;
1138
2.52k
1139
2.52k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1140
2.52k
  // which passes -z rodynamic.
1141
2.52k
  // See "Special Section" in Chapter 4 in the following document:
1142
2.52k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1143
2.52k
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic2.33k
)
1144
200
    this->Flags = SHF_ALLOC;
1145
2.52k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::DynamicSection()
Line
Count
Source
1136
319
                       ".dynamic") {
1137
319
  this->Entsize = ELFT::Is64Bits ? 
160
: 8;
1138
319
1139
319
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1140
319
  // which passes -z rodynamic.
1141
319
  // See "Special Section" in Chapter 4 in the following document:
1142
319
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1143
319
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic296
)
1144
23
    this->Flags = SHF_ALLOC;
1145
319
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::DynamicSection()
Line
Count
Source
1136
133
                       ".dynamic") {
1137
133
  this->Entsize = ELFT::Is64Bits ? 
160
: 8;
1138
133
1139
133
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1140
133
  // which passes -z rodynamic.
1141
133
  // See "Special Section" in Chapter 4 in the following document:
1142
133
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1143
133
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic7
)
1144
126
    this->Flags = SHF_ALLOC;
1145
133
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::DynamicSection()
Line
Count
Source
1136
1.96k
                       ".dynamic") {
1137
1.96k
  this->Entsize = ELFT::Is64Bits ? 16 : 
80
;
1138
1.96k
1139
1.96k
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1140
1.96k
  // which passes -z rodynamic.
1141
1.96k
  // See "Special Section" in Chapter 4 in the following document:
1142
1.96k
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1143
1.96k
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic1.95k
)
1144
6
    this->Flags = SHF_ALLOC;
1145
1.96k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::DynamicSection()
Line
Count
Source
1136
115
                       ".dynamic") {
1137
115
  this->Entsize = ELFT::Is64Bits ? 16 : 
80
;
1138
115
1139
115
  // .dynamic section is not writable on MIPS and on Fuchsia OS
1140
115
  // which passes -z rodynamic.
1141
115
  // See "Special Section" in Chapter 4 in the following document:
1142
115
  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1143
115
  if (Config->EMachine == EM_MIPS || 
Config->ZRodynamic70
)
1144
45
    this->Flags = SHF_ALLOC;
1145
115
}
1146
1147
template <class ELFT>
1148
100
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1149
100
  Entries.push_back({Tag, Fn});
1150
100
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1148
8
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1149
8
  Entries.push_back({Tag, Fn});
1150
8
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1148
69
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1149
69
  Entries.push_back({Tag, Fn});
1150
69
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::add(int, std::__1::function<unsigned long long ()>)
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::add(int, std::__1::function<unsigned long long ()>)
Line
Count
Source
1148
23
void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) {
1149
23
  Entries.push_back({Tag, Fn});
1150
23
}
1151
1152
template <class ELFT>
1153
5.34k
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1154
5.34k
  Entries.push_back({Tag, [=] 
{ return Val; }5.31k
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1154
738
  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
1154
624
  Entries.push_back({Tag, [=] { return Val; }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInt(int, unsigned long long)::'lambda'()::operator()() const
Line
Count
Source
1154
3.59k
  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
1154
354
  Entries.push_back({Tag, [=] { return Val; }});
1155
5.34k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInt(int, unsigned long long)
Line
Count
Source
1153
738
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1154
738
  Entries.push_back({Tag, [=] { return Val; }});
1155
738
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInt(int, unsigned long long)
Line
Count
Source
1153
624
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1154
624
  Entries.push_back({Tag, [=] { return Val; }});
1155
624
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInt(int, unsigned long long)
Line
Count
Source
1153
3.63k
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1154
3.63k
  Entries.push_back({Tag, [=] { return Val; }});
1155
3.63k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInt(int, unsigned long long)
Line
Count
Source
1153
354
void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) {
1154
354
  Entries.push_back({Tag, [=] { return Val; }});
1155
354
}
1156
1157
template <class ELFT>
1158
5.39k
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1159
5.39k
  Entries.push_back({Tag, [=] 
{ return Sec->getVA(0); }5.35k
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1159
728
  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
1159
334
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSec(int, lld::elf::InputSection*)::'lambda'()::operator()() const
Line
Count
Source
1159
4.01k
  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
1159
272
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1160
5.39k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1158
728
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1159
728
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1160
728
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1158
334
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1159
334
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1160
334
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1158
4.06k
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1159
4.06k
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1160
4.06k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addInSec(int, lld::elf::InputSection*)
Line
Count
Source
1158
272
void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) {
1159
272
  Entries.push_back({Tag, [=] { return Sec->getVA(0); }});
1160
272
}
1161
1162
template <class ELFT>
1163
29
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1164
29
  size_t TagOffset = Entries.size() * Entsize;
1165
29
  Entries.push_back(
1166
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
1166
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
1166
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
1166
6
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1167
29
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1163
2
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1164
2
  size_t TagOffset = Entries.size() * Entsize;
1165
2
  Entries.push_back(
1166
2
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1167
2
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addInSecRelative(int, lld::elf::InputSection*)
Line
Count
Source
1163
21
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1164
21
  size_t TagOffset = Entries.size() * Entsize;
1165
21
  Entries.push_back(
1166
21
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1167
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
1163
6
void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) {
1164
6
  size_t TagOffset = Entries.size() * Entsize;
1165
6
  Entries.push_back(
1166
6
      {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }});
1167
6
}
1168
1169
template <class ELFT>
1170
8
void DynamicSection<ELFT>::addOutSec(int32_t Tag, OutputSection *Sec) {
1171
8
  Entries.push_back({Tag, [=] { return Sec->Addr; }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1171
8
  Entries.push_back({Tag, [=] { return Sec->Addr; }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addOutSec(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
1172
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addOutSec(int, lld::elf::OutputSection*)
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addOutSec(int, lld::elf::OutputSection*)
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addOutSec(int, lld::elf::OutputSection*)
Line
Count
Source
1170
8
void DynamicSection<ELFT>::addOutSec(int32_t Tag, OutputSection *Sec) {
1171
8
  Entries.push_back({Tag, [=] { return Sec->Addr; }});
1172
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addOutSec(int, lld::elf::OutputSection*)
1173
1174
template <class ELFT>
1175
256
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1176
256
  Entries.push_back({Tag, [=] 
{ return Sec->Size; }253
});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSize(int, lld::elf::OutputSection*)::'lambda'()::operator()() const
Line
Count
Source
1176
56
  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
1176
8
  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
1176
168
  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
1176
21
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1177
256
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1175
56
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1176
56
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1177
56
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1175
8
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1176
8
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1177
8
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1175
171
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1176
171
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1177
171
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSize(int, lld::elf::OutputSection*)
Line
Count
Source
1175
21
void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) {
1176
21
  Entries.push_back({Tag, [=] { return Sec->Size; }});
1177
21
}
1178
1179
template <class ELFT>
1180
8
void DynamicSection<ELFT>::addSym(int32_t Tag, Symbol *Sym) {
1181
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
1181
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
1182
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
1180
8
void DynamicSection<ELFT>::addSym(int32_t Tag, Symbol *Sym) {
1181
8
  Entries.push_back({Tag, [=] { return Sym->getVA(); }});
1182
8
}
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSym(int, lld::elf::Symbol*)
1183
1184
// A Linker script may assign the RELA relocation sections to the same
1185
// output section. When this occurs we cannot just use the OutputSection
1186
// Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
1187
// overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1188
242
static uint64_t addPltRelSz() {
1189
242
  size_t Size = In.RelaPlt->getSize();
1190
242
  if (In.RelaIplt->getParent() == In.RelaPlt->getParent() &&
1191
242
      
In.RelaIplt->Name == In.RelaPlt->Name222
)
1192
221
    Size += In.RelaIplt->getSize();
1193
242
  return Size;
1194
242
}
1195
1196
// Add remaining entries to complete .dynamic contents.
1197
1.16k
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1198
1.16k
  for (StringRef S : Config->FilterList)
1199
6
    addInt(DT_FILTER, In.DynStrTab->addString(S));
1200
1.16k
  for (StringRef S : Config->AuxiliaryList)
1201
4
    addInt(DT_AUXILIARY, In.DynStrTab->addString(S));
1202
1.16k
1203
1.16k
  if (!Config->Rpath.empty())
1204
7
    addInt(Config->EnableNewDtags ? 
DT_RUNPATH6
:
DT_RPATH1
,
1205
7
           In.DynStrTab->addString(Config->Rpath));
1206
1.16k
1207
1.16k
  for (SharedFile *File : SharedFiles)
1208
341
    if (File->IsNeeded)
1209
324
      addInt(DT_NEEDED, In.DynStrTab->addString(File->SoName));
1210
1.16k
  if (!Config->SoName.empty())
1211
31
    addInt(DT_SONAME, In.DynStrTab->addString(Config->SoName));
1212
1.16k
1213
1.16k
  // Set DT_FLAGS and DT_FLAGS_1.
1214
1.16k
  uint32_t DtFlags = 0;
1215
1.16k
  uint32_t DtFlags1 = 0;
1216
1.16k
  if (Config->Bsymbolic)
1217
8
    DtFlags |= DF_SYMBOLIC;
1218
1.16k
  if (Config->ZGlobal)
1219
1
    DtFlags1 |= DF_1_GLOBAL;
1220
1.16k
  if (Config->ZInitfirst)
1221
1
    DtFlags1 |= DF_1_INITFIRST;
1222
1.16k
  if (Config->ZInterpose)
1223
1
    DtFlags1 |= DF_1_INTERPOSE;
1224
1.16k
  if (Config->ZNodefaultlib)
1225
1
    DtFlags1 |= DF_1_NODEFLIB;
1226
1.16k
  if (Config->ZNodelete)
1227
1
    DtFlags1 |= DF_1_NODELETE;
1228
1.16k
  if (Config->ZNodlopen)
1229
1
    DtFlags1 |= DF_1_NOOPEN;
1230
1.16k
  if (Config->ZNow) {
1231
4
    DtFlags |= DF_BIND_NOW;
1232
4
    DtFlags1 |= DF_1_NOW;
1233
4
  }
1234
1.16k
  if (Config->ZOrigin) {
1235
1
    DtFlags |= DF_ORIGIN;
1236
1
    DtFlags1 |= DF_1_ORIGIN;
1237
1
  }
1238
1.16k
  if (!Config->ZText)
1239
11
    DtFlags |= DF_TEXTREL;
1240
1.16k
  if (Config->HasStaticTlsModel)
1241
16
    DtFlags |= DF_STATIC_TLS;
1242
1.16k
1243
1.16k
  if (DtFlags)
1244
38
    addInt(DT_FLAGS, DtFlags);
1245
1.16k
  if (DtFlags1)
1246
4
    addInt(DT_FLAGS_1, DtFlags1);
1247
1.16k
1248
1.16k
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1249
1.16k
  // need it for each process, so we don't write it for DSOs. The loader writes
1250
1.16k
  // the pointer into this entry.
1251
1.16k
  //
1252
1.16k
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1253
1.16k
  // systems (currently only Fuchsia OS) provide other means to give the
1254
1.16k
  // debugger this information. Such systems may choose make .dynamic read-only.
1255
1.16k
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1256
1.16k
  if (!Config->Shared && 
!Config->Relocatable329
&&
!Config->ZRodynamic327
)
1257
326
    addInt(DT_DEBUG, 0);
1258
1.16k
1259
1.16k
  if (OutputSection *Sec = In.DynStrTab->getParent())
1260
1.16k
    this->Link = Sec->SectionIndex;
1261
1.16k
1262
1.16k
  if (In.RelaDyn->isNeeded()) {
1263
245
    addInSec(In.RelaDyn->DynamicTag, In.RelaDyn);
1264
245
    addSize(In.RelaDyn->SizeDynamicTag, In.RelaDyn->getParent());
1265
245
1266
245
    bool IsRela = Config->IsRela;
1267
245
    addInt(IsRela ? 
DT_RELAENT177
:
DT_RELENT68
,
1268
245
           IsRela ? 
sizeof(Elf_Rela)177
:
sizeof(Elf_Rel)68
);
1269
245
1270
245
    // MIPS dynamic loader does not support RELCOUNT tag.
1271
245
    // The problem is in the tight relation between dynamic
1272
245
    // relocations and GOT. So do not emit this tag on MIPS.
1273
245
    if (Config->EMachine != EM_MIPS) {
1274
229
      size_t NumRelativeRels = In.RelaDyn->getRelativeRelocCount();
1275
229
      if (Config->ZCombreloc && 
NumRelativeRels228
)
1276
51
        addInt(IsRela ? 
DT_RELACOUNT37
:
DT_RELCOUNT14
, NumRelativeRels);
1277
229
    }
1278
245
  }
1279
1.16k
  if (In.RelrDyn && 
!In.RelrDyn->Relocs.empty()4
) {
1280
3
    addInSec(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELR0
: DT_RELR,
1281
3
             In.RelrDyn);
1282
3
    addSize(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELRSZ0
: DT_RELRSZ,
1283
3
            In.RelrDyn->getParent());
1284
3
    addInt(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELRENT0
: DT_RELRENT,
1285
3
           sizeof(Elf_Relr));
1286
3
  }
1287
1.16k
  // .rel[a].plt section usually consists of two parts, containing plt and
1288
1.16k
  // iplt relocations. It is possible to have only iplt relocations in the
1289
1.16k
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1290
1.16k
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1291
1.16k
  // case, so here we always use RelaPlt as marker for the begining of
1292
1.16k
  // .rel[a].plt section.
1293
1.16k
  if (In.RelaPlt->getParent()->Live) {
1294
243
    addInSec(DT_JMPREL, In.RelaPlt);
1295
243
    Entries.push_back({DT_PLTRELSZ, addPltRelSz});
1296
243
    switch (Config->EMachine) {
1297
243
    case EM_MIPS:
1298
15
      addInSec(DT_MIPS_PLTGOT, In.GotPlt);
1299
15
      break;
1300
243
    case EM_SPARCV9:
1301
0
      addInSec(DT_PLTGOT, In.Plt);
1302
0
      break;
1303
243
    default:
1304
228
      addInSec(DT_PLTGOT, In.GotPlt);
1305
228
      break;
1306
243
    }
1307
243
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA192
:
DT_REL51
);
1308
243
  }
1309
1.16k
1310
1.16k
  addInSec(DT_SYMTAB, In.DynSymTab);
1311
1.16k
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1312
1.16k
  addInSec(DT_STRTAB, In.DynStrTab);
1313
1.16k
  addInt(DT_STRSZ, In.DynStrTab->getSize());
1314
1.16k
  if (!Config->ZText)
1315
11
    addInt(DT_TEXTREL, 0);
1316
1.16k
  if (In.GnuHashTab)
1317
929
    addInSec(DT_GNU_HASH, In.GnuHashTab);
1318
1.16k
  if (In.HashTab)
1319
1.15k
    addInSec(DT_HASH, In.HashTab);
1320
1.16k
1321
1.16k
  if (Out::PreinitArray) {
1322
3
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1323
3
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1324
3
  }
1325
1.16k
  if (Out::InitArray) {
1326
3
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1327
3
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1328
3
  }
1329
1.16k
  if (Out::FiniArray) {
1330
2
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1331
2
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1332
2
  }
1333
1.16k
1334
1.16k
  if (Symbol *B = Symtab->find(Config->Init))
1335
6
    if (B->isDefined())
1336
4
      addSym(DT_INIT, B);
1337
1.16k
  if (Symbol *B = Symtab->find(Config->Fini))
1338
6
    if (B->isDefined())
1339
4
      addSym(DT_FINI, B);
1340
1.16k
1341
1.16k
  bool HasVerNeed = SharedFile::VernauxNum != 0;
1342
1.16k
  if (HasVerNeed || 
In.VerDef1.15k
)
1343
59
    addInSec(DT_VERSYM, In.VerSym);
1344
1.16k
  if (In.VerDef) {
1345
52
    addInSec(DT_VERDEF, In.VerDef);
1346
52
    addInt(DT_VERDEFNUM, getVerDefNum());
1347
52
  }
1348
1.16k
  if (HasVerNeed) {
1349
8
    addInSec(DT_VERNEED, In.VerNeed);
1350
8
    unsigned NeedNum = 0;
1351
8
    for (SharedFile *F : SharedFiles)
1352
9
      if (!F->Vernauxs.empty())
1353
9
        ++NeedNum;
1354
8
    addInt(DT_VERNEEDNUM, NeedNum);
1355
8
  }
1356
1.16k
1357
1.16k
  if (Config->EMachine == EM_MIPS) {
1358
100
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
100
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
100
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
100
    addInt(DT_MIPS_SYMTABNO, In.DynSymTab->getNumSymbols());
1362
100
1363
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
1363
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
1363
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
1363
23
    add(DT_MIPS_LOCAL_GOTNO, [] { return In.MipsGot->getLocalEntriesNum(); });
1364
100
1365
100
    if (const Symbol *B = In.MipsGot->getFirstGlobalEntry())
1366
32
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
68
    else
1368
68
      addInt(DT_MIPS_GOTSYM, In.DynSymTab->getNumSymbols());
1369
100
    addInSec(DT_PLTGOT, In.MipsGot);
1370
100
    if (In.MipsRldMap) {
1371
29
      if (!Config->Pie)
1372
26
        addInSec(DT_MIPS_RLD_MAP, In.MipsRldMap);
1373
29
      // Store the offset to the .rld_map section
1374
29
      // relative to the address of the tag.
1375
29
      addInSecRelative(DT_MIPS_RLD_MAP_REL, In.MipsRldMap);
1376
29
    }
1377
100
  }
1378
1.16k
1379
1.16k
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
1.16k
  if (Config->EMachine == EM_PPC64 && 
In.Plt->isNeeded()76
) {
1381
24
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
24
    // stub, which starts directly after the header.
1383
24
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
24
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
24
                         return In.Plt->getVA(0) + Offset;
1386
24
                       }});
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()::'lambda0'()::operator()() const
Unexecuted instantiation: lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()::'lambda0'()::operator()() const
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()::'lambda0'()::operator()() const
Line
Count
Source
1383
13
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
13
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
13
                         return In.Plt->getVA(0) + Offset;
1386
13
                       }});
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()::'lambda0'()::operator()() const
Line
Count
Source
1383
11
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
11
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
11
                         return In.Plt->getVA(0) + Offset;
1386
11
                       }});
1387
24
  }
1388
1.16k
1389
1.16k
  addInt(DT_NULL, 0);
1390
1.16k
1391
1.16k
  getParent()->Link = this->Link;
1392
1.16k
  this->Size = Entries.size() * this->Entsize;
1393
1.16k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeContents()
Line
Count
Source
1197
157
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1198
157
  for (StringRef S : Config->FilterList)
1199
0
    addInt(DT_FILTER, In.DynStrTab->addString(S));
1200
157
  for (StringRef S : Config->AuxiliaryList)
1201
0
    addInt(DT_AUXILIARY, In.DynStrTab->addString(S));
1202
157
1203
157
  if (!Config->Rpath.empty())
1204
2
    addInt(Config->EnableNewDtags ? DT_RUNPATH : 
DT_RPATH0
,
1205
2
           In.DynStrTab->addString(Config->Rpath));
1206
157
1207
157
  for (SharedFile *File : SharedFiles)
1208
55
    if (File->IsNeeded)
1209
49
      addInt(DT_NEEDED, In.DynStrTab->addString(File->SoName));
1210
157
  if (!Config->SoName.empty())
1211
6
    addInt(DT_SONAME, In.DynStrTab->addString(Config->SoName));
1212
157
1213
157
  // Set DT_FLAGS and DT_FLAGS_1.
1214
157
  uint32_t DtFlags = 0;
1215
157
  uint32_t DtFlags1 = 0;
1216
157
  if (Config->Bsymbolic)
1217
2
    DtFlags |= DF_SYMBOLIC;
1218
157
  if (Config->ZGlobal)
1219
0
    DtFlags1 |= DF_1_GLOBAL;
1220
157
  if (Config->ZInitfirst)
1221
0
    DtFlags1 |= DF_1_INITFIRST;
1222
157
  if (Config->ZInterpose)
1223
0
    DtFlags1 |= DF_1_INTERPOSE;
1224
157
  if (Config->ZNodefaultlib)
1225
0
    DtFlags1 |= DF_1_NODEFLIB;
1226
157
  if (Config->ZNodelete)
1227
0
    DtFlags1 |= DF_1_NODELETE;
1228
157
  if (Config->ZNodlopen)
1229
0
    DtFlags1 |= DF_1_NOOPEN;
1230
157
  if (Config->ZNow) {
1231
0
    DtFlags |= DF_BIND_NOW;
1232
0
    DtFlags1 |= DF_1_NOW;
1233
0
  }
1234
157
  if (Config->ZOrigin) {
1235
0
    DtFlags |= DF_ORIGIN;
1236
0
    DtFlags1 |= DF_1_ORIGIN;
1237
0
  }
1238
157
  if (!Config->ZText)
1239
1
    DtFlags |= DF_TEXTREL;
1240
157
  if (Config->HasStaticTlsModel)
1241
10
    DtFlags |= DF_STATIC_TLS;
1242
157
1243
157
  if (DtFlags)
1244
13
    addInt(DT_FLAGS, DtFlags);
1245
157
  if (DtFlags1)
1246
0
    addInt(DT_FLAGS_1, DtFlags1);
1247
157
1248
157
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1249
157
  // need it for each process, so we don't write it for DSOs. The loader writes
1250
157
  // the pointer into this entry.
1251
157
  //
1252
157
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1253
157
  // systems (currently only Fuchsia OS) provide other means to give the
1254
157
  // debugger this information. Such systems may choose make .dynamic read-only.
1255
157
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1256
157
  if (!Config->Shared && 
!Config->Relocatable46
&&
!Config->ZRodynamic46
)
1257
46
    addInt(DT_DEBUG, 0);
1258
157
1259
157
  if (OutputSection *Sec = In.DynStrTab->getParent())
1260
157
    this->Link = Sec->SectionIndex;
1261
157
1262
157
  if (In.RelaDyn->isNeeded()) {
1263
54
    addInSec(In.RelaDyn->DynamicTag, In.RelaDyn);
1264
54
    addSize(In.RelaDyn->SizeDynamicTag, In.RelaDyn->getParent());
1265
54
1266
54
    bool IsRela = Config->IsRela;
1267
54
    addInt(IsRela ? 
DT_RELAENT1
:
DT_RELENT53
,
1268
54
           IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)53
);
1269
54
1270
54
    // MIPS dynamic loader does not support RELCOUNT tag.
1271
54
    // The problem is in the tight relation between dynamic
1272
54
    // relocations and GOT. So do not emit this tag on MIPS.
1273
54
    if (Config->EMachine != EM_MIPS) {
1274
53
      size_t NumRelativeRels = In.RelaDyn->getRelativeRelocCount();
1275
53
      if (Config->ZCombreloc && NumRelativeRels)
1276
14
        addInt(IsRela ? 
DT_RELACOUNT0
: DT_RELCOUNT, NumRelativeRels);
1277
53
    }
1278
54
  }
1279
157
  if (In.RelrDyn && 
!In.RelrDyn->Relocs.empty()2
) {
1280
2
    addInSec(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELR0
: DT_RELR,
1281
2
             In.RelrDyn);
1282
2
    addSize(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELRSZ0
: DT_RELRSZ,
1283
2
            In.RelrDyn->getParent());
1284
2
    addInt(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELRENT0
: DT_RELRENT,
1285
2
           sizeof(Elf_Relr));
1286
2
  }
1287
157
  // .rel[a].plt section usually consists of two parts, containing plt and
1288
157
  // iplt relocations. It is possible to have only iplt relocations in the
1289
157
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1290
157
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1291
157
  // case, so here we always use RelaPlt as marker for the begining of
1292
157
  // .rel[a].plt section.
1293
157
  if (In.RelaPlt->getParent()->Live) {
1294
40
    addInSec(DT_JMPREL, In.RelaPlt);
1295
40
    Entries.push_back({DT_PLTRELSZ, addPltRelSz});
1296
40
    switch (Config->EMachine) {
1297
40
    case EM_MIPS:
1298
2
      addInSec(DT_MIPS_PLTGOT, In.GotPlt);
1299
2
      break;
1300
40
    case EM_SPARCV9:
1301
0
      addInSec(DT_PLTGOT, In.Plt);
1302
0
      break;
1303
40
    default:
1304
38
      addInSec(DT_PLTGOT, In.GotPlt);
1305
38
      break;
1306
40
    }
1307
40
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA2
:
DT_REL38
);
1308
40
  }
1309
157
1310
157
  addInSec(DT_SYMTAB, In.DynSymTab);
1311
157
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1312
157
  addInSec(DT_STRTAB, In.DynStrTab);
1313
157
  addInt(DT_STRSZ, In.DynStrTab->getSize());
1314
157
  if (!Config->ZText)
1315
1
    addInt(DT_TEXTREL, 0);
1316
157
  if (In.GnuHashTab)
1317
113
    addInSec(DT_GNU_HASH, In.GnuHashTab);
1318
157
  if (In.HashTab)
1319
155
    addInSec(DT_HASH, In.HashTab);
1320
157
1321
157
  if (Out::PreinitArray) {
1322
0
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1323
0
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1324
0
  }
1325
157
  if (Out::InitArray) {
1326
0
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1327
0
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1328
0
  }
1329
157
  if (Out::FiniArray) {
1330
0
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1331
0
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1332
0
  }
1333
157
1334
157
  if (Symbol *B = Symtab->find(Config->Init))
1335
0
    if (B->isDefined())
1336
0
      addSym(DT_INIT, B);
1337
157
  if (Symbol *B = Symtab->find(Config->Fini))
1338
0
    if (B->isDefined())
1339
0
      addSym(DT_FINI, B);
1340
157
1341
157
  bool HasVerNeed = SharedFile::VernauxNum != 0;
1342
157
  if (HasVerNeed || In.VerDef)
1343
0
    addInSec(DT_VERSYM, In.VerSym);
1344
157
  if (In.VerDef) {
1345
0
    addInSec(DT_VERDEF, In.VerDef);
1346
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1347
0
  }
1348
157
  if (HasVerNeed) {
1349
0
    addInSec(DT_VERNEED, In.VerNeed);
1350
0
    unsigned NeedNum = 0;
1351
0
    for (SharedFile *F : SharedFiles)
1352
0
      if (!F->Vernauxs.empty())
1353
0
        ++NeedNum;
1354
0
    addInt(DT_VERNEEDNUM, NeedNum);
1355
0
  }
1356
157
1357
157
  if (Config->EMachine == EM_MIPS) {
1358
8
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
8
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
8
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
8
    addInt(DT_MIPS_SYMTABNO, In.DynSymTab->getNumSymbols());
1362
8
1363
8
    add(DT_MIPS_LOCAL_GOTNO, [] { return In.MipsGot->getLocalEntriesNum(); });
1364
8
1365
8
    if (const Symbol *B = In.MipsGot->getFirstGlobalEntry())
1366
3
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
5
    else
1368
5
      addInt(DT_MIPS_GOTSYM, In.DynSymTab->getNumSymbols());
1369
8
    addInSec(DT_PLTGOT, In.MipsGot);
1370
8
    if (In.MipsRldMap) {
1371
2
      if (!Config->Pie)
1372
2
        addInSec(DT_MIPS_RLD_MAP, In.MipsRldMap);
1373
2
      // Store the offset to the .rld_map section
1374
2
      // relative to the address of the tag.
1375
2
      addInSecRelative(DT_MIPS_RLD_MAP_REL, In.MipsRldMap);
1376
2
    }
1377
8
  }
1378
157
1379
157
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
157
  if (Config->EMachine == EM_PPC64 && 
In.Plt->isNeeded()0
) {
1381
0
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
0
    // stub, which starts directly after the header.
1383
0
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
0
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
0
                         return In.Plt->getVA(0) + Offset;
1386
0
                       }});
1387
0
  }
1388
157
1389
157
  addInt(DT_NULL, 0);
1390
157
1391
157
  getParent()->Link = this->Link;
1392
157
  this->Size = Entries.size() * this->Entsize;
1393
157
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeContents()
Line
Count
Source
1197
71
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1198
71
  for (StringRef S : Config->FilterList)
1199
0
    addInt(DT_FILTER, In.DynStrTab->addString(S));
1200
71
  for (StringRef S : Config->AuxiliaryList)
1201
0
    addInt(DT_AUXILIARY, In.DynStrTab->addString(S));
1202
71
1203
71
  if (!Config->Rpath.empty())
1204
0
    addInt(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
1205
0
           In.DynStrTab->addString(Config->Rpath));
1206
71
1207
71
  for (SharedFile *File : SharedFiles)
1208
24
    if (File->IsNeeded)
1209
24
      addInt(DT_NEEDED, In.DynStrTab->addString(File->SoName));
1210
71
  if (!Config->SoName.empty())
1211
0
    addInt(DT_SONAME, In.DynStrTab->addString(Config->SoName));
1212
71
1213
71
  // Set DT_FLAGS and DT_FLAGS_1.
1214
71
  uint32_t DtFlags = 0;
1215
71
  uint32_t DtFlags1 = 0;
1216
71
  if (Config->Bsymbolic)
1217
1
    DtFlags |= DF_SYMBOLIC;
1218
71
  if (Config->ZGlobal)
1219
0
    DtFlags1 |= DF_1_GLOBAL;
1220
71
  if (Config->ZInitfirst)
1221
0
    DtFlags1 |= DF_1_INITFIRST;
1222
71
  if (Config->ZInterpose)
1223
0
    DtFlags1 |= DF_1_INTERPOSE;
1224
71
  if (Config->ZNodefaultlib)
1225
0
    DtFlags1 |= DF_1_NODEFLIB;
1226
71
  if (Config->ZNodelete)
1227
0
    DtFlags1 |= DF_1_NODELETE;
1228
71
  if (Config->ZNodlopen)
1229
0
    DtFlags1 |= DF_1_NOOPEN;
1230
71
  if (Config->ZNow) {
1231
0
    DtFlags |= DF_BIND_NOW;
1232
0
    DtFlags1 |= DF_1_NOW;
1233
0
  }
1234
71
  if (Config->ZOrigin) {
1235
0
    DtFlags |= DF_ORIGIN;
1236
0
    DtFlags1 |= DF_1_ORIGIN;
1237
0
  }
1238
71
  if (!Config->ZText)
1239
0
    DtFlags |= DF_TEXTREL;
1240
71
  if (Config->HasStaticTlsModel)
1241
0
    DtFlags |= DF_STATIC_TLS;
1242
71
1243
71
  if (DtFlags)
1244
1
    addInt(DT_FLAGS, DtFlags);
1245
71
  if (DtFlags1)
1246
0
    addInt(DT_FLAGS_1, DtFlags1);
1247
71
1248
71
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1249
71
  // need it for each process, so we don't write it for DSOs. The loader writes
1250
71
  // the pointer into this entry.
1251
71
  //
1252
71
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1253
71
  // systems (currently only Fuchsia OS) provide other means to give the
1254
71
  // debugger this information. Such systems may choose make .dynamic read-only.
1255
71
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1256
71
  if (!Config->Shared && 
!Config->Relocatable21
&&
!Config->ZRodynamic21
)
1257
21
    addInt(DT_DEBUG, 0);
1258
71
1259
71
  if (OutputSection *Sec = In.DynStrTab->getParent())
1260
71
    this->Link = Sec->SectionIndex;
1261
71
1262
71
  if (In.RelaDyn->isNeeded()) {
1263
8
    addInSec(In.RelaDyn->DynamicTag, In.RelaDyn);
1264
8
    addSize(In.RelaDyn->SizeDynamicTag, In.RelaDyn->getParent());
1265
8
1266
8
    bool IsRela = Config->IsRela;
1267
8
    addInt(IsRela ? 
DT_RELAENT1
:
DT_RELENT7
,
1268
8
           IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)7
);
1269
8
1270
8
    // MIPS dynamic loader does not support RELCOUNT tag.
1271
8
    // The problem is in the tight relation between dynamic
1272
8
    // relocations and GOT. So do not emit this tag on MIPS.
1273
8
    if (Config->EMachine != EM_MIPS) {
1274
1
      size_t NumRelativeRels = In.RelaDyn->getRelativeRelocCount();
1275
1
      if (Config->ZCombreloc && NumRelativeRels)
1276
0
        addInt(IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels);
1277
1
    }
1278
8
  }
1279
71
  if (In.RelrDyn && 
!In.RelrDyn->Relocs.empty()0
) {
1280
0
    addInSec(Config->UseAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
1281
0
             In.RelrDyn);
1282
0
    addSize(Config->UseAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
1283
0
            In.RelrDyn->getParent());
1284
0
    addInt(Config->UseAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
1285
0
           sizeof(Elf_Relr));
1286
0
  }
1287
71
  // .rel[a].plt section usually consists of two parts, containing plt and
1288
71
  // iplt relocations. It is possible to have only iplt relocations in the
1289
71
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1290
71
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1291
71
  // case, so here we always use RelaPlt as marker for the begining of
1292
71
  // .rel[a].plt section.
1293
71
  if (In.RelaPlt->getParent()->Live) {
1294
11
    addInSec(DT_JMPREL, In.RelaPlt);
1295
11
    Entries.push_back({DT_PLTRELSZ, addPltRelSz});
1296
11
    switch (Config->EMachine) {
1297
11
    case EM_MIPS:
1298
11
      addInSec(DT_MIPS_PLTGOT, In.GotPlt);
1299
11
      break;
1300
11
    case EM_SPARCV9:
1301
0
      addInSec(DT_PLTGOT, In.Plt);
1302
0
      break;
1303
11
    default:
1304
0
      addInSec(DT_PLTGOT, In.GotPlt);
1305
0
      break;
1306
11
    }
1307
11
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA0
: DT_REL);
1308
11
  }
1309
71
1310
71
  addInSec(DT_SYMTAB, In.DynSymTab);
1311
71
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1312
71
  addInSec(DT_STRTAB, In.DynStrTab);
1313
71
  addInt(DT_STRSZ, In.DynStrTab->getSize());
1314
71
  if (!Config->ZText)
1315
0
    addInt(DT_TEXTREL, 0);
1316
71
  if (In.GnuHashTab)
1317
1
    addInSec(DT_GNU_HASH, In.GnuHashTab);
1318
71
  if (In.HashTab)
1319
71
    addInSec(DT_HASH, In.HashTab);
1320
71
1321
71
  if (Out::PreinitArray) {
1322
0
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1323
0
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1324
0
  }
1325
71
  if (Out::InitArray) {
1326
0
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1327
0
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1328
0
  }
1329
71
  if (Out::FiniArray) {
1330
0
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1331
0
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1332
0
  }
1333
71
1334
71
  if (Symbol *B = Symtab->find(Config->Init))
1335
0
    if (B->isDefined())
1336
0
      addSym(DT_INIT, B);
1337
71
  if (Symbol *B = Symtab->find(Config->Fini))
1338
0
    if (B->isDefined())
1339
0
      addSym(DT_FINI, B);
1340
71
1341
71
  bool HasVerNeed = SharedFile::VernauxNum != 0;
1342
71
  if (HasVerNeed || 
In.VerDef70
)
1343
1
    addInSec(DT_VERSYM, In.VerSym);
1344
71
  if (In.VerDef) {
1345
0
    addInSec(DT_VERDEF, In.VerDef);
1346
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1347
0
  }
1348
71
  if (HasVerNeed) {
1349
1
    addInSec(DT_VERNEED, In.VerNeed);
1350
1
    unsigned NeedNum = 0;
1351
1
    for (SharedFile *F : SharedFiles)
1352
1
      if (!F->Vernauxs.empty())
1353
1
        ++NeedNum;
1354
1
    addInt(DT_VERNEEDNUM, NeedNum);
1355
1
  }
1356
71
1357
71
  if (Config->EMachine == EM_MIPS) {
1358
69
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
69
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
69
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
69
    addInt(DT_MIPS_SYMTABNO, In.DynSymTab->getNumSymbols());
1362
69
1363
69
    add(DT_MIPS_LOCAL_GOTNO, [] { return In.MipsGot->getLocalEntriesNum(); });
1364
69
1365
69
    if (const Symbol *B = In.MipsGot->getFirstGlobalEntry())
1366
23
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
46
    else
1368
46
      addInt(DT_MIPS_GOTSYM, In.DynSymTab->getNumSymbols());
1369
69
    addInSec(DT_PLTGOT, In.MipsGot);
1370
69
    if (In.MipsRldMap) {
1371
21
      if (!Config->Pie)
1372
19
        addInSec(DT_MIPS_RLD_MAP, In.MipsRldMap);
1373
21
      // Store the offset to the .rld_map section
1374
21
      // relative to the address of the tag.
1375
21
      addInSecRelative(DT_MIPS_RLD_MAP_REL, In.MipsRldMap);
1376
21
    }
1377
69
  }
1378
71
1379
71
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
71
  if (Config->EMachine == EM_PPC64 && 
In.Plt->isNeeded()0
) {
1381
0
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
0
    // stub, which starts directly after the header.
1383
0
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
0
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
0
                         return In.Plt->getVA(0) + Offset;
1386
0
                       }});
1387
0
  }
1388
71
1389
71
  addInt(DT_NULL, 0);
1390
71
1391
71
  getParent()->Link = this->Link;
1392
71
  this->Size = Entries.size() * this->Entsize;
1393
71
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeContents()
Line
Count
Source
1197
882
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1198
882
  for (StringRef S : Config->FilterList)
1199
6
    addInt(DT_FILTER, In.DynStrTab->addString(S));
1200
882
  for (StringRef S : Config->AuxiliaryList)
1201
4
    addInt(DT_AUXILIARY, In.DynStrTab->addString(S));
1202
882
1203
882
  if (!Config->Rpath.empty())
1204
4
    addInt(Config->EnableNewDtags ? 
DT_RUNPATH3
:
DT_RPATH1
,
1205
4
           In.DynStrTab->addString(Config->Rpath));
1206
882
1207
882
  for (SharedFile *File : SharedFiles)
1208
245
    if (File->IsNeeded)
1209
234
      addInt(DT_NEEDED, In.DynStrTab->addString(File->SoName));
1210
882
  if (!Config->SoName.empty())
1211
25
    addInt(DT_SONAME, In.DynStrTab->addString(Config->SoName));
1212
882
1213
882
  // Set DT_FLAGS and DT_FLAGS_1.
1214
882
  uint32_t DtFlags = 0;
1215
882
  uint32_t DtFlags1 = 0;
1216
882
  if (Config->Bsymbolic)
1217
4
    DtFlags |= DF_SYMBOLIC;
1218
882
  if (Config->ZGlobal)
1219
1
    DtFlags1 |= DF_1_GLOBAL;
1220
882
  if (Config->ZInitfirst)
1221
1
    DtFlags1 |= DF_1_INITFIRST;
1222
882
  if (Config->ZInterpose)
1223
1
    DtFlags1 |= DF_1_INTERPOSE;
1224
882
  if (Config->ZNodefaultlib)
1225
1
    DtFlags1 |= DF_1_NODEFLIB;
1226
882
  if (Config->ZNodelete)
1227
1
    DtFlags1 |= DF_1_NODELETE;
1228
882
  if (Config->ZNodlopen)
1229
1
    DtFlags1 |= DF_1_NOOPEN;
1230
882
  if (Config->ZNow) {
1231
4
    DtFlags |= DF_BIND_NOW;
1232
4
    DtFlags1 |= DF_1_NOW;
1233
4
  }
1234
882
  if (Config->ZOrigin) {
1235
1
    DtFlags |= DF_ORIGIN;
1236
1
    DtFlags1 |= DF_1_ORIGIN;
1237
1
  }
1238
882
  if (!Config->ZText)
1239
8
    DtFlags |= DF_TEXTREL;
1240
882
  if (Config->HasStaticTlsModel)
1241
6
    DtFlags |= DF_STATIC_TLS;
1242
882
1243
882
  if (DtFlags)
1244
21
    addInt(DT_FLAGS, DtFlags);
1245
882
  if (DtFlags1)
1246
4
    addInt(DT_FLAGS_1, DtFlags1);
1247
882
1248
882
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1249
882
  // need it for each process, so we don't write it for DSOs. The loader writes
1250
882
  // the pointer into this entry.
1251
882
  //
1252
882
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1253
882
  // systems (currently only Fuchsia OS) provide other means to give the
1254
882
  // debugger this information. Such systems may choose make .dynamic read-only.
1255
882
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1256
882
  if (!Config->Shared && 
!Config->Relocatable247
&&
!Config->ZRodynamic245
)
1257
244
    addInt(DT_DEBUG, 0);
1258
882
1259
882
  if (OutputSection *Sec = In.DynStrTab->getParent())
1260
881
    this->Link = Sec->SectionIndex;
1261
882
1262
882
  if (In.RelaDyn->isNeeded()) {
1263
162
    addInSec(In.RelaDyn->DynamicTag, In.RelaDyn);
1264
162
    addSize(In.RelaDyn->SizeDynamicTag, In.RelaDyn->getParent());
1265
162
1266
162
    bool IsRela = Config->IsRela;
1267
162
    addInt(IsRela ? DT_RELAENT : 
DT_RELENT0
,
1268
162
           IsRela ? sizeof(Elf_Rela) : 
sizeof(Elf_Rel)0
);
1269
162
1270
162
    // MIPS dynamic loader does not support RELCOUNT tag.
1271
162
    // The problem is in the tight relation between dynamic
1272
162
    // relocations and GOT. So do not emit this tag on MIPS.
1273
162
    if (Config->EMachine != EM_MIPS) {
1274
162
      size_t NumRelativeRels = In.RelaDyn->getRelativeRelocCount();
1275
162
      if (Config->ZCombreloc && 
NumRelativeRels161
)
1276
35
        addInt(IsRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, NumRelativeRels);
1277
162
    }
1278
162
  }
1279
882
  if (In.RelrDyn && 
!In.RelrDyn->Relocs.empty()2
) {
1280
1
    addInSec(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELR0
: DT_RELR,
1281
1
             In.RelrDyn);
1282
1
    addSize(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELRSZ0
: DT_RELRSZ,
1283
1
            In.RelrDyn->getParent());
1284
1
    addInt(Config->UseAndroidRelrTags ? 
DT_ANDROID_RELRENT0
: DT_RELRENT,
1285
1
           sizeof(Elf_Relr));
1286
1
  }
1287
882
  // .rel[a].plt section usually consists of two parts, containing plt and
1288
882
  // iplt relocations. It is possible to have only iplt relocations in the
1289
882
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1290
882
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1291
882
  // case, so here we always use RelaPlt as marker for the begining of
1292
882
  // .rel[a].plt section.
1293
882
  if (In.RelaPlt->getParent()->Live) {
1294
179
    addInSec(DT_JMPREL, In.RelaPlt);
1295
179
    Entries.push_back({DT_PLTRELSZ, addPltRelSz});
1296
179
    switch (Config->EMachine) {
1297
179
    case EM_MIPS:
1298
0
      addInSec(DT_MIPS_PLTGOT, In.GotPlt);
1299
0
      break;
1300
179
    case EM_SPARCV9:
1301
0
      addInSec(DT_PLTGOT, In.Plt);
1302
0
      break;
1303
179
    default:
1304
179
      addInSec(DT_PLTGOT, In.GotPlt);
1305
179
      break;
1306
179
    }
1307
179
    addInt(DT_PLTREL, Config->IsRela ? DT_RELA : 
DT_REL0
);
1308
179
  }
1309
882
1310
882
  addInSec(DT_SYMTAB, In.DynSymTab);
1311
882
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1312
882
  addInSec(DT_STRTAB, In.DynStrTab);
1313
882
  addInt(DT_STRSZ, In.DynStrTab->getSize());
1314
882
  if (!Config->ZText)
1315
8
    addInt(DT_TEXTREL, 0);
1316
882
  if (In.GnuHashTab)
1317
783
    addInSec(DT_GNU_HASH, In.GnuHashTab);
1318
882
  if (In.HashTab)
1319
875
    addInSec(DT_HASH, In.HashTab);
1320
882
1321
882
  if (Out::PreinitArray) {
1322
3
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1323
3
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1324
3
  }
1325
882
  if (Out::InitArray) {
1326
3
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1327
3
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1328
3
  }
1329
882
  if (Out::FiniArray) {
1330
2
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1331
2
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1332
2
  }
1333
882
1334
882
  if (Symbol *B = Symtab->find(Config->Init))
1335
6
    if (B->isDefined())
1336
4
      addSym(DT_INIT, B);
1337
882
  if (Symbol *B = Symtab->find(Config->Fini))
1338
6
    if (B->isDefined())
1339
4
      addSym(DT_FINI, B);
1340
882
1341
882
  bool HasVerNeed = SharedFile::VernauxNum != 0;
1342
882
  if (HasVerNeed || 
In.VerDef875
)
1343
58
    addInSec(DT_VERSYM, In.VerSym);
1344
882
  if (In.VerDef) {
1345
52
    addInSec(DT_VERDEF, In.VerDef);
1346
52
    addInt(DT_VERDEFNUM, getVerDefNum());
1347
52
  }
1348
882
  if (HasVerNeed) {
1349
7
    addInSec(DT_VERNEED, In.VerNeed);
1350
7
    unsigned NeedNum = 0;
1351
7
    for (SharedFile *F : SharedFiles)
1352
8
      if (!F->Vernauxs.empty())
1353
8
        ++NeedNum;
1354
7
    addInt(DT_VERNEEDNUM, NeedNum);
1355
7
  }
1356
882
1357
882
  if (Config->EMachine == EM_MIPS) {
1358
0
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
0
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
0
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
0
    addInt(DT_MIPS_SYMTABNO, In.DynSymTab->getNumSymbols());
1362
0
1363
0
    add(DT_MIPS_LOCAL_GOTNO, [] { return In.MipsGot->getLocalEntriesNum(); });
1364
0
1365
0
    if (const Symbol *B = In.MipsGot->getFirstGlobalEntry())
1366
0
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
0
    else
1368
0
      addInt(DT_MIPS_GOTSYM, In.DynSymTab->getNumSymbols());
1369
0
    addInSec(DT_PLTGOT, In.MipsGot);
1370
0
    if (In.MipsRldMap) {
1371
0
      if (!Config->Pie)
1372
0
        addInSec(DT_MIPS_RLD_MAP, In.MipsRldMap);
1373
0
      // Store the offset to the .rld_map section
1374
0
      // relative to the address of the tag.
1375
0
      addInSecRelative(DT_MIPS_RLD_MAP_REL, In.MipsRldMap);
1376
0
    }
1377
0
  }
1378
882
1379
882
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
882
  if (Config->EMachine == EM_PPC64 && 
In.Plt->isNeeded()44
) {
1381
13
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
13
    // stub, which starts directly after the header.
1383
13
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
13
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
13
                         return In.Plt->getVA(0) + Offset;
1386
13
                       }});
1387
13
  }
1388
882
1389
882
  addInt(DT_NULL, 0);
1390
882
1391
882
  getParent()->Link = this->Link;
1392
882
  this->Size = Entries.size() * this->Entsize;
1393
882
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeContents()
Line
Count
Source
1197
55
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1198
55
  for (StringRef S : Config->FilterList)
1199
0
    addInt(DT_FILTER, In.DynStrTab->addString(S));
1200
55
  for (StringRef S : Config->AuxiliaryList)
1201
0
    addInt(DT_AUXILIARY, In.DynStrTab->addString(S));
1202
55
1203
55
  if (!Config->Rpath.empty())
1204
1
    addInt(Config->EnableNewDtags ? DT_RUNPATH : 
DT_RPATH0
,
1205
1
           In.DynStrTab->addString(Config->Rpath));
1206
55
1207
55
  for (SharedFile *File : SharedFiles)
1208
17
    if (File->IsNeeded)
1209
17
      addInt(DT_NEEDED, In.DynStrTab->addString(File->SoName));
1210
55
  if (!Config->SoName.empty())
1211
0
    addInt(DT_SONAME, In.DynStrTab->addString(Config->SoName));
1212
55
1213
55
  // Set DT_FLAGS and DT_FLAGS_1.
1214
55
  uint32_t DtFlags = 0;
1215
55
  uint32_t DtFlags1 = 0;
1216
55
  if (Config->Bsymbolic)
1217
1
    DtFlags |= DF_SYMBOLIC;
1218
55
  if (Config->ZGlobal)
1219
0
    DtFlags1 |= DF_1_GLOBAL;
1220
55
  if (Config->ZInitfirst)
1221
0
    DtFlags1 |= DF_1_INITFIRST;
1222
55
  if (Config->ZInterpose)
1223
0
    DtFlags1 |= DF_1_INTERPOSE;
1224
55
  if (Config->ZNodefaultlib)
1225
0
    DtFlags1 |= DF_1_NODEFLIB;
1226
55
  if (Config->ZNodelete)
1227
0
    DtFlags1 |= DF_1_NODELETE;
1228
55
  if (Config->ZNodlopen)
1229
0
    DtFlags1 |= DF_1_NOOPEN;
1230
55
  if (Config->ZNow) {
1231
0
    DtFlags |= DF_BIND_NOW;
1232
0
    DtFlags1 |= DF_1_NOW;
1233
0
  }
1234
55
  if (Config->ZOrigin) {
1235
0
    DtFlags |= DF_ORIGIN;
1236
0
    DtFlags1 |= DF_1_ORIGIN;
1237
0
  }
1238
55
  if (!Config->ZText)
1239
2
    DtFlags |= DF_TEXTREL;
1240
55
  if (Config->HasStaticTlsModel)
1241
0
    DtFlags |= DF_STATIC_TLS;
1242
55
1243
55
  if (DtFlags)
1244
3
    addInt(DT_FLAGS, DtFlags);
1245
55
  if (DtFlags1)
1246
0
    addInt(DT_FLAGS_1, DtFlags1);
1247
55
1248
55
  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1249
55
  // need it for each process, so we don't write it for DSOs. The loader writes
1250
55
  // the pointer into this entry.
1251
55
  //
1252
55
  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1253
55
  // systems (currently only Fuchsia OS) provide other means to give the
1254
55
  // debugger this information. Such systems may choose make .dynamic read-only.
1255
55
  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1256
55
  if (!Config->Shared && 
!Config->Relocatable15
&&
!Config->ZRodynamic15
)
1257
15
    addInt(DT_DEBUG, 0);
1258
55
1259
55
  if (OutputSection *Sec = In.DynStrTab->getParent())
1260
55
    this->Link = Sec->SectionIndex;
1261
55
1262
55
  if (In.RelaDyn->isNeeded()) {
1263
21
    addInSec(In.RelaDyn->DynamicTag, In.RelaDyn);
1264
21
    addSize(In.RelaDyn->SizeDynamicTag, In.RelaDyn->getParent());
1265
21
1266
21
    bool IsRela = Config->IsRela;
1267
21
    addInt(IsRela ? 
DT_RELAENT13
:
DT_RELENT8
,
1268
21
           IsRela ? 
sizeof(Elf_Rela)13
:
sizeof(Elf_Rel)8
);
1269
21
1270
21
    // MIPS dynamic loader does not support RELCOUNT tag.
1271
21
    // The problem is in the tight relation between dynamic
1272
21
    // relocations and GOT. So do not emit this tag on MIPS.
1273
21
    if (Config->EMachine != EM_MIPS) {
1274
13
      size_t NumRelativeRels = In.RelaDyn->getRelativeRelocCount();
1275
13
      if (Config->ZCombreloc && NumRelativeRels)
1276
2
        addInt(IsRela ? DT_RELACOUNT : 
DT_RELCOUNT0
, NumRelativeRels);
1277
13
    }
1278
21
  }
1279
55
  if (In.RelrDyn && 
!In.RelrDyn->Relocs.empty()0
) {
1280
0
    addInSec(Config->UseAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
1281
0
             In.RelrDyn);
1282
0
    addSize(Config->UseAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
1283
0
            In.RelrDyn->getParent());
1284
0
    addInt(Config->UseAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
1285
0
           sizeof(Elf_Relr));
1286
0
  }
1287
55
  // .rel[a].plt section usually consists of two parts, containing plt and
1288
55
  // iplt relocations. It is possible to have only iplt relocations in the
1289
55
  // output. In that case RelaPlt is empty and have zero offset, the same offset
1290
55
  // as RelaIplt have. And we still want to emit proper dynamic tags for that
1291
55
  // case, so here we always use RelaPlt as marker for the begining of
1292
55
  // .rel[a].plt section.
1293
55
  if (In.RelaPlt->getParent()->Live) {
1294
13
    addInSec(DT_JMPREL, In.RelaPlt);
1295
13
    Entries.push_back({DT_PLTRELSZ, addPltRelSz});
1296
13
    switch (Config->EMachine) {
1297
13
    case EM_MIPS:
1298
2
      addInSec(DT_MIPS_PLTGOT, In.GotPlt);
1299
2
      break;
1300
13
    case EM_SPARCV9:
1301
0
      addInSec(DT_PLTGOT, In.Plt);
1302
0
      break;
1303
13
    default:
1304
11
      addInSec(DT_PLTGOT, In.GotPlt);
1305
11
      break;
1306
13
    }
1307
13
    addInt(DT_PLTREL, Config->IsRela ? 
DT_RELA11
:
DT_REL2
);
1308
13
  }
1309
55
1310
55
  addInSec(DT_SYMTAB, In.DynSymTab);
1311
55
  addInt(DT_SYMENT, sizeof(Elf_Sym));
1312
55
  addInSec(DT_STRTAB, In.DynStrTab);
1313
55
  addInt(DT_STRSZ, In.DynStrTab->getSize());
1314
55
  if (!Config->ZText)
1315
2
    addInt(DT_TEXTREL, 0);
1316
55
  if (In.GnuHashTab)
1317
32
    addInSec(DT_GNU_HASH, In.GnuHashTab);
1318
55
  if (In.HashTab)
1319
55
    addInSec(DT_HASH, In.HashTab);
1320
55
1321
55
  if (Out::PreinitArray) {
1322
0
    addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray);
1323
0
    addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray);
1324
0
  }
1325
55
  if (Out::InitArray) {
1326
0
    addOutSec(DT_INIT_ARRAY, Out::InitArray);
1327
0
    addSize(DT_INIT_ARRAYSZ, Out::InitArray);
1328
0
  }
1329
55
  if (Out::FiniArray) {
1330
0
    addOutSec(DT_FINI_ARRAY, Out::FiniArray);
1331
0
    addSize(DT_FINI_ARRAYSZ, Out::FiniArray);
1332
0
  }
1333
55
1334
55
  if (Symbol *B = Symtab->find(Config->Init))
1335
0
    if (B->isDefined())
1336
0
      addSym(DT_INIT, B);
1337
55
  if (Symbol *B = Symtab->find(Config->Fini))
1338
0
    if (B->isDefined())
1339
0
      addSym(DT_FINI, B);
1340
55
1341
55
  bool HasVerNeed = SharedFile::VernauxNum != 0;
1342
55
  if (HasVerNeed || In.VerDef)
1343
0
    addInSec(DT_VERSYM, In.VerSym);
1344
55
  if (In.VerDef) {
1345
0
    addInSec(DT_VERDEF, In.VerDef);
1346
0
    addInt(DT_VERDEFNUM, getVerDefNum());
1347
0
  }
1348
55
  if (HasVerNeed) {
1349
0
    addInSec(DT_VERNEED, In.VerNeed);
1350
0
    unsigned NeedNum = 0;
1351
0
    for (SharedFile *F : SharedFiles)
1352
0
      if (!F->Vernauxs.empty())
1353
0
        ++NeedNum;
1354
0
    addInt(DT_VERNEEDNUM, NeedNum);
1355
0
  }
1356
55
1357
55
  if (Config->EMachine == EM_MIPS) {
1358
23
    addInt(DT_MIPS_RLD_VERSION, 1);
1359
23
    addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1360
23
    addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase());
1361
23
    addInt(DT_MIPS_SYMTABNO, In.DynSymTab->getNumSymbols());
1362
23
1363
23
    add(DT_MIPS_LOCAL_GOTNO, [] { return In.MipsGot->getLocalEntriesNum(); });
1364
23
1365
23
    if (const Symbol *B = In.MipsGot->getFirstGlobalEntry())
1366
6
      addInt(DT_MIPS_GOTSYM, B->DynsymIndex);
1367
17
    else
1368
17
      addInt(DT_MIPS_GOTSYM, In.DynSymTab->getNumSymbols());
1369
23
    addInSec(DT_PLTGOT, In.MipsGot);
1370
23
    if (In.MipsRldMap) {
1371
6
      if (!Config->Pie)
1372
5
        addInSec(DT_MIPS_RLD_MAP, In.MipsRldMap);
1373
6
      // Store the offset to the .rld_map section
1374
6
      // relative to the address of the tag.
1375
6
      addInSecRelative(DT_MIPS_RLD_MAP_REL, In.MipsRldMap);
1376
6
    }
1377
23
  }
1378
55
1379
55
  // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1380
55
  if (Config->EMachine == EM_PPC64 && 
In.Plt->isNeeded()32
) {
1381
11
    // The Glink tag points to 32 bytes before the first lazy symbol resolution
1382
11
    // stub, which starts directly after the header.
1383
11
    Entries.push_back({DT_PPC64_GLINK, [=] {
1384
11
                         unsigned Offset = Target->PltHeaderSize - 32;
1385
11
                         return In.Plt->getVA(0) + Offset;
1386
11
                       }});
1387
11
  }
1388
55
1389
55
  addInt(DT_NULL, 0);
1390
55
1391
55
  getParent()->Link = this->Link;
1392
55
  this->Size = Entries.size() * this->Entsize;
1393
55
}
1394
1395
1.15k
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
1.15k
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
1.15k
1398
11.3k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
11.3k
    P->d_tag = KV.first;
1400
11.3k
    P->d_un.d_val = KV.second();
1401
11.3k
    ++P;
1402
11.3k
  }
1403
1.15k
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1395
157
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
157
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
157
1398
1.57k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
1.57k
    P->d_tag = KV.first;
1400
1.57k
    P->d_un.d_val = KV.second();
1401
1.57k
    ++P;
1402
1.57k
  }
1403
157
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1395
71
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
71
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
71
1398
1.06k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
1.06k
    P->d_tag = KV.first;
1400
1.06k
    P->d_un.d_val = KV.second();
1401
1.06k
    ++P;
1402
1.06k
  }
1403
71
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1395
873
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
873
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
873
1398
7.98k
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
7.98k
    P->d_tag = KV.first;
1400
7.98k
    P->d_un.d_val = KV.second();
1401
7.98k
    ++P;
1402
7.98k
  }
1403
873
}
lld::elf::DynamicSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1395
55
template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1396
55
  auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1397
55
1398
700
  for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) {
1399
700
    P->d_tag = KV.first;
1400
700
    P->d_un.d_val = KV.second();
1401
700
    ++P;
1402
700
  }
1403
55
}
1404
1405
1.03k
uint64_t DynamicReloc::getOffset() const {
1406
1.03k
  return InputSec->getVA(OffsetInSec);
1407
1.03k
}
1408
1409
691
int64_t DynamicReloc::computeAddend() const {
1410
691
  if (UseSymVA)
1411
202
    return Sym->getVA(Addend);
1412
489
  if (!OutputSec)
1413
489
    return Addend;
1414
0
  // See the comment in the DynamicReloc ctor.
1415
0
  return getMipsPageAddr(OutputSec->Addr) + Addend;
1416
0
}
1417
1418
1.72k
uint32_t DynamicReloc::getSymIndex() const {
1419
1.72k
  if (Sym && 
!UseSymVA1.66k
)
1420
1.16k
    return Sym->DynsymIndex;
1421
562
  return 0;
1422
562
}
1423
1424
RelocationBaseSection::RelocationBaseSection(StringRef Name, uint32_t Type,
1425
                                             int32_t DynamicTag,
1426
                                             int32_t SizeDynamicTag)
1427
    : SyntheticSection(SHF_ALLOC, Type, Config->Wordsize, Name),
1428
7.58k
      DynamicTag(DynamicTag), SizeDynamicTag(SizeDynamicTag) {}
1429
1430
void RelocationBaseSection::addReloc(RelType DynType, InputSectionBase *IS,
1431
138
                                     uint64_t OffsetInSec, Symbol *Sym) {
1432
138
  addReloc({DynType, IS, OffsetInSec, false, Sym, 0});
1433
138
}
1434
1435
void RelocationBaseSection::addReloc(RelType DynType,
1436
                                     InputSectionBase *InputSec,
1437
                                     uint64_t OffsetInSec, Symbol *Sym,
1438
                                     int64_t Addend, RelExpr Expr,
1439
455
                                     RelType Type) {
1440
455
  // Write the addends to the relocated address if required. We skip
1441
455
  // it if the written value would be zero.
1442
455
  if (Config->WriteAddends && 
(151
Expr != R_ADDEND151
||
Addend != 069
))
1443
98
    InputSec->Relocations.push_back({Expr, Type, OffsetInSec, Addend, Sym});
1444
455
  addReloc({DynType, InputSec, OffsetInSec, Expr != R_ADDEND, Sym, Addend});
1445
455
}
1446
1447
989
void RelocationBaseSection::addReloc(const DynamicReloc &Reloc) {
1448
989
  if (Reloc.Type == Target->RelativeRel)
1449
207
    ++NumRelativeRelocs;
1450
989
  Relocs.push_back(Reloc);
1451
989
}
1452
1453
521
void RelocationBaseSection::finalizeContents() {
1454
521
  // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1455
521
  // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1456
521
  // case.
1457
521
  if (In.DynSymTab && 
In.DynSymTab->getParent()494
)
1458
494
    getParent()->Link = In.DynSymTab->getParent()->SectionIndex;
1459
27
  else
1460
27
    getParent()->Link = 0;
1461
521
1462
521
  if (In.RelaPlt == this)
1463
234
    getParent()->Info = In.GotPlt->getParent()->SectionIndex;
1464
521
  if (In.RelaIplt == this)
1465
42
    getParent()->Info = In.IgotPlt->getParent()->SectionIndex;
1466
521
}
1467
1468
RelrBaseSection::RelrBaseSection()
1469
    : SyntheticSection(SHF_ALLOC,
1470
                       Config->UseAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR,
1471
4
                       Config->Wordsize, ".relr.dyn") {}
1472
1473
template <class ELFT>
1474
static void encodeDynamicReloc(typename ELFT::Rela *P,
1475
1.03k
                               const DynamicReloc &Rel) {
1476
1.03k
  if (Config->IsRela)
1477
691
    P->r_addend = Rel.computeAddend();
1478
1.03k
  P->r_offset = Rel.getOffset();
1479
1.03k
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1480
1.03k
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::object::ELFType<(llvm::support::endianness)1, false>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1475
282
                               const DynamicReloc &Rel) {
1476
282
  if (Config->IsRela)
1477
3
    P->r_addend = Rel.computeAddend();
1478
282
  P->r_offset = Rel.getOffset();
1479
282
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1480
282
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)0, false> >(llvm::object::ELFType<(llvm::support::endianness)0, false>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1475
45
                               const DynamicReloc &Rel) {
1476
45
  if (Config->IsRela)
1477
1
    P->r_addend = Rel.computeAddend();
1478
45
  P->r_offset = Rel.getOffset();
1479
45
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1480
45
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::object::ELFType<(llvm::support::endianness)1, true>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1475
643
                               const DynamicReloc &Rel) {
1476
643
  if (Config->IsRela)
1477
643
    P->r_addend = Rel.computeAddend();
1478
643
  P->r_offset = Rel.getOffset();
1479
643
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1480
643
}
SyntheticSections.cpp:void encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)0, true> >(llvm::object::ELFType<(llvm::support::endianness)0, true>::Rela*, lld::elf::DynamicReloc const&)
Line
Count
Source
1475
63
                               const DynamicReloc &Rel) {
1476
63
  if (Config->IsRela)
1477
44
    P->r_addend = Rel.computeAddend();
1478
63
  P->r_offset = Rel.getOffset();
1479
63
  P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1480
63
}
1481
1482
template <class ELFT>
1483
RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
1484
    : RelocationBaseSection(Name, Config->IsRela ? SHT_RELA : SHT_REL,
1485
                            Config->IsRela ? DT_RELA : DT_REL,
1486
                            Config->IsRela ? DT_RELASZ : DT_RELSZ),
1487
7.57k
      Sort(Sort) {
1488
7.57k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)6.12k
:
sizeof(Elf_Rel)1.45k
;
1489
7.57k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1487
956
      Sort(Sort) {
1488
956
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)27
:
sizeof(Elf_Rel)929
;
1489
956
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1487
399
      Sort(Sort) {
1488
399
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)21
:
sizeof(Elf_Rel)378
;
1489
399
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1487
5.87k
      Sort(Sort) {
1488
5.87k
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)5.86k
:
sizeof(Elf_Rel)9
;
1489
5.87k
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelocationSection(llvm::StringRef, bool)
Line
Count
Source
1487
345
      Sort(Sort) {
1488
345
  this->Entsize = Config->IsRela ? 
sizeof(Elf_Rela)207
:
sizeof(Elf_Rel)138
;
1489
345
}
1490
1491
504
static bool compRelocations(const DynamicReloc &A, const DynamicReloc &B) {
1492
504
  bool AIsRel = A.Type == Target->RelativeRel;
1493
504
  bool BIsRel = B.Type == Target->RelativeRel;
1494
504
  if (AIsRel != BIsRel)
1495
159
    return AIsRel;
1496
345
  return A.getSymIndex() < B.getSymIndex();
1497
345
}
1498
1499
512
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1500
512
  if (Sort)
1501
236
    llvm::stable_sort(Relocs, compRelocations);
1502
512
1503
913
  for (const DynamicReloc &Rel : Relocs) {
1504
913
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1505
913
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)625
:
sizeof(Elf_Rel)288
;
1506
913
  }
1507
512
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1499
100
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1500
100
  if (Sort)
1501
53
    llvm::stable_sort(Relocs, compRelocations);
1502
100
1503
228
  for (const DynamicReloc &Rel : Relocs) {
1504
228
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1505
228
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)3
:
sizeof(Elf_Rel)225
;
1506
228
  }
1507
100
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1499
19
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1500
19
  if (Sort)
1501
8
    llvm::stable_sort(Relocs, compRelocations);
1502
19
1503
45
  for (const DynamicReloc &Rel : Relocs) {
1504
45
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1505
45
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)1
:
sizeof(Elf_Rel)44
;
1506
45
  }
1507
19
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1499
358
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1500
358
  if (Sort)
1501
154
    llvm::stable_sort(Relocs, compRelocations);
1502
358
1503
577
  for (const DynamicReloc &Rel : Relocs) {
1504
577
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1505
577
    Buf += Config->IsRela ? sizeof(Elf_Rela) : 
sizeof(Elf_Rel)0
;
1506
577
  }
1507
358
}
lld::elf::RelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1499
35
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1500
35
  if (Sort)
1501
21
    llvm::stable_sort(Relocs, compRelocations);
1502
35
1503
63
  for (const DynamicReloc &Rel : Relocs) {
1504
63
    encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);
1505
63
    Buf += Config->IsRela ? 
sizeof(Elf_Rela)44
:
sizeof(Elf_Rel)19
;
1506
63
  }
1507
35
}
1508
1509
template <class ELFT>
1510
AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1511
    StringRef Name)
1512
    : RelocationBaseSection(
1513
          Name, Config->IsRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1514
          Config->IsRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1515
5
          Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1516
5
  this->Entsize = 1;
1517
5
}
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::AndroidPackedRelocationSection(llvm::StringRef)
Line
Count
Source
1515
1
          Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1516
1
  this->Entsize = 1;
1517
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
1515
4
          Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1516
4
  this->Entsize = 1;
1517
4
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::AndroidPackedRelocationSection(llvm::StringRef)
1518
1519
template <class ELFT>
1520
10
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1521
10
  // This function computes the contents of an Android-format packed relocation
1522
10
  // section.
1523
10
  //
1524
10
  // This format compresses relocations by using relocation groups to factor out
1525
10
  // fields that are common between relocations and storing deltas from previous
1526
10
  // relocations in SLEB128 format (which has a short representation for small
1527
10
  // numbers). A good example of a relocation type with common fields is
1528
10
  // R_*_RELATIVE, which is normally used to represent function pointers in
1529
10
  // vtables. In the REL format, each relative relocation has the same r_info
1530
10
  // field, and is only different from other relative relocations in terms of
1531
10
  // the r_offset field. By sorting relocations by offset, grouping them by
1532
10
  // r_info and representing each relocation with only the delta from the
1533
10
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1534
10
  // byte (or less with run-length encoding). This relocation packer was able to
1535
10
  // reduce the size of the relocation section in an Android Chromium DSO from
1536
10
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1537
10
  //
1538
10
  // A relocation section consists of a header containing the literal bytes
1539
10
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1540
10
  // elements are the total number of relocations in the section and an initial
1541
10
  // r_offset value. The remaining elements define a sequence of relocation
1542
10
  // groups. Each relocation group starts with a header consisting of the
1543
10
  // following elements:
1544
10
  //
1545
10
  // - the number of relocations in the relocation group
1546
10
  // - flags for the relocation group
1547
10
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1548
10
  //   for each relocation in the group.
1549
10
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1550
10
  //   field for each relocation in the group.
1551
10
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1552
10
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1553
10
  //   each relocation in the group.
1554
10
  //
1555
10
  // Following the relocation group header are descriptions of each of the
1556
10
  // relocations in the group. They consist of the following elements:
1557
10
  //
1558
10
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1559
10
  //   delta for this relocation.
1560
10
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1561
10
  //   field for this relocation.
1562
10
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1563
10
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1564
10
  //   this relocation.
1565
10
1566
10
  size_t OldSize = RelocData.size();
1567
10
1568
10
  RelocData = {'A', 'P', 'S', '2'};
1569
10
  raw_svector_ostream OS(RelocData);
1570
250
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()::'lambda'(long long)::operator()(long long) const
Line
Count
Source
1570
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
1570
180
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()::'lambda'(long long)::operator()(long long) const
1571
10
1572
10
  // The format header includes the number of relocations and the initial
1573
10
  // offset (we set this to zero because the first relocation group will
1574
10
  // perform the initial adjustment).
1575
10
  Add(Relocs.size());
1576
10
  Add(0);
1577
10
1578
10
  std::vector<Elf_Rela> Relatives, NonRelatives;
1579
10
1580
120
  for (const DynamicReloc &Rel : Relocs) {
1581
120
    Elf_Rela R;
1582
120
    encodeDynamicReloc<ELFT>(&R, Rel);
1583
120
1584
120
    if (R.getType(Config->IsMips64EL) == Target->RelativeRel)
1585
106
      Relatives.push_back(R);
1586
14
    else
1587
14
      NonRelatives.push_back(R);
1588
120
  }
1589
10
1590
100
  llvm::sort(Relatives, [](const Elf_Rel &A, const Elf_Rel &B) {
1591
100
    return A.r_offset < B.r_offset;
1592
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
1590
48
  llvm::sort(Relatives, [](const Elf_Rel &A, const Elf_Rel &B) {
1591
48
    return A.r_offset < B.r_offset;
1592
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
1590
52
  llvm::sort(Relatives, [](const Elf_Rel &A, const Elf_Rel &B) {
1591
52
    return A.r_offset < B.r_offset;
1592
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
1593
10
1594
10
  // Try to find groups of relative relocations which are spaced one word
1595
10
  // apart from one another. These generally correspond to vtable entries. The
1596
10
  // format allows these groups to be encoded using a sort of run-length
1597
10
  // encoding, but each group will cost 7 bytes in addition to the offset from
1598
10
  // the previous group, so it is only profitable to do this for groups of
1599
10
  // size 8 or larger.
1600
10
  std::vector<Elf_Rela> UngroupedRelatives;
1601
10
  std::vector<std::vector<Elf_Rela>> RelativeGroups;
1602
32
  for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) {
1603
22
    std::vector<Elf_Rela> Group;
1604
106
    do {
1605
106
      Group.push_back(*I++);
1606
106
    } while (I != E && 
(I - 1)->r_offset + Config->Wordsize == I->r_offset100
);
1607
22
1608
22
    if (Group.size() < 8)
1609
14
      UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(),
1610
14
                                Group.end());
1611
8
    else
1612
8
      RelativeGroups.emplace_back(std::move(Group));
1613
22
  }
1614
10
1615
10
  unsigned HasAddendIfRela =
1616
10
      Config->IsRela ? 
RELOCATION_GROUP_HAS_ADDEND_FLAG8
:
02
;
1617
10
1618
10
  uint64_t Offset = 0;
1619
10
  uint64_t Addend = 0;
1620
10
1621
10
  // Emit the run-length encoding for the groups of adjacent relative
1622
10
  // relocations. Each group is represented using two groups in the packed
1623
10
  // format. The first is used to set the current offset to the start of the
1624
10
  // group (and also encodes the first relocation), and the second encodes the
1625
10
  // remaining relocations.
1626
10
  for (std::vector<Elf_Rela> &G : RelativeGroups) {
1627
8
    // The first relocation in the group.
1628
8
    Add(1);
1629
8
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1630
8
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1631
8
    Add(G[0].r_offset - Offset);
1632
8
    Add(Target->RelativeRel);
1633
8
    if (Config->IsRela) {
1634
4
      Add(G[0].r_addend - Addend);
1635
4
      Addend = G[0].r_addend;
1636
4
    }
1637
8
1638
8
    // The remaining relocations.
1639
8
    Add(G.size() - 1);
1640
8
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1641
8
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1642
8
    Add(Config->Wordsize);
1643
8
    Add(Target->RelativeRel);
1644
8
    if (Config->IsRela) {
1645
34
      for (auto I = G.begin() + 1, E = G.end(); I != E; 
++I30
) {
1646
30
        Add(I->r_addend - Addend);
1647
30
        Addend = I->r_addend;
1648
30
      }
1649
4
    }
1650
8
1651
8
    Offset = G.back().r_offset;
1652
8
  }
1653
10
1654
10
  // Now the ungrouped relatives.
1655
10
  if (!UngroupedRelatives.empty()) {
1656
6
    Add(UngroupedRelatives.size());
1657
6
    Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1658
6
    Add(Target->RelativeRel);
1659
38
    for (Elf_Rela &R : UngroupedRelatives) {
1660
38
      Add(R.r_offset - Offset);
1661
38
      Offset = R.r_offset;
1662
38
      if (Config->IsRela) {
1663
22
        Add(R.r_addend - Addend);
1664
22
        Addend = R.r_addend;
1665
22
      }
1666
38
    }
1667
6
  }
1668
10
1669
10
  // Finally the non-relative relocations.
1670
10
  llvm::sort(NonRelatives, [](const Elf_Rela &A, const Elf_Rela &B) {
1671
6
    return A.r_offset < B.r_offset;
1672
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
1670
2
  llvm::sort(NonRelatives, [](const Elf_Rela &A, const Elf_Rela &B) {
1671
2
    return A.r_offset < B.r_offset;
1672
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
1670
4
  llvm::sort(NonRelatives, [](const Elf_Rela &A, const Elf_Rela &B) {
1671
4
    return A.r_offset < B.r_offset;
1672
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
1673
10
  if (!NonRelatives.empty()) {
1674
8
    Add(NonRelatives.size());
1675
8
    Add(HasAddendIfRela);
1676
14
    for (Elf_Rela &R : NonRelatives) {
1677
14
      Add(R.r_offset - Offset);
1678
14
      Offset = R.r_offset;
1679
14
      Add(R.r_info);
1680
14
      if (Config->IsRela) {
1681
10
        Add(R.r_addend - Addend);
1682
10
        Addend = R.r_addend;
1683
10
      }
1684
14
    }
1685
8
  }
1686
10
1687
10
  // Don't allow the section to shrink; otherwise the size of the section can
1688
10
  // oscillate infinitely.
1689
10
  if (RelocData.size() < OldSize)
1690
1
    RelocData.append(OldSize - RelocData.size(), 0);
1691
10
1692
10
  // Returns whether the section size changed. We need to keep recomputing both
1693
10
  // section layout and the contents of this section until the size converges
1694
10
  // because changing this section's size can affect section layout, which in
1695
10
  // turn can affect the sizes of the LEB-encoded integers stored in this
1696
10
  // section.
1697
10
  return RelocData.size() != OldSize;
1698
10
}
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()
Line
Count
Source
1520
2
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1521
2
  // This function computes the contents of an Android-format packed relocation
1522
2
  // section.
1523
2
  //
1524
2
  // This format compresses relocations by using relocation groups to factor out
1525
2
  // fields that are common between relocations and storing deltas from previous
1526
2
  // relocations in SLEB128 format (which has a short representation for small
1527
2
  // numbers). A good example of a relocation type with common fields is
1528
2
  // R_*_RELATIVE, which is normally used to represent function pointers in
1529
2
  // vtables. In the REL format, each relative relocation has the same r_info
1530
2
  // field, and is only different from other relative relocations in terms of
1531
2
  // the r_offset field. By sorting relocations by offset, grouping them by
1532
2
  // r_info and representing each relocation with only the delta from the
1533
2
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1534
2
  // byte (or less with run-length encoding). This relocation packer was able to
1535
2
  // reduce the size of the relocation section in an Android Chromium DSO from
1536
2
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1537
2
  //
1538
2
  // A relocation section consists of a header containing the literal bytes
1539
2
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1540
2
  // elements are the total number of relocations in the section and an initial
1541
2
  // r_offset value. The remaining elements define a sequence of relocation
1542
2
  // groups. Each relocation group starts with a header consisting of the
1543
2
  // following elements:
1544
2
  //
1545
2
  // - the number of relocations in the relocation group
1546
2
  // - flags for the relocation group
1547
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1548
2
  //   for each relocation in the group.
1549
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1550
2
  //   field for each relocation in the group.
1551
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1552
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1553
2
  //   each relocation in the group.
1554
2
  //
1555
2
  // Following the relocation group header are descriptions of each of the
1556
2
  // relocations in the group. They consist of the following elements:
1557
2
  //
1558
2
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1559
2
  //   delta for this relocation.
1560
2
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1561
2
  //   field for this relocation.
1562
2
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1563
2
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1564
2
  //   this relocation.
1565
2
1566
2
  size_t OldSize = RelocData.size();
1567
2
1568
2
  RelocData = {'A', 'P', 'S', '2'};
1569
2
  raw_svector_ostream OS(RelocData);
1570
2
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
1571
2
1572
2
  // The format header includes the number of relocations and the initial
1573
2
  // offset (we set this to zero because the first relocation group will
1574
2
  // perform the initial adjustment).
1575
2
  Add(Relocs.size());
1576
2
  Add(0);
1577
2
1578
2
  std::vector<Elf_Rela> Relatives, NonRelatives;
1579
2
1580
54
  for (const DynamicReloc &Rel : Relocs) {
1581
54
    Elf_Rela R;
1582
54
    encodeDynamicReloc<ELFT>(&R, Rel);
1583
54
1584
54
    if (R.getType(Config->IsMips64EL) == Target->RelativeRel)
1585
50
      Relatives.push_back(R);
1586
4
    else
1587
4
      NonRelatives.push_back(R);
1588
54
  }
1589
2
1590
2
  llvm::sort(Relatives, [](const Elf_Rel &A, const Elf_Rel &B) {
1591
2
    return A.r_offset < B.r_offset;
1592
2
  });
1593
2
1594
2
  // Try to find groups of relative relocations which are spaced one word
1595
2
  // apart from one another. These generally correspond to vtable entries. The
1596
2
  // format allows these groups to be encoded using a sort of run-length
1597
2
  // encoding, but each group will cost 7 bytes in addition to the offset from
1598
2
  // the previous group, so it is only profitable to do this for groups of
1599
2
  // size 8 or larger.
1600
2
  std::vector<Elf_Rela> UngroupedRelatives;
1601
2
  std::vector<std::vector<Elf_Rela>> RelativeGroups;
1602
10
  for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) {
1603
8
    std::vector<Elf_Rela> Group;
1604
50
    do {
1605
50
      Group.push_back(*I++);
1606
50
    } while (I != E && 
(I - 1)->r_offset + Config->Wordsize == I->r_offset48
);
1607
8
1608
8
    if (Group.size() < 8)
1609
4
      UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(),
1610
4
                                Group.end());
1611
4
    else
1612
4
      RelativeGroups.emplace_back(std::move(Group));
1613
8
  }
1614
2
1615
2
  unsigned HasAddendIfRela =
1616
2
      Config->IsRela ? 
RELOCATION_GROUP_HAS_ADDEND_FLAG0
: 0;
1617
2
1618
2
  uint64_t Offset = 0;
1619
2
  uint64_t Addend = 0;
1620
2
1621
2
  // Emit the run-length encoding for the groups of adjacent relative
1622
2
  // relocations. Each group is represented using two groups in the packed
1623
2
  // format. The first is used to set the current offset to the start of the
1624
2
  // group (and also encodes the first relocation), and the second encodes the
1625
2
  // remaining relocations.
1626
4
  for (std::vector<Elf_Rela> &G : RelativeGroups) {
1627
4
    // The first relocation in the group.
1628
4
    Add(1);
1629
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1630
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1631
4
    Add(G[0].r_offset - Offset);
1632
4
    Add(Target->RelativeRel);
1633
4
    if (Config->IsRela) {
1634
0
      Add(G[0].r_addend - Addend);
1635
0
      Addend = G[0].r_addend;
1636
0
    }
1637
4
1638
4
    // The remaining relocations.
1639
4
    Add(G.size() - 1);
1640
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1641
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1642
4
    Add(Config->Wordsize);
1643
4
    Add(Target->RelativeRel);
1644
4
    if (Config->IsRela) {
1645
0
      for (auto I = G.begin() + 1, E = G.end(); I != E; ++I) {
1646
0
        Add(I->r_addend - Addend);
1647
0
        Addend = I->r_addend;
1648
0
      }
1649
0
    }
1650
4
1651
4
    Offset = G.back().r_offset;
1652
4
  }
1653
2
1654
2
  // Now the ungrouped relatives.
1655
2
  if (!UngroupedRelatives.empty()) {
1656
2
    Add(UngroupedRelatives.size());
1657
2
    Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1658
2
    Add(Target->RelativeRel);
1659
16
    for (Elf_Rela &R : UngroupedRelatives) {
1660
16
      Add(R.r_offset - Offset);
1661
16
      Offset = R.r_offset;
1662
16
      if (Config->IsRela) {
1663
0
        Add(R.r_addend - Addend);
1664
0
        Addend = R.r_addend;
1665
0
      }
1666
16
    }
1667
2
  }
1668
2
1669
2
  // Finally the non-relative relocations.
1670
2
  llvm::sort(NonRelatives, [](const Elf_Rela &A, const Elf_Rela &B) {
1671
2
    return A.r_offset < B.r_offset;
1672
2
  });
1673
2
  if (!NonRelatives.empty()) {
1674
2
    Add(NonRelatives.size());
1675
2
    Add(HasAddendIfRela);
1676
4
    for (Elf_Rela &R : NonRelatives) {
1677
4
      Add(R.r_offset - Offset);
1678
4
      Offset = R.r_offset;
1679
4
      Add(R.r_info);
1680
4
      if (Config->IsRela) {
1681
0
        Add(R.r_addend - Addend);
1682
0
        Addend = R.r_addend;
1683
0
      }
1684
4
    }
1685
2
  }
1686
2
1687
2
  // Don't allow the section to shrink; otherwise the size of the section can
1688
2
  // oscillate infinitely.
1689
2
  if (RelocData.size() < OldSize)
1690
0
    RelocData.append(OldSize - RelocData.size(), 0);
1691
2
1692
2
  // Returns whether the section size changed. We need to keep recomputing both
1693
2
  // section layout and the contents of this section until the size converges
1694
2
  // because changing this section's size can affect section layout, which in
1695
2
  // turn can affect the sizes of the LEB-encoded integers stored in this
1696
2
  // section.
1697
2
  return RelocData.size() != OldSize;
1698
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
1520
8
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1521
8
  // This function computes the contents of an Android-format packed relocation
1522
8
  // section.
1523
8
  //
1524
8
  // This format compresses relocations by using relocation groups to factor out
1525
8
  // fields that are common between relocations and storing deltas from previous
1526
8
  // relocations in SLEB128 format (which has a short representation for small
1527
8
  // numbers). A good example of a relocation type with common fields is
1528
8
  // R_*_RELATIVE, which is normally used to represent function pointers in
1529
8
  // vtables. In the REL format, each relative relocation has the same r_info
1530
8
  // field, and is only different from other relative relocations in terms of
1531
8
  // the r_offset field. By sorting relocations by offset, grouping them by
1532
8
  // r_info and representing each relocation with only the delta from the
1533
8
  // previous offset, each 8-byte relocation can be compressed to as little as 1
1534
8
  // byte (or less with run-length encoding). This relocation packer was able to
1535
8
  // reduce the size of the relocation section in an Android Chromium DSO from
1536
8
  // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1537
8
  //
1538
8
  // A relocation section consists of a header containing the literal bytes
1539
8
  // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1540
8
  // elements are the total number of relocations in the section and an initial
1541
8
  // r_offset value. The remaining elements define a sequence of relocation
1542
8
  // groups. Each relocation group starts with a header consisting of the
1543
8
  // following elements:
1544
8
  //
1545
8
  // - the number of relocations in the relocation group
1546
8
  // - flags for the relocation group
1547
8
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1548
8
  //   for each relocation in the group.
1549
8
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1550
8
  //   field for each relocation in the group.
1551
8
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1552
8
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1553
8
  //   each relocation in the group.
1554
8
  //
1555
8
  // Following the relocation group header are descriptions of each of the
1556
8
  // relocations in the group. They consist of the following elements:
1557
8
  //
1558
8
  // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1559
8
  //   delta for this relocation.
1560
8
  // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1561
8
  //   field for this relocation.
1562
8
  // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1563
8
  //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1564
8
  //   this relocation.
1565
8
1566
8
  size_t OldSize = RelocData.size();
1567
8
1568
8
  RelocData = {'A', 'P', 'S', '2'};
1569
8
  raw_svector_ostream OS(RelocData);
1570
8
  auto Add = [&](int64_t V) { encodeSLEB128(V, OS); };
1571
8
1572
8
  // The format header includes the number of relocations and the initial
1573
8
  // offset (we set this to zero because the first relocation group will
1574
8
  // perform the initial adjustment).
1575
8
  Add(Relocs.size());
1576
8
  Add(0);
1577
8
1578
8
  std::vector<Elf_Rela> Relatives, NonRelatives;
1579
8
1580
66
  for (const DynamicReloc &Rel : Relocs) {
1581
66
    Elf_Rela R;
1582
66
    encodeDynamicReloc<ELFT>(&R, Rel);
1583
66
1584
66
    if (R.getType(Config->IsMips64EL) == Target->RelativeRel)
1585
56
      Relatives.push_back(R);
1586
10
    else
1587
10
      NonRelatives.push_back(R);
1588
66
  }
1589
8
1590
8
  llvm::sort(Relatives, [](const Elf_Rel &A, const Elf_Rel &B) {
1591
8
    return A.r_offset < B.r_offset;
1592
8
  });
1593
8
1594
8
  // Try to find groups of relative relocations which are spaced one word
1595
8
  // apart from one another. These generally correspond to vtable entries. The
1596
8
  // format allows these groups to be encoded using a sort of run-length
1597
8
  // encoding, but each group will cost 7 bytes in addition to the offset from
1598
8
  // the previous group, so it is only profitable to do this for groups of
1599
8
  // size 8 or larger.
1600
8
  std::vector<Elf_Rela> UngroupedRelatives;
1601
8
  std::vector<std::vector<Elf_Rela>> RelativeGroups;
1602
22
  for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) {
1603
14
    std::vector<Elf_Rela> Group;
1604
56
    do {
1605
56
      Group.push_back(*I++);
1606
56
    } while (I != E && 
(I - 1)->r_offset + Config->Wordsize == I->r_offset52
);
1607
14
1608
14
    if (Group.size() < 8)
1609
10
      UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(),
1610
10
                                Group.end());
1611
4
    else
1612
4
      RelativeGroups.emplace_back(std::move(Group));
1613
14
  }
1614
8
1615
8
  unsigned HasAddendIfRela =
1616
8
      Config->IsRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 
00
;
1617
8
1618
8
  uint64_t Offset = 0;
1619
8
  uint64_t Addend = 0;
1620
8
1621
8
  // Emit the run-length encoding for the groups of adjacent relative
1622
8
  // relocations. Each group is represented using two groups in the packed
1623
8
  // format. The first is used to set the current offset to the start of the
1624
8
  // group (and also encodes the first relocation), and the second encodes the
1625
8
  // remaining relocations.
1626
8
  for (std::vector<Elf_Rela> &G : RelativeGroups) {
1627
4
    // The first relocation in the group.
1628
4
    Add(1);
1629
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1630
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1631
4
    Add(G[0].r_offset - Offset);
1632
4
    Add(Target->RelativeRel);
1633
4
    if (Config->IsRela) {
1634
4
      Add(G[0].r_addend - Addend);
1635
4
      Addend = G[0].r_addend;
1636
4
    }
1637
4
1638
4
    // The remaining relocations.
1639
4
    Add(G.size() - 1);
1640
4
    Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1641
4
        RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1642
4
    Add(Config->Wordsize);
1643
4
    Add(Target->RelativeRel);
1644
4
    if (Config->IsRela) {
1645
34
      for (auto I = G.begin() + 1, E = G.end(); I != E; 
++I30
) {
1646
30
        Add(I->r_addend - Addend);
1647
30
        Addend = I->r_addend;
1648
30
      }
1649
4
    }
1650
4
1651
4
    Offset = G.back().r_offset;
1652
4
  }
1653
8
1654
8
  // Now the ungrouped relatives.
1655
8
  if (!UngroupedRelatives.empty()) {
1656
4
    Add(UngroupedRelatives.size());
1657
4
    Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela);
1658
4
    Add(Target->RelativeRel);
1659
22
    for (Elf_Rela &R : UngroupedRelatives) {
1660
22
      Add(R.r_offset - Offset);
1661
22
      Offset = R.r_offset;
1662
22
      if (Config->IsRela) {
1663
22
        Add(R.r_addend - Addend);
1664
22
        Addend = R.r_addend;
1665
22
      }
1666
22
    }
1667
4
  }
1668
8
1669
8
  // Finally the non-relative relocations.
1670
8
  llvm::sort(NonRelatives, [](const Elf_Rela &A, const Elf_Rela &B) {
1671
8
    return A.r_offset < B.r_offset;
1672
8
  });
1673
8
  if (!NonRelatives.empty()) {
1674
6
    Add(NonRelatives.size());
1675
6
    Add(HasAddendIfRela);
1676
10
    for (Elf_Rela &R : NonRelatives) {
1677
10
      Add(R.r_offset - Offset);
1678
10
      Offset = R.r_offset;
1679
10
      Add(R.r_info);
1680
10
      if (Config->IsRela) {
1681
10
        Add(R.r_addend - Addend);
1682
10
        Addend = R.r_addend;
1683
10
      }
1684
10
    }
1685
6
  }
1686
8
1687
8
  // Don't allow the section to shrink; otherwise the size of the section can
1688
8
  // oscillate infinitely.
1689
8
  if (RelocData.size() < OldSize)
1690
1
    RelocData.append(OldSize - RelocData.size(), 0);
1691
8
1692
8
  // Returns whether the section size changed. We need to keep recomputing both
1693
8
  // section layout and the contents of this section until the size converges
1694
8
  // because changing this section's size can affect section layout, which in
1695
8
  // turn can affect the sizes of the LEB-encoded integers stored in this
1696
8
  // section.
1697
8
  return RelocData.size() != OldSize;
1698
8
}
Unexecuted instantiation: lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()
1699
1700
4
template <class ELFT> RelrSection<ELFT>::RelrSection() {
1701
4
  this->Entsize = Config->Wordsize;
1702
4
}
lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::RelrSection()
Line
Count
Source
1700
2
template <class ELFT> RelrSection<ELFT>::RelrSection() {
1701
2
  this->Entsize = Config->Wordsize;
1702
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
1700
2
template <class ELFT> RelrSection<ELFT>::RelrSection() {
1701
2
  this->Entsize = Config->Wordsize;
1702
2
}
Unexecuted instantiation: lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::RelrSection()
1703
1704
7
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1705
7
  // This function computes the contents of an SHT_RELR packed relocation
1706
7
  // section.
1707
7
  //
1708
7
  // Proposal for adding SHT_RELR sections to generic-abi is here:
1709
7
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1710
7
  //
1711
7
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1712
7
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1713
7
  //
1714
7
  // i.e. start with an address, followed by any number of bitmaps. The address
1715
7
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1716
7
  // relocations each, at subsequent offsets following the last address entry.
1717
7
  //
1718
7
  // The bitmap entries must have 1 in the least significant bit. The assumption
1719
7
  // here is that an address cannot have 1 in lsb. Odd addresses are not
1720
7
  // supported.
1721
7
  //
1722
7
  // Excluding the least significant bit in the bitmap, each non-zero bit in
1723
7
  // the bitmap represents a relocation to be applied to a corresponding machine
1724
7
  // word that follows the base address word. The second least significant bit
1725
7
  // represents the machine word immediately following the initial address, and
1726
7
  // each bit that follows represents the next word, in linear order. As such,
1727
7
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1728
7
  // 63 relocations in a 64-bit object.
1729
7
  //
1730
7
  // This encoding has a couple of interesting properties:
1731
7
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1732
7
  //    even means address, odd means bitmap.
1733
7
  // 2. Just a simple list of addresses is a valid encoding.
1734
7
1735
7
  size_t OldSize = RelrRelocs.size();
1736
7
  RelrRelocs.clear();
1737
7
1738
7
  // Same as Config->Wordsize but faster because this is a compile-time
1739
7
  // constant.
1740
7
  const size_t Wordsize = sizeof(typename ELFT::uint);
1741
7
1742
7
  // Number of bits to use for the relocation offsets bitmap.
1743
7
  // Must be either 63 or 31.
1744
7
  const size_t NBits = Wordsize * 8 - 1;
1745
7
1746
7
  // Get offsets for all relative relocations and sort them.
1747
7
  std::vector<uint64_t> Offsets;
1748
7
  for (const RelativeReloc &Rel : Relocs)
1749
164
    Offsets.push_back(Rel.getOffset());
1750
7
  llvm::sort(Offsets);
1751
7
1752
7
  // For each leading relocation, find following ones that can be folded
1753
7
  // as a bitmap and fold them.
1754
13
  for (size_t I = 0, E = Offsets.size(); I < E;) {
1755
6
    // Add a leading relocation.
1756
6
    RelrRelocs.push_back(Elf_Relr(Offsets[I]));
1757
6
    uint64_t Base = Offsets[I] + Wordsize;
1758
6
    ++I;
1759
6
1760
6
    // Find foldable relocations to construct bitmaps.
1761
14
    while (I < E) {
1762
8
      uint64_t Bitmap = 0;
1763
8
1764
166
      while (I < E) {
1765
160
        uint64_t Delta = Offsets[I] - Base;
1766
160
1767
160
        // If it is too far, it cannot be folded.
1768
160
        if (Delta >= NBits * Wordsize)
1769
2
          break;
1770
158
1771
158
        // If it is not a multiple of wordsize away, it cannot be folded.
1772
158
        if (Delta % Wordsize)
1773
0
          break;
1774
158
1775
158
        // Fold it.
1776
158
        Bitmap |= 1ULL << (Delta / Wordsize);
1777
158
        ++I;
1778
158
      }
1779
8
1780
8
      if (!Bitmap)
1781
0
        break;
1782
8
1783
8
      RelrRelocs.push_back(Elf_Relr((Bitmap << 1) | 1));
1784
8
      Base += NBits * Wordsize;
1785
8
    }
1786
6
  }
1787
7
1788
7
  return RelrRelocs.size() != OldSize;
1789
7
}
lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::updateAllocSize()
Line
Count
Source
1704
4
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1705
4
  // This function computes the contents of an SHT_RELR packed relocation
1706
4
  // section.
1707
4
  //
1708
4
  // Proposal for adding SHT_RELR sections to generic-abi is here:
1709
4
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1710
4
  //
1711
4
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1712
4
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1713
4
  //
1714
4
  // i.e. start with an address, followed by any number of bitmaps. The address
1715
4
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1716
4
  // relocations each, at subsequent offsets following the last address entry.
1717
4
  //
1718
4
  // The bitmap entries must have 1 in the least significant bit. The assumption
1719
4
  // here is that an address cannot have 1 in lsb. Odd addresses are not
1720
4
  // supported.
1721
4
  //
1722
4
  // Excluding the least significant bit in the bitmap, each non-zero bit in
1723
4
  // the bitmap represents a relocation to be applied to a corresponding machine
1724
4
  // word that follows the base address word. The second least significant bit
1725
4
  // represents the machine word immediately following the initial address, and
1726
4
  // each bit that follows represents the next word, in linear order. As such,
1727
4
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1728
4
  // 63 relocations in a 64-bit object.
1729
4
  //
1730
4
  // This encoding has a couple of interesting properties:
1731
4
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1732
4
  //    even means address, odd means bitmap.
1733
4
  // 2. Just a simple list of addresses is a valid encoding.
1734
4
1735
4
  size_t OldSize = RelrRelocs.size();
1736
4
  RelrRelocs.clear();
1737
4
1738
4
  // Same as Config->Wordsize but faster because this is a compile-time
1739
4
  // constant.
1740
4
  const size_t Wordsize = sizeof(typename ELFT::uint);
1741
4
1742
4
  // Number of bits to use for the relocation offsets bitmap.
1743
4
  // Must be either 63 or 31.
1744
4
  const size_t NBits = Wordsize * 8 - 1;
1745
4
1746
4
  // Get offsets for all relative relocations and sort them.
1747
4
  std::vector<uint64_t> Offsets;
1748
4
  for (const RelativeReloc &Rel : Relocs)
1749
116
    Offsets.push_back(Rel.getOffset());
1750
4
  llvm::sort(Offsets);
1751
4
1752
4
  // For each leading relocation, find following ones that can be folded
1753
4
  // as a bitmap and fold them.
1754
8
  for (size_t I = 0, E = Offsets.size(); I < E;) {
1755
4
    // Add a leading relocation.
1756
4
    RelrRelocs.push_back(Elf_Relr(Offsets[I]));
1757
4
    uint64_t Base = Offsets[I] + Wordsize;
1758
4
    ++I;
1759
4
1760
4
    // Find foldable relocations to construct bitmaps.
1761
10
    while (I < E) {
1762
6
      uint64_t Bitmap = 0;
1763
6
1764
118
      while (I < E) {
1765
114
        uint64_t Delta = Offsets[I] - Base;
1766
114
1767
114
        // If it is too far, it cannot be folded.
1768
114
        if (Delta >= NBits * Wordsize)
1769
2
          break;
1770
112
1771
112
        // If it is not a multiple of wordsize away, it cannot be folded.
1772
112
        if (Delta % Wordsize)
1773
0
          break;
1774
112
1775
112
        // Fold it.
1776
112
        Bitmap |= 1ULL << (Delta / Wordsize);
1777
112
        ++I;
1778
112
      }
1779
6
1780
6
      if (!Bitmap)
1781
0
        break;
1782
6
1783
6
      RelrRelocs.push_back(Elf_Relr((Bitmap << 1) | 1));
1784
6
      Base += NBits * Wordsize;
1785
6
    }
1786
4
  }
1787
4
1788
4
  return RelrRelocs.size() != OldSize;
1789
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
1704
3
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1705
3
  // This function computes the contents of an SHT_RELR packed relocation
1706
3
  // section.
1707
3
  //
1708
3
  // Proposal for adding SHT_RELR sections to generic-abi is here:
1709
3
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1710
3
  //
1711
3
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1712
3
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1713
3
  //
1714
3
  // i.e. start with an address, followed by any number of bitmaps. The address
1715
3
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1716
3
  // relocations each, at subsequent offsets following the last address entry.
1717
3
  //
1718
3
  // The bitmap entries must have 1 in the least significant bit. The assumption
1719
3
  // here is that an address cannot have 1 in lsb. Odd addresses are not
1720
3
  // supported.
1721
3
  //
1722
3
  // Excluding the least significant bit in the bitmap, each non-zero bit in
1723
3
  // the bitmap represents a relocation to be applied to a corresponding machine
1724
3
  // word that follows the base address word. The second least significant bit
1725
3
  // represents the machine word immediately following the initial address, and
1726
3
  // each bit that follows represents the next word, in linear order. As such,
1727
3
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1728
3
  // 63 relocations in a 64-bit object.
1729
3
  //
1730
3
  // This encoding has a couple of interesting properties:
1731
3
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1732
3
  //    even means address, odd means bitmap.
1733
3
  // 2. Just a simple list of addresses is a valid encoding.
1734
3
1735
3
  size_t OldSize = RelrRelocs.size();
1736
3
  RelrRelocs.clear();
1737
3
1738
3
  // Same as Config->Wordsize but faster because this is a compile-time
1739
3
  // constant.
1740
3
  const size_t Wordsize = sizeof(typename ELFT::uint);
1741
3
1742
3
  // Number of bits to use for the relocation offsets bitmap.
1743
3
  // Must be either 63 or 31.
1744
3
  const size_t NBits = Wordsize * 8 - 1;
1745
3
1746
3
  // Get offsets for all relative relocations and sort them.
1747
3
  std::vector<uint64_t> Offsets;
1748
3
  for (const RelativeReloc &Rel : Relocs)
1749
48
    Offsets.push_back(Rel.getOffset());
1750
3
  llvm::sort(Offsets);
1751
3
1752
3
  // For each leading relocation, find following ones that can be folded
1753
3
  // as a bitmap and fold them.
1754
5
  for (size_t I = 0, E = Offsets.size(); I < E;) {
1755
2
    // Add a leading relocation.
1756
2
    RelrRelocs.push_back(Elf_Relr(Offsets[I]));
1757
2
    uint64_t Base = Offsets[I] + Wordsize;
1758
2
    ++I;
1759
2
1760
2
    // Find foldable relocations to construct bitmaps.
1761
4
    while (I < E) {
1762
2
      uint64_t Bitmap = 0;
1763
2
1764
48
      while (I < E) {
1765
46
        uint64_t Delta = Offsets[I] - Base;
1766
46
1767
46
        // If it is too far, it cannot be folded.
1768
46
        if (Delta >= NBits * Wordsize)
1769
0
          break;
1770
46
1771
46
        // If it is not a multiple of wordsize away, it cannot be folded.
1772
46
        if (Delta % Wordsize)
1773
0
          break;
1774
46
1775
46
        // Fold it.
1776
46
        Bitmap |= 1ULL << (Delta / Wordsize);
1777
46
        ++I;
1778
46
      }
1779
2
1780
2
      if (!Bitmap)
1781
0
        break;
1782
2
1783
2
      RelrRelocs.push_back(Elf_Relr((Bitmap << 1) | 1));
1784
2
      Base += NBits * Wordsize;
1785
2
    }
1786
2
  }
1787
3
1788
3
  return RelrRelocs.size() != OldSize;
1789
3
}
Unexecuted instantiation: lld::elf::RelrSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::updateAllocSize()
1790
1791
SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &StrTabSec)
1792
    : SyntheticSection(StrTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1793
                       StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1794
                       Config->Wordsize,
1795
                       StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1796
3.75k
      StrTabSec(StrTabSec) {}
1797
1798
// Orders symbols according to their positions in the GOT,
1799
// in compliance with MIPS ABI rules.
1800
// See "Global Offset Table" in Chapter 5 in the following document
1801
// for detailed description:
1802
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1803
static bool sortMipsSymbols(const SymbolTableEntry &L,
1804
153
                            const SymbolTableEntry &R) {
1805
153
  // Sort entries related to non-local preemptible symbols by GOT indexes.
1806
153
  // All other entries go to the beginning of a dynsym in arbitrary order.
1807
153
  if (L.Sym->isInGot() && 
R.Sym->isInGot()21
)
1808
6
    return L.Sym->GotIndex < R.Sym->GotIndex;
1809
147
  if (!L.Sym->isInGot() && 
!R.Sym->isInGot()132
)
1810
108
    return false;
1811
39
  return !L.Sym->isInGot();
1812
39
}
1813
1814
3.57k
void SymbolTableBaseSection::finalizeContents() {
1815
3.57k
  if (OutputSection *Sec = StrTabSec.getParent())
1816
3.57k
    getParent()->Link = Sec->SectionIndex;
1817
3.57k
1818
3.57k
  if (this->Type != SHT_DYNSYM) {
1819
2.40k
    sortSymTabSymbols();
1820
2.40k
    return;
1821
2.40k
  }
1822
1.16k
1823
1.16k
  // If it is a .dynsym, there should be no local symbols, but we need
1824
1.16k
  // to do a few things for the dynamic linker.
1825
1.16k
1826
1.16k
  // Section's Info field has the index of the first non-local symbol.
1827
1.16k
  // Because the first symbol entry is a null entry, 1 is the first.
1828
1.16k
  getParent()->Info = 1;
1829
1.16k
1830
1.16k
  if (In.GnuHashTab) {
1831
929
    // NB: It also sorts Symbols to meet the GNU hash table requirements.
1832
929
    In.GnuHashTab->addSymbols(Symbols);
1833
929
  } else 
if (236
Config->EMachine == EM_MIPS236
) {
1834
100
    llvm::stable_sort(Symbols, sortMipsSymbols);
1835
100
  }
1836
1.16k
1837
1.16k
  size_t I = 0;
1838
1.16k
  for (const SymbolTableEntry &S : Symbols)
1839
2.02k
    S.Sym->DynsymIndex = ++I;
1840
1.16k
}
1841
1842
// The ELF spec requires that all local symbols precede global symbols, so we
1843
// sort symbol entries in this function. (For .dynsym, we don't do that because
1844
// symbols for dynamic linking are inherently all globals.)
1845
//
1846
// Aside from above, we put local symbols in groups starting with the STT_FILE
1847
// symbol. That is convenient for purpose of identifying where are local symbols
1848
// coming from.
1849
2.40k
void SymbolTableBaseSection::sortSymTabSymbols() {
1850
2.40k
  // Move all local symbols before global symbols.
1851
2.40k
  auto E = std::stable_partition(
1852
85.2k
      Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) {
1853
85.2k
        return S.Sym->isLocal() || 
S.Sym->computeBinding() == STB_LOCAL17.0k
;
1854
85.2k
      });
1855
2.40k
  size_t NumLocals = E - Symbols.begin();
1856
2.40k
  getParent()->Info = NumLocals + 1;
1857
2.40k
1858
2.40k
  // We want to group the local symbols by file. For that we rebuild the local
1859
2.40k
  // part of the symbols vector. We do not need to care about the STT_FILE
1860
2.40k
  // symbols, they are already naturally placed first in each group. That
1861
2.40k
  // happens because STT_FILE is always the first symbol in the object and hence
1862
2.40k
  // precede all other local symbols we add for a file.
1863
2.40k
  MapVector<InputFile *, std::vector<SymbolTableEntry>> Arr;
1864
2.40k
  for (const SymbolTableEntry &S : llvm::make_range(Symbols.begin(), E))
1865
69.9k
    Arr[S.Sym->File].push_back(S);
1866
2.40k
1867
2.40k
  auto I = Symbols.begin();
1868
2.40k
  for (std::pair<InputFile *, std::vector<SymbolTableEntry>> &P : Arr)
1869
2.30k
    for (SymbolTableEntry &Entry : P.second)
1870
69.9k
      *I++ = Entry;
1871
2.40k
}
1872
1873
87.6k
void SymbolTableBaseSection::addSymbol(Symbol *B) {
1874
87.6k
  // Adding a local symbol to a .dynsym is a bug.
1875
87.6k
  assert(this->Type != SHT_DYNSYM || !B->isLocal());
1876
87.6k
1877
87.6k
  bool HashIt = B->isLocal();
1878
87.6k
  Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)});
1879
87.6k
}
1880
1881
170
size_t SymbolTableBaseSection::getSymbolIndex(Symbol *Sym) {
1882
170
  // Initializes symbol lookup tables lazily. This is used only
1883
170
  // for -r or -emit-relocs.
1884
170
  llvm::call_once(OnceFlag, [&] {
1885
67
    SymbolIndexMap.reserve(Symbols.size());
1886
67
    size_t I = 0;
1887
375
    for (const SymbolTableEntry &E : Symbols) {
1888
375
      if (E.Sym->Type == STT_SECTION)
1889
191
        SectionIndexMap[E.Sym->getOutputSection()] = ++I;
1890
184
      else
1891
184
        SymbolIndexMap[E.Sym] = ++I;
1892
375
    }
1893
67
  });
1894
170
1895
170
  // Section symbols are mapped based on their output sections
1896
170
  // to maintain their semantics.
1897
170
  if (Sym->Type == STT_SECTION)
1898
89
    return SectionIndexMap.lookup(Sym->getOutputSection());
1899
81
  return SymbolIndexMap.lookup(Sym);
1900
81
}
1901
1902
template <class ELFT>
1903
SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &StrTabSec)
1904
3.75k
    : SymbolTableBaseSection(StrTabSec) {
1905
3.75k
  this->Entsize = sizeof(Elf_Sym);
1906
3.75k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1904
481
    : SymbolTableBaseSection(StrTabSec) {
1905
481
  this->Entsize = sizeof(Elf_Sym);
1906
481
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1904
206
    : SymbolTableBaseSection(StrTabSec) {
1905
206
  this->Entsize = sizeof(Elf_Sym);
1906
206
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1904
2.89k
    : SymbolTableBaseSection(StrTabSec) {
1905
2.89k
  this->Entsize = sizeof(Elf_Sym);
1906
2.89k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::SymbolTableSection(lld::elf::StringTableSection&)
Line
Count
Source
1904
176
    : SymbolTableBaseSection(StrTabSec) {
1905
176
  this->Entsize = sizeof(Elf_Sym);
1906
176
}
1907
1908
239k
static BssSection *getCommonSec(Symbol *Sym) {
1909
239k
  if (!Config->DefineCommon)
1910
197k
    if (auto *D = dyn_cast<Defined>(Sym))
1911
197k
      return dyn_cast_or_null<BssSection>(D->Section);
1912
42.5k
  return nullptr;
1913
42.5k
}
1914
1915
152k
static uint32_t getSymSectionIndex(Symbol *Sym) {
1916
152k
  if (getCommonSec(Sym))
1917
2
    return SHN_COMMON;
1918
152k
  if (!isa<Defined>(Sym) || 
Sym->NeedsPltAddr151k
)
1919
1.18k
    return SHN_UNDEF;
1920
151k
  if (const OutputSection *OS = Sym->getOutputSection())
1921
150k
    return OS->SectionIndex >= SHN_LORESERVE ? 
(uint32_t)SHN_XINDEX522
1922
150k
                                             : 
OS->SectionIndex150k
;
1923
693
  return SHN_ABS;
1924
693
}
1925
1926
// Write the internal symbol table contents to the output symbol table.
1927
3.52k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1928
3.52k
  // The first entry is a null entry as per the ELF spec.
1929
3.52k
  memset(Buf, 0, sizeof(Elf_Sym));
1930
3.52k
  Buf += sizeof(Elf_Sym);
1931
3.52k
1932
3.52k
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1933
3.52k
1934
87.1k
  for (SymbolTableEntry &Ent : Symbols) {
1935
87.1k
    Symbol *Sym = Ent.Sym;
1936
87.1k
1937
87.1k
    // Set st_info and st_other.
1938
87.1k
    ESym->st_other = 0;
1939
87.1k
    if (Sym->isLocal()) {
1940
68.1k
      ESym->setBindingAndType(STB_LOCAL, Sym->Type);
1941
68.1k
    } else {
1942
18.9k
      ESym->setBindingAndType(Sym->computeBinding(), Sym->Type);
1943
18.9k
      ESym->setVisibility(Sym->Visibility);
1944
18.9k
    }
1945
87.1k
1946
87.1k
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
1947
87.1k
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
1948
87.1k
    if (Config->EMachine == EM_PPC64)
1949
900
      ESym->st_other |= Sym->StOther & 0xe0;
1950
87.1k
1951
87.1k
    ESym->st_name = Ent.StrTabOffset;
1952
87.1k
    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
1953
87.1k
1954
87.1k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1955
87.1k
    // for undefined symbols, so whether copying it or not is up to us if that's
1956
87.1k
    // the case. We'll leave it as zero because by not setting a value, we can
1957
87.1k
    // get the exact same outputs for two sets of input files that differ only
1958
87.1k
    // in undefined symbol size in DSOs.
1959
87.1k
    if (ESym->st_shndx == SHN_UNDEF)
1960
1.18k
      ESym->st_size = 0;
1961
85.9k
    else
1962
85.9k
      ESym->st_size = Sym->getSize();
1963
87.1k
1964
87.1k
    // st_value is usually an address of a symbol, but that has a
1965
87.1k
    // special meaining for uninstantiated common symbols (this can
1966
87.1k
    // occur if -r is given).
1967
87.1k
    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
1968
2
      ESym->st_value = CommonSec->Alignment;
1969
87.1k
    else
1970
87.1k
      ESym->st_value = Sym->getVA();
1971
87.1k
1972
87.1k
    ++ESym;
1973
87.1k
  }
1974
3.52k
1975
3.52k
  // On MIPS we need to mark symbol which has a PLT entry and requires
1976
3.52k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1977
3.52k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1978
3.52k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1979
3.52k
  if (Config->EMachine == EM_MIPS) {
1980
287
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1981
287
1982
11.1k
    for (SymbolTableEntry &Ent : Symbols) {
1983
11.1k
      Symbol *Sym = Ent.Sym;
1984
11.1k
      if (Sym->isInPlt() && 
Sym->NeedsPltAddr34
)
1985
8
        ESym->st_other |= STO_MIPS_PLT;
1986
11.1k
      if (isMicroMips()) {
1987
147
        // We already set the less-significant bit for symbols
1988
147
        // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
1989
147
        // records. That allows us to distinguish such symbols in
1990
147
        // the `MIPS<ELFT>::relocateOne()` routine. Now we should
1991
147
        // clear that bit for non-dynamic symbol table, so tools
1992
147
        // like `objdump` will be able to deal with a correct
1993
147
        // symbol position.
1994
147
        if (Sym->isDefined() &&
1995
147
            
(135
(Sym->StOther & STO_MIPS_MICROMIPS)135
||
Sym->NeedsPltAddr94
)) {
1996
43
          if (!StrTabSec.isDynamic())
1997
37
            ESym->st_value &= ~1;
1998
43
          ESym->st_other |= STO_MIPS_MICROMIPS;
1999
43
        }
2000
147
      }
2001
11.1k
      if (Config->Relocatable)
2002
82
        if (auto *D = dyn_cast<Defined>(Sym))
2003
82
          if (isMipsPIC<ELFT>(D))
2004
7
            ESym->st_other |= STO_MIPS_PIC;
2005
11.1k
      ++ESym;
2006
11.1k
    }
2007
287
  }
2008
3.52k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, false> >::writeTo(unsigned char*)
Line
Count
Source
1927
468
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1928
468
  // The first entry is a null entry as per the ELF spec.
1929
468
  memset(Buf, 0, sizeof(Elf_Sym));
1930
468
  Buf += sizeof(Elf_Sym);
1931
468
1932
468
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1933
468
1934
2.62k
  for (SymbolTableEntry &Ent : Symbols) {
1935
2.62k
    Symbol *Sym = Ent.Sym;
1936
2.62k
1937
2.62k
    // Set st_info and st_other.
1938
2.62k
    ESym->st_other = 0;
1939
2.62k
    if (Sym->isLocal()) {
1940
1.08k
      ESym->setBindingAndType(STB_LOCAL, Sym->Type);
1941
1.53k
    } else {
1942
1.53k
      ESym->setBindingAndType(Sym->computeBinding(), Sym->Type);
1943
1.53k
      ESym->setVisibility(Sym->Visibility);
1944
1.53k
    }
1945
2.62k
1946
2.62k
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
1947
2.62k
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
1948
2.62k
    if (Config->EMachine == EM_PPC64)
1949
0
      ESym->st_other |= Sym->StOther & 0xe0;
1950
2.62k
1951
2.62k
    ESym->st_name = Ent.StrTabOffset;
1952
2.62k
    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
1953
2.62k
1954
2.62k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1955
2.62k
    // for undefined symbols, so whether copying it or not is up to us if that's
1956
2.62k
    // the case. We'll leave it as zero because by not setting a value, we can
1957
2.62k
    // get the exact same outputs for two sets of input files that differ only
1958
2.62k
    // in undefined symbol size in DSOs.
1959
2.62k
    if (ESym->st_shndx == SHN_UNDEF)
1960
192
      ESym->st_size = 0;
1961
2.43k
    else
1962
2.43k
      ESym->st_size = Sym->getSize();
1963
2.62k
1964
2.62k
    // st_value is usually an address of a symbol, but that has a
1965
2.62k
    // special meaining for uninstantiated common symbols (this can
1966
2.62k
    // occur if -r is given).
1967
2.62k
    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
1968
0
      ESym->st_value = CommonSec->Alignment;
1969
2.62k
    else
1970
2.62k
      ESym->st_value = Sym->getVA();
1971
2.62k
1972
2.62k
    ++ESym;
1973
2.62k
  }
1974
468
1975
468
  // On MIPS we need to mark symbol which has a PLT entry and requires
1976
468
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1977
468
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1978
468
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1979
468
  if (Config->EMachine == EM_MIPS) {
1980
31
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1981
31
1982
95
    for (SymbolTableEntry &Ent : Symbols) {
1983
95
      Symbol *Sym = Ent.Sym;
1984
95
      if (Sym->isInPlt() && 
Sym->NeedsPltAddr4
)
1985
0
        ESym->st_other |= STO_MIPS_PLT;
1986
95
      if (isMicroMips()) {
1987
41
        // We already set the less-significant bit for symbols
1988
41
        // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
1989
41
        // records. That allows us to distinguish such symbols in
1990
41
        // the `MIPS<ELFT>::relocateOne()` routine. Now we should
1991
41
        // clear that bit for non-dynamic symbol table, so tools
1992
41
        // like `objdump` will be able to deal with a correct
1993
41
        // symbol position.
1994
41
        if (Sym->isDefined() &&
1995
41
            
(37
(Sym->StOther & STO_MIPS_MICROMIPS)37
||
Sym->NeedsPltAddr21
)) {
1996
16
          if (!StrTabSec.isDynamic())
1997
14
            ESym->st_value &= ~1;
1998
16
          ESym->st_other |= STO_MIPS_MICROMIPS;
1999
16
        }
2000
41
      }
2001
95
      if (Config->Relocatable)
2002
5
        if (auto *D = dyn_cast<Defined>(Sym))
2003
5
          if (isMipsPIC<ELFT>(D))
2004
0
            ESym->st_other |= STO_MIPS_PIC;
2005
95
      ++ESym;
2006
95
    }
2007
31
  }
2008
468
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, false> >::writeTo(unsigned char*)
Line
Count
Source
1927
201
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1928
201
  // The first entry is a null entry as per the ELF spec.
1929
201
  memset(Buf, 0, sizeof(Elf_Sym));
1930
201
  Buf += sizeof(Elf_Sym);
1931
201
1932
201
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1933
201
1934
851
  for (SymbolTableEntry &Ent : Symbols) {
1935
851
    Symbol *Sym = Ent.Sym;
1936
851
1937
851
    // Set st_info and st_other.
1938
851
    ESym->st_other = 0;
1939
851
    if (Sym->isLocal()) {
1940
144
      ESym->setBindingAndType(STB_LOCAL, Sym->Type);
1941
707
    } else {
1942
707
      ESym->setBindingAndType(Sym->computeBinding(), Sym->Type);
1943
707
      ESym->setVisibility(Sym->Visibility);
1944
707
    }
1945
851
1946
851
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
1947
851
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
1948
851
    if (Config->EMachine == EM_PPC64)
1949
0
      ESym->st_other |= Sym->StOther & 0xe0;
1950
851
1951
851
    ESym->st_name = Ent.StrTabOffset;
1952
851
    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
1953
851
1954
851
    // Copy symbol size if it is a defined symbol. st_size is not significant
1955
851
    // for undefined symbols, so whether copying it or not is up to us if that's
1956
851
    // the case. We'll leave it as zero because by not setting a value, we can
1957
851
    // get the exact same outputs for two sets of input files that differ only
1958
851
    // in undefined symbol size in DSOs.
1959
851
    if (ESym->st_shndx == SHN_UNDEF)
1960
66
      ESym->st_size = 0;
1961
785
    else
1962
785
      ESym->st_size = Sym->getSize();
1963
851
1964
851
    // st_value is usually an address of a symbol, but that has a
1965
851
    // special meaining for uninstantiated common symbols (this can
1966
851
    // occur if -r is given).
1967
851
    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
1968
0
      ESym->st_value = CommonSec->Alignment;
1969
851
    else
1970
851
      ESym->st_value = Sym->getVA();
1971
851
1972
851
    ++ESym;
1973
851
  }
1974
201
1975
201
  // On MIPS we need to mark symbol which has a PLT entry and requires
1976
201
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1977
201
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1978
201
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1979
201
  if (Config->EMachine == EM_MIPS) {
1980
192
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1981
192
1982
829
    for (SymbolTableEntry &Ent : Symbols) {
1983
829
      Symbol *Sym = Ent.Sym;
1984
829
      if (Sym->isInPlt() && 
Sym->NeedsPltAddr26
)
1985
8
        ESym->st_other |= STO_MIPS_PLT;
1986
829
      if (isMicroMips()) {
1987
106
        // We already set the less-significant bit for symbols
1988
106
        // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
1989
106
        // records. That allows us to distinguish such symbols in
1990
106
        // the `MIPS<ELFT>::relocateOne()` routine. Now we should
1991
106
        // clear that bit for non-dynamic symbol table, so tools
1992
106
        // like `objdump` will be able to deal with a correct
1993
106
        // symbol position.
1994
106
        if (Sym->isDefined() &&
1995
106
            
(98
(Sym->StOther & STO_MIPS_MICROMIPS)98
||
Sym->NeedsPltAddr73
)) {
1996
27
          if (!StrTabSec.isDynamic())
1997
23
            ESym->st_value &= ~1;
1998
27
          ESym->st_other |= STO_MIPS_MICROMIPS;
1999
27
        }
2000
106
      }
2001
829
      if (Config->Relocatable)
2002
66
        if (auto *D = dyn_cast<Defined>(Sym))
2003
66
          if (isMipsPIC<ELFT>(D))
2004
7
            ESym->st_other |= STO_MIPS_PIC;
2005
829
      ++ESym;
2006
829
    }
2007
192
  }
2008
201
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)1, true> >::writeTo(unsigned char*)
Line
Count
Source
1927
2.69k
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1928
2.69k
  // The first entry is a null entry as per the ELF spec.
1929
2.69k
  memset(Buf, 0, sizeof(Elf_Sym));
1930
2.69k
  Buf += sizeof(Elf_Sym);
1931
2.69k
1932
2.69k
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1933
2.69k
1934
73.0k
  for (SymbolTableEntry &Ent : Symbols) {
1935
73.0k
    Symbol *Sym = Ent.Sym;
1936
73.0k
1937
73.0k
    // Set st_info and st_other.
1938
73.0k
    ESym->st_other = 0;
1939
73.0k
    if (Sym->isLocal()) {
1940
66.8k
      ESym->setBindingAndType(STB_LOCAL, Sym->Type);
1941
66.8k
    } else {
1942
6.19k
      ESym->setBindingAndType(Sym->computeBinding(), Sym->Type);
1943
6.19k
      ESym->setVisibility(Sym->Visibility);
1944
6.19k
    }
1945
73.0k
1946
73.0k
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
1947
73.0k
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
1948
73.0k
    if (Config->EMachine == EM_PPC64)
1949
520
      ESym->st_other |= Sym->StOther & 0xe0;
1950
73.0k
1951
73.0k
    ESym->st_name = Ent.StrTabOffset;
1952
73.0k
    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
1953
73.0k
1954
73.0k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1955
73.0k
    // for undefined symbols, so whether copying it or not is up to us if that's
1956
73.0k
    // the case. We'll leave it as zero because by not setting a value, we can
1957
73.0k
    // get the exact same outputs for two sets of input files that differ only
1958
73.0k
    // in undefined symbol size in DSOs.
1959
73.0k
    if (ESym->st_shndx == SHN_UNDEF)
1960
841
      ESym->st_size = 0;
1961
72.2k
    else
1962
72.2k
      ESym->st_size = Sym->getSize();
1963
73.0k
1964
73.0k
    // st_value is usually an address of a symbol, but that has a
1965
73.0k
    // special meaining for uninstantiated common symbols (this can
1966
73.0k
    // occur if -r is given).
1967
73.0k
    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
1968
2
      ESym->st_value = CommonSec->Alignment;
1969
73.0k
    else
1970
73.0k
      ESym->st_value = Sym->getVA();
1971
73.0k
1972
73.0k
    ++ESym;
1973
73.0k
  }
1974
2.69k
1975
2.69k
  // On MIPS we need to mark symbol which has a PLT entry and requires
1976
2.69k
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1977
2.69k
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1978
2.69k
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1979
2.69k
  if (Config->EMachine == EM_MIPS) {
1980
3
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1981
3
1982
6
    for (SymbolTableEntry &Ent : Symbols) {
1983
6
      Symbol *Sym = Ent.Sym;
1984
6
      if (Sym->isInPlt() && 
Sym->NeedsPltAddr0
)
1985
0
        ESym->st_other |= STO_MIPS_PLT;
1986
6
      if (isMicroMips()) {
1987
0
        // We already set the less-significant bit for symbols
1988
0
        // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
1989
0
        // records. That allows us to distinguish such symbols in
1990
0
        // the `MIPS<ELFT>::relocateOne()` routine. Now we should
1991
0
        // clear that bit for non-dynamic symbol table, so tools
1992
0
        // like `objdump` will be able to deal with a correct
1993
0
        // symbol position.
1994
0
        if (Sym->isDefined() &&
1995
0
            ((Sym->StOther & STO_MIPS_MICROMIPS) || Sym->NeedsPltAddr)) {
1996
0
          if (!StrTabSec.isDynamic())
1997
0
            ESym->st_value &= ~1;
1998
0
          ESym->st_other |= STO_MIPS_MICROMIPS;
1999
0
        }
2000
0
      }
2001
6
      if (Config->Relocatable)
2002
0
        if (auto *D = dyn_cast<Defined>(Sym))
2003
0
          if (isMipsPIC<ELFT>(D))
2004
0
            ESym->st_other |= STO_MIPS_PIC;
2005
6
      ++ESym;
2006
6
    }
2007
3
  }
2008
2.69k
}
lld::elf::SymbolTableSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::writeTo(unsigned char*)
Line
Count
Source
1927
163
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1928
163
  // The first entry is a null entry as per the ELF spec.
1929
163
  memset(Buf, 0, sizeof(Elf_Sym));
1930
163
  Buf += sizeof(Elf_Sym);
1931
163
1932
163
  auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1933
163
1934
10.6k
  for (SymbolTableEntry &Ent : Symbols) {
1935
10.6k
    Symbol *Sym = Ent.Sym;
1936
10.6k
1937
10.6k
    // Set st_info and st_other.
1938
10.6k
    ESym->st_other = 0;
1939
10.6k
    if (Sym->isLocal()) {
1940
76
      ESym->setBindingAndType(STB_LOCAL, Sym->Type);
1941
10.5k
    } else {
1942
10.5k
      ESym->setBindingAndType(Sym->computeBinding(), Sym->Type);
1943
10.5k
      ESym->setVisibility(Sym->Visibility);
1944
10.5k
    }
1945
10.6k
1946
10.6k
    // The 3 most significant bits of st_other are used by OpenPOWER ABI.
1947
10.6k
    // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
1948
10.6k
    if (Config->EMachine == EM_PPC64)
1949
380
      ESym->st_other |= Sym->StOther & 0xe0;
1950
10.6k
1951
10.6k
    ESym->st_name = Ent.StrTabOffset;
1952
10.6k
    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
1953
10.6k
1954
10.6k
    // Copy symbol size if it is a defined symbol. st_size is not significant
1955
10.6k
    // for undefined symbols, so whether copying it or not is up to us if that's
1956
10.6k
    // the case. We'll leave it as zero because by not setting a value, we can
1957
10.6k
    // get the exact same outputs for two sets of input files that differ only
1958
10.6k
    // in undefined symbol size in DSOs.
1959
10.6k
    if (ESym->st_shndx == SHN_UNDEF)
1960
84
      ESym->st_size = 0;
1961
10.5k
    else
1962
10.5k
      ESym->st_size = Sym->getSize();
1963
10.6k
1964
10.6k
    // st_value is usually an address of a symbol, but that has a
1965
10.6k
    // special meaining for uninstantiated common symbols (this can
1966
10.6k
    // occur if -r is given).
1967
10.6k
    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
1968
0
      ESym->st_value = CommonSec->Alignment;
1969
10.6k
    else
1970
10.6k
      ESym->st_value = Sym->getVA();
1971
10.6k
1972
10.6k
    ++ESym;
1973
10.6k
  }
1974
163
1975
163
  // On MIPS we need to mark symbol which has a PLT entry and requires
1976
163
  // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1977
163
  // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1978
163
  // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1979
163
  if (Config->EMachine == EM_MIPS) {
1980
61
    auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1981
61
1982
10.2k
    for (SymbolTableEntry &Ent : Symbols) {
1983
10.2k
      Symbol *Sym = Ent.Sym;
1984
10.2k
      if (Sym->isInPlt() && 
Sym->NeedsPltAddr4
)
1985
0
        ESym->st_other |= STO_MIPS_PLT;
1986
10.2k
      if (isMicroMips()) {
1987
0
        // We already set the less-significant bit for symbols
1988
0
        // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT