xref: /openbsd/gnu/llvm/lld/ELF/OutputSections.cpp (revision 667950d7)
1ece8a530Spatrick //===- OutputSections.cpp -------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick 
9ece8a530Spatrick #include "OutputSections.h"
10ece8a530Spatrick #include "Config.h"
11ece8a530Spatrick #include "LinkerScript.h"
12ece8a530Spatrick #include "SymbolTable.h"
13ece8a530Spatrick #include "SyntheticSections.h"
14ece8a530Spatrick #include "Target.h"
15ece8a530Spatrick #include "lld/Common/Memory.h"
16ece8a530Spatrick #include "lld/Common/Strings.h"
17ece8a530Spatrick #include "lld/Common/Threads.h"
18ece8a530Spatrick #include "llvm/BinaryFormat/Dwarf.h"
19ece8a530Spatrick #include "llvm/Support/Compression.h"
20ece8a530Spatrick #include "llvm/Support/MD5.h"
21ece8a530Spatrick #include "llvm/Support/MathExtras.h"
22ece8a530Spatrick #include "llvm/Support/SHA1.h"
23ece8a530Spatrick #include <regex>
24ece8a530Spatrick 
25ece8a530Spatrick using namespace llvm;
26ece8a530Spatrick using namespace llvm::dwarf;
27ece8a530Spatrick using namespace llvm::object;
28ece8a530Spatrick using namespace llvm::support::endian;
29ece8a530Spatrick using namespace llvm::ELF;
30ece8a530Spatrick 
31ece8a530Spatrick namespace lld {
32ece8a530Spatrick namespace elf {
33ece8a530Spatrick uint8_t *Out::bufferStart;
34ece8a530Spatrick uint8_t Out::first;
35ece8a530Spatrick PhdrEntry *Out::tlsPhdr;
36ece8a530Spatrick OutputSection *Out::elfHeader;
37ece8a530Spatrick OutputSection *Out::programHeaders;
38ece8a530Spatrick OutputSection *Out::preinitArray;
39ece8a530Spatrick OutputSection *Out::initArray;
40ece8a530Spatrick OutputSection *Out::finiArray;
41ece8a530Spatrick 
42ece8a530Spatrick std::vector<OutputSection *> outputSections;
43ece8a530Spatrick 
44ece8a530Spatrick uint32_t OutputSection::getPhdrFlags() const {
45ece8a530Spatrick   uint32_t ret = 0;
46ece8a530Spatrick   if (config->emachine != EM_ARM || !(flags & SHF_ARM_PURECODE))
47ece8a530Spatrick     ret |= PF_R;
48ece8a530Spatrick   if (flags & SHF_WRITE)
49ece8a530Spatrick     ret |= PF_W;
50ece8a530Spatrick   if (flags & SHF_EXECINSTR)
51ece8a530Spatrick     ret |= PF_X;
52ece8a530Spatrick   return ret;
53ece8a530Spatrick }
54ece8a530Spatrick 
55ece8a530Spatrick template <class ELFT>
56ece8a530Spatrick void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
57ece8a530Spatrick   shdr->sh_entsize = entsize;
58ece8a530Spatrick   shdr->sh_addralign = alignment;
59ece8a530Spatrick   shdr->sh_type = type;
60ece8a530Spatrick   shdr->sh_offset = offset;
61ece8a530Spatrick   shdr->sh_flags = flags;
62ece8a530Spatrick   shdr->sh_info = info;
63ece8a530Spatrick   shdr->sh_link = link;
64ece8a530Spatrick   shdr->sh_addr = addr;
65ece8a530Spatrick   shdr->sh_size = size;
66ece8a530Spatrick   shdr->sh_name = shName;
67ece8a530Spatrick }
68ece8a530Spatrick 
69ece8a530Spatrick OutputSection::OutputSection(StringRef name, uint32_t type, uint64_t flags)
70ece8a530Spatrick     : BaseCommand(OutputSectionKind),
71ece8a530Spatrick       SectionBase(Output, name, flags, /*Entsize*/ 0, /*Alignment*/ 1, type,
72ece8a530Spatrick                   /*Info*/ 0, /*Link*/ 0) {}
73ece8a530Spatrick 
74ece8a530Spatrick // We allow sections of types listed below to merged into a
75ece8a530Spatrick // single progbits section. This is typically done by linker
76ece8a530Spatrick // scripts. Merging nobits and progbits will force disk space
77ece8a530Spatrick // to be allocated for nobits sections. Other ones don't require
78ece8a530Spatrick // any special treatment on top of progbits, so there doesn't
79ece8a530Spatrick // seem to be a harm in merging them.
80ece8a530Spatrick static bool canMergeToProgbits(unsigned type) {
81ece8a530Spatrick   return type == SHT_NOBITS || type == SHT_PROGBITS || type == SHT_INIT_ARRAY ||
82ece8a530Spatrick          type == SHT_PREINIT_ARRAY || type == SHT_FINI_ARRAY ||
83ece8a530Spatrick          type == SHT_NOTE;
84ece8a530Spatrick }
85ece8a530Spatrick 
86ece8a530Spatrick // Record that isec will be placed in the OutputSection. isec does not become
87ece8a530Spatrick // permanent until finalizeInputSections() is called. The function should not be
88ece8a530Spatrick // used after finalizeInputSections() is called. If you need to add an
89ece8a530Spatrick // InputSection post finalizeInputSections(), then you must do the following:
90ece8a530Spatrick //
91ece8a530Spatrick // 1. Find or create an InputSectionDescription to hold InputSection.
92ece8a530Spatrick // 2. Add the InputSection to the InputSectionDescription::sections.
93ece8a530Spatrick // 3. Call commitSection(isec).
94ece8a530Spatrick void OutputSection::recordSection(InputSectionBase *isec) {
95ece8a530Spatrick   partition = isec->partition;
96ece8a530Spatrick   isec->parent = this;
97ece8a530Spatrick   if (sectionCommands.empty() ||
98ece8a530Spatrick       !isa<InputSectionDescription>(sectionCommands.back()))
99ece8a530Spatrick     sectionCommands.push_back(make<InputSectionDescription>(""));
100ece8a530Spatrick   auto *isd = cast<InputSectionDescription>(sectionCommands.back());
101ece8a530Spatrick   isd->sectionBases.push_back(isec);
102ece8a530Spatrick }
103ece8a530Spatrick 
104ece8a530Spatrick // Update fields (type, flags, alignment, etc) according to the InputSection
105ece8a530Spatrick // isec. Also check whether the InputSection flags and type are consistent with
106ece8a530Spatrick // other InputSections.
107ece8a530Spatrick void OutputSection::commitSection(InputSection *isec) {
108ece8a530Spatrick   if (!hasInputSections) {
109ece8a530Spatrick     // If IS is the first section to be added to this section,
110ece8a530Spatrick     // initialize type, entsize and flags from isec.
111ece8a530Spatrick     hasInputSections = true;
112ece8a530Spatrick     type = isec->type;
113ece8a530Spatrick     entsize = isec->entsize;
114ece8a530Spatrick     flags = isec->flags;
115ece8a530Spatrick   } else {
116ece8a530Spatrick     // Otherwise, check if new type or flags are compatible with existing ones.
117*667950d7Spatrick     if ((flags ^ isec->flags) & SHF_TLS)
118ece8a530Spatrick       error("incompatible section flags for " + name + "\n>>> " + toString(isec) +
119ece8a530Spatrick             ": 0x" + utohexstr(isec->flags) + "\n>>> output section " + name +
120ece8a530Spatrick             ": 0x" + utohexstr(flags));
121ece8a530Spatrick 
122ece8a530Spatrick     if (type != isec->type) {
123ece8a530Spatrick       if (!canMergeToProgbits(type) || !canMergeToProgbits(isec->type))
124ece8a530Spatrick         error("section type mismatch for " + isec->name + "\n>>> " +
125ece8a530Spatrick               toString(isec) + ": " +
126ece8a530Spatrick               getELFSectionTypeName(config->emachine, isec->type) +
127ece8a530Spatrick               "\n>>> output section " + name + ": " +
128ece8a530Spatrick               getELFSectionTypeName(config->emachine, type));
129ece8a530Spatrick       type = SHT_PROGBITS;
130ece8a530Spatrick     }
131ece8a530Spatrick   }
132ece8a530Spatrick   if (noload)
133ece8a530Spatrick     type = SHT_NOBITS;
134ece8a530Spatrick 
135ece8a530Spatrick   isec->parent = this;
136ece8a530Spatrick   uint64_t andMask =
137ece8a530Spatrick       config->emachine == EM_ARM ? (uint64_t)SHF_ARM_PURECODE : 0;
138ece8a530Spatrick   uint64_t orMask = ~andMask;
139ece8a530Spatrick   uint64_t andFlags = (flags & isec->flags) & andMask;
140ece8a530Spatrick   uint64_t orFlags = (flags | isec->flags) & orMask;
141ece8a530Spatrick   flags = andFlags | orFlags;
142ece8a530Spatrick   if (nonAlloc)
143ece8a530Spatrick     flags &= ~(uint64_t)SHF_ALLOC;
144ece8a530Spatrick 
145ece8a530Spatrick   alignment = std::max(alignment, isec->alignment);
146ece8a530Spatrick 
147ece8a530Spatrick   // If this section contains a table of fixed-size entries, sh_entsize
148ece8a530Spatrick   // holds the element size. If it contains elements of different size we
149ece8a530Spatrick   // set sh_entsize to 0.
150ece8a530Spatrick   if (entsize != isec->entsize)
151ece8a530Spatrick     entsize = 0;
152ece8a530Spatrick }
153ece8a530Spatrick 
154ece8a530Spatrick // This function scans over the InputSectionBase list sectionBases to create
155ece8a530Spatrick // InputSectionDescription::sections.
156ece8a530Spatrick //
157ece8a530Spatrick // It removes MergeInputSections from the input section array and adds
158ece8a530Spatrick // new synthetic sections at the location of the first input section
159ece8a530Spatrick // that it replaces. It then finalizes each synthetic section in order
160ece8a530Spatrick // to compute an output offset for each piece of each input section.
161ece8a530Spatrick void OutputSection::finalizeInputSections() {
162ece8a530Spatrick   std::vector<MergeSyntheticSection *> mergeSections;
163ece8a530Spatrick   for (BaseCommand *base : sectionCommands) {
164ece8a530Spatrick     auto *cmd = dyn_cast<InputSectionDescription>(base);
165ece8a530Spatrick     if (!cmd)
166ece8a530Spatrick       continue;
167ece8a530Spatrick     cmd->sections.reserve(cmd->sectionBases.size());
168ece8a530Spatrick     for (InputSectionBase *s : cmd->sectionBases) {
169ece8a530Spatrick       MergeInputSection *ms = dyn_cast<MergeInputSection>(s);
170ece8a530Spatrick       if (!ms) {
171ece8a530Spatrick         cmd->sections.push_back(cast<InputSection>(s));
172ece8a530Spatrick         continue;
173ece8a530Spatrick       }
174ece8a530Spatrick 
175ece8a530Spatrick       // We do not want to handle sections that are not alive, so just remove
176ece8a530Spatrick       // them instead of trying to merge.
177ece8a530Spatrick       if (!ms->isLive())
178ece8a530Spatrick         continue;
179ece8a530Spatrick 
180ece8a530Spatrick       auto i = llvm::find_if(mergeSections, [=](MergeSyntheticSection *sec) {
181ece8a530Spatrick         // While we could create a single synthetic section for two different
182ece8a530Spatrick         // values of Entsize, it is better to take Entsize into consideration.
183ece8a530Spatrick         //
184ece8a530Spatrick         // With a single synthetic section no two pieces with different Entsize
185ece8a530Spatrick         // could be equal, so we may as well have two sections.
186ece8a530Spatrick         //
187ece8a530Spatrick         // Using Entsize in here also allows us to propagate it to the synthetic
188ece8a530Spatrick         // section.
189ece8a530Spatrick         //
190ece8a530Spatrick         // SHF_STRINGS section with different alignments should not be merged.
191ece8a530Spatrick         return sec->flags == ms->flags && sec->entsize == ms->entsize &&
192ece8a530Spatrick                (sec->alignment == ms->alignment || !(sec->flags & SHF_STRINGS));
193ece8a530Spatrick       });
194ece8a530Spatrick       if (i == mergeSections.end()) {
195ece8a530Spatrick         MergeSyntheticSection *syn =
196ece8a530Spatrick             createMergeSynthetic(name, ms->type, ms->flags, ms->alignment);
197ece8a530Spatrick         mergeSections.push_back(syn);
198ece8a530Spatrick         i = std::prev(mergeSections.end());
199ece8a530Spatrick         syn->entsize = ms->entsize;
200ece8a530Spatrick         cmd->sections.push_back(syn);
201ece8a530Spatrick       }
202ece8a530Spatrick       (*i)->addSection(ms);
203ece8a530Spatrick     }
204ece8a530Spatrick 
205ece8a530Spatrick     // sectionBases should not be used from this point onwards. Clear it to
206ece8a530Spatrick     // catch misuses.
207ece8a530Spatrick     cmd->sectionBases.clear();
208ece8a530Spatrick 
209ece8a530Spatrick     // Some input sections may be removed from the list after ICF.
210ece8a530Spatrick     for (InputSection *s : cmd->sections)
211ece8a530Spatrick       commitSection(s);
212ece8a530Spatrick   }
213ece8a530Spatrick   for (auto *ms : mergeSections)
214ece8a530Spatrick     ms->finalizeContents();
215ece8a530Spatrick }
216ece8a530Spatrick 
217ece8a530Spatrick static void sortByOrder(MutableArrayRef<InputSection *> in,
218ece8a530Spatrick                         llvm::function_ref<int(InputSectionBase *s)> order) {
219ece8a530Spatrick   std::vector<std::pair<int, InputSection *>> v;
220ece8a530Spatrick   for (InputSection *s : in)
221ece8a530Spatrick     v.push_back({order(s), s});
222ece8a530Spatrick   llvm::stable_sort(v, less_first());
223ece8a530Spatrick 
224ece8a530Spatrick   for (size_t i = 0; i < v.size(); ++i)
225ece8a530Spatrick     in[i] = v[i].second;
226ece8a530Spatrick }
227ece8a530Spatrick 
228ece8a530Spatrick uint64_t getHeaderSize() {
229ece8a530Spatrick   if (config->oFormatBinary)
230ece8a530Spatrick     return 0;
231ece8a530Spatrick   return Out::elfHeader->size + Out::programHeaders->size;
232ece8a530Spatrick }
233ece8a530Spatrick 
234ece8a530Spatrick bool OutputSection::classof(const BaseCommand *c) {
235ece8a530Spatrick   return c->kind == OutputSectionKind;
236ece8a530Spatrick }
237ece8a530Spatrick 
238ece8a530Spatrick void OutputSection::sort(llvm::function_ref<int(InputSectionBase *s)> order) {
239ece8a530Spatrick   assert(isLive());
240ece8a530Spatrick   for (BaseCommand *b : sectionCommands)
241ece8a530Spatrick     if (auto *isd = dyn_cast<InputSectionDescription>(b))
242ece8a530Spatrick       sortByOrder(isd->sections, order);
243ece8a530Spatrick }
244ece8a530Spatrick 
245ece8a530Spatrick // Fill [Buf, Buf + Size) with Filler.
246ece8a530Spatrick // This is used for linker script "=fillexp" command.
247ece8a530Spatrick static void fill(uint8_t *buf, size_t size,
248ece8a530Spatrick                  const std::array<uint8_t, 4> &filler) {
249ece8a530Spatrick   size_t i = 0;
250ece8a530Spatrick   for (; i + 4 < size; i += 4)
251ece8a530Spatrick     memcpy(buf + i, filler.data(), 4);
252ece8a530Spatrick   memcpy(buf + i, filler.data(), size - i);
253ece8a530Spatrick }
254ece8a530Spatrick 
255ece8a530Spatrick // Compress section contents if this section contains debug info.
256ece8a530Spatrick template <class ELFT> void OutputSection::maybeCompress() {
257ece8a530Spatrick   using Elf_Chdr = typename ELFT::Chdr;
258ece8a530Spatrick 
259ece8a530Spatrick   // Compress only DWARF debug sections.
260ece8a530Spatrick   if (!config->compressDebugSections || (flags & SHF_ALLOC) ||
261ece8a530Spatrick       !name.startswith(".debug_"))
262ece8a530Spatrick     return;
263ece8a530Spatrick 
264ece8a530Spatrick   // Create a section header.
265ece8a530Spatrick   zDebugHeader.resize(sizeof(Elf_Chdr));
266ece8a530Spatrick   auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
267ece8a530Spatrick   hdr->ch_type = ELFCOMPRESS_ZLIB;
268ece8a530Spatrick   hdr->ch_size = size;
269ece8a530Spatrick   hdr->ch_addralign = alignment;
270ece8a530Spatrick 
271ece8a530Spatrick   // Write section contents to a temporary buffer and compress it.
272ece8a530Spatrick   std::vector<uint8_t> buf(size);
273ece8a530Spatrick   writeTo<ELFT>(buf.data());
274ece8a530Spatrick   // We chose 1 as the default compression level because it is the fastest. If
275ece8a530Spatrick   // -O2 is given, we use level 6 to compress debug info more by ~15%. We found
276ece8a530Spatrick   // that level 7 to 9 doesn't make much difference (~1% more compression) while
277ece8a530Spatrick   // they take significant amount of time (~2x), so level 6 seems enough.
278ece8a530Spatrick   if (Error e = zlib::compress(toStringRef(buf), compressedData,
279ece8a530Spatrick                                config->optimize >= 2 ? 6 : 1))
280ece8a530Spatrick     fatal("compress failed: " + llvm::toString(std::move(e)));
281ece8a530Spatrick 
282ece8a530Spatrick   // Update section headers.
283ece8a530Spatrick   size = sizeof(Elf_Chdr) + compressedData.size();
284ece8a530Spatrick   flags |= SHF_COMPRESSED;
285ece8a530Spatrick }
286ece8a530Spatrick 
287ece8a530Spatrick static void writeInt(uint8_t *buf, uint64_t data, uint64_t size) {
288ece8a530Spatrick   if (size == 1)
289ece8a530Spatrick     *buf = data;
290ece8a530Spatrick   else if (size == 2)
291ece8a530Spatrick     write16(buf, data);
292ece8a530Spatrick   else if (size == 4)
293ece8a530Spatrick     write32(buf, data);
294ece8a530Spatrick   else if (size == 8)
295ece8a530Spatrick     write64(buf, data);
296ece8a530Spatrick   else
297ece8a530Spatrick     llvm_unreachable("unsupported Size argument");
298ece8a530Spatrick }
299ece8a530Spatrick 
300ece8a530Spatrick template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
301ece8a530Spatrick   if (type == SHT_NOBITS)
302ece8a530Spatrick     return;
303ece8a530Spatrick 
304ece8a530Spatrick   // If -compress-debug-section is specified and if this is a debug section,
305ece8a530Spatrick   // we've already compressed section contents. If that's the case,
306ece8a530Spatrick   // just write it down.
307ece8a530Spatrick   if (!compressedData.empty()) {
308ece8a530Spatrick     memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
309ece8a530Spatrick     memcpy(buf + zDebugHeader.size(), compressedData.data(),
310ece8a530Spatrick            compressedData.size());
311ece8a530Spatrick     return;
312ece8a530Spatrick   }
313ece8a530Spatrick 
314ece8a530Spatrick   // Write leading padding.
315ece8a530Spatrick   std::vector<InputSection *> sections = getInputSections(this);
316ece8a530Spatrick   std::array<uint8_t, 4> filler = getFiller();
317ece8a530Spatrick   bool nonZeroFiller = read32(filler.data()) != 0;
318ece8a530Spatrick   if (nonZeroFiller)
319ece8a530Spatrick     fill(buf, sections.empty() ? size : sections[0]->outSecOff, filler);
320ece8a530Spatrick 
321ece8a530Spatrick   parallelForEachN(0, sections.size(), [&](size_t i) {
322ece8a530Spatrick     InputSection *isec = sections[i];
323ece8a530Spatrick     isec->writeTo<ELFT>(buf);
324ece8a530Spatrick 
325ece8a530Spatrick     // Fill gaps between sections.
326ece8a530Spatrick     if (nonZeroFiller) {
327ece8a530Spatrick       uint8_t *start = buf + isec->outSecOff + isec->getSize();
328ece8a530Spatrick       uint8_t *end;
329ece8a530Spatrick       if (i + 1 == sections.size())
330ece8a530Spatrick         end = buf + size;
331ece8a530Spatrick       else
332ece8a530Spatrick         end = buf + sections[i + 1]->outSecOff;
333ece8a530Spatrick       fill(start, end - start, filler);
334ece8a530Spatrick     }
335ece8a530Spatrick   });
336ece8a530Spatrick 
337ece8a530Spatrick   // Linker scripts may have BYTE()-family commands with which you
338ece8a530Spatrick   // can write arbitrary bytes to the output. Process them if any.
339ece8a530Spatrick   for (BaseCommand *base : sectionCommands)
340ece8a530Spatrick     if (auto *data = dyn_cast<ByteCommand>(base))
341ece8a530Spatrick       writeInt(buf + data->offset, data->expression().getValue(), data->size);
342ece8a530Spatrick }
343ece8a530Spatrick 
344ece8a530Spatrick static void finalizeShtGroup(OutputSection *os,
345ece8a530Spatrick                              InputSection *section) {
346ece8a530Spatrick   assert(config->relocatable);
347ece8a530Spatrick 
348ece8a530Spatrick   // sh_link field for SHT_GROUP sections should contain the section index of
349ece8a530Spatrick   // the symbol table.
350ece8a530Spatrick   os->link = in.symTab->getParent()->sectionIndex;
351ece8a530Spatrick 
352ece8a530Spatrick   // sh_info then contain index of an entry in symbol table section which
353ece8a530Spatrick   // provides signature of the section group.
354ece8a530Spatrick   ArrayRef<Symbol *> symbols = section->file->getSymbols();
355ece8a530Spatrick   os->info = in.symTab->getSymbolIndex(symbols[section->info]);
356ece8a530Spatrick }
357ece8a530Spatrick 
358ece8a530Spatrick void OutputSection::finalize() {
359ece8a530Spatrick   std::vector<InputSection *> v = getInputSections(this);
360ece8a530Spatrick   InputSection *first = v.empty() ? nullptr : v[0];
361ece8a530Spatrick 
362ece8a530Spatrick   if (flags & SHF_LINK_ORDER) {
363ece8a530Spatrick     // We must preserve the link order dependency of sections with the
364ece8a530Spatrick     // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
365ece8a530Spatrick     // need to translate the InputSection sh_link to the OutputSection sh_link,
366ece8a530Spatrick     // all InputSections in the OutputSection have the same dependency.
367ece8a530Spatrick     if (auto *ex = dyn_cast<ARMExidxSyntheticSection>(first))
368ece8a530Spatrick       link = ex->getLinkOrderDep()->getParent()->sectionIndex;
369*667950d7Spatrick     else if (first->flags & SHF_LINK_ORDER)
370*667950d7Spatrick       if (auto *d = first->getLinkOrderDep())
371ece8a530Spatrick         link = d->getParent()->sectionIndex;
372ece8a530Spatrick   }
373ece8a530Spatrick 
374ece8a530Spatrick   if (type == SHT_GROUP) {
375ece8a530Spatrick     finalizeShtGroup(this, first);
376ece8a530Spatrick     return;
377ece8a530Spatrick   }
378ece8a530Spatrick 
379ece8a530Spatrick   if (!config->copyRelocs || (type != SHT_RELA && type != SHT_REL))
380ece8a530Spatrick     return;
381ece8a530Spatrick 
382ece8a530Spatrick   if (isa<SyntheticSection>(first))
383ece8a530Spatrick     return;
384ece8a530Spatrick 
385ece8a530Spatrick   link = in.symTab->getParent()->sectionIndex;
386ece8a530Spatrick   // sh_info for SHT_REL[A] sections should contain the section header index of
387ece8a530Spatrick   // the section to which the relocation applies.
388ece8a530Spatrick   InputSectionBase *s = first->getRelocatedSection();
389ece8a530Spatrick   info = s->getOutputSection()->sectionIndex;
390ece8a530Spatrick   flags |= SHF_INFO_LINK;
391ece8a530Spatrick }
392ece8a530Spatrick 
393ece8a530Spatrick // Returns true if S is in one of the many forms the compiler driver may pass
394ece8a530Spatrick // crtbegin files.
395ece8a530Spatrick //
396ece8a530Spatrick // Gcc uses any of crtbegin[<empty>|S|T].o.
397ece8a530Spatrick // Clang uses Gcc's plus clang_rt.crtbegin[<empty>|S|T][-<arch>|<empty>].o.
398ece8a530Spatrick 
399ece8a530Spatrick static bool isCrtbegin(StringRef s) {
400ece8a530Spatrick   static std::regex re(R"((clang_rt\.)?crtbegin[ST]?(-.*)?\.o)");
401ece8a530Spatrick   s = sys::path::filename(s);
402ece8a530Spatrick   return std::regex_match(s.begin(), s.end(), re);
403ece8a530Spatrick }
404ece8a530Spatrick 
405ece8a530Spatrick static bool isCrtend(StringRef s) {
406ece8a530Spatrick   static std::regex re(R"((clang_rt\.)?crtend[ST]?(-.*)?\.o)");
407ece8a530Spatrick   s = sys::path::filename(s);
408ece8a530Spatrick   return std::regex_match(s.begin(), s.end(), re);
409ece8a530Spatrick }
410ece8a530Spatrick 
411ece8a530Spatrick // .ctors and .dtors are sorted by this priority from highest to lowest.
412ece8a530Spatrick //
413ece8a530Spatrick //  1. The section was contained in crtbegin (crtbegin contains
414ece8a530Spatrick //     some sentinel value in its .ctors and .dtors so that the runtime
415ece8a530Spatrick //     can find the beginning of the sections.)
416ece8a530Spatrick //
417ece8a530Spatrick //  2. The section has an optional priority value in the form of ".ctors.N"
418ece8a530Spatrick //     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
419ece8a530Spatrick //     they are compared as string rather than number.
420ece8a530Spatrick //
421ece8a530Spatrick //  3. The section is just ".ctors" or ".dtors".
422ece8a530Spatrick //
423ece8a530Spatrick //  4. The section was contained in crtend, which contains an end marker.
424ece8a530Spatrick //
425ece8a530Spatrick // In an ideal world, we don't need this function because .init_array and
426ece8a530Spatrick // .ctors are duplicate features (and .init_array is newer.) However, there
427ece8a530Spatrick // are too many real-world use cases of .ctors, so we had no choice to
428ece8a530Spatrick // support that with this rather ad-hoc semantics.
429ece8a530Spatrick static bool compCtors(const InputSection *a, const InputSection *b) {
430ece8a530Spatrick   bool beginA = isCrtbegin(a->file->getName());
431ece8a530Spatrick   bool beginB = isCrtbegin(b->file->getName());
432ece8a530Spatrick   if (beginA != beginB)
433ece8a530Spatrick     return beginA;
434ece8a530Spatrick   bool endA = isCrtend(a->file->getName());
435ece8a530Spatrick   bool endB = isCrtend(b->file->getName());
436ece8a530Spatrick   if (endA != endB)
437ece8a530Spatrick     return endB;
438ece8a530Spatrick   StringRef x = a->name;
439ece8a530Spatrick   StringRef y = b->name;
440ece8a530Spatrick   assert(x.startswith(".ctors") || x.startswith(".dtors"));
441ece8a530Spatrick   assert(y.startswith(".ctors") || y.startswith(".dtors"));
442ece8a530Spatrick   x = x.substr(6);
443ece8a530Spatrick   y = y.substr(6);
444ece8a530Spatrick   return x < y;
445ece8a530Spatrick }
446ece8a530Spatrick 
447ece8a530Spatrick // Sorts input sections by the special rules for .ctors and .dtors.
448ece8a530Spatrick // Unfortunately, the rules are different from the one for .{init,fini}_array.
449ece8a530Spatrick // Read the comment above.
450ece8a530Spatrick void OutputSection::sortCtorsDtors() {
451ece8a530Spatrick   assert(sectionCommands.size() == 1);
452ece8a530Spatrick   auto *isd = cast<InputSectionDescription>(sectionCommands[0]);
453ece8a530Spatrick   llvm::stable_sort(isd->sections, compCtors);
454ece8a530Spatrick }
455ece8a530Spatrick 
456ece8a530Spatrick // If an input string is in the form of "foo.N" where N is a number,
457ece8a530Spatrick // return N. Otherwise, returns 65536, which is one greater than the
458ece8a530Spatrick // lowest priority.
459ece8a530Spatrick int getPriority(StringRef s) {
460ece8a530Spatrick   size_t pos = s.rfind('.');
461ece8a530Spatrick   if (pos == StringRef::npos)
462ece8a530Spatrick     return 65536;
463ece8a530Spatrick   int v;
464ece8a530Spatrick   if (!to_integer(s.substr(pos + 1), v, 10))
465ece8a530Spatrick     return 65536;
466ece8a530Spatrick   return v;
467ece8a530Spatrick }
468ece8a530Spatrick 
469ece8a530Spatrick std::vector<InputSection *> getInputSections(OutputSection *os) {
470ece8a530Spatrick   std::vector<InputSection *> ret;
471ece8a530Spatrick   for (BaseCommand *base : os->sectionCommands)
472ece8a530Spatrick     if (auto *isd = dyn_cast<InputSectionDescription>(base))
473ece8a530Spatrick       ret.insert(ret.end(), isd->sections.begin(), isd->sections.end());
474ece8a530Spatrick   return ret;
475ece8a530Spatrick }
476ece8a530Spatrick 
477ece8a530Spatrick // Sorts input sections by section name suffixes, so that .foo.N comes
478ece8a530Spatrick // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
479ece8a530Spatrick // We want to keep the original order if the priorities are the same
480ece8a530Spatrick // because the compiler keeps the original initialization order in a
481ece8a530Spatrick // translation unit and we need to respect that.
482ece8a530Spatrick // For more detail, read the section of the GCC's manual about init_priority.
483ece8a530Spatrick void OutputSection::sortInitFini() {
484ece8a530Spatrick   // Sort sections by priority.
485ece8a530Spatrick   sort([](InputSectionBase *s) { return getPriority(s->name); });
486ece8a530Spatrick }
487ece8a530Spatrick 
488ece8a530Spatrick std::array<uint8_t, 4> OutputSection::getFiller() {
489ece8a530Spatrick   if (filler)
490ece8a530Spatrick     return *filler;
491ece8a530Spatrick   if (flags & SHF_EXECINSTR)
492ece8a530Spatrick     return target->trapInstr;
493ece8a530Spatrick   return {0, 0, 0, 0};
494ece8a530Spatrick }
495ece8a530Spatrick 
496ece8a530Spatrick template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
497ece8a530Spatrick template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
498ece8a530Spatrick template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
499ece8a530Spatrick template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
500ece8a530Spatrick 
501ece8a530Spatrick template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
502ece8a530Spatrick template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
503ece8a530Spatrick template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
504ece8a530Spatrick template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
505ece8a530Spatrick 
506ece8a530Spatrick template void OutputSection::maybeCompress<ELF32LE>();
507ece8a530Spatrick template void OutputSection::maybeCompress<ELF32BE>();
508ece8a530Spatrick template void OutputSection::maybeCompress<ELF64LE>();
509ece8a530Spatrick template void OutputSection::maybeCompress<ELF64BE>();
510ece8a530Spatrick 
511ece8a530Spatrick } // namespace elf
512ece8a530Spatrick } // namespace lld
513