blob: f8daa57fd8557c043b073b9e7efd5e512370c363 [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"
Rui Ueyama95642b92016-11-01 23:09:07 +000013#include "LinkerScript.h"
Rui Ueyamafbbde542016-06-29 09:08:02 +000014#include "Strings.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015#include "SymbolTable.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000016#include "SyntheticSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000017#include "Target.h"
Rui Ueyama244a4352016-12-03 21:24:51 +000018#include "Threads.h"
Rui Ueyama520d9162016-12-08 18:31:13 +000019#include "lld/Support/Memory.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000020#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000021#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000022#include "llvm/Support/MathExtras.h"
Rafael Espindolaa42b3bc2016-09-27 16:43:49 +000023#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
Rafael Espindola5805c4f2015-09-21 21:38:08 +000025using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000026using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000028using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029using namespace llvm::ELF;
30
31using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000032using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033
Rafael Espindolae08e78d2016-11-09 23:23:45 +000034OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
35 uint64_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000036 : Name(Name) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000037 this->Type = Type;
38 this->Flags = Flags;
39 this->Addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040}
41
Rafael Espindolae08e78d2016-11-09 23:23:45 +000042uint32_t OutputSectionBase::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000043 uint32_t Ret = PF_R;
44 if (Flags & SHF_WRITE)
45 Ret |= PF_W;
46 if (Flags & SHF_EXECINSTR)
47 Ret |= PF_X;
48 return Ret;
49}
50
Rafael Espindola35c6af32015-09-25 17:19:10 +000051template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +000052void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000053 Shdr->sh_entsize = Entsize;
54 Shdr->sh_addralign = Addralign;
55 Shdr->sh_type = Type;
56 Shdr->sh_offset = Offset;
57 Shdr->sh_flags = Flags;
58 Shdr->sh_info = Info;
59 Shdr->sh_link = Link;
60 Shdr->sh_addr = Addr;
61 Shdr->sh_size = Size;
62 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000063}
64
Rui Ueyamadf5d14d2016-11-09 22:32:42 +000065template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
66 switch (Type) {
67 case SHT_RELA:
68 return sizeof(typename ELFT::Rela);
69 case SHT_REL:
70 return sizeof(typename ELFT::Rel);
71 case SHT_MIPS_REGINFO:
72 return sizeof(Elf_Mips_RegInfo<ELFT>);
73 case SHT_MIPS_OPTIONS:
74 return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
75 case SHT_MIPS_ABIFLAGS:
76 return sizeof(Elf_Mips_ABIFlags<ELFT>);
77 default:
78 return 0;
79 }
80}
81
George Rimarf6bc65a2016-01-15 13:34:52 +000082template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +000083OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
Rafael Espindolae08e78d2016-11-09 23:23:45 +000084 : OutputSectionBase(Name, Type, Flags) {
Rui Ueyamadf5d14d2016-11-09 22:32:42 +000085 this->Entsize = getEntsize<ELFT>(Type);
George Rimar58941ee2016-02-25 08:23:37 +000086}
87
Rafael Espindola4d2d9c02016-11-18 19:02:15 +000088template <typename ELFT>
89static bool compareByFilePosition(InputSection<ELFT> *A,
90 InputSection<ELFT> *B) {
Peter Smith719eb8e2016-11-24 11:43:55 +000091 // Synthetic doesn't have link order dependecy, stable_sort will keep it last
92 if (A->kind() == InputSectionData::Synthetic ||
93 B->kind() == InputSectionData::Synthetic)
94 return false;
Rafael Espindola4d2d9c02016-11-18 19:02:15 +000095 auto *LA = cast<InputSection<ELFT>>(A->getLinkOrderDep());
96 auto *LB = cast<InputSection<ELFT>>(B->getLinkOrderDep());
97 OutputSectionBase *AOut = LA->OutSec;
98 OutputSectionBase *BOut = LB->OutSec;
99 if (AOut != BOut)
100 return AOut->SectionIndex < BOut->SectionIndex;
101 return LA->OutSecOff < LB->OutSecOff;
102}
103
George Rimar58941ee2016-02-25 08:23:37 +0000104template <class ELFT> void OutputSection<ELFT>::finalize() {
Eugene Levianta96d9022016-11-16 10:02:27 +0000105 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
Rafael Espindola4d2d9c02016-11-18 19:02:15 +0000106 std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>);
107 Size = 0;
108 assignOffsets();
109
Rafael Espindola933fcab2016-11-17 23:16:39 +0000110 // We must preserve the link order dependency of sections with the
111 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
112 // need to translate the InputSection sh_link to the OutputSection sh_link,
113 // all InputSections in the OutputSection have the same dependency.
Eugene Levianta96d9022016-11-16 10:02:27 +0000114 if (auto *D = this->Sections.front()->getLinkOrderDep())
115 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000116 }
Eugene Leviant22eb0262016-11-14 09:16:00 +0000117
Rafael Espindola933fcab2016-11-17 23:16:39 +0000118 uint32_t Type = this->Type;
119 if (!Config->Relocatable || (Type != SHT_RELA && Type != SHT_REL))
George Rimar58941ee2016-02-25 08:23:37 +0000120 return;
Rafael Espindola933fcab2016-11-17 23:16:39 +0000121
Eugene Leviant9230db92016-11-17 09:16:34 +0000122 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000123 // sh_info for SHT_REL[A] sections should contain the section header index of
124 // the section to which the relocation applies.
125 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000126 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000127}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000128
129template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000130void OutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000131 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000132 auto *S = cast<InputSection<ELFT>>(C);
133 Sections.push_back(S);
134 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000135 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000136 // Keep sh_entsize value of the input section to be able to perform merging
137 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000138 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000139 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000140}
141
Rui Ueyama809d8e22016-06-23 04:33:42 +0000142// This function is called after we sort input sections
143// and scan relocations to setup sections' offsets.
144template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000145 uintX_t Off = this->Size;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000146 for (InputSection<ELFT> *S : Sections) {
147 Off = alignTo(Off, S->Alignment);
148 S->OutSecOff = Off;
149 Off += S->getSize();
150 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000151 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000152}
153
George Rimar1a33c0f2016-11-10 09:05:20 +0000154template <class ELFT>
155void OutputSection<ELFT>::sort(
156 std::function<unsigned(InputSection<ELFT> *S)> Order) {
157 typedef std::pair<unsigned, InputSection<ELFT> *> Pair;
158 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
159
160 std::vector<Pair> V;
161 for (InputSection<ELFT> *S : Sections)
162 V.push_back({Order(S), S});
163 std::stable_sort(V.begin(), V.end(), Comp);
164 Sections.clear();
165 for (Pair &P : V)
166 Sections.push_back(P.second);
167}
168
Rui Ueyamac4185702016-02-10 23:20:42 +0000169// Sorts input sections by section name suffixes, so that .foo.N comes
170// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000171// We want to keep the original order if the priorities are the same
172// because the compiler keeps the original initialization order in a
173// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000174// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000175template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000176 // Sort sections by priority.
George Rimar1a33c0f2016-11-10 09:05:20 +0000177 sort([](InputSection<ELFT> *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000178}
Rui Ueyamac4185702016-02-10 23:20:42 +0000179
Rui Ueyama5af83682016-02-11 23:41:38 +0000180// Returns true if S matches /Filename.?\.o$/.
181static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
182 if (!S.endswith(".o"))
183 return false;
184 S = S.drop_back(2);
185 if (S.endswith(Filename))
186 return true;
187 return !S.empty() && S.drop_back().endswith(Filename);
188}
189
190static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
191static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
192
193// .ctors and .dtors are sorted by this priority from highest to lowest.
194//
195// 1. The section was contained in crtbegin (crtbegin contains
196// some sentinel value in its .ctors and .dtors so that the runtime
197// can find the beginning of the sections.)
198//
199// 2. The section has an optional priority value in the form of ".ctors.N"
200// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
201// they are compared as string rather than number.
202//
203// 3. The section is just ".ctors" or ".dtors".
204//
205// 4. The section was contained in crtend, which contains an end marker.
206//
207// In an ideal world, we don't need this function because .init_array and
208// .ctors are duplicate features (and .init_array is newer.) However, there
209// are too many real-world use cases of .ctors, so we had no choice to
210// support that with this rather ad-hoc semantics.
211template <class ELFT>
212static bool compCtors(const InputSection<ELFT> *A,
213 const InputSection<ELFT> *B) {
214 bool BeginA = isCrtbegin(A->getFile()->getName());
215 bool BeginB = isCrtbegin(B->getFile()->getName());
216 if (BeginA != BeginB)
217 return BeginA;
218 bool EndA = isCrtend(A->getFile()->getName());
219 bool EndB = isCrtend(B->getFile()->getName());
220 if (EndA != EndB)
221 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000222 StringRef X = A->Name;
223 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000224 assert(X.startswith(".ctors") || X.startswith(".dtors"));
225 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
226 X = X.substr(6);
227 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000228 if (X.empty() && Y.empty())
229 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000230 return X < Y;
231}
232
233// Sorts input sections by the special rules for .ctors and .dtors.
234// Unfortunately, the rules are different from the one for .{init,fini}_array.
235// Read the comment above.
236template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
237 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000238}
239
Rui Ueyama16068ae2016-11-19 18:05:56 +0000240// Fill [Buf, Buf + Size) with Filler. Filler is written in big
241// endian order. This is used for linker script "=fillexp" command.
242void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
243 uint8_t V[4];
244 write32be(V, Filler);
George Rimare2ee72b2016-02-26 14:48:31 +0000245 size_t I = 0;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000246 for (; I + 4 < Size; I += 4)
247 memcpy(Buf + I, V, 4);
248 memcpy(Buf + I, V, Size - I);
George Rimare2ee72b2016-02-26 14:48:31 +0000249}
250
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant84569e62016-11-29 08:05:44 +0000252 Loc = Buf;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000253 if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000254 fill(Buf, this->Size, Filler);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000255
Davide Italiano2e8b2a72016-11-18 02:23:48 +0000256 auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); };
Rui Ueyama244a4352016-12-03 21:24:51 +0000257 forEach(Sections.begin(), Sections.end(), Fn);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000258
George Rimare38cbab2016-09-26 19:22:50 +0000259 // Linker scripts may have BYTE()-family commands with which you
260 // can write arbitrary bytes to the output. Process them if any.
261 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262}
263
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000264template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000265EhOutputSection<ELFT>::EhOutputSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000266 : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000267
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000268// Search for an existing CIE record or create a new one.
269// CIE records from input object files are uniquified by their contents
270// and where their relocations point to.
271template <class ELFT>
272template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000273CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000274 ArrayRef<RelTy> Rels) {
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000275 auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000276 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +0000277 if (read32<E>(Piece.data().data() + 4) != 0)
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000278 fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000279
280 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000281 unsigned FirstRelI = Piece.FirstRelocation;
282 if (FirstRelI != (unsigned)-1)
283 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000284
285 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +0000286 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000287
288 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000289 if (Cie->Piece == nullptr) {
290 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000291 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000292 }
293 return Cie;
294}
295
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000296// There is one FDE per function. Returns true if a given FDE
297// points to a live function.
298template <class ELFT>
299template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000300bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000301 ArrayRef<RelTy> Rels) {
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000302 auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID);
Rafael Espindola2deeb602016-07-21 20:18:30 +0000303 unsigned FirstRelI = Piece.FirstRelocation;
304 if (FirstRelI == (unsigned)-1)
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000305 fatal(toString(Sec) + ": FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +0000306 const RelTy &Rel = Rels[FirstRelI];
307 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000308 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
309 if (!D || !D->Section)
310 return false;
311 InputSectionBase<ELFT> *Target = D->Section->Repl;
312 return Target && Target->Live;
313}
314
315// .eh_frame is a sequence of CIE or FDE records. In general, there
316// is one CIE record per input object file which is followed by
317// a list of FDEs. This function searches an existing CIE or create a new
318// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +0000319template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000320template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000321void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000322 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000323 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +0000324
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000325 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000326 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000327 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +0000328 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +0000329 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000330
331 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000332 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000333 if (ID == 0) {
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000334 OffsetToCie[Offset] = addCie(Piece, Rels);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000335 continue;
336 }
337
338 uint32_t CieOffset = Offset + 4 - ID;
339 CieRecord *Cie = OffsetToCie[CieOffset];
340 if (!Cie)
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000341 fatal(toString(Sec) + ": invalid CIE reference");
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000342
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000343 if (!isFdeLive(Piece, Rels))
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000344 continue;
345 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +0000346 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000347 }
348}
349
350template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000351void EhOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000352 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000353 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000354 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000355 Sections.push_back(Sec);
356
357 // .eh_frame is a sequence of CIE or FDE records. This function
358 // splits it into pieces so that we can call
359 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000360 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000361 if (Sec->Pieces.empty())
362 return;
363
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000364 if (Sec->NumRelocations) {
365 if (Sec->AreRelocsRela)
366 addSectionAux(Sec, Sec->relas());
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000367 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000368 addSectionAux(Sec, Sec->rels());
Rui Ueyama0de86c12016-01-27 22:23:44 +0000369 return;
370 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000371 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000372}
373
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000374template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +0000375static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
376 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +0000377
378 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000379 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +0000380 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +0000381}
382
Rui Ueyama1e479c22016-05-23 15:07:59 +0000383template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000384 if (this->Size)
Rui Ueyamae9381bd2016-07-16 02:47:42 +0000385 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +0000386
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000387 size_t Off = 0;
388 for (CieRecord *Cie : Cies) {
389 Cie->Piece->OutputOff = Off;
390 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000391
Rafael Espindola113860b2016-10-20 10:55:58 +0000392 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000393 Fde->OutputOff = Off;
394 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000395 }
396 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000397 this->Size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000398}
399
Rui Ueyamae75e9332016-05-23 03:00:33 +0000400template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
401 const endianness E = ELFT::TargetEndianness;
402 switch (Size) {
403 case DW_EH_PE_udata2:
404 return read16<E>(Buf);
405 case DW_EH_PE_udata4:
406 return read32<E>(Buf);
407 case DW_EH_PE_udata8:
408 return read64<E>(Buf);
409 case DW_EH_PE_absptr:
410 if (ELFT::Is64Bits)
411 return read64<E>(Buf);
412 return read32<E>(Buf);
413 }
414 fatal("unknown FDE size encoding");
415}
416
417// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
418// We need it to create .eh_frame_hdr section.
419template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000420typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +0000421 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000422 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +0000423 // stored at FDE + 8 byte.
424 size_t Off = FdeOff + 8;
425 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
426 if ((Enc & 0x70) == DW_EH_PE_absptr)
427 return Addr;
428 if ((Enc & 0x70) == DW_EH_PE_pcrel)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000429 return Addr + this->Addr + Off;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000430 fatal("unknown FDE size relative encoding");
431}
432
Rui Ueyama1e479c22016-05-23 15:07:59 +0000433template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000434 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000435 for (CieRecord *Cie : Cies) {
436 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000437 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000438
Rafael Espindola32aca872016-10-05 18:40:00 +0000439 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000440 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000441 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +0000442
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000443 // FDE's second word should have the offset to an associated CIE.
444 // Write it.
445 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000446 }
447 }
448
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000449 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +0000450 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000451
452 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000453 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +0000454 // we obtain two addresses and pass them to EhFrameHdr object.
Eugene Leviant952eb4d2016-11-21 15:52:10 +0000455 if (In<ELFT>::EhFrameHdr) {
Rui Ueyama3b31e672016-05-23 16:24:16 +0000456 for (CieRecord *Cie : Cies) {
Eugene Leviant531df4f2016-11-23 09:45:17 +0000457 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece);
Rui Ueyama3b31e672016-05-23 16:24:16 +0000458 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +0000459 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000460 uintX_t FdeVA = this->Addr + Fde->OutputOff;
Eugene Leviant952eb4d2016-11-21 15:52:10 +0000461 In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
Rui Ueyama3b31e672016-05-23 16:24:16 +0000462 }
Rui Ueyamae75e9332016-05-23 03:00:33 +0000463 }
464 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000465}
466
467template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000468MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000469 uintX_t Flags, uintX_t Alignment)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000470 : OutputSectionBase(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000471 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000472
473template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola259d6452016-10-04 22:43:38 +0000474 Builder.write(Buf);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000475}
476
Rafael Espindolac159c962015-10-19 21:00:02 +0000477template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000478void MergeOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama10803512016-05-22 00:25:30 +0000479 auto *Sec = cast<MergeInputSection<ELFT>>(C);
480 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000481 this->updateAlignment(Sec->Alignment);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000482 this->Entsize = Sec->Entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +0000483 Sections.push_back(Sec);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000484}
485
486template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
Rui Ueyama77f2a872016-11-18 05:05:43 +0000487 return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000488}
489
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000490template <class ELFT> void MergeOutputSection<ELFT>::finalizeTailMerge() {
Rui Ueyama77f2a872016-11-18 05:05:43 +0000491 // Add all string pieces to the string table builder to create section
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000492 // contents.
493 for (MergeInputSection<ELFT> *Sec : Sections)
494 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
495 if (Sec->Pieces[I].Live)
496 Builder.add(Sec->getData(I));
Rui Ueyama77f2a872016-11-18 05:05:43 +0000497
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000498 // Fix the string table content. After this, the contents will never change.
499 Builder.finalize();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000500 this->Size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +0000501
Rui Ueyama77f2a872016-11-18 05:05:43 +0000502 // finalize() fixed tail-optimized strings, so we can now get
503 // offsets of strings. Get an offset for each string and save it
504 // to a corresponding StringPiece for easy access.
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000505 for (MergeInputSection<ELFT> *Sec : Sections)
506 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
507 if (Sec->Pieces[I].Live)
508 Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
509}
510
511template <class ELFT> void MergeOutputSection<ELFT>::finalizeNoTailMerge() {
512 // Add all string pieces to the string table builder to create section
513 // contents. Because we are not tail-optimizing, offsets of strings are
514 // fixed when they are added to the builder (string table builder contains
515 // a hash table from strings to offsets).
516 for (MergeInputSection<ELFT> *Sec : Sections)
517 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
518 if (Sec->Pieces[I].Live)
519 Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I));
520
521 Builder.finalizeInOrder();
522 this->Size = Builder.getSize();
523}
524
525template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
Rui Ueyama5c851a52016-11-18 19:45:04 +0000526 if (shouldTailMerge())
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000527 finalizeTailMerge();
528 else
529 finalizeNoTailMerge();
Rui Ueyama406b4692016-05-27 14:39:13 +0000530}
531
Rafael Espindolac159c962015-10-19 21:00:02 +0000532template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000533static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000534 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +0000535}
536
537template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +0000538static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
539 StringRef OutsecName) {
Rafael Espindola17c35832016-09-13 12:25:30 +0000540 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +0000541 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +0000542
543 // For SHF_MERGE we create different output sections for each alignment.
544 // This makes each output section simple and keeps a single level mapping from
545 // input to output.
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000546 // In case of relocatable object generation we do not try to perform merging
547 // and treat SHF_MERGE sections as regular ones, but also create different
548 // output sections for them to allow merging at final linking stage.
Rafael Espindola17c35832016-09-13 12:25:30 +0000549 uintX_t Alignment = 0;
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000550 if (isa<MergeInputSection<ELFT>>(C) ||
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000551 (Config->Relocatable && (C->Flags & SHF_MERGE)))
552 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola17c35832016-09-13 12:25:30 +0000553
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000554 return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000555}
556
557template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000558std::pair<OutputSectionBase *, bool>
George Rimar6892afa2016-07-12 09:49:43 +0000559OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
560 StringRef OutsecName) {
561 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000562 return create(Key, C);
563}
George Rimar6892afa2016-07-12 09:49:43 +0000564
Rafael Espindola10897f12016-09-13 14:23:14 +0000565template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000566std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +0000567OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
568 InputSectionBase<ELFT> *C) {
569 uintX_t Flags = getOutFlags(C);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000570 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +0000571 if (Sec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000572 Sec->Flags |= Flags;
Rafael Espindola10897f12016-09-13 14:23:14 +0000573 return {Sec, false};
574 }
575
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000576 uint32_t Type = C->Type;
Rafael Espindola16853bb2016-09-08 12:33:41 +0000577 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +0000578 case InputSectionBase<ELFT>::Regular:
Eugene Leviant41ca3272016-11-10 09:48:29 +0000579 case InputSectionBase<ELFT>::Synthetic:
Rui Ueyama95642b92016-11-01 23:09:07 +0000580 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +0000581 break;
582 case InputSectionBase<ELFT>::EHFrame:
583 return {Out<ELFT>::EhFrame, false};
584 case InputSectionBase<ELFT>::Merge:
Rui Ueyama95642b92016-11-01 23:09:07 +0000585 Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000586 break;
George Rimar6892afa2016-07-12 09:49:43 +0000587 }
588 return {Sec, true};
589}
590
George Rimar6892afa2016-07-12 09:49:43 +0000591template <bool Is64Bits>
592typename lld::elf::SectionKey<Is64Bits>
593DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
594 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
595}
596
597template <bool Is64Bits>
598typename lld::elf::SectionKey<Is64Bits>
599DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
600 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
601 0};
602}
603
604template <bool Is64Bits>
605unsigned
606DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
607 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
608}
609
610template <bool Is64Bits>
611bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
612 const Key &RHS) {
613 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
614 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
615 LHS.Alignment == RHS.Alignment;
616}
617
618namespace llvm {
619template struct DenseMapInfo<SectionKey<true>>;
620template struct DenseMapInfo<SectionKey<false>>;
621}
622
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000623namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000624namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000625
626template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
627template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
628template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
629template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000630
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631template class OutputSection<ELF32LE>;
632template class OutputSection<ELF32BE>;
633template class OutputSection<ELF64LE>;
634template class OutputSection<ELF64BE>;
635
Rui Ueyama1e479c22016-05-23 15:07:59 +0000636template class EhOutputSection<ELF32LE>;
637template class EhOutputSection<ELF32BE>;
638template class EhOutputSection<ELF64LE>;
639template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000640
Rafael Espindolac159c962015-10-19 21:00:02 +0000641template class MergeOutputSection<ELF32LE>;
642template class MergeOutputSection<ELF32BE>;
643template class MergeOutputSection<ELF64LE>;
644template class MergeOutputSection<ELF64BE>;
645
George Rimar6892afa2016-07-12 09:49:43 +0000646template class OutputSectionFactory<ELF32LE>;
647template class OutputSectionFactory<ELF32BE>;
648template class OutputSectionFactory<ELF64LE>;
649template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000650}
651}