blob: b9d6f403b34dd80d6f36a80054226e398f4ab7af [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 Ueyama9381eb12016-12-18 14:06:06 +000014#include "Memory.h"
Rui Ueyamafbbde542016-06-29 09:08:02 +000015#include "Strings.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000016#include "SymbolTable.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000017#include "SyntheticSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000018#include "Target.h"
Rui Ueyama244a4352016-12-03 21:24:51 +000019#include "Threads.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;
Rafael Espindolaea590d92017-02-08 15:19:03 +000060 Shdr->sh_addr = Addr;
Rafael Espindola04a2e342016-11-09 01:42:41 +000061 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;
George Rimar82bd8be2017-02-08 16:18:10 +0000119 if (!Config->copyRelocs() || (Type != SHT_RELA && Type != SHT_REL))
George Rimar58941ee2016-02-25 08:23:37 +0000120 return;
Rafael Espindola933fcab2016-11-17 23:16:39 +0000121
Rafael Espindola82f00ec2017-02-16 14:23:43 +0000122 InputSection<ELFT> *First = Sections[0];
123 if (isa<SyntheticSection<ELFT>>(First))
124 return;
125
Eugene Leviant9230db92016-11-17 09:16:34 +0000126 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000127 // sh_info for SHT_REL[A] sections should contain the section header index of
128 // the section to which the relocation applies.
Rafael Espindola82f00ec2017-02-16 14:23:43 +0000129 InputSectionBase<ELFT> *S = First->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000130 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000131}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000132
133template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000134void OutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000135 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000136 auto *S = cast<InputSection<ELFT>>(C);
137 Sections.push_back(S);
138 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000139 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000140 // Keep sh_entsize value of the input section to be able to perform merging
141 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000142 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000143 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000144}
145
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000146template <class ELFT>
147void OutputSection<ELFT>::forEachInputSection(
148 std::function<void(InputSectionData *)> F) {
149 for (InputSection<ELFT> *S : Sections)
150 F(S);
151}
152
Rui Ueyama809d8e22016-06-23 04:33:42 +0000153// This function is called after we sort input sections
154// and scan relocations to setup sections' offsets.
155template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000156 uintX_t Off = this->Size;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000157 for (InputSection<ELFT> *S : Sections) {
158 Off = alignTo(Off, S->Alignment);
159 S->OutSecOff = Off;
160 Off += S->getSize();
161 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000162 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000163}
164
George Rimar1a33c0f2016-11-10 09:05:20 +0000165template <class ELFT>
166void OutputSection<ELFT>::sort(
Rui Ueyama31270312016-12-20 01:51:08 +0000167 std::function<int(InputSection<ELFT> *S)> Order) {
George Rimar1a33c0f2016-11-10 09:05:20 +0000168 typedef std::pair<unsigned, InputSection<ELFT> *> Pair;
169 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
170
171 std::vector<Pair> V;
172 for (InputSection<ELFT> *S : Sections)
173 V.push_back({Order(S), S});
174 std::stable_sort(V.begin(), V.end(), Comp);
175 Sections.clear();
176 for (Pair &P : V)
177 Sections.push_back(P.second);
178}
179
Rui Ueyamac4185702016-02-10 23:20:42 +0000180// Sorts input sections by section name suffixes, so that .foo.N comes
181// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000182// We want to keep the original order if the priorities are the same
183// because the compiler keeps the original initialization order in a
184// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000185// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000186template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000187 // Sort sections by priority.
George Rimar1a33c0f2016-11-10 09:05:20 +0000188 sort([](InputSection<ELFT> *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000189}
Rui Ueyamac4185702016-02-10 23:20:42 +0000190
Rui Ueyama5af83682016-02-11 23:41:38 +0000191// Returns true if S matches /Filename.?\.o$/.
192static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
193 if (!S.endswith(".o"))
194 return false;
195 S = S.drop_back(2);
196 if (S.endswith(Filename))
197 return true;
198 return !S.empty() && S.drop_back().endswith(Filename);
199}
200
201static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
202static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
203
204// .ctors and .dtors are sorted by this priority from highest to lowest.
205//
206// 1. The section was contained in crtbegin (crtbegin contains
207// some sentinel value in its .ctors and .dtors so that the runtime
208// can find the beginning of the sections.)
209//
210// 2. The section has an optional priority value in the form of ".ctors.N"
211// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
212// they are compared as string rather than number.
213//
214// 3. The section is just ".ctors" or ".dtors".
215//
216// 4. The section was contained in crtend, which contains an end marker.
217//
218// In an ideal world, we don't need this function because .init_array and
219// .ctors are duplicate features (and .init_array is newer.) However, there
220// are too many real-world use cases of .ctors, so we had no choice to
221// support that with this rather ad-hoc semantics.
222template <class ELFT>
223static bool compCtors(const InputSection<ELFT> *A,
224 const InputSection<ELFT> *B) {
225 bool BeginA = isCrtbegin(A->getFile()->getName());
226 bool BeginB = isCrtbegin(B->getFile()->getName());
227 if (BeginA != BeginB)
228 return BeginA;
229 bool EndA = isCrtend(A->getFile()->getName());
230 bool EndB = isCrtend(B->getFile()->getName());
231 if (EndA != EndB)
232 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000233 StringRef X = A->Name;
234 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000235 assert(X.startswith(".ctors") || X.startswith(".dtors"));
236 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
237 X = X.substr(6);
238 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000239 if (X.empty() && Y.empty())
240 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000241 return X < Y;
242}
243
244// Sorts input sections by the special rules for .ctors and .dtors.
245// Unfortunately, the rules are different from the one for .{init,fini}_array.
246// Read the comment above.
247template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
248 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000249}
250
Rui Ueyama16068ae2016-11-19 18:05:56 +0000251// Fill [Buf, Buf + Size) with Filler. Filler is written in big
252// endian order. This is used for linker script "=fillexp" command.
253void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
254 uint8_t V[4];
255 write32be(V, Filler);
George Rimare2ee72b2016-02-26 14:48:31 +0000256 size_t I = 0;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000257 for (; I + 4 < Size; I += 4)
258 memcpy(Buf + I, V, 4);
259 memcpy(Buf + I, V, Size - I);
George Rimare2ee72b2016-02-26 14:48:31 +0000260}
261
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant84569e62016-11-29 08:05:44 +0000263 Loc = Buf;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000264 if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000265 fill(Buf, this->Size, Filler);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000266
Davide Italiano2e8b2a72016-11-18 02:23:48 +0000267 auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); };
Rui Ueyama244a4352016-12-03 21:24:51 +0000268 forEach(Sections.begin(), Sections.end(), Fn);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000269
George Rimare38cbab2016-09-26 19:22:50 +0000270 // Linker scripts may have BYTE()-family commands with which you
271 // can write arbitrary bytes to the output. Process them if any.
272 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000273}
274
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000275template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000276EhOutputSection<ELFT>::EhOutputSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000277 : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000278
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000279template <class ELFT>
280void EhOutputSection<ELFT>::forEachInputSection(
281 std::function<void(InputSectionData *)> F) {
282 for (EhInputSection<ELFT> *S : Sections)
283 F(S);
284}
285
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000286// Search for an existing CIE record or create a new one.
287// CIE records from input object files are uniquified by their contents
288// and where their relocations point to.
289template <class ELFT>
290template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000291CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000292 ArrayRef<RelTy> Rels) {
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000293 auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000294 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +0000295 if (read32<E>(Piece.data().data() + 4) != 0)
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000296 fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000297
298 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000299 unsigned FirstRelI = Piece.FirstRelocation;
300 if (FirstRelI != (unsigned)-1)
301 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000302
303 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +0000304 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000305
306 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000307 if (Cie->Piece == nullptr) {
308 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000309 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000310 }
311 return Cie;
312}
313
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000314// There is one FDE per function. Returns true if a given FDE
315// points to a live function.
316template <class ELFT>
317template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000318bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000319 ArrayRef<RelTy> Rels) {
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000320 auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID);
Rafael Espindola2deeb602016-07-21 20:18:30 +0000321 unsigned FirstRelI = Piece.FirstRelocation;
322 if (FirstRelI == (unsigned)-1)
Rafael Espindolaf340ca82017-02-15 01:29:23 +0000323 return false;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000324 const RelTy &Rel = Rels[FirstRelI];
325 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000326 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
327 if (!D || !D->Section)
328 return false;
329 InputSectionBase<ELFT> *Target = D->Section->Repl;
330 return Target && Target->Live;
331}
332
333// .eh_frame is a sequence of CIE or FDE records. In general, there
334// is one CIE record per input object file which is followed by
335// a list of FDEs. This function searches an existing CIE or create a new
336// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +0000337template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000338template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000339void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000340 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000341 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +0000342
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000343 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000344 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000345 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +0000346 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +0000347 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000348
349 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000350 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000351 if (ID == 0) {
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000352 OffsetToCie[Offset] = addCie(Piece, Rels);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000353 continue;
354 }
355
356 uint32_t CieOffset = Offset + 4 - ID;
357 CieRecord *Cie = OffsetToCie[CieOffset];
358 if (!Cie)
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000359 fatal(toString(Sec) + ": invalid CIE reference");
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000360
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000361 if (!isFdeLive(Piece, Rels))
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000362 continue;
363 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +0000364 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000365 }
366}
367
368template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000369void EhOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000370 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000371 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000372 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000373 Sections.push_back(Sec);
374
375 // .eh_frame is a sequence of CIE or FDE records. This function
376 // splits it into pieces so that we can call
377 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000378 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000379 if (Sec->Pieces.empty())
380 return;
381
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000382 if (Sec->NumRelocations) {
383 if (Sec->AreRelocsRela)
384 addSectionAux(Sec, Sec->relas());
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000385 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000386 addSectionAux(Sec, Sec->rels());
Rui Ueyama0de86c12016-01-27 22:23:44 +0000387 return;
388 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000389 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000390}
391
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000392template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +0000393static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
394 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +0000395
396 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000397 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +0000398 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +0000399}
400
Rui Ueyama1e479c22016-05-23 15:07:59 +0000401template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000402 if (this->Size)
Rui Ueyamae9381bd2016-07-16 02:47:42 +0000403 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +0000404
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000405 size_t Off = 0;
406 for (CieRecord *Cie : Cies) {
407 Cie->Piece->OutputOff = Off;
408 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000409
Rafael Espindola113860b2016-10-20 10:55:58 +0000410 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000411 Fde->OutputOff = Off;
412 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000413 }
414 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000415 this->Size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000416}
417
Rui Ueyamae75e9332016-05-23 03:00:33 +0000418template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
419 const endianness E = ELFT::TargetEndianness;
420 switch (Size) {
421 case DW_EH_PE_udata2:
422 return read16<E>(Buf);
423 case DW_EH_PE_udata4:
424 return read32<E>(Buf);
425 case DW_EH_PE_udata8:
426 return read64<E>(Buf);
427 case DW_EH_PE_absptr:
428 if (ELFT::Is64Bits)
429 return read64<E>(Buf);
430 return read32<E>(Buf);
431 }
432 fatal("unknown FDE size encoding");
433}
434
435// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
436// We need it to create .eh_frame_hdr section.
437template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000438typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +0000439 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000440 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +0000441 // stored at FDE + 8 byte.
442 size_t Off = FdeOff + 8;
443 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
444 if ((Enc & 0x70) == DW_EH_PE_absptr)
445 return Addr;
446 if ((Enc & 0x70) == DW_EH_PE_pcrel)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000447 return Addr + this->Addr + Off;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000448 fatal("unknown FDE size relative encoding");
449}
450
Rui Ueyama1e479c22016-05-23 15:07:59 +0000451template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000452 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000453 for (CieRecord *Cie : Cies) {
454 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000455 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000456
Rafael Espindola32aca872016-10-05 18:40:00 +0000457 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000458 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000459 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +0000460
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000461 // FDE's second word should have the offset to an associated CIE.
462 // Write it.
463 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000464 }
465 }
466
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000467 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +0000468 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000469
470 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000471 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +0000472 // we obtain two addresses and pass them to EhFrameHdr object.
Eugene Leviant952eb4d2016-11-21 15:52:10 +0000473 if (In<ELFT>::EhFrameHdr) {
Rui Ueyama3b31e672016-05-23 16:24:16 +0000474 for (CieRecord *Cie : Cies) {
Eugene Leviant531df4f2016-11-23 09:45:17 +0000475 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece);
Rui Ueyama3b31e672016-05-23 16:24:16 +0000476 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +0000477 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000478 uintX_t FdeVA = this->Addr + Fde->OutputOff;
Eugene Leviant952eb4d2016-11-21 15:52:10 +0000479 In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
Rui Ueyama3b31e672016-05-23 16:24:16 +0000480 }
Rui Ueyamae75e9332016-05-23 03:00:33 +0000481 }
482 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000483}
484
485template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000486static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000487 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +0000488}
489
Rafael Espindolabd3ab092017-01-05 14:52:46 +0000490namespace llvm {
491template <> struct DenseMapInfo<lld::elf::SectionKey> {
492 static lld::elf::SectionKey getEmptyKey();
493 static lld::elf::SectionKey getTombstoneKey();
494 static unsigned getHashValue(const lld::elf::SectionKey &Val);
495 static bool isEqual(const lld::elf::SectionKey &LHS,
496 const lld::elf::SectionKey &RHS);
497};
498}
499
Rafael Espindola10897f12016-09-13 14:23:14 +0000500template <class ELFT>
Rafael Espindola72447082017-01-05 14:35:41 +0000501static SectionKey createKey(InputSectionBase<ELFT> *C, StringRef OutsecName) {
Rafael Espindola33713982017-01-05 14:20:35 +0000502 // The ELF spec just says
503 // ----------------------------------------------------------------
504 // In the first phase, input sections that match in name, type and
505 // attribute flags should be concatenated into single sections.
506 // ----------------------------------------------------------------
507 //
508 // However, it is clear that at least some flags have to be ignored for
509 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
510 // ignored. We should not have two output .text sections just because one was
511 // in a group and another was not for example.
512 //
513 // It also seems that that wording was a late addition and didn't get the
514 // necessary scrutiny.
515 //
516 // Merging sections with different flags is expected by some users. One
517 // reason is that if one file has
518 //
519 // int *const bar __attribute__((section(".foo"))) = (int *)0;
520 //
521 // gcc with -fPIC will produce a read only .foo section. But if another
522 // file has
523 //
524 // int zed;
525 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
526 //
527 // gcc with -fPIC will produce a read write section.
528 //
529 // Last but not least, when using linker script the merge rules are forced by
530 // the script. Unfortunately, linker scripts are name based. This means that
531 // expressions like *(.foo*) can refer to multiple input sections with
532 // different flags. We cannot put them in different output sections or we
533 // would produce wrong results for
534 //
535 // start = .; *(.foo.*) end = .; *(.bar)
536 //
537 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
538 // another. The problem is that there is no way to layout those output
539 // sections such that the .foo sections are the only thing between the start
540 // and end symbols.
541 //
542 // Given the above issues, we instead merge sections by name and error on
543 // incompatible types and flags.
Rafael Espindola17c35832016-09-13 12:25:30 +0000544
Rafael Espindola33713982017-01-05 14:20:35 +0000545 typedef typename ELFT::uint uintX_t;
Rafael Espindola33713982017-01-05 14:20:35 +0000546
Rafael Espindola17c35832016-09-13 12:25:30 +0000547 uintX_t Alignment = 0;
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000548 uintX_t Flags = 0;
549 if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000550 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000551 Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
552 }
Rafael Espindola17c35832016-09-13 12:25:30 +0000553
Rafael Espindola72447082017-01-05 14:35:41 +0000554 return SectionKey{OutsecName, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000555}
556
Rafael Espindola82902742017-02-16 17:32:26 +0000557template <class ELFT>
558OutputSectionFactory<ELFT>::OutputSectionFactory(
559 std::vector<OutputSectionBase *> &OutputSections)
560 : OutputSections(OutputSections) {}
George Rimar6892afa2016-07-12 09:49:43 +0000561
Rafael Espindola33713982017-01-05 14:20:35 +0000562static uint64_t getIncompatibleFlags(uint64_t Flags) {
563 return Flags & (SHF_ALLOC | SHF_TLS);
564}
565
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000566// We allow sections of types listed below to merged into a
567// single progbits section. This is typically done by linker
568// scripts. Merging nobits and progbits will force disk space
569// to be allocated for nobits sections. Other ones don't require
570// any special treatment on top of progbits, so there doesn't
571// seem to be a harm in merging them.
572static bool canMergeToProgbits(unsigned Type) {
573 return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
574 Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
575 Type == SHT_NOTE;
576}
577
Rafael Espindola10897f12016-09-13 14:23:14 +0000578template <class ELFT>
Rafael Espindola82902742017-02-16 17:32:26 +0000579void OutputSectionFactory<ELFT>::addInputSec(InputSectionBase<ELFT> *IS,
580 StringRef OutsecName) {
581 if (!IS->Live) {
582 reportDiscarded(IS);
583 return;
584 }
585
586 SectionKey Key = createKey(IS, OutsecName);
587 uintX_t Flags = getOutFlags(IS);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000588 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +0000589 if (Sec) {
Rafael Espindola82902742017-02-16 17:32:26 +0000590 if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
Rafael Espindola33713982017-01-05 14:20:35 +0000591 error("Section has flags incompatible with others with the same name " +
Rafael Espindola82902742017-02-16 17:32:26 +0000592 toString(IS));
593 if (Sec->Type != IS->Type) {
594 if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000595 Sec->Type = SHT_PROGBITS;
596 else
597 error("Section has different type from others with the same name " +
Rafael Espindola82902742017-02-16 17:32:26 +0000598 toString(IS));
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000599 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000600 Sec->Flags |= Flags;
Rafael Espindola82902742017-02-16 17:32:26 +0000601 } else {
602 uint32_t Type = IS->Type;
603 if (IS->kind() == InputSectionBase<ELFT>::EHFrame) {
604 Out<ELFT>::EhFrame->addSection(IS);
605 return;
606 }
607 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
608 OutputSections.push_back(Sec);
Rafael Espindola10897f12016-09-13 14:23:14 +0000609 }
610
Rafael Espindola82902742017-02-16 17:32:26 +0000611 Sec->addSection(IS);
George Rimar6892afa2016-07-12 09:49:43 +0000612}
613
Rafael Espindola82902742017-02-16 17:32:26 +0000614template <class ELFT> OutputSectionFactory<ELFT>::~OutputSectionFactory() {}
615
Rafael Espindola72447082017-01-05 14:35:41 +0000616SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
617 return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000618}
619
Rafael Espindola72447082017-01-05 14:35:41 +0000620SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
621 return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000622}
623
Rafael Espindola72447082017-01-05 14:35:41 +0000624unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
Rafael Espindola33713982017-01-05 14:20:35 +0000625 return hash_combine(Val.Name, Val.Flags, Val.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000626}
627
Rafael Espindola72447082017-01-05 14:35:41 +0000628bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
629 const SectionKey &RHS) {
George Rimar6892afa2016-07-12 09:49:43 +0000630 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
Rafael Espindola33713982017-01-05 14:20:35 +0000631 LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000632}
633
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000634namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000635namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000636
637template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
638template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
639template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
640template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000641
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000642template class OutputSection<ELF32LE>;
643template class OutputSection<ELF32BE>;
644template class OutputSection<ELF64LE>;
645template class OutputSection<ELF64BE>;
646
Rui Ueyama1e479c22016-05-23 15:07:59 +0000647template class EhOutputSection<ELF32LE>;
648template class EhOutputSection<ELF32BE>;
649template class EhOutputSection<ELF64LE>;
650template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000651
George Rimar6892afa2016-07-12 09:49:43 +0000652template class OutputSectionFactory<ELF32LE>;
653template class OutputSectionFactory<ELF32BE>;
654template class OutputSectionFactory<ELF64LE>;
655template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000656}
657}