blob: 10d08c6c50ded8769330fdac07450cf00242c2af [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"
Rui Ueyamaf5febef2016-05-24 02:55:45 +000012#include "EhFrame.h"
George Rimare2ee72b2016-02-26 14:48:31 +000013#include "LinkerScript.h"
Rui Ueyamafbbde542016-06-29 09:08:02 +000014#include "Strings.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000016#include "Target.h"
Rui Ueyamae9809502016-03-11 04:23:12 +000017#include "lld/Core/Parallel.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000018#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000019#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000020#include "llvm/Support/MathExtras.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000021#include "llvm/Support/SHA1.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000022#include <map>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000023
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000025using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000027using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028using namespace llvm::ELF;
29
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000032
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000033template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000034OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
35 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000036 : Name(Name) {
Sean Silva580c1b62016-04-20 04:26:16 +000037 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000038 Header.sh_type = Type;
39 Header.sh_flags = Flags;
Rui Ueyama3b04d832016-07-14 05:46:24 +000040 Header.sh_addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041}
42
Rafael Espindola0b113672016-07-27 14:10:56 +000043template <class ELFT> uint32_t OutputSectionBase<ELFT>::getPhdrFlags() const {
44 uintX_t Flags = getFlags();
45 uint32_t Ret = PF_R;
46 if (Flags & SHF_WRITE)
47 Ret |= PF_W;
48 if (Flags & SHF_EXECINSTR)
49 Ret |= PF_X;
50 return Ret;
51}
52
Rafael Espindola35c6af32015-09-25 17:19:10 +000053template <class ELFT>
Rui Ueyamac63c1db2016-03-13 06:50:33 +000054void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *Shdr) {
55 *Shdr = Header;
56}
57
58template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000059GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000060 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Rui Ueyama803b1202016-07-13 18:55:14 +000061 this->Header.sh_addralign = Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +000062}
63
Rafael Espindola67d72c02016-03-11 12:06:30 +000064template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
65 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
66 Entries.push_back(&Sym);
George Rimar648a2c32015-10-20 08:54:27 +000067}
68
69template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000070 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000071}
72
George Rimar648a2c32015-10-20 08:54:27 +000073template <class ELFT> void GotPltSection<ELFT>::finalize() {
Rui Ueyama803b1202016-07-13 18:55:14 +000074 this->Header.sh_size = (Target->GotPltHeaderEntriesNum + Entries.size()) *
75 Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +000076}
77
78template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000079 Target->writeGotPltHeader(Buf);
Rui Ueyama803b1202016-07-13 18:55:14 +000080 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +000081 for (const SymbolBody *B : Entries) {
Rui Ueyamac9fee5f2016-06-16 16:14:50 +000082 Target->writeGotPlt(Buf, *B);
George Rimar648a2c32015-10-20 08:54:27 +000083 Buf += sizeof(uintX_t);
84 }
85}
86
87template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000088GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000089 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000090 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000091 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama803b1202016-07-13 18:55:14 +000092 this->Header.sh_addralign = Target->GotEntrySize;
Rafael Espindola35c6af32015-09-25 17:19:10 +000093}
94
Simon Atanasyan41325112016-06-19 21:39:37 +000095template <class ELFT>
96void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Rafael Espindola67d72c02016-03-11 12:06:30 +000097 Sym.GotIndex = Entries.size();
98 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +000099}
100
Simon Atanasyan41325112016-06-19 21:39:37 +0000101template <class ELFT>
102void GotSection<ELFT>::addMipsEntry(SymbolBody &Sym, uintX_t Addend,
103 RelExpr Expr) {
104 // For "true" local symbols which can be referenced from the same module
105 // only compiler creates two instructions for address loading:
106 //
107 // lw $8, 0($gp) # R_MIPS_GOT16
108 // addi $8, $8, 0 # R_MIPS_LO16
109 //
110 // The first instruction loads high 16 bits of the symbol address while
111 // the second adds an offset. That allows to reduce number of required
112 // GOT entries because only one global offset table entry is necessary
113 // for every 64 KBytes of local data. So for local symbols we need to
114 // allocate number of GOT entries to hold all required "page" addresses.
115 //
116 // All global symbols (hidden and regular) considered by compiler uniformly.
117 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
118 // to load address of the symbol. So for each such symbol we need to
119 // allocate dedicated GOT entry to store its address.
120 //
121 // If a symbol is preemptible we need help of dynamic linker to get its
122 // final address. The corresponding GOT entries are allocated in the
123 // "global" part of GOT. Entries for non preemptible global symbol allocated
124 // in the "local" part of GOT.
125 //
126 // See "Global Offset Table" in Chapter 5:
127 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
128 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
129 // At this point we do not know final symbol value so to reduce number
130 // of allocated GOT entries do the following trick. Save all output
131 // sections referenced by GOT relocations. Then later in the `finalize`
132 // method calculate number of "pages" required to cover all saved output
133 // section and allocate appropriate number of GOT entries.
134 auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec;
135 MipsOutSections.insert(OutSec);
136 return;
137 }
Simon Atanasyan002e2442016-06-23 15:26:31 +0000138 if (Sym.isTls()) {
139 // GOT entries created for MIPS TLS relocations behave like
140 // almost GOT entries from other ABIs. They go to the end
141 // of the global offset table.
142 Sym.GotIndex = Entries.size();
143 Entries.push_back(&Sym);
144 return;
145 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000146 auto AddEntry = [&](SymbolBody &S, uintX_t A, MipsGotEntries &Items) {
147 if (S.isInGot() && !A)
148 return;
149 size_t NewIndex = Items.size();
150 if (!MipsGotMap.insert({{&S, A}, NewIndex}).second)
151 return;
152 Items.emplace_back(&S, A);
153 if (!A)
154 S.GotIndex = NewIndex;
155 };
156 if (Sym.isPreemptible()) {
157 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
158 AddEntry(Sym, 0, MipsGlobal);
159 Sym.IsInGlobalMipsGot = true;
160 } else
161 AddEntry(Sym, Addend, MipsLocal);
162}
163
Rafael Espindola67d72c02016-03-11 12:06:30 +0000164template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000165 if (Sym.GlobalDynIndex != -1U)
George Rimar90cd0a82015-12-01 19:20:26 +0000166 return false;
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000167 Sym.GlobalDynIndex = Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +0000168 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +0000169 Entries.push_back(nullptr);
Rafael Espindolaa8777c22016-06-08 21:31:59 +0000170 Entries.push_back(&Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000171 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172}
173
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000174// Reserves TLS entries for a TLS module ID and a TLS block offset.
175// In total it takes two GOT slots.
176template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
177 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +0000178 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000179 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000180 Entries.push_back(nullptr);
181 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000182 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000183}
184
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000185template <class ELFT>
186typename GotSection<ELFT>::uintX_t
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000187GotSection<ELFT>::getMipsLocalPageOffset(uintX_t EntryValue) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000188 // Initialize the entry by the %hi(EntryValue) expression
189 // but without right-shifting.
Simon Atanasyan41325112016-06-19 21:39:37 +0000190 EntryValue = (EntryValue + 0x8000) & ~0xffff;
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000191 // Take into account MIPS GOT header.
192 // See comment in the GotSection::writeTo.
193 size_t NewIndex = MipsLocalGotPos.size() + 2;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000194 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
Simon Atanasyan41325112016-06-19 21:39:37 +0000195 assert(!P.second || MipsLocalGotPos.size() <= MipsPageEntries);
George Rimar71e64b22016-05-11 09:41:15 +0000196 return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197}
198
Igor Kudrin304860a2015-11-12 04:39:49 +0000199template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000200typename GotSection<ELFT>::uintX_t
Simon Atanasyan41325112016-06-19 21:39:37 +0000201GotSection<ELFT>::getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const {
202 uintX_t Off = MipsPageEntries;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000203 if (B.isTls())
204 Off += MipsLocal.size() + MipsGlobal.size() + B.GotIndex;
205 else if (B.IsInGlobalMipsGot)
Simon Atanasyan41325112016-06-19 21:39:37 +0000206 Off += MipsLocal.size() + B.GotIndex;
207 else if (B.isInGot())
208 Off += B.GotIndex;
209 else {
210 auto It = MipsGotMap.find({&B, Addend});
211 assert(It != MipsGotMap.end());
212 Off += It->second;
213 }
214 return Off * sizeof(uintX_t) - MipsGPOffset;
215}
216
217template <class ELFT>
Simon Atanasyan002e2442016-06-23 15:26:31 +0000218typename GotSection<ELFT>::uintX_t GotSection<ELFT>::getMipsTlsOffset() {
219 return (MipsPageEntries + MipsLocal.size() + MipsGlobal.size()) *
220 sizeof(uintX_t);
221}
222
223template <class ELFT>
Simon Atanasyan41325112016-06-19 21:39:37 +0000224typename GotSection<ELFT>::uintX_t
George Rimar90cd0a82015-12-01 19:20:26 +0000225GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000226 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
George Rimar90cd0a82015-12-01 19:20:26 +0000227}
228
229template <class ELFT>
Rafael Espindola74031ba2016-04-07 15:20:56 +0000230typename GotSection<ELFT>::uintX_t
231GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000232 return B.GlobalDynIndex * sizeof(uintX_t);
Rafael Espindola74031ba2016-04-07 15:20:56 +0000233}
234
235template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000236const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
Simon Atanasyan41325112016-06-19 21:39:37 +0000237 return MipsGlobal.empty() ? nullptr : MipsGlobal.front().first;
Igor Kudrin304860a2015-11-12 04:39:49 +0000238}
239
240template <class ELFT>
241unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Simon Atanasyan41325112016-06-19 21:39:37 +0000242 return MipsPageEntries + MipsLocal.size();
Igor Kudrin304860a2015-11-12 04:39:49 +0000243}
244
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000245template <class ELFT> void GotSection<ELFT>::finalize() {
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000246 size_t EntriesNum = Entries.size();
247 if (Config->EMachine == EM_MIPS) {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000248 // Take into account MIPS GOT header.
249 // See comment in the GotSection::writeTo.
Simon Atanasyan41325112016-06-19 21:39:37 +0000250 MipsPageEntries += 2;
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000251 for (const OutputSectionBase<ELFT> *OutSec : MipsOutSections) {
252 // Calculate an upper bound of MIPS GOT entries required to store page
253 // addresses of local symbols. We assume the worst case - each 64kb
254 // page of the output section has at least one GOT relocation against it.
255 // Add 0x8000 to the section's size because the page address stored
256 // in the GOT entry is calculated as (value + 0x8000) & ~0xffff.
Simon Atanasyan41325112016-06-19 21:39:37 +0000257 MipsPageEntries += (OutSec->getSize() + 0x8000 + 0xfffe) / 0xffff;
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000258 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000259 EntriesNum += MipsPageEntries + MipsLocal.size() + MipsGlobal.size();
Simon Atanasyand2980d32016-03-29 14:07:22 +0000260 }
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000261 this->Header.sh_size = EntriesNum * sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000262}
263
Simon Atanasyan41325112016-06-19 21:39:37 +0000264template <class ELFT> void GotSection<ELFT>::writeMipsGot(uint8_t *&Buf) {
265 // Set the MSB of the second GOT slot. This is not required by any
266 // MIPS ABI documentation, though.
267 //
268 // There is a comment in glibc saying that "The MSB of got[1] of a
269 // gnu object is set to identify gnu objects," and in GNU gold it
270 // says "the second entry will be used by some runtime loaders".
271 // But how this field is being used is unclear.
272 //
273 // We are not really willing to mimic other linkers behaviors
274 // without understanding why they do that, but because all files
275 // generated by GNU tools have this special GOT value, and because
276 // we've been doing this for years, it is probably a safe bet to
277 // keep doing this for now. We really need to revisit this to see
278 // if we had to do this.
279 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
280 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
281 // Write 'page address' entries to the local part of the GOT.
282 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
283 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
284 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000285 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000286 Buf += MipsPageEntries * sizeof(uintX_t);
287 auto AddEntry = [&](const MipsGotEntry &SA) {
288 uint8_t *Entry = Buf;
289 Buf += sizeof(uintX_t);
George Rimar06e930d2016-06-20 10:01:50 +0000290 const SymbolBody* Body = SA.first;
291 uintX_t VA = Body->template getVA<ELFT>(SA.second);
Simon Atanasyan41325112016-06-19 21:39:37 +0000292 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
293 };
294 std::for_each(std::begin(MipsLocal), std::end(MipsLocal), AddEntry);
295 std::for_each(std::begin(MipsGlobal), std::end(MipsGlobal), AddEntry);
296}
297
298template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
299 if (Config->EMachine == EM_MIPS)
300 writeMipsGot(Buf);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000301 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000302 uint8_t *Entry = Buf;
303 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000304 if (!B)
305 continue;
Simon Atanasyan41325112016-06-19 21:39:37 +0000306 if (B->isPreemptible())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000307 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000308 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000309 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000310 }
311}
312
Rafael Espindola35c6af32015-09-25 17:19:10 +0000313template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000314PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000315 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000316 this->Header.sh_addralign = 16;
317}
318
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000319template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000320 // At beginning of PLT, we have code to call the dynamic linker
321 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama4a90f572016-06-16 16:28:50 +0000322 Target->writePltHeader(Buf);
323 size_t Off = Target->PltHeaderSize;
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000324
George Rimarfb5d7f22015-11-26 19:58:51 +0000325 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000326 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000327 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000328 uint64_t Got = B->getGotPltVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000329 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000330 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000331 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000332 }
333}
334
Rafael Espindola67d72c02016-03-11 12:06:30 +0000335template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
336 Sym.PltIndex = Entries.size();
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000337 unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000338 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339}
340
George Rimar648a2c32015-10-20 08:54:27 +0000341template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000342 this->Header.sh_size =
Rui Ueyama4a90f572016-06-16 16:28:50 +0000343 Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000344}
345
346template <class ELFT>
George Rimarc191acf2016-05-10 15:47:57 +0000347RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000348 : OutputSectionBase<ELFT>(Name, Config->Rela ? SHT_RELA : SHT_REL,
George Rimarc191acf2016-05-10 15:47:57 +0000349 SHF_ALLOC),
350 Sort(Sort) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000351 this->Header.sh_entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000352 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000353}
354
George Rimar5828c232015-11-30 17:49:19 +0000355template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000356void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000357 Relocs.push_back(Reloc);
358}
359
George Rimarc191acf2016-05-10 15:47:57 +0000360template <class ELFT, class RelTy>
361static bool compRelocations(const RelTy &A, const RelTy &B) {
362 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
363}
364
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarc191acf2016-05-10 15:47:57 +0000366 uint8_t *BufBegin = Buf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000367 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rui Ueyama614be592016-03-13 05:23:40 +0000368 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000369 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000370
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000371 if (Config->Rela)
Rui Ueyama809d8e22016-06-23 04:33:42 +0000372 P->r_addend = Rel.getAddend();
373 P->r_offset = Rel.getOffset();
Simon Atanasyan002e2442016-06-23 15:26:31 +0000374 if (Config->EMachine == EM_MIPS && Rel.getOutputSec() == Out<ELFT>::Got)
375 // Dynamic relocation against MIPS GOT section make deal TLS entries
376 // allocated in the end of the GOT. We need to adjust the offset to take
377 // in account 'local' and 'global' GOT entries.
378 P->r_offset += Out<ELFT>::Got->getMipsTlsOffset();
Rui Ueyama809d8e22016-06-23 04:33:42 +0000379 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000380 }
George Rimarc191acf2016-05-10 15:47:57 +0000381
382 if (Sort) {
383 if (Config->Rela)
384 std::stable_sort((Elf_Rela *)BufBegin,
385 (Elf_Rela *)BufBegin + Relocs.size(),
386 compRelocations<ELFT, Elf_Rela>);
387 else
388 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
389 compRelocations<ELFT, Elf_Rel>);
390 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391}
392
George Rimar77b77792015-11-25 22:15:01 +0000393template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000394 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000395}
396
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000397template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyamacbd434a2016-08-09 04:31:21 +0000398 this->Header.sh_link = isOutputDynamic<ELFT>()
399 ? Out<ELFT>::DynSymTab->SectionIndex
400 : Out<ELFT>::SymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000401 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
402}
403
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000404template <class ELFT>
405InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000406 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000407 this->Header.sh_size = Config->DynamicLinker.size() + 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408}
409
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000410template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama1e720b92016-03-13 06:50:34 +0000411 StringRef S = Config->DynamicLinker;
412 memcpy(Buf, S.data(), S.size());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000413}
414
Rafael Espindola35c6af32015-09-25 17:19:10 +0000415template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000416HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000417 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000418 this->Header.sh_entsize = sizeof(Elf_Word);
419 this->Header.sh_addralign = sizeof(Elf_Word);
420}
421
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000422static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000423 uint32_t H = 0;
424 for (char C : Name) {
425 H = (H << 4) + C;
426 uint32_t G = H & 0xf0000000;
427 if (G)
428 H ^= G >> 24;
429 H &= ~G;
430 }
431 return H;
432}
433
Rui Ueyama0db335f2015-10-07 16:58:54 +0000434template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000435 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000436
George Rimare9e1d322016-02-18 15:17:01 +0000437 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000438 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000439
440 // Create as many buckets as there are symbols.
441 // FIXME: This is simplistic. We can try to optimize it, but implementing
442 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000443 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000444 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
445}
446
447template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000448 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000449 auto *P = reinterpret_cast<Elf_Word *>(Buf);
450 *P++ = NumSymbols; // nbucket
451 *P++ = NumSymbols; // nchain
452
453 Elf_Word *Buckets = P;
454 Elf_Word *Chains = P + NumSymbols;
455
Rafael Espindolae2c24612016-01-29 01:24:25 +0000456 for (const std::pair<SymbolBody *, unsigned> &P :
457 Out<ELFT>::DynSymTab->getSymbols()) {
458 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000459 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000460 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000461 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000462 Chains[I] = Buckets[Hash];
463 Buckets[Hash] = I;
464 }
465}
466
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000467static uint32_t hashGnu(StringRef Name) {
468 uint32_t H = 5381;
469 for (uint8_t C : Name)
470 H = (H << 5) + H + C;
471 return H;
472}
473
474template <class ELFT>
475GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000476 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000477 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000478 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000479}
480
481template <class ELFT>
482unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
483 if (!NumHashed)
484 return 0;
485
486 // These values are prime numbers which are not greater than 2^(N-1) + 1.
487 // In result, for any particular NumHashed we return a prime number
488 // which is not greater than NumHashed.
489 static const unsigned Primes[] = {
490 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
491 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
492
493 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
494 array_lengthof(Primes) - 1)];
495}
496
497// Bloom filter estimation: at least 8 bits for each hashed symbol.
498// GNU Hash table requirement: it should be a power of 2,
499// the minimum value is 1, even for an empty table.
500// Expected results for a 32-bit target:
501// calcMaskWords(0..4) = 1
502// calcMaskWords(5..8) = 2
503// calcMaskWords(9..16) = 4
504// For a 64-bit target:
505// calcMaskWords(0..8) = 1
506// calcMaskWords(9..16) = 2
507// calcMaskWords(17..32) = 4
508template <class ELFT>
509unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
510 if (!NumHashed)
511 return 1;
512 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
513}
514
515template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000516 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000517 NBuckets = calcNBuckets(NumHashed);
518 MaskWords = calcMaskWords(NumHashed);
519 // Second hash shift estimation: just predefined values.
520 Shift2 = ELFT::Is64Bits ? 6 : 5;
521
522 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
523 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
524 + sizeof(Elf_Off) * MaskWords // Bloom Filter
525 + sizeof(Elf_Word) * NBuckets // Hash Buckets
526 + sizeof(Elf_Word) * NumHashed; // Hash Values
527}
528
529template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
530 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000531 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000532 return;
533 writeBloomFilter(Buf);
534 writeHashTable(Buf);
535}
536
537template <class ELFT>
538void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
539 auto *P = reinterpret_cast<Elf_Word *>(Buf);
540 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000541 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000542 *P++ = MaskWords;
543 *P++ = Shift2;
544 Buf = reinterpret_cast<uint8_t *>(P);
545}
546
547template <class ELFT>
548void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
549 unsigned C = sizeof(Elf_Off) * 8;
550
551 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000552 for (const SymbolData &Sym : Symbols) {
553 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
554 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
555 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000556 Masks[Pos] |= V;
557 }
558 Buf += sizeof(Elf_Off) * MaskWords;
559}
560
561template <class ELFT>
562void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
563 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
564 Elf_Word *Values = Buckets + NBuckets;
565
566 int PrevBucket = -1;
567 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000568 for (const SymbolData &Sym : Symbols) {
569 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000570 assert(PrevBucket <= Bucket);
571 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000572 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000573 PrevBucket = Bucket;
574 if (I > 0)
575 Values[I - 1] |= 1;
576 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000577 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000578 ++I;
579 }
580 if (I > 0)
581 Values[I - 1] |= 1;
582}
583
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000584// Add symbols to this symbol hash table. Note that this function
585// destructively sort a given vector -- which is needed because
586// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000587template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000588void GnuHashTableSection<ELFT>::addSymbols(
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000589 std::vector<std::pair<SymbolBody *, size_t>> &V) {
Davide Italianob4b68b62016-07-04 19:49:55 +0000590 // Ideally this will just be 'auto' but GCC 6.1 is not able
591 // to deduce it correctly.
592 std::vector<std::pair<SymbolBody *, size_t>>::iterator Mid =
593 std::stable_partition(V.begin(), V.end(),
594 [](std::pair<SymbolBody *, size_t> &P) {
595 return P.first->isUndefined();
596 });
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000597 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000598 return;
Davide Italianof8591cf2016-07-06 17:41:55 +0000599 for (auto I = Mid, E = V.end(); I != E; ++I) {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000600 SymbolBody *B = I->first;
601 size_t StrOff = I->second;
602 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
603 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000604
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000605 unsigned NBuckets = calcNBuckets(Symbols.size());
606 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000607 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000608 return L.Hash % NBuckets < R.Hash % NBuckets;
609 });
610
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000611 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000612 for (const SymbolData &Sym : Symbols)
613 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000614}
615
George Rimard3566302016-06-20 11:55:12 +0000616// Returns the number of version definition entries. Because the first entry
617// is for the version definition itself, it is the number of versioned symbols
618// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000619static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000620
Igor Kudrinf1d60292015-10-28 07:05:56 +0000621template <class ELFT>
Rui Ueyamaace4f902016-05-24 04:25:47 +0000622DynamicSection<ELFT>::DynamicSection()
623 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000624 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000625 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000626 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000627
628 // .dynamic section is not writable on MIPS.
629 // See "Special Section" in Chapter 4 in the following document:
630 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
631 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000632 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000633}
634
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000635template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000636 if (this->Header.sh_size)
637 return; // Already finalized.
638
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000639 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000640 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000641
Rafael Espindolae2c24612016-01-29 01:24:25 +0000642 auto Add = [=](Entry E) { Entries.push_back(E); };
643
644 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000645 // DynStrTab and doing this here allows this function to set DT_STRSZ.
646 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000647 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
648 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rui Ueyamaace4f902016-05-24 04:25:47 +0000649 for (const std::unique_ptr<SharedFile<ELFT>> &F :
650 Symtab<ELFT>::X->getSharedFiles())
Rafael Espindolade069362016-01-25 21:32:04 +0000651 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000652 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
653 if (!Config->SoName.empty())
654 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
655
Rafael Espindolade069362016-01-25 21:32:04 +0000656 Out<ELFT>::DynStrTab->finalize();
657
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000658 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000659 bool IsRela = Config->Rela;
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000660 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
661 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
662 Add({IsRela ? DT_RELAENT : DT_RELENT,
663 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000664 }
George Rimar648a2c32015-10-20 08:54:27 +0000665 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000666 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
667 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
668 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
669 Out<ELFT>::GotPlt});
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000670 Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000671 }
672
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000673 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
674 Add({DT_SYMENT, sizeof(Elf_Sym)});
675 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
676 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000677 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000678 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000679 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000680 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000681
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000682 if (Out<ELFT>::PreinitArray) {
683 Add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
684 Add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000685 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000686 if (Out<ELFT>::InitArray) {
687 Add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
688 Add({DT_INIT_ARRAYSZ, (uintX_t)Out<ELFT>::InitArray->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000689 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000690 if (Out<ELFT>::FiniArray) {
691 Add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
692 Add({DT_FINI_ARRAYSZ, (uintX_t)Out<ELFT>::FiniArray->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000693 }
694
Rui Ueyamaace4f902016-05-24 04:25:47 +0000695 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000696 Add({DT_INIT, B});
Rui Ueyamaace4f902016-05-24 04:25:47 +0000697 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000698 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000699
Rafael Espindolade069362016-01-25 21:32:04 +0000700 uint32_t DtFlags = 0;
701 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000702 if (Config->Bsymbolic)
703 DtFlags |= DF_SYMBOLIC;
704 if (Config->ZNodelete)
705 DtFlags1 |= DF_1_NODELETE;
706 if (Config->ZNow) {
707 DtFlags |= DF_BIND_NOW;
708 DtFlags1 |= DF_1_NOW;
709 }
710 if (Config->ZOrigin) {
711 DtFlags |= DF_ORIGIN;
712 DtFlags1 |= DF_1_ORIGIN;
713 }
714
715 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000716 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000717 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000718 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000719
Ed Mastef5d3cf62016-01-06 15:52:27 +0000720 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000721 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000722
George Rimard3566302016-06-20 11:55:12 +0000723 bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0;
724 if (HasVerNeed || Out<ELFT>::VerDef)
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000725 Add({DT_VERSYM, Out<ELFT>::VerSym});
George Rimard3566302016-06-20 11:55:12 +0000726 if (Out<ELFT>::VerDef) {
727 Add({DT_VERDEF, Out<ELFT>::VerDef});
728 Add({DT_VERDEFNUM, getVerDefNum()});
729 }
730 if (HasVerNeed) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000731 Add({DT_VERNEED, Out<ELFT>::VerNeed});
George Rimard3566302016-06-20 11:55:12 +0000732 Add({DT_VERNEEDNUM, Out<ELFT>::VerNeed->getNeedNum()});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000733 }
734
Igor Kudrin304860a2015-11-12 04:39:49 +0000735 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000736 Add({DT_MIPS_RLD_VERSION, 1});
737 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
Rui Ueyamacafc0f2e2016-07-14 17:40:18 +0000738 Add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000739 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
740 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000741 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000742 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000743 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000744 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
745 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000746 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000747 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000748 }
749
Rafael Espindolade069362016-01-25 21:32:04 +0000750 // +1 for DT_NULL
751 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000752}
753
754template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000755 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
756
Rafael Espindolade069362016-01-25 21:32:04 +0000757 for (const Entry &E : Entries) {
758 P->d_tag = E.Tag;
759 switch (E.Kind) {
760 case Entry::SecAddr:
761 P->d_un.d_ptr = E.OutSec->getVA();
762 break;
763 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000764 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000765 break;
766 case Entry::PlainInt:
767 P->d_un.d_val = E.Val;
768 break;
769 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000770 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000771 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000772}
773
774template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000775EhFrameHeader<ELFT>::EhFrameHeader()
Rui Ueyamade9777a2016-05-23 16:30:41 +0000776 : OutputSectionBase<ELFT>(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000777
Rui Ueyama95a232e2016-05-23 01:45:05 +0000778// .eh_frame_hdr contains a binary search table of pointers to FDEs.
779// Each entry of the search table consists of two values,
780// the starting PC from where FDEs covers, and the FDE's address.
781// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000782template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
783 const endianness E = ELFT::TargetEndianness;
784
Rui Ueyamae75e9332016-05-23 03:00:33 +0000785 // Sort the FDE list by their PC and uniqueify. Usually there is only
786 // one FDE for a PC (i.e. function), but if ICF merges two functions
787 // into one, there can be more than one FDEs pointing to the address.
788 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
789 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
790 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
791 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000792
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000793 Buf[0] = 1;
794 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
795 Buf[2] = DW_EH_PE_udata4;
796 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000797 write32<E>(Buf + 4, Out<ELFT>::EhFrame->getVA() - this->getVA() - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000798 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000799 Buf += 12;
800
Rui Ueyamae75e9332016-05-23 03:00:33 +0000801 uintX_t VA = this->getVA();
802 for (FdeData &Fde : Fdes) {
803 write32<E>(Buf, Fde.Pc - VA);
804 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000805 Buf += 8;
806 }
807}
808
Rui Ueyamade9777a2016-05-23 16:30:41 +0000809template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
810 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
811 this->Header.sh_size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
812}
813
George Rimarf6bc65a2016-01-15 13:34:52 +0000814template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000815void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
816 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000817}
818
George Rimarf6bc65a2016-01-15 13:34:52 +0000819template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000820OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
821 : OutputSectionBase<ELFT>(Name, Type, Flags) {
822 if (Type == SHT_RELA)
823 this->Header.sh_entsize = sizeof(Elf_Rela);
824 else if (Type == SHT_REL)
825 this->Header.sh_entsize = sizeof(Elf_Rel);
826}
827
828template <class ELFT> void OutputSection<ELFT>::finalize() {
829 uint32_t Type = this->Header.sh_type;
830 if (Type != SHT_RELA && Type != SHT_REL)
831 return;
832 this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex;
833 // sh_info for SHT_REL[A] sections should contain the section header index of
834 // the section to which the relocation applies.
835 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
836 this->Header.sh_info = S->OutSec->SectionIndex;
837}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000838
839template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000840void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000841 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000842 auto *S = cast<InputSection<ELFT>>(C);
843 Sections.push_back(S);
844 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000845 this->updateAlignment(S->Alignment);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000846}
847
Rui Ueyamac4185702016-02-10 23:20:42 +0000848// If an input string is in the form of "foo.N" where N is a number,
849// return N. Otherwise, returns 65536, which is one greater than the
850// lowest priority.
851static int getPriority(StringRef S) {
852 size_t Pos = S.rfind('.');
853 if (Pos == StringRef::npos)
854 return 65536;
855 int V;
856 if (S.substr(Pos + 1).getAsInteger(10, V))
857 return 65536;
858 return V;
859}
860
Rui Ueyama809d8e22016-06-23 04:33:42 +0000861// This function is called after we sort input sections
862// and scan relocations to setup sections' offsets.
863template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
864 uintX_t Off = this->Header.sh_size;
865 for (InputSection<ELFT> *S : Sections) {
866 Off = alignTo(Off, S->Alignment);
867 S->OutSecOff = Off;
868 Off += S->getSize();
869 }
870 this->Header.sh_size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000871}
872
Rui Ueyamac4185702016-02-10 23:20:42 +0000873// Sorts input sections by section name suffixes, so that .foo.N comes
874// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000875// We want to keep the original order if the priorities are the same
876// because the compiler keeps the original initialization order in a
877// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000878// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000879template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000880 // Sort sections by priority.
881 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000882 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
883
Rui Ueyamac4185702016-02-10 23:20:42 +0000884 std::vector<Pair> V;
885 for (InputSection<ELFT> *S : Sections)
886 V.push_back({getPriority(S->getSectionName()), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000887 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000888 Sections.clear();
889 for (Pair &P : V)
890 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000891}
Rui Ueyamac4185702016-02-10 23:20:42 +0000892
Rui Ueyama5af83682016-02-11 23:41:38 +0000893// Returns true if S matches /Filename.?\.o$/.
894static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
895 if (!S.endswith(".o"))
896 return false;
897 S = S.drop_back(2);
898 if (S.endswith(Filename))
899 return true;
900 return !S.empty() && S.drop_back().endswith(Filename);
901}
902
903static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
904static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
905
906// .ctors and .dtors are sorted by this priority from highest to lowest.
907//
908// 1. The section was contained in crtbegin (crtbegin contains
909// some sentinel value in its .ctors and .dtors so that the runtime
910// can find the beginning of the sections.)
911//
912// 2. The section has an optional priority value in the form of ".ctors.N"
913// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
914// they are compared as string rather than number.
915//
916// 3. The section is just ".ctors" or ".dtors".
917//
918// 4. The section was contained in crtend, which contains an end marker.
919//
920// In an ideal world, we don't need this function because .init_array and
921// .ctors are duplicate features (and .init_array is newer.) However, there
922// are too many real-world use cases of .ctors, so we had no choice to
923// support that with this rather ad-hoc semantics.
924template <class ELFT>
925static bool compCtors(const InputSection<ELFT> *A,
926 const InputSection<ELFT> *B) {
927 bool BeginA = isCrtbegin(A->getFile()->getName());
928 bool BeginB = isCrtbegin(B->getFile()->getName());
929 if (BeginA != BeginB)
930 return BeginA;
931 bool EndA = isCrtend(A->getFile()->getName());
932 bool EndB = isCrtend(B->getFile()->getName());
933 if (EndA != EndB)
934 return EndB;
935 StringRef X = A->getSectionName();
936 StringRef Y = B->getSectionName();
937 assert(X.startswith(".ctors") || X.startswith(".dtors"));
938 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
939 X = X.substr(6);
940 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000941 if (X.empty() && Y.empty())
942 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000943 return X < Y;
944}
945
946// Sorts input sections by the special rules for .ctors and .dtors.
947// Unfortunately, the rules are different from the one for .{init,fini}_array.
948// Read the comment above.
949template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
950 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000951}
952
George Rimare2ee72b2016-02-26 14:48:31 +0000953static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
954 size_t I = 0;
955 for (; I + A.size() < Size; I += A.size())
956 memcpy(Buf + I, A.data(), A.size());
957 memcpy(Buf + I, A.data(), Size - I);
958}
959
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000960template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000961 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +0000962 if (!Filler.empty())
963 fill(Buf, this->getSize(), Filler);
Rui Ueyamae9809502016-03-11 04:23:12 +0000964 if (Config->Threads) {
965 parallel_for_each(Sections.begin(), Sections.end(),
966 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
967 } else {
968 for (InputSection<ELFT> *C : Sections)
969 C->writeTo(Buf);
970 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000971}
972
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000973template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000974EhOutputSection<ELFT>::EhOutputSection()
Rui Ueyama3b31e672016-05-23 16:24:16 +0000975 : OutputSectionBase<ELFT>(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000976
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000977// Search for an existing CIE record or create a new one.
978// CIE records from input object files are uniquified by their contents
979// and where their relocations point to.
980template <class ELFT>
981template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000982CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000983 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000984 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000985 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +0000986 if (read32<E>(Piece.data().data() + 4) != 0)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000987 fatal("CIE expected at beginning of .eh_frame: " + Sec->getSectionName());
988
989 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000990 unsigned FirstRelI = Piece.FirstRelocation;
991 if (FirstRelI != (unsigned)-1)
992 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000993
994 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +0000995 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000996
997 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000998 if (Cie->Piece == nullptr) {
999 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001000 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001001 }
1002 return Cie;
1003}
1004
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001005// There is one FDE per function. Returns true if a given FDE
1006// points to a live function.
1007template <class ELFT>
1008template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001009bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001010 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001011 ArrayRef<RelTy> Rels) {
1012 unsigned FirstRelI = Piece.FirstRelocation;
1013 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001014 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +00001015 const RelTy &Rel = Rels[FirstRelI];
1016 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001017 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
1018 if (!D || !D->Section)
1019 return false;
1020 InputSectionBase<ELFT> *Target = D->Section->Repl;
1021 return Target && Target->Live;
1022}
1023
1024// .eh_frame is a sequence of CIE or FDE records. In general, there
1025// is one CIE record per input object file which is followed by
1026// a list of FDEs. This function searches an existing CIE or create a new
1027// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +00001028template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +00001029template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001030void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +00001031 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001032 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +00001033
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001034 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001035 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001036 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +00001037 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +00001038 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001039
1040 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001041 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001042 if (ID == 0) {
1043 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
1044 continue;
1045 }
1046
1047 uint32_t CieOffset = Offset + 4 - ID;
1048 CieRecord *Cie = OffsetToCie[CieOffset];
1049 if (!Cie)
1050 fatal("invalid CIE reference");
1051
1052 if (!isFdeLive(Piece, Sec, Rels))
1053 continue;
1054 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +00001055 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001056 }
1057}
1058
1059template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001060void EhOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001061 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001062 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001063 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001064 Sections.push_back(Sec);
1065
1066 // .eh_frame is a sequence of CIE or FDE records. This function
1067 // splits it into pieces so that we can call
1068 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +00001069 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001070 if (Sec->Pieces.empty())
1071 return;
1072
1073 if (const Elf_Shdr *RelSec = Sec->RelocSection) {
1074 ELFFile<ELFT> &Obj = Sec->getFile()->getObj();
1075 if (RelSec->sh_type == SHT_RELA)
1076 addSectionAux(Sec, Obj.relas(RelSec));
1077 else
1078 addSectionAux(Sec, Obj.rels(RelSec));
Rui Ueyama0de86c12016-01-27 22:23:44 +00001079 return;
1080 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001081 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001082}
1083
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001084template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +00001085static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
1086 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +00001087
1088 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001089 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +00001090 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +00001091}
1092
Rui Ueyama1e479c22016-05-23 15:07:59 +00001093template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rui Ueyamae9381bd2016-07-16 02:47:42 +00001094 if (this->Header.sh_size)
1095 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +00001096
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001097 size_t Off = 0;
1098 for (CieRecord *Cie : Cies) {
1099 Cie->Piece->OutputOff = Off;
1100 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001101
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001102 for (SectionPiece *Fde : Cie->FdePieces) {
1103 Fde->OutputOff = Off;
1104 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001105 }
1106 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001107 this->Header.sh_size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001108}
1109
Rui Ueyamae75e9332016-05-23 03:00:33 +00001110template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
1111 const endianness E = ELFT::TargetEndianness;
1112 switch (Size) {
1113 case DW_EH_PE_udata2:
1114 return read16<E>(Buf);
1115 case DW_EH_PE_udata4:
1116 return read32<E>(Buf);
1117 case DW_EH_PE_udata8:
1118 return read64<E>(Buf);
1119 case DW_EH_PE_absptr:
1120 if (ELFT::Is64Bits)
1121 return read64<E>(Buf);
1122 return read32<E>(Buf);
1123 }
1124 fatal("unknown FDE size encoding");
1125}
1126
1127// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
1128// We need it to create .eh_frame_hdr section.
1129template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001130typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +00001131 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001132 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +00001133 // stored at FDE + 8 byte.
1134 size_t Off = FdeOff + 8;
1135 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
1136 if ((Enc & 0x70) == DW_EH_PE_absptr)
1137 return Addr;
1138 if ((Enc & 0x70) == DW_EH_PE_pcrel)
1139 return Addr + this->getVA() + Off;
1140 fatal("unknown FDE size relative encoding");
1141}
1142
Rui Ueyama1e479c22016-05-23 15:07:59 +00001143template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001144 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001145 for (CieRecord *Cie : Cies) {
1146 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001147 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001148
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001149 for (SectionPiece *Fde : Cie->FdePieces) {
1150 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001151 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +00001152
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001153 // FDE's second word should have the offset to an associated CIE.
1154 // Write it.
1155 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001156 }
1157 }
1158
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001159 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +00001160 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +00001161
1162 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001163 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +00001164 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +00001165 if (Out<ELFT>::EhFrameHdr) {
1166 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +00001167 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +00001168 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +00001169 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rui Ueyama3b31e672016-05-23 16:24:16 +00001170 uintX_t FdeVA = this->getVA() + Fde->OutputOff;
1171 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
1172 }
Rui Ueyamae75e9332016-05-23 03:00:33 +00001173 }
1174 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001175}
1176
1177template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001178MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001179 uintX_t Flags, uintX_t Alignment)
1180 : OutputSectionBase<ELFT>(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001181 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001182
1183template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001184 if (shouldTailMerge()) {
1185 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001186 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001187 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001188 }
Rui Ueyama31f9f612016-05-06 00:52:08 +00001189 for (const std::pair<CachedHash<StringRef>, size_t> &P : Builder.getMap()) {
1190 StringRef Data = P.first.Val;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001191 memcpy(Buf + P.second, Data.data(), Data.size());
1192 }
1193}
1194
Rui Ueyama34dc99e2016-05-22 01:15:32 +00001195static StringRef toStringRef(ArrayRef<uint8_t> A) {
1196 return {(const char *)A.data(), A.size()};
1197}
1198
Rafael Espindolac159c962015-10-19 21:00:02 +00001199template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001200void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama10803512016-05-22 00:25:30 +00001201 auto *Sec = cast<MergeInputSection<ELFT>>(C);
1202 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001203 this->updateAlignment(Sec->Alignment);
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001204 this->Header.sh_entsize = Sec->getSectionHdr()->sh_entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +00001205 Sections.push_back(Sec);
Rafael Espindolac159c962015-10-19 21:00:02 +00001206
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001207 bool IsString = this->Header.sh_flags & SHF_STRINGS;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001208
Rui Ueyamab7eda282016-05-24 02:10:28 +00001209 for (SectionPiece &Piece : Sec->Pieces) {
Rui Ueyama3ea87272016-05-22 00:13:04 +00001210 if (!Piece.Live)
Rafael Espindola0b9531c2016-04-22 22:09:35 +00001211 continue;
Rui Ueyamad8849272016-05-25 16:37:01 +00001212 uintX_t OutputOffset = Builder.add(toStringRef(Piece.data()));
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001213 if (!IsString || !shouldTailMerge())
1214 Piece.OutputOff = OutputOffset;
Rafael Espindolac159c962015-10-19 21:00:02 +00001215 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001216}
1217
1218template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001219unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1220 return Builder.getOffset(Val);
1221}
1222
1223template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1224 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1225}
1226
1227template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1228 if (shouldTailMerge())
1229 Builder.finalize();
1230 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001231}
1232
Rui Ueyama406b4692016-05-27 14:39:13 +00001233template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() {
1234 for (MergeInputSection<ELFT> *Sec : Sections)
1235 Sec->finalizePieces();
1236}
1237
Rafael Espindolac159c962015-10-19 21:00:02 +00001238template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001239StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001240 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1241 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyama3b04d832016-07-14 05:46:24 +00001242 Dynamic(Dynamic) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +00001243
Rafael Espindolae2c24612016-01-29 01:24:25 +00001244// Adds a string to the string table. If HashIt is true we hash and check for
1245// duplicates. It is optional because the name of global symbols are already
1246// uniqued and hashing them again has a big cost for a small value: uniquing
1247// them with some other string that happens to be the same.
1248template <class ELFT>
1249unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1250 if (HashIt) {
1251 auto R = StringMap.insert(std::make_pair(S, Size));
1252 if (!R.second)
1253 return R.first->second;
1254 }
1255 unsigned Ret = Size;
1256 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001257 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001258 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001259}
1260
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001261template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001262 // ELF string tables start with NUL byte, so advance the pointer by one.
1263 ++Buf;
1264 for (StringRef S : Strings) {
1265 memcpy(Buf, S.data(), S.size());
1266 Buf += S.size() + 1;
1267 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001268}
1269
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001270template <class ELFT>
Rui Ueyama809d8e22016-06-23 04:33:42 +00001271typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
1272 if (OutputSec)
1273 return OutputSec->getVA() + OffsetInSec;
Rafael Espindola0f7ceda2016-07-20 17:58:07 +00001274 return InputSec->OutSec->getVA() + OffsetInSec;
Rui Ueyama809d8e22016-06-23 04:33:42 +00001275}
1276
1277template <class ELFT>
1278typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
1279 if (UseSymVA)
1280 return Sym->getVA<ELFT>(Addend);
1281 return Addend;
1282}
1283
1284template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
1285 if (Sym && !UseSymVA)
1286 return Sym->DynsymIndex;
1287 return 0;
1288}
1289
1290template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001291SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamaace4f902016-05-24 04:25:47 +00001292 StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001293 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1294 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001295 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyamaace4f902016-05-24 04:25:47 +00001296 StrTabSec(StrTabSec) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001297 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001298 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001299}
1300
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001301// Orders symbols according to their positions in the GOT,
1302// in compliance with MIPS ABI rules.
1303// See "Global Offset Table" in Chapter 5 in the following document
1304// for detailed description:
1305// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001306static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1307 const std::pair<SymbolBody *, unsigned> &R) {
Simon Atanasyand2980d32016-03-29 14:07:22 +00001308 // Sort entries related to non-local preemptible symbols by GOT indexes.
1309 // All other entries go to the first part of GOT in arbitrary order.
Simon Atanasyan7b8481b2016-06-20 11:37:56 +00001310 bool LIsInLocalGot = !L.first->IsInGlobalMipsGot;
1311 bool RIsInLocalGot = !R.first->IsInGlobalMipsGot;
1312 if (LIsInLocalGot || RIsInLocalGot)
1313 return !RIsInLocalGot;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001314 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001315}
1316
Rafael Espindolabadd3972016-04-08 15:30:56 +00001317static uint8_t getSymbolBinding(SymbolBody *Body) {
Peter Collingbourne4f952702016-05-01 04:55:03 +00001318 Symbol *S = Body->symbol();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001319 uint8_t Visibility = S->Visibility;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001320 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1321 return STB_LOCAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001322 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
Rafael Espindolabadd3972016-04-08 15:30:56 +00001323 return STB_GLOBAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001324 return S->Binding;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001325}
1326
Rui Ueyama0db335f2015-10-07 16:58:54 +00001327template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001328 if (this->Header.sh_size)
1329 return; // Already finalized.
1330
Rui Ueyama0db335f2015-10-07 16:58:54 +00001331 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001332 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001333 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001334
George Rimar58941ee2016-02-25 08:23:37 +00001335 if (Config->Relocatable) {
1336 size_t I = NumLocals;
1337 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
1338 P.first->DynsymIndex = ++I;
1339 return;
1340 }
1341
Igor Kudrinab665fc2015-10-20 21:47:58 +00001342 if (!StrTabSec.isDynamic()) {
1343 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001344 [](const std::pair<SymbolBody *, unsigned> &L,
1345 const std::pair<SymbolBody *, unsigned> &R) {
1346 return getSymbolBinding(L.first) == STB_LOCAL &&
1347 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001348 });
1349 return;
1350 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001351 if (Out<ELFT>::GnuHashTab)
1352 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1353 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001354 else if (Config->EMachine == EM_MIPS)
1355 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001356 size_t I = 0;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001357 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001358 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001359}
1360
1361template <class ELFT>
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001362void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1363 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001364}
1365
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001366template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001367 Buf += sizeof(Elf_Sym);
1368
1369 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1370 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001371 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1372 writeLocalSymbols(Buf);
1373
1374 writeGlobalSymbols(Buf);
1375}
1376
1377template <class ELFT>
1378void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1379 // Iterate over all input object files to copy their local symbols
1380 // to the output symbol table pointed by Buf.
Rui Ueyamaace4f902016-05-24 04:25:47 +00001381 for (const std::unique_ptr<ObjectFile<ELFT>> &File :
1382 Symtab<ELFT>::X->getObjectFiles()) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001383 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1384 File->KeptLocalSyms) {
1385 const DefinedRegular<ELFT> &Body = *P.first;
1386 InputSectionBase<ELFT> *Section = Body.Section;
Rui Ueyamac55733e2015-09-30 00:54:29 +00001387 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001388
1389 if (!Section) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001390 ESym->st_shndx = SHN_ABS;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001391 ESym->st_value = Body.Value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001392 } else {
Rafael Espindolac159c962015-10-19 21:00:02 +00001393 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1394 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001395 ESym->st_value = OutSec->getVA() + Section->getOffset(Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001396 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001397 ESym->st_name = P.second;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001398 ESym->st_size = Body.template getSize<ELFT>();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001399 ESym->setBindingAndType(STB_LOCAL, Body.Type);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001400 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001401 }
1402 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001403}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001404
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001405template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001406void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001407 // Write the internal symbol table contents to the output symbol table
1408 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001409 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001410 for (const std::pair<SymbolBody *, size_t> &P : Symbols) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001411 SymbolBody *Body = P.first;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001412 size_t StrOff = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001413
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001414 uint8_t Type = Body->Type;
1415 uintX_t Size = Body->getSize<ELFT>();
Rafael Espindola8614c562015-10-06 14:33:58 +00001416
Igor Kudrin853b88d2015-10-20 20:52:14 +00001417 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001418 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001419 ESym->st_name = StrOff;
Peter Collingbourne4f952702016-05-01 04:55:03 +00001420 ESym->setVisibility(Body->symbol()->Visibility);
Rui Ueyamab5a69702016-02-01 21:00:35 +00001421 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001422
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001423 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001424 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001425 else if (isa<DefinedRegular<ELFT>>(Body))
1426 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +00001427
1428 // On MIPS we need to mark symbol which has a PLT entry and requires pointer
1429 // equality by STO_MIPS_PLT flag. That is necessary to help dynamic linker
1430 // distinguish such symbols and MIPS lazy-binding stubs.
1431 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1432 if (Config->EMachine == EM_MIPS && Body->isInPlt() &&
1433 Body->NeedsCopyOrPltAddr)
Simon Atanasyanbea96982016-02-25 21:09:05 +00001434 ESym->st_other |= STO_MIPS_PLT;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001435 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001436 }
1437}
1438
Igor Kudrin853b88d2015-10-20 20:52:14 +00001439template <class ELFT>
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001440const OutputSectionBase<ELFT> *
1441SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1442 switch (Sym->kind()) {
1443 case SymbolBody::DefinedSyntheticKind:
Peter Collingbourne6a422592016-05-03 01:21:08 +00001444 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001445 case SymbolBody::DefinedRegularKind: {
Rafael Espindola5ffa13c2016-04-15 00:15:02 +00001446 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +00001447 if (D.Section)
1448 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001449 break;
1450 }
1451 case SymbolBody::DefinedCommonKind:
Rui Ueyama07784902016-08-02 01:35:13 +00001452 return CommonInputSection<ELFT>::X->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001453 case SymbolBody::SharedKind:
1454 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1455 return Out<ELFT>::Bss;
1456 break;
Peter Collingbourne60976ed2016-04-27 00:05:06 +00001457 case SymbolBody::UndefinedKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001458 case SymbolBody::LazyArchiveKind:
1459 case SymbolBody::LazyObjectKind:
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001460 break;
1461 case SymbolBody::DefinedBitcodeKind:
Davide Italianof6523ae2016-03-29 02:20:10 +00001462 llvm_unreachable("should have been replaced");
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001463 }
1464 return nullptr;
1465}
1466
1467template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +00001468VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rui Ueyamaf5244642016-07-16 02:36:00 +00001469 : OutputSectionBase<ELFT>(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
1470 this->Header.sh_addralign = sizeof(uint32_t);
1471}
George Rimard3566302016-06-20 11:55:12 +00001472
1473static StringRef getFileDefName() {
1474 if (!Config->SoName.empty())
1475 return Config->SoName;
1476 return Config->OutputFile;
1477}
1478
1479template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1480 FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001481 for (VersionDefinition &V : Config->VersionDefinitions)
George Rimard3566302016-06-20 11:55:12 +00001482 V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name);
1483
1484 this->Header.sh_size =
1485 (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1486 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +00001487
1488 // sh_info should be set to the number of definitions. This fact is missed in
1489 // documentation, but confirmed by binutils community:
1490 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1491 this->Header.sh_info = getVerDefNum();
1492}
1493
Rui Ueyama9f619642016-07-16 02:29:45 +00001494template <class ELFT>
1495void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1496 StringRef Name, size_t NameOff) {
1497 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +00001498 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +00001499 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +00001500 Verdef->vd_aux = sizeof(Elf_Verdef);
1501 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1502 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +00001503 Verdef->vd_ndx = Index;
1504 Verdef->vd_hash = hashSysv(Name);
George Rimard3566302016-06-20 11:55:12 +00001505
Rui Ueyama9f619642016-07-16 02:29:45 +00001506 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1507 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +00001508 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001509}
1510
1511template <class ELFT>
1512void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001513 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1514
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001515 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001516 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1517 writeOne(Buf, V.Id, V.Name, V.NameOff);
1518 }
1519
1520 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +00001521 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +00001522 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001523}
1524
1525template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001526VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001527 : OutputSectionBase<ELFT>(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001528 this->Header.sh_addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001529}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001530
1531template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1532 this->Header.sh_size =
1533 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1);
1534 this->Header.sh_entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +00001535 // At the moment of june 2016 GNU docs does not mention that sh_link field
1536 // should be set, but Sun docs do. Also readelf relies on this field.
1537 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001538}
1539
1540template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1541 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1542 for (const std::pair<SymbolBody *, size_t> &P :
1543 Out<ELFT>::DynSymTab->getSymbols()) {
George Rimard3566302016-06-20 11:55:12 +00001544 OutVersym->vs_index = P.first->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001545 ++OutVersym;
1546 }
1547}
1548
1549template <class ELFT>
1550VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001551 : OutputSectionBase<ELFT>(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001552 this->Header.sh_addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +00001553
1554 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1555 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1556 // First identifiers are reserved by verdef section if it exist.
1557 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001558}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001559
1560template <class ELFT>
1561void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1562 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +00001563 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001564 return;
1565 }
Rui Ueyama434b5612016-07-17 03:11:46 +00001566 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001567 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1568 // to create one by adding it to our needed list and creating a dynstr entry
1569 // for the soname.
1570 if (F->VerdefMap.empty())
1571 Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())});
1572 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1573 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1574 // prepare to create one by allocating a version identifier and creating a
1575 // dynstr entry for the version name.
1576 if (NV.Index == 0) {
1577 NV.StrTab = Out<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +00001578 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001579 NV.Index = NextIndex++;
1580 }
George Rimard3566302016-06-20 11:55:12 +00001581 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001582}
1583
1584template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1585 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1586 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1587 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1588
1589 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1590 // Create an Elf_Verneed for this DSO.
1591 Verneed->vn_version = 1;
1592 Verneed->vn_cnt = P.first->VerdefMap.size();
1593 Verneed->vn_file = P.second;
1594 Verneed->vn_aux =
1595 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1596 Verneed->vn_next = sizeof(Elf_Verneed);
1597 ++Verneed;
1598
1599 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1600 // VerdefMap, which will only contain references to needed version
1601 // definitions. Each Elf_Vernaux is based on the information contained in
1602 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1603 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1604 // data structures within a single input file.
1605 for (auto &NV : P.first->VerdefMap) {
1606 Vernaux->vna_hash = NV.first->vd_hash;
1607 Vernaux->vna_flags = 0;
1608 Vernaux->vna_other = NV.second.Index;
1609 Vernaux->vna_name = NV.second.StrTab;
1610 Vernaux->vna_next = sizeof(Elf_Vernaux);
1611 ++Vernaux;
1612 }
1613
1614 Vernaux[-1].vna_next = 0;
1615 }
1616 Verneed[-1].vn_next = 0;
1617}
1618
1619template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1620 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
1621 this->Header.sh_info = Needed.size();
1622 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1623 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1624 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1625 this->Header.sh_size = Size;
1626}
1627
1628template <class ELFT>
Rui Ueyama3a41be22016-04-07 22:49:21 +00001629BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
1630 : OutputSectionBase<ELFT>(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC),
1631 HashSize(HashSize) {
1632 // 16 bytes for the note section header.
1633 this->Header.sh_size = 16 + HashSize;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001634}
1635
1636template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
1637 const endianness E = ELFT::TargetEndianness;
1638 write32<E>(Buf, 4); // Name size
Rui Ueyama3a41be22016-04-07 22:49:21 +00001639 write32<E>(Buf + 4, HashSize); // Content size
Rui Ueyama634ddf02016-03-11 20:51:53 +00001640 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
1641 memcpy(Buf + 12, "GNU", 4); // Name string
1642 HashBuf = Buf + 16;
1643}
1644
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001645template <class ELFT>
1646void BuildIdFnv1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
Rui Ueyama634ddf02016-03-11 20:51:53 +00001647 const endianness E = ELFT::TargetEndianness;
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001648
1649 // 64-bit FNV-1 hash
1650 uint64_t Hash = 0xcbf29ce484222325;
1651 for (ArrayRef<uint8_t> Buf : Bufs) {
1652 for (uint8_t B : Buf) {
1653 Hash *= 0x100000001b3;
1654 Hash ^= B;
1655 }
1656 }
Rui Ueyama3a41be22016-04-07 22:49:21 +00001657 write64<E>(this->HashBuf, Hash);
1658}
1659
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001660template <class ELFT>
1661void BuildIdMd5<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001662 MD5 Hash;
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001663 for (ArrayRef<uint8_t> Buf : Bufs)
1664 Hash.update(Buf);
Rui Ueyama3a41be22016-04-07 22:49:21 +00001665 MD5::MD5Result Res;
1666 Hash.final(Res);
1667 memcpy(this->HashBuf, Res, 16);
Rui Ueyama634ddf02016-03-11 20:51:53 +00001668}
1669
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001670template <class ELFT>
1671void BuildIdSha1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001672 SHA1 Hash;
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001673 for (ArrayRef<uint8_t> Buf : Bufs)
1674 Hash.update(Buf);
Rui Ueyamad86ec302016-04-07 23:51:56 +00001675 memcpy(this->HashBuf, Hash.final().data(), 20);
1676}
1677
Rui Ueyama634ddf02016-03-11 20:51:53 +00001678template <class ELFT>
Rui Ueyama9194db72016-05-13 21:55:56 +00001679BuildIdHexstring<ELFT>::BuildIdHexstring()
1680 : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
1681
1682template <class ELFT>
1683void BuildIdHexstring<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
1684 memcpy(this->HashBuf, Config->BuildIdVector.data(),
1685 Config->BuildIdVector.size());
1686}
1687
1688template <class ELFT>
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001689MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1690 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1691 this->Header.sh_addralign = 4;
1692 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1693 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1694}
1695
1696template <class ELFT>
1697void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1698 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
Simon Atanasyan4ee29182016-04-27 05:31:28 +00001699 R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
Rui Ueyama70eed362016-01-06 22:42:43 +00001700 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001701}
1702
1703template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001704void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001705 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001706 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001707 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan84bb3552016-05-26 20:46:01 +00001708 S->OutSec = this;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001709}
1710
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001711template <class ELFT>
1712MipsOptionsOutputSection<ELFT>::MipsOptionsOutputSection()
1713 : OutputSectionBase<ELFT>(".MIPS.options", SHT_MIPS_OPTIONS,
1714 SHF_ALLOC | SHF_MIPS_NOSTRIP) {
1715 this->Header.sh_addralign = 8;
1716 this->Header.sh_entsize = 1;
1717 this->Header.sh_size = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
1718}
1719
1720template <class ELFT>
1721void MipsOptionsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1722 auto *Opt = reinterpret_cast<Elf_Mips_Options *>(Buf);
1723 Opt->kind = ODK_REGINFO;
1724 Opt->size = this->Header.sh_size;
1725 Opt->section = 0;
1726 Opt->info = 0;
1727 auto *Reg = reinterpret_cast<Elf_Mips_RegInfo *>(Buf + sizeof(*Opt));
1728 Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
1729 Reg->ri_gprmask = GprMask;
1730}
1731
1732template <class ELFT>
1733void MipsOptionsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1734 auto *S = cast<MipsOptionsInputSection<ELFT>>(C);
1735 if (S->Reginfo)
1736 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan84bb3552016-05-26 20:46:01 +00001737 S->OutSec = this;
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001738}
1739
George Rimar6892afa2016-07-12 09:49:43 +00001740template <class ELFT>
1741std::pair<OutputSectionBase<ELFT> *, bool>
1742OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1743 StringRef OutsecName) {
1744 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
1745 OutputSectionBase<ELFT> *&Sec = Map[Key];
1746 if (Sec)
1747 return {Sec, false};
1748
1749 switch (C->SectionKind) {
1750 case InputSectionBase<ELFT>::Regular:
1751 Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
1752 break;
1753 case InputSectionBase<ELFT>::EHFrame:
1754 return {Out<ELFT>::EhFrame, false};
1755 case InputSectionBase<ELFT>::Merge:
1756 Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags,
1757 Key.Alignment);
1758 break;
1759 case InputSectionBase<ELFT>::MipsReginfo:
1760 Sec = new MipsReginfoOutputSection<ELFT>();
1761 break;
1762 case InputSectionBase<ELFT>::MipsOptions:
1763 Sec = new MipsOptionsOutputSection<ELFT>();
1764 break;
1765 }
Rui Ueyamaa7f78842016-07-20 17:19:03 +00001766 OwningSections.emplace_back(Sec);
George Rimar6892afa2016-07-12 09:49:43 +00001767 return {Sec, true};
1768}
1769
1770template <class ELFT>
1771OutputSectionBase<ELFT> *OutputSectionFactory<ELFT>::lookup(StringRef Name,
1772 uint32_t Type,
1773 uintX_t Flags) {
1774 return Map.lookup({Name, Type, Flags, 0});
1775}
1776
1777template <class ELFT>
1778SectionKey<ELFT::Is64Bits>
1779OutputSectionFactory<ELFT>::createKey(InputSectionBase<ELFT> *C,
1780 StringRef OutsecName) {
1781 const Elf_Shdr *H = C->getSectionHdr();
1782 uintX_t Flags = H->sh_flags & ~SHF_GROUP & ~SHF_COMPRESSED;
1783
1784 // For SHF_MERGE we create different output sections for each alignment.
1785 // This makes each output section simple and keeps a single level mapping from
1786 // input to output.
1787 uintX_t Alignment = 0;
1788 if (isa<MergeInputSection<ELFT>>(C))
1789 Alignment = std::max(H->sh_addralign, H->sh_entsize);
1790
1791 uint32_t Type = H->sh_type;
1792 return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, Alignment};
1793}
1794
1795template <bool Is64Bits>
1796typename lld::elf::SectionKey<Is64Bits>
1797DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
1798 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
1799}
1800
1801template <bool Is64Bits>
1802typename lld::elf::SectionKey<Is64Bits>
1803DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
1804 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
1805 0};
1806}
1807
1808template <bool Is64Bits>
1809unsigned
1810DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
1811 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
1812}
1813
1814template <bool Is64Bits>
1815bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
1816 const Key &RHS) {
1817 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
1818 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
1819 LHS.Alignment == RHS.Alignment;
1820}
1821
1822namespace llvm {
1823template struct DenseMapInfo<SectionKey<true>>;
1824template struct DenseMapInfo<SectionKey<false>>;
1825}
1826
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001827namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001828namespace elf {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001829template class OutputSectionBase<ELF32LE>;
1830template class OutputSectionBase<ELF32BE>;
1831template class OutputSectionBase<ELF64LE>;
1832template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001833
George Rimarf6bc65a2016-01-15 13:34:52 +00001834template class EhFrameHeader<ELF32LE>;
1835template class EhFrameHeader<ELF32BE>;
1836template class EhFrameHeader<ELF64LE>;
1837template class EhFrameHeader<ELF64BE>;
1838
George Rimar648a2c32015-10-20 08:54:27 +00001839template class GotPltSection<ELF32LE>;
1840template class GotPltSection<ELF32BE>;
1841template class GotPltSection<ELF64LE>;
1842template class GotPltSection<ELF64BE>;
1843
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001844template class GotSection<ELF32LE>;
1845template class GotSection<ELF32BE>;
1846template class GotSection<ELF64LE>;
1847template class GotSection<ELF64BE>;
1848
1849template class PltSection<ELF32LE>;
1850template class PltSection<ELF32BE>;
1851template class PltSection<ELF64LE>;
1852template class PltSection<ELF64BE>;
1853
1854template class RelocationSection<ELF32LE>;
1855template class RelocationSection<ELF32BE>;
1856template class RelocationSection<ELF64LE>;
1857template class RelocationSection<ELF64BE>;
1858
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001859template class InterpSection<ELF32LE>;
1860template class InterpSection<ELF32BE>;
1861template class InterpSection<ELF64LE>;
1862template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001863
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001864template class GnuHashTableSection<ELF32LE>;
1865template class GnuHashTableSection<ELF32BE>;
1866template class GnuHashTableSection<ELF64LE>;
1867template class GnuHashTableSection<ELF64BE>;
1868
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001869template class HashTableSection<ELF32LE>;
1870template class HashTableSection<ELF32BE>;
1871template class HashTableSection<ELF64LE>;
1872template class HashTableSection<ELF64BE>;
1873
1874template class DynamicSection<ELF32LE>;
1875template class DynamicSection<ELF32BE>;
1876template class DynamicSection<ELF64LE>;
1877template class DynamicSection<ELF64BE>;
1878
1879template class OutputSection<ELF32LE>;
1880template class OutputSection<ELF32BE>;
1881template class OutputSection<ELF64LE>;
1882template class OutputSection<ELF64BE>;
1883
Rui Ueyama1e479c22016-05-23 15:07:59 +00001884template class EhOutputSection<ELF32LE>;
1885template class EhOutputSection<ELF32BE>;
1886template class EhOutputSection<ELF64LE>;
1887template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001888
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001889template class MipsReginfoOutputSection<ELF32LE>;
1890template class MipsReginfoOutputSection<ELF32BE>;
1891template class MipsReginfoOutputSection<ELF64LE>;
1892template class MipsReginfoOutputSection<ELF64BE>;
1893
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001894template class MipsOptionsOutputSection<ELF32LE>;
1895template class MipsOptionsOutputSection<ELF32BE>;
1896template class MipsOptionsOutputSection<ELF64LE>;
1897template class MipsOptionsOutputSection<ELF64BE>;
1898
Rafael Espindolac159c962015-10-19 21:00:02 +00001899template class MergeOutputSection<ELF32LE>;
1900template class MergeOutputSection<ELF32BE>;
1901template class MergeOutputSection<ELF64LE>;
1902template class MergeOutputSection<ELF64BE>;
1903
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001904template class StringTableSection<ELF32LE>;
1905template class StringTableSection<ELF32BE>;
1906template class StringTableSection<ELF64LE>;
1907template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001908
1909template class SymbolTableSection<ELF32LE>;
1910template class SymbolTableSection<ELF32BE>;
1911template class SymbolTableSection<ELF64LE>;
1912template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001913
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001914template class VersionTableSection<ELF32LE>;
1915template class VersionTableSection<ELF32BE>;
1916template class VersionTableSection<ELF64LE>;
1917template class VersionTableSection<ELF64BE>;
1918
1919template class VersionNeedSection<ELF32LE>;
1920template class VersionNeedSection<ELF32BE>;
1921template class VersionNeedSection<ELF64LE>;
1922template class VersionNeedSection<ELF64BE>;
1923
George Rimard3566302016-06-20 11:55:12 +00001924template class VersionDefinitionSection<ELF32LE>;
1925template class VersionDefinitionSection<ELF32BE>;
1926template class VersionDefinitionSection<ELF64LE>;
1927template class VersionDefinitionSection<ELF64BE>;
1928
Rui Ueyama634ddf02016-03-11 20:51:53 +00001929template class BuildIdSection<ELF32LE>;
1930template class BuildIdSection<ELF32BE>;
1931template class BuildIdSection<ELF64LE>;
1932template class BuildIdSection<ELF64BE>;
Rui Ueyama3a41be22016-04-07 22:49:21 +00001933
1934template class BuildIdFnv1<ELF32LE>;
1935template class BuildIdFnv1<ELF32BE>;
1936template class BuildIdFnv1<ELF64LE>;
1937template class BuildIdFnv1<ELF64BE>;
1938
1939template class BuildIdMd5<ELF32LE>;
1940template class BuildIdMd5<ELF32BE>;
1941template class BuildIdMd5<ELF64LE>;
1942template class BuildIdMd5<ELF64BE>;
Rui Ueyamad86ec302016-04-07 23:51:56 +00001943
1944template class BuildIdSha1<ELF32LE>;
1945template class BuildIdSha1<ELF32BE>;
1946template class BuildIdSha1<ELF64LE>;
1947template class BuildIdSha1<ELF64BE>;
Rui Ueyama9194db72016-05-13 21:55:56 +00001948
1949template class BuildIdHexstring<ELF32LE>;
1950template class BuildIdHexstring<ELF32BE>;
1951template class BuildIdHexstring<ELF64LE>;
1952template class BuildIdHexstring<ELF64BE>;
George Rimar6892afa2016-07-12 09:49:43 +00001953
1954template class OutputSectionFactory<ELF32LE>;
1955template class OutputSectionFactory<ELF32BE>;
1956template class OutputSectionFactory<ELF64LE>;
1957template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001958}
1959}