1 //===- 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"
12 #include "LinkerScript.h"
13 #include "Memory.h"
14 #include "Strings.h"
15 #include "SymbolTable.h"
16 #include "SyntheticSections.h"
17 #include "Target.h"
18 #include "Threads.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/MD5.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/SHA1.h"
23 
24 using namespace llvm;
25 using namespace llvm::dwarf;
26 using namespace llvm::object;
27 using namespace llvm::support::endian;
28 using namespace llvm::ELF;
29 
30 using namespace lld;
31 using namespace lld::elf;
32 
33 uint8_t Out::First;
34 OutputSection *Out::Opd;
35 uint8_t *Out::OpdBuf;
36 PhdrEntry *Out::TlsPhdr;
37 OutputSection *Out::DebugInfo;
38 OutputSection *Out::ElfHeader;
39 OutputSection *Out::ProgramHeaders;
40 OutputSection *Out::PreinitArray;
41 OutputSection *Out::InitArray;
42 OutputSection *Out::FiniArray;
43 
44 uint32_t OutputSection::getPhdrFlags() const {
45   uint32_t Ret = PF_R;
46   if (Flags & SHF_WRITE)
47     Ret |= PF_W;
48   if (Flags & SHF_EXECINSTR)
49     Ret |= PF_X;
50   return Ret;
51 }
52 
53 template <class ELFT>
54 void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
55   Shdr->sh_entsize = Entsize;
56   Shdr->sh_addralign = Alignment;
57   Shdr->sh_type = Type;
58   Shdr->sh_offset = Offset;
59   Shdr->sh_flags = Flags;
60   Shdr->sh_info = Info;
61   Shdr->sh_link = Link;
62   Shdr->sh_addr = Addr;
63   Shdr->sh_size = Size;
64   Shdr->sh_name = ShName;
65 }
66 
67 OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
68     : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
69                   /*Info*/ 0,
70                   /*Link*/ 0) {}
71 
72 static bool compareByFilePosition(InputSection *A, InputSection *B) {
73   // Synthetic doesn't have link order dependecy, stable_sort will keep it last
74   if (A->kind() == InputSectionBase::Synthetic ||
75       B->kind() == InputSectionBase::Synthetic)
76     return false;
77   auto *LA = cast<InputSection>(A->getLinkOrderDep());
78   auto *LB = cast<InputSection>(B->getLinkOrderDep());
79   OutputSection *AOut = LA->OutSec;
80   OutputSection *BOut = LB->OutSec;
81   if (AOut != BOut)
82     return AOut->SectionIndex < BOut->SectionIndex;
83   return LA->OutSecOff < LB->OutSecOff;
84 }
85 
86 template <class ELFT> void OutputSection::finalize() {
87   if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
88     std::sort(Sections.begin(), Sections.end(), compareByFilePosition);
89     assignOffsets();
90 
91     // We must preserve the link order dependency of sections with the
92     // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
93     // need to translate the InputSection sh_link to the OutputSection sh_link,
94     // all InputSections in the OutputSection have the same dependency.
95     if (auto *D = this->Sections.front()->getLinkOrderDep())
96       this->Link = D->OutSec->SectionIndex;
97   }
98 
99   uint32_t Type = this->Type;
100   if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL))
101     return;
102 
103   InputSection *First = Sections[0];
104   if (isa<SyntheticSection>(First))
105     return;
106 
107   this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
108   // sh_info for SHT_REL[A] sections should contain the section header index of
109   // the section to which the relocation applies.
110   InputSectionBase *S = First->getRelocatedSection();
111   this->Info = S->OutSec->SectionIndex;
112 }
113 
114 void OutputSection::addSection(InputSection *S) {
115   assert(S->Live);
116   Sections.push_back(S);
117   S->OutSec = this;
118   this->updateAlignment(S->Alignment);
119 
120   // If this section contains a table of fixed-size entries, sh_entsize
121   // holds the element size. Consequently, if this contains two or more
122   // input sections, all of them must have the same sh_entsize. However,
123   // you can put different types of input sections into one output
124   // sectin by using linker scripts. I don't know what to do here.
125   // Probably we sholuld handle that as an error. But for now we just
126   // pick the largest sh_entsize.
127   this->Entsize = std::max(this->Entsize, S->Entsize);
128 }
129 
130 // This function is called after we sort input sections
131 // and scan relocations to setup sections' offsets.
132 void OutputSection::assignOffsets() {
133   uint64_t Off = 0;
134   for (InputSection *S : Sections) {
135     Off = alignTo(Off, S->Alignment);
136     S->OutSecOff = Off;
137     Off += S->getSize();
138   }
139   this->Size = Off;
140 }
141 
142 void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
143   typedef std::pair<unsigned, InputSection *> Pair;
144   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
145 
146   std::vector<Pair> V;
147   for (InputSection *S : Sections)
148     V.push_back({Order(S), S});
149   std::stable_sort(V.begin(), V.end(), Comp);
150   Sections.clear();
151   for (Pair &P : V)
152     Sections.push_back(P.second);
153 }
154 
155 // Sorts input sections by section name suffixes, so that .foo.N comes
156 // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
157 // We want to keep the original order if the priorities are the same
158 // because the compiler keeps the original initialization order in a
159 // translation unit and we need to respect that.
160 // For more detail, read the section of the GCC's manual about init_priority.
161 void OutputSection::sortInitFini() {
162   // Sort sections by priority.
163   sort([](InputSectionBase *S) { return getPriority(S->Name); });
164 }
165 
166 // Returns true if S matches /Filename.?\.o$/.
167 static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
168   if (!S.endswith(".o"))
169     return false;
170   S = S.drop_back(2);
171   if (S.endswith(Filename))
172     return true;
173   return !S.empty() && S.drop_back().endswith(Filename);
174 }
175 
176 static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
177 static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
178 
179 // .ctors and .dtors are sorted by this priority from highest to lowest.
180 //
181 //  1. The section was contained in crtbegin (crtbegin contains
182 //     some sentinel value in its .ctors and .dtors so that the runtime
183 //     can find the beginning of the sections.)
184 //
185 //  2. The section has an optional priority value in the form of ".ctors.N"
186 //     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
187 //     they are compared as string rather than number.
188 //
189 //  3. The section is just ".ctors" or ".dtors".
190 //
191 //  4. The section was contained in crtend, which contains an end marker.
192 //
193 // In an ideal world, we don't need this function because .init_array and
194 // .ctors are duplicate features (and .init_array is newer.) However, there
195 // are too many real-world use cases of .ctors, so we had no choice to
196 // support that with this rather ad-hoc semantics.
197 static bool compCtors(const InputSection *A, const InputSection *B) {
198   bool BeginA = isCrtbegin(A->File->getName());
199   bool BeginB = isCrtbegin(B->File->getName());
200   if (BeginA != BeginB)
201     return BeginA;
202   bool EndA = isCrtend(A->File->getName());
203   bool EndB = isCrtend(B->File->getName());
204   if (EndA != EndB)
205     return EndB;
206   StringRef X = A->Name;
207   StringRef Y = B->Name;
208   assert(X.startswith(".ctors") || X.startswith(".dtors"));
209   assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
210   X = X.substr(6);
211   Y = Y.substr(6);
212   if (X.empty() && Y.empty())
213     return false;
214   return X < Y;
215 }
216 
217 // Sorts input sections by the special rules for .ctors and .dtors.
218 // Unfortunately, the rules are different from the one for .{init,fini}_array.
219 // Read the comment above.
220 void OutputSection::sortCtorsDtors() {
221   std::stable_sort(Sections.begin(), Sections.end(), compCtors);
222 }
223 
224 // Fill [Buf, Buf + Size) with Filler.
225 // This is used for linker script "=fillexp" command.
226 static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
227   size_t I = 0;
228   for (; I + 4 < Size; I += 4)
229     memcpy(Buf + I, &Filler, 4);
230   memcpy(Buf + I, &Filler, Size - I);
231 }
232 
233 uint32_t OutputSection::getFiller() {
234   // Determine what to fill gaps between InputSections with, as specified by the
235   // linker script. If nothing is specified and this is an executable section,
236   // fall back to trap instructions to prevent bad diassembly and detect invalid
237   // jumps to padding.
238   if (Optional<uint32_t> Filler = Script->getFiller(Name))
239     return *Filler;
240   if (Flags & SHF_EXECINSTR)
241     return Target->TrapInstr;
242   return 0;
243 }
244 
245 template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
246   Loc = Buf;
247 
248   // Write leading padding.
249   uint32_t Filler = getFiller();
250   if (Filler)
251     fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler);
252 
253   parallelFor(0, Sections.size(), [=](size_t I) {
254     InputSection *Sec = Sections[I];
255     Sec->writeTo<ELFT>(Buf);
256 
257     // Fill gaps between sections.
258     if (Filler) {
259       uint8_t *Start = Buf + Sec->OutSecOff + Sec->getSize();
260       uint8_t *End;
261       if (I + 1 == Sections.size())
262         End = Buf + Size;
263       else
264         End = Buf + Sections[I + 1]->OutSecOff;
265       fill(Start, End - Start, Filler);
266     }
267   });
268 
269   // Linker scripts may have BYTE()-family commands with which you
270   // can write arbitrary bytes to the output. Process them if any.
271   Script->writeDataBytes(Name, Buf);
272 }
273 
274 static uint64_t getOutFlags(InputSectionBase *S) {
275   return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
276 }
277 
278 static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
279   //  The ELF spec just says
280   // ----------------------------------------------------------------
281   // In the first phase, input sections that match in name, type and
282   // attribute flags should be concatenated into single sections.
283   // ----------------------------------------------------------------
284   //
285   // However, it is clear that at least some flags have to be ignored for
286   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
287   // ignored. We should not have two output .text sections just because one was
288   // in a group and another was not for example.
289   //
290   // It also seems that that wording was a late addition and didn't get the
291   // necessary scrutiny.
292   //
293   // Merging sections with different flags is expected by some users. One
294   // reason is that if one file has
295   //
296   // int *const bar __attribute__((section(".foo"))) = (int *)0;
297   //
298   // gcc with -fPIC will produce a read only .foo section. But if another
299   // file has
300   //
301   // int zed;
302   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
303   //
304   // gcc with -fPIC will produce a read write section.
305   //
306   // Last but not least, when using linker script the merge rules are forced by
307   // the script. Unfortunately, linker scripts are name based. This means that
308   // expressions like *(.foo*) can refer to multiple input sections with
309   // different flags. We cannot put them in different output sections or we
310   // would produce wrong results for
311   //
312   // start = .; *(.foo.*) end = .; *(.bar)
313   //
314   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
315   // another. The problem is that there is no way to layout those output
316   // sections such that the .foo sections are the only thing between the start
317   // and end symbols.
318   //
319   // Given the above issues, we instead merge sections by name and error on
320   // incompatible types and flags.
321 
322   uint32_t Alignment = 0;
323   uint64_t Flags = 0;
324   if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
325     Alignment = std::max<uint64_t>(C->Alignment, C->Entsize);
326     Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
327   }
328 
329   return SectionKey{OutsecName, Flags, Alignment};
330 }
331 
332 OutputSectionFactory::OutputSectionFactory(
333     std::vector<OutputSection *> &OutputSections)
334     : OutputSections(OutputSections) {}
335 
336 static uint64_t getIncompatibleFlags(uint64_t Flags) {
337   return Flags & (SHF_ALLOC | SHF_TLS);
338 }
339 
340 // We allow sections of types listed below to merged into a
341 // single progbits section. This is typically done by linker
342 // scripts. Merging nobits and progbits will force disk space
343 // to be allocated for nobits sections. Other ones don't require
344 // any special treatment on top of progbits, so there doesn't
345 // seem to be a harm in merging them.
346 static bool canMergeToProgbits(unsigned Type) {
347   return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
348          Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
349          Type == SHT_NOTE;
350 }
351 
352 static void reportDiscarded(InputSectionBase *IS) {
353   if (!Config->PrintGcSections)
354     return;
355   message("removing unused section from '" + IS->Name + "' in file '" +
356           IS->File->getName());
357 }
358 
359 void OutputSectionFactory::addInputSec(InputSectionBase *IS,
360                                        StringRef OutsecName) {
361   if (!IS->Live) {
362     reportDiscarded(IS);
363     return;
364   }
365 
366   SectionKey Key = createKey(IS, OutsecName);
367   uint64_t Flags = getOutFlags(IS);
368   OutputSection *&Sec = Map[Key];
369   if (Sec) {
370     if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
371       error("Section has flags incompatible with others with the same name " +
372             toString(IS));
373     if (Sec->Type != IS->Type) {
374       if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
375         Sec->Type = SHT_PROGBITS;
376       else
377         error("Section has different type from others with the same name " +
378               toString(IS));
379     }
380     Sec->Flags |= Flags;
381   } else {
382     Sec = make<OutputSection>(Key.Name, IS->Type, Flags);
383     OutputSections.push_back(Sec);
384   }
385 
386   Sec->addSection(cast<InputSection>(IS));
387 }
388 
389 OutputSectionFactory::~OutputSectionFactory() {}
390 
391 SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
392   return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
393 }
394 
395 SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
396   return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
397 }
398 
399 unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
400   return hash_combine(Val.Name, Val.Flags, Val.Alignment);
401 }
402 
403 bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
404                                        const SectionKey &RHS) {
405   return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
406          LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
407 }
408 
409 uint64_t elf::getHeaderSize() {
410   if (Config->OFormatBinary)
411     return 0;
412   return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
413 }
414 
415 template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
416 template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
417 template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
418 template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
419 
420 template void OutputSection::finalize<ELF32LE>();
421 template void OutputSection::finalize<ELF32BE>();
422 template void OutputSection::finalize<ELF64LE>();
423 template void OutputSection::finalize<ELF64BE>();
424 
425 template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
426 template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
427 template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
428 template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
429