blob: bd3bec0579e62d299e9255b5eff59f47d9a4a950 [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 Rimar58fa5242016-10-20 09:19:48 +000013#include "GdbIndex.h"
Rui Ueyama95642b92016-11-01 23:09:07 +000014#include "LinkerScript.h"
15#include "Memory.h"
Rui Ueyamafbbde542016-06-29 09:08:02 +000016#include "Strings.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000017#include "SymbolTable.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000018#include "SyntheticSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000019#include "Target.h"
Rui Ueyamae9809502016-03-11 04:23:12 +000020#include "lld/Core/Parallel.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000021#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000022#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000023#include "llvm/Support/MathExtras.h"
Rafael Espindolaa42b3bc2016-09-27 16:43:49 +000024#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000025
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000027using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000029using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030using namespace llvm::ELF;
31
32using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000033using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000035template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000036OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
37 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038 : Name(Name) {
Sean Silva580c1b62016-04-20 04:26:16 +000039 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000040 Header.sh_type = Type;
41 Header.sh_flags = Flags;
Rui Ueyama3b04d832016-07-14 05:46:24 +000042 Header.sh_addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043}
44
Rafael Espindola0b113672016-07-27 14:10:56 +000045template <class ELFT> uint32_t OutputSectionBase<ELFT>::getPhdrFlags() const {
46 uintX_t Flags = getFlags();
47 uint32_t Ret = PF_R;
48 if (Flags & SHF_WRITE)
49 Ret |= PF_W;
50 if (Flags & SHF_EXECINSTR)
51 Ret |= PF_X;
52 return Ret;
53}
54
Rafael Espindola35c6af32015-09-25 17:19:10 +000055template <class ELFT>
Rui Ueyamac63c1db2016-03-13 06:50:33 +000056void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *Shdr) {
57 *Shdr = Header;
58}
59
60template <class ELFT>
George Rimar58fa5242016-10-20 09:19:48 +000061GdbIndexSection<ELFT>::GdbIndexSection()
62 : OutputSectionBase<ELFT>(".gdb_index", SHT_PROGBITS, 0) {}
63
64template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
65 std::vector<InputSection<ELFT> *> &IS =
66 static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections;
67
68 for (InputSection<ELFT> *I : IS)
69 readDwarf(I);
70}
71
72template <class ELFT>
73void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
74 std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I);
75 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
76}
77
78template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
79 parseDebugSections();
80
81 // GdbIndex header consist from version fields
82 // and 5 more fields with different kinds of offsets.
83 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
84 this->Header.sh_size = CuTypesOffset;
85}
86
87template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
88 write32le(Buf, 7); // Write Version
89 write32le(Buf + 4, CuListOffset); // CU list offset
90 write32le(Buf + 8, CuTypesOffset); // Types CU list offset
91 write32le(Buf + 12, CuTypesOffset); // Address area offset
92 write32le(Buf + 16, CuTypesOffset); // Symbol table offset
93 write32le(Buf + 20, CuTypesOffset); // Constant pool offset
94 Buf += 24;
95
96 // Write the CU list.
97 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
98 write64le(Buf, CU.first);
99 write64le(Buf + 8, CU.second);
100 Buf += 16;
101 }
102}
103
104template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000105GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000106 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Rui Ueyama803b1202016-07-13 18:55:14 +0000107 this->Header.sh_addralign = Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000108}
109
Rafael Espindola67d72c02016-03-11 12:06:30 +0000110template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
111 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
112 Entries.push_back(&Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000113}
114
115template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000116 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +0000117}
118
George Rimar648a2c32015-10-20 08:54:27 +0000119template <class ELFT> void GotPltSection<ELFT>::finalize() {
Rui Ueyama803b1202016-07-13 18:55:14 +0000120 this->Header.sh_size = (Target->GotPltHeaderEntriesNum + Entries.size()) *
121 Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000122}
123
124template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000125 Target->writeGotPltHeader(Buf);
Rui Ueyama803b1202016-07-13 18:55:14 +0000126 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000127 for (const SymbolBody *B : Entries) {
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000128 Target->writeGotPlt(Buf, *B);
George Rimar648a2c32015-10-20 08:54:27 +0000129 Buf += sizeof(uintX_t);
130 }
131}
132
133template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000134GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000135 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000136 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000137 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama803b1202016-07-13 18:55:14 +0000138 this->Header.sh_addralign = Target->GotEntrySize;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000139}
140
George Rimara4c7e742016-10-20 08:36:42 +0000141template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000142 Sym.GotIndex = Entries.size();
143 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +0000144}
145
Simon Atanasyan41325112016-06-19 21:39:37 +0000146template <class ELFT>
147void GotSection<ELFT>::addMipsEntry(SymbolBody &Sym, uintX_t Addend,
148 RelExpr Expr) {
149 // For "true" local symbols which can be referenced from the same module
150 // only compiler creates two instructions for address loading:
151 //
152 // lw $8, 0($gp) # R_MIPS_GOT16
153 // addi $8, $8, 0 # R_MIPS_LO16
154 //
155 // The first instruction loads high 16 bits of the symbol address while
156 // the second adds an offset. That allows to reduce number of required
157 // GOT entries because only one global offset table entry is necessary
158 // for every 64 KBytes of local data. So for local symbols we need to
159 // allocate number of GOT entries to hold all required "page" addresses.
160 //
161 // All global symbols (hidden and regular) considered by compiler uniformly.
162 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
163 // to load address of the symbol. So for each such symbol we need to
164 // allocate dedicated GOT entry to store its address.
165 //
166 // If a symbol is preemptible we need help of dynamic linker to get its
167 // final address. The corresponding GOT entries are allocated in the
168 // "global" part of GOT. Entries for non preemptible global symbol allocated
169 // in the "local" part of GOT.
170 //
171 // See "Global Offset Table" in Chapter 5:
172 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
173 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
174 // At this point we do not know final symbol value so to reduce number
175 // of allocated GOT entries do the following trick. Save all output
176 // sections referenced by GOT relocations. Then later in the `finalize`
177 // method calculate number of "pages" required to cover all saved output
178 // section and allocate appropriate number of GOT entries.
179 auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec;
180 MipsOutSections.insert(OutSec);
181 return;
182 }
Simon Atanasyan002e2442016-06-23 15:26:31 +0000183 if (Sym.isTls()) {
184 // GOT entries created for MIPS TLS relocations behave like
185 // almost GOT entries from other ABIs. They go to the end
186 // of the global offset table.
187 Sym.GotIndex = Entries.size();
188 Entries.push_back(&Sym);
189 return;
190 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000191 auto AddEntry = [&](SymbolBody &S, uintX_t A, MipsGotEntries &Items) {
192 if (S.isInGot() && !A)
193 return;
194 size_t NewIndex = Items.size();
195 if (!MipsGotMap.insert({{&S, A}, NewIndex}).second)
196 return;
197 Items.emplace_back(&S, A);
198 if (!A)
199 S.GotIndex = NewIndex;
200 };
201 if (Sym.isPreemptible()) {
202 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
203 AddEntry(Sym, 0, MipsGlobal);
204 Sym.IsInGlobalMipsGot = true;
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000205 } else if (Expr == R_MIPS_GOT_OFF32) {
206 AddEntry(Sym, Addend, MipsLocal32);
207 Sym.Is32BitMipsGot = true;
208 } else {
209 // Hold local GOT entries accessed via a 16-bit index separately.
210 // That allows to write them in the beginning of the GOT and keep
211 // their indexes as less as possible to escape relocation's overflow.
Simon Atanasyan41325112016-06-19 21:39:37 +0000212 AddEntry(Sym, Addend, MipsLocal);
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000213 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000214}
215
Rafael Espindola67d72c02016-03-11 12:06:30 +0000216template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000217 if (Sym.GlobalDynIndex != -1U)
George Rimar90cd0a82015-12-01 19:20:26 +0000218 return false;
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000219 Sym.GlobalDynIndex = Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +0000220 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +0000221 Entries.push_back(nullptr);
Rafael Espindolaa8777c22016-06-08 21:31:59 +0000222 Entries.push_back(&Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000223 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224}
225
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000226// Reserves TLS entries for a TLS module ID and a TLS block offset.
227// In total it takes two GOT slots.
228template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
229 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +0000230 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000231 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000232 Entries.push_back(nullptr);
233 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000234 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000235}
236
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000237template <class ELFT>
238typename GotSection<ELFT>::uintX_t
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000239GotSection<ELFT>::getMipsLocalPageOffset(uintX_t EntryValue) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000240 // Initialize the entry by the %hi(EntryValue) expression
241 // but without right-shifting.
Simon Atanasyan41325112016-06-19 21:39:37 +0000242 EntryValue = (EntryValue + 0x8000) & ~0xffff;
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000243 // Take into account MIPS GOT header.
244 // See comment in the GotSection::writeTo.
245 size_t NewIndex = MipsLocalGotPos.size() + 2;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000246 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
Simon Atanasyan41325112016-06-19 21:39:37 +0000247 assert(!P.second || MipsLocalGotPos.size() <= MipsPageEntries);
George Rimar71e64b22016-05-11 09:41:15 +0000248 return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249}
250
Igor Kudrin304860a2015-11-12 04:39:49 +0000251template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000252typename GotSection<ELFT>::uintX_t
Simon Atanasyan41325112016-06-19 21:39:37 +0000253GotSection<ELFT>::getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const {
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000254 // Calculate offset of the GOT entries block: TLS, global, local.
255 uintX_t GotBlockOff;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000256 if (B.isTls())
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000257 GotBlockOff = getMipsTlsOffset();
Simon Atanasyan002e2442016-06-23 15:26:31 +0000258 else if (B.IsInGlobalMipsGot)
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000259 GotBlockOff = getMipsLocalEntriesNum() * sizeof(uintX_t);
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000260 else if (B.Is32BitMipsGot)
261 GotBlockOff = (MipsPageEntries + MipsLocal.size()) * sizeof(uintX_t);
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000262 else
263 GotBlockOff = MipsPageEntries * sizeof(uintX_t);
264 // Calculate index of the GOT entry in the block.
265 uintX_t GotIndex;
266 if (B.isInGot())
267 GotIndex = B.GotIndex;
Simon Atanasyan41325112016-06-19 21:39:37 +0000268 else {
269 auto It = MipsGotMap.find({&B, Addend});
270 assert(It != MipsGotMap.end());
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000271 GotIndex = It->second;
Simon Atanasyan41325112016-06-19 21:39:37 +0000272 }
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000273 return GotBlockOff + GotIndex * sizeof(uintX_t) - MipsGPOffset;
Simon Atanasyan41325112016-06-19 21:39:37 +0000274}
275
276template <class ELFT>
Simon Atanasyanbc946932016-10-19 17:13:43 +0000277typename GotSection<ELFT>::uintX_t GotSection<ELFT>::getMipsTlsOffset() const {
Simon Atanasyanbadd5b32016-10-20 17:53:59 +0000278 return (getMipsLocalEntriesNum() + MipsGlobal.size()) * sizeof(uintX_t);
Simon Atanasyan002e2442016-06-23 15:26:31 +0000279}
280
281template <class ELFT>
Simon Atanasyan41325112016-06-19 21:39:37 +0000282typename GotSection<ELFT>::uintX_t
George Rimar90cd0a82015-12-01 19:20:26 +0000283GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000284 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
George Rimar90cd0a82015-12-01 19:20:26 +0000285}
286
287template <class ELFT>
Rafael Espindola74031ba2016-04-07 15:20:56 +0000288typename GotSection<ELFT>::uintX_t
289GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000290 return B.GlobalDynIndex * sizeof(uintX_t);
Rafael Espindola74031ba2016-04-07 15:20:56 +0000291}
292
293template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000294const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
Simon Atanasyan41325112016-06-19 21:39:37 +0000295 return MipsGlobal.empty() ? nullptr : MipsGlobal.front().first;
Igor Kudrin304860a2015-11-12 04:39:49 +0000296}
297
298template <class ELFT>
299unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000300 return MipsPageEntries + MipsLocal.size() + MipsLocal32.size();
Igor Kudrin304860a2015-11-12 04:39:49 +0000301}
302
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000303template <class ELFT> void GotSection<ELFT>::finalize() {
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000304 size_t EntriesNum = Entries.size();
305 if (Config->EMachine == EM_MIPS) {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000306 // Take into account MIPS GOT header.
307 // See comment in the GotSection::writeTo.
Simon Atanasyan41325112016-06-19 21:39:37 +0000308 MipsPageEntries += 2;
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000309 for (const OutputSectionBase<ELFT> *OutSec : MipsOutSections) {
310 // Calculate an upper bound of MIPS GOT entries required to store page
311 // addresses of local symbols. We assume the worst case - each 64kb
312 // page of the output section has at least one GOT relocation against it.
313 // Add 0x8000 to the section's size because the page address stored
314 // in the GOT entry is calculated as (value + 0x8000) & ~0xffff.
Simon Atanasyan41325112016-06-19 21:39:37 +0000315 MipsPageEntries += (OutSec->getSize() + 0x8000 + 0xfffe) / 0xffff;
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000316 }
Simon Atanasyanbadd5b32016-10-20 17:53:59 +0000317 EntriesNum += getMipsLocalEntriesNum() + MipsGlobal.size();
Simon Atanasyand2980d32016-03-29 14:07:22 +0000318 }
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000319 this->Header.sh_size = EntriesNum * sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000320}
321
Rui Ueyamac442cff2016-09-08 21:46:21 +0000322template <class ELFT>
323static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
324 typedef typename ELFT::uint uintX_t;
325 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
326}
327
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000328template <class ELFT> void GotSection<ELFT>::writeMipsGot(uint8_t *Buf) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000329 // Set the MSB of the second GOT slot. This is not required by any
330 // MIPS ABI documentation, though.
331 //
332 // There is a comment in glibc saying that "The MSB of got[1] of a
333 // gnu object is set to identify gnu objects," and in GNU gold it
334 // says "the second entry will be used by some runtime loaders".
335 // But how this field is being used is unclear.
336 //
337 // We are not really willing to mimic other linkers behaviors
338 // without understanding why they do that, but because all files
339 // generated by GNU tools have this special GOT value, and because
340 // we've been doing this for years, it is probably a safe bet to
341 // keep doing this for now. We really need to revisit this to see
342 // if we had to do this.
343 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
344 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
345 // Write 'page address' entries to the local part of the GOT.
346 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
347 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000348 writeUint<ELFT>(Entry, L.first);
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000349 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000350 Buf += MipsPageEntries * sizeof(uintX_t);
351 auto AddEntry = [&](const MipsGotEntry &SA) {
352 uint8_t *Entry = Buf;
353 Buf += sizeof(uintX_t);
George Rimara4c7e742016-10-20 08:36:42 +0000354 const SymbolBody *Body = SA.first;
George Rimar06e930d2016-06-20 10:01:50 +0000355 uintX_t VA = Body->template getVA<ELFT>(SA.second);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000356 writeUint<ELFT>(Entry, VA);
Simon Atanasyan41325112016-06-19 21:39:37 +0000357 };
358 std::for_each(std::begin(MipsLocal), std::end(MipsLocal), AddEntry);
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000359 std::for_each(std::begin(MipsLocal32), std::end(MipsLocal32), AddEntry);
Simon Atanasyan41325112016-06-19 21:39:37 +0000360 std::for_each(std::begin(MipsGlobal), std::end(MipsGlobal), AddEntry);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000361 // Initialize TLS-related GOT entries. If the entry has a corresponding
362 // dynamic relocations, leave it initialized by zero. Write down adjusted
363 // TLS symbol's values otherwise. To calculate the adjustments use offsets
364 // for thread-local storage.
365 // https://www.linux-mips.org/wiki/NPTL
366 if (TlsIndexOff != -1U && !Config->Pic)
Rui Ueyamac442cff2016-09-08 21:46:21 +0000367 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000368 for (const SymbolBody *B : Entries) {
369 if (!B || B->isPreemptible())
370 continue;
371 uintX_t VA = B->getVA<ELFT>();
372 if (B->GotIndex != -1U) {
373 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000374 writeUint<ELFT>(Entry, VA - 0x7000);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000375 }
376 if (B->GlobalDynIndex != -1U) {
377 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000378 writeUint<ELFT>(Entry, 1);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000379 Entry += sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000380 writeUint<ELFT>(Entry, VA - 0x8000);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000381 }
382 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000383}
384
385template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000386 if (Config->EMachine == EM_MIPS) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000387 writeMipsGot(Buf);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000388 return;
389 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000390 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000391 uint8_t *Entry = Buf;
392 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000393 if (!B)
394 continue;
Simon Atanasyan41325112016-06-19 21:39:37 +0000395 if (B->isPreemptible())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000396 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000397 uintX_t VA = B->getVA<ELFT>();
Rui Ueyamac442cff2016-09-08 21:46:21 +0000398 writeUint<ELFT>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000399 }
400}
401
Rafael Espindola35c6af32015-09-25 17:19:10 +0000402template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000403PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000404 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000405 this->Header.sh_addralign = 16;
406}
407
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000409 // At beginning of PLT, we have code to call the dynamic linker
410 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama4a90f572016-06-16 16:28:50 +0000411 Target->writePltHeader(Buf);
412 size_t Off = Target->PltHeaderSize;
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000413
George Rimarfb5d7f22015-11-26 19:58:51 +0000414 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000415 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000416 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000417 uint64_t Got = B->getGotPltVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000418 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000419 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000420 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000421 }
422}
423
Rafael Espindola67d72c02016-03-11 12:06:30 +0000424template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
425 Sym.PltIndex = Entries.size();
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000426 unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000427 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000428}
429
George Rimar648a2c32015-10-20 08:54:27 +0000430template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000431 this->Header.sh_size =
Rui Ueyama4a90f572016-06-16 16:28:50 +0000432 Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000433}
434
435template <class ELFT>
George Rimarc191acf2016-05-10 15:47:57 +0000436RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000437 : OutputSectionBase<ELFT>(Name, Config->Rela ? SHT_RELA : SHT_REL,
George Rimarc191acf2016-05-10 15:47:57 +0000438 SHF_ALLOC),
439 Sort(Sort) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000440 this->Header.sh_entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000441 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000442}
443
George Rimar5828c232015-11-30 17:49:19 +0000444template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000445void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
Eugene Leviantaa498192016-08-31 08:51:39 +0000446 if (Reloc.Type == Target->RelativeRel)
447 ++NumRelativeRelocs;
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000448 Relocs.push_back(Reloc);
449}
450
George Rimarc191acf2016-05-10 15:47:57 +0000451template <class ELFT, class RelTy>
452static bool compRelocations(const RelTy &A, const RelTy &B) {
Eugene Leviantaa498192016-08-31 08:51:39 +0000453 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
454 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
455 if (AIsRel != BIsRel)
456 return AIsRel;
457
George Rimarc191acf2016-05-10 15:47:57 +0000458 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
459}
460
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000461template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarc191acf2016-05-10 15:47:57 +0000462 uint8_t *BufBegin = Buf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000463 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rui Ueyama614be592016-03-13 05:23:40 +0000464 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000465 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000466
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000467 if (Config->Rela)
Rui Ueyama809d8e22016-06-23 04:33:42 +0000468 P->r_addend = Rel.getAddend();
469 P->r_offset = Rel.getOffset();
Simon Atanasyan002e2442016-06-23 15:26:31 +0000470 if (Config->EMachine == EM_MIPS && Rel.getOutputSec() == Out<ELFT>::Got)
471 // Dynamic relocation against MIPS GOT section make deal TLS entries
472 // allocated in the end of the GOT. We need to adjust the offset to take
473 // in account 'local' and 'global' GOT entries.
474 P->r_offset += Out<ELFT>::Got->getMipsTlsOffset();
Rui Ueyama809d8e22016-06-23 04:33:42 +0000475 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000476 }
George Rimarc191acf2016-05-10 15:47:57 +0000477
478 if (Sort) {
479 if (Config->Rela)
480 std::stable_sort((Elf_Rela *)BufBegin,
481 (Elf_Rela *)BufBegin + Relocs.size(),
482 compRelocations<ELFT, Elf_Rela>);
483 else
484 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
485 compRelocations<ELFT, Elf_Rel>);
486 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000487}
488
George Rimar77b77792015-11-25 22:15:01 +0000489template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000490 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000491}
492
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000493template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama1034c9e2016-08-09 04:42:01 +0000494 this->Header.sh_link = Out<ELFT>::DynSymTab
Rui Ueyamacbd434a2016-08-09 04:31:21 +0000495 ? Out<ELFT>::DynSymTab->SectionIndex
496 : Out<ELFT>::SymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000497 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
498}
499
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000500template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000501HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000502 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000503 this->Header.sh_entsize = sizeof(Elf_Word);
504 this->Header.sh_addralign = sizeof(Elf_Word);
505}
506
Rui Ueyama0db335f2015-10-07 16:58:54 +0000507template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000508 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000509
George Rimare9e1d322016-02-18 15:17:01 +0000510 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000511 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000512
513 // Create as many buckets as there are symbols.
514 // FIXME: This is simplistic. We can try to optimize it, but implementing
515 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000516 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000517 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
518}
519
520template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000521 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000522 auto *P = reinterpret_cast<Elf_Word *>(Buf);
523 *P++ = NumSymbols; // nbucket
524 *P++ = NumSymbols; // nchain
525
526 Elf_Word *Buckets = P;
527 Elf_Word *Chains = P + NumSymbols;
528
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000529 for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) {
530 SymbolBody *Body = S.Symbol;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000531 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000532 unsigned I = Body->DynsymIndex;
Davide Italianoe7fd0be2016-11-07 21:56:56 +0000533 uint32_t Hash = hashSysV(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000534 Chains[I] = Buckets[Hash];
535 Buckets[Hash] = I;
536 }
537}
538
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000539static uint32_t hashGnu(StringRef Name) {
540 uint32_t H = 5381;
541 for (uint8_t C : Name)
542 H = (H << 5) + H + C;
543 return H;
544}
545
546template <class ELFT>
547GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000548 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000549 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000550 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000551}
552
553template <class ELFT>
554unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
555 if (!NumHashed)
556 return 0;
557
558 // These values are prime numbers which are not greater than 2^(N-1) + 1.
559 // In result, for any particular NumHashed we return a prime number
560 // which is not greater than NumHashed.
561 static const unsigned Primes[] = {
562 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
563 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
564
565 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
566 array_lengthof(Primes) - 1)];
567}
568
569// Bloom filter estimation: at least 8 bits for each hashed symbol.
570// GNU Hash table requirement: it should be a power of 2,
571// the minimum value is 1, even for an empty table.
572// Expected results for a 32-bit target:
573// calcMaskWords(0..4) = 1
574// calcMaskWords(5..8) = 2
575// calcMaskWords(9..16) = 4
576// For a 64-bit target:
577// calcMaskWords(0..8) = 1
578// calcMaskWords(9..16) = 2
579// calcMaskWords(17..32) = 4
580template <class ELFT>
581unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
582 if (!NumHashed)
583 return 1;
584 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
585}
586
587template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000588 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000589 NBuckets = calcNBuckets(NumHashed);
590 MaskWords = calcMaskWords(NumHashed);
591 // Second hash shift estimation: just predefined values.
592 Shift2 = ELFT::Is64Bits ? 6 : 5;
593
594 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
595 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
596 + sizeof(Elf_Off) * MaskWords // Bloom Filter
597 + sizeof(Elf_Word) * NBuckets // Hash Buckets
598 + sizeof(Elf_Word) * NumHashed; // Hash Values
599}
600
601template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
602 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000603 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000604 return;
605 writeBloomFilter(Buf);
606 writeHashTable(Buf);
607}
608
609template <class ELFT>
610void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
611 auto *P = reinterpret_cast<Elf_Word *>(Buf);
612 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000613 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000614 *P++ = MaskWords;
615 *P++ = Shift2;
616 Buf = reinterpret_cast<uint8_t *>(P);
617}
618
619template <class ELFT>
620void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
621 unsigned C = sizeof(Elf_Off) * 8;
622
623 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000624 for (const SymbolData &Sym : Symbols) {
625 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
626 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
627 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000628 Masks[Pos] |= V;
629 }
630 Buf += sizeof(Elf_Off) * MaskWords;
631}
632
633template <class ELFT>
634void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
635 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
636 Elf_Word *Values = Buckets + NBuckets;
637
638 int PrevBucket = -1;
639 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000640 for (const SymbolData &Sym : Symbols) {
641 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000642 assert(PrevBucket <= Bucket);
643 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000644 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000645 PrevBucket = Bucket;
646 if (I > 0)
647 Values[I - 1] |= 1;
648 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000649 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000650 ++I;
651 }
652 if (I > 0)
653 Values[I - 1] |= 1;
654}
655
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000656// Add symbols to this symbol hash table. Note that this function
657// destructively sort a given vector -- which is needed because
658// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000659template <class ELFT>
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000660void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
Davide Italianob4b68b62016-07-04 19:49:55 +0000661 // Ideally this will just be 'auto' but GCC 6.1 is not able
662 // to deduce it correctly.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000663 std::vector<SymbolTableEntry>::iterator Mid =
664 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
665 return S.Symbol->isUndefined();
666 });
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000667 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000668 return;
Davide Italianof8591cf2016-07-06 17:41:55 +0000669 for (auto I = Mid, E = V.end(); I != E; ++I) {
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000670 SymbolBody *B = I->Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000671 size_t StrOff = I->StrTabOffset;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000672 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
673 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000674
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000675 unsigned NBuckets = calcNBuckets(Symbols.size());
676 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000677 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000678 return L.Hash % NBuckets < R.Hash % NBuckets;
679 });
680
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000681 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000682 for (const SymbolData &Sym : Symbols)
683 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000684}
685
George Rimard3566302016-06-20 11:55:12 +0000686// Returns the number of version definition entries. Because the first entry
687// is for the version definition itself, it is the number of versioned symbols
688// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000689static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000690
Igor Kudrinf1d60292015-10-28 07:05:56 +0000691template <class ELFT>
Rui Ueyamaace4f902016-05-24 04:25:47 +0000692DynamicSection<ELFT>::DynamicSection()
693 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000694 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000695 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000696 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000697
698 // .dynamic section is not writable on MIPS.
699 // See "Special Section" in Chapter 4 in the following document:
700 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
701 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000702 Header.sh_flags = SHF_ALLOC;
Rui Ueyamaa9593932016-11-02 02:18:01 +0000703
704 addEntries();
Rafael Espindola35c6af32015-09-25 17:19:10 +0000705}
706
Rui Ueyamaa9593932016-11-02 02:18:01 +0000707// There are some dynamic entries that don't depend on other sections.
708// Such entries can be set early.
709template <class ELFT> void DynamicSection<ELFT>::addEntries() {
710 // Add strings to .dynstr early so that .dynstr's size will be
711 // fixed early.
George Rimarb952ece2016-09-02 09:13:05 +0000712 for (StringRef S : Config->AuxiliaryList)
713 Add({DT_AUXILIARY, Out<ELFT>::DynStrTab->addString(S)});
Rafael Espindolade069362016-01-25 21:32:04 +0000714 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000715 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
716 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000717 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
Rafael Espindolade069362016-01-25 21:32:04 +0000718 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000719 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
720 if (!Config->SoName.empty())
721 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
722
Rui Ueyamaa9593932016-11-02 02:18:01 +0000723 // Set DT_FLAGS and DT_FLAGS_1.
724 uint32_t DtFlags = 0;
725 uint32_t DtFlags1 = 0;
726 if (Config->Bsymbolic)
727 DtFlags |= DF_SYMBOLIC;
728 if (Config->ZNodelete)
729 DtFlags1 |= DF_1_NODELETE;
730 if (Config->ZNow) {
731 DtFlags |= DF_BIND_NOW;
732 DtFlags1 |= DF_1_NOW;
733 }
734 if (Config->ZOrigin) {
735 DtFlags |= DF_ORIGIN;
736 DtFlags1 |= DF_1_ORIGIN;
737 }
738
739 if (DtFlags)
740 Add({DT_FLAGS, DtFlags});
741 if (DtFlags1)
742 Add({DT_FLAGS_1, DtFlags1});
743
744 if (!Config->Entry.empty())
745 Add({DT_DEBUG, (uint64_t)0});
746}
747
748// Add remaining entries to complete .dynamic contents.
749template <class ELFT> void DynamicSection<ELFT>::finalize() {
750 if (this->Header.sh_size)
751 return; // Already finalized.
752
753 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindolade069362016-01-25 21:32:04 +0000754
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000755 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000756 bool IsRela = Config->Rela;
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000757 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
758 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
759 Add({IsRela ? DT_RELAENT : DT_RELENT,
760 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Eugene Leviantaa498192016-08-31 08:51:39 +0000761
Simon Atanasyan65b24612016-09-04 17:40:12 +0000762 // MIPS dynamic loader does not support RELCOUNT tag.
763 // The problem is in the tight relation between dynamic
764 // relocations and GOT. So do not emit this tag on MIPS.
765 if (Config->EMachine != EM_MIPS) {
766 size_t NumRelativeRels = Out<ELFT>::RelaDyn->getRelativeRelocCount();
767 if (Config->ZCombreloc && NumRelativeRels)
768 Add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
769 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000770 }
George Rimar648a2c32015-10-20 08:54:27 +0000771 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000772 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
773 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
774 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
775 Out<ELFT>::GotPlt});
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000776 Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000777 }
778
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000779 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
780 Add({DT_SYMENT, sizeof(Elf_Sym)});
781 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
782 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000783 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000784 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000785 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000786 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000787
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000788 if (Out<ELFT>::PreinitArray) {
789 Add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
George Rimar03e05602016-08-19 15:23:39 +0000790 Add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
Rafael Espindolade069362016-01-25 21:32:04 +0000791 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000792 if (Out<ELFT>::InitArray) {
793 Add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
George Rimar03e05602016-08-19 15:23:39 +0000794 Add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
Rafael Espindolade069362016-01-25 21:32:04 +0000795 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000796 if (Out<ELFT>::FiniArray) {
797 Add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
George Rimar03e05602016-08-19 15:23:39 +0000798 Add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000799 }
800
Rui Ueyamaace4f902016-05-24 04:25:47 +0000801 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000802 Add({DT_INIT, B});
Rui Ueyamaace4f902016-05-24 04:25:47 +0000803 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000804 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000805
George Rimard3566302016-06-20 11:55:12 +0000806 bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0;
807 if (HasVerNeed || Out<ELFT>::VerDef)
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000808 Add({DT_VERSYM, Out<ELFT>::VerSym});
George Rimard3566302016-06-20 11:55:12 +0000809 if (Out<ELFT>::VerDef) {
810 Add({DT_VERDEF, Out<ELFT>::VerDef});
811 Add({DT_VERDEFNUM, getVerDefNum()});
812 }
813 if (HasVerNeed) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000814 Add({DT_VERNEED, Out<ELFT>::VerNeed});
George Rimard3566302016-06-20 11:55:12 +0000815 Add({DT_VERNEEDNUM, Out<ELFT>::VerNeed->getNeedNum()});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000816 }
817
Igor Kudrin304860a2015-11-12 04:39:49 +0000818 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000819 Add({DT_MIPS_RLD_VERSION, 1});
820 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
Rui Ueyamacafc0f2e2016-07-14 17:40:18 +0000821 Add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000822 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
823 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000824 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000825 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000826 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000827 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
828 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000829 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000830 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000831 }
832
Rafael Espindolade069362016-01-25 21:32:04 +0000833 // +1 for DT_NULL
Rui Ueyamaa9593932016-11-02 02:18:01 +0000834 this->Header.sh_size = (Entries.size() + 1) * this->Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000835}
836
837template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000838 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
839
Rafael Espindolade069362016-01-25 21:32:04 +0000840 for (const Entry &E : Entries) {
841 P->d_tag = E.Tag;
842 switch (E.Kind) {
843 case Entry::SecAddr:
844 P->d_un.d_ptr = E.OutSec->getVA();
845 break;
George Rimar03e05602016-08-19 15:23:39 +0000846 case Entry::SecSize:
847 P->d_un.d_val = E.OutSec->getSize();
848 break;
Rafael Espindolade069362016-01-25 21:32:04 +0000849 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000850 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000851 break;
852 case Entry::PlainInt:
853 P->d_un.d_val = E.Val;
854 break;
855 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000856 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000857 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000858}
859
860template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000861EhFrameHeader<ELFT>::EhFrameHeader()
Rui Ueyamade9777a2016-05-23 16:30:41 +0000862 : OutputSectionBase<ELFT>(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000863
Rui Ueyama95a232e2016-05-23 01:45:05 +0000864// .eh_frame_hdr contains a binary search table of pointers to FDEs.
865// Each entry of the search table consists of two values,
866// the starting PC from where FDEs covers, and the FDE's address.
867// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000868template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
869 const endianness E = ELFT::TargetEndianness;
870
Rui Ueyamae75e9332016-05-23 03:00:33 +0000871 // Sort the FDE list by their PC and uniqueify. Usually there is only
872 // one FDE for a PC (i.e. function), but if ICF merges two functions
873 // into one, there can be more than one FDEs pointing to the address.
874 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
875 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
876 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
877 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000878
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000879 Buf[0] = 1;
880 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
881 Buf[2] = DW_EH_PE_udata4;
882 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000883 write32<E>(Buf + 4, Out<ELFT>::EhFrame->getVA() - this->getVA() - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000884 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000885 Buf += 12;
886
Rui Ueyamae75e9332016-05-23 03:00:33 +0000887 uintX_t VA = this->getVA();
888 for (FdeData &Fde : Fdes) {
889 write32<E>(Buf, Fde.Pc - VA);
890 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000891 Buf += 8;
892 }
893}
894
Rui Ueyamade9777a2016-05-23 16:30:41 +0000895template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
896 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
897 this->Header.sh_size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
898}
899
George Rimarf6bc65a2016-01-15 13:34:52 +0000900template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000901void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
902 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000903}
904
George Rimarf6bc65a2016-01-15 13:34:52 +0000905template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000906OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
907 : OutputSectionBase<ELFT>(Name, Type, Flags) {
908 if (Type == SHT_RELA)
909 this->Header.sh_entsize = sizeof(Elf_Rela);
910 else if (Type == SHT_REL)
911 this->Header.sh_entsize = sizeof(Elf_Rel);
912}
913
914template <class ELFT> void OutputSection<ELFT>::finalize() {
915 uint32_t Type = this->Header.sh_type;
Peter Smith580ba952016-10-21 11:25:33 +0000916 if (this->Header.sh_flags & SHF_LINK_ORDER) {
917 if (!Config->Relocatable) {
918 // SHF_LINK_ORDER only has meaning in relocatable objects
919 this->Header.sh_flags &= ~SHF_LINK_ORDER;
920 }
921 else if (!this->Sections.empty()) {
922 // When doing a relocatable link we must preserve the link order
923 // dependency of sections with the SHF_LINK_ORDER flag. The dependency
924 // is indicated by the sh_link field. We need to translate the
925 // InputSection sh_link to the OutputSection sh_link, all InputSections
926 // in the OutputSection have the same dependency.
927 if (auto *D = this->Sections.front()->getLinkOrderDep())
928 this->Header.sh_link = D->OutSec->SectionIndex;
929 }
930 }
George Rimar58941ee2016-02-25 08:23:37 +0000931 if (Type != SHT_RELA && Type != SHT_REL)
932 return;
933 this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex;
934 // sh_info for SHT_REL[A] sections should contain the section header index of
935 // the section to which the relocation applies.
936 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
937 this->Header.sh_info = S->OutSec->SectionIndex;
938}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000939
940template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000941void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000942 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000943 auto *S = cast<InputSection<ELFT>>(C);
944 Sections.push_back(S);
945 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000946 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000947 // Keep sh_entsize value of the input section to be able to perform merging
948 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000949 if (Config->Relocatable && (S->Flags & SHF_MERGE))
950 this->Header.sh_entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000951}
952
Rui Ueyama809d8e22016-06-23 04:33:42 +0000953// This function is called after we sort input sections
954// and scan relocations to setup sections' offsets.
955template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
956 uintX_t Off = this->Header.sh_size;
957 for (InputSection<ELFT> *S : Sections) {
958 Off = alignTo(Off, S->Alignment);
959 S->OutSecOff = Off;
960 Off += S->getSize();
961 }
962 this->Header.sh_size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000963}
964
Rui Ueyamac4185702016-02-10 23:20:42 +0000965// Sorts input sections by section name suffixes, so that .foo.N comes
966// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000967// We want to keep the original order if the priorities are the same
968// because the compiler keeps the original initialization order in a
969// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000970// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000971template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000972 // Sort sections by priority.
973 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000974 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
975
Rui Ueyamac4185702016-02-10 23:20:42 +0000976 std::vector<Pair> V;
977 for (InputSection<ELFT> *S : Sections)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000978 V.push_back({getPriority(S->Name), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000979 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000980 Sections.clear();
981 for (Pair &P : V)
982 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000983}
Rui Ueyamac4185702016-02-10 23:20:42 +0000984
Rui Ueyama5af83682016-02-11 23:41:38 +0000985// Returns true if S matches /Filename.?\.o$/.
986static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
987 if (!S.endswith(".o"))
988 return false;
989 S = S.drop_back(2);
990 if (S.endswith(Filename))
991 return true;
992 return !S.empty() && S.drop_back().endswith(Filename);
993}
994
995static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
996static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
997
998// .ctors and .dtors are sorted by this priority from highest to lowest.
999//
1000// 1. The section was contained in crtbegin (crtbegin contains
1001// some sentinel value in its .ctors and .dtors so that the runtime
1002// can find the beginning of the sections.)
1003//
1004// 2. The section has an optional priority value in the form of ".ctors.N"
1005// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
1006// they are compared as string rather than number.
1007//
1008// 3. The section is just ".ctors" or ".dtors".
1009//
1010// 4. The section was contained in crtend, which contains an end marker.
1011//
1012// In an ideal world, we don't need this function because .init_array and
1013// .ctors are duplicate features (and .init_array is newer.) However, there
1014// are too many real-world use cases of .ctors, so we had no choice to
1015// support that with this rather ad-hoc semantics.
1016template <class ELFT>
1017static bool compCtors(const InputSection<ELFT> *A,
1018 const InputSection<ELFT> *B) {
1019 bool BeginA = isCrtbegin(A->getFile()->getName());
1020 bool BeginB = isCrtbegin(B->getFile()->getName());
1021 if (BeginA != BeginB)
1022 return BeginA;
1023 bool EndA = isCrtend(A->getFile()->getName());
1024 bool EndB = isCrtend(B->getFile()->getName());
1025 if (EndA != EndB)
1026 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +00001027 StringRef X = A->Name;
1028 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +00001029 assert(X.startswith(".ctors") || X.startswith(".dtors"));
1030 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
1031 X = X.substr(6);
1032 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +00001033 if (X.empty() && Y.empty())
1034 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +00001035 return X < Y;
1036}
1037
1038// Sorts input sections by the special rules for .ctors and .dtors.
1039// Unfortunately, the rules are different from the one for .{init,fini}_array.
1040// Read the comment above.
1041template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
1042 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +00001043}
1044
George Rimare2ee72b2016-02-26 14:48:31 +00001045static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
1046 size_t I = 0;
1047 for (; I + A.size() < Size; I += A.size())
1048 memcpy(Buf + I, A.data(), A.size());
1049 memcpy(Buf + I, A.data(), Size - I);
1050}
1051
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001052template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +00001053 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +00001054 if (!Filler.empty())
1055 fill(Buf, this->getSize(), Filler);
Rui Ueyamaeb943e82016-11-04 18:22:36 +00001056 if (Config->Threads) {
1057 parallel_for_each(Sections.begin(), Sections.end(),
1058 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
1059 } else {
1060 for (InputSection<ELFT> *C : Sections)
1061 C->writeTo(Buf);
1062 }
George Rimare38cbab2016-09-26 19:22:50 +00001063 // Linker scripts may have BYTE()-family commands with which you
1064 // can write arbitrary bytes to the output. Process them if any.
1065 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001066}
1067
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001068template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +00001069EhOutputSection<ELFT>::EhOutputSection()
Rui Ueyama3b31e672016-05-23 16:24:16 +00001070 : OutputSectionBase<ELFT>(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001071
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001072// Search for an existing CIE record or create a new one.
1073// CIE records from input object files are uniquified by their contents
1074// and where their relocations point to.
1075template <class ELFT>
1076template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001077CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001078 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001079 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001080 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +00001081 if (read32<E>(Piece.data().data() + 4) != 0)
Rafael Espindola042a3f22016-09-08 14:06:08 +00001082 fatal("CIE expected at beginning of .eh_frame: " + Sec->Name);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001083
1084 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001085 unsigned FirstRelI = Piece.FirstRelocation;
1086 if (FirstRelI != (unsigned)-1)
1087 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001088
1089 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +00001090 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001091
1092 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001093 if (Cie->Piece == nullptr) {
1094 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001095 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001096 }
1097 return Cie;
1098}
1099
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001100// There is one FDE per function. Returns true if a given FDE
1101// points to a live function.
1102template <class ELFT>
1103template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001104bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001105 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001106 ArrayRef<RelTy> Rels) {
1107 unsigned FirstRelI = Piece.FirstRelocation;
1108 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001109 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +00001110 const RelTy &Rel = Rels[FirstRelI];
1111 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001112 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
1113 if (!D || !D->Section)
1114 return false;
1115 InputSectionBase<ELFT> *Target = D->Section->Repl;
1116 return Target && Target->Live;
1117}
1118
1119// .eh_frame is a sequence of CIE or FDE records. In general, there
1120// is one CIE record per input object file which is followed by
1121// a list of FDEs. This function searches an existing CIE or create a new
1122// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +00001123template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +00001124template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001125void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +00001126 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001127 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +00001128
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001129 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001130 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001131 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +00001132 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +00001133 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001134
1135 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001136 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001137 if (ID == 0) {
1138 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
1139 continue;
1140 }
1141
1142 uint32_t CieOffset = Offset + 4 - ID;
1143 CieRecord *Cie = OffsetToCie[CieOffset];
1144 if (!Cie)
1145 fatal("invalid CIE reference");
1146
1147 if (!isFdeLive(Piece, Sec, Rels))
1148 continue;
1149 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +00001150 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001151 }
1152}
1153
1154template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001155void EhOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001156 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001157 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001158 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001159 Sections.push_back(Sec);
1160
1161 // .eh_frame is a sequence of CIE or FDE records. This function
1162 // splits it into pieces so that we can call
1163 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +00001164 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001165 if (Sec->Pieces.empty())
1166 return;
1167
1168 if (const Elf_Shdr *RelSec = Sec->RelocSection) {
Rafael Espindolae19abab2016-11-03 20:44:50 +00001169 ELFFile<ELFT> Obj = Sec->getFile()->getObj();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001170 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola454fe152016-11-03 19:07:44 +00001171 addSectionAux(Sec, check(Obj.relas(RelSec)));
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001172 else
Rafael Espindola454fe152016-11-03 19:07:44 +00001173 addSectionAux(Sec, check(Obj.rels(RelSec)));
Rui Ueyama0de86c12016-01-27 22:23:44 +00001174 return;
1175 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001176 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001177}
1178
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001179template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +00001180static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
1181 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +00001182
1183 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001184 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +00001185 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +00001186}
1187
Rui Ueyama1e479c22016-05-23 15:07:59 +00001188template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rui Ueyamae9381bd2016-07-16 02:47:42 +00001189 if (this->Header.sh_size)
1190 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +00001191
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001192 size_t Off = 0;
1193 for (CieRecord *Cie : Cies) {
1194 Cie->Piece->OutputOff = Off;
1195 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001196
Rafael Espindola113860b2016-10-20 10:55:58 +00001197 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001198 Fde->OutputOff = Off;
1199 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001200 }
1201 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001202 this->Header.sh_size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001203}
1204
Rui Ueyamae75e9332016-05-23 03:00:33 +00001205template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
1206 const endianness E = ELFT::TargetEndianness;
1207 switch (Size) {
1208 case DW_EH_PE_udata2:
1209 return read16<E>(Buf);
1210 case DW_EH_PE_udata4:
1211 return read32<E>(Buf);
1212 case DW_EH_PE_udata8:
1213 return read64<E>(Buf);
1214 case DW_EH_PE_absptr:
1215 if (ELFT::Is64Bits)
1216 return read64<E>(Buf);
1217 return read32<E>(Buf);
1218 }
1219 fatal("unknown FDE size encoding");
1220}
1221
1222// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
1223// We need it to create .eh_frame_hdr section.
1224template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001225typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +00001226 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001227 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +00001228 // stored at FDE + 8 byte.
1229 size_t Off = FdeOff + 8;
1230 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
1231 if ((Enc & 0x70) == DW_EH_PE_absptr)
1232 return Addr;
1233 if ((Enc & 0x70) == DW_EH_PE_pcrel)
1234 return Addr + this->getVA() + Off;
1235 fatal("unknown FDE size relative encoding");
1236}
1237
Rui Ueyama1e479c22016-05-23 15:07:59 +00001238template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001239 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001240 for (CieRecord *Cie : Cies) {
1241 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001242 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001243
Rafael Espindola32aca872016-10-05 18:40:00 +00001244 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001245 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001246 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +00001247
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001248 // FDE's second word should have the offset to an associated CIE.
1249 // Write it.
1250 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001251 }
1252 }
1253
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001254 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +00001255 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +00001256
1257 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001258 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +00001259 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +00001260 if (Out<ELFT>::EhFrameHdr) {
1261 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +00001262 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +00001263 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +00001264 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rui Ueyama3b31e672016-05-23 16:24:16 +00001265 uintX_t FdeVA = this->getVA() + Fde->OutputOff;
1266 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
1267 }
Rui Ueyamae75e9332016-05-23 03:00:33 +00001268 }
1269 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001270}
1271
1272template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001273MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001274 uintX_t Flags, uintX_t Alignment)
1275 : OutputSectionBase<ELFT>(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001276 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001277
1278template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola259d6452016-10-04 22:43:38 +00001279 Builder.write(Buf);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001280}
1281
Rafael Espindolac159c962015-10-19 21:00:02 +00001282template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001283void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama10803512016-05-22 00:25:30 +00001284 auto *Sec = cast<MergeInputSection<ELFT>>(C);
1285 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001286 this->updateAlignment(Sec->Alignment);
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001287 this->Header.sh_entsize = Sec->Entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +00001288 Sections.push_back(Sec);
Rafael Espindolac159c962015-10-19 21:00:02 +00001289
Rafael Espindola113860b2016-10-20 10:55:58 +00001290 auto HashI = Sec->Hashes.begin();
1291 for (auto I = Sec->Pieces.begin(), E = Sec->Pieces.end(); I != E; ++I) {
1292 SectionPiece &Piece = *I;
1293 uint32_t Hash = *HashI;
1294 ++HashI;
Rui Ueyama3ea87272016-05-22 00:13:04 +00001295 if (!Piece.Live)
Rafael Espindola0b9531c2016-04-22 22:09:35 +00001296 continue;
Rafael Espindola113860b2016-10-20 10:55:58 +00001297 StringRef Data = toStringRef(Sec->getData(I));
1298 CachedHashStringRef V(Data, Hash);
Rafael Espindola5fc2b1d2016-10-05 19:36:02 +00001299 uintX_t OutputOffset = Builder.add(V);
Rafael Espindola7b8bb532016-10-05 15:35:18 +00001300 if (!shouldTailMerge())
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001301 Piece.OutputOff = OutputOffset;
Rafael Espindolac159c962015-10-19 21:00:02 +00001302 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001303}
1304
1305template <class ELFT>
Justin Lebaree34a732016-10-17 22:24:36 +00001306unsigned MergeOutputSection<ELFT>::getOffset(CachedHashStringRef Val) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001307 return Builder.getOffset(Val);
1308}
1309
1310template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1311 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1312}
1313
1314template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1315 if (shouldTailMerge())
1316 Builder.finalize();
Rafael Espindola259d6452016-10-04 22:43:38 +00001317 else
1318 Builder.finalizeInOrder();
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001319 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001320}
1321
Rui Ueyama406b4692016-05-27 14:39:13 +00001322template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() {
1323 for (MergeInputSection<ELFT> *Sec : Sections)
1324 Sec->finalizePieces();
1325}
1326
Rafael Espindolac159c962015-10-19 21:00:02 +00001327template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001328StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001329 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1330 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyama3b04d832016-07-14 05:46:24 +00001331 Dynamic(Dynamic) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +00001332
Rafael Espindolae2c24612016-01-29 01:24:25 +00001333// Adds a string to the string table. If HashIt is true we hash and check for
1334// duplicates. It is optional because the name of global symbols are already
1335// uniqued and hashing them again has a big cost for a small value: uniquing
1336// them with some other string that happens to be the same.
1337template <class ELFT>
1338unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1339 if (HashIt) {
1340 auto R = StringMap.insert(std::make_pair(S, Size));
1341 if (!R.second)
1342 return R.first->second;
1343 }
1344 unsigned Ret = Size;
1345 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001346 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001347 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001348}
1349
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001350template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001351 // ELF string tables start with NUL byte, so advance the pointer by one.
1352 ++Buf;
1353 for (StringRef S : Strings) {
1354 memcpy(Buf, S.data(), S.size());
1355 Buf += S.size() + 1;
1356 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001357}
1358
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001359template <class ELFT>
Rui Ueyama809d8e22016-06-23 04:33:42 +00001360typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
1361 if (OutputSec)
1362 return OutputSec->getVA() + OffsetInSec;
George Rimar3e6833b2016-08-19 15:46:28 +00001363 return InputSec->OutSec->getVA() + InputSec->getOffset(OffsetInSec);
Rui Ueyama809d8e22016-06-23 04:33:42 +00001364}
1365
1366template <class ELFT>
1367typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
1368 if (UseSymVA)
1369 return Sym->getVA<ELFT>(Addend);
1370 return Addend;
1371}
1372
1373template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
1374 if (Sym && !UseSymVA)
1375 return Sym->DynsymIndex;
1376 return 0;
1377}
1378
1379template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001380SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamaace4f902016-05-24 04:25:47 +00001381 StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001382 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1383 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001384 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyamaace4f902016-05-24 04:25:47 +00001385 StrTabSec(StrTabSec) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001386 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001387 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001388}
1389
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001390// Orders symbols according to their positions in the GOT,
1391// in compliance with MIPS ABI rules.
1392// See "Global Offset Table" in Chapter 5 in the following document
1393// for detailed description:
1394// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001395static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
Simon Atanasyand2980d32016-03-29 14:07:22 +00001396 // Sort entries related to non-local preemptible symbols by GOT indexes.
1397 // All other entries go to the first part of GOT in arbitrary order.
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001398 bool LIsInLocalGot = !L->IsInGlobalMipsGot;
1399 bool RIsInLocalGot = !R->IsInGlobalMipsGot;
Simon Atanasyan7b8481b2016-06-20 11:37:56 +00001400 if (LIsInLocalGot || RIsInLocalGot)
1401 return !RIsInLocalGot;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001402 return L->GotIndex < R->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001403}
1404
Rafael Espindolabadd3972016-04-08 15:30:56 +00001405static uint8_t getSymbolBinding(SymbolBody *Body) {
Peter Collingbourne4f952702016-05-01 04:55:03 +00001406 Symbol *S = Body->symbol();
George Rimar11f83b72016-08-19 08:31:02 +00001407 if (Config->Relocatable)
1408 return S->Binding;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001409 uint8_t Visibility = S->Visibility;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001410 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1411 return STB_LOCAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001412 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
Rafael Espindolabadd3972016-04-08 15:30:56 +00001413 return STB_GLOBAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001414 return S->Binding;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001415}
1416
Rui Ueyama0db335f2015-10-07 16:58:54 +00001417template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001418 if (this->Header.sh_size)
1419 return; // Already finalized.
1420
Rui Ueyama0db335f2015-10-07 16:58:54 +00001421 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001422 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001423 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001424
George Rimar58941ee2016-02-25 08:23:37 +00001425 if (Config->Relocatable) {
1426 size_t I = NumLocals;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001427 for (const SymbolTableEntry &S : Symbols)
1428 S.Symbol->DynsymIndex = ++I;
George Rimar58941ee2016-02-25 08:23:37 +00001429 return;
1430 }
1431
Igor Kudrinab665fc2015-10-20 21:47:58 +00001432 if (!StrTabSec.isDynamic()) {
1433 std::stable_sort(Symbols.begin(), Symbols.end(),
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001434 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1435 return getSymbolBinding(L.Symbol) == STB_LOCAL &&
1436 getSymbolBinding(R.Symbol) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001437 });
1438 return;
1439 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001440 if (Out<ELFT>::GnuHashTab)
1441 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1442 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001443 else if (Config->EMachine == EM_MIPS)
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001444 std::stable_sort(Symbols.begin(), Symbols.end(),
1445 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1446 return sortMipsSymbols(L.Symbol, R.Symbol);
1447 });
Igor Kudrinab665fc2015-10-20 21:47:58 +00001448 size_t I = 0;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001449 for (const SymbolTableEntry &S : Symbols)
1450 S.Symbol->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001451}
1452
George Rimara4c7e742016-10-20 08:36:42 +00001453template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001454 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001455}
1456
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001457template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001458 Buf += sizeof(Elf_Sym);
1459
1460 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1461 // .dynsym only contains global symbols.
George Rimar9503f6d2016-08-31 08:46:30 +00001462 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001463 writeLocalSymbols(Buf);
1464
1465 writeGlobalSymbols(Buf);
1466}
1467
1468template <class ELFT>
1469void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1470 // Iterate over all input object files to copy their local symbols
1471 // to the output symbol table pointed by Buf.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +00001472 for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001473 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1474 File->KeptLocalSyms) {
1475 const DefinedRegular<ELFT> &Body = *P.first;
1476 InputSectionBase<ELFT> *Section = Body.Section;
Rui Ueyamac55733e2015-09-30 00:54:29 +00001477 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001478
1479 if (!Section) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001480 ESym->st_shndx = SHN_ABS;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001481 ESym->st_value = Body.Value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001482 } else {
Rafael Espindolac159c962015-10-19 21:00:02 +00001483 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1484 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001485 ESym->st_value = OutSec->getVA() + Section->getOffset(Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001486 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001487 ESym->st_name = P.second;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001488 ESym->st_size = Body.template getSize<ELFT>();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001489 ESym->setBindingAndType(STB_LOCAL, Body.Type);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001490 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001491 }
1492 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001493}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001494
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001495template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001496void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001497 // Write the internal symbol table contents to the output symbol table
1498 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001499 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001500 for (const SymbolTableEntry &S : Symbols) {
1501 SymbolBody *Body = S.Symbol;
1502 size_t StrOff = S.StrTabOffset;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001503
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001504 uint8_t Type = Body->Type;
1505 uintX_t Size = Body->getSize<ELFT>();
Rafael Espindola8614c562015-10-06 14:33:58 +00001506
Igor Kudrin853b88d2015-10-20 20:52:14 +00001507 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001508 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001509 ESym->st_name = StrOff;
Peter Collingbourne4f952702016-05-01 04:55:03 +00001510 ESym->setVisibility(Body->symbol()->Visibility);
Rui Ueyamab5a69702016-02-01 21:00:35 +00001511 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001512
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001513 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001514 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001515 else if (isa<DefinedRegular<ELFT>>(Body))
1516 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +00001517
Simon Atanasyanf967f092016-09-29 12:58:36 +00001518 if (Config->EMachine == EM_MIPS) {
1519 // On MIPS we need to mark symbol which has a PLT entry and requires
1520 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1521 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1522 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1523 if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
1524 ESym->st_other |= STO_MIPS_PLT;
1525 if (Config->Relocatable) {
1526 auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
1527 if (D && D->isMipsPIC())
1528 ESym->st_other |= STO_MIPS_PIC;
1529 }
1530 }
Igor Kudrinab665fc2015-10-20 21:47:58 +00001531 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001532 }
1533}
1534
Igor Kudrin853b88d2015-10-20 20:52:14 +00001535template <class ELFT>
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001536const OutputSectionBase<ELFT> *
1537SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1538 switch (Sym->kind()) {
1539 case SymbolBody::DefinedSyntheticKind:
Peter Collingbourne6a422592016-05-03 01:21:08 +00001540 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001541 case SymbolBody::DefinedRegularKind: {
Rafael Espindola5ffa13c2016-04-15 00:15:02 +00001542 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +00001543 if (D.Section)
1544 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001545 break;
1546 }
1547 case SymbolBody::DefinedCommonKind:
Rui Ueyamae8a61022016-11-05 23:05:47 +00001548 return In<ELFT>::Common->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001549 case SymbolBody::SharedKind:
1550 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1551 return Out<ELFT>::Bss;
1552 break;
Peter Collingbourne60976ed2016-04-27 00:05:06 +00001553 case SymbolBody::UndefinedKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001554 case SymbolBody::LazyArchiveKind:
1555 case SymbolBody::LazyObjectKind:
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001556 break;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001557 }
1558 return nullptr;
1559}
1560
1561template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +00001562VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rui Ueyamaf5244642016-07-16 02:36:00 +00001563 : OutputSectionBase<ELFT>(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
1564 this->Header.sh_addralign = sizeof(uint32_t);
1565}
George Rimard3566302016-06-20 11:55:12 +00001566
1567static StringRef getFileDefName() {
1568 if (!Config->SoName.empty())
1569 return Config->SoName;
1570 return Config->OutputFile;
1571}
1572
1573template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1574 FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001575 for (VersionDefinition &V : Config->VersionDefinitions)
George Rimard3566302016-06-20 11:55:12 +00001576 V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name);
1577
1578 this->Header.sh_size =
1579 (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1580 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +00001581
1582 // sh_info should be set to the number of definitions. This fact is missed in
1583 // documentation, but confirmed by binutils community:
1584 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1585 this->Header.sh_info = getVerDefNum();
1586}
1587
Rui Ueyama9f619642016-07-16 02:29:45 +00001588template <class ELFT>
1589void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1590 StringRef Name, size_t NameOff) {
1591 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +00001592 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +00001593 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +00001594 Verdef->vd_aux = sizeof(Elf_Verdef);
1595 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1596 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +00001597 Verdef->vd_ndx = Index;
Davide Italianoe7fd0be2016-11-07 21:56:56 +00001598 Verdef->vd_hash = hashSysV(Name);
George Rimard3566302016-06-20 11:55:12 +00001599
Rui Ueyama9f619642016-07-16 02:29:45 +00001600 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1601 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +00001602 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001603}
1604
1605template <class ELFT>
1606void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001607 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1608
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001609 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001610 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1611 writeOne(Buf, V.Id, V.Name, V.NameOff);
1612 }
1613
1614 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +00001615 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +00001616 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001617}
1618
1619template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001620VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001621 : OutputSectionBase<ELFT>(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001622 this->Header.sh_addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001623}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001624
1625template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1626 this->Header.sh_size =
1627 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1);
1628 this->Header.sh_entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +00001629 // At the moment of june 2016 GNU docs does not mention that sh_link field
1630 // should be set, but Sun docs do. Also readelf relies on this field.
1631 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001632}
1633
1634template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1635 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001636 for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) {
1637 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001638 ++OutVersym;
1639 }
1640}
1641
1642template <class ELFT>
1643VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001644 : OutputSectionBase<ELFT>(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindolaaae59562016-04-29 23:20:30 +00001645 this->Header.sh_addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +00001646
1647 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1648 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1649 // First identifiers are reserved by verdef section if it exist.
1650 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001651}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001652
1653template <class ELFT>
1654void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1655 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +00001656 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001657 return;
1658 }
Rui Ueyama434b5612016-07-17 03:11:46 +00001659 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001660 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1661 // to create one by adding it to our needed list and creating a dynstr entry
1662 // for the soname.
1663 if (F->VerdefMap.empty())
1664 Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())});
1665 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1666 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1667 // prepare to create one by allocating a version identifier and creating a
1668 // dynstr entry for the version name.
1669 if (NV.Index == 0) {
1670 NV.StrTab = Out<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +00001671 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001672 NV.Index = NextIndex++;
1673 }
George Rimard3566302016-06-20 11:55:12 +00001674 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001675}
1676
1677template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1678 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1679 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1680 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1681
1682 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1683 // Create an Elf_Verneed for this DSO.
1684 Verneed->vn_version = 1;
1685 Verneed->vn_cnt = P.first->VerdefMap.size();
1686 Verneed->vn_file = P.second;
1687 Verneed->vn_aux =
1688 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1689 Verneed->vn_next = sizeof(Elf_Verneed);
1690 ++Verneed;
1691
1692 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1693 // VerdefMap, which will only contain references to needed version
1694 // definitions. Each Elf_Vernaux is based on the information contained in
1695 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1696 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1697 // data structures within a single input file.
1698 for (auto &NV : P.first->VerdefMap) {
1699 Vernaux->vna_hash = NV.first->vd_hash;
1700 Vernaux->vna_flags = 0;
1701 Vernaux->vna_other = NV.second.Index;
1702 Vernaux->vna_name = NV.second.StrTab;
1703 Vernaux->vna_next = sizeof(Elf_Vernaux);
1704 ++Vernaux;
1705 }
1706
1707 Vernaux[-1].vna_next = 0;
1708 }
1709 Verneed[-1].vn_next = 0;
1710}
1711
1712template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1713 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
1714 this->Header.sh_info = Needed.size();
1715 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1716 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1717 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1718 this->Header.sh_size = Size;
1719}
1720
1721template <class ELFT>
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001722MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1723 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1724 this->Header.sh_addralign = 4;
1725 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1726 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1727}
1728
1729template <class ELFT>
1730void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1731 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
Simon Atanasyan353fc1e92016-09-29 12:58:48 +00001732 if (Config->Relocatable)
1733 R->ri_gp_value = 0;
1734 else
1735 R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
Rui Ueyama70eed362016-01-06 22:42:43 +00001736 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001737}
1738
1739template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001740void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001741 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001742 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001743 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan84bb3552016-05-26 20:46:01 +00001744 S->OutSec = this;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001745}
1746
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001747template <class ELFT>
1748MipsOptionsOutputSection<ELFT>::MipsOptionsOutputSection()
1749 : OutputSectionBase<ELFT>(".MIPS.options", SHT_MIPS_OPTIONS,
1750 SHF_ALLOC | SHF_MIPS_NOSTRIP) {
1751 this->Header.sh_addralign = 8;
1752 this->Header.sh_entsize = 1;
1753 this->Header.sh_size = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
1754}
1755
1756template <class ELFT>
1757void MipsOptionsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1758 auto *Opt = reinterpret_cast<Elf_Mips_Options *>(Buf);
1759 Opt->kind = ODK_REGINFO;
1760 Opt->size = this->Header.sh_size;
1761 Opt->section = 0;
1762 Opt->info = 0;
1763 auto *Reg = reinterpret_cast<Elf_Mips_RegInfo *>(Buf + sizeof(*Opt));
Simon Atanasyan353fc1e92016-09-29 12:58:48 +00001764 if (Config->Relocatable)
1765 Reg->ri_gp_value = 0;
1766 else
1767 Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset;
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001768 Reg->ri_gprmask = GprMask;
1769}
1770
1771template <class ELFT>
1772void MipsOptionsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1773 auto *S = cast<MipsOptionsInputSection<ELFT>>(C);
1774 if (S->Reginfo)
1775 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan84bb3552016-05-26 20:46:01 +00001776 S->OutSec = this;
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001777}
1778
George Rimar6892afa2016-07-12 09:49:43 +00001779template <class ELFT>
Simon Atanasyan85c6b442016-08-12 06:28:49 +00001780MipsAbiFlagsOutputSection<ELFT>::MipsAbiFlagsOutputSection()
1781 : OutputSectionBase<ELFT>(".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC) {
1782 this->Header.sh_addralign = 8;
1783 this->Header.sh_entsize = sizeof(Elf_Mips_ABIFlags);
1784 this->Header.sh_size = sizeof(Elf_Mips_ABIFlags);
1785 memset(&Flags, 0, sizeof(Flags));
1786}
1787
1788template <class ELFT>
1789void MipsAbiFlagsOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1790 memcpy(Buf, &Flags, sizeof(Flags));
1791}
1792
1793template <class ELFT>
1794void MipsAbiFlagsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1795 // Check compatibility and merge fields from input .MIPS.abiflags
1796 // to the output one.
1797 auto *S = cast<MipsAbiFlagsInputSection<ELFT>>(C);
1798 S->OutSec = this;
1799 if (S->Flags->version != 0) {
1800 error(getFilename(S->getFile()) + ": unexpected .MIPS.abiflags version " +
1801 Twine(S->Flags->version));
1802 return;
1803 }
1804 // LLD checks ISA compatibility in getMipsEFlags(). Here we just
1805 // select the highest number of ISA/Rev/Ext.
1806 Flags.isa_level = std::max(Flags.isa_level, S->Flags->isa_level);
1807 Flags.isa_rev = std::max(Flags.isa_rev, S->Flags->isa_rev);
1808 Flags.isa_ext = std::max(Flags.isa_ext, S->Flags->isa_ext);
1809 Flags.gpr_size = std::max(Flags.gpr_size, S->Flags->gpr_size);
1810 Flags.cpr1_size = std::max(Flags.cpr1_size, S->Flags->cpr1_size);
1811 Flags.cpr2_size = std::max(Flags.cpr2_size, S->Flags->cpr2_size);
1812 Flags.ases |= S->Flags->ases;
1813 Flags.flags1 |= S->Flags->flags1;
1814 Flags.flags2 |= S->Flags->flags2;
1815 Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->Flags->fp_abi,
1816 getFilename(S->getFile()));
1817}
1818
1819template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +00001820static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001821 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +00001822}
1823
1824template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +00001825static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
1826 StringRef OutsecName) {
Rafael Espindola17c35832016-09-13 12:25:30 +00001827 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +00001828 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +00001829
1830 // For SHF_MERGE we create different output sections for each alignment.
1831 // This makes each output section simple and keeps a single level mapping from
1832 // input to output.
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +00001833 // In case of relocatable object generation we do not try to perform merging
1834 // and treat SHF_MERGE sections as regular ones, but also create different
1835 // output sections for them to allow merging at final linking stage.
Rafael Espindola17c35832016-09-13 12:25:30 +00001836 uintX_t Alignment = 0;
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +00001837 if (isa<MergeInputSection<ELFT>>(C) ||
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001838 (Config->Relocatable && (C->Flags & SHF_MERGE)))
1839 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola17c35832016-09-13 12:25:30 +00001840
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001841 return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +00001842}
1843
1844template <class ELFT>
George Rimar6892afa2016-07-12 09:49:43 +00001845std::pair<OutputSectionBase<ELFT> *, bool>
1846OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1847 StringRef OutsecName) {
1848 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +00001849 return create(Key, C);
1850}
George Rimar6892afa2016-07-12 09:49:43 +00001851
Rafael Espindola10897f12016-09-13 14:23:14 +00001852template <class ELFT>
1853std::pair<OutputSectionBase<ELFT> *, bool>
1854OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
1855 InputSectionBase<ELFT> *C) {
1856 uintX_t Flags = getOutFlags(C);
1857 OutputSectionBase<ELFT> *&Sec = Map[Key];
1858 if (Sec) {
1859 Sec->updateFlags(Flags);
1860 return {Sec, false};
1861 }
1862
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001863 uint32_t Type = C->Type;
Rafael Espindola16853bb2016-09-08 12:33:41 +00001864 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +00001865 case InputSectionBase<ELFT>::Regular:
Rui Ueyama95642b92016-11-01 23:09:07 +00001866 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +00001867 break;
1868 case InputSectionBase<ELFT>::EHFrame:
1869 return {Out<ELFT>::EhFrame, false};
1870 case InputSectionBase<ELFT>::Merge:
Rui Ueyama95642b92016-11-01 23:09:07 +00001871 Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +00001872 break;
1873 case InputSectionBase<ELFT>::MipsReginfo:
Rui Ueyama95642b92016-11-01 23:09:07 +00001874 Sec = make<MipsReginfoOutputSection<ELFT>>();
George Rimar6892afa2016-07-12 09:49:43 +00001875 break;
1876 case InputSectionBase<ELFT>::MipsOptions:
Rui Ueyama95642b92016-11-01 23:09:07 +00001877 Sec = make<MipsOptionsOutputSection<ELFT>>();
George Rimar6892afa2016-07-12 09:49:43 +00001878 break;
Simon Atanasyan85c6b442016-08-12 06:28:49 +00001879 case InputSectionBase<ELFT>::MipsAbiFlags:
Rui Ueyama95642b92016-11-01 23:09:07 +00001880 Sec = make<MipsAbiFlagsOutputSection<ELFT>>();
Simon Atanasyan85c6b442016-08-12 06:28:49 +00001881 break;
George Rimar6892afa2016-07-12 09:49:43 +00001882 }
1883 return {Sec, true};
1884}
1885
George Rimar6892afa2016-07-12 09:49:43 +00001886template <bool Is64Bits>
1887typename lld::elf::SectionKey<Is64Bits>
1888DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
1889 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
1890}
1891
1892template <bool Is64Bits>
1893typename lld::elf::SectionKey<Is64Bits>
1894DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
1895 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
1896 0};
1897}
1898
1899template <bool Is64Bits>
1900unsigned
1901DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
1902 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
1903}
1904
1905template <bool Is64Bits>
1906bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
1907 const Key &RHS) {
1908 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
1909 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
1910 LHS.Alignment == RHS.Alignment;
1911}
1912
1913namespace llvm {
1914template struct DenseMapInfo<SectionKey<true>>;
1915template struct DenseMapInfo<SectionKey<false>>;
1916}
1917
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001918namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001919namespace elf {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001920template class OutputSectionBase<ELF32LE>;
1921template class OutputSectionBase<ELF32BE>;
1922template class OutputSectionBase<ELF64LE>;
1923template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001924
George Rimarf6bc65a2016-01-15 13:34:52 +00001925template class EhFrameHeader<ELF32LE>;
1926template class EhFrameHeader<ELF32BE>;
1927template class EhFrameHeader<ELF64LE>;
1928template class EhFrameHeader<ELF64BE>;
1929
George Rimar648a2c32015-10-20 08:54:27 +00001930template class GotPltSection<ELF32LE>;
1931template class GotPltSection<ELF32BE>;
1932template class GotPltSection<ELF64LE>;
1933template class GotPltSection<ELF64BE>;
1934
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001935template class GotSection<ELF32LE>;
1936template class GotSection<ELF32BE>;
1937template class GotSection<ELF64LE>;
1938template class GotSection<ELF64BE>;
1939
1940template class PltSection<ELF32LE>;
1941template class PltSection<ELF32BE>;
1942template class PltSection<ELF64LE>;
1943template class PltSection<ELF64BE>;
1944
1945template class RelocationSection<ELF32LE>;
1946template class RelocationSection<ELF32BE>;
1947template class RelocationSection<ELF64LE>;
1948template class RelocationSection<ELF64BE>;
1949
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001950template class GnuHashTableSection<ELF32LE>;
1951template class GnuHashTableSection<ELF32BE>;
1952template class GnuHashTableSection<ELF64LE>;
1953template class GnuHashTableSection<ELF64BE>;
1954
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001955template class HashTableSection<ELF32LE>;
1956template class HashTableSection<ELF32BE>;
1957template class HashTableSection<ELF64LE>;
1958template class HashTableSection<ELF64BE>;
1959
1960template class DynamicSection<ELF32LE>;
1961template class DynamicSection<ELF32BE>;
1962template class DynamicSection<ELF64LE>;
1963template class DynamicSection<ELF64BE>;
1964
1965template class OutputSection<ELF32LE>;
1966template class OutputSection<ELF32BE>;
1967template class OutputSection<ELF64LE>;
1968template class OutputSection<ELF64BE>;
1969
Rui Ueyama1e479c22016-05-23 15:07:59 +00001970template class EhOutputSection<ELF32LE>;
1971template class EhOutputSection<ELF32BE>;
1972template class EhOutputSection<ELF64LE>;
1973template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001974
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001975template class MipsReginfoOutputSection<ELF32LE>;
1976template class MipsReginfoOutputSection<ELF32BE>;
1977template class MipsReginfoOutputSection<ELF64LE>;
1978template class MipsReginfoOutputSection<ELF64BE>;
1979
Simon Atanasyanadd74f32016-05-04 10:07:38 +00001980template class MipsOptionsOutputSection<ELF32LE>;
1981template class MipsOptionsOutputSection<ELF32BE>;
1982template class MipsOptionsOutputSection<ELF64LE>;
1983template class MipsOptionsOutputSection<ELF64BE>;
1984
Simon Atanasyan85c6b442016-08-12 06:28:49 +00001985template class MipsAbiFlagsOutputSection<ELF32LE>;
1986template class MipsAbiFlagsOutputSection<ELF32BE>;
1987template class MipsAbiFlagsOutputSection<ELF64LE>;
1988template class MipsAbiFlagsOutputSection<ELF64BE>;
1989
Rafael Espindolac159c962015-10-19 21:00:02 +00001990template class MergeOutputSection<ELF32LE>;
1991template class MergeOutputSection<ELF32BE>;
1992template class MergeOutputSection<ELF64LE>;
1993template class MergeOutputSection<ELF64BE>;
1994
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001995template class StringTableSection<ELF32LE>;
1996template class StringTableSection<ELF32BE>;
1997template class StringTableSection<ELF64LE>;
1998template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001999
2000template class SymbolTableSection<ELF32LE>;
2001template class SymbolTableSection<ELF32BE>;
2002template class SymbolTableSection<ELF64LE>;
2003template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00002004
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00002005template class VersionTableSection<ELF32LE>;
2006template class VersionTableSection<ELF32BE>;
2007template class VersionTableSection<ELF64LE>;
2008template class VersionTableSection<ELF64BE>;
2009
2010template class VersionNeedSection<ELF32LE>;
2011template class VersionNeedSection<ELF32BE>;
2012template class VersionNeedSection<ELF64LE>;
2013template class VersionNeedSection<ELF64BE>;
2014
George Rimard3566302016-06-20 11:55:12 +00002015template class VersionDefinitionSection<ELF32LE>;
2016template class VersionDefinitionSection<ELF32BE>;
2017template class VersionDefinitionSection<ELF64LE>;
2018template class VersionDefinitionSection<ELF64BE>;
2019
George Rimar58fa5242016-10-20 09:19:48 +00002020template class GdbIndexSection<ELF32LE>;
2021template class GdbIndexSection<ELF32BE>;
2022template class GdbIndexSection<ELF64LE>;
2023template class GdbIndexSection<ELF64BE>;
2024
George Rimar6892afa2016-07-12 09:49:43 +00002025template class OutputSectionFactory<ELF32LE>;
2026template class OutputSectionFactory<ELF32BE>;
2027template class OutputSectionFactory<ELF64LE>;
2028template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00002029}
2030}