blob: 37d098cbf37e21d00c17c7465693c613507e3ea3 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
11#include "Config.h"
George Rimare2ee72b2016-02-26 14:48:31 +000012#include "LinkerScript.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000013#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000014#include "Target.h"
Rui Ueyamae9809502016-03-11 04:23:12 +000015#include "lld/Core/Parallel.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000016#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000017#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000018#include "llvm/Support/MathExtras.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000019#include "llvm/Support/SHA1.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000020#include <map>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000023using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000025using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026using namespace llvm::ELF;
27
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030
George Rimar12737b72016-02-25 08:40:26 +000031static bool isAlpha(char C) {
32 return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
33}
34
35static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
36
37// Returns true if S is valid as a C language identifier.
Rafael Espindolae0df00b2016-02-28 00:25:54 +000038bool elf::isValidCIdentifier(StringRef S) {
Rui Ueyamadad77c52016-02-26 15:42:06 +000039 return !S.empty() && isAlpha(S[0]) &&
40 std::all_of(S.begin() + 1, S.end(), isAlnum);
George Rimar12737b72016-02-25 08:40:26 +000041}
42
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000043template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000044OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
45 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000046 : Name(Name) {
Sean Silva580c1b62016-04-20 04:26:16 +000047 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000048 Header.sh_type = Type;
49 Header.sh_flags = Flags;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000050}
51
Rafael Espindola35c6af32015-09-25 17:19:10 +000052template <class ELFT>
Rui Ueyamac63c1db2016-03-13 06:50:33 +000053void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *Shdr) {
54 *Shdr = Header;
55}
56
57template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000058GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000059 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
George Rimar648a2c32015-10-20 08:54:27 +000060 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000061}
62
Rafael Espindola67d72c02016-03-11 12:06:30 +000063template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
64 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
65 Entries.push_back(&Sym);
George Rimar648a2c32015-10-20 08:54:27 +000066}
67
68template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000069 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000070}
71
George Rimar648a2c32015-10-20 08:54:27 +000072template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000073 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000074 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000075}
76
77template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000078 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000079 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000080 for (const SymbolBody *B : Entries) {
Rui Ueyamab5a69702016-02-01 21:00:35 +000081 Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
George Rimar648a2c32015-10-20 08:54:27 +000082 Buf += sizeof(uintX_t);
83 }
84}
85
86template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000087GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000088 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000089 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000090 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000091 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000092}
93
Rafael Espindola67d72c02016-03-11 12:06:30 +000094template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Simon Atanasyanf3ec3be2016-03-22 08:36:48 +000095 if (Config->EMachine == EM_MIPS) {
Simon Atanasyanbea20c32016-03-23 09:28:02 +000096 // For "true" local symbols which can be referenced from the same module
97 // only compiler creates two instructions for address loading:
98 //
99 // lw $8, 0($gp) # R_MIPS_GOT16
100 // addi $8, $8, 0 # R_MIPS_LO16
101 //
102 // The first instruction loads high 16 bits of the symbol address while
103 // the second adds an offset. That allows to reduce number of required
104 // GOT entries because only one global offset table entry is necessary
105 // for every 64 KBytes of local data. So for local symbols we need to
106 // allocate number of GOT entries to hold all required "page" addresses.
107 //
108 // All global symbols (hidden and regular) considered by compiler uniformly.
109 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
110 // to load address of the symbol. So for each such symbol we need to
111 // allocate dedicated GOT entry to store its address.
112 //
113 // If a symbol is preemptible we need help of dynamic linker to get its
114 // final address. The corresponding GOT entries are allocated in the
115 // "global" part of GOT. Entries for non preemptible global symbol allocated
116 // in the "local" part of GOT.
117 //
118 // See "Global Offset Table" in Chapter 5:
119 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Simon Atanasyand2980d32016-03-29 14:07:22 +0000120 if (Sym.isLocal()) {
121 // At this point we do not know final symbol value so to reduce number
122 // of allocated GOT entries do the following trick. Save all output
123 // sections referenced by GOT relocations. Then later in the `finalize`
124 // method calculate number of "pages" required to cover all saved output
125 // section and allocate appropriate number of GOT entries.
126 auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec;
127 MipsOutSections.insert(OutSec);
128 return;
129 }
130 if (!Sym.isPreemptible()) {
131 // In case of non-local symbols require an entry in the local part
132 // of MIPS GOT, we set GotIndex to 1 just to accent that this symbol
133 // has the GOT entry and escape creation more redundant GOT entries.
134 // FIXME (simon): We can try to store such symbols in the `Entries`
135 // container. But in that case we have to sort out that container
136 // and update GotIndex assigned to symbols.
137 Sym.GotIndex = 1;
Simon Atanasyanf3ec3be2016-03-22 08:36:48 +0000138 ++MipsLocalEntries;
139 return;
140 }
141 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000142 Sym.GotIndex = Entries.size();
143 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +0000144}
145
Rafael Espindola67d72c02016-03-11 12:06:30 +0000146template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000147 if (Sym.symbol()->GlobalDynIndex != -1U)
George Rimar90cd0a82015-12-01 19:20:26 +0000148 return false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000149 Sym.symbol()->GlobalDynIndex = Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +0000150 // Global Dynamic TLS entries take two GOT slots.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000151 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +0000152 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +0000153 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000154}
155
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000156// Reserves TLS entries for a TLS module ID and a TLS block offset.
157// In total it takes two GOT slots.
158template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
159 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +0000160 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000161 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000162 Entries.push_back(nullptr);
163 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000164 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000165}
166
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167template <class ELFT>
168typename GotSection<ELFT>::uintX_t
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000169GotSection<ELFT>::getMipsLocalPageOffset(uintX_t EntryValue) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000170 // Initialize the entry by the %hi(EntryValue) expression
171 // but without right-shifting.
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000172 return getMipsLocalEntryOffset((EntryValue + 0x8000) & ~0xffff);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000173}
174
175template <class ELFT>
176typename GotSection<ELFT>::uintX_t
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000177GotSection<ELFT>::getMipsLocalEntryOffset(uintX_t EntryValue) {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000178 // Take into account MIPS GOT header.
179 // See comment in the GotSection::writeTo.
180 size_t NewIndex = MipsLocalGotPos.size() + 2;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000181 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
182 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
George Rimar71e64b22016-05-11 09:41:15 +0000183 return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000184}
185
Igor Kudrin304860a2015-11-12 04:39:49 +0000186template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000187typename GotSection<ELFT>::uintX_t
188GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000189 return this->getVA() + B.symbol()->GlobalDynIndex * sizeof(uintX_t);
George Rimar90cd0a82015-12-01 19:20:26 +0000190}
191
192template <class ELFT>
Rafael Espindola74031ba2016-04-07 15:20:56 +0000193typename GotSection<ELFT>::uintX_t
194GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000195 return B.symbol()->GlobalDynIndex * sizeof(uintX_t);
Rafael Espindola74031ba2016-04-07 15:20:56 +0000196}
197
198template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000199const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
200 return Entries.empty() ? nullptr : Entries.front();
201}
202
203template <class ELFT>
204unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000205 return MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000206}
207
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000208template <class ELFT> void GotSection<ELFT>::finalize() {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000209 if (Config->EMachine == EM_MIPS)
210 // Take into account MIPS GOT header.
211 // See comment in the GotSection::writeTo.
212 MipsLocalEntries += 2;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000213 for (const OutputSectionBase<ELFT> *OutSec : MipsOutSections) {
214 // Calculate an upper bound of MIPS GOT entries required to store page
215 // addresses of local symbols. We assume the worst case - each 64kb
216 // page of the output section has at least one GOT relocation against it.
217 // Add 0x8000 to the section's size because the page address stored
218 // in the GOT entry is calculated as (value + 0x8000) & ~0xffff.
219 MipsLocalEntries += (OutSec->getSize() + 0x8000 + 0xfffe) / 0xffff;
220 }
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000221 this->Header.sh_size = (MipsLocalEntries + Entries.size()) * sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000222}
223
Rafael Espindolaa6627382015-10-06 23:56:53 +0000224template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000225 if (Config->EMachine == EM_MIPS) {
226 // Set the MSB of the second GOT slot. This is not required by any
227 // MIPS ABI documentation, though.
228 //
229 // There is a comment in glibc saying that "The MSB of got[1] of a
230 // gnu object is set to identify gnu objects," and in GNU gold it
231 // says "the second entry will be used by some runtime loaders".
232 // But how this field is being used is unclear.
233 //
234 // We are not really willing to mimic other linkers behaviors
235 // without understanding why they do that, but because all files
236 // generated by GNU tools have this special GOT value, and because
237 // we've been doing this for years, it is probably a safe bet to
238 // keep doing this for now. We really need to revisit this to see
239 // if we had to do this.
240 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
241 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
242 }
Rui Ueyama5cbf5d22016-02-02 02:29:03 +0000243 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000244 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
245 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
246 }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000247 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000248 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000249 uint8_t *Entry = Buf;
250 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000251 if (!B)
252 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000253 // MIPS has special rules to fill up GOT entries.
254 // See "Global Offset Table" in Chapter 5 in the following document
255 // for detailed description:
256 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
257 // As the first approach, we can just store addresses for all symbols.
Rui Ueyamac4466602016-03-13 19:48:18 +0000258 if (Config->EMachine != EM_MIPS && B->isPreemptible())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000259 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000260 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000261 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000262 }
263}
264
Rafael Espindola35c6af32015-09-25 17:19:10 +0000265template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000266PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000267 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000268 this->Header.sh_addralign = 16;
269}
270
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000271template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000272 // At beginning of PLT, we have code to call the dynamic linker
273 // to resolve dynsyms at runtime. Write such code.
274 Target->writePltZero(Buf);
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000275 size_t Off = Target->PltZeroSize;
276
George Rimarfb5d7f22015-11-26 19:58:51 +0000277 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000278 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000279 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000280 uint64_t Got = B->getGotPltVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000281 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000282 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000283 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000284 }
285}
286
Rafael Espindola67d72c02016-03-11 12:06:30 +0000287template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
288 Sym.PltIndex = Entries.size();
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000289 unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000290 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000291}
292
George Rimar648a2c32015-10-20 08:54:27 +0000293template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000294 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000295 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000296}
297
298template <class ELFT>
George Rimarc191acf2016-05-10 15:47:57 +0000299RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000300 : OutputSectionBase<ELFT>(Name, Config->Rela ? SHT_RELA : SHT_REL,
George Rimarc191acf2016-05-10 15:47:57 +0000301 SHF_ALLOC),
302 Sort(Sort) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000303 this->Header.sh_entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000304 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000305}
306
George Rimar5828c232015-11-30 17:49:19 +0000307template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000308void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000309 Relocs.push_back(Reloc);
310}
311
George Rimarc191acf2016-05-10 15:47:57 +0000312template <class ELFT, class RelTy>
313static bool compRelocations(const RelTy &A, const RelTy &B) {
314 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
315}
316
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000317template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarc191acf2016-05-10 15:47:57 +0000318 uint8_t *BufBegin = Buf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000319 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rui Ueyama614be592016-03-13 05:23:40 +0000320 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000321 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolade9857e2016-02-04 21:33:05 +0000322 SymbolBody *Sym = Rel.Sym;
Rui Ueyamab0210e832016-01-26 00:24:57 +0000323
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000324 if (Config->Rela)
Rui Ueyama614be592016-03-13 05:23:40 +0000325 P->r_addend = Rel.UseSymVA ? Sym->getVA<ELFT>(Rel.Addend) : Rel.Addend;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000326 P->r_offset = Rel.OffsetInSec + Rel.OffsetSec->getVA();
Rafael Espindolade9857e2016-02-04 21:33:05 +0000327 uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
328 P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000329 }
George Rimarc191acf2016-05-10 15:47:57 +0000330
331 if (Sort) {
332 if (Config->Rela)
333 std::stable_sort((Elf_Rela *)BufBegin,
334 (Elf_Rela *)BufBegin + Relocs.size(),
335 compRelocations<ELFT, Elf_Rela>);
336 else
337 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
338 compRelocations<ELFT, Elf_Rel>);
339 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000340}
341
George Rimar77b77792015-11-25 22:15:01 +0000342template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000343 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000344}
345
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000346template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000347 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
348 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
350}
351
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000352template <class ELFT>
353InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000354 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000355 this->Header.sh_size = Config->DynamicLinker.size() + 1;
356 this->Header.sh_addralign = 1;
357}
358
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000359template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama1e720b92016-03-13 06:50:34 +0000360 StringRef S = Config->DynamicLinker;
361 memcpy(Buf, S.data(), S.size());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000362}
363
Rafael Espindola35c6af32015-09-25 17:19:10 +0000364template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000365HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000366 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000367 this->Header.sh_entsize = sizeof(Elf_Word);
368 this->Header.sh_addralign = sizeof(Elf_Word);
369}
370
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000371static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000372 uint32_t H = 0;
373 for (char C : Name) {
374 H = (H << 4) + C;
375 uint32_t G = H & 0xf0000000;
376 if (G)
377 H ^= G >> 24;
378 H &= ~G;
379 }
380 return H;
381}
382
Rui Ueyama0db335f2015-10-07 16:58:54 +0000383template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000384 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000385
George Rimare9e1d322016-02-18 15:17:01 +0000386 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000387 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000388
389 // Create as many buckets as there are symbols.
390 // FIXME: This is simplistic. We can try to optimize it, but implementing
391 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000392 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000393 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
394}
395
396template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000397 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000398 auto *P = reinterpret_cast<Elf_Word *>(Buf);
399 *P++ = NumSymbols; // nbucket
400 *P++ = NumSymbols; // nchain
401
402 Elf_Word *Buckets = P;
403 Elf_Word *Chains = P + NumSymbols;
404
Rafael Espindolae2c24612016-01-29 01:24:25 +0000405 for (const std::pair<SymbolBody *, unsigned> &P :
406 Out<ELFT>::DynSymTab->getSymbols()) {
407 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000408 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000409 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000410 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000411 Chains[I] = Buckets[Hash];
412 Buckets[Hash] = I;
413 }
414}
415
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000416static uint32_t hashGnu(StringRef Name) {
417 uint32_t H = 5381;
418 for (uint8_t C : Name)
419 H = (H << 5) + H + C;
420 return H;
421}
422
423template <class ELFT>
424GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000425 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000426 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000427 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000428}
429
430template <class ELFT>
431unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
432 if (!NumHashed)
433 return 0;
434
435 // These values are prime numbers which are not greater than 2^(N-1) + 1.
436 // In result, for any particular NumHashed we return a prime number
437 // which is not greater than NumHashed.
438 static const unsigned Primes[] = {
439 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
440 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
441
442 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
443 array_lengthof(Primes) - 1)];
444}
445
446// Bloom filter estimation: at least 8 bits for each hashed symbol.
447// GNU Hash table requirement: it should be a power of 2,
448// the minimum value is 1, even for an empty table.
449// Expected results for a 32-bit target:
450// calcMaskWords(0..4) = 1
451// calcMaskWords(5..8) = 2
452// calcMaskWords(9..16) = 4
453// For a 64-bit target:
454// calcMaskWords(0..8) = 1
455// calcMaskWords(9..16) = 2
456// calcMaskWords(17..32) = 4
457template <class ELFT>
458unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
459 if (!NumHashed)
460 return 1;
461 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
462}
463
464template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000465 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000466 NBuckets = calcNBuckets(NumHashed);
467 MaskWords = calcMaskWords(NumHashed);
468 // Second hash shift estimation: just predefined values.
469 Shift2 = ELFT::Is64Bits ? 6 : 5;
470
471 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
472 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
473 + sizeof(Elf_Off) * MaskWords // Bloom Filter
474 + sizeof(Elf_Word) * NBuckets // Hash Buckets
475 + sizeof(Elf_Word) * NumHashed; // Hash Values
476}
477
478template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
479 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000480 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000481 return;
482 writeBloomFilter(Buf);
483 writeHashTable(Buf);
484}
485
486template <class ELFT>
487void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
488 auto *P = reinterpret_cast<Elf_Word *>(Buf);
489 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000490 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000491 *P++ = MaskWords;
492 *P++ = Shift2;
493 Buf = reinterpret_cast<uint8_t *>(P);
494}
495
496template <class ELFT>
497void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
498 unsigned C = sizeof(Elf_Off) * 8;
499
500 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000501 for (const SymbolData &Sym : Symbols) {
502 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
503 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
504 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000505 Masks[Pos] |= V;
506 }
507 Buf += sizeof(Elf_Off) * MaskWords;
508}
509
510template <class ELFT>
511void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
512 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
513 Elf_Word *Values = Buckets + NBuckets;
514
515 int PrevBucket = -1;
516 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000517 for (const SymbolData &Sym : Symbols) {
518 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000519 assert(PrevBucket <= Bucket);
520 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000521 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000522 PrevBucket = Bucket;
523 if (I > 0)
524 Values[I - 1] |= 1;
525 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000526 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000527 ++I;
528 }
529 if (I > 0)
530 Values[I - 1] |= 1;
531}
532
Rafael Espindola31f88882015-11-02 14:33:11 +0000533static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000534 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000535 return !B->isUndefined();
536}
537
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000538// Add symbols to this symbol hash table. Note that this function
539// destructively sort a given vector -- which is needed because
540// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000541template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000542void GnuHashTableSection<ELFT>::addSymbols(
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000543 std::vector<std::pair<SymbolBody *, size_t>> &V) {
544 auto Mid = std::stable_partition(V.begin(), V.end(),
545 [](std::pair<SymbolBody *, size_t> &P) {
546 return !includeInGnuHashTable(P.first);
547 });
548 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000549 return;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000550 for (auto I = Mid, E = V.end(); I != E; ++I) {
551 SymbolBody *B = I->first;
552 size_t StrOff = I->second;
553 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
554 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000555
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000556 unsigned NBuckets = calcNBuckets(Symbols.size());
557 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000558 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000559 return L.Hash % NBuckets < R.Hash % NBuckets;
560 });
561
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000562 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000563 for (const SymbolData &Sym : Symbols)
564 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000565}
566
567template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000568DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000569 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000570 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000571 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000572 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000573 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000574
575 // .dynamic section is not writable on MIPS.
576 // See "Special Section" in Chapter 4 in the following document:
577 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
578 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000579 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000580}
581
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000582template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000583 if (this->Header.sh_size)
584 return; // Already finalized.
585
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000586 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000587 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000588
Rafael Espindolae2c24612016-01-29 01:24:25 +0000589 auto Add = [=](Entry E) { Entries.push_back(E); };
590
591 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000592 // DynStrTab and doing this here allows this function to set DT_STRSZ.
593 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000594 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
595 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000596 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
597 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000598 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
599 if (!Config->SoName.empty())
600 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
601
Rafael Espindolade069362016-01-25 21:32:04 +0000602 Out<ELFT>::DynStrTab->finalize();
603
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000604 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000605 bool IsRela = Config->Rela;
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000606 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
607 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
608 Add({IsRela ? DT_RELAENT : DT_RELENT,
609 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000610 }
George Rimar648a2c32015-10-20 08:54:27 +0000611 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000612 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
613 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
614 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
615 Out<ELFT>::GotPlt});
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000616 Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000617 }
618
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000619 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
620 Add({DT_SYMENT, sizeof(Elf_Sym)});
621 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
622 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000623 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000624 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000625 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000626 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000627
Rafael Espindolade069362016-01-25 21:32:04 +0000628 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000629 Add({DT_PREINIT_ARRAY, PreInitArraySec});
630 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000631 }
632 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000633 Add({DT_INIT_ARRAY, InitArraySec});
634 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000635 }
636 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000637 Add({DT_FINI_ARRAY, FiniArraySec});
638 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000639 }
640
Rui Ueyama304d1352016-01-25 21:47:25 +0000641 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000642 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000643 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000644 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000645
Rafael Espindolade069362016-01-25 21:32:04 +0000646 uint32_t DtFlags = 0;
647 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000648 if (Config->Bsymbolic)
649 DtFlags |= DF_SYMBOLIC;
650 if (Config->ZNodelete)
651 DtFlags1 |= DF_1_NODELETE;
652 if (Config->ZNow) {
653 DtFlags |= DF_BIND_NOW;
654 DtFlags1 |= DF_1_NOW;
655 }
656 if (Config->ZOrigin) {
657 DtFlags |= DF_ORIGIN;
658 DtFlags1 |= DF_1_ORIGIN;
659 }
660
661 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000662 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000663 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000664 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000665
Ed Mastef5d3cf62016-01-06 15:52:27 +0000666 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000667 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000668
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000669 if (size_t NeedNum = Out<ELFT>::VerNeed->getNeedNum()) {
670 Add({DT_VERSYM, Out<ELFT>::VerSym});
671 Add({DT_VERNEED, Out<ELFT>::VerNeed});
672 Add({DT_VERNEEDNUM, NeedNum});
673 }
674
Igor Kudrin304860a2015-11-12 04:39:49 +0000675 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000676 Add({DT_MIPS_RLD_VERSION, 1});
677 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
678 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
679 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
680 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000681 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000682 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000683 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000684 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
685 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000686 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000687 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000688 }
689
Rafael Espindolade069362016-01-25 21:32:04 +0000690 // +1 for DT_NULL
691 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000692}
693
694template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000695 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
696
Rafael Espindolade069362016-01-25 21:32:04 +0000697 for (const Entry &E : Entries) {
698 P->d_tag = E.Tag;
699 switch (E.Kind) {
700 case Entry::SecAddr:
701 P->d_un.d_ptr = E.OutSec->getVA();
702 break;
703 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000704 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000705 break;
706 case Entry::PlainInt:
707 P->d_un.d_val = E.Val;
708 break;
709 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000710 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000711 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000712}
713
714template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000715EhFrameHeader<ELFT>::EhFrameHeader()
Rui Ueyamade9777a2016-05-23 16:30:41 +0000716 : OutputSectionBase<ELFT>(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000717
Rui Ueyama95a232e2016-05-23 01:45:05 +0000718// .eh_frame_hdr contains a binary search table of pointers to FDEs.
719// Each entry of the search table consists of two values,
720// the starting PC from where FDEs covers, and the FDE's address.
721// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000722template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
723 const endianness E = ELFT::TargetEndianness;
724
Rui Ueyamae75e9332016-05-23 03:00:33 +0000725 // Sort the FDE list by their PC and uniqueify. Usually there is only
726 // one FDE for a PC (i.e. function), but if ICF merges two functions
727 // into one, there can be more than one FDEs pointing to the address.
728 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
729 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
730 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
731 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000732
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000733 Buf[0] = 1;
734 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
735 Buf[2] = DW_EH_PE_udata4;
736 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000737 write32<E>(Buf + 4, Out<ELFT>::EhFrame->getVA() - this->getVA() - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000738 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000739 Buf += 12;
740
Rui Ueyamae75e9332016-05-23 03:00:33 +0000741 uintX_t VA = this->getVA();
742 for (FdeData &Fde : Fdes) {
743 write32<E>(Buf, Fde.Pc - VA);
744 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000745 Buf += 8;
746 }
747}
748
Rui Ueyamade9777a2016-05-23 16:30:41 +0000749template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
750 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
751 this->Header.sh_size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
752}
753
George Rimarf6bc65a2016-01-15 13:34:52 +0000754template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000755void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
756 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000757}
758
George Rimarf6bc65a2016-01-15 13:34:52 +0000759template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000760OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
761 : OutputSectionBase<ELFT>(Name, Type, Flags) {
762 if (Type == SHT_RELA)
763 this->Header.sh_entsize = sizeof(Elf_Rela);
764 else if (Type == SHT_REL)
765 this->Header.sh_entsize = sizeof(Elf_Rel);
766}
767
768template <class ELFT> void OutputSection<ELFT>::finalize() {
769 uint32_t Type = this->Header.sh_type;
770 if (Type != SHT_RELA && Type != SHT_REL)
771 return;
772 this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex;
773 // sh_info for SHT_REL[A] sections should contain the section header index of
774 // the section to which the relocation applies.
775 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
776 this->Header.sh_info = S->OutSec->SectionIndex;
777}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000778
779template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000780void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000781 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000782 auto *S = cast<InputSection<ELFT>>(C);
783 Sections.push_back(S);
784 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +0000785 this->updateAlign(S->Align);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000786}
787
Rui Ueyamac4185702016-02-10 23:20:42 +0000788// If an input string is in the form of "foo.N" where N is a number,
789// return N. Otherwise, returns 65536, which is one greater than the
790// lowest priority.
791static int getPriority(StringRef S) {
792 size_t Pos = S.rfind('.');
793 if (Pos == StringRef::npos)
794 return 65536;
795 int V;
796 if (S.substr(Pos + 1).getAsInteger(10, V))
797 return 65536;
798 return V;
799}
800
Rafael Espindola56004c52016-04-07 14:22:09 +0000801template <class ELFT>
802void OutputSection<ELFT>::forEachInputSection(
Rui Ueyama10803512016-05-22 00:25:30 +0000803 std::function<void(InputSectionBase<ELFT> *)> F) {
Rafael Espindola56004c52016-04-07 14:22:09 +0000804 for (InputSection<ELFT> *S : Sections)
805 F(S);
Rui Ueyama5af83682016-02-11 23:41:38 +0000806}
807
Rui Ueyamac4185702016-02-10 23:20:42 +0000808// Sorts input sections by section name suffixes, so that .foo.N comes
809// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000810// We want to keep the original order if the priorities are the same
811// because the compiler keeps the original initialization order in a
812// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000813// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000814template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000815 // Sort sections by priority.
816 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000817 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
818
Rui Ueyamac4185702016-02-10 23:20:42 +0000819 std::vector<Pair> V;
820 for (InputSection<ELFT> *S : Sections)
821 V.push_back({getPriority(S->getSectionName()), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000822 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000823 Sections.clear();
824 for (Pair &P : V)
825 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000826}
Rui Ueyamac4185702016-02-10 23:20:42 +0000827
Rui Ueyama5af83682016-02-11 23:41:38 +0000828// Returns true if S matches /Filename.?\.o$/.
829static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
830 if (!S.endswith(".o"))
831 return false;
832 S = S.drop_back(2);
833 if (S.endswith(Filename))
834 return true;
835 return !S.empty() && S.drop_back().endswith(Filename);
836}
837
838static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
839static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
840
841// .ctors and .dtors are sorted by this priority from highest to lowest.
842//
843// 1. The section was contained in crtbegin (crtbegin contains
844// some sentinel value in its .ctors and .dtors so that the runtime
845// can find the beginning of the sections.)
846//
847// 2. The section has an optional priority value in the form of ".ctors.N"
848// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
849// they are compared as string rather than number.
850//
851// 3. The section is just ".ctors" or ".dtors".
852//
853// 4. The section was contained in crtend, which contains an end marker.
854//
855// In an ideal world, we don't need this function because .init_array and
856// .ctors are duplicate features (and .init_array is newer.) However, there
857// are too many real-world use cases of .ctors, so we had no choice to
858// support that with this rather ad-hoc semantics.
859template <class ELFT>
860static bool compCtors(const InputSection<ELFT> *A,
861 const InputSection<ELFT> *B) {
862 bool BeginA = isCrtbegin(A->getFile()->getName());
863 bool BeginB = isCrtbegin(B->getFile()->getName());
864 if (BeginA != BeginB)
865 return BeginA;
866 bool EndA = isCrtend(A->getFile()->getName());
867 bool EndB = isCrtend(B->getFile()->getName());
868 if (EndA != EndB)
869 return EndB;
870 StringRef X = A->getSectionName();
871 StringRef Y = B->getSectionName();
872 assert(X.startswith(".ctors") || X.startswith(".dtors"));
873 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
874 X = X.substr(6);
875 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000876 if (X.empty() && Y.empty())
877 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000878 return X < Y;
879}
880
881// Sorts input sections by the special rules for .ctors and .dtors.
882// Unfortunately, the rules are different from the one for .{init,fini}_array.
883// Read the comment above.
884template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
885 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000886}
887
George Rimare2ee72b2016-02-26 14:48:31 +0000888static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
889 size_t I = 0;
890 for (; I + A.size() < Size; I += A.size())
891 memcpy(Buf + I, A.data(), A.size());
892 memcpy(Buf + I, A.data(), Size - I);
893}
894
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000895template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000896 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +0000897 if (!Filler.empty())
898 fill(Buf, this->getSize(), Filler);
Rui Ueyamae9809502016-03-11 04:23:12 +0000899 if (Config->Threads) {
900 parallel_for_each(Sections.begin(), Sections.end(),
901 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
902 } else {
903 for (InputSection<ELFT> *C : Sections)
904 C->writeTo(Buf);
905 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000906}
907
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000908template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000909EhOutputSection<ELFT>::EhOutputSection()
Rui Ueyama3b31e672016-05-23 16:24:16 +0000910 : OutputSectionBase<ELFT>(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000911
912template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000913void EhOutputSection<ELFT>::forEachInputSection(
Rafael Espindola56004c52016-04-07 14:22:09 +0000914 std::function<void(InputSectionBase<ELFT> *)> F) {
915 for (EHInputSection<ELFT> *S : Sections)
916 F(S);
917}
918
George Rimarf6bc65a2016-01-15 13:34:52 +0000919// Read a byte and advance D by one byte.
920static uint8_t readByte(ArrayRef<uint8_t> &D) {
921 if (D.empty())
George Rimar57610422016-03-11 14:43:02 +0000922 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000923 uint8_t B = D.front();
924 D = D.slice(1);
925 return B;
926}
927
928static void skipLeb128(ArrayRef<uint8_t> &D) {
929 while (!D.empty()) {
930 uint8_t Val = D.front();
931 D = D.slice(1);
932 if ((Val & 0x80) == 0)
933 return;
934 }
George Rimar57610422016-03-11 14:43:02 +0000935 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000936}
937
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000938template <class ELFT> static size_t getAugPSize(unsigned Enc) {
939 switch (Enc & 0x0f) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000940 case DW_EH_PE_absptr:
941 case DW_EH_PE_signed:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000942 return ELFT::Is64Bits ? 8 : 4;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000943 case DW_EH_PE_udata2:
944 case DW_EH_PE_sdata2:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000945 return 2;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000946 case DW_EH_PE_udata4:
947 case DW_EH_PE_sdata4:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000948 return 4;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000949 case DW_EH_PE_udata8:
950 case DW_EH_PE_sdata8:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000951 return 8;
952 }
George Rimar57610422016-03-11 14:43:02 +0000953 fatal("unknown FDE encoding");
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000954}
955
956template <class ELFT> static void skipAugP(ArrayRef<uint8_t> &D) {
957 uint8_t Enc = readByte(D);
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000958 if ((Enc & 0xf0) == DW_EH_PE_aligned)
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000959 fatal("DW_EH_PE_aligned encoding is not supported");
960 size_t Size = getAugPSize<ELFT>(Enc);
Rafael Espindola9e072d32016-02-09 22:47:34 +0000961 if (Size >= D.size())
George Rimar57610422016-03-11 14:43:02 +0000962 fatal("corrupted CIE");
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000963 D = D.slice(Size);
964}
965
Rui Ueyama6de2e682016-05-24 02:08:38 +0000966template <class ELFT> static uint8_t getFdeEncoding(ArrayRef<uint8_t> D) {
Rui Ueyamabe748c22016-02-08 05:18:44 +0000967 if (D.size() < 8)
968 fatal("CIE too small");
George Rimarf6bc65a2016-01-15 13:34:52 +0000969 D = D.slice(8);
970
971 uint8_t Version = readByte(D);
972 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000973 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +0000974
Saleem Abdulrasoolc0571e12016-02-15 03:45:18 +0000975 const unsigned char *AugEnd = std::find(D.begin() + 1, D.end(), '\0');
Rui Ueyamabe748c22016-02-08 05:18:44 +0000976 if (AugEnd == D.end())
George Rimar57610422016-03-11 14:43:02 +0000977 fatal("corrupted CIE");
Saleem Abdulrasoolc0571e12016-02-15 03:45:18 +0000978 StringRef Aug(reinterpret_cast<const char *>(D.begin()), AugEnd - D.begin());
Rui Ueyamabe748c22016-02-08 05:18:44 +0000979 D = D.slice(Aug.size() + 1);
George Rimarf6bc65a2016-01-15 13:34:52 +0000980
981 // Code alignment factor should always be 1 for .eh_frame.
982 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000983 fatal("CIE code alignment must be 1");
Rui Ueyamabe748c22016-02-08 05:18:44 +0000984
985 // Skip data alignment factor.
George Rimarf6bc65a2016-01-15 13:34:52 +0000986 skipLeb128(D);
987
988 // Skip the return address register. In CIE version 1 this is a single
989 // byte. In CIE version 3 this is an unsigned LEB128.
990 if (Version == 1)
991 readByte(D);
992 else
993 skipLeb128(D);
994
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000995 // We only care about an 'R' value, but other records may precede an 'R'
996 // record. Records are not in TLV (type-length-value) format, so we need
997 // to teach the linker how to skip records for each type.
Rui Ueyamad3bd97a2016-02-09 23:11:21 +0000998 for (char C : Aug) {
999 if (C == 'R')
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001000 return readByte(D);
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001001 if (C == 'z') {
1002 skipLeb128(D);
1003 continue;
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001004 }
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001005 if (C == 'P') {
1006 skipAugP<ELFT>(D);
1007 continue;
1008 }
Simon Atanasyanea423e22016-03-02 05:38:42 +00001009 if (C == 'L') {
1010 readByte(D);
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001011 continue;
Simon Atanasyanea423e22016-03-02 05:38:42 +00001012 }
George Rimar57610422016-03-11 14:43:02 +00001013 fatal("unknown .eh_frame augmentation string: " + Aug);
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001014 }
Rui Ueyamadbcfedb2016-02-09 21:46:11 +00001015 return DW_EH_PE_absptr;
George Rimarf6bc65a2016-01-15 13:34:52 +00001016}
1017
Rui Ueyama6bf7d912016-05-21 19:06:33 +00001018// Returns the first relocation that points to a region
1019// between Begin and Begin+Size.
1020template <class IntTy, class RelTy>
1021static const RelTy *getReloc(IntTy Begin, IntTy Size, ArrayRef<RelTy> Rels) {
1022 size_t I = 0;
1023 size_t E = Rels.size();
1024 while (I != E && Rels[I].r_offset < Begin)
1025 ++I;
1026 if (I == E || Begin + Size <= Rels[I].r_offset)
1027 return nullptr;
1028 return &Rels[I];
1029}
1030
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001031// Search for an existing CIE record or create a new one.
1032// CIE records from input object files are uniquified by their contents
1033// and where their relocations point to.
1034template <class ELFT>
1035template <class RelTy>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001036CieRecord *EhOutputSection<ELFT>::addCie(SectionPiece &Piece,
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001037 EHInputSection<ELFT> *Sec,
1038 ArrayRef<RelTy> Rels) {
1039 const endianness E = ELFT::TargetEndianness;
1040 if (read32<E>(Piece.Data.data() + 4) != 0)
1041 fatal("CIE expected at beginning of .eh_frame: " + Sec->getSectionName());
1042
1043 SymbolBody *Personality = nullptr;
1044 if (const RelTy *Rel = getReloc(Piece.InputOff, Piece.size(), Rels))
1045 Personality = &Sec->getFile()->getRelocTargetSym(*Rel);
1046
1047 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001048 CieRecord *Cie = &CieMap[{Piece.Data, Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001049
1050 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001051 if (Cie->Piece == nullptr) {
1052 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001053 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001054 }
1055 return Cie;
1056}
1057
1058template <class ELFT> static void validateFde(SectionPiece &Piece) {
1059 // We assume that all FDEs refer the first CIE in the same object file.
1060 const endianness E = ELFT::TargetEndianness;
1061 uint32_t ID = read32<E>(Piece.Data.data() + 4);
1062 if (Piece.InputOff + 4 - ID != 0)
1063 fatal("invalid CIE reference");
1064}
1065
1066// There is one FDE per function. Returns true if a given FDE
1067// points to a live function.
1068template <class ELFT>
1069template <class RelTy>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001070bool EhOutputSection<ELFT>::isFdeLive(SectionPiece &Piece,
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001071 EHInputSection<ELFT> *Sec,
1072 ArrayRef<RelTy> Rels) {
1073 const RelTy *Rel = getReloc(Piece.InputOff, Piece.size(), Rels);
1074 if (!Rel)
1075 fatal("FDE doesn't reference another section");
1076 SymbolBody &B = Sec->getFile()->getRelocTargetSym(*Rel);
1077 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
1078 if (!D || !D->Section)
1079 return false;
1080 InputSectionBase<ELFT> *Target = D->Section->Repl;
1081 return Target && Target->Live;
1082}
1083
1084// .eh_frame is a sequence of CIE or FDE records. In general, there
1085// is one CIE record per input object file which is followed by
1086// a list of FDEs. This function searches an existing CIE or create a new
1087// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +00001088template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +00001089template <class RelTy>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001090void EhOutputSection<ELFT>::addSectionAux(EHInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +00001091 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001092 SectionPiece &CiePiece = Sec->Pieces[0];
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001093 CieRecord *Cie = addCie(CiePiece, Sec, Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001094
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001095 for (size_t I = 1, End = Sec->Pieces.size(); I != End; ++I) {
1096 SectionPiece &FdePiece = Sec->Pieces[I];
1097 validateFde<ELFT>(FdePiece);
1098 if (!isFdeLive(FdePiece, Sec, Rels))
1099 continue;
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001100 Cie->FdePieces.push_back(&FdePiece);
Rui Ueyamade9777a2016-05-23 16:30:41 +00001101 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001102 }
1103}
1104
1105template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001106void EhOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama10803512016-05-22 00:25:30 +00001107 auto *Sec = cast<EHInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001108 Sec->OutSec = this;
1109 this->updateAlign(Sec->Align);
1110 Sections.push_back(Sec);
1111
1112 // .eh_frame is a sequence of CIE or FDE records. This function
1113 // splits it into pieces so that we can call
1114 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +00001115 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001116 if (Sec->Pieces.empty())
1117 return;
1118
1119 if (const Elf_Shdr *RelSec = Sec->RelocSection) {
1120 ELFFile<ELFT> &Obj = Sec->getFile()->getObj();
1121 if (RelSec->sh_type == SHT_RELA)
1122 addSectionAux(Sec, Obj.relas(RelSec));
1123 else
1124 addSectionAux(Sec, Obj.rels(RelSec));
Rui Ueyama0de86c12016-01-27 22:23:44 +00001125 return;
1126 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001127 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001128}
1129
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001130template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +00001131static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
1132 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +00001133
1134 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001135 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +00001136 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +00001137}
1138
Rui Ueyama1e479c22016-05-23 15:07:59 +00001139template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola56004c52016-04-07 14:22:09 +00001140 if (Finalized)
1141 return;
1142 Finalized = true;
1143
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001144 size_t Off = 0;
1145 for (CieRecord *Cie : Cies) {
1146 Cie->Piece->OutputOff = Off;
1147 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001148
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001149 for (SectionPiece *Fde : Cie->FdePieces) {
1150 Fde->OutputOff = Off;
1151 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001152 }
1153 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001154 this->Header.sh_size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001155}
1156
Rui Ueyamae75e9332016-05-23 03:00:33 +00001157template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
1158 const endianness E = ELFT::TargetEndianness;
1159 switch (Size) {
1160 case DW_EH_PE_udata2:
1161 return read16<E>(Buf);
1162 case DW_EH_PE_udata4:
1163 return read32<E>(Buf);
1164 case DW_EH_PE_udata8:
1165 return read64<E>(Buf);
1166 case DW_EH_PE_absptr:
1167 if (ELFT::Is64Bits)
1168 return read64<E>(Buf);
1169 return read32<E>(Buf);
1170 }
1171 fatal("unknown FDE size encoding");
1172}
1173
1174// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
1175// We need it to create .eh_frame_hdr section.
1176template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001177typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +00001178 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001179 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +00001180 // stored at FDE + 8 byte.
1181 size_t Off = FdeOff + 8;
1182 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
1183 if ((Enc & 0x70) == DW_EH_PE_absptr)
1184 return Addr;
1185 if ((Enc & 0x70) == DW_EH_PE_pcrel)
1186 return Addr + this->getVA() + Off;
1187 fatal("unknown FDE size relative encoding");
1188}
1189
Rui Ueyama1e479c22016-05-23 15:07:59 +00001190template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001191 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001192 for (CieRecord *Cie : Cies) {
1193 size_t CieOffset = Cie->Piece->OutputOff;
1194 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->Data);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001195
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001196 for (SectionPiece *Fde : Cie->FdePieces) {
1197 size_t Off = Fde->OutputOff;
1198 writeCieFde<ELFT>(Buf + Off, Fde->Data);
Rafael Espindola56004c52016-04-07 14:22:09 +00001199
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001200 // FDE's second word should have the offset to an associated CIE.
1201 // Write it.
1202 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001203 }
1204 }
1205
Rafael Espindola22ef9562016-04-13 01:40:19 +00001206 for (EHInputSection<ELFT> *S : Sections)
1207 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +00001208
1209 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001210 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +00001211 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +00001212 if (Out<ELFT>::EhFrameHdr) {
1213 for (CieRecord *Cie : Cies) {
Rui Ueyama6de2e682016-05-24 02:08:38 +00001214 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->Data);
Rui Ueyama3b31e672016-05-23 16:24:16 +00001215 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +00001216 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rui Ueyama3b31e672016-05-23 16:24:16 +00001217 uintX_t FdeVA = this->getVA() + Fde->OutputOff;
1218 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
1219 }
Rui Ueyamae75e9332016-05-23 03:00:33 +00001220 }
1221 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001222}
1223
1224template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001225MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001226 uintX_t Flags, uintX_t Alignment)
1227 : OutputSectionBase<ELFT>(Name, Type, Flags),
1228 Builder(llvm::StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001229
1230template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001231 if (shouldTailMerge()) {
1232 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001233 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001234 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001235 }
Rui Ueyama31f9f612016-05-06 00:52:08 +00001236 for (const std::pair<CachedHash<StringRef>, size_t> &P : Builder.getMap()) {
1237 StringRef Data = P.first.Val;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001238 memcpy(Buf + P.second, Data.data(), Data.size());
1239 }
1240}
1241
Rui Ueyama34dc99e2016-05-22 01:15:32 +00001242static StringRef toStringRef(ArrayRef<uint8_t> A) {
1243 return {(const char *)A.data(), A.size()};
1244}
1245
Rafael Espindolac159c962015-10-19 21:00:02 +00001246template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001247void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama10803512016-05-22 00:25:30 +00001248 auto *Sec = cast<MergeInputSection<ELFT>>(C);
1249 Sec->OutSec = this;
1250 this->updateAlign(Sec->Align);
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001251 this->Header.sh_entsize = Sec->getSectionHdr()->sh_entsize;
Rafael Espindolac159c962015-10-19 21:00:02 +00001252
Rui Ueyama10803512016-05-22 00:25:30 +00001253 ArrayRef<uint8_t> D = Sec->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001254 StringRef Data((const char *)D.data(), D.size());
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001255 bool IsString = this->Header.sh_flags & SHF_STRINGS;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001256
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001257 for (size_t I = 0, N = Sec->Pieces.size(); I != N; ++I) {
1258 SectionPiece &Piece = Sec->Pieces[I];
Rui Ueyama3ea87272016-05-22 00:13:04 +00001259 if (!Piece.Live)
Rafael Espindola0b9531c2016-04-22 22:09:35 +00001260 continue;
Rui Ueyama34dc99e2016-05-22 01:15:32 +00001261 uintX_t OutputOffset = Builder.add(toStringRef(Piece.Data));
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001262 if (!IsString || !shouldTailMerge())
1263 Piece.OutputOff = OutputOffset;
Rafael Espindolac159c962015-10-19 21:00:02 +00001264 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001265}
1266
1267template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001268unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1269 return Builder.getOffset(Val);
1270}
1271
1272template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1273 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1274}
1275
1276template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1277 if (shouldTailMerge())
1278 Builder.finalize();
1279 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001280}
1281
1282template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001283StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001284 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1285 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001286 Dynamic(Dynamic) {
1287 this->Header.sh_addralign = 1;
1288}
1289
Rafael Espindolae2c24612016-01-29 01:24:25 +00001290// Adds a string to the string table. If HashIt is true we hash and check for
1291// duplicates. It is optional because the name of global symbols are already
1292// uniqued and hashing them again has a big cost for a small value: uniquing
1293// them with some other string that happens to be the same.
1294template <class ELFT>
1295unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1296 if (HashIt) {
1297 auto R = StringMap.insert(std::make_pair(S, Size));
1298 if (!R.second)
1299 return R.first->second;
1300 }
1301 unsigned Ret = Size;
1302 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001303 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001304 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001305}
1306
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001307template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001308 // ELF string tables start with NUL byte, so advance the pointer by one.
1309 ++Buf;
1310 for (StringRef S : Strings) {
1311 memcpy(Buf, S.data(), S.size());
1312 Buf += S.size() + 1;
1313 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001314}
1315
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001316template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001317SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001318 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001319 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1320 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001321 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001322 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001323 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001324 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001325}
1326
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001327// Orders symbols according to their positions in the GOT,
1328// in compliance with MIPS ABI rules.
1329// See "Global Offset Table" in Chapter 5 in the following document
1330// for detailed description:
1331// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001332static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1333 const std::pair<SymbolBody *, unsigned> &R) {
Simon Atanasyand2980d32016-03-29 14:07:22 +00001334 // Sort entries related to non-local preemptible symbols by GOT indexes.
1335 // All other entries go to the first part of GOT in arbitrary order.
1336 bool LIsInLocalGot = !L.first->isInGot() || !L.first->isPreemptible();
1337 bool RIsInLocalGot = !R.first->isInGot() || !R.first->isPreemptible();
1338 if (LIsInLocalGot || RIsInLocalGot)
1339 return !RIsInLocalGot;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001340 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001341}
1342
Rafael Espindolabadd3972016-04-08 15:30:56 +00001343static uint8_t getSymbolBinding(SymbolBody *Body) {
Peter Collingbourne4f952702016-05-01 04:55:03 +00001344 Symbol *S = Body->symbol();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001345 uint8_t Visibility = S->Visibility;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001346 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1347 return STB_LOCAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001348 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
Rafael Espindolabadd3972016-04-08 15:30:56 +00001349 return STB_GLOBAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001350 return S->Binding;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001351}
1352
Rui Ueyama0db335f2015-10-07 16:58:54 +00001353template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001354 if (this->Header.sh_size)
1355 return; // Already finalized.
1356
Rui Ueyama0db335f2015-10-07 16:58:54 +00001357 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001358 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001359 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001360
George Rimar58941ee2016-02-25 08:23:37 +00001361 if (Config->Relocatable) {
1362 size_t I = NumLocals;
1363 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
1364 P.first->DynsymIndex = ++I;
1365 return;
1366 }
1367
Igor Kudrinab665fc2015-10-20 21:47:58 +00001368 if (!StrTabSec.isDynamic()) {
1369 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001370 [](const std::pair<SymbolBody *, unsigned> &L,
1371 const std::pair<SymbolBody *, unsigned> &R) {
1372 return getSymbolBinding(L.first) == STB_LOCAL &&
1373 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001374 });
1375 return;
1376 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001377 if (Out<ELFT>::GnuHashTab)
1378 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1379 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001380 else if (Config->EMachine == EM_MIPS)
1381 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001382 size_t I = 0;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001383 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001384 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001385}
1386
1387template <class ELFT>
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001388void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1389 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001390}
1391
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001392template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001393 Buf += sizeof(Elf_Sym);
1394
1395 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1396 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001397 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1398 writeLocalSymbols(Buf);
1399
1400 writeGlobalSymbols(Buf);
1401}
1402
1403template <class ELFT>
1404void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1405 // Iterate over all input object files to copy their local symbols
1406 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001407 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001408 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1409 File->KeptLocalSyms) {
1410 const DefinedRegular<ELFT> &Body = *P.first;
1411 InputSectionBase<ELFT> *Section = Body.Section;
Rui Ueyamac55733e2015-09-30 00:54:29 +00001412 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001413
1414 if (!Section) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001415 ESym->st_shndx = SHN_ABS;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001416 ESym->st_value = Body.Value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001417 } else {
Rafael Espindolac159c962015-10-19 21:00:02 +00001418 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1419 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001420 ESym->st_value = OutSec->getVA() + Section->getOffset(Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001421 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001422 ESym->st_name = P.second;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001423 ESym->st_size = Body.template getSize<ELFT>();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001424 ESym->setBindingAndType(STB_LOCAL, Body.Type);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001425 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001426 }
1427 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001428}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001429
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001430template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001431void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001432 // Write the internal symbol table contents to the output symbol table
1433 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001434 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001435 for (const std::pair<SymbolBody *, size_t> &P : Symbols) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001436 SymbolBody *Body = P.first;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001437 size_t StrOff = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001438
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001439 uint8_t Type = Body->Type;
1440 uintX_t Size = Body->getSize<ELFT>();
Rafael Espindola8614c562015-10-06 14:33:58 +00001441
Igor Kudrin853b88d2015-10-20 20:52:14 +00001442 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001443 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001444 ESym->st_name = StrOff;
Peter Collingbourne4f952702016-05-01 04:55:03 +00001445 ESym->setVisibility(Body->symbol()->Visibility);
Rui Ueyamab5a69702016-02-01 21:00:35 +00001446 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001447
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001448 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001449 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001450 else if (isa<DefinedRegular<ELFT>>(Body))
1451 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +00001452
1453 // On MIPS we need to mark symbol which has a PLT entry and requires pointer
1454 // equality by STO_MIPS_PLT flag. That is necessary to help dynamic linker
1455 // distinguish such symbols and MIPS lazy-binding stubs.
1456 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1457 if (Config->EMachine == EM_MIPS && Body->isInPlt() &&
1458 Body->NeedsCopyOrPltAddr)
Simon Atanasyanbea96982016-02-25 21:09:05 +00001459 ESym->st_other |= STO_MIPS_PLT;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001460 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001461 }
1462}
1463
Igor Kudrin853b88d2015-10-20 20:52:14 +00001464template <class ELFT>
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001465const OutputSectionBase<ELFT> *
1466SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1467 switch (Sym->kind()) {
1468 case SymbolBody::DefinedSyntheticKind:
Peter Collingbourne6a422592016-05-03 01:21:08 +00001469 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001470 case SymbolBody::DefinedRegularKind: {
Rafael Espindola5ffa13c2016-04-15 00:15:02 +00001471 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +00001472 if (D.Section)
1473 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001474 break;
1475 }
1476 case SymbolBody::DefinedCommonKind:
1477 return Out<ELFT>::Bss;
1478 case SymbolBody::SharedKind:
1479 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1480 return Out<ELFT>::Bss;
1481 break;
Peter Collingbourne60976ed2016-04-27 00:05:06 +00001482 case SymbolBody::UndefinedKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001483 case SymbolBody::LazyArchiveKind:
1484 case SymbolBody::LazyObjectKind:
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001485 break;
1486 case SymbolBody::DefinedBitcodeKind:
Davide Italianof6523ae2016-03-29 02:20:10 +00001487 llvm_unreachable("should have been replaced");
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001488 }
1489 return nullptr;
1490}
1491
1492template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001493VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001494 : OutputSectionBase<ELFT>(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001495 this->Header.sh_addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001496}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001497
1498template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1499 this->Header.sh_size =
1500 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1);
1501 this->Header.sh_entsize = sizeof(Elf_Versym);
1502}
1503
1504template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1505 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1506 for (const std::pair<SymbolBody *, size_t> &P :
1507 Out<ELFT>::DynSymTab->getSymbols()) {
1508 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(P.first))
1509 OutVersym->vs_index = SS->VersionId;
1510 else
1511 // The reserved identifier for a non-versioned global symbol.
1512 OutVersym->vs_index = 1;
1513 ++OutVersym;
1514 }
1515}
1516
1517template <class ELFT>
1518VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001519 : OutputSectionBase<ELFT>(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001520 this->Header.sh_addralign = sizeof(uint32_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001521}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001522
1523template <class ELFT>
1524void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1525 if (!SS->Verdef) {
1526 // The reserved identifier for a non-versioned global symbol.
1527 SS->VersionId = 1;
1528 return;
1529 }
1530 SharedFile<ELFT> *F = SS->File;
1531 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1532 // to create one by adding it to our needed list and creating a dynstr entry
1533 // for the soname.
1534 if (F->VerdefMap.empty())
1535 Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())});
1536 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1537 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1538 // prepare to create one by allocating a version identifier and creating a
1539 // dynstr entry for the version name.
1540 if (NV.Index == 0) {
1541 NV.StrTab = Out<ELFT>::DynStrTab->addString(
1542 SS->File->getStringTable().data() + SS->Verdef->getAux()->vda_name);
1543 NV.Index = NextIndex++;
1544 }
1545 SS->VersionId = NV.Index;
1546}
1547
1548template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1549 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1550 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1551 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1552
1553 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1554 // Create an Elf_Verneed for this DSO.
1555 Verneed->vn_version = 1;
1556 Verneed->vn_cnt = P.first->VerdefMap.size();
1557 Verneed->vn_file = P.second;
1558 Verneed->vn_aux =
1559 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1560 Verneed->vn_next = sizeof(Elf_Verneed);
1561 ++Verneed;
1562
1563 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1564 // VerdefMap, which will only contain references to needed version
1565 // definitions. Each Elf_Vernaux is based on the information contained in
1566 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1567 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1568 // data structures within a single input file.
1569 for (auto &NV : P.first->VerdefMap) {
1570 Vernaux->vna_hash = NV.first->vd_hash;
1571 Vernaux->vna_flags = 0;
1572 Vernaux->vna_other = NV.second.Index;
1573 Vernaux->vna_name = NV.second.StrTab;
1574 Vernaux->vna_next = sizeof(Elf_Vernaux);
1575 ++Vernaux;
1576 }
1577
1578 Vernaux[-1].vna_next = 0;
1579 }
1580 Verneed[-1].vn_next = 0;
1581}
1582
1583template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1584 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
1585 this->Header.sh_info = Needed.size();
1586 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1587 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1588 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1589 this->Header.sh_size = Size;
1590}
1591
1592template <class ELFT>
Rui Ueyama3a41be22016-04-07 22:49:21 +00001593BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
1594 : OutputSectionBase<ELFT>(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC),
1595 HashSize(HashSize) {
1596 // 16 bytes for the note section header.
1597 this->Header.sh_size = 16 + HashSize;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001598}
1599
1600template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
1601 const endianness E = ELFT::TargetEndianness;
1602 write32<E>(Buf, 4); // Name size
Rui Ueyama3a41be22016-04-07 22:49:21 +00001603 write32<E>(Buf + 4, HashSize); // Content size
Rui Ueyama634ddf02016-03-11 20:51:53 +00001604 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
1605 memcpy(Buf + 12, "GNU", 4); // Name string
1606 HashBuf = Buf + 16;
1607}
1608
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001609template <class ELFT>
1610void BuildIdFnv1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
Rui Ueyama634ddf02016-03-11 20:51:53 +00001611 const endianness E = ELFT::TargetEndianness;
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001612
1613 // 64-bit FNV-1 hash
1614 uint64_t Hash = 0xcbf29ce484222325;
1615 for (ArrayRef<uint8_t> Buf : Bufs) {
1616 for (uint8_t B : Buf) {
1617 Hash *= 0x100000001b3;
1618 Hash ^= B;
1619 }
1620 }
Rui Ueyama3a41be22016-04-07 22:49:21 +00001621 write64<E>(this->HashBuf, Hash);
1622}
1623
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001624template <class ELFT>
1625void BuildIdMd5<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
1626 llvm::MD5 Hash;
1627 for (ArrayRef<uint8_t> Buf : Bufs)
1628 Hash.update(Buf);
Rui Ueyama3a41be22016-04-07 22:49:21 +00001629 MD5::MD5Result Res;
1630 Hash.final(Res);
1631 memcpy(this->HashBuf, Res, 16);
Rui Ueyama634ddf02016-03-11 20:51:53 +00001632}
1633
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001634template <class ELFT>
1635void BuildIdSha1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
1636 llvm::SHA1 Hash;
1637 for (ArrayRef<uint8_t> Buf : Bufs)
1638 Hash.update(Buf);
Rui Ueyamad86ec302016-04-07 23:51:56 +00001639 memcpy(this->HashBuf, Hash.final().data(), 20);
1640}
1641
Rui Ueyama634ddf02016-03-11 20:51:53 +00001642template <class ELFT>
Rui Ueyama9194db72016-05-13 21:55:56 +00001643BuildIdHexstring<ELFT>::BuildIdHexstring()
1644 : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
1645
1646template <class ELFT>
1647void BuildIdHexstring<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
1648 memcpy(this->HashBuf, Config->BuildIdVector.data(),
1649 Config->BuildIdVector.size());
1650}
1651
1652template <class ELFT>
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001653MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1654 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1655 this->Header.sh_addralign = 4;
1656 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1657 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1658}
1659
1660template <class ELFT>
1661void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1662 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
Simon Atanasyan4ee29182016-04-27 05:31:28 +00001663 R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
Rui Ueyama70eed362016-01-06 22:42:43 +00001664 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001665}
1666
1667template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001668void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001669 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001670 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001671 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001672}
1673
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001674template <class ELFT>
1675MipsOptionsOutputSection<ELFT>::MipsOptionsOutputSection()
1676 : OutputSectionBase<ELFT>(".MIPS.options", SHT_MIPS_OPTIONS,
1677 SHF_ALLOC | SHF_MIPS_NOSTRIP) {
1678 this->Header.sh_addralign = 8;
1679 this->Header.sh_entsize = 1;
1680 this->Header.sh_size = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
1681}
1682
1683template <class ELFT>
1684void MipsOptionsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1685 auto *Opt = reinterpret_cast<Elf_Mips_Options *>(Buf);
1686 Opt->kind = ODK_REGINFO;
1687 Opt->size = this->Header.sh_size;
1688 Opt->section = 0;
1689 Opt->info = 0;
1690 auto *Reg = reinterpret_cast<Elf_Mips_RegInfo *>(Buf + sizeof(*Opt));
1691 Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
1692 Reg->ri_gprmask = GprMask;
1693}
1694
1695template <class ELFT>
1696void MipsOptionsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1697 auto *S = cast<MipsOptionsInputSection<ELFT>>(C);
1698 if (S->Reginfo)
1699 GprMask |= S->Reginfo->ri_gprmask;
1700}
1701
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001702namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001703namespace elf {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001704template class OutputSectionBase<ELF32LE>;
1705template class OutputSectionBase<ELF32BE>;
1706template class OutputSectionBase<ELF64LE>;
1707template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001708
George Rimarf6bc65a2016-01-15 13:34:52 +00001709template class EhFrameHeader<ELF32LE>;
1710template class EhFrameHeader<ELF32BE>;
1711template class EhFrameHeader<ELF64LE>;
1712template class EhFrameHeader<ELF64BE>;
1713
George Rimar648a2c32015-10-20 08:54:27 +00001714template class GotPltSection<ELF32LE>;
1715template class GotPltSection<ELF32BE>;
1716template class GotPltSection<ELF64LE>;
1717template class GotPltSection<ELF64BE>;
1718
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001719template class GotSection<ELF32LE>;
1720template class GotSection<ELF32BE>;
1721template class GotSection<ELF64LE>;
1722template class GotSection<ELF64BE>;
1723
1724template class PltSection<ELF32LE>;
1725template class PltSection<ELF32BE>;
1726template class PltSection<ELF64LE>;
1727template class PltSection<ELF64BE>;
1728
1729template class RelocationSection<ELF32LE>;
1730template class RelocationSection<ELF32BE>;
1731template class RelocationSection<ELF64LE>;
1732template class RelocationSection<ELF64BE>;
1733
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001734template class InterpSection<ELF32LE>;
1735template class InterpSection<ELF32BE>;
1736template class InterpSection<ELF64LE>;
1737template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001738
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001739template class GnuHashTableSection<ELF32LE>;
1740template class GnuHashTableSection<ELF32BE>;
1741template class GnuHashTableSection<ELF64LE>;
1742template class GnuHashTableSection<ELF64BE>;
1743
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001744template class HashTableSection<ELF32LE>;
1745template class HashTableSection<ELF32BE>;
1746template class HashTableSection<ELF64LE>;
1747template class HashTableSection<ELF64BE>;
1748
1749template class DynamicSection<ELF32LE>;
1750template class DynamicSection<ELF32BE>;
1751template class DynamicSection<ELF64LE>;
1752template class DynamicSection<ELF64BE>;
1753
1754template class OutputSection<ELF32LE>;
1755template class OutputSection<ELF32BE>;
1756template class OutputSection<ELF64LE>;
1757template class OutputSection<ELF64BE>;
1758
Rui Ueyama1e479c22016-05-23 15:07:59 +00001759template class EhOutputSection<ELF32LE>;
1760template class EhOutputSection<ELF32BE>;
1761template class EhOutputSection<ELF64LE>;
1762template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001763
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001764template class MipsReginfoOutputSection<ELF32LE>;
1765template class MipsReginfoOutputSection<ELF32BE>;
1766template class MipsReginfoOutputSection<ELF64LE>;
1767template class MipsReginfoOutputSection<ELF64BE>;
1768
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001769template class MipsOptionsOutputSection<ELF32LE>;
1770template class MipsOptionsOutputSection<ELF32BE>;
1771template class MipsOptionsOutputSection<ELF64LE>;
1772template class MipsOptionsOutputSection<ELF64BE>;
1773
Rafael Espindolac159c962015-10-19 21:00:02 +00001774template class MergeOutputSection<ELF32LE>;
1775template class MergeOutputSection<ELF32BE>;
1776template class MergeOutputSection<ELF64LE>;
1777template class MergeOutputSection<ELF64BE>;
1778
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001779template class StringTableSection<ELF32LE>;
1780template class StringTableSection<ELF32BE>;
1781template class StringTableSection<ELF64LE>;
1782template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001783
1784template class SymbolTableSection<ELF32LE>;
1785template class SymbolTableSection<ELF32BE>;
1786template class SymbolTableSection<ELF64LE>;
1787template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001788
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001789template class VersionTableSection<ELF32LE>;
1790template class VersionTableSection<ELF32BE>;
1791template class VersionTableSection<ELF64LE>;
1792template class VersionTableSection<ELF64BE>;
1793
1794template class VersionNeedSection<ELF32LE>;
1795template class VersionNeedSection<ELF32BE>;
1796template class VersionNeedSection<ELF64LE>;
1797template class VersionNeedSection<ELF64BE>;
1798
Rui Ueyama634ddf02016-03-11 20:51:53 +00001799template class BuildIdSection<ELF32LE>;
1800template class BuildIdSection<ELF32BE>;
1801template class BuildIdSection<ELF64LE>;
1802template class BuildIdSection<ELF64BE>;
Rui Ueyama3a41be22016-04-07 22:49:21 +00001803
1804template class BuildIdFnv1<ELF32LE>;
1805template class BuildIdFnv1<ELF32BE>;
1806template class BuildIdFnv1<ELF64LE>;
1807template class BuildIdFnv1<ELF64BE>;
1808
1809template class BuildIdMd5<ELF32LE>;
1810template class BuildIdMd5<ELF32BE>;
1811template class BuildIdMd5<ELF64LE>;
1812template class BuildIdMd5<ELF64BE>;
Rui Ueyamad86ec302016-04-07 23:51:56 +00001813
1814template class BuildIdSha1<ELF32LE>;
1815template class BuildIdSha1<ELF32BE>;
1816template class BuildIdSha1<ELF64LE>;
1817template class BuildIdSha1<ELF64BE>;
Rui Ueyama9194db72016-05-13 21:55:56 +00001818
1819template class BuildIdHexstring<ELF32LE>;
1820template class BuildIdHexstring<ELF32BE>;
1821template class BuildIdHexstring<ELF64LE>;
1822template class BuildIdHexstring<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001823}
1824}