blob: 7ce9361bff822763bfcdf8e13b6d4c89f6b5faec [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 Ueyama95642b92016-11-01 23:09:07 +000012#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000013#include "Memory.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"
George Rimarf6bc65a2016-01-15 13:34:52 +000019#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000020#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000021#include "llvm/Support/MathExtras.h"
Rafael Espindolaa42b3bc2016-09-27 16:43:49 +000022#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000023
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000025using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000027using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028using namespace llvm::ELF;
29
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000032
Rafael Espindolae08e78d2016-11-09 23:23:45 +000033OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
34 uint64_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035 : Name(Name) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000036 this->Type = Type;
37 this->Flags = Flags;
38 this->Addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039}
40
Rafael Espindolae08e78d2016-11-09 23:23:45 +000041uint32_t OutputSectionBase::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000042 uint32_t Ret = PF_R;
43 if (Flags & SHF_WRITE)
44 Ret |= PF_W;
45 if (Flags & SHF_EXECINSTR)
46 Ret |= PF_X;
47 return Ret;
48}
49
Rafael Espindola35c6af32015-09-25 17:19:10 +000050template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +000051void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000052 Shdr->sh_entsize = Entsize;
53 Shdr->sh_addralign = Addralign;
54 Shdr->sh_type = Type;
55 Shdr->sh_offset = Offset;
56 Shdr->sh_flags = Flags;
57 Shdr->sh_info = Info;
58 Shdr->sh_link = Link;
Rafael Espindolaea590d92017-02-08 15:19:03 +000059 Shdr->sh_addr = Addr;
Rafael Espindola04a2e342016-11-09 01:42:41 +000060 Shdr->sh_size = Size;
61 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000062}
63
Rui Ueyamadf5d14d2016-11-09 22:32:42 +000064template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
65 switch (Type) {
66 case SHT_RELA:
67 return sizeof(typename ELFT::Rela);
68 case SHT_REL:
69 return sizeof(typename ELFT::Rel);
70 case SHT_MIPS_REGINFO:
71 return sizeof(Elf_Mips_RegInfo<ELFT>);
72 case SHT_MIPS_OPTIONS:
73 return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
74 case SHT_MIPS_ABIFLAGS:
75 return sizeof(Elf_Mips_ABIFlags<ELFT>);
76 default:
77 return 0;
78 }
79}
80
George Rimarf6bc65a2016-01-15 13:34:52 +000081template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +000082OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
Rafael Espindolae08e78d2016-11-09 23:23:45 +000083 : OutputSectionBase(Name, Type, Flags) {
Rui Ueyamadf5d14d2016-11-09 22:32:42 +000084 this->Entsize = getEntsize<ELFT>(Type);
George Rimar58941ee2016-02-25 08:23:37 +000085}
86
Rafael Espindola4d2d9c02016-11-18 19:02:15 +000087template <typename ELFT>
Rafael Espindola774ea7d2017-02-23 16:49:07 +000088static bool compareByFilePosition(InputSection *A, InputSection *B) {
Peter Smith719eb8e2016-11-24 11:43:55 +000089 // Synthetic doesn't have link order dependecy, stable_sort will keep it last
Rafael Espindolac404d502017-02-23 02:32:18 +000090 if (A->kind() == InputSectionBase::Synthetic ||
91 B->kind() == InputSectionBase::Synthetic)
Peter Smith719eb8e2016-11-24 11:43:55 +000092 return false;
Rafael Espindola774ea7d2017-02-23 16:49:07 +000093 auto *LA = cast<InputSection>(A->template getLinkOrderDep<ELFT>());
94 auto *LB = cast<InputSection>(B->template getLinkOrderDep<ELFT>());
Rafael Espindola4d2d9c02016-11-18 19:02:15 +000095 OutputSectionBase *AOut = LA->OutSec;
96 OutputSectionBase *BOut = LB->OutSec;
97 if (AOut != BOut)
98 return AOut->SectionIndex < BOut->SectionIndex;
99 return LA->OutSecOff < LB->OutSecOff;
100}
101
George Rimar58941ee2016-02-25 08:23:37 +0000102template <class ELFT> void OutputSection<ELFT>::finalize() {
Eugene Levianta96d9022016-11-16 10:02:27 +0000103 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
Rafael Espindola4d2d9c02016-11-18 19:02:15 +0000104 std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>);
105 Size = 0;
106 assignOffsets();
107
Rafael Espindola933fcab2016-11-17 23:16:39 +0000108 // We must preserve the link order dependency of sections with the
109 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
110 // need to translate the InputSection sh_link to the OutputSection sh_link,
111 // all InputSections in the OutputSection have the same dependency.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000112 if (auto *D = this->Sections.front()->template getLinkOrderDep<ELFT>())
Eugene Levianta96d9022016-11-16 10:02:27 +0000113 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000114 }
Eugene Leviant22eb0262016-11-14 09:16:00 +0000115
Rafael Espindola933fcab2016-11-17 23:16:39 +0000116 uint32_t Type = this->Type;
George Rimar82bd8be2017-02-08 16:18:10 +0000117 if (!Config->copyRelocs() || (Type != SHT_RELA && Type != SHT_REL))
George Rimar58941ee2016-02-25 08:23:37 +0000118 return;
Rafael Espindola933fcab2016-11-17 23:16:39 +0000119
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000120 InputSection *First = Sections[0];
Rafael Espindola82f00ec2017-02-16 14:23:43 +0000121 if (isa<SyntheticSection<ELFT>>(First))
122 return;
123
Eugene Leviant9230db92016-11-17 09:16:34 +0000124 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000125 // sh_info for SHT_REL[A] sections should contain the section header index of
126 // the section to which the relocation applies.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000127 InputSectionBase *S = First->getRelocatedSection<ELFT>();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000128 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000129}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000130
131template <class ELFT>
Rafael Espindolac404d502017-02-23 02:32:18 +0000132void OutputSection<ELFT>::addSection(InputSectionBase *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000133 assert(C->Live);
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000134 auto *S = cast<InputSection>(C);
Rui Ueyama40845e62015-12-26 05:51:07 +0000135 Sections.push_back(S);
136 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000137 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000138 // Keep sh_entsize value of the input section to be able to perform merging
139 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000140 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000141 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142}
143
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000144template <class ELFT>
145void OutputSection<ELFT>::forEachInputSection(
Rafael Espindolac404d502017-02-23 02:32:18 +0000146 std::function<void(InputSectionBase *)> F) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000147 for (InputSection *S : Sections)
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000148 F(S);
149}
150
Rui Ueyama809d8e22016-06-23 04:33:42 +0000151// This function is called after we sort input sections
152// and scan relocations to setup sections' offsets.
153template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000154 uintX_t Off = this->Size;
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000155 for (InputSection *S : Sections) {
Rui Ueyama809d8e22016-06-23 04:33:42 +0000156 Off = alignTo(Off, S->Alignment);
157 S->OutSecOff = Off;
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000158 Off += S->template getSize<ELFT>();
Rui Ueyama809d8e22016-06-23 04:33:42 +0000159 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000160 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000161}
162
George Rimar1a33c0f2016-11-10 09:05:20 +0000163template <class ELFT>
Rafael Espindolac404d502017-02-23 02:32:18 +0000164void OutputSection<ELFT>::sort(std::function<int(InputSectionBase *S)> Order) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000165 typedef std::pair<unsigned, InputSection *> Pair;
George Rimar1a33c0f2016-11-10 09:05:20 +0000166 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
167
168 std::vector<Pair> V;
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000169 for (InputSection *S : Sections)
George Rimar1a33c0f2016-11-10 09:05:20 +0000170 V.push_back({Order(S), S});
171 std::stable_sort(V.begin(), V.end(), Comp);
172 Sections.clear();
173 for (Pair &P : V)
174 Sections.push_back(P.second);
175}
176
Rui Ueyamac4185702016-02-10 23:20:42 +0000177// Sorts input sections by section name suffixes, so that .foo.N comes
178// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000179// We want to keep the original order if the priorities are the same
180// because the compiler keeps the original initialization order in a
181// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000182// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000183template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000184 // Sort sections by priority.
Rafael Espindolac404d502017-02-23 02:32:18 +0000185 sort([](InputSectionBase *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000186}
Rui Ueyamac4185702016-02-10 23:20:42 +0000187
Rui Ueyama5af83682016-02-11 23:41:38 +0000188// Returns true if S matches /Filename.?\.o$/.
189static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
190 if (!S.endswith(".o"))
191 return false;
192 S = S.drop_back(2);
193 if (S.endswith(Filename))
194 return true;
195 return !S.empty() && S.drop_back().endswith(Filename);
196}
197
198static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
199static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
200
201// .ctors and .dtors are sorted by this priority from highest to lowest.
202//
203// 1. The section was contained in crtbegin (crtbegin contains
204// some sentinel value in its .ctors and .dtors so that the runtime
205// can find the beginning of the sections.)
206//
207// 2. The section has an optional priority value in the form of ".ctors.N"
208// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
209// they are compared as string rather than number.
210//
211// 3. The section is just ".ctors" or ".dtors".
212//
213// 4. The section was contained in crtend, which contains an end marker.
214//
215// In an ideal world, we don't need this function because .init_array and
216// .ctors are duplicate features (and .init_array is newer.) However, there
217// are too many real-world use cases of .ctors, so we had no choice to
218// support that with this rather ad-hoc semantics.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000219static bool compCtors(const InputSection *A, const InputSection *B) {
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000220 bool BeginA = isCrtbegin(A->File->getName());
221 bool BeginB = isCrtbegin(B->File->getName());
Rui Ueyama5af83682016-02-11 23:41:38 +0000222 if (BeginA != BeginB)
223 return BeginA;
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000224 bool EndA = isCrtend(A->File->getName());
225 bool EndB = isCrtend(B->File->getName());
Rui Ueyama5af83682016-02-11 23:41:38 +0000226 if (EndA != EndB)
227 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000228 StringRef X = A->Name;
229 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000230 assert(X.startswith(".ctors") || X.startswith(".dtors"));
231 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
232 X = X.substr(6);
233 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000234 if (X.empty() && Y.empty())
235 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000236 return X < Y;
237}
238
239// Sorts input sections by the special rules for .ctors and .dtors.
240// Unfortunately, the rules are different from the one for .{init,fini}_array.
241// Read the comment above.
242template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000243 std::stable_sort(Sections.begin(), Sections.end(), compCtors);
Rui Ueyamac4185702016-02-10 23:20:42 +0000244}
245
Rui Ueyama16068ae2016-11-19 18:05:56 +0000246// Fill [Buf, Buf + Size) with Filler. Filler is written in big
247// endian order. This is used for linker script "=fillexp" command.
248void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
249 uint8_t V[4];
250 write32be(V, Filler);
George Rimare2ee72b2016-02-26 14:48:31 +0000251 size_t I = 0;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000252 for (; I + 4 < Size; I += 4)
253 memcpy(Buf + I, V, 4);
254 memcpy(Buf + I, V, Size - I);
George Rimare2ee72b2016-02-26 14:48:31 +0000255}
256
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant84569e62016-11-29 08:05:44 +0000258 Loc = Buf;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000259 if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000260 fill(Buf, this->Size, Filler);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000261
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000262 auto Fn = [=](InputSection *IS) { IS->writeTo<ELFT>(Buf); };
Rui Ueyama244a4352016-12-03 21:24:51 +0000263 forEach(Sections.begin(), Sections.end(), Fn);
Rui Ueyama16068ae2016-11-19 18:05:56 +0000264
George Rimare38cbab2016-09-26 19:22:50 +0000265 // Linker scripts may have BYTE()-family commands with which you
266 // can write arbitrary bytes to the output. Process them if any.
267 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268}
269
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000270template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000271static typename ELFT::uint getOutFlags(InputSectionBase *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000272 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +0000273}
274
275template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000276static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
Rafael Espindola33713982017-01-05 14:20:35 +0000277 // The ELF spec just says
278 // ----------------------------------------------------------------
279 // In the first phase, input sections that match in name, type and
280 // attribute flags should be concatenated into single sections.
281 // ----------------------------------------------------------------
282 //
283 // However, it is clear that at least some flags have to be ignored for
284 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
285 // ignored. We should not have two output .text sections just because one was
286 // in a group and another was not for example.
287 //
288 // It also seems that that wording was a late addition and didn't get the
289 // necessary scrutiny.
290 //
291 // Merging sections with different flags is expected by some users. One
292 // reason is that if one file has
293 //
294 // int *const bar __attribute__((section(".foo"))) = (int *)0;
295 //
296 // gcc with -fPIC will produce a read only .foo section. But if another
297 // file has
298 //
299 // int zed;
300 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
301 //
302 // gcc with -fPIC will produce a read write section.
303 //
304 // Last but not least, when using linker script the merge rules are forced by
305 // the script. Unfortunately, linker scripts are name based. This means that
306 // expressions like *(.foo*) can refer to multiple input sections with
307 // different flags. We cannot put them in different output sections or we
308 // would produce wrong results for
309 //
310 // start = .; *(.foo.*) end = .; *(.bar)
311 //
312 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
313 // another. The problem is that there is no way to layout those output
314 // sections such that the .foo sections are the only thing between the start
315 // and end symbols.
316 //
317 // Given the above issues, we instead merge sections by name and error on
318 // incompatible types and flags.
Rafael Espindola17c35832016-09-13 12:25:30 +0000319
Rafael Espindola33713982017-01-05 14:20:35 +0000320 typedef typename ELFT::uint uintX_t;
Rafael Espindola33713982017-01-05 14:20:35 +0000321
Rafael Espindola17c35832016-09-13 12:25:30 +0000322 uintX_t Alignment = 0;
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000323 uintX_t Flags = 0;
324 if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000325 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000326 Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
327 }
Rafael Espindola17c35832016-09-13 12:25:30 +0000328
Rafael Espindola72447082017-01-05 14:35:41 +0000329 return SectionKey{OutsecName, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000330}
331
Rafael Espindola82902742017-02-16 17:32:26 +0000332template <class ELFT>
333OutputSectionFactory<ELFT>::OutputSectionFactory(
334 std::vector<OutputSectionBase *> &OutputSections)
335 : OutputSections(OutputSections) {}
George Rimar6892afa2016-07-12 09:49:43 +0000336
Rafael Espindola33713982017-01-05 14:20:35 +0000337static uint64_t getIncompatibleFlags(uint64_t Flags) {
338 return Flags & (SHF_ALLOC | SHF_TLS);
339}
340
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000341// We allow sections of types listed below to merged into a
342// single progbits section. This is typically done by linker
343// scripts. Merging nobits and progbits will force disk space
344// to be allocated for nobits sections. Other ones don't require
345// any special treatment on top of progbits, so there doesn't
346// seem to be a harm in merging them.
347static bool canMergeToProgbits(unsigned Type) {
348 return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
349 Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
350 Type == SHT_NOTE;
351}
352
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000353template <class ELFT> static void reportDiscarded(InputSectionBase *IS) {
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000354 if (!Config->PrintGcSections)
355 return;
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000356 message("removing unused section from '" + IS->Name + "' in file '" +
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000357 IS->getFile<ELFT>()->getName());
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000358}
359
Rafael Espindola10897f12016-09-13 14:23:14 +0000360template <class ELFT>
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000361void OutputSectionFactory<ELFT>::addInputSec(InputSectionBase *IS,
Rafael Espindola82902742017-02-16 17:32:26 +0000362 StringRef OutsecName) {
363 if (!IS->Live) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000364 reportDiscarded<ELFT>(IS);
Rafael Espindola82902742017-02-16 17:32:26 +0000365 return;
366 }
367
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000368 SectionKey Key = createKey<ELFT>(IS, OutsecName);
369 uintX_t Flags = getOutFlags<ELFT>(IS);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000370 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +0000371 if (Sec) {
Rafael Espindola82902742017-02-16 17:32:26 +0000372 if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
Rafael Espindola33713982017-01-05 14:20:35 +0000373 error("Section has flags incompatible with others with the same name " +
Rafael Espindola82902742017-02-16 17:32:26 +0000374 toString(IS));
375 if (Sec->Type != IS->Type) {
376 if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000377 Sec->Type = SHT_PROGBITS;
378 else
379 error("Section has different type from others with the same name " +
Rafael Espindola82902742017-02-16 17:32:26 +0000380 toString(IS));
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000381 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000382 Sec->Flags |= Flags;
Rafael Espindola82902742017-02-16 17:32:26 +0000383 } else {
384 uint32_t Type = IS->Type;
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000385 if (IS->kind() == InputSectionBase::EHFrame) {
Rafael Espindola66b4e212017-02-23 22:06:28 +0000386 In<ELFT>::EhFrame->addSection(IS);
Rafael Espindola82902742017-02-16 17:32:26 +0000387 return;
388 }
389 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
390 OutputSections.push_back(Sec);
Rafael Espindola10897f12016-09-13 14:23:14 +0000391 }
392
Rafael Espindola82902742017-02-16 17:32:26 +0000393 Sec->addSection(IS);
George Rimar6892afa2016-07-12 09:49:43 +0000394}
395
Rafael Espindola82902742017-02-16 17:32:26 +0000396template <class ELFT> OutputSectionFactory<ELFT>::~OutputSectionFactory() {}
397
Rafael Espindola72447082017-01-05 14:35:41 +0000398SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
399 return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000400}
401
Rafael Espindola72447082017-01-05 14:35:41 +0000402SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
403 return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000404}
405
Rafael Espindola72447082017-01-05 14:35:41 +0000406unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
Rafael Espindola33713982017-01-05 14:20:35 +0000407 return hash_combine(Val.Name, Val.Flags, Val.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000408}
409
Rafael Espindola72447082017-01-05 14:35:41 +0000410bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
411 const SectionKey &RHS) {
George Rimar6892afa2016-07-12 09:49:43 +0000412 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
Rafael Espindola33713982017-01-05 14:20:35 +0000413 LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000414}
415
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000416namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000417namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000418
419template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
420template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
421template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
422template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000423
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000424template class OutputSection<ELF32LE>;
425template class OutputSection<ELF32BE>;
426template class OutputSection<ELF64LE>;
427template class OutputSection<ELF64BE>;
428
George Rimar6892afa2016-07-12 09:49:43 +0000429template class OutputSectionFactory<ELF32LE>;
430template class OutputSectionFactory<ELF32BE>;
431template class OutputSectionFactory<ELF64LE>;
432template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000433}
434}