blob: 14709ea9de261d37a7127031a40c017ed2c11352 [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"
Eugene Leviant933dae72016-08-26 09:55:37 +000022#include "llvm/Support/RandomNumberGenerator.h"
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
Rui Ueyamac442cff2016-09-08 21:46:21 +0000264template <class ELFT>
265static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
266 typedef typename ELFT::uint uintX_t;
267 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
268}
269
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000270template <class ELFT> void GotSection<ELFT>::writeMipsGot(uint8_t *Buf) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000271 // Set the MSB of the second GOT slot. This is not required by any
272 // MIPS ABI documentation, though.
273 //
274 // There is a comment in glibc saying that "The MSB of got[1] of a
275 // gnu object is set to identify gnu objects," and in GNU gold it
276 // says "the second entry will be used by some runtime loaders".
277 // But how this field is being used is unclear.
278 //
279 // We are not really willing to mimic other linkers behaviors
280 // without understanding why they do that, but because all files
281 // generated by GNU tools have this special GOT value, and because
282 // we've been doing this for years, it is probably a safe bet to
283 // keep doing this for now. We really need to revisit this to see
284 // if we had to do this.
285 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
286 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
287 // Write 'page address' entries to the local part of the GOT.
288 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
289 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000290 writeUint<ELFT>(Entry, L.first);
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000291 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000292 Buf += MipsPageEntries * sizeof(uintX_t);
293 auto AddEntry = [&](const MipsGotEntry &SA) {
294 uint8_t *Entry = Buf;
295 Buf += sizeof(uintX_t);
George Rimar06e930d2016-06-20 10:01:50 +0000296 const SymbolBody* Body = SA.first;
297 uintX_t VA = Body->template getVA<ELFT>(SA.second);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000298 writeUint<ELFT>(Entry, VA);
Simon Atanasyan41325112016-06-19 21:39:37 +0000299 };
300 std::for_each(std::begin(MipsLocal), std::end(MipsLocal), AddEntry);
301 std::for_each(std::begin(MipsGlobal), std::end(MipsGlobal), AddEntry);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000302 // Initialize TLS-related GOT entries. If the entry has a corresponding
303 // dynamic relocations, leave it initialized by zero. Write down adjusted
304 // TLS symbol's values otherwise. To calculate the adjustments use offsets
305 // for thread-local storage.
306 // https://www.linux-mips.org/wiki/NPTL
307 if (TlsIndexOff != -1U && !Config->Pic)
Rui Ueyamac442cff2016-09-08 21:46:21 +0000308 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000309 for (const SymbolBody *B : Entries) {
310 if (!B || B->isPreemptible())
311 continue;
312 uintX_t VA = B->getVA<ELFT>();
313 if (B->GotIndex != -1U) {
314 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000315 writeUint<ELFT>(Entry, VA - 0x7000);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000316 }
317 if (B->GlobalDynIndex != -1U) {
318 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000319 writeUint<ELFT>(Entry, 1);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000320 Entry += sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000321 writeUint<ELFT>(Entry, VA - 0x8000);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000322 }
323 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000324}
325
326template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000327 if (Config->EMachine == EM_MIPS) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000328 writeMipsGot(Buf);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000329 return;
330 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000331 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000332 uint8_t *Entry = Buf;
333 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000334 if (!B)
335 continue;
Simon Atanasyan41325112016-06-19 21:39:37 +0000336 if (B->isPreemptible())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000337 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000338 uintX_t VA = B->getVA<ELFT>();
Rui Ueyamac442cff2016-09-08 21:46:21 +0000339 writeUint<ELFT>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000340 }
341}
342
Rafael Espindola35c6af32015-09-25 17:19:10 +0000343template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000344PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000345 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000346 this->Header.sh_addralign = 16;
347}
348
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000350 // At beginning of PLT, we have code to call the dynamic linker
351 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama4a90f572016-06-16 16:28:50 +0000352 Target->writePltHeader(Buf);
353 size_t Off = Target->PltHeaderSize;
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000354
George Rimarfb5d7f22015-11-26 19:58:51 +0000355 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000356 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000357 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000358 uint64_t Got = B->getGotPltVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000359 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000360 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000361 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000362 }
363}
364
Rafael Espindola67d72c02016-03-11 12:06:30 +0000365template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
366 Sym.PltIndex = Entries.size();
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000367 unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000368 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000369}
370
George Rimar648a2c32015-10-20 08:54:27 +0000371template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000372 this->Header.sh_size =
Rui Ueyama4a90f572016-06-16 16:28:50 +0000373 Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000374}
375
376template <class ELFT>
George Rimarc191acf2016-05-10 15:47:57 +0000377RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000378 : OutputSectionBase<ELFT>(Name, Config->Rela ? SHT_RELA : SHT_REL,
George Rimarc191acf2016-05-10 15:47:57 +0000379 SHF_ALLOC),
380 Sort(Sort) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000381 this->Header.sh_entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000382 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000383}
384
George Rimar5828c232015-11-30 17:49:19 +0000385template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000386void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
Eugene Leviantaa498192016-08-31 08:51:39 +0000387 if (Reloc.Type == Target->RelativeRel)
388 ++NumRelativeRelocs;
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000389 Relocs.push_back(Reloc);
390}
391
George Rimarc191acf2016-05-10 15:47:57 +0000392template <class ELFT, class RelTy>
393static bool compRelocations(const RelTy &A, const RelTy &B) {
Eugene Leviantaa498192016-08-31 08:51:39 +0000394 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
395 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
396 if (AIsRel != BIsRel)
397 return AIsRel;
398
George Rimarc191acf2016-05-10 15:47:57 +0000399 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
400}
401
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000402template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarc191acf2016-05-10 15:47:57 +0000403 uint8_t *BufBegin = Buf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000404 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rui Ueyama614be592016-03-13 05:23:40 +0000405 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000406 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000407
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000408 if (Config->Rela)
Rui Ueyama809d8e22016-06-23 04:33:42 +0000409 P->r_addend = Rel.getAddend();
410 P->r_offset = Rel.getOffset();
Simon Atanasyan002e2442016-06-23 15:26:31 +0000411 if (Config->EMachine == EM_MIPS && Rel.getOutputSec() == Out<ELFT>::Got)
412 // Dynamic relocation against MIPS GOT section make deal TLS entries
413 // allocated in the end of the GOT. We need to adjust the offset to take
414 // in account 'local' and 'global' GOT entries.
415 P->r_offset += Out<ELFT>::Got->getMipsTlsOffset();
Rui Ueyama809d8e22016-06-23 04:33:42 +0000416 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000417 }
George Rimarc191acf2016-05-10 15:47:57 +0000418
419 if (Sort) {
420 if (Config->Rela)
421 std::stable_sort((Elf_Rela *)BufBegin,
422 (Elf_Rela *)BufBegin + Relocs.size(),
423 compRelocations<ELFT, Elf_Rela>);
424 else
425 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
426 compRelocations<ELFT, Elf_Rel>);
427 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000428}
429
George Rimar77b77792015-11-25 22:15:01 +0000430template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000431 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000432}
433
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000434template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama1034c9e2016-08-09 04:42:01 +0000435 this->Header.sh_link = Out<ELFT>::DynSymTab
Rui Ueyamacbd434a2016-08-09 04:31:21 +0000436 ? Out<ELFT>::DynSymTab->SectionIndex
437 : Out<ELFT>::SymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000438 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
439}
440
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000441template <class ELFT>
442InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000443 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444 this->Header.sh_size = Config->DynamicLinker.size() + 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000445}
446
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000447template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama1e720b92016-03-13 06:50:34 +0000448 StringRef S = Config->DynamicLinker;
449 memcpy(Buf, S.data(), S.size());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000450}
451
Rafael Espindola35c6af32015-09-25 17:19:10 +0000452template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000453HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000454 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000455 this->Header.sh_entsize = sizeof(Elf_Word);
456 this->Header.sh_addralign = sizeof(Elf_Word);
457}
458
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000459static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000460 uint32_t H = 0;
461 for (char C : Name) {
462 H = (H << 4) + C;
463 uint32_t G = H & 0xf0000000;
464 if (G)
465 H ^= G >> 24;
466 H &= ~G;
467 }
468 return H;
469}
470
Rui Ueyama0db335f2015-10-07 16:58:54 +0000471template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000472 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000473
George Rimare9e1d322016-02-18 15:17:01 +0000474 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000475 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000476
477 // Create as many buckets as there are symbols.
478 // FIXME: This is simplistic. We can try to optimize it, but implementing
479 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000480 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000481 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
482}
483
484template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000485 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000486 auto *P = reinterpret_cast<Elf_Word *>(Buf);
487 *P++ = NumSymbols; // nbucket
488 *P++ = NumSymbols; // nchain
489
490 Elf_Word *Buckets = P;
491 Elf_Word *Chains = P + NumSymbols;
492
Rafael Espindolae2c24612016-01-29 01:24:25 +0000493 for (const std::pair<SymbolBody *, unsigned> &P :
494 Out<ELFT>::DynSymTab->getSymbols()) {
495 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000496 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000497 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000498 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000499 Chains[I] = Buckets[Hash];
500 Buckets[Hash] = I;
501 }
502}
503
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000504static uint32_t hashGnu(StringRef Name) {
505 uint32_t H = 5381;
506 for (uint8_t C : Name)
507 H = (H << 5) + H + C;
508 return H;
509}
510
511template <class ELFT>
512GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000513 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000514 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000515 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000516}
517
518template <class ELFT>
519unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
520 if (!NumHashed)
521 return 0;
522
523 // These values are prime numbers which are not greater than 2^(N-1) + 1.
524 // In result, for any particular NumHashed we return a prime number
525 // which is not greater than NumHashed.
526 static const unsigned Primes[] = {
527 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
528 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
529
530 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
531 array_lengthof(Primes) - 1)];
532}
533
534// Bloom filter estimation: at least 8 bits for each hashed symbol.
535// GNU Hash table requirement: it should be a power of 2,
536// the minimum value is 1, even for an empty table.
537// Expected results for a 32-bit target:
538// calcMaskWords(0..4) = 1
539// calcMaskWords(5..8) = 2
540// calcMaskWords(9..16) = 4
541// For a 64-bit target:
542// calcMaskWords(0..8) = 1
543// calcMaskWords(9..16) = 2
544// calcMaskWords(17..32) = 4
545template <class ELFT>
546unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
547 if (!NumHashed)
548 return 1;
549 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
550}
551
552template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000553 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000554 NBuckets = calcNBuckets(NumHashed);
555 MaskWords = calcMaskWords(NumHashed);
556 // Second hash shift estimation: just predefined values.
557 Shift2 = ELFT::Is64Bits ? 6 : 5;
558
559 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
560 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
561 + sizeof(Elf_Off) * MaskWords // Bloom Filter
562 + sizeof(Elf_Word) * NBuckets // Hash Buckets
563 + sizeof(Elf_Word) * NumHashed; // Hash Values
564}
565
566template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
567 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000568 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000569 return;
570 writeBloomFilter(Buf);
571 writeHashTable(Buf);
572}
573
574template <class ELFT>
575void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
576 auto *P = reinterpret_cast<Elf_Word *>(Buf);
577 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000578 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000579 *P++ = MaskWords;
580 *P++ = Shift2;
581 Buf = reinterpret_cast<uint8_t *>(P);
582}
583
584template <class ELFT>
585void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
586 unsigned C = sizeof(Elf_Off) * 8;
587
588 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000589 for (const SymbolData &Sym : Symbols) {
590 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
591 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
592 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000593 Masks[Pos] |= V;
594 }
595 Buf += sizeof(Elf_Off) * MaskWords;
596}
597
598template <class ELFT>
599void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
600 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
601 Elf_Word *Values = Buckets + NBuckets;
602
603 int PrevBucket = -1;
604 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000605 for (const SymbolData &Sym : Symbols) {
606 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000607 assert(PrevBucket <= Bucket);
608 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000609 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000610 PrevBucket = Bucket;
611 if (I > 0)
612 Values[I - 1] |= 1;
613 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000614 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000615 ++I;
616 }
617 if (I > 0)
618 Values[I - 1] |= 1;
619}
620
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000621// Add symbols to this symbol hash table. Note that this function
622// destructively sort a given vector -- which is needed because
623// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000624template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000625void GnuHashTableSection<ELFT>::addSymbols(
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000626 std::vector<std::pair<SymbolBody *, size_t>> &V) {
Davide Italianob4b68b62016-07-04 19:49:55 +0000627 // Ideally this will just be 'auto' but GCC 6.1 is not able
628 // to deduce it correctly.
629 std::vector<std::pair<SymbolBody *, size_t>>::iterator Mid =
630 std::stable_partition(V.begin(), V.end(),
631 [](std::pair<SymbolBody *, size_t> &P) {
632 return P.first->isUndefined();
633 });
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000634 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000635 return;
Davide Italianof8591cf2016-07-06 17:41:55 +0000636 for (auto I = Mid, E = V.end(); I != E; ++I) {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000637 SymbolBody *B = I->first;
638 size_t StrOff = I->second;
639 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
640 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000641
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000642 unsigned NBuckets = calcNBuckets(Symbols.size());
643 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000644 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000645 return L.Hash % NBuckets < R.Hash % NBuckets;
646 });
647
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000648 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000649 for (const SymbolData &Sym : Symbols)
650 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000651}
652
George Rimard3566302016-06-20 11:55:12 +0000653// Returns the number of version definition entries. Because the first entry
654// is for the version definition itself, it is the number of versioned symbols
655// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000656static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000657
Igor Kudrinf1d60292015-10-28 07:05:56 +0000658template <class ELFT>
Rui Ueyamaace4f902016-05-24 04:25:47 +0000659DynamicSection<ELFT>::DynamicSection()
660 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000661 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000662 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000663 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000664
665 // .dynamic section is not writable on MIPS.
666 // See "Special Section" in Chapter 4 in the following document:
667 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
668 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000669 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000670}
671
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000672template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000673 if (this->Header.sh_size)
674 return; // Already finalized.
675
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000676 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000677 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000678
Rafael Espindolae2c24612016-01-29 01:24:25 +0000679 auto Add = [=](Entry E) { Entries.push_back(E); };
680
681 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000682 // DynStrTab and doing this here allows this function to set DT_STRSZ.
George Rimarb952ece2016-09-02 09:13:05 +0000683 for (StringRef S : Config->AuxiliaryList)
684 Add({DT_AUXILIARY, Out<ELFT>::DynStrTab->addString(S)});
Rafael Espindolade069362016-01-25 21:32:04 +0000685 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000686 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
687 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000688 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
Rafael Espindolade069362016-01-25 21:32:04 +0000689 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000690 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
691 if (!Config->SoName.empty())
692 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
693
Rafael Espindolade069362016-01-25 21:32:04 +0000694 Out<ELFT>::DynStrTab->finalize();
695
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000696 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000697 bool IsRela = Config->Rela;
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000698 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
699 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
700 Add({IsRela ? DT_RELAENT : DT_RELENT,
701 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Eugene Leviantaa498192016-08-31 08:51:39 +0000702
Simon Atanasyan65b24612016-09-04 17:40:12 +0000703 // MIPS dynamic loader does not support RELCOUNT tag.
704 // The problem is in the tight relation between dynamic
705 // relocations and GOT. So do not emit this tag on MIPS.
706 if (Config->EMachine != EM_MIPS) {
707 size_t NumRelativeRels = Out<ELFT>::RelaDyn->getRelativeRelocCount();
708 if (Config->ZCombreloc && NumRelativeRels)
709 Add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
710 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000711 }
George Rimar648a2c32015-10-20 08:54:27 +0000712 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000713 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
714 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
715 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
716 Out<ELFT>::GotPlt});
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000717 Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000718 }
719
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000720 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
721 Add({DT_SYMENT, sizeof(Elf_Sym)});
722 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
723 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000724 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000725 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000726 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000727 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000728
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000729 if (Out<ELFT>::PreinitArray) {
730 Add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
George Rimar03e05602016-08-19 15:23:39 +0000731 Add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
Rafael Espindolade069362016-01-25 21:32:04 +0000732 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000733 if (Out<ELFT>::InitArray) {
734 Add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
George Rimar03e05602016-08-19 15:23:39 +0000735 Add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
Rafael Espindolade069362016-01-25 21:32:04 +0000736 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000737 if (Out<ELFT>::FiniArray) {
738 Add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
George Rimar03e05602016-08-19 15:23:39 +0000739 Add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000740 }
741
Rui Ueyamaace4f902016-05-24 04:25:47 +0000742 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000743 Add({DT_INIT, B});
Rui Ueyamaace4f902016-05-24 04:25:47 +0000744 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000745 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000746
Rafael Espindolade069362016-01-25 21:32:04 +0000747 uint32_t DtFlags = 0;
748 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000749 if (Config->Bsymbolic)
750 DtFlags |= DF_SYMBOLIC;
751 if (Config->ZNodelete)
752 DtFlags1 |= DF_1_NODELETE;
753 if (Config->ZNow) {
754 DtFlags |= DF_BIND_NOW;
755 DtFlags1 |= DF_1_NOW;
756 }
757 if (Config->ZOrigin) {
758 DtFlags |= DF_ORIGIN;
759 DtFlags1 |= DF_1_ORIGIN;
760 }
761
762 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000763 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000764 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000765 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000766
Ed Mastef5d3cf62016-01-06 15:52:27 +0000767 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000768 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000769
George Rimard3566302016-06-20 11:55:12 +0000770 bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0;
771 if (HasVerNeed || Out<ELFT>::VerDef)
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000772 Add({DT_VERSYM, Out<ELFT>::VerSym});
George Rimard3566302016-06-20 11:55:12 +0000773 if (Out<ELFT>::VerDef) {
774 Add({DT_VERDEF, Out<ELFT>::VerDef});
775 Add({DT_VERDEFNUM, getVerDefNum()});
776 }
777 if (HasVerNeed) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000778 Add({DT_VERNEED, Out<ELFT>::VerNeed});
George Rimard3566302016-06-20 11:55:12 +0000779 Add({DT_VERNEEDNUM, Out<ELFT>::VerNeed->getNeedNum()});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000780 }
781
Igor Kudrin304860a2015-11-12 04:39:49 +0000782 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000783 Add({DT_MIPS_RLD_VERSION, 1});
784 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
Rui Ueyamacafc0f2e2016-07-14 17:40:18 +0000785 Add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000786 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
787 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000788 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000789 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000790 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000791 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
792 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000793 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000794 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000795 }
796
Rafael Espindolade069362016-01-25 21:32:04 +0000797 // +1 for DT_NULL
798 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000799}
800
801template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000802 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
803
Rafael Espindolade069362016-01-25 21:32:04 +0000804 for (const Entry &E : Entries) {
805 P->d_tag = E.Tag;
806 switch (E.Kind) {
807 case Entry::SecAddr:
808 P->d_un.d_ptr = E.OutSec->getVA();
809 break;
George Rimar03e05602016-08-19 15:23:39 +0000810 case Entry::SecSize:
811 P->d_un.d_val = E.OutSec->getSize();
812 break;
Rafael Espindolade069362016-01-25 21:32:04 +0000813 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000814 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000815 break;
816 case Entry::PlainInt:
817 P->d_un.d_val = E.Val;
818 break;
819 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000820 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000821 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000822}
823
824template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000825EhFrameHeader<ELFT>::EhFrameHeader()
Rui Ueyamade9777a2016-05-23 16:30:41 +0000826 : OutputSectionBase<ELFT>(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000827
Rui Ueyama95a232e2016-05-23 01:45:05 +0000828// .eh_frame_hdr contains a binary search table of pointers to FDEs.
829// Each entry of the search table consists of two values,
830// the starting PC from where FDEs covers, and the FDE's address.
831// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000832template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
833 const endianness E = ELFT::TargetEndianness;
834
Rui Ueyamae75e9332016-05-23 03:00:33 +0000835 // Sort the FDE list by their PC and uniqueify. Usually there is only
836 // one FDE for a PC (i.e. function), but if ICF merges two functions
837 // into one, there can be more than one FDEs pointing to the address.
838 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
839 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
840 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
841 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000842
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000843 Buf[0] = 1;
844 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
845 Buf[2] = DW_EH_PE_udata4;
846 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000847 write32<E>(Buf + 4, Out<ELFT>::EhFrame->getVA() - this->getVA() - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000848 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000849 Buf += 12;
850
Rui Ueyamae75e9332016-05-23 03:00:33 +0000851 uintX_t VA = this->getVA();
852 for (FdeData &Fde : Fdes) {
853 write32<E>(Buf, Fde.Pc - VA);
854 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000855 Buf += 8;
856 }
857}
858
Rui Ueyamade9777a2016-05-23 16:30:41 +0000859template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
860 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
861 this->Header.sh_size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
862}
863
George Rimarf6bc65a2016-01-15 13:34:52 +0000864template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000865void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
866 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000867}
868
George Rimarf6bc65a2016-01-15 13:34:52 +0000869template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000870OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
871 : OutputSectionBase<ELFT>(Name, Type, Flags) {
872 if (Type == SHT_RELA)
873 this->Header.sh_entsize = sizeof(Elf_Rela);
874 else if (Type == SHT_REL)
875 this->Header.sh_entsize = sizeof(Elf_Rel);
876}
877
878template <class ELFT> void OutputSection<ELFT>::finalize() {
879 uint32_t Type = this->Header.sh_type;
880 if (Type != SHT_RELA && Type != SHT_REL)
881 return;
882 this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex;
883 // sh_info for SHT_REL[A] sections should contain the section header index of
884 // the section to which the relocation applies.
885 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
886 this->Header.sh_info = S->OutSec->SectionIndex;
887}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000888
889template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000890void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000891 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000892 auto *S = cast<InputSection<ELFT>>(C);
893 Sections.push_back(S);
894 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000895 this->updateAlignment(S->Alignment);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000896}
897
Rui Ueyamac4185702016-02-10 23:20:42 +0000898// If an input string is in the form of "foo.N" where N is a number,
899// return N. Otherwise, returns 65536, which is one greater than the
900// lowest priority.
901static int getPriority(StringRef S) {
902 size_t Pos = S.rfind('.');
903 if (Pos == StringRef::npos)
904 return 65536;
905 int V;
906 if (S.substr(Pos + 1).getAsInteger(10, V))
907 return 65536;
908 return V;
909}
910
Rui Ueyama809d8e22016-06-23 04:33:42 +0000911// This function is called after we sort input sections
912// and scan relocations to setup sections' offsets.
913template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
914 uintX_t Off = this->Header.sh_size;
915 for (InputSection<ELFT> *S : Sections) {
916 Off = alignTo(Off, S->Alignment);
917 S->OutSecOff = Off;
918 Off += S->getSize();
919 }
920 this->Header.sh_size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000921}
922
Rui Ueyamac4185702016-02-10 23:20:42 +0000923// Sorts input sections by section name suffixes, so that .foo.N comes
924// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000925// We want to keep the original order if the priorities are the same
926// because the compiler keeps the original initialization order in a
927// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000928// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000929template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000930 // Sort sections by priority.
931 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000932 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
933
Rui Ueyamac4185702016-02-10 23:20:42 +0000934 std::vector<Pair> V;
935 for (InputSection<ELFT> *S : Sections)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000936 V.push_back({getPriority(S->Name), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000937 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000938 Sections.clear();
939 for (Pair &P : V)
940 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000941}
Rui Ueyamac4185702016-02-10 23:20:42 +0000942
Rui Ueyama5af83682016-02-11 23:41:38 +0000943// Returns true if S matches /Filename.?\.o$/.
944static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
945 if (!S.endswith(".o"))
946 return false;
947 S = S.drop_back(2);
948 if (S.endswith(Filename))
949 return true;
950 return !S.empty() && S.drop_back().endswith(Filename);
951}
952
953static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
954static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
955
956// .ctors and .dtors are sorted by this priority from highest to lowest.
957//
958// 1. The section was contained in crtbegin (crtbegin contains
959// some sentinel value in its .ctors and .dtors so that the runtime
960// can find the beginning of the sections.)
961//
962// 2. The section has an optional priority value in the form of ".ctors.N"
963// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
964// they are compared as string rather than number.
965//
966// 3. The section is just ".ctors" or ".dtors".
967//
968// 4. The section was contained in crtend, which contains an end marker.
969//
970// In an ideal world, we don't need this function because .init_array and
971// .ctors are duplicate features (and .init_array is newer.) However, there
972// are too many real-world use cases of .ctors, so we had no choice to
973// support that with this rather ad-hoc semantics.
974template <class ELFT>
975static bool compCtors(const InputSection<ELFT> *A,
976 const InputSection<ELFT> *B) {
977 bool BeginA = isCrtbegin(A->getFile()->getName());
978 bool BeginB = isCrtbegin(B->getFile()->getName());
979 if (BeginA != BeginB)
980 return BeginA;
981 bool EndA = isCrtend(A->getFile()->getName());
982 bool EndB = isCrtend(B->getFile()->getName());
983 if (EndA != EndB)
984 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000985 StringRef X = A->Name;
986 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000987 assert(X.startswith(".ctors") || X.startswith(".dtors"));
988 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
989 X = X.substr(6);
990 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000991 if (X.empty() && Y.empty())
992 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000993 return X < Y;
994}
995
996// Sorts input sections by the special rules for .ctors and .dtors.
997// Unfortunately, the rules are different from the one for .{init,fini}_array.
998// Read the comment above.
999template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
1000 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +00001001}
1002
George Rimare2ee72b2016-02-26 14:48:31 +00001003static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
1004 size_t I = 0;
1005 for (; I + A.size() < Size; I += A.size())
1006 memcpy(Buf + I, A.data(), A.size());
1007 memcpy(Buf + I, A.data(), Size - I);
1008}
1009
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001010template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +00001011 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +00001012 if (!Filler.empty())
1013 fill(Buf, this->getSize(), Filler);
Rui Ueyamae9809502016-03-11 04:23:12 +00001014 if (Config->Threads) {
1015 parallel_for_each(Sections.begin(), Sections.end(),
1016 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
1017 } else {
1018 for (InputSection<ELFT> *C : Sections)
1019 C->writeTo(Buf);
1020 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001021}
1022
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001023template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +00001024EhOutputSection<ELFT>::EhOutputSection()
Rui Ueyama3b31e672016-05-23 16:24:16 +00001025 : OutputSectionBase<ELFT>(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001026
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001027// Search for an existing CIE record or create a new one.
1028// CIE records from input object files are uniquified by their contents
1029// and where their relocations point to.
1030template <class ELFT>
1031template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001032CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001033 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001034 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001035 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +00001036 if (read32<E>(Piece.data().data() + 4) != 0)
Rafael Espindola042a3f22016-09-08 14:06:08 +00001037 fatal("CIE expected at beginning of .eh_frame: " + Sec->Name);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001038
1039 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001040 unsigned FirstRelI = Piece.FirstRelocation;
1041 if (FirstRelI != (unsigned)-1)
1042 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001043
1044 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +00001045 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001046
1047 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001048 if (Cie->Piece == nullptr) {
1049 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001050 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001051 }
1052 return Cie;
1053}
1054
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001055// There is one FDE per function. Returns true if a given FDE
1056// points to a live function.
1057template <class ELFT>
1058template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001059bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001060 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001061 ArrayRef<RelTy> Rels) {
1062 unsigned FirstRelI = Piece.FirstRelocation;
1063 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001064 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +00001065 const RelTy &Rel = Rels[FirstRelI];
1066 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001067 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
1068 if (!D || !D->Section)
1069 return false;
1070 InputSectionBase<ELFT> *Target = D->Section->Repl;
1071 return Target && Target->Live;
1072}
1073
1074// .eh_frame is a sequence of CIE or FDE records. In general, there
1075// is one CIE record per input object file which is followed by
1076// a list of FDEs. This function searches an existing CIE or create a new
1077// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +00001078template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +00001079template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001080void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +00001081 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001082 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +00001083
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001084 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001085 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001086 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +00001087 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +00001088 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001089
1090 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001091 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001092 if (ID == 0) {
1093 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
1094 continue;
1095 }
1096
1097 uint32_t CieOffset = Offset + 4 - ID;
1098 CieRecord *Cie = OffsetToCie[CieOffset];
1099 if (!Cie)
1100 fatal("invalid CIE reference");
1101
1102 if (!isFdeLive(Piece, Sec, Rels))
1103 continue;
1104 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +00001105 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001106 }
1107}
1108
1109template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001110void EhOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001111 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001112 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001113 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001114 Sections.push_back(Sec);
1115
1116 // .eh_frame is a sequence of CIE or FDE records. This function
1117 // splits it into pieces so that we can call
1118 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +00001119 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001120 if (Sec->Pieces.empty())
1121 return;
1122
1123 if (const Elf_Shdr *RelSec = Sec->RelocSection) {
1124 ELFFile<ELFT> &Obj = Sec->getFile()->getObj();
1125 if (RelSec->sh_type == SHT_RELA)
1126 addSectionAux(Sec, Obj.relas(RelSec));
1127 else
1128 addSectionAux(Sec, Obj.rels(RelSec));
Rui Ueyama0de86c12016-01-27 22:23:44 +00001129 return;
1130 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001131 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001132}
1133
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001134template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +00001135static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
1136 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +00001137
1138 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001139 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +00001140 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +00001141}
1142
Rui Ueyama1e479c22016-05-23 15:07:59 +00001143template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rui Ueyamae9381bd2016-07-16 02:47:42 +00001144 if (this->Header.sh_size)
1145 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +00001146
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001147 size_t Off = 0;
1148 for (CieRecord *Cie : Cies) {
1149 Cie->Piece->OutputOff = Off;
1150 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001151
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001152 for (SectionPiece *Fde : Cie->FdePieces) {
1153 Fde->OutputOff = Off;
1154 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001155 }
1156 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001157 this->Header.sh_size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001158}
1159
Rui Ueyamae75e9332016-05-23 03:00:33 +00001160template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
1161 const endianness E = ELFT::TargetEndianness;
1162 switch (Size) {
1163 case DW_EH_PE_udata2:
1164 return read16<E>(Buf);
1165 case DW_EH_PE_udata4:
1166 return read32<E>(Buf);
1167 case DW_EH_PE_udata8:
1168 return read64<E>(Buf);
1169 case DW_EH_PE_absptr:
1170 if (ELFT::Is64Bits)
1171 return read64<E>(Buf);
1172 return read32<E>(Buf);
1173 }
1174 fatal("unknown FDE size encoding");
1175}
1176
1177// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
1178// We need it to create .eh_frame_hdr section.
1179template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001180typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +00001181 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001182 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +00001183 // stored at FDE + 8 byte.
1184 size_t Off = FdeOff + 8;
1185 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
1186 if ((Enc & 0x70) == DW_EH_PE_absptr)
1187 return Addr;
1188 if ((Enc & 0x70) == DW_EH_PE_pcrel)
1189 return Addr + this->getVA() + Off;
1190 fatal("unknown FDE size relative encoding");
1191}
1192
Rui Ueyama1e479c22016-05-23 15:07:59 +00001193template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001194 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001195 for (CieRecord *Cie : Cies) {
1196 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001197 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001198
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001199 for (SectionPiece *Fde : Cie->FdePieces) {
1200 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001201 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +00001202
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001203 // FDE's second word should have the offset to an associated CIE.
1204 // Write it.
1205 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001206 }
1207 }
1208
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001209 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +00001210 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +00001211
1212 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001213 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +00001214 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +00001215 if (Out<ELFT>::EhFrameHdr) {
1216 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +00001217 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +00001218 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +00001219 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rui Ueyama3b31e672016-05-23 16:24:16 +00001220 uintX_t FdeVA = this->getVA() + Fde->OutputOff;
1221 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
1222 }
Rui Ueyamae75e9332016-05-23 03:00:33 +00001223 }
1224 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001225}
1226
1227template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001228MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001229 uintX_t Flags, uintX_t Alignment)
1230 : OutputSectionBase<ELFT>(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001231 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001232
1233template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001234 if (shouldTailMerge()) {
1235 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001236 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001237 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001238 }
Rui Ueyama31f9f612016-05-06 00:52:08 +00001239 for (const std::pair<CachedHash<StringRef>, size_t> &P : Builder.getMap()) {
1240 StringRef Data = P.first.Val;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001241 memcpy(Buf + P.second, Data.data(), Data.size());
1242 }
1243}
1244
Rui Ueyama34dc99e2016-05-22 01:15:32 +00001245static StringRef toStringRef(ArrayRef<uint8_t> A) {
1246 return {(const char *)A.data(), A.size()};
1247}
1248
Rafael Espindolac159c962015-10-19 21:00:02 +00001249template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001250void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama10803512016-05-22 00:25:30 +00001251 auto *Sec = cast<MergeInputSection<ELFT>>(C);
1252 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001253 this->updateAlignment(Sec->Alignment);
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001254 this->Header.sh_entsize = Sec->getSectionHdr()->sh_entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +00001255 Sections.push_back(Sec);
Rafael Espindolac159c962015-10-19 21:00:02 +00001256
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001257 bool IsString = this->Header.sh_flags & SHF_STRINGS;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001258
Rui Ueyamab7eda282016-05-24 02:10:28 +00001259 for (SectionPiece &Piece : Sec->Pieces) {
Rui Ueyama3ea87272016-05-22 00:13:04 +00001260 if (!Piece.Live)
Rafael Espindola0b9531c2016-04-22 22:09:35 +00001261 continue;
Rui Ueyamad8849272016-05-25 16:37:01 +00001262 uintX_t OutputOffset = Builder.add(toStringRef(Piece.data()));
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001263 if (!IsString || !shouldTailMerge())
1264 Piece.OutputOff = OutputOffset;
Rafael Espindolac159c962015-10-19 21:00:02 +00001265 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001266}
1267
1268template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001269unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1270 return Builder.getOffset(Val);
1271}
1272
1273template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1274 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1275}
1276
1277template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1278 if (shouldTailMerge())
1279 Builder.finalize();
1280 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001281}
1282
Rui Ueyama406b4692016-05-27 14:39:13 +00001283template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() {
1284 for (MergeInputSection<ELFT> *Sec : Sections)
1285 Sec->finalizePieces();
1286}
1287
Rafael Espindolac159c962015-10-19 21:00:02 +00001288template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001289StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001290 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1291 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyama3b04d832016-07-14 05:46:24 +00001292 Dynamic(Dynamic) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +00001293
Rafael Espindolae2c24612016-01-29 01:24:25 +00001294// Adds a string to the string table. If HashIt is true we hash and check for
1295// duplicates. It is optional because the name of global symbols are already
1296// uniqued and hashing them again has a big cost for a small value: uniquing
1297// them with some other string that happens to be the same.
1298template <class ELFT>
1299unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1300 if (HashIt) {
1301 auto R = StringMap.insert(std::make_pair(S, Size));
1302 if (!R.second)
1303 return R.first->second;
1304 }
1305 unsigned Ret = Size;
1306 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001307 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001308 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001309}
1310
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001311template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001312 // ELF string tables start with NUL byte, so advance the pointer by one.
1313 ++Buf;
1314 for (StringRef S : Strings) {
1315 memcpy(Buf, S.data(), S.size());
1316 Buf += S.size() + 1;
1317 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001318}
1319
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001320template <class ELFT>
Rui Ueyama809d8e22016-06-23 04:33:42 +00001321typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
1322 if (OutputSec)
1323 return OutputSec->getVA() + OffsetInSec;
George Rimar3e6833b2016-08-19 15:46:28 +00001324 return InputSec->OutSec->getVA() + InputSec->getOffset(OffsetInSec);
Rui Ueyama809d8e22016-06-23 04:33:42 +00001325}
1326
1327template <class ELFT>
1328typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
1329 if (UseSymVA)
1330 return Sym->getVA<ELFT>(Addend);
1331 return Addend;
1332}
1333
1334template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
1335 if (Sym && !UseSymVA)
1336 return Sym->DynsymIndex;
1337 return 0;
1338}
1339
1340template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001341SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamaace4f902016-05-24 04:25:47 +00001342 StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001343 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1344 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001345 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyamaace4f902016-05-24 04:25:47 +00001346 StrTabSec(StrTabSec) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001347 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001348 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001349}
1350
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001351// Orders symbols according to their positions in the GOT,
1352// in compliance with MIPS ABI rules.
1353// See "Global Offset Table" in Chapter 5 in the following document
1354// for detailed description:
1355// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001356static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1357 const std::pair<SymbolBody *, unsigned> &R) {
Simon Atanasyand2980d32016-03-29 14:07:22 +00001358 // Sort entries related to non-local preemptible symbols by GOT indexes.
1359 // All other entries go to the first part of GOT in arbitrary order.
Simon Atanasyan7b8481b2016-06-20 11:37:56 +00001360 bool LIsInLocalGot = !L.first->IsInGlobalMipsGot;
1361 bool RIsInLocalGot = !R.first->IsInGlobalMipsGot;
1362 if (LIsInLocalGot || RIsInLocalGot)
1363 return !RIsInLocalGot;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001364 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001365}
1366
Rafael Espindolabadd3972016-04-08 15:30:56 +00001367static uint8_t getSymbolBinding(SymbolBody *Body) {
Peter Collingbourne4f952702016-05-01 04:55:03 +00001368 Symbol *S = Body->symbol();
George Rimar11f83b72016-08-19 08:31:02 +00001369 if (Config->Relocatable)
1370 return S->Binding;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001371 uint8_t Visibility = S->Visibility;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001372 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1373 return STB_LOCAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001374 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
Rafael Espindolabadd3972016-04-08 15:30:56 +00001375 return STB_GLOBAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001376 return S->Binding;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001377}
1378
Rui Ueyama0db335f2015-10-07 16:58:54 +00001379template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001380 if (this->Header.sh_size)
1381 return; // Already finalized.
1382
Rui Ueyama0db335f2015-10-07 16:58:54 +00001383 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001384 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001385 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001386
George Rimar58941ee2016-02-25 08:23:37 +00001387 if (Config->Relocatable) {
1388 size_t I = NumLocals;
1389 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
1390 P.first->DynsymIndex = ++I;
1391 return;
1392 }
1393
Igor Kudrinab665fc2015-10-20 21:47:58 +00001394 if (!StrTabSec.isDynamic()) {
1395 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001396 [](const std::pair<SymbolBody *, unsigned> &L,
1397 const std::pair<SymbolBody *, unsigned> &R) {
1398 return getSymbolBinding(L.first) == STB_LOCAL &&
1399 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001400 });
1401 return;
1402 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001403 if (Out<ELFT>::GnuHashTab)
1404 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1405 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001406 else if (Config->EMachine == EM_MIPS)
1407 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001408 size_t I = 0;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001409 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001410 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001411}
1412
1413template <class ELFT>
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001414void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1415 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001416}
1417
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001418template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001419 Buf += sizeof(Elf_Sym);
1420
1421 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1422 // .dynsym only contains global symbols.
George Rimar9503f6d2016-08-31 08:46:30 +00001423 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001424 writeLocalSymbols(Buf);
1425
1426 writeGlobalSymbols(Buf);
1427}
1428
1429template <class ELFT>
1430void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1431 // Iterate over all input object files to copy their local symbols
1432 // to the output symbol table pointed by Buf.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +00001433 for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001434 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1435 File->KeptLocalSyms) {
1436 const DefinedRegular<ELFT> &Body = *P.first;
1437 InputSectionBase<ELFT> *Section = Body.Section;
Rui Ueyamac55733e2015-09-30 00:54:29 +00001438 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001439
1440 if (!Section) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001441 ESym->st_shndx = SHN_ABS;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001442 ESym->st_value = Body.Value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001443 } else {
Rafael Espindolac159c962015-10-19 21:00:02 +00001444 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1445 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001446 ESym->st_value = OutSec->getVA() + Section->getOffset(Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001447 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001448 ESym->st_name = P.second;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001449 ESym->st_size = Body.template getSize<ELFT>();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001450 ESym->setBindingAndType(STB_LOCAL, Body.Type);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001451 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001452 }
1453 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001454}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001455
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001456template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001457void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001458 // Write the internal symbol table contents to the output symbol table
1459 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001460 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001461 for (const std::pair<SymbolBody *, size_t> &P : Symbols) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001462 SymbolBody *Body = P.first;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001463 size_t StrOff = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001464
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001465 uint8_t Type = Body->Type;
1466 uintX_t Size = Body->getSize<ELFT>();
Rafael Espindola8614c562015-10-06 14:33:58 +00001467
Igor Kudrin853b88d2015-10-20 20:52:14 +00001468 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001469 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001470 ESym->st_name = StrOff;
Peter Collingbourne4f952702016-05-01 04:55:03 +00001471 ESym->setVisibility(Body->symbol()->Visibility);
Rui Ueyamab5a69702016-02-01 21:00:35 +00001472 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001473
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001474 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001475 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001476 else if (isa<DefinedRegular<ELFT>>(Body))
1477 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +00001478
1479 // On MIPS we need to mark symbol which has a PLT entry and requires pointer
1480 // equality by STO_MIPS_PLT flag. That is necessary to help dynamic linker
1481 // distinguish such symbols and MIPS lazy-binding stubs.
1482 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1483 if (Config->EMachine == EM_MIPS && Body->isInPlt() &&
1484 Body->NeedsCopyOrPltAddr)
Simon Atanasyanbea96982016-02-25 21:09:05 +00001485 ESym->st_other |= STO_MIPS_PLT;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001486 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001487 }
1488}
1489
Igor Kudrin853b88d2015-10-20 20:52:14 +00001490template <class ELFT>
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001491const OutputSectionBase<ELFT> *
1492SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1493 switch (Sym->kind()) {
1494 case SymbolBody::DefinedSyntheticKind:
Peter Collingbourne6a422592016-05-03 01:21:08 +00001495 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001496 case SymbolBody::DefinedRegularKind: {
Rafael Espindola5ffa13c2016-04-15 00:15:02 +00001497 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +00001498 if (D.Section)
1499 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001500 break;
1501 }
1502 case SymbolBody::DefinedCommonKind:
Rui Ueyama07784902016-08-02 01:35:13 +00001503 return CommonInputSection<ELFT>::X->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001504 case SymbolBody::SharedKind:
1505 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1506 return Out<ELFT>::Bss;
1507 break;
Peter Collingbourne60976ed2016-04-27 00:05:06 +00001508 case SymbolBody::UndefinedKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001509 case SymbolBody::LazyArchiveKind:
1510 case SymbolBody::LazyObjectKind:
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001511 break;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001512 }
1513 return nullptr;
1514}
1515
1516template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +00001517VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rui Ueyamaf5244642016-07-16 02:36:00 +00001518 : OutputSectionBase<ELFT>(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
1519 this->Header.sh_addralign = sizeof(uint32_t);
1520}
George Rimard3566302016-06-20 11:55:12 +00001521
1522static StringRef getFileDefName() {
1523 if (!Config->SoName.empty())
1524 return Config->SoName;
1525 return Config->OutputFile;
1526}
1527
1528template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1529 FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001530 for (VersionDefinition &V : Config->VersionDefinitions)
George Rimard3566302016-06-20 11:55:12 +00001531 V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name);
1532
1533 this->Header.sh_size =
1534 (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1535 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +00001536
1537 // sh_info should be set to the number of definitions. This fact is missed in
1538 // documentation, but confirmed by binutils community:
1539 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1540 this->Header.sh_info = getVerDefNum();
1541}
1542
Rui Ueyama9f619642016-07-16 02:29:45 +00001543template <class ELFT>
1544void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1545 StringRef Name, size_t NameOff) {
1546 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +00001547 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +00001548 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +00001549 Verdef->vd_aux = sizeof(Elf_Verdef);
1550 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1551 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +00001552 Verdef->vd_ndx = Index;
1553 Verdef->vd_hash = hashSysv(Name);
George Rimard3566302016-06-20 11:55:12 +00001554
Rui Ueyama9f619642016-07-16 02:29:45 +00001555 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1556 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +00001557 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001558}
1559
1560template <class ELFT>
1561void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001562 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1563
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001564 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001565 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1566 writeOne(Buf, V.Id, V.Name, V.NameOff);
1567 }
1568
1569 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +00001570 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +00001571 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001572}
1573
1574template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001575VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001576 : OutputSectionBase<ELFT>(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001577 this->Header.sh_addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001578}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001579
1580template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1581 this->Header.sh_size =
1582 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1);
1583 this->Header.sh_entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +00001584 // At the moment of june 2016 GNU docs does not mention that sh_link field
1585 // should be set, but Sun docs do. Also readelf relies on this field.
1586 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001587}
1588
1589template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1590 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1591 for (const std::pair<SymbolBody *, size_t> &P :
1592 Out<ELFT>::DynSymTab->getSymbols()) {
George Rimard3566302016-06-20 11:55:12 +00001593 OutVersym->vs_index = P.first->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001594 ++OutVersym;
1595 }
1596}
1597
1598template <class ELFT>
1599VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001600 : OutputSectionBase<ELFT>(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001601 this->Header.sh_addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +00001602
1603 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1604 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1605 // First identifiers are reserved by verdef section if it exist.
1606 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001607}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001608
1609template <class ELFT>
1610void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1611 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +00001612 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001613 return;
1614 }
Rui Ueyama434b5612016-07-17 03:11:46 +00001615 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001616 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1617 // to create one by adding it to our needed list and creating a dynstr entry
1618 // for the soname.
1619 if (F->VerdefMap.empty())
1620 Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())});
1621 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1622 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1623 // prepare to create one by allocating a version identifier and creating a
1624 // dynstr entry for the version name.
1625 if (NV.Index == 0) {
1626 NV.StrTab = Out<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +00001627 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001628 NV.Index = NextIndex++;
1629 }
George Rimard3566302016-06-20 11:55:12 +00001630 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001631}
1632
1633template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1634 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1635 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1636 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1637
1638 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1639 // Create an Elf_Verneed for this DSO.
1640 Verneed->vn_version = 1;
1641 Verneed->vn_cnt = P.first->VerdefMap.size();
1642 Verneed->vn_file = P.second;
1643 Verneed->vn_aux =
1644 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1645 Verneed->vn_next = sizeof(Elf_Verneed);
1646 ++Verneed;
1647
1648 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1649 // VerdefMap, which will only contain references to needed version
1650 // definitions. Each Elf_Vernaux is based on the information contained in
1651 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1652 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1653 // data structures within a single input file.
1654 for (auto &NV : P.first->VerdefMap) {
1655 Vernaux->vna_hash = NV.first->vd_hash;
1656 Vernaux->vna_flags = 0;
1657 Vernaux->vna_other = NV.second.Index;
1658 Vernaux->vna_name = NV.second.StrTab;
1659 Vernaux->vna_next = sizeof(Elf_Vernaux);
1660 ++Vernaux;
1661 }
1662
1663 Vernaux[-1].vna_next = 0;
1664 }
1665 Verneed[-1].vn_next = 0;
1666}
1667
1668template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1669 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
1670 this->Header.sh_info = Needed.size();
1671 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1672 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1673 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1674 this->Header.sh_size = Size;
1675}
1676
1677template <class ELFT>
Rui Ueyama3a41be22016-04-07 22:49:21 +00001678BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
1679 : OutputSectionBase<ELFT>(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC),
1680 HashSize(HashSize) {
1681 // 16 bytes for the note section header.
1682 this->Header.sh_size = 16 + HashSize;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001683}
1684
1685template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
1686 const endianness E = ELFT::TargetEndianness;
1687 write32<E>(Buf, 4); // Name size
Rui Ueyama3a41be22016-04-07 22:49:21 +00001688 write32<E>(Buf + 4, HashSize); // Content size
Rui Ueyama634ddf02016-03-11 20:51:53 +00001689 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
1690 memcpy(Buf + 12, "GNU", 4); // Name string
1691 HashBuf = Buf + 16;
1692}
1693
Rafael Espindolad88d7162016-09-14 11:32:57 +00001694static uint64_t murmurHash64A(const void *Key, int Len) {
1695 uint64_t Seed = 0;
1696 const uint64_t M = 0xc6a4a7935bd1e995LLU;
1697 const int R = 47;
1698
1699 uint64_t H = Seed ^ (Len * M);
1700
1701 const uint64_t *Data = (const uint64_t *)Key;
1702 const uint64_t *End = Data + (Len / 8);
1703
1704 while (Data != End) {
1705 uint64_t K = *Data++;
1706
1707 K *= M;
1708 K ^= K >> R;
1709 K *= M;
1710
1711 H ^= K;
1712 H *= M;
1713 }
1714
1715 const unsigned char *Data2 = (const unsigned char *)Data;
1716 switch (Len & 7) {
1717 case 7:
1718 H ^= uint64_t(Data2[6]) << 48;
1719 case 6:
1720 H ^= uint64_t(Data2[5]) << 40;
1721 case 5:
1722 H ^= uint64_t(Data2[4]) << 32;
1723 case 4:
1724 H ^= uint64_t(Data2[3]) << 24;
1725 case 3:
1726 H ^= uint64_t(Data2[2]) << 16;
1727 case 2:
1728 H ^= uint64_t(Data2[1]) << 8;
1729 case 1:
1730 H ^= uint64_t(Data2[0]);
1731 H *= M;
1732 };
1733
1734 H ^= H >> R;
1735 H *= M;
1736 H ^= H >> R;
1737
1738 return H;
1739}
1740
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001741template <class ELFT>
Rafael Espindolad88d7162016-09-14 11:32:57 +00001742void BuildIdFastHash<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyama634ddf02016-03-11 20:51:53 +00001743 const endianness E = ELFT::TargetEndianness;
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001744
Rafael Espindolad88d7162016-09-14 11:32:57 +00001745 // 64-bit murmur2 hash
1746 uint64_t Hash = murmurHash64A(Buf.data(), Buf.size());
Rui Ueyama3a41be22016-04-07 22:49:21 +00001747 write64<E>(this->HashBuf, Hash);
1748}
1749
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001750template <class ELFT>
Petr Hosekfdfcb792016-09-01 22:43:03 +00001751void BuildIdMd5<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001752 MD5 Hash;
Petr Hosekfdfcb792016-09-01 22:43:03 +00001753 Hash.update(Buf);
Rui Ueyama3a41be22016-04-07 22:49:21 +00001754 MD5::MD5Result Res;
1755 Hash.final(Res);
1756 memcpy(this->HashBuf, Res, 16);
Rui Ueyama634ddf02016-03-11 20:51:53 +00001757}
1758
Rui Ueyamadd368fc2016-05-02 23:35:59 +00001759template <class ELFT>
Petr Hosekfdfcb792016-09-01 22:43:03 +00001760void BuildIdSha1<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001761 SHA1 Hash;
Petr Hosekfdfcb792016-09-01 22:43:03 +00001762 Hash.update(Buf);
Rui Ueyamad86ec302016-04-07 23:51:56 +00001763 memcpy(this->HashBuf, Hash.final().data(), 20);
1764}
1765
Rui Ueyama634ddf02016-03-11 20:51:53 +00001766template <class ELFT>
Petr Hosekfdfcb792016-09-01 22:43:03 +00001767void BuildIdUuid<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Eugene Leviant933dae72016-08-26 09:55:37 +00001768 if (getRandomBytes(this->HashBuf, 16))
1769 error("entropy source failure");
1770}
1771
1772template <class ELFT>
Rui Ueyama9194db72016-05-13 21:55:56 +00001773BuildIdHexstring<ELFT>::BuildIdHexstring()
1774 : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
1775
1776template <class ELFT>
Petr Hosekfdfcb792016-09-01 22:43:03 +00001777void BuildIdHexstring<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyama9194db72016-05-13 21:55:56 +00001778 memcpy(this->HashBuf, Config->BuildIdVector.data(),
1779 Config->BuildIdVector.size());
1780}
1781
1782template <class ELFT>
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001783MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1784 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1785 this->Header.sh_addralign = 4;
1786 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1787 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1788}
1789
1790template <class ELFT>
1791void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1792 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
Simon Atanasyan4ee29182016-04-27 05:31:28 +00001793 R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
Rui Ueyama70eed362016-01-06 22:42:43 +00001794 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001795}
1796
1797template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001798void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001799 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001800 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001801 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan84bb3552016-05-26 20:46:01 +00001802 S->OutSec = this;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001803}
1804
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001805template <class ELFT>
1806MipsOptionsOutputSection<ELFT>::MipsOptionsOutputSection()
1807 : OutputSectionBase<ELFT>(".MIPS.options", SHT_MIPS_OPTIONS,
1808 SHF_ALLOC | SHF_MIPS_NOSTRIP) {
1809 this->Header.sh_addralign = 8;
1810 this->Header.sh_entsize = 1;
1811 this->Header.sh_size = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
1812}
1813
1814template <class ELFT>
1815void MipsOptionsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1816 auto *Opt = reinterpret_cast<Elf_Mips_Options *>(Buf);
1817 Opt->kind = ODK_REGINFO;
1818 Opt->size = this->Header.sh_size;
1819 Opt->section = 0;
1820 Opt->info = 0;
1821 auto *Reg = reinterpret_cast<Elf_Mips_RegInfo *>(Buf + sizeof(*Opt));
1822 Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
1823 Reg->ri_gprmask = GprMask;
1824}
1825
1826template <class ELFT>
1827void MipsOptionsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1828 auto *S = cast<MipsOptionsInputSection<ELFT>>(C);
1829 if (S->Reginfo)
1830 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan84bb3552016-05-26 20:46:01 +00001831 S->OutSec = this;
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001832}
1833
George Rimar6892afa2016-07-12 09:49:43 +00001834template <class ELFT>
Simon Atanasyan85c6b442016-08-12 06:28:49 +00001835MipsAbiFlagsOutputSection<ELFT>::MipsAbiFlagsOutputSection()
1836 : OutputSectionBase<ELFT>(".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC) {
1837 this->Header.sh_addralign = 8;
1838 this->Header.sh_entsize = sizeof(Elf_Mips_ABIFlags);
1839 this->Header.sh_size = sizeof(Elf_Mips_ABIFlags);
1840 memset(&Flags, 0, sizeof(Flags));
1841}
1842
1843template <class ELFT>
1844void MipsAbiFlagsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1845 memcpy(Buf, &Flags, sizeof(Flags));
1846}
1847
1848template <class ELFT>
1849void MipsAbiFlagsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1850 // Check compatibility and merge fields from input .MIPS.abiflags
1851 // to the output one.
1852 auto *S = cast<MipsAbiFlagsInputSection<ELFT>>(C);
1853 S->OutSec = this;
1854 if (S->Flags->version != 0) {
1855 error(getFilename(S->getFile()) + ": unexpected .MIPS.abiflags version " +
1856 Twine(S->Flags->version));
1857 return;
1858 }
1859 // LLD checks ISA compatibility in getMipsEFlags(). Here we just
1860 // select the highest number of ISA/Rev/Ext.
1861 Flags.isa_level = std::max(Flags.isa_level, S->Flags->isa_level);
1862 Flags.isa_rev = std::max(Flags.isa_rev, S->Flags->isa_rev);
1863 Flags.isa_ext = std::max(Flags.isa_ext, S->Flags->isa_ext);
1864 Flags.gpr_size = std::max(Flags.gpr_size, S->Flags->gpr_size);
1865 Flags.cpr1_size = std::max(Flags.cpr1_size, S->Flags->cpr1_size);
1866 Flags.cpr2_size = std::max(Flags.cpr2_size, S->Flags->cpr2_size);
1867 Flags.ases |= S->Flags->ases;
1868 Flags.flags1 |= S->Flags->flags1;
1869 Flags.flags2 |= S->Flags->flags2;
1870 Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->Flags->fp_abi,
1871 getFilename(S->getFile()));
1872}
1873
1874template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +00001875static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
1876 return S->getSectionHdr()->sh_flags & ~SHF_GROUP & ~SHF_COMPRESSED;
1877}
1878
1879template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +00001880static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
1881 StringRef OutsecName) {
1882 const typename ELFT::Shdr *H = C->getSectionHdr();
1883 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +00001884 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +00001885
1886 // For SHF_MERGE we create different output sections for each alignment.
1887 // This makes each output section simple and keeps a single level mapping from
1888 // input to output.
1889 uintX_t Alignment = 0;
1890 if (isa<MergeInputSection<ELFT>>(C))
1891 Alignment = std::max(H->sh_addralign, H->sh_entsize);
1892
1893 uint32_t Type = H->sh_type;
1894 return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, Alignment};
1895}
1896
1897template <class ELFT>
George Rimar6892afa2016-07-12 09:49:43 +00001898std::pair<OutputSectionBase<ELFT> *, bool>
1899OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1900 StringRef OutsecName) {
1901 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +00001902 return create(Key, C);
1903}
George Rimar6892afa2016-07-12 09:49:43 +00001904
Rafael Espindola10897f12016-09-13 14:23:14 +00001905template <class ELFT>
1906std::pair<OutputSectionBase<ELFT> *, bool>
1907OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
1908 InputSectionBase<ELFT> *C) {
1909 uintX_t Flags = getOutFlags(C);
1910 OutputSectionBase<ELFT> *&Sec = Map[Key];
1911 if (Sec) {
1912 Sec->updateFlags(Flags);
1913 return {Sec, false};
1914 }
1915
1916 uint32_t Type = C->getSectionHdr()->sh_type;
Rafael Espindola16853bb2016-09-08 12:33:41 +00001917 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +00001918 case InputSectionBase<ELFT>::Regular:
Rafael Espindola10897f12016-09-13 14:23:14 +00001919 Sec = new OutputSection<ELFT>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +00001920 break;
1921 case InputSectionBase<ELFT>::EHFrame:
1922 return {Out<ELFT>::EhFrame, false};
1923 case InputSectionBase<ELFT>::Merge:
Rafael Espindola10897f12016-09-13 14:23:14 +00001924 Sec = new MergeOutputSection<ELFT>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +00001925 break;
1926 case InputSectionBase<ELFT>::MipsReginfo:
1927 Sec = new MipsReginfoOutputSection<ELFT>();
1928 break;
1929 case InputSectionBase<ELFT>::MipsOptions:
1930 Sec = new MipsOptionsOutputSection<ELFT>();
1931 break;
Simon Atanasyan85c6b442016-08-12 06:28:49 +00001932 case InputSectionBase<ELFT>::MipsAbiFlags:
1933 Sec = new MipsAbiFlagsOutputSection<ELFT>();
1934 break;
George Rimar6892afa2016-07-12 09:49:43 +00001935 }
Rui Ueyamabdf836d2016-08-12 01:10:17 +00001936 Out<ELFT>::Pool.emplace_back(Sec);
George Rimar6892afa2016-07-12 09:49:43 +00001937 return {Sec, true};
1938}
1939
George Rimar6892afa2016-07-12 09:49:43 +00001940template <bool Is64Bits>
1941typename lld::elf::SectionKey<Is64Bits>
1942DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
1943 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
1944}
1945
1946template <bool Is64Bits>
1947typename lld::elf::SectionKey<Is64Bits>
1948DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
1949 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
1950 0};
1951}
1952
1953template <bool Is64Bits>
1954unsigned
1955DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
1956 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
1957}
1958
1959template <bool Is64Bits>
1960bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
1961 const Key &RHS) {
1962 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
1963 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
1964 LHS.Alignment == RHS.Alignment;
1965}
1966
1967namespace llvm {
1968template struct DenseMapInfo<SectionKey<true>>;
1969template struct DenseMapInfo<SectionKey<false>>;
1970}
1971
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001972namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001973namespace elf {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001974template class OutputSectionBase<ELF32LE>;
1975template class OutputSectionBase<ELF32BE>;
1976template class OutputSectionBase<ELF64LE>;
1977template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001978
George Rimarf6bc65a2016-01-15 13:34:52 +00001979template class EhFrameHeader<ELF32LE>;
1980template class EhFrameHeader<ELF32BE>;
1981template class EhFrameHeader<ELF64LE>;
1982template class EhFrameHeader<ELF64BE>;
1983
George Rimar648a2c32015-10-20 08:54:27 +00001984template class GotPltSection<ELF32LE>;
1985template class GotPltSection<ELF32BE>;
1986template class GotPltSection<ELF64LE>;
1987template class GotPltSection<ELF64BE>;
1988
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001989template class GotSection<ELF32LE>;
1990template class GotSection<ELF32BE>;
1991template class GotSection<ELF64LE>;
1992template class GotSection<ELF64BE>;
1993
1994template class PltSection<ELF32LE>;
1995template class PltSection<ELF32BE>;
1996template class PltSection<ELF64LE>;
1997template class PltSection<ELF64BE>;
1998
1999template class RelocationSection<ELF32LE>;
2000template class RelocationSection<ELF32BE>;
2001template class RelocationSection<ELF64LE>;
2002template class RelocationSection<ELF64BE>;
2003
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00002004template class InterpSection<ELF32LE>;
2005template class InterpSection<ELF32BE>;
2006template class InterpSection<ELF64LE>;
2007template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00002008
Igor Kudrin1b0d7062015-10-22 08:21:35 +00002009template class GnuHashTableSection<ELF32LE>;
2010template class GnuHashTableSection<ELF32BE>;
2011template class GnuHashTableSection<ELF64LE>;
2012template class GnuHashTableSection<ELF64BE>;
2013
Rafael Espindola5805c4f2015-09-21 21:38:08 +00002014template class HashTableSection<ELF32LE>;
2015template class HashTableSection<ELF32BE>;
2016template class HashTableSection<ELF64LE>;
2017template class HashTableSection<ELF64BE>;
2018
2019template class DynamicSection<ELF32LE>;
2020template class DynamicSection<ELF32BE>;
2021template class DynamicSection<ELF64LE>;
2022template class DynamicSection<ELF64BE>;
2023
2024template class OutputSection<ELF32LE>;
2025template class OutputSection<ELF32BE>;
2026template class OutputSection<ELF64LE>;
2027template class OutputSection<ELF64BE>;
2028
Rui Ueyama1e479c22016-05-23 15:07:59 +00002029template class EhOutputSection<ELF32LE>;
2030template class EhOutputSection<ELF32BE>;
2031template class EhOutputSection<ELF64LE>;
2032template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00002033
Simon Atanasyan1d7df402015-12-20 10:57:34 +00002034template class MipsReginfoOutputSection<ELF32LE>;
2035template class MipsReginfoOutputSection<ELF32BE>;
2036template class MipsReginfoOutputSection<ELF64LE>;
2037template class MipsReginfoOutputSection<ELF64BE>;
2038
Simon Atanasyanadd74f32016-05-04 10:07:38 +00002039template class MipsOptionsOutputSection<ELF32LE>;
2040template class MipsOptionsOutputSection<ELF32BE>;
2041template class MipsOptionsOutputSection<ELF64LE>;
2042template class MipsOptionsOutputSection<ELF64BE>;
2043
Simon Atanasyan85c6b442016-08-12 06:28:49 +00002044template class MipsAbiFlagsOutputSection<ELF32LE>;
2045template class MipsAbiFlagsOutputSection<ELF32BE>;
2046template class MipsAbiFlagsOutputSection<ELF64LE>;
2047template class MipsAbiFlagsOutputSection<ELF64BE>;
2048
Rafael Espindolac159c962015-10-19 21:00:02 +00002049template class MergeOutputSection<ELF32LE>;
2050template class MergeOutputSection<ELF32BE>;
2051template class MergeOutputSection<ELF64LE>;
2052template class MergeOutputSection<ELF64BE>;
2053
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00002054template class StringTableSection<ELF32LE>;
2055template class StringTableSection<ELF32BE>;
2056template class StringTableSection<ELF64LE>;
2057template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00002058
2059template class SymbolTableSection<ELF32LE>;
2060template class SymbolTableSection<ELF32BE>;
2061template class SymbolTableSection<ELF64LE>;
2062template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00002063
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00002064template class VersionTableSection<ELF32LE>;
2065template class VersionTableSection<ELF32BE>;
2066template class VersionTableSection<ELF64LE>;
2067template class VersionTableSection<ELF64BE>;
2068
2069template class VersionNeedSection<ELF32LE>;
2070template class VersionNeedSection<ELF32BE>;
2071template class VersionNeedSection<ELF64LE>;
2072template class VersionNeedSection<ELF64BE>;
2073
George Rimard3566302016-06-20 11:55:12 +00002074template class VersionDefinitionSection<ELF32LE>;
2075template class VersionDefinitionSection<ELF32BE>;
2076template class VersionDefinitionSection<ELF64LE>;
2077template class VersionDefinitionSection<ELF64BE>;
2078
Rui Ueyama634ddf02016-03-11 20:51:53 +00002079template class BuildIdSection<ELF32LE>;
2080template class BuildIdSection<ELF32BE>;
2081template class BuildIdSection<ELF64LE>;
2082template class BuildIdSection<ELF64BE>;
Rui Ueyama3a41be22016-04-07 22:49:21 +00002083
Rafael Espindolad88d7162016-09-14 11:32:57 +00002084template class BuildIdFastHash<ELF32LE>;
2085template class BuildIdFastHash<ELF32BE>;
2086template class BuildIdFastHash<ELF64LE>;
2087template class BuildIdFastHash<ELF64BE>;
Rui Ueyama3a41be22016-04-07 22:49:21 +00002088
2089template class BuildIdMd5<ELF32LE>;
2090template class BuildIdMd5<ELF32BE>;
2091template class BuildIdMd5<ELF64LE>;
2092template class BuildIdMd5<ELF64BE>;
Rui Ueyamad86ec302016-04-07 23:51:56 +00002093
2094template class BuildIdSha1<ELF32LE>;
2095template class BuildIdSha1<ELF32BE>;
2096template class BuildIdSha1<ELF64LE>;
2097template class BuildIdSha1<ELF64BE>;
Rui Ueyama9194db72016-05-13 21:55:56 +00002098
Eugene Leviant933dae72016-08-26 09:55:37 +00002099template class BuildIdUuid<ELF32LE>;
2100template class BuildIdUuid<ELF32BE>;
2101template class BuildIdUuid<ELF64LE>;
2102template class BuildIdUuid<ELF64BE>;
2103
Rui Ueyama9194db72016-05-13 21:55:56 +00002104template class BuildIdHexstring<ELF32LE>;
2105template class BuildIdHexstring<ELF32BE>;
2106template class BuildIdHexstring<ELF64LE>;
2107template class BuildIdHexstring<ELF64BE>;
George Rimar6892afa2016-07-12 09:49:43 +00002108
2109template class OutputSectionFactory<ELF32LE>;
2110template class OutputSectionFactory<ELF32BE>;
2111template class OutputSectionFactory<ELF64LE>;
2112template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00002113}
2114}