blob: 4df316e2c1cec2b0c16edd8875bd8053c36f4b6e [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
Rafael Espindolae08e78d2016-11-09 23:23:45 +000035OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
36 uint64_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037 : Name(Name) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000038 this->Type = Type;
39 this->Flags = Flags;
40 this->Addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041}
42
Rafael Espindolae08e78d2016-11-09 23:23:45 +000043uint32_t OutputSectionBase::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000044 uint32_t Ret = PF_R;
45 if (Flags & SHF_WRITE)
46 Ret |= PF_W;
47 if (Flags & SHF_EXECINSTR)
48 Ret |= PF_X;
49 return Ret;
50}
51
Rafael Espindola35c6af32015-09-25 17:19:10 +000052template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +000053void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000054 Shdr->sh_entsize = Entsize;
55 Shdr->sh_addralign = Addralign;
56 Shdr->sh_type = Type;
57 Shdr->sh_offset = Offset;
58 Shdr->sh_flags = Flags;
59 Shdr->sh_info = Info;
60 Shdr->sh_link = Link;
61 Shdr->sh_addr = Addr;
62 Shdr->sh_size = Size;
63 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000064}
65
66template <class ELFT>
George Rimar58fa5242016-10-20 09:19:48 +000067GdbIndexSection<ELFT>::GdbIndexSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +000068 : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {}
George Rimar58fa5242016-10-20 09:19:48 +000069
70template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
71 std::vector<InputSection<ELFT> *> &IS =
72 static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections;
73
74 for (InputSection<ELFT> *I : IS)
75 readDwarf(I);
76}
77
78template <class ELFT>
79void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
80 std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I);
81 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
82}
83
84template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
85 parseDebugSections();
86
87 // GdbIndex header consist from version fields
88 // and 5 more fields with different kinds of offsets.
89 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
Rafael Espindola04a2e342016-11-09 01:42:41 +000090 this->Size = CuTypesOffset;
George Rimar58fa5242016-10-20 09:19:48 +000091}
92
93template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
94 write32le(Buf, 7); // Write Version
95 write32le(Buf + 4, CuListOffset); // CU list offset
96 write32le(Buf + 8, CuTypesOffset); // Types CU list offset
97 write32le(Buf + 12, CuTypesOffset); // Address area offset
98 write32le(Buf + 16, CuTypesOffset); // Symbol table offset
99 write32le(Buf + 20, CuTypesOffset); // Constant pool offset
100 Buf += 24;
101
102 // Write the CU list.
103 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
104 write64le(Buf, CU.first);
105 write64le(Buf + 8, CU.second);
106 Buf += 16;
107 }
108}
109
110template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000111GotPltSection<ELFT>::GotPltSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000112 : OutputSectionBase(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000113 this->Addralign = Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000114}
115
Rafael Espindola67d72c02016-03-11 12:06:30 +0000116template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
117 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
118 Entries.push_back(&Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000119}
120
121template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000122 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +0000123}
124
George Rimar648a2c32015-10-20 08:54:27 +0000125template <class ELFT> void GotPltSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000126 this->Size = (Target->GotPltHeaderEntriesNum + Entries.size()) *
127 Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000128}
129
130template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000131 Target->writeGotPltHeader(Buf);
Rui Ueyama803b1202016-07-13 18:55:14 +0000132 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000133 for (const SymbolBody *B : Entries) {
Rui Ueyamac9fee5f2016-06-16 16:14:50 +0000134 Target->writeGotPlt(Buf, *B);
George Rimar648a2c32015-10-20 08:54:27 +0000135 Buf += sizeof(uintX_t);
136 }
137}
138
139template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000140GotSection<ELFT>::GotSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000141 : OutputSectionBase(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000142 if (Config->EMachine == EM_MIPS)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000143 this->Flags |= SHF_MIPS_GPREL;
144 this->Addralign = Target->GotEntrySize;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000145}
146
George Rimara4c7e742016-10-20 08:36:42 +0000147template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000148 Sym.GotIndex = Entries.size();
149 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +0000150}
151
Simon Atanasyan41325112016-06-19 21:39:37 +0000152template <class ELFT>
153void GotSection<ELFT>::addMipsEntry(SymbolBody &Sym, uintX_t Addend,
154 RelExpr Expr) {
155 // For "true" local symbols which can be referenced from the same module
156 // only compiler creates two instructions for address loading:
157 //
158 // lw $8, 0($gp) # R_MIPS_GOT16
159 // addi $8, $8, 0 # R_MIPS_LO16
160 //
161 // The first instruction loads high 16 bits of the symbol address while
162 // the second adds an offset. That allows to reduce number of required
163 // GOT entries because only one global offset table entry is necessary
164 // for every 64 KBytes of local data. So for local symbols we need to
165 // allocate number of GOT entries to hold all required "page" addresses.
166 //
167 // All global symbols (hidden and regular) considered by compiler uniformly.
168 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
169 // to load address of the symbol. So for each such symbol we need to
170 // allocate dedicated GOT entry to store its address.
171 //
172 // If a symbol is preemptible we need help of dynamic linker to get its
173 // final address. The corresponding GOT entries are allocated in the
174 // "global" part of GOT. Entries for non preemptible global symbol allocated
175 // in the "local" part of GOT.
176 //
177 // See "Global Offset Table" in Chapter 5:
178 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
179 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
180 // At this point we do not know final symbol value so to reduce number
181 // of allocated GOT entries do the following trick. Save all output
182 // sections referenced by GOT relocations. Then later in the `finalize`
183 // method calculate number of "pages" required to cover all saved output
184 // section and allocate appropriate number of GOT entries.
185 auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec;
186 MipsOutSections.insert(OutSec);
187 return;
188 }
Simon Atanasyan002e2442016-06-23 15:26:31 +0000189 if (Sym.isTls()) {
190 // GOT entries created for MIPS TLS relocations behave like
191 // almost GOT entries from other ABIs. They go to the end
192 // of the global offset table.
193 Sym.GotIndex = Entries.size();
194 Entries.push_back(&Sym);
195 return;
196 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000197 auto AddEntry = [&](SymbolBody &S, uintX_t A, MipsGotEntries &Items) {
198 if (S.isInGot() && !A)
199 return;
200 size_t NewIndex = Items.size();
201 if (!MipsGotMap.insert({{&S, A}, NewIndex}).second)
202 return;
203 Items.emplace_back(&S, A);
204 if (!A)
205 S.GotIndex = NewIndex;
206 };
207 if (Sym.isPreemptible()) {
208 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
209 AddEntry(Sym, 0, MipsGlobal);
210 Sym.IsInGlobalMipsGot = true;
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000211 } else if (Expr == R_MIPS_GOT_OFF32) {
212 AddEntry(Sym, Addend, MipsLocal32);
213 Sym.Is32BitMipsGot = true;
214 } else {
215 // Hold local GOT entries accessed via a 16-bit index separately.
216 // That allows to write them in the beginning of the GOT and keep
217 // their indexes as less as possible to escape relocation's overflow.
Simon Atanasyan41325112016-06-19 21:39:37 +0000218 AddEntry(Sym, Addend, MipsLocal);
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000219 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000220}
221
Rafael Espindola67d72c02016-03-11 12:06:30 +0000222template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000223 if (Sym.GlobalDynIndex != -1U)
George Rimar90cd0a82015-12-01 19:20:26 +0000224 return false;
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000225 Sym.GlobalDynIndex = Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +0000226 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +0000227 Entries.push_back(nullptr);
Rafael Espindolaa8777c22016-06-08 21:31:59 +0000228 Entries.push_back(&Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000229 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230}
231
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000232// Reserves TLS entries for a TLS module ID and a TLS block offset.
233// In total it takes two GOT slots.
234template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
235 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +0000236 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000237 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000238 Entries.push_back(nullptr);
239 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000240 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000241}
242
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243template <class ELFT>
244typename GotSection<ELFT>::uintX_t
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000245GotSection<ELFT>::getMipsLocalPageOffset(uintX_t EntryValue) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000246 // Initialize the entry by the %hi(EntryValue) expression
247 // but without right-shifting.
Simon Atanasyan41325112016-06-19 21:39:37 +0000248 EntryValue = (EntryValue + 0x8000) & ~0xffff;
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000249 // Take into account MIPS GOT header.
250 // See comment in the GotSection::writeTo.
251 size_t NewIndex = MipsLocalGotPos.size() + 2;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000252 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
Simon Atanasyan41325112016-06-19 21:39:37 +0000253 assert(!P.second || MipsLocalGotPos.size() <= MipsPageEntries);
George Rimar71e64b22016-05-11 09:41:15 +0000254 return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000255}
256
Igor Kudrin304860a2015-11-12 04:39:49 +0000257template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000258typename GotSection<ELFT>::uintX_t
Simon Atanasyan41325112016-06-19 21:39:37 +0000259GotSection<ELFT>::getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const {
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000260 // Calculate offset of the GOT entries block: TLS, global, local.
261 uintX_t GotBlockOff;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000262 if (B.isTls())
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000263 GotBlockOff = getMipsTlsOffset();
Simon Atanasyan002e2442016-06-23 15:26:31 +0000264 else if (B.IsInGlobalMipsGot)
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000265 GotBlockOff = getMipsLocalEntriesNum() * sizeof(uintX_t);
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000266 else if (B.Is32BitMipsGot)
267 GotBlockOff = (MipsPageEntries + MipsLocal.size()) * sizeof(uintX_t);
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000268 else
269 GotBlockOff = MipsPageEntries * sizeof(uintX_t);
270 // Calculate index of the GOT entry in the block.
271 uintX_t GotIndex;
272 if (B.isInGot())
273 GotIndex = B.GotIndex;
Simon Atanasyan41325112016-06-19 21:39:37 +0000274 else {
275 auto It = MipsGotMap.find({&B, Addend});
276 assert(It != MipsGotMap.end());
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000277 GotIndex = It->second;
Simon Atanasyan41325112016-06-19 21:39:37 +0000278 }
Simon Atanasyanf4415a32016-10-20 17:53:55 +0000279 return GotBlockOff + GotIndex * sizeof(uintX_t) - MipsGPOffset;
Simon Atanasyan41325112016-06-19 21:39:37 +0000280}
281
282template <class ELFT>
Simon Atanasyanbc946932016-10-19 17:13:43 +0000283typename GotSection<ELFT>::uintX_t GotSection<ELFT>::getMipsTlsOffset() const {
Simon Atanasyanbadd5b32016-10-20 17:53:59 +0000284 return (getMipsLocalEntriesNum() + MipsGlobal.size()) * sizeof(uintX_t);
Simon Atanasyan002e2442016-06-23 15:26:31 +0000285}
286
287template <class ELFT>
Simon Atanasyan41325112016-06-19 21:39:37 +0000288typename GotSection<ELFT>::uintX_t
George Rimar90cd0a82015-12-01 19:20:26 +0000289GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000290 return this->Addr + B.GlobalDynIndex * sizeof(uintX_t);
George Rimar90cd0a82015-12-01 19:20:26 +0000291}
292
293template <class ELFT>
Rafael Espindola74031ba2016-04-07 15:20:56 +0000294typename GotSection<ELFT>::uintX_t
295GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Rafael Espindola6211d9a2016-06-05 19:03:28 +0000296 return B.GlobalDynIndex * sizeof(uintX_t);
Rafael Espindola74031ba2016-04-07 15:20:56 +0000297}
298
299template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000300const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
Simon Atanasyan41325112016-06-19 21:39:37 +0000301 return MipsGlobal.empty() ? nullptr : MipsGlobal.front().first;
Igor Kudrin304860a2015-11-12 04:39:49 +0000302}
303
304template <class ELFT>
305unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000306 return MipsPageEntries + MipsLocal.size() + MipsLocal32.size();
Igor Kudrin304860a2015-11-12 04:39:49 +0000307}
308
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000309template <class ELFT> void GotSection<ELFT>::finalize() {
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000310 size_t EntriesNum = Entries.size();
311 if (Config->EMachine == EM_MIPS) {
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000312 // Take into account MIPS GOT header.
313 // See comment in the GotSection::writeTo.
Simon Atanasyan41325112016-06-19 21:39:37 +0000314 MipsPageEntries += 2;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000315 for (const OutputSectionBase *OutSec : MipsOutSections) {
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000316 // Calculate an upper bound of MIPS GOT entries required to store page
317 // addresses of local symbols. We assume the worst case - each 64kb
318 // page of the output section has at least one GOT relocation against it.
319 // Add 0x8000 to the section's size because the page address stored
320 // in the GOT entry is calculated as (value + 0x8000) & ~0xffff.
Rafael Espindola04a2e342016-11-09 01:42:41 +0000321 MipsPageEntries += (OutSec->Size + 0x8000 + 0xfffe) / 0xffff;
Simon Atanasyan311b4b12016-06-10 12:26:28 +0000322 }
Simon Atanasyanbadd5b32016-10-20 17:53:59 +0000323 EntriesNum += getMipsLocalEntriesNum() + MipsGlobal.size();
Simon Atanasyand2980d32016-03-29 14:07:22 +0000324 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000325 this->Size = EntriesNum * sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000326}
327
Rui Ueyamac442cff2016-09-08 21:46:21 +0000328template <class ELFT>
329static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
330 typedef typename ELFT::uint uintX_t;
331 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
332}
333
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000334template <class ELFT> void GotSection<ELFT>::writeMipsGot(uint8_t *Buf) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000335 // Set the MSB of the second GOT slot. This is not required by any
336 // MIPS ABI documentation, though.
337 //
338 // There is a comment in glibc saying that "The MSB of got[1] of a
339 // gnu object is set to identify gnu objects," and in GNU gold it
340 // says "the second entry will be used by some runtime loaders".
341 // But how this field is being used is unclear.
342 //
343 // We are not really willing to mimic other linkers behaviors
344 // without understanding why they do that, but because all files
345 // generated by GNU tools have this special GOT value, and because
346 // we've been doing this for years, it is probably a safe bet to
347 // keep doing this for now. We really need to revisit this to see
348 // if we had to do this.
349 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
350 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
351 // Write 'page address' entries to the local part of the GOT.
352 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
353 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000354 writeUint<ELFT>(Entry, L.first);
Simon Atanasyan1ef1bf82016-04-25 20:25:05 +0000355 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000356 Buf += MipsPageEntries * sizeof(uintX_t);
357 auto AddEntry = [&](const MipsGotEntry &SA) {
358 uint8_t *Entry = Buf;
359 Buf += sizeof(uintX_t);
George Rimara4c7e742016-10-20 08:36:42 +0000360 const SymbolBody *Body = SA.first;
George Rimar06e930d2016-06-20 10:01:50 +0000361 uintX_t VA = Body->template getVA<ELFT>(SA.second);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000362 writeUint<ELFT>(Entry, VA);
Simon Atanasyan41325112016-06-19 21:39:37 +0000363 };
364 std::for_each(std::begin(MipsLocal), std::end(MipsLocal), AddEntry);
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000365 std::for_each(std::begin(MipsLocal32), std::end(MipsLocal32), AddEntry);
Simon Atanasyan41325112016-06-19 21:39:37 +0000366 std::for_each(std::begin(MipsGlobal), std::end(MipsGlobal), AddEntry);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000367 // Initialize TLS-related GOT entries. If the entry has a corresponding
368 // dynamic relocations, leave it initialized by zero. Write down adjusted
369 // TLS symbol's values otherwise. To calculate the adjustments use offsets
370 // for thread-local storage.
371 // https://www.linux-mips.org/wiki/NPTL
372 if (TlsIndexOff != -1U && !Config->Pic)
Rui Ueyamac442cff2016-09-08 21:46:21 +0000373 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000374 for (const SymbolBody *B : Entries) {
375 if (!B || B->isPreemptible())
376 continue;
377 uintX_t VA = B->getVA<ELFT>();
378 if (B->GotIndex != -1U) {
379 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000380 writeUint<ELFT>(Entry, VA - 0x7000);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000381 }
382 if (B->GlobalDynIndex != -1U) {
383 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000384 writeUint<ELFT>(Entry, 1);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000385 Entry += sizeof(uintX_t);
Rui Ueyamac442cff2016-09-08 21:46:21 +0000386 writeUint<ELFT>(Entry, VA - 0x8000);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000387 }
388 }
Simon Atanasyan41325112016-06-19 21:39:37 +0000389}
390
391template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000392 if (Config->EMachine == EM_MIPS) {
Simon Atanasyan41325112016-06-19 21:39:37 +0000393 writeMipsGot(Buf);
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000394 return;
395 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000396 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000397 uint8_t *Entry = Buf;
398 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000399 if (!B)
400 continue;
Simon Atanasyan41325112016-06-19 21:39:37 +0000401 if (B->isPreemptible())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000402 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000403 uintX_t VA = B->getVA<ELFT>();
Rui Ueyamac442cff2016-09-08 21:46:21 +0000404 writeUint<ELFT>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000405 }
406}
407
Rafael Espindola35c6af32015-09-25 17:19:10 +0000408template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000409PltSection<ELFT>::PltSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000410 : OutputSectionBase(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000411 this->Addralign = 16;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000412}
413
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000414template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000415 // At beginning of PLT, we have code to call the dynamic linker
416 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama4a90f572016-06-16 16:28:50 +0000417 Target->writePltHeader(Buf);
418 size_t Off = Target->PltHeaderSize;
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000419
George Rimarfb5d7f22015-11-26 19:58:51 +0000420 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000421 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000422 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000423 uint64_t Got = B->getGotPltVA<ELFT>();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000424 uint64_t Plt = this->Addr + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000425 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000426 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000427 }
428}
429
Rafael Espindola67d72c02016-03-11 12:06:30 +0000430template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
431 Sym.PltIndex = Entries.size();
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000432 unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000433 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000434}
435
George Rimar648a2c32015-10-20 08:54:27 +0000436template <class ELFT> void PltSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000437 this->Size = Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000438}
439
440template <class ELFT>
George Rimarc191acf2016-05-10 15:47:57 +0000441RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000442 : OutputSectionBase(Name, Config->Rela ? SHT_RELA : SHT_REL, SHF_ALLOC),
George Rimarc191acf2016-05-10 15:47:57 +0000443 Sort(Sort) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000444 this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
445 this->Addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000446}
447
George Rimar5828c232015-11-30 17:49:19 +0000448template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000449void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
Eugene Leviantaa498192016-08-31 08:51:39 +0000450 if (Reloc.Type == Target->RelativeRel)
451 ++NumRelativeRelocs;
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000452 Relocs.push_back(Reloc);
453}
454
George Rimarc191acf2016-05-10 15:47:57 +0000455template <class ELFT, class RelTy>
456static bool compRelocations(const RelTy &A, const RelTy &B) {
Eugene Leviantaa498192016-08-31 08:51:39 +0000457 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
458 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
459 if (AIsRel != BIsRel)
460 return AIsRel;
461
George Rimarc191acf2016-05-10 15:47:57 +0000462 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
463}
464
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000465template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarc191acf2016-05-10 15:47:57 +0000466 uint8_t *BufBegin = Buf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000467 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rui Ueyama614be592016-03-13 05:23:40 +0000468 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000469 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000470
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000471 if (Config->Rela)
Rui Ueyama809d8e22016-06-23 04:33:42 +0000472 P->r_addend = Rel.getAddend();
473 P->r_offset = Rel.getOffset();
Simon Atanasyan002e2442016-06-23 15:26:31 +0000474 if (Config->EMachine == EM_MIPS && Rel.getOutputSec() == Out<ELFT>::Got)
475 // Dynamic relocation against MIPS GOT section make deal TLS entries
476 // allocated in the end of the GOT. We need to adjust the offset to take
477 // in account 'local' and 'global' GOT entries.
478 P->r_offset += Out<ELFT>::Got->getMipsTlsOffset();
Rui Ueyama809d8e22016-06-23 04:33:42 +0000479 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000480 }
George Rimarc191acf2016-05-10 15:47:57 +0000481
482 if (Sort) {
483 if (Config->Rela)
484 std::stable_sort((Elf_Rela *)BufBegin,
485 (Elf_Rela *)BufBegin + Relocs.size(),
486 compRelocations<ELFT, Elf_Rela>);
487 else
488 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
489 compRelocations<ELFT, Elf_Rel>);
490 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000491}
492
George Rimar77b77792015-11-25 22:15:01 +0000493template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000494 return this->Entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000495}
496
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000497template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000498 this->Link = Out<ELFT>::DynSymTab ? Out<ELFT>::DynSymTab->SectionIndex
499 : Out<ELFT>::SymTab->SectionIndex;
500 this->Size = Relocs.size() * this->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000501}
502
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000503template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000504HashTableSection<ELFT>::HashTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000505 : OutputSectionBase(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000506 this->Entsize = sizeof(Elf_Word);
507 this->Addralign = sizeof(Elf_Word);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000508}
509
Rui Ueyama0db335f2015-10-07 16:58:54 +0000510template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000511 this->Link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000512
George Rimare9e1d322016-02-18 15:17:01 +0000513 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000514 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000515
516 // Create as many buckets as there are symbols.
517 // FIXME: This is simplistic. We can try to optimize it, but implementing
518 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000519 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000520 this->Size = NumEntries * sizeof(Elf_Word);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000521}
522
523template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000524 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000525 auto *P = reinterpret_cast<Elf_Word *>(Buf);
526 *P++ = NumSymbols; // nbucket
527 *P++ = NumSymbols; // nchain
528
529 Elf_Word *Buckets = P;
530 Elf_Word *Chains = P + NumSymbols;
531
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000532 for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) {
533 SymbolBody *Body = S.Symbol;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000534 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000535 unsigned I = Body->DynsymIndex;
Davide Italianoe7fd0be2016-11-07 21:56:56 +0000536 uint32_t Hash = hashSysV(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000537 Chains[I] = Buckets[Hash];
538 Buckets[Hash] = I;
539 }
540}
541
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000542static uint32_t hashGnu(StringRef Name) {
543 uint32_t H = 5381;
544 for (uint8_t C : Name)
545 H = (H << 5) + H + C;
546 return H;
547}
548
549template <class ELFT>
550GnuHashTableSection<ELFT>::GnuHashTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000551 : OutputSectionBase(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000552 this->Entsize = ELFT::Is64Bits ? 0 : 4;
553 this->Addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000554}
555
556template <class ELFT>
557unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
558 if (!NumHashed)
559 return 0;
560
561 // These values are prime numbers which are not greater than 2^(N-1) + 1.
562 // In result, for any particular NumHashed we return a prime number
563 // which is not greater than NumHashed.
564 static const unsigned Primes[] = {
565 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
566 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
567
568 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
569 array_lengthof(Primes) - 1)];
570}
571
572// Bloom filter estimation: at least 8 bits for each hashed symbol.
573// GNU Hash table requirement: it should be a power of 2,
574// the minimum value is 1, even for an empty table.
575// Expected results for a 32-bit target:
576// calcMaskWords(0..4) = 1
577// calcMaskWords(5..8) = 2
578// calcMaskWords(9..16) = 4
579// For a 64-bit target:
580// calcMaskWords(0..8) = 1
581// calcMaskWords(9..16) = 2
582// calcMaskWords(17..32) = 4
583template <class ELFT>
584unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
585 if (!NumHashed)
586 return 1;
587 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
588}
589
590template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000591 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000592 NBuckets = calcNBuckets(NumHashed);
593 MaskWords = calcMaskWords(NumHashed);
594 // Second hash shift estimation: just predefined values.
595 Shift2 = ELFT::Is64Bits ? 6 : 5;
596
Rafael Espindola04a2e342016-11-09 01:42:41 +0000597 this->Link = Out<ELFT>::DynSymTab->SectionIndex;
598 this->Size = sizeof(Elf_Word) * 4 // Header
599 + sizeof(Elf_Off) * MaskWords // Bloom Filter
600 + sizeof(Elf_Word) * NBuckets // Hash Buckets
601 + sizeof(Elf_Word) * NumHashed; // Hash Values
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000602}
603
604template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
605 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000606 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000607 return;
608 writeBloomFilter(Buf);
609 writeHashTable(Buf);
610}
611
612template <class ELFT>
613void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
614 auto *P = reinterpret_cast<Elf_Word *>(Buf);
615 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000616 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000617 *P++ = MaskWords;
618 *P++ = Shift2;
619 Buf = reinterpret_cast<uint8_t *>(P);
620}
621
622template <class ELFT>
623void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
624 unsigned C = sizeof(Elf_Off) * 8;
625
626 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000627 for (const SymbolData &Sym : Symbols) {
628 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
629 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
630 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000631 Masks[Pos] |= V;
632 }
633 Buf += sizeof(Elf_Off) * MaskWords;
634}
635
636template <class ELFT>
637void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
638 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
639 Elf_Word *Values = Buckets + NBuckets;
640
641 int PrevBucket = -1;
642 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000643 for (const SymbolData &Sym : Symbols) {
644 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000645 assert(PrevBucket <= Bucket);
646 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000647 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000648 PrevBucket = Bucket;
649 if (I > 0)
650 Values[I - 1] |= 1;
651 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000652 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000653 ++I;
654 }
655 if (I > 0)
656 Values[I - 1] |= 1;
657}
658
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000659// Add symbols to this symbol hash table. Note that this function
660// destructively sort a given vector -- which is needed because
661// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000662template <class ELFT>
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000663void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
Davide Italianob4b68b62016-07-04 19:49:55 +0000664 // Ideally this will just be 'auto' but GCC 6.1 is not able
665 // to deduce it correctly.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000666 std::vector<SymbolTableEntry>::iterator Mid =
667 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
668 return S.Symbol->isUndefined();
669 });
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000670 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000671 return;
Davide Italianof8591cf2016-07-06 17:41:55 +0000672 for (auto I = Mid, E = V.end(); I != E; ++I) {
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000673 SymbolBody *B = I->Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000674 size_t StrOff = I->StrTabOffset;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000675 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
676 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000677
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000678 unsigned NBuckets = calcNBuckets(Symbols.size());
679 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000680 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000681 return L.Hash % NBuckets < R.Hash % NBuckets;
682 });
683
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000684 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000685 for (const SymbolData &Sym : Symbols)
686 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000687}
688
George Rimard3566302016-06-20 11:55:12 +0000689// Returns the number of version definition entries. Because the first entry
690// is for the version definition itself, it is the number of versioned symbols
691// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000692static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000693
Igor Kudrinf1d60292015-10-28 07:05:56 +0000694template <class ELFT>
Rui Ueyamaace4f902016-05-24 04:25:47 +0000695DynamicSection<ELFT>::DynamicSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000696 : OutputSectionBase(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000697 this->Addralign = sizeof(uintX_t);
698 this->Entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000699
700 // .dynamic section is not writable on MIPS.
701 // See "Special Section" in Chapter 4 in the following document:
702 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
703 if (Config->EMachine == EM_MIPS)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000704 this->Flags = SHF_ALLOC;
Rui Ueyamaa9593932016-11-02 02:18:01 +0000705
706 addEntries();
Rafael Espindola35c6af32015-09-25 17:19:10 +0000707}
708
Rui Ueyamaa9593932016-11-02 02:18:01 +0000709// There are some dynamic entries that don't depend on other sections.
710// Such entries can be set early.
711template <class ELFT> void DynamicSection<ELFT>::addEntries() {
712 // Add strings to .dynstr early so that .dynstr's size will be
713 // fixed early.
George Rimarb952ece2016-09-02 09:13:05 +0000714 for (StringRef S : Config->AuxiliaryList)
715 Add({DT_AUXILIARY, Out<ELFT>::DynStrTab->addString(S)});
Rafael Espindolade069362016-01-25 21:32:04 +0000716 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000717 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
718 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000719 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
Rafael Espindolade069362016-01-25 21:32:04 +0000720 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000721 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
722 if (!Config->SoName.empty())
723 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
724
Rui Ueyamaa9593932016-11-02 02:18:01 +0000725 // Set DT_FLAGS and DT_FLAGS_1.
726 uint32_t DtFlags = 0;
727 uint32_t DtFlags1 = 0;
728 if (Config->Bsymbolic)
729 DtFlags |= DF_SYMBOLIC;
730 if (Config->ZNodelete)
731 DtFlags1 |= DF_1_NODELETE;
732 if (Config->ZNow) {
733 DtFlags |= DF_BIND_NOW;
734 DtFlags1 |= DF_1_NOW;
735 }
736 if (Config->ZOrigin) {
737 DtFlags |= DF_ORIGIN;
738 DtFlags1 |= DF_1_ORIGIN;
739 }
740
741 if (DtFlags)
742 Add({DT_FLAGS, DtFlags});
743 if (DtFlags1)
744 Add({DT_FLAGS_1, DtFlags1});
745
746 if (!Config->Entry.empty())
747 Add({DT_DEBUG, (uint64_t)0});
748}
749
750// Add remaining entries to complete .dynamic contents.
751template <class ELFT> void DynamicSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000752 if (this->Size)
Rui Ueyamaa9593932016-11-02 02:18:01 +0000753 return; // Already finalized.
754
Rafael Espindola04a2e342016-11-09 01:42:41 +0000755 this->Link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindolade069362016-01-25 21:32:04 +0000756
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000757 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000758 bool IsRela = Config->Rela;
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000759 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
Rafael Espindola04a2e342016-11-09 01:42:41 +0000760 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->Size});
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000761 Add({IsRela ? DT_RELAENT : DT_RELENT,
762 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Eugene Leviantaa498192016-08-31 08:51:39 +0000763
Simon Atanasyan65b24612016-09-04 17:40:12 +0000764 // MIPS dynamic loader does not support RELCOUNT tag.
765 // The problem is in the tight relation between dynamic
766 // relocations and GOT. So do not emit this tag on MIPS.
767 if (Config->EMachine != EM_MIPS) {
768 size_t NumRelativeRels = Out<ELFT>::RelaDyn->getRelativeRelocCount();
769 if (Config->ZCombreloc && NumRelativeRels)
770 Add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
771 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000772 }
George Rimar648a2c32015-10-20 08:54:27 +0000773 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000774 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
Rafael Espindola04a2e342016-11-09 01:42:41 +0000775 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->Size});
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000776 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
777 Out<ELFT>::GotPlt});
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000778 Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000779 }
780
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000781 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
782 Add({DT_SYMENT, sizeof(Elf_Sym)});
783 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
Rafael Espindola04a2e342016-11-09 01:42:41 +0000784 Add({DT_STRSZ, Out<ELFT>::DynStrTab->Size});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000785 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000786 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000787 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000788 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000789
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000790 if (Out<ELFT>::PreinitArray) {
791 Add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
George Rimar03e05602016-08-19 15:23:39 +0000792 Add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
Rafael Espindolade069362016-01-25 21:32:04 +0000793 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000794 if (Out<ELFT>::InitArray) {
795 Add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
George Rimar03e05602016-08-19 15:23:39 +0000796 Add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
Rafael Espindolade069362016-01-25 21:32:04 +0000797 }
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000798 if (Out<ELFT>::FiniArray) {
799 Add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
George Rimar03e05602016-08-19 15:23:39 +0000800 Add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000801 }
802
Rui Ueyamaace4f902016-05-24 04:25:47 +0000803 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000804 Add({DT_INIT, B});
Rui Ueyamaace4f902016-05-24 04:25:47 +0000805 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000806 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000807
George Rimard3566302016-06-20 11:55:12 +0000808 bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0;
809 if (HasVerNeed || Out<ELFT>::VerDef)
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000810 Add({DT_VERSYM, Out<ELFT>::VerSym});
George Rimard3566302016-06-20 11:55:12 +0000811 if (Out<ELFT>::VerDef) {
812 Add({DT_VERDEF, Out<ELFT>::VerDef});
813 Add({DT_VERDEFNUM, getVerDefNum()});
814 }
815 if (HasVerNeed) {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000816 Add({DT_VERNEED, Out<ELFT>::VerNeed});
George Rimard3566302016-06-20 11:55:12 +0000817 Add({DT_VERNEEDNUM, Out<ELFT>::VerNeed->getNeedNum()});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000818 }
819
Igor Kudrin304860a2015-11-12 04:39:49 +0000820 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000821 Add({DT_MIPS_RLD_VERSION, 1});
822 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
Rui Ueyamacafc0f2e2016-07-14 17:40:18 +0000823 Add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000824 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
825 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000826 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000827 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000828 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000829 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
830 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000831 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000832 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000833 }
834
Rafael Espindolade069362016-01-25 21:32:04 +0000835 // +1 for DT_NULL
Rafael Espindola04a2e342016-11-09 01:42:41 +0000836 this->Size = (Entries.size() + 1) * this->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000837}
838
839template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000840 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
841
Rafael Espindolade069362016-01-25 21:32:04 +0000842 for (const Entry &E : Entries) {
843 P->d_tag = E.Tag;
844 switch (E.Kind) {
845 case Entry::SecAddr:
Rafael Espindola04a2e342016-11-09 01:42:41 +0000846 P->d_un.d_ptr = E.OutSec->Addr;
Rafael Espindolade069362016-01-25 21:32:04 +0000847 break;
George Rimar03e05602016-08-19 15:23:39 +0000848 case Entry::SecSize:
Rafael Espindola04a2e342016-11-09 01:42:41 +0000849 P->d_un.d_val = E.OutSec->Size;
George Rimar03e05602016-08-19 15:23:39 +0000850 break;
Rafael Espindolade069362016-01-25 21:32:04 +0000851 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000852 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000853 break;
854 case Entry::PlainInt:
855 P->d_un.d_val = E.Val;
856 break;
857 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000858 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000859 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000860}
861
862template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000863EhFrameHeader<ELFT>::EhFrameHeader()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000864 : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000865
Rui Ueyama95a232e2016-05-23 01:45:05 +0000866// .eh_frame_hdr contains a binary search table of pointers to FDEs.
867// Each entry of the search table consists of two values,
868// the starting PC from where FDEs covers, and the FDE's address.
869// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000870template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
871 const endianness E = ELFT::TargetEndianness;
872
Rui Ueyamae75e9332016-05-23 03:00:33 +0000873 // Sort the FDE list by their PC and uniqueify. Usually there is only
874 // one FDE for a PC (i.e. function), but if ICF merges two functions
875 // into one, there can be more than one FDEs pointing to the address.
876 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
877 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
878 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
879 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000880
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000881 Buf[0] = 1;
882 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
883 Buf[2] = DW_EH_PE_udata4;
884 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000885 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000886 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000887 Buf += 12;
888
Rafael Espindola04a2e342016-11-09 01:42:41 +0000889 uintX_t VA = this->Addr;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000890 for (FdeData &Fde : Fdes) {
891 write32<E>(Buf, Fde.Pc - VA);
892 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000893 Buf += 8;
894 }
895}
896
Rui Ueyamade9777a2016-05-23 16:30:41 +0000897template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
898 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
Rafael Espindola04a2e342016-11-09 01:42:41 +0000899 this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
Rui Ueyamade9777a2016-05-23 16:30:41 +0000900}
901
George Rimarf6bc65a2016-01-15 13:34:52 +0000902template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000903void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
904 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000905}
906
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000907template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
908 switch (Type) {
909 case SHT_RELA:
910 return sizeof(typename ELFT::Rela);
911 case SHT_REL:
912 return sizeof(typename ELFT::Rel);
913 case SHT_MIPS_REGINFO:
914 return sizeof(Elf_Mips_RegInfo<ELFT>);
915 case SHT_MIPS_OPTIONS:
916 return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
917 case SHT_MIPS_ABIFLAGS:
918 return sizeof(Elf_Mips_ABIFlags<ELFT>);
919 default:
920 return 0;
921 }
922}
923
George Rimarf6bc65a2016-01-15 13:34:52 +0000924template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000925OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000926 : OutputSectionBase(Name, Type, Flags) {
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000927 this->Entsize = getEntsize<ELFT>(Type);
George Rimar58941ee2016-02-25 08:23:37 +0000928}
929
930template <class ELFT> void OutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000931 uint32_t Type = this->Type;
932 if (this->Flags & SHF_LINK_ORDER) {
Peter Smith580ba952016-10-21 11:25:33 +0000933 if (!Config->Relocatable) {
934 // SHF_LINK_ORDER only has meaning in relocatable objects
Rafael Espindola04a2e342016-11-09 01:42:41 +0000935 this->Flags &= ~SHF_LINK_ORDER;
Peter Smith580ba952016-10-21 11:25:33 +0000936 }
937 else if (!this->Sections.empty()) {
938 // When doing a relocatable link we must preserve the link order
939 // dependency of sections with the SHF_LINK_ORDER flag. The dependency
940 // is indicated by the sh_link field. We need to translate the
941 // InputSection sh_link to the OutputSection sh_link, all InputSections
942 // in the OutputSection have the same dependency.
943 if (auto *D = this->Sections.front()->getLinkOrderDep())
Rafael Espindola04a2e342016-11-09 01:42:41 +0000944 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000945 }
946 }
George Rimar58941ee2016-02-25 08:23:37 +0000947 if (Type != SHT_RELA && Type != SHT_REL)
948 return;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000949 this->Link = Out<ELFT>::SymTab->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000950 // sh_info for SHT_REL[A] sections should contain the section header index of
951 // the section to which the relocation applies.
952 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000953 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000954}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000955
956template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000957void OutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000958 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000959 auto *S = cast<InputSection<ELFT>>(C);
960 Sections.push_back(S);
961 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000962 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000963 // Keep sh_entsize value of the input section to be able to perform merging
964 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000965 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000966 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000967}
968
Rui Ueyama809d8e22016-06-23 04:33:42 +0000969// This function is called after we sort input sections
970// and scan relocations to setup sections' offsets.
971template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000972 uintX_t Off = this->Size;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000973 for (InputSection<ELFT> *S : Sections) {
974 Off = alignTo(Off, S->Alignment);
975 S->OutSecOff = Off;
976 Off += S->getSize();
977 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000978 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000979}
980
Rui Ueyamac4185702016-02-10 23:20:42 +0000981// Sorts input sections by section name suffixes, so that .foo.N comes
982// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000983// We want to keep the original order if the priorities are the same
984// because the compiler keeps the original initialization order in a
985// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000986// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000987template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000988 // Sort sections by priority.
989 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000990 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
991
Rui Ueyamac4185702016-02-10 23:20:42 +0000992 std::vector<Pair> V;
993 for (InputSection<ELFT> *S : Sections)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000994 V.push_back({getPriority(S->Name), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000995 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000996 Sections.clear();
997 for (Pair &P : V)
998 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000999}
Rui Ueyamac4185702016-02-10 23:20:42 +00001000
Rui Ueyama5af83682016-02-11 23:41:38 +00001001// Returns true if S matches /Filename.?\.o$/.
1002static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
1003 if (!S.endswith(".o"))
1004 return false;
1005 S = S.drop_back(2);
1006 if (S.endswith(Filename))
1007 return true;
1008 return !S.empty() && S.drop_back().endswith(Filename);
1009}
1010
1011static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
1012static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
1013
1014// .ctors and .dtors are sorted by this priority from highest to lowest.
1015//
1016// 1. The section was contained in crtbegin (crtbegin contains
1017// some sentinel value in its .ctors and .dtors so that the runtime
1018// can find the beginning of the sections.)
1019//
1020// 2. The section has an optional priority value in the form of ".ctors.N"
1021// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
1022// they are compared as string rather than number.
1023//
1024// 3. The section is just ".ctors" or ".dtors".
1025//
1026// 4. The section was contained in crtend, which contains an end marker.
1027//
1028// In an ideal world, we don't need this function because .init_array and
1029// .ctors are duplicate features (and .init_array is newer.) However, there
1030// are too many real-world use cases of .ctors, so we had no choice to
1031// support that with this rather ad-hoc semantics.
1032template <class ELFT>
1033static bool compCtors(const InputSection<ELFT> *A,
1034 const InputSection<ELFT> *B) {
1035 bool BeginA = isCrtbegin(A->getFile()->getName());
1036 bool BeginB = isCrtbegin(B->getFile()->getName());
1037 if (BeginA != BeginB)
1038 return BeginA;
1039 bool EndA = isCrtend(A->getFile()->getName());
1040 bool EndB = isCrtend(B->getFile()->getName());
1041 if (EndA != EndB)
1042 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +00001043 StringRef X = A->Name;
1044 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +00001045 assert(X.startswith(".ctors") || X.startswith(".dtors"));
1046 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
1047 X = X.substr(6);
1048 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +00001049 if (X.empty() && Y.empty())
1050 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +00001051 return X < Y;
1052}
1053
1054// Sorts input sections by the special rules for .ctors and .dtors.
1055// Unfortunately, the rules are different from the one for .{init,fini}_array.
1056// Read the comment above.
1057template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
1058 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +00001059}
1060
George Rimare2ee72b2016-02-26 14:48:31 +00001061static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
1062 size_t I = 0;
1063 for (; I + A.size() < Size; I += A.size())
1064 memcpy(Buf + I, A.data(), A.size());
1065 memcpy(Buf + I, A.data(), Size - I);
1066}
1067
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001068template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +00001069 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +00001070 if (!Filler.empty())
Rafael Espindola04a2e342016-11-09 01:42:41 +00001071 fill(Buf, this->Size, Filler);
Rui Ueyamaeb943e82016-11-04 18:22:36 +00001072 if (Config->Threads) {
1073 parallel_for_each(Sections.begin(), Sections.end(),
1074 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
1075 } else {
1076 for (InputSection<ELFT> *C : Sections)
1077 C->writeTo(Buf);
1078 }
George Rimare38cbab2016-09-26 19:22:50 +00001079 // Linker scripts may have BYTE()-family commands with which you
1080 // can write arbitrary bytes to the output. Process them if any.
1081 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001082}
1083
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001084template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +00001085EhOutputSection<ELFT>::EhOutputSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001086 : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001087
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001088// Search for an existing CIE record or create a new one.
1089// CIE records from input object files are uniquified by their contents
1090// and where their relocations point to.
1091template <class ELFT>
1092template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001093CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001094 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001095 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001096 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +00001097 if (read32<E>(Piece.data().data() + 4) != 0)
Rafael Espindola042a3f22016-09-08 14:06:08 +00001098 fatal("CIE expected at beginning of .eh_frame: " + Sec->Name);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001099
1100 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001101 unsigned FirstRelI = Piece.FirstRelocation;
1102 if (FirstRelI != (unsigned)-1)
1103 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001104
1105 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +00001106 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001107
1108 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001109 if (Cie->Piece == nullptr) {
1110 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +00001111 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001112 }
1113 return Cie;
1114}
1115
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001116// There is one FDE per function. Returns true if a given FDE
1117// points to a live function.
1118template <class ELFT>
1119template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +00001120bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001121 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +00001122 ArrayRef<RelTy> Rels) {
1123 unsigned FirstRelI = Piece.FirstRelocation;
1124 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001125 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +00001126 const RelTy &Rel = Rels[FirstRelI];
1127 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001128 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
1129 if (!D || !D->Section)
1130 return false;
1131 InputSectionBase<ELFT> *Target = D->Section->Repl;
1132 return Target && Target->Live;
1133}
1134
1135// .eh_frame is a sequence of CIE or FDE records. In general, there
1136// is one CIE record per input object file which is followed by
1137// a list of FDEs. This function searches an existing CIE or create a new
1138// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +00001139template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +00001140template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001141void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +00001142 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001143 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +00001144
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001145 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +00001146 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001147 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +00001148 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +00001149 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001150
1151 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001152 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +00001153 if (ID == 0) {
1154 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
1155 continue;
1156 }
1157
1158 uint32_t CieOffset = Offset + 4 - ID;
1159 CieRecord *Cie = OffsetToCie[CieOffset];
1160 if (!Cie)
1161 fatal("invalid CIE reference");
1162
1163 if (!isFdeLive(Piece, Sec, Rels))
1164 continue;
1165 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +00001166 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001167 }
1168}
1169
1170template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001171void EhOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001172 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001173 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001174 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001175 Sections.push_back(Sec);
1176
1177 // .eh_frame is a sequence of CIE or FDE records. This function
1178 // splits it into pieces so that we can call
1179 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +00001180 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001181 if (Sec->Pieces.empty())
1182 return;
1183
1184 if (const Elf_Shdr *RelSec = Sec->RelocSection) {
Rafael Espindolae19abab2016-11-03 20:44:50 +00001185 ELFFile<ELFT> Obj = Sec->getFile()->getObj();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001186 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola454fe152016-11-03 19:07:44 +00001187 addSectionAux(Sec, check(Obj.relas(RelSec)));
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001188 else
Rafael Espindola454fe152016-11-03 19:07:44 +00001189 addSectionAux(Sec, check(Obj.rels(RelSec)));
Rui Ueyama0de86c12016-01-27 22:23:44 +00001190 return;
1191 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001192 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001193}
1194
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001195template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +00001196static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
1197 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +00001198
1199 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001200 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +00001201 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +00001202}
1203
Rui Ueyama1e479c22016-05-23 15:07:59 +00001204template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001205 if (this->Size)
Rui Ueyamae9381bd2016-07-16 02:47:42 +00001206 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +00001207
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001208 size_t Off = 0;
1209 for (CieRecord *Cie : Cies) {
1210 Cie->Piece->OutputOff = Off;
1211 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001212
Rafael Espindola113860b2016-10-20 10:55:58 +00001213 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001214 Fde->OutputOff = Off;
1215 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +00001216 }
1217 }
Rafael Espindola04a2e342016-11-09 01:42:41 +00001218 this->Size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001219}
1220
Rui Ueyamae75e9332016-05-23 03:00:33 +00001221template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
1222 const endianness E = ELFT::TargetEndianness;
1223 switch (Size) {
1224 case DW_EH_PE_udata2:
1225 return read16<E>(Buf);
1226 case DW_EH_PE_udata4:
1227 return read32<E>(Buf);
1228 case DW_EH_PE_udata8:
1229 return read64<E>(Buf);
1230 case DW_EH_PE_absptr:
1231 if (ELFT::Is64Bits)
1232 return read64<E>(Buf);
1233 return read32<E>(Buf);
1234 }
1235 fatal("unknown FDE size encoding");
1236}
1237
1238// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
1239// We need it to create .eh_frame_hdr section.
1240template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +00001241typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +00001242 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001243 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +00001244 // stored at FDE + 8 byte.
1245 size_t Off = FdeOff + 8;
1246 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
1247 if ((Enc & 0x70) == DW_EH_PE_absptr)
1248 return Addr;
1249 if ((Enc & 0x70) == DW_EH_PE_pcrel)
Rafael Espindola04a2e342016-11-09 01:42:41 +00001250 return Addr + this->Addr + Off;
Rui Ueyamae75e9332016-05-23 03:00:33 +00001251 fatal("unknown FDE size relative encoding");
1252}
1253
Rui Ueyama1e479c22016-05-23 15:07:59 +00001254template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001255 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001256 for (CieRecord *Cie : Cies) {
1257 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001258 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001259
Rafael Espindola32aca872016-10-05 18:40:00 +00001260 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001261 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +00001262 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +00001263
Rui Ueyamaf8b285c2016-05-22 23:16:14 +00001264 // FDE's second word should have the offset to an associated CIE.
1265 // Write it.
1266 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001267 }
1268 }
1269
Rui Ueyama0b9a9032016-05-24 04:19:20 +00001270 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +00001271 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +00001272
1273 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +00001274 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +00001275 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +00001276 if (Out<ELFT>::EhFrameHdr) {
1277 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +00001278 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +00001279 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +00001280 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rafael Espindola04a2e342016-11-09 01:42:41 +00001281 uintX_t FdeVA = this->Addr + Fde->OutputOff;
Rui Ueyama3b31e672016-05-23 16:24:16 +00001282 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
1283 }
Rui Ueyamae75e9332016-05-23 03:00:33 +00001284 }
1285 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001286}
1287
1288template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001289MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001290 uintX_t Flags, uintX_t Alignment)
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001291 : OutputSectionBase(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +00001292 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001293
1294template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola259d6452016-10-04 22:43:38 +00001295 Builder.write(Buf);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001296}
1297
Rafael Espindolac159c962015-10-19 21:00:02 +00001298template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001299void MergeOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama10803512016-05-22 00:25:30 +00001300 auto *Sec = cast<MergeInputSection<ELFT>>(C);
1301 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +00001302 this->updateAlignment(Sec->Alignment);
Rafael Espindola04a2e342016-11-09 01:42:41 +00001303 this->Entsize = Sec->Entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +00001304 Sections.push_back(Sec);
Rafael Espindolac159c962015-10-19 21:00:02 +00001305
Rafael Espindola113860b2016-10-20 10:55:58 +00001306 auto HashI = Sec->Hashes.begin();
1307 for (auto I = Sec->Pieces.begin(), E = Sec->Pieces.end(); I != E; ++I) {
1308 SectionPiece &Piece = *I;
1309 uint32_t Hash = *HashI;
1310 ++HashI;
Rui Ueyama3ea87272016-05-22 00:13:04 +00001311 if (!Piece.Live)
Rafael Espindola0b9531c2016-04-22 22:09:35 +00001312 continue;
Rafael Espindola113860b2016-10-20 10:55:58 +00001313 StringRef Data = toStringRef(Sec->getData(I));
1314 CachedHashStringRef V(Data, Hash);
Rafael Espindola5fc2b1d2016-10-05 19:36:02 +00001315 uintX_t OutputOffset = Builder.add(V);
Rafael Espindola7b8bb532016-10-05 15:35:18 +00001316 if (!shouldTailMerge())
Rui Ueyamac6ebb022016-05-22 01:03:41 +00001317 Piece.OutputOff = OutputOffset;
Rafael Espindolac159c962015-10-19 21:00:02 +00001318 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001319}
1320
1321template <class ELFT>
Justin Lebaree34a732016-10-17 22:24:36 +00001322unsigned MergeOutputSection<ELFT>::getOffset(CachedHashStringRef Val) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001323 return Builder.getOffset(Val);
1324}
1325
1326template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001327 return Config->Optimize >= 2 && this->Flags & SHF_STRINGS;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001328}
1329
1330template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1331 if (shouldTailMerge())
1332 Builder.finalize();
Rafael Espindola259d6452016-10-04 22:43:38 +00001333 else
1334 Builder.finalizeInOrder();
Rafael Espindola04a2e342016-11-09 01:42:41 +00001335 this->Size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001336}
1337
Rui Ueyama406b4692016-05-27 14:39:13 +00001338template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() {
1339 for (MergeInputSection<ELFT> *Sec : Sections)
1340 Sec->finalizePieces();
1341}
1342
Rafael Espindolac159c962015-10-19 21:00:02 +00001343template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001344StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001345 : OutputSectionBase(Name, SHT_STRTAB, Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola08113852016-11-08 20:25:44 +00001346 Dynamic(Dynamic) {
1347 // ELF string tables start with a NUL byte, so 1.
Rafael Espindola04a2e342016-11-09 01:42:41 +00001348 this->Size = 1;
Rafael Espindola08113852016-11-08 20:25:44 +00001349}
Rafael Espindola35c6af32015-09-25 17:19:10 +00001350
Rafael Espindolae2c24612016-01-29 01:24:25 +00001351// Adds a string to the string table. If HashIt is true we hash and check for
1352// duplicates. It is optional because the name of global symbols are already
1353// uniqued and hashing them again has a big cost for a small value: uniquing
1354// them with some other string that happens to be the same.
1355template <class ELFT>
1356unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1357 if (HashIt) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001358 auto R = StringMap.insert(std::make_pair(S, this->Size));
Rafael Espindolae2c24612016-01-29 01:24:25 +00001359 if (!R.second)
1360 return R.first->second;
1361 }
Rafael Espindola04a2e342016-11-09 01:42:41 +00001362 unsigned Ret = this->Size;
1363 this->Size = this->Size + S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001364 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001365 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001366}
1367
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001368template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001369 // ELF string tables start with NUL byte, so advance the pointer by one.
1370 ++Buf;
1371 for (StringRef S : Strings) {
1372 memcpy(Buf, S.data(), S.size());
1373 Buf += S.size() + 1;
1374 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001375}
1376
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001377template <class ELFT>
Rui Ueyama809d8e22016-06-23 04:33:42 +00001378typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
1379 if (OutputSec)
Rafael Espindola04a2e342016-11-09 01:42:41 +00001380 return OutputSec->Addr + OffsetInSec;
1381 return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec);
Rui Ueyama809d8e22016-06-23 04:33:42 +00001382}
1383
1384template <class ELFT>
1385typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
1386 if (UseSymVA)
1387 return Sym->getVA<ELFT>(Addend);
1388 return Addend;
1389}
1390
1391template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
1392 if (Sym && !UseSymVA)
1393 return Sym->DynsymIndex;
1394 return 0;
1395}
1396
1397template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001398SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamaace4f902016-05-24 04:25:47 +00001399 StringTableSection<ELFT> &StrTabSec)
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001400 : OutputSectionBase(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1401 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1402 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyamaace4f902016-05-24 04:25:47 +00001403 StrTabSec(StrTabSec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001404 this->Entsize = sizeof(Elf_Sym);
1405 this->Addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001406}
1407
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001408// Orders symbols according to their positions in the GOT,
1409// in compliance with MIPS ABI rules.
1410// See "Global Offset Table" in Chapter 5 in the following document
1411// for detailed description:
1412// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001413static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
Simon Atanasyand2980d32016-03-29 14:07:22 +00001414 // Sort entries related to non-local preemptible symbols by GOT indexes.
1415 // All other entries go to the first part of GOT in arbitrary order.
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001416 bool LIsInLocalGot = !L->IsInGlobalMipsGot;
1417 bool RIsInLocalGot = !R->IsInGlobalMipsGot;
Simon Atanasyan7b8481b2016-06-20 11:37:56 +00001418 if (LIsInLocalGot || RIsInLocalGot)
1419 return !RIsInLocalGot;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001420 return L->GotIndex < R->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001421}
1422
Rafael Espindolabadd3972016-04-08 15:30:56 +00001423static uint8_t getSymbolBinding(SymbolBody *Body) {
Peter Collingbourne4f952702016-05-01 04:55:03 +00001424 Symbol *S = Body->symbol();
George Rimar11f83b72016-08-19 08:31:02 +00001425 if (Config->Relocatable)
1426 return S->Binding;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001427 uint8_t Visibility = S->Visibility;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001428 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1429 return STB_LOCAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001430 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
Rafael Espindolabadd3972016-04-08 15:30:56 +00001431 return STB_GLOBAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001432 return S->Binding;
Rafael Espindolabadd3972016-04-08 15:30:56 +00001433}
1434
Rui Ueyama0db335f2015-10-07 16:58:54 +00001435template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001436 if (this->Size)
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001437 return; // Already finalized.
1438
Rafael Espindola04a2e342016-11-09 01:42:41 +00001439 this->Size = getNumSymbols() * sizeof(Elf_Sym);
1440 this->Link = StrTabSec.SectionIndex;
1441 this->Info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001442
George Rimar58941ee2016-02-25 08:23:37 +00001443 if (Config->Relocatable) {
1444 size_t I = NumLocals;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001445 for (const SymbolTableEntry &S : Symbols)
1446 S.Symbol->DynsymIndex = ++I;
George Rimar58941ee2016-02-25 08:23:37 +00001447 return;
1448 }
1449
Igor Kudrinab665fc2015-10-20 21:47:58 +00001450 if (!StrTabSec.isDynamic()) {
1451 std::stable_sort(Symbols.begin(), Symbols.end(),
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001452 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1453 return getSymbolBinding(L.Symbol) == STB_LOCAL &&
1454 getSymbolBinding(R.Symbol) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001455 });
1456 return;
1457 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001458 if (Out<ELFT>::GnuHashTab)
1459 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1460 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001461 else if (Config->EMachine == EM_MIPS)
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001462 std::stable_sort(Symbols.begin(), Symbols.end(),
1463 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1464 return sortMipsSymbols(L.Symbol, R.Symbol);
1465 });
Igor Kudrinab665fc2015-10-20 21:47:58 +00001466 size_t I = 0;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001467 for (const SymbolTableEntry &S : Symbols)
1468 S.Symbol->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001469}
1470
George Rimara4c7e742016-10-20 08:36:42 +00001471template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001472 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001473}
1474
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001475template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001476 Buf += sizeof(Elf_Sym);
1477
1478 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1479 // .dynsym only contains global symbols.
George Rimar9503f6d2016-08-31 08:46:30 +00001480 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001481 writeLocalSymbols(Buf);
1482
1483 writeGlobalSymbols(Buf);
1484}
1485
1486template <class ELFT>
1487void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1488 // Iterate over all input object files to copy their local symbols
1489 // to the output symbol table pointed by Buf.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +00001490 for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001491 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1492 File->KeptLocalSyms) {
1493 const DefinedRegular<ELFT> &Body = *P.first;
1494 InputSectionBase<ELFT> *Section = Body.Section;
Rui Ueyamac55733e2015-09-30 00:54:29 +00001495 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001496
1497 if (!Section) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001498 ESym->st_shndx = SHN_ABS;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001499 ESym->st_value = Body.Value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001500 } else {
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001501 const OutputSectionBase *OutSec = Section->OutSec;
Rafael Espindolac159c962015-10-19 21:00:02 +00001502 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola04a2e342016-11-09 01:42:41 +00001503 ESym->st_value = OutSec->Addr + Section->getOffset(Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001504 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001505 ESym->st_name = P.second;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001506 ESym->st_size = Body.template getSize<ELFT>();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +00001507 ESym->setBindingAndType(STB_LOCAL, Body.Type);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001508 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001509 }
1510 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001511}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001512
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001513template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001514void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001515 // Write the internal symbol table contents to the output symbol table
1516 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001517 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001518 for (const SymbolTableEntry &S : Symbols) {
1519 SymbolBody *Body = S.Symbol;
1520 size_t StrOff = S.StrTabOffset;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001521
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +00001522 uint8_t Type = Body->Type;
1523 uintX_t Size = Body->getSize<ELFT>();
Rafael Espindola8614c562015-10-06 14:33:58 +00001524
Igor Kudrin853b88d2015-10-20 20:52:14 +00001525 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001526 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001527 ESym->st_name = StrOff;
Peter Collingbourne4f952702016-05-01 04:55:03 +00001528 ESym->setVisibility(Body->symbol()->Visibility);
Rui Ueyamab5a69702016-02-01 21:00:35 +00001529 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001530
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001531 if (const OutputSectionBase *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001532 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001533 else if (isa<DefinedRegular<ELFT>>(Body))
1534 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +00001535
Simon Atanasyanf967f092016-09-29 12:58:36 +00001536 if (Config->EMachine == EM_MIPS) {
1537 // On MIPS we need to mark symbol which has a PLT entry and requires
1538 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1539 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1540 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1541 if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
1542 ESym->st_other |= STO_MIPS_PLT;
1543 if (Config->Relocatable) {
1544 auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
1545 if (D && D->isMipsPIC())
1546 ESym->st_other |= STO_MIPS_PIC;
1547 }
1548 }
Igor Kudrinab665fc2015-10-20 21:47:58 +00001549 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001550 }
1551}
1552
Igor Kudrin853b88d2015-10-20 20:52:14 +00001553template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001554const OutputSectionBase *
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001555SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1556 switch (Sym->kind()) {
1557 case SymbolBody::DefinedSyntheticKind:
Peter Collingbourne6a422592016-05-03 01:21:08 +00001558 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001559 case SymbolBody::DefinedRegularKind: {
Rafael Espindola5ffa13c2016-04-15 00:15:02 +00001560 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +00001561 if (D.Section)
1562 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001563 break;
1564 }
1565 case SymbolBody::DefinedCommonKind:
Rui Ueyamae8a61022016-11-05 23:05:47 +00001566 return In<ELFT>::Common->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001567 case SymbolBody::SharedKind:
1568 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1569 return Out<ELFT>::Bss;
1570 break;
Peter Collingbourne60976ed2016-04-27 00:05:06 +00001571 case SymbolBody::UndefinedKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +00001572 case SymbolBody::LazyArchiveKind:
1573 case SymbolBody::LazyObjectKind:
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001574 break;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001575 }
1576 return nullptr;
1577}
1578
1579template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +00001580VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001581 : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001582 this->Addralign = sizeof(uint32_t);
Rui Ueyamaf5244642016-07-16 02:36:00 +00001583}
George Rimard3566302016-06-20 11:55:12 +00001584
1585static StringRef getFileDefName() {
1586 if (!Config->SoName.empty())
1587 return Config->SoName;
1588 return Config->OutputFile;
1589}
1590
1591template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1592 FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001593 for (VersionDefinition &V : Config->VersionDefinitions)
George Rimard3566302016-06-20 11:55:12 +00001594 V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name);
1595
Rafael Espindola04a2e342016-11-09 01:42:41 +00001596 this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1597 this->Link = Out<ELFT>::DynStrTab->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +00001598
1599 // sh_info should be set to the number of definitions. This fact is missed in
1600 // documentation, but confirmed by binutils community:
1601 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
Rafael Espindola04a2e342016-11-09 01:42:41 +00001602 this->Info = getVerDefNum();
George Rimard3566302016-06-20 11:55:12 +00001603}
1604
Rui Ueyama9f619642016-07-16 02:29:45 +00001605template <class ELFT>
1606void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1607 StringRef Name, size_t NameOff) {
1608 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +00001609 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +00001610 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +00001611 Verdef->vd_aux = sizeof(Elf_Verdef);
1612 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1613 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +00001614 Verdef->vd_ndx = Index;
Davide Italianoe7fd0be2016-11-07 21:56:56 +00001615 Verdef->vd_hash = hashSysV(Name);
George Rimard3566302016-06-20 11:55:12 +00001616
Rui Ueyama9f619642016-07-16 02:29:45 +00001617 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1618 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +00001619 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001620}
1621
1622template <class ELFT>
1623void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001624 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1625
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001626 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001627 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1628 writeOne(Buf, V.Id, V.Name, V.NameOff);
1629 }
1630
1631 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +00001632 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +00001633 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001634}
1635
1636template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001637VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001638 : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001639 this->Addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001640}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001641
1642template <class ELFT> void VersionTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001643 this->Size =
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001644 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1);
Rafael Espindola04a2e342016-11-09 01:42:41 +00001645 this->Entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +00001646 // At the moment of june 2016 GNU docs does not mention that sh_link field
1647 // should be set, but Sun docs do. Also readelf relies on this field.
Rafael Espindola04a2e342016-11-09 01:42:41 +00001648 this->Link = Out<ELFT>::DynSymTab->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001649}
1650
1651template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1652 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001653 for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) {
1654 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001655 ++OutVersym;
1656 }
1657}
1658
1659template <class ELFT>
1660VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001661 : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001662 this->Addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +00001663
1664 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1665 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1666 // First identifiers are reserved by verdef section if it exist.
1667 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001668}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001669
1670template <class ELFT>
1671void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1672 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +00001673 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001674 return;
1675 }
Rui Ueyama434b5612016-07-17 03:11:46 +00001676 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001677 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1678 // to create one by adding it to our needed list and creating a dynstr entry
1679 // for the soname.
1680 if (F->VerdefMap.empty())
1681 Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())});
1682 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1683 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1684 // prepare to create one by allocating a version identifier and creating a
1685 // dynstr entry for the version name.
1686 if (NV.Index == 0) {
1687 NV.StrTab = Out<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +00001688 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001689 NV.Index = NextIndex++;
1690 }
George Rimard3566302016-06-20 11:55:12 +00001691 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001692}
1693
1694template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1695 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1696 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1697 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1698
1699 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1700 // Create an Elf_Verneed for this DSO.
1701 Verneed->vn_version = 1;
1702 Verneed->vn_cnt = P.first->VerdefMap.size();
1703 Verneed->vn_file = P.second;
1704 Verneed->vn_aux =
1705 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1706 Verneed->vn_next = sizeof(Elf_Verneed);
1707 ++Verneed;
1708
1709 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1710 // VerdefMap, which will only contain references to needed version
1711 // definitions. Each Elf_Vernaux is based on the information contained in
1712 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1713 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1714 // data structures within a single input file.
1715 for (auto &NV : P.first->VerdefMap) {
1716 Vernaux->vna_hash = NV.first->vd_hash;
1717 Vernaux->vna_flags = 0;
1718 Vernaux->vna_other = NV.second.Index;
1719 Vernaux->vna_name = NV.second.StrTab;
1720 Vernaux->vna_next = sizeof(Elf_Vernaux);
1721 ++Vernaux;
1722 }
1723
1724 Vernaux[-1].vna_next = 0;
1725 }
1726 Verneed[-1].vn_next = 0;
1727}
1728
1729template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001730 this->Link = Out<ELFT>::DynStrTab->SectionIndex;
1731 this->Info = Needed.size();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001732 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1733 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1734 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
Rafael Espindola04a2e342016-11-09 01:42:41 +00001735 this->Size = Size;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001736}
1737
1738template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +00001739static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001740 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +00001741}
1742
1743template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +00001744static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
1745 StringRef OutsecName) {
Rafael Espindola17c35832016-09-13 12:25:30 +00001746 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +00001747 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +00001748
1749 // For SHF_MERGE we create different output sections for each alignment.
1750 // This makes each output section simple and keeps a single level mapping from
1751 // input to output.
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +00001752 // In case of relocatable object generation we do not try to perform merging
1753 // and treat SHF_MERGE sections as regular ones, but also create different
1754 // output sections for them to allow merging at final linking stage.
Rafael Espindola17c35832016-09-13 12:25:30 +00001755 uintX_t Alignment = 0;
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +00001756 if (isa<MergeInputSection<ELFT>>(C) ||
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001757 (Config->Relocatable && (C->Flags & SHF_MERGE)))
1758 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola17c35832016-09-13 12:25:30 +00001759
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001760 return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +00001761}
1762
1763template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001764std::pair<OutputSectionBase *, bool>
George Rimar6892afa2016-07-12 09:49:43 +00001765OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1766 StringRef OutsecName) {
1767 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +00001768 return create(Key, C);
1769}
George Rimar6892afa2016-07-12 09:49:43 +00001770
Rafael Espindola10897f12016-09-13 14:23:14 +00001771template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001772std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +00001773OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
1774 InputSectionBase<ELFT> *C) {
1775 uintX_t Flags = getOutFlags(C);
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001776 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +00001777 if (Sec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001778 Sec->Flags |= Flags;
Rafael Espindola10897f12016-09-13 14:23:14 +00001779 return {Sec, false};
1780 }
1781
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001782 uint32_t Type = C->Type;
Rafael Espindola16853bb2016-09-08 12:33:41 +00001783 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +00001784 case InputSectionBase<ELFT>::Regular:
Rui Ueyama95642b92016-11-01 23:09:07 +00001785 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +00001786 break;
1787 case InputSectionBase<ELFT>::EHFrame:
1788 return {Out<ELFT>::EhFrame, false};
1789 case InputSectionBase<ELFT>::Merge:
Rui Ueyama95642b92016-11-01 23:09:07 +00001790 Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +00001791 break;
George Rimar6892afa2016-07-12 09:49:43 +00001792 }
1793 return {Sec, true};
1794}
1795
George Rimar6892afa2016-07-12 09:49:43 +00001796template <bool Is64Bits>
1797typename lld::elf::SectionKey<Is64Bits>
1798DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
1799 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
1800}
1801
1802template <bool Is64Bits>
1803typename lld::elf::SectionKey<Is64Bits>
1804DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
1805 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
1806 0};
1807}
1808
1809template <bool Is64Bits>
1810unsigned
1811DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
1812 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
1813}
1814
1815template <bool Is64Bits>
1816bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
1817 const Key &RHS) {
1818 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
1819 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
1820 LHS.Alignment == RHS.Alignment;
1821}
1822
1823namespace llvm {
1824template struct DenseMapInfo<SectionKey<true>>;
1825template struct DenseMapInfo<SectionKey<false>>;
1826}
1827
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001828namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001829namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001830
1831template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
1832template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
1833template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
1834template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001835
George Rimarf6bc65a2016-01-15 13:34:52 +00001836template class EhFrameHeader<ELF32LE>;
1837template class EhFrameHeader<ELF32BE>;
1838template class EhFrameHeader<ELF64LE>;
1839template class EhFrameHeader<ELF64BE>;
1840
George Rimar648a2c32015-10-20 08:54:27 +00001841template class GotPltSection<ELF32LE>;
1842template class GotPltSection<ELF32BE>;
1843template class GotPltSection<ELF64LE>;
1844template class GotPltSection<ELF64BE>;
1845
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001846template class GotSection<ELF32LE>;
1847template class GotSection<ELF32BE>;
1848template class GotSection<ELF64LE>;
1849template class GotSection<ELF64BE>;
1850
1851template class PltSection<ELF32LE>;
1852template class PltSection<ELF32BE>;
1853template class PltSection<ELF64LE>;
1854template class PltSection<ELF64BE>;
1855
1856template class RelocationSection<ELF32LE>;
1857template class RelocationSection<ELF32BE>;
1858template class RelocationSection<ELF64LE>;
1859template class RelocationSection<ELF64BE>;
1860
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001861template class GnuHashTableSection<ELF32LE>;
1862template class GnuHashTableSection<ELF32BE>;
1863template class GnuHashTableSection<ELF64LE>;
1864template class GnuHashTableSection<ELF64BE>;
1865
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001866template class HashTableSection<ELF32LE>;
1867template class HashTableSection<ELF32BE>;
1868template class HashTableSection<ELF64LE>;
1869template class HashTableSection<ELF64BE>;
1870
1871template class DynamicSection<ELF32LE>;
1872template class DynamicSection<ELF32BE>;
1873template class DynamicSection<ELF64LE>;
1874template class DynamicSection<ELF64BE>;
1875
1876template class OutputSection<ELF32LE>;
1877template class OutputSection<ELF32BE>;
1878template class OutputSection<ELF64LE>;
1879template class OutputSection<ELF64BE>;
1880
Rui Ueyama1e479c22016-05-23 15:07:59 +00001881template class EhOutputSection<ELF32LE>;
1882template class EhOutputSection<ELF32BE>;
1883template class EhOutputSection<ELF64LE>;
1884template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001885
Rafael Espindolac159c962015-10-19 21:00:02 +00001886template class MergeOutputSection<ELF32LE>;
1887template class MergeOutputSection<ELF32BE>;
1888template class MergeOutputSection<ELF64LE>;
1889template class MergeOutputSection<ELF64BE>;
1890
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001891template class StringTableSection<ELF32LE>;
1892template class StringTableSection<ELF32BE>;
1893template class StringTableSection<ELF64LE>;
1894template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001895
1896template class SymbolTableSection<ELF32LE>;
1897template class SymbolTableSection<ELF32BE>;
1898template class SymbolTableSection<ELF64LE>;
1899template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001900
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001901template class VersionTableSection<ELF32LE>;
1902template class VersionTableSection<ELF32BE>;
1903template class VersionTableSection<ELF64LE>;
1904template class VersionTableSection<ELF64BE>;
1905
1906template class VersionNeedSection<ELF32LE>;
1907template class VersionNeedSection<ELF32BE>;
1908template class VersionNeedSection<ELF64LE>;
1909template class VersionNeedSection<ELF64BE>;
1910
George Rimard3566302016-06-20 11:55:12 +00001911template class VersionDefinitionSection<ELF32LE>;
1912template class VersionDefinitionSection<ELF32BE>;
1913template class VersionDefinitionSection<ELF64LE>;
1914template class VersionDefinitionSection<ELF64BE>;
1915
George Rimar58fa5242016-10-20 09:19:48 +00001916template class GdbIndexSection<ELF32LE>;
1917template class GdbIndexSection<ELF32BE>;
1918template class GdbIndexSection<ELF64LE>;
1919template class GdbIndexSection<ELF64BE>;
1920
George Rimar6892afa2016-07-12 09:49:43 +00001921template class OutputSectionFactory<ELF32LE>;
1922template class OutputSectionFactory<ELF32BE>;
1923template class OutputSectionFactory<ELF64LE>;
1924template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001925}
1926}