blob: 21198828b004bf81e0c33aaf28a3476a1830e0e1 [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"
George Rimar1a33c0f2016-11-10 09:05:20 +000017#include "SymbolListFile.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018#include "SymbolTable.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000019#include "SyntheticSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000020#include "Target.h"
Rui Ueyamae9809502016-03-11 04:23:12 +000021#include "lld/Core/Parallel.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000022#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000023#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000024#include "llvm/Support/MathExtras.h"
Rafael Espindolaa42b3bc2016-09-27 16:43:49 +000025#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000028using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000030using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031using namespace llvm::ELF;
32
33using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035
Rafael Espindolae08e78d2016-11-09 23:23:45 +000036OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
37 uint64_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038 : Name(Name) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000039 this->Type = Type;
40 this->Flags = Flags;
41 this->Addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042}
43
Rafael Espindolae08e78d2016-11-09 23:23:45 +000044uint32_t OutputSectionBase::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000045 uint32_t Ret = PF_R;
46 if (Flags & SHF_WRITE)
47 Ret |= PF_W;
48 if (Flags & SHF_EXECINSTR)
49 Ret |= PF_X;
50 return Ret;
51}
52
Rafael Espindola35c6af32015-09-25 17:19:10 +000053template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +000054void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000055 Shdr->sh_entsize = Entsize;
56 Shdr->sh_addralign = Addralign;
57 Shdr->sh_type = Type;
58 Shdr->sh_offset = Offset;
59 Shdr->sh_flags = Flags;
60 Shdr->sh_info = Info;
61 Shdr->sh_link = Link;
62 Shdr->sh_addr = Addr;
63 Shdr->sh_size = Size;
64 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000065}
66
67template <class ELFT>
George Rimar58fa5242016-10-20 09:19:48 +000068GdbIndexSection<ELFT>::GdbIndexSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +000069 : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {}
George Rimar58fa5242016-10-20 09:19:48 +000070
71template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
72 std::vector<InputSection<ELFT> *> &IS =
73 static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections;
74
75 for (InputSection<ELFT> *I : IS)
76 readDwarf(I);
77}
78
79template <class ELFT>
80void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
81 std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I);
82 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
83}
84
85template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
86 parseDebugSections();
87
88 // GdbIndex header consist from version fields
89 // and 5 more fields with different kinds of offsets.
90 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
Rafael Espindola04a2e342016-11-09 01:42:41 +000091 this->Size = CuTypesOffset;
George Rimar58fa5242016-10-20 09:19:48 +000092}
93
94template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
95 write32le(Buf, 7); // Write Version
96 write32le(Buf + 4, CuListOffset); // CU list offset
97 write32le(Buf + 8, CuTypesOffset); // Types CU list offset
98 write32le(Buf + 12, CuTypesOffset); // Address area offset
99 write32le(Buf + 16, CuTypesOffset); // Symbol table offset
100 write32le(Buf + 20, CuTypesOffset); // Constant pool offset
101 Buf += 24;
102
103 // Write the CU list.
104 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
105 write64le(Buf, CU.first);
106 write64le(Buf + 8, CU.second);
107 Buf += 16;
108 }
109}
110
111template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000112PltSection<ELFT>::PltSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000113 : OutputSectionBase(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000114 this->Addralign = 16;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000115}
116
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000118 // At beginning of PLT, we have code to call the dynamic linker
119 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama4a90f572016-06-16 16:28:50 +0000120 Target->writePltHeader(Buf);
121 size_t Off = Target->PltHeaderSize;
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000122
George Rimarfb5d7f22015-11-26 19:58:51 +0000123 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000124 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000125 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000126 uint64_t Got = B->getGotPltVA<ELFT>();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000127 uint64_t Plt = this->Addr + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000128 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000129 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130 }
131}
132
Rafael Espindola67d72c02016-03-11 12:06:30 +0000133template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
134 Sym.PltIndex = Entries.size();
Eugene Levianta96d9022016-11-16 10:02:27 +0000135 unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000136 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137}
138
George Rimar648a2c32015-10-20 08:54:27 +0000139template <class ELFT> void PltSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000140 this->Size = Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000141}
142
George Rimard3566302016-06-20 11:55:12 +0000143// Returns the number of version definition entries. Because the first entry
144// is for the version definition itself, it is the number of versioned symbols
145// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000146static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000147
Igor Kudrinf1d60292015-10-28 07:05:56 +0000148template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000149EhFrameHeader<ELFT>::EhFrameHeader()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000150 : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000151
Rui Ueyama95a232e2016-05-23 01:45:05 +0000152// .eh_frame_hdr contains a binary search table of pointers to FDEs.
153// Each entry of the search table consists of two values,
154// the starting PC from where FDEs covers, and the FDE's address.
155// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000156template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
157 const endianness E = ELFT::TargetEndianness;
158
Rui Ueyamae75e9332016-05-23 03:00:33 +0000159 // Sort the FDE list by their PC and uniqueify. Usually there is only
160 // one FDE for a PC (i.e. function), but if ICF merges two functions
161 // into one, there can be more than one FDEs pointing to the address.
162 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
163 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
164 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
165 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000166
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000167 Buf[0] = 1;
168 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
169 Buf[2] = DW_EH_PE_udata4;
170 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000171 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000172 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000173 Buf += 12;
174
Rafael Espindola04a2e342016-11-09 01:42:41 +0000175 uintX_t VA = this->Addr;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000176 for (FdeData &Fde : Fdes) {
177 write32<E>(Buf, Fde.Pc - VA);
178 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000179 Buf += 8;
180 }
181}
182
Rui Ueyamade9777a2016-05-23 16:30:41 +0000183template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
184 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
Rafael Espindola04a2e342016-11-09 01:42:41 +0000185 this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
Rui Ueyamade9777a2016-05-23 16:30:41 +0000186}
187
George Rimarf6bc65a2016-01-15 13:34:52 +0000188template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000189void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
190 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000191}
192
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000193template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
194 switch (Type) {
195 case SHT_RELA:
196 return sizeof(typename ELFT::Rela);
197 case SHT_REL:
198 return sizeof(typename ELFT::Rel);
199 case SHT_MIPS_REGINFO:
200 return sizeof(Elf_Mips_RegInfo<ELFT>);
201 case SHT_MIPS_OPTIONS:
202 return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
203 case SHT_MIPS_ABIFLAGS:
204 return sizeof(Elf_Mips_ABIFlags<ELFT>);
205 default:
206 return 0;
207 }
208}
209
George Rimarf6bc65a2016-01-15 13:34:52 +0000210template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000211OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000212 : OutputSectionBase(Name, Type, Flags) {
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000213 this->Entsize = getEntsize<ELFT>(Type);
George Rimar58941ee2016-02-25 08:23:37 +0000214}
215
216template <class ELFT> void OutputSection<ELFT>::finalize() {
Eugene Levianta96d9022016-11-16 10:02:27 +0000217 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
Rafael Espindola933fcab2016-11-17 23:16:39 +0000218 // We must preserve the link order dependency of sections with the
219 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
220 // need to translate the InputSection sh_link to the OutputSection sh_link,
221 // all InputSections in the OutputSection have the same dependency.
Eugene Levianta96d9022016-11-16 10:02:27 +0000222 if (auto *D = this->Sections.front()->getLinkOrderDep())
223 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000224 }
Eugene Leviant22eb0262016-11-14 09:16:00 +0000225
Rafael Espindola933fcab2016-11-17 23:16:39 +0000226 uint32_t Type = this->Type;
227 if (!Config->Relocatable || (Type != SHT_RELA && Type != SHT_REL))
George Rimar58941ee2016-02-25 08:23:37 +0000228 return;
Rafael Espindola933fcab2016-11-17 23:16:39 +0000229
Eugene Leviant9230db92016-11-17 09:16:34 +0000230 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000231 // sh_info for SHT_REL[A] sections should contain the section header index of
232 // the section to which the relocation applies.
233 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000234 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000235}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000236
237template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000238void OutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000239 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000240 auto *S = cast<InputSection<ELFT>>(C);
241 Sections.push_back(S);
242 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000243 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000244 // Keep sh_entsize value of the input section to be able to perform merging
245 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000246 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000247 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248}
249
Rui Ueyama809d8e22016-06-23 04:33:42 +0000250// This function is called after we sort input sections
251// and scan relocations to setup sections' offsets.
252template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000253 uintX_t Off = this->Size;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000254 for (InputSection<ELFT> *S : Sections) {
255 Off = alignTo(Off, S->Alignment);
256 S->OutSecOff = Off;
257 Off += S->getSize();
258 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000259 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000260}
261
George Rimar1a33c0f2016-11-10 09:05:20 +0000262template <class ELFT>
263void OutputSection<ELFT>::sort(
264 std::function<unsigned(InputSection<ELFT> *S)> Order) {
265 typedef std::pair<unsigned, InputSection<ELFT> *> Pair;
266 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
267
268 std::vector<Pair> V;
269 for (InputSection<ELFT> *S : Sections)
270 V.push_back({Order(S), S});
271 std::stable_sort(V.begin(), V.end(), Comp);
272 Sections.clear();
273 for (Pair &P : V)
274 Sections.push_back(P.second);
275}
276
Rui Ueyamac4185702016-02-10 23:20:42 +0000277// Sorts input sections by section name suffixes, so that .foo.N comes
278// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000279// We want to keep the original order if the priorities are the same
280// because the compiler keeps the original initialization order in a
281// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000282// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000283template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000284 // Sort sections by priority.
George Rimar1a33c0f2016-11-10 09:05:20 +0000285 sort([](InputSection<ELFT> *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000286}
Rui Ueyamac4185702016-02-10 23:20:42 +0000287
Rui Ueyama5af83682016-02-11 23:41:38 +0000288// Returns true if S matches /Filename.?\.o$/.
289static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
290 if (!S.endswith(".o"))
291 return false;
292 S = S.drop_back(2);
293 if (S.endswith(Filename))
294 return true;
295 return !S.empty() && S.drop_back().endswith(Filename);
296}
297
298static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
299static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
300
301// .ctors and .dtors are sorted by this priority from highest to lowest.
302//
303// 1. The section was contained in crtbegin (crtbegin contains
304// some sentinel value in its .ctors and .dtors so that the runtime
305// can find the beginning of the sections.)
306//
307// 2. The section has an optional priority value in the form of ".ctors.N"
308// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
309// they are compared as string rather than number.
310//
311// 3. The section is just ".ctors" or ".dtors".
312//
313// 4. The section was contained in crtend, which contains an end marker.
314//
315// In an ideal world, we don't need this function because .init_array and
316// .ctors are duplicate features (and .init_array is newer.) However, there
317// are too many real-world use cases of .ctors, so we had no choice to
318// support that with this rather ad-hoc semantics.
319template <class ELFT>
320static bool compCtors(const InputSection<ELFT> *A,
321 const InputSection<ELFT> *B) {
322 bool BeginA = isCrtbegin(A->getFile()->getName());
323 bool BeginB = isCrtbegin(B->getFile()->getName());
324 if (BeginA != BeginB)
325 return BeginA;
326 bool EndA = isCrtend(A->getFile()->getName());
327 bool EndB = isCrtend(B->getFile()->getName());
328 if (EndA != EndB)
329 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000330 StringRef X = A->Name;
331 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000332 assert(X.startswith(".ctors") || X.startswith(".dtors"));
333 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
334 X = X.substr(6);
335 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000336 if (X.empty() && Y.empty())
337 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000338 return X < Y;
339}
340
341// Sorts input sections by the special rules for .ctors and .dtors.
342// Unfortunately, the rules are different from the one for .{init,fini}_array.
343// Read the comment above.
344template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
345 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000346}
347
George Rimare2ee72b2016-02-26 14:48:31 +0000348static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
349 size_t I = 0;
350 for (; I + A.size() < Size; I += A.size())
351 memcpy(Buf + I, A.data(), A.size());
352 memcpy(Buf + I, A.data(), Size - I);
353}
354
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000355template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000356 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +0000357 if (!Filler.empty())
Rafael Espindola04a2e342016-11-09 01:42:41 +0000358 fill(Buf, this->Size, Filler);
Davide Italiano2e8b2a72016-11-18 02:23:48 +0000359 auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); };
Davide Italiano44665e72016-11-18 02:18:04 +0000360 if (Config->Threads)
361 parallel_for_each(Sections.begin(), Sections.end(), Fn);
362 else
363 std::for_each(Sections.begin(), Sections.end(), Fn);
George Rimare38cbab2016-09-26 19:22:50 +0000364 // Linker scripts may have BYTE()-family commands with which you
365 // can write arbitrary bytes to the output. Process them if any.
366 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000367}
368
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000369template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000370EhOutputSection<ELFT>::EhOutputSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000371 : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000372
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000373// Search for an existing CIE record or create a new one.
374// CIE records from input object files are uniquified by their contents
375// and where their relocations point to.
376template <class ELFT>
377template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000378CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000379 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000380 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000381 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +0000382 if (read32<E>(Piece.data().data() + 4) != 0)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000383 fatal("CIE expected at beginning of .eh_frame: " + Sec->Name);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000384
385 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000386 unsigned FirstRelI = Piece.FirstRelocation;
387 if (FirstRelI != (unsigned)-1)
388 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000389
390 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +0000391 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000392
393 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000394 if (Cie->Piece == nullptr) {
395 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000396 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000397 }
398 return Cie;
399}
400
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000401// There is one FDE per function. Returns true if a given FDE
402// points to a live function.
403template <class ELFT>
404template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000405bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000406 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000407 ArrayRef<RelTy> Rels) {
408 unsigned FirstRelI = Piece.FirstRelocation;
409 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000410 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +0000411 const RelTy &Rel = Rels[FirstRelI];
412 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000413 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
414 if (!D || !D->Section)
415 return false;
416 InputSectionBase<ELFT> *Target = D->Section->Repl;
417 return Target && Target->Live;
418}
419
420// .eh_frame is a sequence of CIE or FDE records. In general, there
421// is one CIE record per input object file which is followed by
422// a list of FDEs. This function searches an existing CIE or create a new
423// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +0000424template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000425template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000426void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000427 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000428 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +0000429
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000430 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000431 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000432 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +0000433 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +0000434 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000435
436 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000437 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000438 if (ID == 0) {
439 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
440 continue;
441 }
442
443 uint32_t CieOffset = Offset + 4 - ID;
444 CieRecord *Cie = OffsetToCie[CieOffset];
445 if (!Cie)
446 fatal("invalid CIE reference");
447
448 if (!isFdeLive(Piece, Sec, Rels))
449 continue;
450 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +0000451 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000452 }
453}
454
455template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000456void EhOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000457 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000458 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000459 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000460 Sections.push_back(Sec);
461
462 // .eh_frame is a sequence of CIE or FDE records. This function
463 // splits it into pieces so that we can call
464 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000465 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000466 if (Sec->Pieces.empty())
467 return;
468
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000469 if (Sec->NumRelocations) {
470 if (Sec->AreRelocsRela)
471 addSectionAux(Sec, Sec->relas());
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000472 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000473 addSectionAux(Sec, Sec->rels());
Rui Ueyama0de86c12016-01-27 22:23:44 +0000474 return;
475 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000476 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000477}
478
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000479template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +0000480static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
481 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +0000482
483 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000484 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +0000485 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +0000486}
487
Rui Ueyama1e479c22016-05-23 15:07:59 +0000488template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000489 if (this->Size)
Rui Ueyamae9381bd2016-07-16 02:47:42 +0000490 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +0000491
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000492 size_t Off = 0;
493 for (CieRecord *Cie : Cies) {
494 Cie->Piece->OutputOff = Off;
495 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000496
Rafael Espindola113860b2016-10-20 10:55:58 +0000497 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000498 Fde->OutputOff = Off;
499 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000500 }
501 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000502 this->Size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000503}
504
Rui Ueyamae75e9332016-05-23 03:00:33 +0000505template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
506 const endianness E = ELFT::TargetEndianness;
507 switch (Size) {
508 case DW_EH_PE_udata2:
509 return read16<E>(Buf);
510 case DW_EH_PE_udata4:
511 return read32<E>(Buf);
512 case DW_EH_PE_udata8:
513 return read64<E>(Buf);
514 case DW_EH_PE_absptr:
515 if (ELFT::Is64Bits)
516 return read64<E>(Buf);
517 return read32<E>(Buf);
518 }
519 fatal("unknown FDE size encoding");
520}
521
522// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
523// We need it to create .eh_frame_hdr section.
524template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000525typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +0000526 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000527 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +0000528 // stored at FDE + 8 byte.
529 size_t Off = FdeOff + 8;
530 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
531 if ((Enc & 0x70) == DW_EH_PE_absptr)
532 return Addr;
533 if ((Enc & 0x70) == DW_EH_PE_pcrel)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000534 return Addr + this->Addr + Off;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000535 fatal("unknown FDE size relative encoding");
536}
537
Rui Ueyama1e479c22016-05-23 15:07:59 +0000538template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000539 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000540 for (CieRecord *Cie : Cies) {
541 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000542 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000543
Rafael Espindola32aca872016-10-05 18:40:00 +0000544 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000545 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000546 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +0000547
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000548 // FDE's second word should have the offset to an associated CIE.
549 // Write it.
550 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000551 }
552 }
553
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000554 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +0000555 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000556
557 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000558 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +0000559 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +0000560 if (Out<ELFT>::EhFrameHdr) {
561 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +0000562 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +0000563 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +0000564 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000565 uintX_t FdeVA = this->Addr + Fde->OutputOff;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000566 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
567 }
Rui Ueyamae75e9332016-05-23 03:00:33 +0000568 }
569 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000570}
571
572template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000573MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000574 uintX_t Flags, uintX_t Alignment)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000575 : OutputSectionBase(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000576 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000577
578template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola259d6452016-10-04 22:43:38 +0000579 Builder.write(Buf);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000580}
581
Rafael Espindolac159c962015-10-19 21:00:02 +0000582template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000583void MergeOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama10803512016-05-22 00:25:30 +0000584 auto *Sec = cast<MergeInputSection<ELFT>>(C);
585 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000586 this->updateAlignment(Sec->Alignment);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000587 this->Entsize = Sec->Entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +0000588 Sections.push_back(Sec);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000589}
590
591template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
Rui Ueyama77f2a872016-11-18 05:05:43 +0000592 return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000593}
594
595template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
Rui Ueyama77f2a872016-11-18 05:05:43 +0000596 // Add all string pieces to the string table builder to create section
597 // contents. If we are not tail-optimizing, offsets of strings are fixed
598 // when they are added to the builder (string table builder contains a
599 // hash table from strings to offsets), so we record them if available.
600 for (MergeInputSection<ELFT> *Sec : Sections) {
601 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) {
602 if (!Sec->Pieces[I].Live)
603 continue;
604 uint32_t OutputOffset = Builder.add(Sec->getData(I));
605
606 // Save the offset in the generated string table.
607 if (!shouldTailMerge())
608 Sec->Pieces[I].OutputOff = OutputOffset;
609 }
610 }
611
612 // Fix the string table content. After this, the contents
613 // will never change.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000614 if (shouldTailMerge())
615 Builder.finalize();
Rafael Espindola259d6452016-10-04 22:43:38 +0000616 else
617 Builder.finalizeInOrder();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000618 this->Size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +0000619
Rui Ueyama77f2a872016-11-18 05:05:43 +0000620 // finalize() fixed tail-optimized strings, so we can now get
621 // offsets of strings. Get an offset for each string and save it
622 // to a corresponding StringPiece for easy access.
623 if (shouldTailMerge()) {
624 for (MergeInputSection<ELFT> *Sec : Sections) {
625 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) {
626 if (!Sec->Pieces[I].Live)
627 continue;
628 Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
629 }
630 }
631 }
Rui Ueyama406b4692016-05-27 14:39:13 +0000632}
633
Rafael Espindolac159c962015-10-19 21:00:02 +0000634template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +0000635VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000636 : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000637 this->Addralign = sizeof(uint32_t);
Rui Ueyamaf5244642016-07-16 02:36:00 +0000638}
George Rimard3566302016-06-20 11:55:12 +0000639
640static StringRef getFileDefName() {
641 if (!Config->SoName.empty())
642 return Config->SoName;
643 return Config->OutputFile;
644}
645
646template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
Eugene Leviant22eb0262016-11-14 09:16:00 +0000647 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000648 for (VersionDefinition &V : Config->VersionDefinitions)
Eugene Leviant22eb0262016-11-14 09:16:00 +0000649 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
George Rimard3566302016-06-20 11:55:12 +0000650
Rafael Espindola04a2e342016-11-09 01:42:41 +0000651 this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
Eugene Leviant22eb0262016-11-14 09:16:00 +0000652 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +0000653
654 // sh_info should be set to the number of definitions. This fact is missed in
655 // documentation, but confirmed by binutils community:
656 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
Rafael Espindola04a2e342016-11-09 01:42:41 +0000657 this->Info = getVerDefNum();
George Rimard3566302016-06-20 11:55:12 +0000658}
659
Rui Ueyama9f619642016-07-16 02:29:45 +0000660template <class ELFT>
661void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
662 StringRef Name, size_t NameOff) {
663 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +0000664 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +0000665 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +0000666 Verdef->vd_aux = sizeof(Elf_Verdef);
667 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
668 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +0000669 Verdef->vd_ndx = Index;
Davide Italianoe7fd0be2016-11-07 21:56:56 +0000670 Verdef->vd_hash = hashSysV(Name);
George Rimard3566302016-06-20 11:55:12 +0000671
Rui Ueyama9f619642016-07-16 02:29:45 +0000672 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
673 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +0000674 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +0000675}
676
677template <class ELFT>
678void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +0000679 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
680
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000681 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +0000682 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
683 writeOne(Buf, V.Id, V.Name, V.NameOff);
684 }
685
686 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +0000687 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +0000688 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +0000689}
690
691template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000692VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000693 : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000694 this->Addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +0000695}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000696
697template <class ELFT> void VersionTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000698 this->Size =
Eugene Leviant9230db92016-11-17 09:16:34 +0000699 sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000700 this->Entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +0000701 // At the moment of june 2016 GNU docs does not mention that sh_link field
702 // should be set, but Sun docs do. Also readelf relies on this field.
Eugene Leviant9230db92016-11-17 09:16:34 +0000703 this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000704}
705
706template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
707 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
Eugene Leviant9230db92016-11-17 09:16:34 +0000708 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000709 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000710 ++OutVersym;
711 }
712}
713
714template <class ELFT>
715VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000716 : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000717 this->Addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +0000718
719 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
720 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
721 // First identifiers are reserved by verdef section if it exist.
722 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +0000723}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000724
725template <class ELFT>
726void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
727 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +0000728 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000729 return;
730 }
Rui Ueyama434b5612016-07-17 03:11:46 +0000731 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000732 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
733 // to create one by adding it to our needed list and creating a dynstr entry
734 // for the soname.
735 if (F->VerdefMap.empty())
Eugene Leviant22eb0262016-11-14 09:16:00 +0000736 Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000737 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
738 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
739 // prepare to create one by allocating a version identifier and creating a
740 // dynstr entry for the version name.
741 if (NV.Index == 0) {
Eugene Leviant22eb0262016-11-14 09:16:00 +0000742 NV.StrTab = In<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +0000743 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000744 NV.Index = NextIndex++;
745 }
George Rimard3566302016-06-20 11:55:12 +0000746 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000747}
748
749template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
750 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
751 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
752 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
753
754 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
755 // Create an Elf_Verneed for this DSO.
756 Verneed->vn_version = 1;
757 Verneed->vn_cnt = P.first->VerdefMap.size();
758 Verneed->vn_file = P.second;
759 Verneed->vn_aux =
760 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
761 Verneed->vn_next = sizeof(Elf_Verneed);
762 ++Verneed;
763
764 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
765 // VerdefMap, which will only contain references to needed version
766 // definitions. Each Elf_Vernaux is based on the information contained in
767 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
768 // pointers, but is deterministic because the pointers refer to Elf_Verdef
769 // data structures within a single input file.
770 for (auto &NV : P.first->VerdefMap) {
771 Vernaux->vna_hash = NV.first->vd_hash;
772 Vernaux->vna_flags = 0;
773 Vernaux->vna_other = NV.second.Index;
774 Vernaux->vna_name = NV.second.StrTab;
775 Vernaux->vna_next = sizeof(Elf_Vernaux);
776 ++Vernaux;
777 }
778
779 Vernaux[-1].vna_next = 0;
780 }
781 Verneed[-1].vn_next = 0;
782}
783
784template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
Eugene Leviant22eb0262016-11-14 09:16:00 +0000785 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000786 this->Info = Needed.size();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000787 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
788 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
789 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000790 this->Size = Size;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000791}
792
793template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000794static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000795 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +0000796}
797
798template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +0000799static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
800 StringRef OutsecName) {
Rafael Espindola17c35832016-09-13 12:25:30 +0000801 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +0000802 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +0000803
804 // For SHF_MERGE we create different output sections for each alignment.
805 // This makes each output section simple and keeps a single level mapping from
806 // input to output.
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000807 // In case of relocatable object generation we do not try to perform merging
808 // and treat SHF_MERGE sections as regular ones, but also create different
809 // output sections for them to allow merging at final linking stage.
Rafael Espindola17c35832016-09-13 12:25:30 +0000810 uintX_t Alignment = 0;
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000811 if (isa<MergeInputSection<ELFT>>(C) ||
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000812 (Config->Relocatable && (C->Flags & SHF_MERGE)))
813 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola17c35832016-09-13 12:25:30 +0000814
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000815 return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000816}
817
818template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000819std::pair<OutputSectionBase *, bool>
George Rimar6892afa2016-07-12 09:49:43 +0000820OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
821 StringRef OutsecName) {
822 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000823 return create(Key, C);
824}
George Rimar6892afa2016-07-12 09:49:43 +0000825
Rafael Espindola10897f12016-09-13 14:23:14 +0000826template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000827std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +0000828OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
829 InputSectionBase<ELFT> *C) {
830 uintX_t Flags = getOutFlags(C);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000831 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +0000832 if (Sec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000833 Sec->Flags |= Flags;
Rafael Espindola10897f12016-09-13 14:23:14 +0000834 return {Sec, false};
835 }
836
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000837 uint32_t Type = C->Type;
Rafael Espindola16853bb2016-09-08 12:33:41 +0000838 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +0000839 case InputSectionBase<ELFT>::Regular:
Eugene Leviant41ca3272016-11-10 09:48:29 +0000840 case InputSectionBase<ELFT>::Synthetic:
Rui Ueyama95642b92016-11-01 23:09:07 +0000841 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +0000842 break;
843 case InputSectionBase<ELFT>::EHFrame:
844 return {Out<ELFT>::EhFrame, false};
845 case InputSectionBase<ELFT>::Merge:
Rui Ueyama95642b92016-11-01 23:09:07 +0000846 Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000847 break;
George Rimar6892afa2016-07-12 09:49:43 +0000848 }
849 return {Sec, true};
850}
851
George Rimar6892afa2016-07-12 09:49:43 +0000852template <bool Is64Bits>
853typename lld::elf::SectionKey<Is64Bits>
854DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
855 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
856}
857
858template <bool Is64Bits>
859typename lld::elf::SectionKey<Is64Bits>
860DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
861 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
862 0};
863}
864
865template <bool Is64Bits>
866unsigned
867DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
868 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
869}
870
871template <bool Is64Bits>
872bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
873 const Key &RHS) {
874 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
875 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
876 LHS.Alignment == RHS.Alignment;
877}
878
879namespace llvm {
880template struct DenseMapInfo<SectionKey<true>>;
881template struct DenseMapInfo<SectionKey<false>>;
882}
883
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000884namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000885namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000886
887template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
888template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
889template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
890template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000891
George Rimarf6bc65a2016-01-15 13:34:52 +0000892template class EhFrameHeader<ELF32LE>;
893template class EhFrameHeader<ELF32BE>;
894template class EhFrameHeader<ELF64LE>;
895template class EhFrameHeader<ELF64BE>;
896
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000897template class PltSection<ELF32LE>;
898template class PltSection<ELF32BE>;
899template class PltSection<ELF64LE>;
900template class PltSection<ELF64BE>;
901
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000902template class OutputSection<ELF32LE>;
903template class OutputSection<ELF32BE>;
904template class OutputSection<ELF64LE>;
905template class OutputSection<ELF64BE>;
906
Rui Ueyama1e479c22016-05-23 15:07:59 +0000907template class EhOutputSection<ELF32LE>;
908template class EhOutputSection<ELF32BE>;
909template class EhOutputSection<ELF64LE>;
910template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000911
Rafael Espindolac159c962015-10-19 21:00:02 +0000912template class MergeOutputSection<ELF32LE>;
913template class MergeOutputSection<ELF32BE>;
914template class MergeOutputSection<ELF64LE>;
915template class MergeOutputSection<ELF64BE>;
916
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000917template class VersionTableSection<ELF32LE>;
918template class VersionTableSection<ELF32BE>;
919template class VersionTableSection<ELF64LE>;
920template class VersionTableSection<ELF64BE>;
921
922template class VersionNeedSection<ELF32LE>;
923template class VersionNeedSection<ELF32BE>;
924template class VersionNeedSection<ELF64LE>;
925template class VersionNeedSection<ELF64BE>;
926
George Rimard3566302016-06-20 11:55:12 +0000927template class VersionDefinitionSection<ELF32LE>;
928template class VersionDefinitionSection<ELF32BE>;
929template class VersionDefinitionSection<ELF64LE>;
930template class VersionDefinitionSection<ELF64BE>;
931
George Rimar58fa5242016-10-20 09:19:48 +0000932template class GdbIndexSection<ELF32LE>;
933template class GdbIndexSection<ELF32BE>;
934template class GdbIndexSection<ELF64LE>;
935template class GdbIndexSection<ELF64BE>;
936
George Rimar6892afa2016-07-12 09:49:43 +0000937template class OutputSectionFactory<ELF32LE>;
938template class OutputSectionFactory<ELF32BE>;
939template class OutputSectionFactory<ELF64LE>;
940template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000941}
942}