blob: 6f04a04be8d0379b3c97c5fc69b07ff0504040f1 [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"
Zachary Turner264b5d92017-06-07 03:48:56 +000019#include "llvm/BinaryFormat/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
Rui Ueyama9d1bacb12017-02-27 02:31:26 +000033uint8_t Out::First;
Rui Ueyama9d1bacb12017-02-27 02:31:26 +000034OutputSection *Out::Opd;
35uint8_t *Out::OpdBuf;
36PhdrEntry *Out::TlsPhdr;
37OutputSection *Out::DebugInfo;
38OutputSection *Out::ElfHeader;
39OutputSection *Out::ProgramHeaders;
40OutputSection *Out::PreinitArray;
41OutputSection *Out::InitArray;
42OutputSection *Out::FiniArray;
43
Rafael Espindolaf51c8052017-06-13 23:26:31 +000044std::vector<OutputSection *> elf::OutputSections;
45std::vector<OutputSectionCommand *> elf::OutputSectionCommands;
46
Rafael Espindola24e6f362017-02-24 15:07:30 +000047uint32_t OutputSection::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000048 uint32_t Ret = PF_R;
49 if (Flags & SHF_WRITE)
50 Ret |= PF_W;
51 if (Flags & SHF_EXECINSTR)
52 Ret |= PF_X;
53 return Ret;
54}
55
Rafael Espindola35c6af32015-09-25 17:19:10 +000056template <class ELFT>
Rafael Espindola24e6f362017-02-24 15:07:30 +000057void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000058 Shdr->sh_entsize = Entsize;
Rafael Espindola37707632017-03-07 14:55:52 +000059 Shdr->sh_addralign = Alignment;
Rafael Espindola04a2e342016-11-09 01:42:41 +000060 Shdr->sh_type = Type;
61 Shdr->sh_offset = Offset;
62 Shdr->sh_flags = Flags;
63 Shdr->sh_info = Info;
64 Shdr->sh_link = Link;
Rafael Espindolaea590d92017-02-08 15:19:03 +000065 Shdr->sh_addr = Addr;
Rafael Espindola04a2e342016-11-09 01:42:41 +000066 Shdr->sh_size = Size;
67 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000068}
69
Rafael Espindola24e6f362017-02-24 15:07:30 +000070OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
Rafael Espindola5616adf2017-03-08 22:36:28 +000071 : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
72 /*Info*/ 0,
Rafael Espindola660c9ab2017-05-05 21:34:26 +000073 /*Link*/ 0),
74 SectionIndex(INT_MAX) {}
George Rimar58941ee2016-02-25 08:23:37 +000075
George Rimard86a4e52017-05-08 10:18:12 +000076static uint64_t updateOffset(uint64_t Off, InputSection *S) {
77 Off = alignTo(Off, S->Alignment);
78 S->OutSecOff = Off;
79 return Off + S->getSize();
80}
81
Rafael Espindoladc8eb812017-04-07 01:40:21 +000082void OutputSection::addSection(InputSection *S) {
83 assert(S->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +000084 Sections.push_back(S);
Rafael Espindoladb5e56f2017-05-31 20:17:44 +000085 S->Parent = this;
Rui Ueyama424b4082016-06-17 01:18:46 +000086 this->updateAlignment(S->Alignment);
Rui Ueyama27876642017-03-01 04:04:23 +000087
George Rimard86a4e52017-05-08 10:18:12 +000088 // The actual offsets will be computed by assignAddresses. For now, use
89 // crude approximation so that it is at least easy for other code to know the
90 // section order. It is also used to calculate the output section size early
91 // for compressed debug sections.
92 this->Size = updateOffset(Size, S);
93
Rui Ueyama27876642017-03-01 04:04:23 +000094 // If this section contains a table of fixed-size entries, sh_entsize
95 // holds the element size. Consequently, if this contains two or more
96 // input sections, all of them must have the same sh_entsize. However,
97 // you can put different types of input sections into one output
98 // sectin by using linker scripts. I don't know what to do here.
99 // Probably we sholuld handle that as an error. But for now we just
100 // pick the largest sh_entsize.
101 this->Entsize = std::max(this->Entsize, S->Entsize);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102}
103
Rui Ueyama809d8e22016-06-23 04:33:42 +0000104// This function is called after we sort input sections
105// and scan relocations to setup sections' offsets.
George Rimarf98c5c12017-03-16 10:24:54 +0000106void OutputSection::assignOffsets() {
Rafael Espindola72046202017-06-07 23:08:55 +0000107 OutputSectionCommand *Cmd = Script->getCmd(this);
George Rimarefc31dd2017-03-01 11:10:53 +0000108 uint64_t Off = 0;
Rafael Espindola72046202017-06-07 23:08:55 +0000109 for (BaseCommand *Base : Cmd->Commands)
110 if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
111 for (InputSection *S : ISD->Sections)
112 Off = updateOffset(Off, S);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000113 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000114}
115
Rafael Espindola24e6f362017-02-24 15:07:30 +0000116void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000117 typedef std::pair<unsigned, InputSection *> Pair;
George Rimar1a33c0f2016-11-10 09:05:20 +0000118 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
119
120 std::vector<Pair> V;
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000121 for (InputSection *S : Sections)
George Rimar1a33c0f2016-11-10 09:05:20 +0000122 V.push_back({Order(S), S});
123 std::stable_sort(V.begin(), V.end(), Comp);
124 Sections.clear();
125 for (Pair &P : V)
126 Sections.push_back(P.second);
127}
128
Rui Ueyamac4185702016-02-10 23:20:42 +0000129// Sorts input sections by section name suffixes, so that .foo.N comes
130// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000131// We want to keep the original order if the priorities are the same
132// because the compiler keeps the original initialization order in a
133// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000134// For more detail, read the section of the GCC's manual about init_priority.
Rafael Espindola24e6f362017-02-24 15:07:30 +0000135void OutputSection::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000136 // Sort sections by priority.
Rafael Espindolac404d502017-02-23 02:32:18 +0000137 sort([](InputSectionBase *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000138}
Rui Ueyamac4185702016-02-10 23:20:42 +0000139
Rui Ueyama5af83682016-02-11 23:41:38 +0000140// Returns true if S matches /Filename.?\.o$/.
141static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
142 if (!S.endswith(".o"))
143 return false;
144 S = S.drop_back(2);
145 if (S.endswith(Filename))
146 return true;
147 return !S.empty() && S.drop_back().endswith(Filename);
148}
149
150static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
151static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
152
153// .ctors and .dtors are sorted by this priority from highest to lowest.
154//
155// 1. The section was contained in crtbegin (crtbegin contains
156// some sentinel value in its .ctors and .dtors so that the runtime
157// can find the beginning of the sections.)
158//
159// 2. The section has an optional priority value in the form of ".ctors.N"
160// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
161// they are compared as string rather than number.
162//
163// 3. The section is just ".ctors" or ".dtors".
164//
165// 4. The section was contained in crtend, which contains an end marker.
166//
167// In an ideal world, we don't need this function because .init_array and
168// .ctors are duplicate features (and .init_array is newer.) However, there
169// are too many real-world use cases of .ctors, so we had no choice to
170// support that with this rather ad-hoc semantics.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000171static bool compCtors(const InputSection *A, const InputSection *B) {
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000172 bool BeginA = isCrtbegin(A->File->getName());
173 bool BeginB = isCrtbegin(B->File->getName());
Rui Ueyama5af83682016-02-11 23:41:38 +0000174 if (BeginA != BeginB)
175 return BeginA;
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000176 bool EndA = isCrtend(A->File->getName());
177 bool EndB = isCrtend(B->File->getName());
Rui Ueyama5af83682016-02-11 23:41:38 +0000178 if (EndA != EndB)
179 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000180 StringRef X = A->Name;
181 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000182 assert(X.startswith(".ctors") || X.startswith(".dtors"));
183 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
184 X = X.substr(6);
185 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000186 if (X.empty() && Y.empty())
187 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000188 return X < Y;
189}
190
191// Sorts input sections by the special rules for .ctors and .dtors.
192// Unfortunately, the rules are different from the one for .{init,fini}_array.
193// Read the comment above.
Rafael Espindola24e6f362017-02-24 15:07:30 +0000194void OutputSection::sortCtorsDtors() {
Rafael Espindolaa2e3fee2017-02-24 13:19:33 +0000195 std::stable_sort(Sections.begin(), Sections.end(), compCtors);
Rui Ueyamac4185702016-02-10 23:20:42 +0000196}
197
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000198static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
Rafael Espindola33713982017-01-05 14:20:35 +0000199 // The ELF spec just says
200 // ----------------------------------------------------------------
201 // In the first phase, input sections that match in name, type and
202 // attribute flags should be concatenated into single sections.
203 // ----------------------------------------------------------------
204 //
205 // However, it is clear that at least some flags have to be ignored for
206 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
207 // ignored. We should not have two output .text sections just because one was
208 // in a group and another was not for example.
209 //
210 // It also seems that that wording was a late addition and didn't get the
211 // necessary scrutiny.
212 //
213 // Merging sections with different flags is expected by some users. One
214 // reason is that if one file has
215 //
216 // int *const bar __attribute__((section(".foo"))) = (int *)0;
217 //
218 // gcc with -fPIC will produce a read only .foo section. But if another
219 // file has
220 //
221 // int zed;
222 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
223 //
224 // gcc with -fPIC will produce a read write section.
225 //
226 // Last but not least, when using linker script the merge rules are forced by
227 // the script. Unfortunately, linker scripts are name based. This means that
228 // expressions like *(.foo*) can refer to multiple input sections with
229 // different flags. We cannot put them in different output sections or we
230 // would produce wrong results for
231 //
232 // start = .; *(.foo.*) end = .; *(.bar)
233 //
234 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
235 // another. The problem is that there is no way to layout those output
236 // sections such that the .foo sections are the only thing between the start
237 // and end symbols.
238 //
239 // Given the above issues, we instead merge sections by name and error on
240 // incompatible types and flags.
Rafael Espindola17c35832016-09-13 12:25:30 +0000241
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000242 uint32_t Alignment = 0;
George Rimar09268b72017-03-14 09:25:03 +0000243 uint64_t Flags = 0;
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000244 if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
George Rimar09268b72017-03-14 09:25:03 +0000245 Alignment = std::max<uint64_t>(C->Alignment, C->Entsize);
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000246 Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
247 }
Rafael Espindola17c35832016-09-13 12:25:30 +0000248
Rafael Espindola72447082017-01-05 14:35:41 +0000249 return SectionKey{OutsecName, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +0000250}
251
Rui Ueyama02a036f2017-02-27 02:31:48 +0000252OutputSectionFactory::OutputSectionFactory(
Rafael Espindola24e6f362017-02-24 15:07:30 +0000253 std::vector<OutputSection *> &OutputSections)
Rafael Espindola82902742017-02-16 17:32:26 +0000254 : OutputSections(OutputSections) {}
George Rimar6892afa2016-07-12 09:49:43 +0000255
Rafael Espindola33713982017-01-05 14:20:35 +0000256static uint64_t getIncompatibleFlags(uint64_t Flags) {
257 return Flags & (SHF_ALLOC | SHF_TLS);
258}
259
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000260// We allow sections of types listed below to merged into a
261// single progbits section. This is typically done by linker
262// scripts. Merging nobits and progbits will force disk space
263// to be allocated for nobits sections. Other ones don't require
264// any special treatment on top of progbits, so there doesn't
265// seem to be a harm in merging them.
266static bool canMergeToProgbits(unsigned Type) {
267 return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
268 Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
269 Type == SHT_NOTE;
270}
271
Rafael Espindolae39709b2017-05-30 20:40:03 +0000272void elf::reportDiscarded(InputSectionBase *IS) {
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000273 if (!Config->PrintGcSections)
274 return;
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000275 message("removing unused section from '" + IS->Name + "' in file '" +
George Rimarf08b5922017-03-14 09:19:34 +0000276 IS->File->getName());
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000277}
278
Rui Ueyama02a036f2017-02-27 02:31:48 +0000279void OutputSectionFactory::addInputSec(InputSectionBase *IS,
280 StringRef OutsecName) {
George Rimar8cde9a72017-06-06 06:38:32 +0000281 // Sections with the SHT_GROUP attribute reach here only when the - r option
282 // is given. Such sections define "section groups", and InputFiles.cpp has
283 // dedup'ed section groups by their signatures. For the -r, we want to pass
284 // through all SHT_GROUP sections without merging them because merging them
285 // creates broken section contents.
286 if (IS->Type == SHT_GROUP) {
287 OutputSection *Out = nullptr;
288 addInputSec(IS, OutsecName, Out);
289 return;
290 }
291
George Rimar990c9cb2017-06-07 09:20:35 +0000292 // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
293 // relocation sections .rela.foo and .rela.bar for example. Most tools do
294 // not allow multiple REL[A] sections for output section. Hence we
295 // should combine these relocation sections into single output.
296 // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
297 // other REL[A] sections created by linker itself.
298 if (!isa<SyntheticSection>(IS) &&
299 (IS->Type == SHT_REL || IS->Type == SHT_RELA)) {
300 auto *Sec = cast<InputSection>(IS);
301 OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
302 addInputSec(IS, OutsecName, Out->RelocationSection);
303 return;
304 }
305
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000306 SectionKey Key = createKey(IS, OutsecName);
307 OutputSection *&Sec = Map[Key];
308 return addInputSec(IS, OutsecName, Sec);
309}
310
311void OutputSectionFactory::addInputSec(InputSectionBase *IS,
312 StringRef OutsecName,
313 OutputSection *&Sec) {
Rafael Espindola82902742017-02-16 17:32:26 +0000314 if (!IS->Live) {
George Rimarf08b5922017-03-14 09:19:34 +0000315 reportDiscarded(IS);
Rafael Espindola82902742017-02-16 17:32:26 +0000316 return;
317 }
318
Rafael Espindola10897f12016-09-13 14:23:14 +0000319 if (Sec) {
Rafael Espindola82902742017-02-16 17:32:26 +0000320 if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
Rui Ueyama577168e2017-04-25 16:00:44 +0000321 error("incompatible section flags for " + Sec->Name +
322 "\n>>> " + toString(IS) + ": 0x" + utohexstr(IS->Flags) +
323 "\n>>> output section " + Sec->Name + ": 0x" +
324 utohexstr(Sec->Flags));
Rafael Espindola82902742017-02-16 17:32:26 +0000325 if (Sec->Type != IS->Type) {
326 if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000327 Sec->Type = SHT_PROGBITS;
328 else
Rui Ueyama896cbc42017-05-10 16:57:50 +0000329 error("section type mismatch for " + IS->Name +
330 "\n>>> " + toString(IS) + ": " +
331 getELFSectionTypeName(Config->EMachine, IS->Type) +
332 "\n>>> output section " + Sec->Name + ": " +
333 getELFSectionTypeName(Config->EMachine, Sec->Type));
Eugene Leviant0c0789b2017-01-31 10:26:52 +0000334 }
Rui Ueyama1c837b52017-06-10 00:38:55 +0000335 Sec->Flags |= IS->Flags;
Rafael Espindola82902742017-02-16 17:32:26 +0000336 } else {
Rui Ueyama1c837b52017-06-10 00:38:55 +0000337 Sec = make<OutputSection>(OutsecName, IS->Type, IS->Flags);
Rafael Espindola82902742017-02-16 17:32:26 +0000338 OutputSections.push_back(Sec);
Rafael Espindola10897f12016-09-13 14:23:14 +0000339 }
340
Rafael Espindoladc8eb812017-04-07 01:40:21 +0000341 Sec->addSection(cast<InputSection>(IS));
George Rimar6892afa2016-07-12 09:49:43 +0000342}
343
Rui Ueyama02a036f2017-02-27 02:31:48 +0000344OutputSectionFactory::~OutputSectionFactory() {}
Rafael Espindola82902742017-02-16 17:32:26 +0000345
Rafael Espindola72447082017-01-05 14:35:41 +0000346SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
347 return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000348}
349
Rafael Espindola72447082017-01-05 14:35:41 +0000350SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
351 return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
George Rimar6892afa2016-07-12 09:49:43 +0000352}
353
Rafael Espindola72447082017-01-05 14:35:41 +0000354unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
Rafael Espindola33713982017-01-05 14:20:35 +0000355 return hash_combine(Val.Name, Val.Flags, Val.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +0000356}
357
Rafael Espindola72447082017-01-05 14:35:41 +0000358bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
359 const SectionKey &RHS) {
George Rimar6892afa2016-07-12 09:49:43 +0000360 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
Rafael Espindola33713982017-01-05 14:20:35 +0000361 LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000362}
363
George Rimar78aa2702017-03-13 14:40:58 +0000364uint64_t elf::getHeaderSize() {
365 if (Config->OFormatBinary)
366 return 0;
367 return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
368}
369
Rafael Espindola24e6f362017-02-24 15:07:30 +0000370template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
371template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
372template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
373template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);