blob: f94040d7e991656ec1c06646f6ebe0ceef14ef86 [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
George Rimard3566302016-06-20 11:55:12 +0000111// Returns the number of version definition entries. Because the first entry
112// is for the version definition itself, it is the number of versioned symbols
113// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000114static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000115
Igor Kudrinf1d60292015-10-28 07:05:56 +0000116template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000117EhFrameHeader<ELFT>::EhFrameHeader()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000118 : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000119
Rui Ueyama95a232e2016-05-23 01:45:05 +0000120// .eh_frame_hdr contains a binary search table of pointers to FDEs.
121// Each entry of the search table consists of two values,
122// the starting PC from where FDEs covers, and the FDE's address.
123// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000124template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
125 const endianness E = ELFT::TargetEndianness;
126
Rui Ueyamae75e9332016-05-23 03:00:33 +0000127 // Sort the FDE list by their PC and uniqueify. Usually there is only
128 // one FDE for a PC (i.e. function), but if ICF merges two functions
129 // into one, there can be more than one FDEs pointing to the address.
130 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
131 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
132 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
133 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000134
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000135 Buf[0] = 1;
136 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
137 Buf[2] = DW_EH_PE_udata4;
138 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000139 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000140 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000141 Buf += 12;
142
Rafael Espindola04a2e342016-11-09 01:42:41 +0000143 uintX_t VA = this->Addr;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000144 for (FdeData &Fde : Fdes) {
145 write32<E>(Buf, Fde.Pc - VA);
146 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000147 Buf += 8;
148 }
149}
150
Rui Ueyamade9777a2016-05-23 16:30:41 +0000151template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
152 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
Rafael Espindola04a2e342016-11-09 01:42:41 +0000153 this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
Rui Ueyamade9777a2016-05-23 16:30:41 +0000154}
155
George Rimarf6bc65a2016-01-15 13:34:52 +0000156template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000157void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
158 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000159}
160
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000161template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
162 switch (Type) {
163 case SHT_RELA:
164 return sizeof(typename ELFT::Rela);
165 case SHT_REL:
166 return sizeof(typename ELFT::Rel);
167 case SHT_MIPS_REGINFO:
168 return sizeof(Elf_Mips_RegInfo<ELFT>);
169 case SHT_MIPS_OPTIONS:
170 return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
171 case SHT_MIPS_ABIFLAGS:
172 return sizeof(Elf_Mips_ABIFlags<ELFT>);
173 default:
174 return 0;
175 }
176}
177
George Rimarf6bc65a2016-01-15 13:34:52 +0000178template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000179OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000180 : OutputSectionBase(Name, Type, Flags) {
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000181 this->Entsize = getEntsize<ELFT>(Type);
George Rimar58941ee2016-02-25 08:23:37 +0000182}
183
Rafael Espindola4d2d9c02016-11-18 19:02:15 +0000184template <typename ELFT>
185static bool compareByFilePosition(InputSection<ELFT> *A,
186 InputSection<ELFT> *B) {
187 auto *LA = cast<InputSection<ELFT>>(A->getLinkOrderDep());
188 auto *LB = cast<InputSection<ELFT>>(B->getLinkOrderDep());
189 OutputSectionBase *AOut = LA->OutSec;
190 OutputSectionBase *BOut = LB->OutSec;
191 if (AOut != BOut)
192 return AOut->SectionIndex < BOut->SectionIndex;
193 return LA->OutSecOff < LB->OutSecOff;
194}
195
George Rimar58941ee2016-02-25 08:23:37 +0000196template <class ELFT> void OutputSection<ELFT>::finalize() {
Eugene Levianta96d9022016-11-16 10:02:27 +0000197 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
Rafael Espindola4d2d9c02016-11-18 19:02:15 +0000198 std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>);
199 Size = 0;
200 assignOffsets();
201
Rafael Espindola933fcab2016-11-17 23:16:39 +0000202 // We must preserve the link order dependency of sections with the
203 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
204 // need to translate the InputSection sh_link to the OutputSection sh_link,
205 // all InputSections in the OutputSection have the same dependency.
Eugene Levianta96d9022016-11-16 10:02:27 +0000206 if (auto *D = this->Sections.front()->getLinkOrderDep())
207 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000208 }
Eugene Leviant22eb0262016-11-14 09:16:00 +0000209
Rafael Espindola933fcab2016-11-17 23:16:39 +0000210 uint32_t Type = this->Type;
211 if (!Config->Relocatable || (Type != SHT_RELA && Type != SHT_REL))
George Rimar58941ee2016-02-25 08:23:37 +0000212 return;
Rafael Espindola933fcab2016-11-17 23:16:39 +0000213
Eugene Leviant9230db92016-11-17 09:16:34 +0000214 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000215 // sh_info for SHT_REL[A] sections should contain the section header index of
216 // the section to which the relocation applies.
217 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000218 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000219}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000220
221template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000222void OutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000223 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000224 auto *S = cast<InputSection<ELFT>>(C);
225 Sections.push_back(S);
226 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000227 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000228 // Keep sh_entsize value of the input section to be able to perform merging
229 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000230 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000231 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232}
233
Rui Ueyama809d8e22016-06-23 04:33:42 +0000234// This function is called after we sort input sections
235// and scan relocations to setup sections' offsets.
236template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000237 uintX_t Off = this->Size;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000238 for (InputSection<ELFT> *S : Sections) {
239 Off = alignTo(Off, S->Alignment);
240 S->OutSecOff = Off;
241 Off += S->getSize();
242 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000243 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000244}
245
George Rimar1a33c0f2016-11-10 09:05:20 +0000246template <class ELFT>
247void OutputSection<ELFT>::sort(
248 std::function<unsigned(InputSection<ELFT> *S)> Order) {
249 typedef std::pair<unsigned, InputSection<ELFT> *> Pair;
250 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
251
252 std::vector<Pair> V;
253 for (InputSection<ELFT> *S : Sections)
254 V.push_back({Order(S), S});
255 std::stable_sort(V.begin(), V.end(), Comp);
256 Sections.clear();
257 for (Pair &P : V)
258 Sections.push_back(P.second);
259}
260
Rui Ueyamac4185702016-02-10 23:20:42 +0000261// Sorts input sections by section name suffixes, so that .foo.N comes
262// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000263// We want to keep the original order if the priorities are the same
264// because the compiler keeps the original initialization order in a
265// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000266// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000267template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000268 // Sort sections by priority.
George Rimar1a33c0f2016-11-10 09:05:20 +0000269 sort([](InputSection<ELFT> *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000270}
Rui Ueyamac4185702016-02-10 23:20:42 +0000271
Rui Ueyama5af83682016-02-11 23:41:38 +0000272// Returns true if S matches /Filename.?\.o$/.
273static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
274 if (!S.endswith(".o"))
275 return false;
276 S = S.drop_back(2);
277 if (S.endswith(Filename))
278 return true;
279 return !S.empty() && S.drop_back().endswith(Filename);
280}
281
282static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
283static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
284
285// .ctors and .dtors are sorted by this priority from highest to lowest.
286//
287// 1. The section was contained in crtbegin (crtbegin contains
288// some sentinel value in its .ctors and .dtors so that the runtime
289// can find the beginning of the sections.)
290//
291// 2. The section has an optional priority value in the form of ".ctors.N"
292// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
293// they are compared as string rather than number.
294//
295// 3. The section is just ".ctors" or ".dtors".
296//
297// 4. The section was contained in crtend, which contains an end marker.
298//
299// In an ideal world, we don't need this function because .init_array and
300// .ctors are duplicate features (and .init_array is newer.) However, there
301// are too many real-world use cases of .ctors, so we had no choice to
302// support that with this rather ad-hoc semantics.
303template <class ELFT>
304static bool compCtors(const InputSection<ELFT> *A,
305 const InputSection<ELFT> *B) {
306 bool BeginA = isCrtbegin(A->getFile()->getName());
307 bool BeginB = isCrtbegin(B->getFile()->getName());
308 if (BeginA != BeginB)
309 return BeginA;
310 bool EndA = isCrtend(A->getFile()->getName());
311 bool EndB = isCrtend(B->getFile()->getName());
312 if (EndA != EndB)
313 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000314 StringRef X = A->Name;
315 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000316 assert(X.startswith(".ctors") || X.startswith(".dtors"));
317 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
318 X = X.substr(6);
319 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000320 if (X.empty() && Y.empty())
321 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000322 return X < Y;
323}
324
325// Sorts input sections by the special rules for .ctors and .dtors.
326// Unfortunately, the rules are different from the one for .{init,fini}_array.
327// Read the comment above.
328template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
329 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000330}
331
Rui Ueyama16068ae2016-11-19 18:05:56 +0000332// Fill [Buf, Buf + Size) with Filler. Filler is written in big
333// endian order. This is used for linker script "=fillexp" command.
334void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
335 uint8_t V[4];
336 write32be(V, Filler);
George Rimare2ee72b2016-02-26 14:48:31 +0000337 size_t I = 0;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000338 for (; I + 4 < Size; I += 4)
339 memcpy(Buf + I, V, 4);
340 memcpy(Buf + I, V, Size - I);
George Rimare2ee72b2016-02-26 14:48:31 +0000341}
342
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000343template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama16068ae2016-11-19 18:05:56 +0000344 if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000345 fill(Buf, this->Size, Filler);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000346
Davide Italiano2e8b2a72016-11-18 02:23:48 +0000347 auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); };
Davide Italiano44665e72016-11-18 02:18:04 +0000348 if (Config->Threads)
349 parallel_for_each(Sections.begin(), Sections.end(), Fn);
350 else
351 std::for_each(Sections.begin(), Sections.end(), Fn);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000352
George Rimare38cbab2016-09-26 19:22:50 +0000353 // Linker scripts may have BYTE()-family commands with which you
354 // can write arbitrary bytes to the output. Process them if any.
355 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356}
357
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000358template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000359EhOutputSection<ELFT>::EhOutputSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000360 : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000361
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000362// Search for an existing CIE record or create a new one.
363// CIE records from input object files are uniquified by their contents
364// and where their relocations point to.
365template <class ELFT>
366template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000367CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000368 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000369 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000370 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +0000371 if (read32<E>(Piece.data().data() + 4) != 0)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000372 fatal("CIE expected at beginning of .eh_frame: " + Sec->Name);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000373
374 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000375 unsigned FirstRelI = Piece.FirstRelocation;
376 if (FirstRelI != (unsigned)-1)
377 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000378
379 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +0000380 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000381
382 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000383 if (Cie->Piece == nullptr) {
384 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000385 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000386 }
387 return Cie;
388}
389
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000390// There is one FDE per function. Returns true if a given FDE
391// points to a live function.
392template <class ELFT>
393template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000394bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000395 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000396 ArrayRef<RelTy> Rels) {
397 unsigned FirstRelI = Piece.FirstRelocation;
398 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000399 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +0000400 const RelTy &Rel = Rels[FirstRelI];
401 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000402 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
403 if (!D || !D->Section)
404 return false;
405 InputSectionBase<ELFT> *Target = D->Section->Repl;
406 return Target && Target->Live;
407}
408
409// .eh_frame is a sequence of CIE or FDE records. In general, there
410// is one CIE record per input object file which is followed by
411// a list of FDEs. This function searches an existing CIE or create a new
412// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +0000413template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000414template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000415void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000416 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000417 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +0000418
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000419 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000420 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000421 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +0000422 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +0000423 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000424
425 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000426 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000427 if (ID == 0) {
428 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
429 continue;
430 }
431
432 uint32_t CieOffset = Offset + 4 - ID;
433 CieRecord *Cie = OffsetToCie[CieOffset];
434 if (!Cie)
435 fatal("invalid CIE reference");
436
437 if (!isFdeLive(Piece, Sec, Rels))
438 continue;
439 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +0000440 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000441 }
442}
443
444template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000445void EhOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000446 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000447 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000448 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000449 Sections.push_back(Sec);
450
451 // .eh_frame is a sequence of CIE or FDE records. This function
452 // splits it into pieces so that we can call
453 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000454 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000455 if (Sec->Pieces.empty())
456 return;
457
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000458 if (Sec->NumRelocations) {
459 if (Sec->AreRelocsRela)
460 addSectionAux(Sec, Sec->relas());
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000461 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000462 addSectionAux(Sec, Sec->rels());
Rui Ueyama0de86c12016-01-27 22:23:44 +0000463 return;
464 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000465 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000466}
467
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000468template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +0000469static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
470 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +0000471
472 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000473 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +0000474 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +0000475}
476
Rui Ueyama1e479c22016-05-23 15:07:59 +0000477template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000478 if (this->Size)
Rui Ueyamae9381bd2016-07-16 02:47:42 +0000479 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +0000480
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000481 size_t Off = 0;
482 for (CieRecord *Cie : Cies) {
483 Cie->Piece->OutputOff = Off;
484 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000485
Rafael Espindola113860b2016-10-20 10:55:58 +0000486 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000487 Fde->OutputOff = Off;
488 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000489 }
490 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000491 this->Size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000492}
493
Rui Ueyamae75e9332016-05-23 03:00:33 +0000494template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
495 const endianness E = ELFT::TargetEndianness;
496 switch (Size) {
497 case DW_EH_PE_udata2:
498 return read16<E>(Buf);
499 case DW_EH_PE_udata4:
500 return read32<E>(Buf);
501 case DW_EH_PE_udata8:
502 return read64<E>(Buf);
503 case DW_EH_PE_absptr:
504 if (ELFT::Is64Bits)
505 return read64<E>(Buf);
506 return read32<E>(Buf);
507 }
508 fatal("unknown FDE size encoding");
509}
510
511// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
512// We need it to create .eh_frame_hdr section.
513template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000514typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +0000515 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000516 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +0000517 // stored at FDE + 8 byte.
518 size_t Off = FdeOff + 8;
519 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
520 if ((Enc & 0x70) == DW_EH_PE_absptr)
521 return Addr;
522 if ((Enc & 0x70) == DW_EH_PE_pcrel)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000523 return Addr + this->Addr + Off;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000524 fatal("unknown FDE size relative encoding");
525}
526
Rui Ueyama1e479c22016-05-23 15:07:59 +0000527template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000528 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000529 for (CieRecord *Cie : Cies) {
530 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000531 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000532
Rafael Espindola32aca872016-10-05 18:40:00 +0000533 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000534 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000535 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +0000536
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000537 // FDE's second word should have the offset to an associated CIE.
538 // Write it.
539 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000540 }
541 }
542
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000543 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +0000544 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000545
546 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000547 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +0000548 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +0000549 if (Out<ELFT>::EhFrameHdr) {
550 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +0000551 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +0000552 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +0000553 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000554 uintX_t FdeVA = this->Addr + Fde->OutputOff;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000555 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
556 }
Rui Ueyamae75e9332016-05-23 03:00:33 +0000557 }
558 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000559}
560
561template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000562MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000563 uintX_t Flags, uintX_t Alignment)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000564 : OutputSectionBase(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000565 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000566
567template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola259d6452016-10-04 22:43:38 +0000568 Builder.write(Buf);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000569}
570
Rafael Espindolac159c962015-10-19 21:00:02 +0000571template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000572void MergeOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama10803512016-05-22 00:25:30 +0000573 auto *Sec = cast<MergeInputSection<ELFT>>(C);
574 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000575 this->updateAlignment(Sec->Alignment);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000576 this->Entsize = Sec->Entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +0000577 Sections.push_back(Sec);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000578}
579
580template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
Rui Ueyama77f2a872016-11-18 05:05:43 +0000581 return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000582}
583
584template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
Rui Ueyama77f2a872016-11-18 05:05:43 +0000585 // Add all string pieces to the string table builder to create section
586 // contents. If we are not tail-optimizing, offsets of strings are fixed
587 // when they are added to the builder (string table builder contains a
588 // hash table from strings to offsets), so we record them if available.
589 for (MergeInputSection<ELFT> *Sec : Sections) {
590 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) {
591 if (!Sec->Pieces[I].Live)
592 continue;
593 uint32_t OutputOffset = Builder.add(Sec->getData(I));
594
595 // Save the offset in the generated string table.
596 if (!shouldTailMerge())
597 Sec->Pieces[I].OutputOff = OutputOffset;
598 }
599 }
600
601 // Fix the string table content. After this, the contents
602 // will never change.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000603 if (shouldTailMerge())
604 Builder.finalize();
Rafael Espindola259d6452016-10-04 22:43:38 +0000605 else
606 Builder.finalizeInOrder();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000607 this->Size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +0000608
Rui Ueyama77f2a872016-11-18 05:05:43 +0000609 // finalize() fixed tail-optimized strings, so we can now get
610 // offsets of strings. Get an offset for each string and save it
611 // to a corresponding StringPiece for easy access.
Rui Ueyama5c851a52016-11-18 19:45:04 +0000612 if (shouldTailMerge())
613 for (MergeInputSection<ELFT> *Sec : Sections)
614 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
615 if (Sec->Pieces[I].Live)
616 Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
Rui Ueyama406b4692016-05-27 14:39:13 +0000617}
618
Rafael Espindolac159c962015-10-19 21:00:02 +0000619template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +0000620VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000621 : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000622 this->Addralign = sizeof(uint32_t);
Rui Ueyamaf5244642016-07-16 02:36:00 +0000623}
George Rimard3566302016-06-20 11:55:12 +0000624
625static StringRef getFileDefName() {
626 if (!Config->SoName.empty())
627 return Config->SoName;
628 return Config->OutputFile;
629}
630
631template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
Eugene Leviant22eb0262016-11-14 09:16:00 +0000632 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000633 for (VersionDefinition &V : Config->VersionDefinitions)
Eugene Leviant22eb0262016-11-14 09:16:00 +0000634 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
George Rimard3566302016-06-20 11:55:12 +0000635
Rafael Espindola04a2e342016-11-09 01:42:41 +0000636 this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
Eugene Leviant22eb0262016-11-14 09:16:00 +0000637 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +0000638
639 // sh_info should be set to the number of definitions. This fact is missed in
640 // documentation, but confirmed by binutils community:
641 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
Rafael Espindola04a2e342016-11-09 01:42:41 +0000642 this->Info = getVerDefNum();
George Rimard3566302016-06-20 11:55:12 +0000643}
644
Rui Ueyama9f619642016-07-16 02:29:45 +0000645template <class ELFT>
646void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
647 StringRef Name, size_t NameOff) {
648 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +0000649 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +0000650 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +0000651 Verdef->vd_aux = sizeof(Elf_Verdef);
652 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
653 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +0000654 Verdef->vd_ndx = Index;
Davide Italianoe7fd0be2016-11-07 21:56:56 +0000655 Verdef->vd_hash = hashSysV(Name);
George Rimard3566302016-06-20 11:55:12 +0000656
Rui Ueyama9f619642016-07-16 02:29:45 +0000657 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
658 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +0000659 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +0000660}
661
662template <class ELFT>
663void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +0000664 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
665
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000666 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +0000667 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
668 writeOne(Buf, V.Id, V.Name, V.NameOff);
669 }
670
671 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +0000672 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +0000673 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +0000674}
675
676template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000677VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000678 : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000679 this->Addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +0000680}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000681
682template <class ELFT> void VersionTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000683 this->Size =
Eugene Leviant9230db92016-11-17 09:16:34 +0000684 sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000685 this->Entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +0000686 // At the moment of june 2016 GNU docs does not mention that sh_link field
687 // should be set, but Sun docs do. Also readelf relies on this field.
Eugene Leviant9230db92016-11-17 09:16:34 +0000688 this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000689}
690
691template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
692 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
Eugene Leviant9230db92016-11-17 09:16:34 +0000693 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000694 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000695 ++OutVersym;
696 }
697}
698
699template <class ELFT>
700VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000701 : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000702 this->Addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +0000703
704 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
705 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
706 // First identifiers are reserved by verdef section if it exist.
707 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +0000708}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000709
710template <class ELFT>
711void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
712 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +0000713 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000714 return;
715 }
Rui Ueyama434b5612016-07-17 03:11:46 +0000716 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000717 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
718 // to create one by adding it to our needed list and creating a dynstr entry
719 // for the soname.
720 if (F->VerdefMap.empty())
Eugene Leviant22eb0262016-11-14 09:16:00 +0000721 Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000722 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
723 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
724 // prepare to create one by allocating a version identifier and creating a
725 // dynstr entry for the version name.
726 if (NV.Index == 0) {
Eugene Leviant22eb0262016-11-14 09:16:00 +0000727 NV.StrTab = In<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +0000728 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000729 NV.Index = NextIndex++;
730 }
George Rimard3566302016-06-20 11:55:12 +0000731 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000732}
733
734template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
735 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
736 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
737 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
738
739 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
740 // Create an Elf_Verneed for this DSO.
741 Verneed->vn_version = 1;
742 Verneed->vn_cnt = P.first->VerdefMap.size();
743 Verneed->vn_file = P.second;
744 Verneed->vn_aux =
745 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
746 Verneed->vn_next = sizeof(Elf_Verneed);
747 ++Verneed;
748
749 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
750 // VerdefMap, which will only contain references to needed version
751 // definitions. Each Elf_Vernaux is based on the information contained in
752 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
753 // pointers, but is deterministic because the pointers refer to Elf_Verdef
754 // data structures within a single input file.
755 for (auto &NV : P.first->VerdefMap) {
756 Vernaux->vna_hash = NV.first->vd_hash;
757 Vernaux->vna_flags = 0;
758 Vernaux->vna_other = NV.second.Index;
759 Vernaux->vna_name = NV.second.StrTab;
760 Vernaux->vna_next = sizeof(Elf_Vernaux);
761 ++Vernaux;
762 }
763
764 Vernaux[-1].vna_next = 0;
765 }
766 Verneed[-1].vn_next = 0;
767}
768
769template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
Eugene Leviant22eb0262016-11-14 09:16:00 +0000770 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000771 this->Info = Needed.size();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000772 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
773 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
774 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000775 this->Size = Size;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000776}
777
778template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000779static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000780 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +0000781}
782
783template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +0000784static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
785 StringRef OutsecName) {
Rafael Espindola17c35832016-09-13 12:25:30 +0000786 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +0000787 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +0000788
789 // For SHF_MERGE we create different output sections for each alignment.
790 // This makes each output section simple and keeps a single level mapping from
791 // input to output.
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000792 // In case of relocatable object generation we do not try to perform merging
793 // and treat SHF_MERGE sections as regular ones, but also create different
794 // output sections for them to allow merging at final linking stage.
Rafael Espindola17c35832016-09-13 12:25:30 +0000795 uintX_t Alignment = 0;
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000796 if (isa<MergeInputSection<ELFT>>(C) ||
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000797 (Config->Relocatable && (C->Flags & SHF_MERGE)))
798 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola17c35832016-09-13 12:25:30 +0000799
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000800 return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000801}
802
803template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000804std::pair<OutputSectionBase *, bool>
George Rimar6892afa2016-07-12 09:49:43 +0000805OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
806 StringRef OutsecName) {
807 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000808 return create(Key, C);
809}
George Rimar6892afa2016-07-12 09:49:43 +0000810
Rafael Espindola10897f12016-09-13 14:23:14 +0000811template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000812std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +0000813OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
814 InputSectionBase<ELFT> *C) {
815 uintX_t Flags = getOutFlags(C);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000816 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +0000817 if (Sec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000818 Sec->Flags |= Flags;
Rafael Espindola10897f12016-09-13 14:23:14 +0000819 return {Sec, false};
820 }
821
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000822 uint32_t Type = C->Type;
Rafael Espindola16853bb2016-09-08 12:33:41 +0000823 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +0000824 case InputSectionBase<ELFT>::Regular:
Eugene Leviant41ca3272016-11-10 09:48:29 +0000825 case InputSectionBase<ELFT>::Synthetic:
Rui Ueyama95642b92016-11-01 23:09:07 +0000826 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +0000827 break;
828 case InputSectionBase<ELFT>::EHFrame:
829 return {Out<ELFT>::EhFrame, false};
830 case InputSectionBase<ELFT>::Merge:
Rui Ueyama95642b92016-11-01 23:09:07 +0000831 Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000832 break;
George Rimar6892afa2016-07-12 09:49:43 +0000833 }
834 return {Sec, true};
835}
836
George Rimar6892afa2016-07-12 09:49:43 +0000837template <bool Is64Bits>
838typename lld::elf::SectionKey<Is64Bits>
839DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
840 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
841}
842
843template <bool Is64Bits>
844typename lld::elf::SectionKey<Is64Bits>
845DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
846 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
847 0};
848}
849
850template <bool Is64Bits>
851unsigned
852DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
853 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
854}
855
856template <bool Is64Bits>
857bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
858 const Key &RHS) {
859 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
860 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
861 LHS.Alignment == RHS.Alignment;
862}
863
864namespace llvm {
865template struct DenseMapInfo<SectionKey<true>>;
866template struct DenseMapInfo<SectionKey<false>>;
867}
868
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000869namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000870namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000871
872template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
873template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
874template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
875template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000876
George Rimarf6bc65a2016-01-15 13:34:52 +0000877template class EhFrameHeader<ELF32LE>;
878template class EhFrameHeader<ELF32BE>;
879template class EhFrameHeader<ELF64LE>;
880template class EhFrameHeader<ELF64BE>;
881
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000882template class OutputSection<ELF32LE>;
883template class OutputSection<ELF32BE>;
884template class OutputSection<ELF64LE>;
885template class OutputSection<ELF64BE>;
886
Rui Ueyama1e479c22016-05-23 15:07:59 +0000887template class EhOutputSection<ELF32LE>;
888template class EhOutputSection<ELF32BE>;
889template class EhOutputSection<ELF64LE>;
890template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000891
Rafael Espindolac159c962015-10-19 21:00:02 +0000892template class MergeOutputSection<ELF32LE>;
893template class MergeOutputSection<ELF32BE>;
894template class MergeOutputSection<ELF64LE>;
895template class MergeOutputSection<ELF64BE>;
896
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000897template class VersionTableSection<ELF32LE>;
898template class VersionTableSection<ELF32BE>;
899template class VersionTableSection<ELF64LE>;
900template class VersionTableSection<ELF64BE>;
901
902template class VersionNeedSection<ELF32LE>;
903template class VersionNeedSection<ELF32BE>;
904template class VersionNeedSection<ELF64LE>;
905template class VersionNeedSection<ELF64BE>;
906
George Rimard3566302016-06-20 11:55:12 +0000907template class VersionDefinitionSection<ELF32LE>;
908template class VersionDefinitionSection<ELF32BE>;
909template class VersionDefinitionSection<ELF64LE>;
910template class VersionDefinitionSection<ELF64BE>;
911
George Rimar58fa5242016-10-20 09:19:48 +0000912template class GdbIndexSection<ELF32LE>;
913template class GdbIndexSection<ELF32BE>;
914template class GdbIndexSection<ELF64LE>;
915template class GdbIndexSection<ELF64BE>;
916
George Rimar6892afa2016-07-12 09:49:43 +0000917template class OutputSectionFactory<ELF32LE>;
918template class OutputSectionFactory<ELF32BE>;
919template class OutputSectionFactory<ELF64LE>;
920template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000921}
922}