blob: 72f80e7857ada7a98c5dbcb50917216c0002294d [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 Rimardbf93392017-04-17 08:58:12 +000019#include "llvm/Support/Compression.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
Rui Ueyama9d1bacb12017-02-27 02:31:26 +000034uint8_t Out::First;
Rui Ueyama9d1bacb12017-02-27 02:31:26 +000035OutputSection *Out::Opd;
36uint8_t *Out::OpdBuf;
37PhdrEntry *Out::TlsPhdr;
38OutputSection *Out::DebugInfo;
39OutputSection *Out::ElfHeader;
40OutputSection *Out::ProgramHeaders;
41OutputSection *Out::PreinitArray;
42OutputSection *Out::InitArray;
43OutputSection *Out::FiniArray;
44
Rafael Espindola24e6f362017-02-24 15:07:30 +000045uint32_t OutputSection::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000046 uint32_t Ret = PF_R;
47 if (Flags & SHF_WRITE)
48 Ret |= PF_W;
49 if (Flags & SHF_EXECINSTR)
50 Ret |= PF_X;
51 return Ret;
52}
53
Rafael Espindola35c6af32015-09-25 17:19:10 +000054template <class ELFT>
Rafael Espindola24e6f362017-02-24 15:07:30 +000055void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000056 Shdr->sh_entsize = Entsize;
Rafael Espindola37707632017-03-07 14:55:52 +000057 Shdr->sh_addralign = Alignment;
Rafael Espindola04a2e342016-11-09 01:42:41 +000058 Shdr->sh_type = Type;
59 Shdr->sh_offset = Offset;
60 Shdr->sh_flags = Flags;
61 Shdr->sh_info = Info;
62 Shdr->sh_link = Link;
Rafael Espindolaea590d92017-02-08 15:19:03 +000063 Shdr->sh_addr = Addr;
Rafael Espindola04a2e342016-11-09 01:42:41 +000064 Shdr->sh_size = Size;
65 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000066}
67
Rafael Espindola24e6f362017-02-24 15:07:30 +000068OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
Rafael Espindola5616adf2017-03-08 22:36:28 +000069 : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
70 /*Info*/ 0,
71 /*Link*/ 0) {}
George Rimar58941ee2016-02-25 08:23:37 +000072
Rafael Espindola774ea7d2017-02-23 16:49:07 +000073static bool compareByFilePosition(InputSection *A, InputSection *B) {
Peter Smith719eb8e2016-11-24 11:43:55 +000074 // Synthetic doesn't have link order dependecy, stable_sort will keep it last
Rafael Espindolac404d502017-02-23 02:32:18 +000075 if (A->kind() == InputSectionBase::Synthetic ||
76 B->kind() == InputSectionBase::Synthetic)
Peter Smith719eb8e2016-11-24 11:43:55 +000077 return false;
George Rimar9353e2d2017-03-21 08:29:48 +000078 auto *LA = cast<InputSection>(A->getLinkOrderDep());
79 auto *LB = cast<InputSection>(B->getLinkOrderDep());
Rafael Espindola24e6f362017-02-24 15:07:30 +000080 OutputSection *AOut = LA->OutSec;
81 OutputSection *BOut = LB->OutSec;
Rafael Espindola4d2d9c02016-11-18 19:02:15 +000082 if (AOut != BOut)
83 return AOut->SectionIndex < BOut->SectionIndex;
84 return LA->OutSecOff < LB->OutSecOff;
85}
86
Rui Ueyama066c4ab2017-04-19 11:31:58 +000087// Compress section contents if this section contains debug info.
George Rimardbf93392017-04-17 08:58:12 +000088template <class ELFT> void OutputSection::maybeCompress() {
Rui Ueyama066c4ab2017-04-19 11:31:58 +000089 typedef typename ELFT::Chdr Elf_Chdr;
90
91 // Compress only DWARF debug sections.
92 if (!Config->CompressDebugSections || !(Flags & SHF_ALLOC) ||
93 !Name.startswith(".debug_"))
George Rimardbf93392017-04-17 08:58:12 +000094 return;
95
Rui Ueyama066c4ab2017-04-19 11:31:58 +000096 // Create a section header.
Rui Ueyama07c62c12017-04-19 11:32:13 +000097 ZDebugHeader.resize(sizeof(Elf_Chdr));
98 auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
Rui Ueyama066c4ab2017-04-19 11:31:58 +000099 Hdr->ch_type = ELFCOMPRESS_ZLIB;
100 Hdr->ch_size = Size;
101 Hdr->ch_addralign = Alignment;
George Rimardbf93392017-04-17 08:58:12 +0000102
Rui Ueyama066c4ab2017-04-19 11:31:58 +0000103 // Write section contents to a temporary buffer and compress it.
104 std::vector<uint8_t> Buf(Size);
105 writeTo<ELFT>(Buf.data());
106 if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
George Rimardbf93392017-04-17 08:58:12 +0000107 fatal("compress failed: " + llvm::toString(std::move(E)));
108
Rui Ueyama066c4ab2017-04-19 11:31:58 +0000109 // Update section headers.
110 Size = sizeof(Elf_Chdr) + CompressedData.size();
111 Flags |= SHF_COMPRESSED;
George Rimardbf93392017-04-17 08:58:12 +0000112}
113
Rafael Espindola24e6f362017-02-24 15:07:30 +0000114template <class ELFT> void OutputSection::finalize() {
Eugene Levianta96d9022016-11-16 10:02:27 +0000115 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
George Rimar9353e2d2017-03-21 08:29:48 +0000116 std::sort(Sections.begin(), Sections.end(), compareByFilePosition);
George Rimarf98c5c12017-03-16 10:24:54 +0000117 assignOffsets();
Rafael Espindola4d2d9c02016-11-18 19:02:15 +0000118
Rafael Espindola933fcab2016-11-17 23:16:39 +0000119 // We must preserve the link order dependency of sections with the
120 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
121 // need to translate the InputSection sh_link to the OutputSection sh_link,
122 // all InputSections in the OutputSection have the same dependency.
George Rimar9353e2d2017-03-21 08:29:48 +0000123 if (auto *D = this->Sections.front()->getLinkOrderDep())
Eugene Levianta96d9022016-11-16 10:02:27 +0000124 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000125 }
Eugene Leviant22eb0262016-11-14 09:16:00 +0000126
Rafael Espindola933fcab2016-11-17 23:16:39 +0000127 uint32_t Type = this->Type;
Rui Ueyamad57e74b72017-03-17 23:29:01 +0000128 if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL))
George Rimar58941ee2016-02-25 08:23:37 +0000129 return;
Rafael Espindola933fcab2016-11-17 23:16:39 +0000130
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000131 InputSection *First = Sections[0];
Rui Ueyama9320cb02017-02-27 02:56:02 +0000132 if (isa<SyntheticSection>(First))
Rafael Espindola82f00ec2017-02-16 14:23:43 +0000133 return;
134
Eugene Leviant9230db92016-11-17 09:16:34 +0000135 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000136 // sh_info for SHT_REL[A] sections should contain the section header index of
137 // the section to which the relocation applies.
George Rimar1ec03e42017-03-21 09:13:27 +0000138 InputSectionBase *S = First->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000139 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000140}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000141
Rafael Espindoladc8eb812017-04-07 01:40:21 +0000142void OutputSection::addSection(InputSection *S) {
143 assert(S->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000144 Sections.push_back(S);
145 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000146 this->updateAlignment(S->Alignment);
Rui Ueyama27876642017-03-01 04:04:23 +0000147
148 // If this section contains a table of fixed-size entries, sh_entsize
149 // holds the element size. Consequently, if this contains two or more
150 // input sections, all of them must have the same sh_entsize. However,
151 // you can put different types of input sections into one output
152 // sectin by using linker scripts. I don't know what to do here.
153 // Probably we sholuld handle that as an error. But for now we just
154 // pick the largest sh_entsize.
155 this->Entsize = std::max(this->Entsize, S->Entsize);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156}
157
Rui Ueyama809d8e22016-06-23 04:33:42 +0000158// This function is called after we sort input sections
159// and scan relocations to setup sections' offsets.
George Rimarf98c5c12017-03-16 10:24:54 +0000160void OutputSection::assignOffsets() {
George Rimarefc31dd2017-03-01 11:10:53 +0000161 uint64_t Off = 0;
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000162 for (InputSection *S : Sections) {
Rui Ueyama809d8e22016-06-23 04:33:42 +0000163 Off = alignTo(Off, S->Alignment);
164 S->OutSecOff = Off;
Rafael Espindola76b6bd32017-03-08 15:44:30 +0000165 Off += S->getSize();
Rui Ueyama809d8e22016-06-23 04:33:42 +0000166 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000167 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000168}
169
Rafael Espindola24e6f362017-02-24 15:07:30 +0000170void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000171 typedef std::pair<unsigned, InputSection *> Pair;
George Rimar1a33c0f2016-11-10 09:05:20 +0000172 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
173
174 std::vector<Pair> V;
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000175 for (InputSection *S : Sections)
George Rimar1a33c0f2016-11-10 09:05:20 +0000176 V.push_back({Order(S), S});
177 std::stable_sort(V.begin(), V.end(), Comp);
178 Sections.clear();
179 for (Pair &P : V)
180 Sections.push_back(P.second);
181}
182
Rui Ueyamac4185702016-02-10 23:20:42 +0000183// Sorts input sections by section name suffixes, so that .foo.N comes
184// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000185// We want to keep the original order if the priorities are the same
186// because the compiler keeps the original initialization order in a
187// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000188// For more detail, read the section of the GCC's manual about init_priority.
Rafael Espindola24e6f362017-02-24 15:07:30 +0000189void OutputSection::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000190 // Sort sections by priority.
Rafael Espindolac404d502017-02-23 02:32:18 +0000191 sort([](InputSectionBase *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000192}
Rui Ueyamac4185702016-02-10 23:20:42 +0000193
Rui Ueyama5af83682016-02-11 23:41:38 +0000194// Returns true if S matches /Filename.?\.o$/.
195static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
196 if (!S.endswith(".o"))
197 return false;
198 S = S.drop_back(2);
199 if (S.endswith(Filename))
200 return true;
201 return !S.empty() && S.drop_back().endswith(Filename);
202}
203
204static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
205static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
206
207// .ctors and .dtors are sorted by this priority from highest to lowest.
208//
209// 1. The section was contained in crtbegin (crtbegin contains
210// some sentinel value in its .ctors and .dtors so that the runtime
211// can find the beginning of the sections.)
212//
213// 2. The section has an optional priority value in the form of ".ctors.N"
214// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
215// they are compared as string rather than number.
216//
217// 3. The section is just ".ctors" or ".dtors".
218//
219// 4. The section was contained in crtend, which contains an end marker.
220//
221// In an ideal world, we don't need this function because .init_array and
222// .ctors are duplicate features (and .init_array is newer.) However, there
223// are too many real-world use cases of .ctors, so we had no choice to
224// support that with this rather ad-hoc semantics.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000225static bool compCtors(const InputSection *A, const InputSection *B) {
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000226 bool BeginA = isCrtbegin(A->File->getName());
227 bool BeginB = isCrtbegin(B->File->getName());
Rui Ueyama5af83682016-02-11 23:41:38 +0000228 if (BeginA != BeginB)
229 return BeginA;
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000230 bool EndA = isCrtend(A->File->getName());
231 bool EndB = isCrtend(B->File->getName());
Rui Ueyama5af83682016-02-11 23:41:38 +0000232 if (EndA != EndB)
233 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000234 StringRef X = A->Name;
235 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000236 assert(X.startswith(".ctors") || X.startswith(".dtors"));
237 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
238 X = X.substr(6);
239 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000240 if (X.empty() && Y.empty())
241 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000242 return X < Y;
243}
244
245// Sorts input sections by the special rules for .ctors and .dtors.
246// Unfortunately, the rules are different from the one for .{init,fini}_array.
247// Read the comment above.
Rafael Espindola24e6f362017-02-24 15:07:30 +0000248void OutputSection::sortCtorsDtors() {
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000249 std::stable_sort(Sections.begin(), Sections.end(), compCtors);
Rui Ueyamac4185702016-02-10 23:20:42 +0000250}
251
Rui Ueyamab58079d42017-04-11 22:45:57 +0000252// Fill [Buf, Buf + Size) with Filler.
253// This is used for linker script "=fillexp" command.
Rui Ueyamac49bb0f2017-04-03 19:14:35 +0000254static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
George Rimare2ee72b2016-02-26 14:48:31 +0000255 size_t I = 0;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000256 for (; I + 4 < Size; I += 4)
Rui Ueyamab58079d42017-04-11 22:45:57 +0000257 memcpy(Buf + I, &Filler, 4);
258 memcpy(Buf + I, &Filler, Size - I);
George Rimare2ee72b2016-02-26 14:48:31 +0000259}
260
Rui Ueyama8f8c2f92017-04-11 22:45:38 +0000261uint32_t OutputSection::getFiller() {
James Henderson9d9a6632017-04-07 10:36:42 +0000262 // Determine what to fill gaps between InputSections with, as specified by the
263 // linker script. If nothing is specified and this is an executable section,
264 // fall back to trap instructions to prevent bad diassembly and detect invalid
265 // jumps to padding.
266 if (Optional<uint32_t> Filler = Script->getFiller(Name))
267 return *Filler;
268 if (Flags & SHF_EXECINSTR)
269 return Target->TrapInstr;
270 return 0;
271}
272
Rafael Espindola24e6f362017-02-24 15:07:30 +0000273template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
Eugene Leviant84569e62016-11-29 08:05:44 +0000274 Loc = Buf;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000275
George Rimardbf93392017-04-17 08:58:12 +0000276 // We may have already rendered compressed content when using
277 // -compress-debug-sections option. Write it together with header.
278 if (!CompressedData.empty()) {
Rui Ueyama07c62c12017-04-19 11:32:13 +0000279 memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
280 memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
281 CompressedData.size());
George Rimardbf93392017-04-17 08:58:12 +0000282 return;
283 }
284
James Henderson9d9a6632017-04-07 10:36:42 +0000285 // Write leading padding.
Rui Ueyama8f8c2f92017-04-11 22:45:38 +0000286 uint32_t Filler = getFiller();
287 if (Filler)
288 fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler);
James Henderson9d9a6632017-04-07 10:36:42 +0000289
290 parallelFor(0, Sections.size(), [=](size_t I) {
291 InputSection *Sec = Sections[I];
292 Sec->writeTo<ELFT>(Buf);
293
Rui Ueyama8f8c2f92017-04-11 22:45:38 +0000294 // Fill gaps between sections.
295 if (Filler) {
296 uint8_t *Start = Buf + Sec->OutSecOff + Sec->getSize();
297 uint8_t *End;
298 if (I + 1 == Sections.size())
299 End = Buf + Size;
300 else
301 End = Buf + Sections[I + 1]->OutSecOff;
302 fill(Start, End - Start, Filler);
303 }
James Henderson9d9a6632017-04-07 10:36:42 +0000304 });
Rui Ueyama16068ae2016-11-19 18:05:56 +0000305
George Rimare38cbab2016-09-26 19:22:50 +0000306 // Linker scripts may have BYTE()-family commands with which you
307 // can write arbitrary bytes to the output. Process them if any.
James Henderson9d9a6632017-04-07 10:36:42 +0000308 Script->writeDataBytes(Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000309}
310
George Rimar788fe382017-03-14 09:14:28 +0000311static uint64_t getOutFlags(InputSectionBase *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000312 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +0000313}
314
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000315static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
Rafael Espindola33713982017-01-05 14:20:35 +0000316 // The ELF spec just says
317 // ----------------------------------------------------------------
318 // In the first phase, input sections that match in name, type and
319 // attribute flags should be concatenated into single sections.
320 // ----------------------------------------------------------------
321 //
322 // However, it is clear that at least some flags have to be ignored for
323 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
324 // ignored. We should not have two output .text sections just because one was
325 // in a group and another was not for example.
326 //
327 // It also seems that that wording was a late addition and didn't get the
328 // necessary scrutiny.
329 //
330 // Merging sections with different flags is expected by some users. One
331 // reason is that if one file has
332 //
333 // int *const bar __attribute__((section(".foo"))) = (int *)0;
334 //
335 // gcc with -fPIC will produce a read only .foo section. But if another
336 // file has
337 //
338 // int zed;
339 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
340 //
341 // gcc with -fPIC will produce a read write section.
342 //
343 // Last but not least, when using linker script the merge rules are forced by
344 // the script. Unfortunately, linker scripts are name based. This means that
345 // expressions like *(.foo*) can refer to multiple input sections with
346 // different flags. We cannot put them in different output sections or we
347 // would produce wrong results for
348 //
349 // start = .; *(.foo.*) end = .; *(.bar)
350 //
351 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
352 // another. The problem is that there is no way to layout those output
353 // sections such that the .foo sections are the only thing between the start
354 // and end symbols.
355 //
356 // Given the above issues, we instead merge sections by name and error on
357 // incompatible types and flags.
Rafael Espindola17c35832016-09-13 12:25:30 +0000358
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000359 uint32_t Alignment = 0;
George Rimar09268b72017-03-14 09:25:03 +0000360 uint64_t Flags = 0;
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000361 if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
George Rimar09268b72017-03-14 09:25:03 +0000362 Alignment = std::max<uint64_t>(C->Alignment, C->Entsize);
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000363 Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
364 }
Rafael Espindola17c35832016-09-13 12:25:30 +0000365
Rafael Espindola72447082017-01-05 14:35:41 +0000366 return SectionKey{OutsecName, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000367}
368
Rui Ueyama02a036f2017-02-27 02:31:48 +0000369OutputSectionFactory::OutputSectionFactory(
Rafael Espindola24e6f362017-02-24 15:07:30 +0000370 std::vector<OutputSection *> &OutputSections)
Rafael Espindola82902742017-02-16 17:32:26 +0000371 : OutputSections(OutputSections) {}
George Rimar6892afa2016-07-12 09:49:43 +0000372
Rafael Espindola33713982017-01-05 14:20:35 +0000373static uint64_t getIncompatibleFlags(uint64_t Flags) {
374 return Flags & (SHF_ALLOC | SHF_TLS);
375}
376
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000377// We allow sections of types listed below to merged into a
378// single progbits section. This is typically done by linker
379// scripts. Merging nobits and progbits will force disk space
380// to be allocated for nobits sections. Other ones don't require
381// any special treatment on top of progbits, so there doesn't
382// seem to be a harm in merging them.
383static bool canMergeToProgbits(unsigned Type) {
384 return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
385 Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
386 Type == SHT_NOTE;
387}
388
George Rimarf08b5922017-03-14 09:19:34 +0000389static void reportDiscarded(InputSectionBase *IS) {
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000390 if (!Config->PrintGcSections)
391 return;
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000392 message("removing unused section from '" + IS->Name + "' in file '" +
George Rimarf08b5922017-03-14 09:19:34 +0000393 IS->File->getName());
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000394}
395
Rui Ueyama02a036f2017-02-27 02:31:48 +0000396void OutputSectionFactory::addInputSec(InputSectionBase *IS,
397 StringRef OutsecName) {
Rafael Espindola82902742017-02-16 17:32:26 +0000398 if (!IS->Live) {
George Rimarf08b5922017-03-14 09:19:34 +0000399 reportDiscarded(IS);
Rafael Espindola82902742017-02-16 17:32:26 +0000400 return;
401 }
402
George Rimar09268b72017-03-14 09:25:03 +0000403 SectionKey Key = createKey(IS, OutsecName);
George Rimar788fe382017-03-14 09:14:28 +0000404 uint64_t Flags = getOutFlags(IS);
Rafael Espindola24e6f362017-02-24 15:07:30 +0000405 OutputSection *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +0000406 if (Sec) {
Rafael Espindola82902742017-02-16 17:32:26 +0000407 if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
Rafael Espindola33713982017-01-05 14:20:35 +0000408 error("Section has flags incompatible with others with the same name " +
Rafael Espindola82902742017-02-16 17:32:26 +0000409 toString(IS));
410 if (Sec->Type != IS->Type) {
411 if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000412 Sec->Type = SHT_PROGBITS;
413 else
414 error("Section has different type from others with the same name " +
Rafael Espindola82902742017-02-16 17:32:26 +0000415 toString(IS));
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000416 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000417 Sec->Flags |= Flags;
Rafael Espindola82902742017-02-16 17:32:26 +0000418 } else {
Petr Hosek7b793212017-03-10 20:00:42 +0000419 Sec = make<OutputSection>(Key.Name, IS->Type, Flags);
Rafael Espindola82902742017-02-16 17:32:26 +0000420 OutputSections.push_back(Sec);
Rafael Espindola10897f12016-09-13 14:23:14 +0000421 }
422
Rafael Espindoladc8eb812017-04-07 01:40:21 +0000423 Sec->addSection(cast<InputSection>(IS));
George Rimar6892afa2016-07-12 09:49:43 +0000424}
425
Rui Ueyama02a036f2017-02-27 02:31:48 +0000426OutputSectionFactory::~OutputSectionFactory() {}
Rafael Espindola82902742017-02-16 17:32:26 +0000427
Rafael Espindola72447082017-01-05 14:35:41 +0000428SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
429 return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000430}
431
Rafael Espindola72447082017-01-05 14:35:41 +0000432SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
433 return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000434}
435
Rafael Espindola72447082017-01-05 14:35:41 +0000436unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
Rafael Espindola33713982017-01-05 14:20:35 +0000437 return hash_combine(Val.Name, Val.Flags, Val.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000438}
439
Rafael Espindola72447082017-01-05 14:35:41 +0000440bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
441 const SectionKey &RHS) {
George Rimar6892afa2016-07-12 09:49:43 +0000442 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
Rafael Espindola33713982017-01-05 14:20:35 +0000443 LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000444}
445
George Rimar78aa2702017-03-13 14:40:58 +0000446uint64_t elf::getHeaderSize() {
447 if (Config->OFormatBinary)
448 return 0;
449 return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
450}
451
Rafael Espindola24e6f362017-02-24 15:07:30 +0000452template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
453template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
454template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
455template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000456
Rafael Espindola24e6f362017-02-24 15:07:30 +0000457template void OutputSection::finalize<ELF32LE>();
458template void OutputSection::finalize<ELF32BE>();
459template void OutputSection::finalize<ELF64LE>();
460template void OutputSection::finalize<ELF64BE>();
461
George Rimardbf93392017-04-17 08:58:12 +0000462template void OutputSection::maybeCompress<ELF32LE>();
463template void OutputSection::maybeCompress<ELF32BE>();
464template void OutputSection::maybeCompress<ELF64LE>();
465template void OutputSection::maybeCompress<ELF64BE>();
466
Rafael Espindola24e6f362017-02-24 15:07:30 +0000467template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
468template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
469template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
470template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);