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 OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
34                                      uint64_t Flags)
35     : Name(Name) {
36   this->Type = Type;
37   this->Flags = Flags;
38   this->Addralign = 1;
39 }
40 
41 uint32_t OutputSectionBase::getPhdrFlags() const {
42   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 
50 template <class ELFT>
51 void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
52   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;
59   Shdr->sh_addr = Addr;
60   Shdr->sh_size = Size;
61   Shdr->sh_name = ShName;
62 }
63 
64 template <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 
81 template <class ELFT>
82 OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
83     : OutputSectionBase(Name, Type, Flags) {
84   this->Entsize = getEntsize<ELFT>(Type);
85 }
86 
87 template <typename ELFT>
88 static bool compareByFilePosition(InputSection *A, InputSection *B) {
89   // Synthetic doesn't have link order dependecy, stable_sort will keep it last
90   if (A->kind() == InputSectionBase::Synthetic ||
91       B->kind() == InputSectionBase::Synthetic)
92     return false;
93   auto *LA = cast<InputSection>(A->template getLinkOrderDep<ELFT>());
94   auto *LB = cast<InputSection>(B->template getLinkOrderDep<ELFT>());
95   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 
102 template <class ELFT> void OutputSection<ELFT>::finalize() {
103   if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
104     std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>);
105     Size = 0;
106     assignOffsets();
107 
108     // 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.
112     if (auto *D = this->Sections.front()->template getLinkOrderDep<ELFT>())
113       this->Link = D->OutSec->SectionIndex;
114   }
115 
116   uint32_t Type = this->Type;
117   if (!Config->copyRelocs() || (Type != SHT_RELA && Type != SHT_REL))
118     return;
119 
120   InputSection *First = Sections[0];
121   if (isa<SyntheticSection<ELFT>>(First))
122     return;
123 
124   this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
125   // sh_info for SHT_REL[A] sections should contain the section header index of
126   // the section to which the relocation applies.
127   InputSectionBase *S = First->getRelocatedSection<ELFT>();
128   this->Info = S->OutSec->SectionIndex;
129 }
130 
131 template <class ELFT>
132 void OutputSection<ELFT>::addSection(InputSectionBase *C) {
133   assert(C->Live);
134   auto *S = cast<InputSection>(C);
135   Sections.push_back(S);
136   S->OutSec = this;
137   this->updateAlignment(S->Alignment);
138   // 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.
140   if (Config->Relocatable && (S->Flags & SHF_MERGE))
141     this->Entsize = S->Entsize;
142 }
143 
144 template <class ELFT>
145 void OutputSection<ELFT>::forEachInputSection(
146     std::function<void(InputSectionBase *)> F) {
147   for (InputSection *S : Sections)
148     F(S);
149 }
150 
151 // This function is called after we sort input sections
152 // and scan relocations to setup sections' offsets.
153 template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
154   uintX_t Off = this->Size;
155   for (InputSection *S : Sections) {
156     Off = alignTo(Off, S->Alignment);
157     S->OutSecOff = Off;
158     Off += S->template getSize<ELFT>();
159   }
160   this->Size = Off;
161 }
162 
163 template <class ELFT>
164 void OutputSection<ELFT>::sort(std::function<int(InputSectionBase *S)> Order) {
165   typedef std::pair<unsigned, InputSection *> Pair;
166   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
167 
168   std::vector<Pair> V;
169   for (InputSection *S : Sections)
170     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 
177 // 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.
179 // 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.
182 // For more detail, read the section of the GCC's manual about init_priority.
183 template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
184   // Sort sections by priority.
185   sort([](InputSectionBase *S) { return getPriority(S->Name); });
186 }
187 
188 // Returns true if S matches /Filename.?\.o$/.
189 static 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 
198 static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
199 static 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.
219 template <class ELFT>
220 static bool compCtors(const InputSection *A, const InputSection *B) {
221   bool BeginA = isCrtbegin(A->template getFile<ELFT>()->getName());
222   bool BeginB = isCrtbegin(B->template getFile<ELFT>()->getName());
223   if (BeginA != BeginB)
224     return BeginA;
225   bool EndA = isCrtend(A->template getFile<ELFT>()->getName());
226   bool EndB = isCrtend(B->template getFile<ELFT>()->getName());
227   if (EndA != EndB)
228     return EndB;
229   StringRef X = A->Name;
230   StringRef Y = B->Name;
231   assert(X.startswith(".ctors") || X.startswith(".dtors"));
232   assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
233   X = X.substr(6);
234   Y = Y.substr(6);
235   if (X.empty() && Y.empty())
236     return false;
237   return X < Y;
238 }
239 
240 // Sorts input sections by the special rules for .ctors and .dtors.
241 // Unfortunately, the rules are different from the one for .{init,fini}_array.
242 // Read the comment above.
243 template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
244   std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
245 }
246 
247 // Fill [Buf, Buf + Size) with Filler. Filler is written in big
248 // endian order. This is used for linker script "=fillexp" command.
249 void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
250   uint8_t V[4];
251   write32be(V, Filler);
252   size_t I = 0;
253   for (; I + 4 < Size; I += 4)
254     memcpy(Buf + I, V, 4);
255   memcpy(Buf + I, V, Size - I);
256 }
257 
258 template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
259   Loc = Buf;
260   if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
261     fill(Buf, this->Size, Filler);
262 
263   auto Fn = [=](InputSection *IS) { IS->writeTo<ELFT>(Buf); };
264   forEach(Sections.begin(), Sections.end(), Fn);
265 
266   // Linker scripts may have BYTE()-family commands with which you
267   // can write arbitrary bytes to the output. Process them if any.
268   Script<ELFT>::X->writeDataBytes(this->Name, Buf);
269 }
270 
271 template <class ELFT>
272 static typename ELFT::uint getOutFlags(InputSectionBase *S) {
273   return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
274 }
275 
276 template <class ELFT>
277 static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
278   //  The ELF spec just says
279   // ----------------------------------------------------------------
280   // In the first phase, input sections that match in name, type and
281   // attribute flags should be concatenated into single sections.
282   // ----------------------------------------------------------------
283   //
284   // However, it is clear that at least some flags have to be ignored for
285   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
286   // ignored. We should not have two output .text sections just because one was
287   // in a group and another was not for example.
288   //
289   // It also seems that that wording was a late addition and didn't get the
290   // necessary scrutiny.
291   //
292   // Merging sections with different flags is expected by some users. One
293   // reason is that if one file has
294   //
295   // int *const bar __attribute__((section(".foo"))) = (int *)0;
296   //
297   // gcc with -fPIC will produce a read only .foo section. But if another
298   // file has
299   //
300   // int zed;
301   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
302   //
303   // gcc with -fPIC will produce a read write section.
304   //
305   // Last but not least, when using linker script the merge rules are forced by
306   // the script. Unfortunately, linker scripts are name based. This means that
307   // expressions like *(.foo*) can refer to multiple input sections with
308   // different flags. We cannot put them in different output sections or we
309   // would produce wrong results for
310   //
311   // start = .; *(.foo.*) end = .; *(.bar)
312   //
313   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
314   // another. The problem is that there is no way to layout those output
315   // sections such that the .foo sections are the only thing between the start
316   // and end symbols.
317   //
318   // Given the above issues, we instead merge sections by name and error on
319   // incompatible types and flags.
320 
321   typedef typename ELFT::uint uintX_t;
322 
323   uintX_t Alignment = 0;
324   uintX_t Flags = 0;
325   if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
326     Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
327     Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
328   }
329 
330   return SectionKey{OutsecName, Flags, Alignment};
331 }
332 
333 template <class ELFT>
334 OutputSectionFactory<ELFT>::OutputSectionFactory(
335     std::vector<OutputSectionBase *> &OutputSections)
336     : OutputSections(OutputSections) {}
337 
338 static uint64_t getIncompatibleFlags(uint64_t Flags) {
339   return Flags & (SHF_ALLOC | SHF_TLS);
340 }
341 
342 // We allow sections of types listed below to merged into a
343 // single progbits section. This is typically done by linker
344 // scripts. Merging nobits and progbits will force disk space
345 // to be allocated for nobits sections. Other ones don't require
346 // any special treatment on top of progbits, so there doesn't
347 // seem to be a harm in merging them.
348 static bool canMergeToProgbits(unsigned Type) {
349   return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
350          Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
351          Type == SHT_NOTE;
352 }
353 
354 template <class ELFT> static void reportDiscarded(InputSectionBase *IS) {
355   if (!Config->PrintGcSections)
356     return;
357   message("removing unused section from '" + IS->Name + "' in file '" +
358           IS->getFile<ELFT>()->getName());
359 }
360 
361 template <class ELFT>
362 void OutputSectionFactory<ELFT>::addInputSec(InputSectionBase *IS,
363                                              StringRef OutsecName) {
364   if (!IS->Live) {
365     reportDiscarded<ELFT>(IS);
366     return;
367   }
368 
369   SectionKey Key = createKey<ELFT>(IS, OutsecName);
370   uintX_t Flags = getOutFlags<ELFT>(IS);
371   OutputSectionBase *&Sec = Map[Key];
372   if (Sec) {
373     if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
374       error("Section has flags incompatible with others with the same name " +
375             toString(IS));
376     if (Sec->Type != IS->Type) {
377       if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
378         Sec->Type = SHT_PROGBITS;
379       else
380         error("Section has different type from others with the same name " +
381               toString(IS));
382     }
383     Sec->Flags |= Flags;
384   } else {
385     uint32_t Type = IS->Type;
386     if (IS->kind() == InputSectionBase::EHFrame) {
387       In<ELFT>::EhFrame->addSection(IS);
388       return;
389     }
390     Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
391     OutputSections.push_back(Sec);
392   }
393 
394   Sec->addSection(IS);
395 }
396 
397 template <class ELFT> OutputSectionFactory<ELFT>::~OutputSectionFactory() {}
398 
399 SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
400   return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
401 }
402 
403 SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
404   return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
405 }
406 
407 unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
408   return hash_combine(Val.Name, Val.Flags, Val.Alignment);
409 }
410 
411 bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
412                                        const SectionKey &RHS) {
413   return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
414          LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
415 }
416 
417 namespace lld {
418 namespace elf {
419 
420 template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
421 template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
422 template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
423 template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
424 
425 template class OutputSection<ELF32LE>;
426 template class OutputSection<ELF32BE>;
427 template class OutputSection<ELF64LE>;
428 template class OutputSection<ELF64BE>;
429 
430 template class OutputSectionFactory<ELF32LE>;
431 template class OutputSectionFactory<ELF32BE>;
432 template class OutputSectionFactory<ELF64LE>;
433 template class OutputSectionFactory<ELF64BE>;
434 }
435 }
436