xref: /freebsd/contrib/llvm-project/lld/ELF/Writer.cpp (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Writer.h"
10 #include "AArch64ErrataFix.h"
11 #include "ARMErrataFix.h"
12 #include "CallGraphSort.h"
13 #include "Config.h"
14 #include "InputFiles.h"
15 #include "LinkerScript.h"
16 #include "MapFile.h"
17 #include "OutputSections.h"
18 #include "Relocations.h"
19 #include "SymbolTable.h"
20 #include "Symbols.h"
21 #include "SyntheticSections.h"
22 #include "Target.h"
23 #include "lld/Common/Arrays.h"
24 #include "lld/Common/CommonLinkerContext.h"
25 #include "lld/Common/Filesystem.h"
26 #include "lld/Common/Strings.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/Support/BLAKE3.h"
30 #include "llvm/Support/Parallel.h"
31 #include "llvm/Support/RandomNumberGenerator.h"
32 #include "llvm/Support/TimeProfiler.h"
33 #include "llvm/Support/xxhash.h"
34 #include <climits>
35 
36 #define DEBUG_TYPE "lld"
37 
38 using namespace llvm;
39 using namespace llvm::ELF;
40 using namespace llvm::object;
41 using namespace llvm::support;
42 using namespace llvm::support::endian;
43 using namespace lld;
44 using namespace lld::elf;
45 
46 namespace {
47 // The writer writes a SymbolTable result to a file.
48 template <class ELFT> class Writer {
49 public:
50   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
51 
Writer()52   Writer() : buffer(errorHandler().outputBuffer) {}
53 
54   void run();
55 
56 private:
57   void addSectionSymbols();
58   void sortSections();
59   void resolveShfLinkOrder();
60   void finalizeAddressDependentContent();
61   void optimizeBasicBlockJumps();
62   void sortInputSections();
63   void sortOrphanSections();
64   void finalizeSections();
65   void checkExecuteOnly();
66   void setReservedSymbolSections();
67 
68   SmallVector<PhdrEntry *, 0> createPhdrs(Partition &part);
69   void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
70                          unsigned pFlags);
71   void assignFileOffsets();
72   void assignFileOffsetsBinary();
73   void setPhdrs(Partition &part);
74   void checkSections();
75   void fixSectionAlignments();
76   void openFile();
77   void writeTrapInstr();
78   void writeHeader();
79   void writeSections();
80   void writeSectionsBinary();
81   void writeBuildId();
82 
83   std::unique_ptr<FileOutputBuffer> &buffer;
84 
85   void addRelIpltSymbols();
86   void addStartEndSymbols();
87   void addStartStopSymbols(OutputSection &osec);
88 
89   uint64_t fileSize;
90   uint64_t sectionHeaderOff;
91 };
92 } // anonymous namespace
93 
writeResult()94 template <class ELFT> void elf::writeResult() {
95   Writer<ELFT>().run();
96 }
97 
removeEmptyPTLoad(SmallVector<PhdrEntry *,0> & phdrs)98 static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
99   auto it = std::stable_partition(
100       phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) {
101         if (p->p_type != PT_LOAD)
102           return true;
103         if (!p->firstSec)
104           return false;
105         uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
106         return size != 0;
107       });
108 
109   // Clear OutputSection::ptLoad for sections contained in removed
110   // segments.
111   DenseSet<PhdrEntry *> removed(it, phdrs.end());
112   for (OutputSection *sec : outputSections)
113     if (removed.count(sec->ptLoad))
114       sec->ptLoad = nullptr;
115   phdrs.erase(it, phdrs.end());
116 }
117 
copySectionsIntoPartitions()118 void elf::copySectionsIntoPartitions() {
119   SmallVector<InputSectionBase *, 0> newSections;
120   const size_t ehSize = ctx.ehInputSections.size();
121   for (unsigned part = 2; part != partitions.size() + 1; ++part) {
122     for (InputSectionBase *s : ctx.inputSections) {
123       if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
124         continue;
125       auto *copy = make<InputSection>(cast<InputSection>(*s));
126       copy->partition = part;
127       newSections.push_back(copy);
128     }
129     for (size_t i = 0; i != ehSize; ++i) {
130       assert(ctx.ehInputSections[i]->isLive());
131       auto *copy = make<EhInputSection>(*ctx.ehInputSections[i]);
132       copy->partition = part;
133       ctx.ehInputSections.push_back(copy);
134     }
135   }
136 
137   ctx.inputSections.insert(ctx.inputSections.end(), newSections.begin(),
138                            newSections.end());
139 }
140 
addOptionalRegular(StringRef name,SectionBase * sec,uint64_t val,uint8_t stOther=STV_HIDDEN)141 static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
142                                    uint64_t val, uint8_t stOther = STV_HIDDEN) {
143   Symbol *s = symtab.find(name);
144   if (!s || s->isDefined() || s->isCommon())
145     return nullptr;
146 
147   s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, stOther,
148                      STT_NOTYPE, val,
149                      /*size=*/0, sec});
150   s->isUsedInRegularObj = true;
151   return cast<Defined>(s);
152 }
153 
154 // The linker is expected to define some symbols depending on
155 // the linking result. This function defines such symbols.
addReservedSymbols()156 void elf::addReservedSymbols() {
157   if (config->emachine == EM_MIPS) {
158     auto addAbsolute = [](StringRef name) {
159       Symbol *sym =
160           symtab.addSymbol(Defined{ctx.internalFile, name, STB_GLOBAL,
161                                    STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});
162       sym->isUsedInRegularObj = true;
163       return cast<Defined>(sym);
164     };
165     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
166     // so that it points to an absolute address which by default is relative
167     // to GOT. Default offset is 0x7ff0.
168     // See "Global Data Symbols" in Chapter 6 in the following document:
169     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
170     ElfSym::mipsGp = addAbsolute("_gp");
171 
172     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
173     // start of function and 'gp' pointer into GOT.
174     if (symtab.find("_gp_disp"))
175       ElfSym::mipsGpDisp = addAbsolute("_gp_disp");
176 
177     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
178     // pointer. This symbol is used in the code generated by .cpload pseudo-op
179     // in case of using -mno-shared option.
180     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
181     if (symtab.find("__gnu_local_gp"))
182       ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp");
183   } else if (config->emachine == EM_PPC) {
184     // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
185     // support Small Data Area, define it arbitrarily as 0.
186     addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
187   } else if (config->emachine == EM_PPC64) {
188     addPPC64SaveRestore();
189   }
190 
191   // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
192   // combines the typical ELF GOT with the small data sections. It commonly
193   // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
194   // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
195   // represent the TOC base which is offset by 0x8000 bytes from the start of
196   // the .got section.
197   // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
198   // correctness of some relocations depends on its value.
199   StringRef gotSymName =
200       (config->emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
201 
202   if (Symbol *s = symtab.find(gotSymName)) {
203     if (s->isDefined()) {
204       error(toString(s->file) + " cannot redefine linker defined symbol '" +
205             gotSymName + "'");
206       return;
207     }
208 
209     uint64_t gotOff = 0;
210     if (config->emachine == EM_PPC64)
211       gotOff = 0x8000;
212 
213     s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, STV_HIDDEN,
214                        STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
215     ElfSym::globalOffsetTable = cast<Defined>(s);
216   }
217 
218   // __ehdr_start is the location of ELF file headers. Note that we define
219   // this symbol unconditionally even when using a linker script, which
220   // differs from the behavior implemented by GNU linker which only define
221   // this symbol if ELF headers are in the memory mapped segment.
222   addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN);
223 
224   // __executable_start is not documented, but the expectation of at
225   // least the Android libc is that it points to the ELF header.
226   addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN);
227 
228   // __dso_handle symbol is passed to cxa_finalize as a marker to identify
229   // each DSO. The address of the symbol doesn't matter as long as they are
230   // different in different DSOs, so we chose the start address of the DSO.
231   addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN);
232 
233   // If linker script do layout we do not need to create any standard symbols.
234   if (script->hasSectionsCommand)
235     return;
236 
237   auto add = [](StringRef s, int64_t pos) {
238     return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT);
239   };
240 
241   ElfSym::bss = add("__bss_start", 0);
242   ElfSym::end1 = add("end", -1);
243   ElfSym::end2 = add("_end", -1);
244   ElfSym::etext1 = add("etext", -1);
245   ElfSym::etext2 = add("_etext", -1);
246   ElfSym::edata1 = add("edata", -1);
247   ElfSym::edata2 = add("_edata", -1);
248 }
249 
demoteDefined(Defined & sym,DenseMap<SectionBase *,size_t> & map)250 static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
251   if (map.empty())
252     for (auto [i, sec] : llvm::enumerate(sym.file->getSections()))
253       map.try_emplace(sec, i);
254   // Change WEAK to GLOBAL so that if a scanned relocation references sym,
255   // maybeReportUndefined will report an error.
256   uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;
257   Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
258             /*discardedSecIdx=*/map.lookup(sym.section))
259       .overwrite(sym);
260   // Eliminate from the symbol table, otherwise we would leave an undefined
261   // symbol if the symbol is unreferenced in the absence of GC.
262   sym.isUsedInRegularObj = false;
263 }
264 
265 // If all references to a DSO happen to be weak, the DSO is not added to
266 // DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
267 // dangling references to an unneeded DSO. Use a weak binding to avoid
268 // --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
269 //
270 // In addition, demote symbols defined in discarded sections, so that
271 // references to /DISCARD/ discarded symbols will lead to errors.
demoteSymbolsAndComputeIsPreemptible()272 static void demoteSymbolsAndComputeIsPreemptible() {
273   llvm::TimeTraceScope timeScope("Demote symbols");
274   DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
275   for (Symbol *sym : symtab.getSymbols()) {
276     if (auto *d = dyn_cast<Defined>(sym)) {
277       if (d->section && !d->section->isLive())
278         demoteDefined(*d, sectionIndexMap[d->file]);
279     } else {
280       auto *s = dyn_cast<SharedSymbol>(sym);
281       if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
282         uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
283         Undefined(ctx.internalFile, sym->getName(), binding, sym->stOther,
284                   sym->type)
285             .overwrite(*sym);
286         sym->versionId = VER_NDX_GLOBAL;
287       }
288     }
289 
290     if (config->hasDynSymTab)
291       sym->isPreemptible = computeIsPreemptible(*sym);
292   }
293 }
294 
findSection(StringRef name,unsigned partition=1)295 static OutputSection *findSection(StringRef name, unsigned partition = 1) {
296   for (SectionCommand *cmd : script->sectionCommands)
297     if (auto *osd = dyn_cast<OutputDesc>(cmd))
298       if (osd->osec.name == name && osd->osec.partition == partition)
299         return &osd->osec;
300   return nullptr;
301 }
302 
303 // The main function of the writer.
run()304 template <class ELFT> void Writer<ELFT>::run() {
305   // Now that we have a complete set of output sections. This function
306   // completes section contents. For example, we need to add strings
307   // to the string table, and add entries to .got and .plt.
308   // finalizeSections does that.
309   finalizeSections();
310   checkExecuteOnly();
311 
312   // If --compressed-debug-sections is specified, compress .debug_* sections.
313   // Do it right now because it changes the size of output sections.
314   for (OutputSection *sec : outputSections)
315     sec->maybeCompress<ELFT>();
316 
317   if (script->hasSectionsCommand)
318     script->allocateHeaders(mainPart->phdrs);
319 
320   // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
321   // 0 sized region. This has to be done late since only after assignAddresses
322   // we know the size of the sections.
323   for (Partition &part : partitions)
324     removeEmptyPTLoad(part.phdrs);
325 
326   if (!config->oFormatBinary)
327     assignFileOffsets();
328   else
329     assignFileOffsetsBinary();
330 
331   for (Partition &part : partitions)
332     setPhdrs(part);
333 
334   // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
335   // because the files may be useful in case checkSections() or openFile()
336   // fails, for example, due to an erroneous file size.
337   writeMapAndCref();
338 
339   // Handle --print-memory-usage option.
340   if (config->printMemoryUsage)
341     script->printMemoryUsage(lld::outs());
342 
343   if (config->checkSections)
344     checkSections();
345 
346   // It does not make sense try to open the file if we have error already.
347   if (errorCount())
348     return;
349 
350   {
351     llvm::TimeTraceScope timeScope("Write output file");
352     // Write the result down to a file.
353     openFile();
354     if (errorCount())
355       return;
356 
357     if (!config->oFormatBinary) {
358       if (config->zSeparate != SeparateSegmentKind::None)
359         writeTrapInstr();
360       writeHeader();
361       writeSections();
362     } else {
363       writeSectionsBinary();
364     }
365 
366     // Backfill .note.gnu.build-id section content. This is done at last
367     // because the content is usually a hash value of the entire output file.
368     writeBuildId();
369     if (errorCount())
370       return;
371 
372     if (auto e = buffer->commit())
373       fatal("failed to write output '" + buffer->getPath() +
374             "': " + toString(std::move(e)));
375 
376     if (!config->cmseOutputLib.empty())
377       writeARMCmseImportLib<ELFT>();
378   }
379 }
380 
381 template <class ELFT, class RelTy>
markUsedLocalSymbolsImpl(ObjFile<ELFT> * file,llvm::ArrayRef<RelTy> rels)382 static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
383                                      llvm::ArrayRef<RelTy> rels) {
384   for (const RelTy &rel : rels) {
385     Symbol &sym = file->getRelocTargetSym(rel);
386     if (sym.isLocal())
387       sym.used = true;
388   }
389 }
390 
391 // The function ensures that the "used" field of local symbols reflects the fact
392 // that the symbol is used in a relocation from a live section.
markUsedLocalSymbols()393 template <class ELFT> static void markUsedLocalSymbols() {
394   // With --gc-sections, the field is already filled.
395   // See MarkLive<ELFT>::resolveReloc().
396   if (config->gcSections)
397     return;
398   for (ELFFileBase *file : ctx.objectFiles) {
399     ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
400     for (InputSectionBase *s : f->getSections()) {
401       InputSection *isec = dyn_cast_or_null<InputSection>(s);
402       if (!isec)
403         continue;
404       if (isec->type == SHT_REL) {
405         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());
406       } else if (isec->type == SHT_RELA) {
407         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());
408       } else if (isec->type == SHT_CREL) {
409         // The is64=true variant also works with ELF32 since only the r_symidx
410         // member is used.
411         for (Elf_Crel_Impl<true> r : RelocsCrel<true>(isec->content_)) {
412           Symbol &sym = file->getSymbol(r.r_symidx);
413           if (sym.isLocal())
414             sym.used = true;
415         }
416       }
417     }
418   }
419 }
420 
shouldKeepInSymtab(const Defined & sym)421 static bool shouldKeepInSymtab(const Defined &sym) {
422   if (sym.isSection())
423     return false;
424 
425   // If --emit-reloc or -r is given, preserve symbols referenced by relocations
426   // from live sections.
427   if (sym.used && config->copyRelocs)
428     return true;
429 
430   // Exclude local symbols pointing to .ARM.exidx sections.
431   // They are probably mapping symbols "$d", which are optional for these
432   // sections. After merging the .ARM.exidx sections, some of these symbols
433   // may become dangling. The easiest way to avoid the issue is not to add
434   // them to the symbol table from the beginning.
435   if (config->emachine == EM_ARM && sym.section &&
436       sym.section->type == SHT_ARM_EXIDX)
437     return false;
438 
439   if (config->discard == DiscardPolicy::None)
440     return true;
441   if (config->discard == DiscardPolicy::All)
442     return false;
443 
444   // In ELF assembly .L symbols are normally discarded by the assembler.
445   // If the assembler fails to do so, the linker discards them if
446   // * --discard-locals is used.
447   // * The symbol is in a SHF_MERGE section, which is normally the reason for
448   //   the assembler keeping the .L symbol.
449   if (sym.getName().starts_with(".L") &&
450       (config->discard == DiscardPolicy::Locals ||
451        (sym.section && (sym.section->flags & SHF_MERGE))))
452     return false;
453   return true;
454 }
455 
includeInSymtab(const Symbol & b)456 bool lld::elf::includeInSymtab(const Symbol &b) {
457   if (auto *d = dyn_cast<Defined>(&b)) {
458     // Always include absolute symbols.
459     SectionBase *sec = d->section;
460     if (!sec)
461       return true;
462     assert(sec->isLive());
463 
464     if (auto *s = dyn_cast<MergeInputSection>(sec))
465       return s->getSectionPiece(d->value).live;
466     return true;
467   }
468   return b.used || !config->gcSections;
469 }
470 
471 // Scan local symbols to:
472 //
473 // - demote symbols defined relative to /DISCARD/ discarded input sections so
474 //   that relocations referencing them will lead to errors.
475 // - copy eligible symbols to .symTab
demoteAndCopyLocalSymbols()476 static void demoteAndCopyLocalSymbols() {
477   llvm::TimeTraceScope timeScope("Add local symbols");
478   for (ELFFileBase *file : ctx.objectFiles) {
479     DenseMap<SectionBase *, size_t> sectionIndexMap;
480     for (Symbol *b : file->getLocalSymbols()) {
481       assert(b->isLocal() && "should have been caught in initializeSymbols()");
482       auto *dr = dyn_cast<Defined>(b);
483       if (!dr)
484         continue;
485 
486       if (dr->section && !dr->section->isLive())
487         demoteDefined(*dr, sectionIndexMap);
488       else if (in.symTab && includeInSymtab(*b) && shouldKeepInSymtab(*dr))
489         in.symTab->addSymbol(b);
490     }
491   }
492 }
493 
494 // Create a section symbol for each output section so that we can represent
495 // relocations that point to the section. If we know that no relocation is
496 // referring to a section (that happens if the section is a synthetic one), we
497 // don't create a section symbol for that section.
addSectionSymbols()498 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
499   for (SectionCommand *cmd : script->sectionCommands) {
500     auto *osd = dyn_cast<OutputDesc>(cmd);
501     if (!osd)
502       continue;
503     OutputSection &osec = osd->osec;
504     InputSectionBase *isec = nullptr;
505     // Iterate over all input sections and add a STT_SECTION symbol if any input
506     // section may be a relocation target.
507     for (SectionCommand *cmd : osec.commands) {
508       auto *isd = dyn_cast<InputSectionDescription>(cmd);
509       if (!isd)
510         continue;
511       for (InputSectionBase *s : isd->sections) {
512         // Relocations are not using REL[A] section symbols.
513         if (isStaticRelSecType(s->type))
514           continue;
515 
516         // Unlike other synthetic sections, mergeable output sections contain
517         // data copied from input sections, and there may be a relocation
518         // pointing to its contents if -r or --emit-reloc is given.
519         if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE))
520           continue;
521 
522         isec = s;
523         break;
524       }
525     }
526     if (!isec)
527       continue;
528 
529     // Set the symbol to be relative to the output section so that its st_value
530     // equals the output section address. Note, there may be a gap between the
531     // start of the output section and isec.
532     in.symTab->addSymbol(makeDefined(isec->file, "", STB_LOCAL, /*stOther=*/0,
533                                      STT_SECTION,
534                                      /*value=*/0, /*size=*/0, &osec));
535   }
536 }
537 
538 // Today's loaders have a feature to make segments read-only after
539 // processing dynamic relocations to enhance security. PT_GNU_RELRO
540 // is defined for that.
541 //
542 // This function returns true if a section needs to be put into a
543 // PT_GNU_RELRO segment.
isRelroSection(const OutputSection * sec)544 static bool isRelroSection(const OutputSection *sec) {
545   if (!config->zRelro)
546     return false;
547   if (sec->relro)
548     return true;
549 
550   uint64_t flags = sec->flags;
551 
552   // Non-allocatable or non-writable sections don't need RELRO because
553   // they are not writable or not even mapped to memory in the first place.
554   // RELRO is for sections that are essentially read-only but need to
555   // be writable only at process startup to allow dynamic linker to
556   // apply relocations.
557   if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
558     return false;
559 
560   // Once initialized, TLS data segments are used as data templates
561   // for a thread-local storage. For each new thread, runtime
562   // allocates memory for a TLS and copy templates there. No thread
563   // are supposed to use templates directly. Thus, it can be in RELRO.
564   if (flags & SHF_TLS)
565     return true;
566 
567   // .init_array, .preinit_array and .fini_array contain pointers to
568   // functions that are executed on process startup or exit. These
569   // pointers are set by the static linker, and they are not expected
570   // to change at runtime. But if you are an attacker, you could do
571   // interesting things by manipulating pointers in .fini_array, for
572   // example. So they are put into RELRO.
573   uint32_t type = sec->type;
574   if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
575       type == SHT_PREINIT_ARRAY)
576     return true;
577 
578   // .got contains pointers to external symbols. They are resolved by
579   // the dynamic linker when a module is loaded into memory, and after
580   // that they are not expected to change. So, it can be in RELRO.
581   if (in.got && sec == in.got->getParent())
582     return true;
583 
584   // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
585   // through r2 register, which is reserved for that purpose. Since r2 is used
586   // for accessing .got as well, .got and .toc need to be close enough in the
587   // virtual address space. Usually, .toc comes just after .got. Since we place
588   // .got into RELRO, .toc needs to be placed into RELRO too.
589   if (sec->name == ".toc")
590     return true;
591 
592   // .got.plt contains pointers to external function symbols. They are
593   // by default resolved lazily, so we usually cannot put it into RELRO.
594   // However, if "-z now" is given, the lazy symbol resolution is
595   // disabled, which enables us to put it into RELRO.
596   if (sec == in.gotPlt->getParent())
597     return config->zNow;
598 
599   if (in.relroPadding && sec == in.relroPadding->getParent())
600     return true;
601 
602   // .dynamic section contains data for the dynamic linker, and
603   // there's no need to write to it at runtime, so it's better to put
604   // it into RELRO.
605   if (sec->name == ".dynamic")
606     return true;
607 
608   // Sections with some special names are put into RELRO. This is a
609   // bit unfortunate because section names shouldn't be significant in
610   // ELF in spirit. But in reality many linker features depend on
611   // magic section names.
612   StringRef s = sec->name;
613 
614   bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" ||
615                      s == ".ctors" || s == ".dtors" || s == ".jcr" ||
616                      s == ".eh_frame" || s == ".fini_array" ||
617                      s == ".init_array" || s == ".preinit_array";
618 
619   bool abiSpecific =
620       config->osabi == ELFOSABI_OPENBSD && s == ".openbsd.randomdata";
621 
622   return abiAgnostic || abiSpecific;
623 }
624 
625 // We compute a rank for each section. The rank indicates where the
626 // section should be placed in the file.  Instead of using simple
627 // numbers (0,1,2...), we use a series of flags. One for each decision
628 // point when placing the section.
629 // Using flags has two key properties:
630 // * It is easy to check if a give branch was taken.
631 // * It is easy two see how similar two ranks are (see getRankProximity).
632 enum RankFlags {
633   RF_NOT_ADDR_SET = 1 << 27,
634   RF_NOT_ALLOC = 1 << 26,
635   RF_PARTITION = 1 << 18, // Partition number (8 bits)
636   RF_LARGE_ALT = 1 << 15,
637   RF_WRITE = 1 << 14,
638   RF_EXEC_WRITE = 1 << 13,
639   RF_EXEC = 1 << 12,
640   RF_RODATA = 1 << 11,
641   RF_LARGE = 1 << 10,
642   RF_NOT_RELRO = 1 << 9,
643   RF_NOT_TLS = 1 << 8,
644   RF_BSS = 1 << 7,
645 };
646 
getSectionRank(OutputSection & osec)647 unsigned elf::getSectionRank(OutputSection &osec) {
648   unsigned rank = osec.partition * RF_PARTITION;
649 
650   // We want to put section specified by -T option first, so we
651   // can start assigning VA starting from them later.
652   if (config->sectionStartMap.count(osec.name))
653     return rank;
654   rank |= RF_NOT_ADDR_SET;
655 
656   // Allocatable sections go first to reduce the total PT_LOAD size and
657   // so debug info doesn't change addresses in actual code.
658   if (!(osec.flags & SHF_ALLOC))
659     return rank | RF_NOT_ALLOC;
660 
661   // Sort sections based on their access permission in the following
662   // order: R, RX, RXW, RW(RELRO), RW(non-RELRO).
663   //
664   // Read-only sections come first such that they go in the PT_LOAD covering the
665   // program headers at the start of the file.
666   //
667   // The layout for writable sections is PT_LOAD(PT_GNU_RELRO(.data.rel.ro
668   // .bss.rel.ro) | .data .bss), where | marks where page alignment happens.
669   // An alternative ordering is PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro
670   // .bss.rel.ro) | .bss), but it may waste more bytes due to 2 alignment
671   // places.
672   bool isExec = osec.flags & SHF_EXECINSTR;
673   bool isWrite = osec.flags & SHF_WRITE;
674 
675   if (!isWrite && !isExec) {
676     // Among PROGBITS sections, place .lrodata further from .text.
677     // For -z lrodata-after-bss, place .lrodata after .lbss like GNU ld. This
678     // layout has one extra PT_LOAD, but alleviates relocation overflow
679     // pressure for absolute relocations referencing small data from -fno-pic
680     // relocatable files.
681     if (osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64)
682       rank |= config->zLrodataAfterBss ? RF_LARGE_ALT : 0;
683     else
684       rank |= config->zLrodataAfterBss ? 0 : RF_LARGE;
685 
686     if (osec.type == SHT_LLVM_PART_EHDR)
687       ;
688     else if (osec.type == SHT_LLVM_PART_PHDR)
689       rank |= 1;
690     else if (osec.name == ".interp")
691       rank |= 2;
692     // Put .note sections at the beginning so that they are likely to be
693     // included in a truncate core file. In particular, .note.gnu.build-id, if
694     // available, can identify the object file.
695     else if (osec.type == SHT_NOTE)
696       rank |= 3;
697     // Make PROGBITS sections (e.g .rodata .eh_frame) closer to .text to
698     // alleviate relocation overflow pressure. Large special sections such as
699     // .dynstr and .dynsym can be away from .text.
700     else if (osec.type != SHT_PROGBITS)
701       rank |= 4;
702     else
703       rank |= RF_RODATA;
704   } else if (isExec) {
705     rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
706   } else {
707     rank |= RF_WRITE;
708     // The TLS initialization block needs to be a single contiguous block. Place
709     // TLS sections directly before the other RELRO sections.
710     if (!(osec.flags & SHF_TLS))
711       rank |= RF_NOT_TLS;
712     if (isRelroSection(&osec))
713       osec.relro = true;
714     else
715       rank |= RF_NOT_RELRO;
716     // Place .ldata and .lbss after .bss. Making .bss closer to .text
717     // alleviates relocation overflow pressure.
718     // For -z lrodata-after-bss, place .lbss/.lrodata/.ldata after .bss.
719     // .bss/.lbss being adjacent reuses the NOBITS size optimization.
720     if (osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64) {
721       rank |= config->zLrodataAfterBss
722                   ? (osec.type == SHT_NOBITS ? 1 : RF_LARGE_ALT)
723                   : RF_LARGE;
724     }
725   }
726 
727   // Within TLS sections, or within other RelRo sections, or within non-RelRo
728   // sections, place non-NOBITS sections first.
729   if (osec.type == SHT_NOBITS)
730     rank |= RF_BSS;
731 
732   // Some architectures have additional ordering restrictions for sections
733   // within the same PT_LOAD.
734   if (config->emachine == EM_PPC64) {
735     // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
736     // that we would like to make sure appear is a specific order to maximize
737     // their coverage by a single signed 16-bit offset from the TOC base
738     // pointer.
739     StringRef name = osec.name;
740     if (name == ".got")
741       rank |= 1;
742     else if (name == ".toc")
743       rank |= 2;
744   }
745 
746   if (config->emachine == EM_MIPS) {
747     if (osec.name != ".got")
748       rank |= 1;
749     // All sections with SHF_MIPS_GPREL flag should be grouped together
750     // because data in these sections is addressable with a gp relative address.
751     if (osec.flags & SHF_MIPS_GPREL)
752       rank |= 2;
753   }
754 
755   if (config->emachine == EM_RISCV) {
756     // .sdata and .sbss are placed closer to make GP relaxation more profitable
757     // and match GNU ld.
758     StringRef name = osec.name;
759     if (name == ".sdata" || (osec.type == SHT_NOBITS && name != ".sbss"))
760       rank |= 1;
761   }
762 
763   return rank;
764 }
765 
compareSections(const SectionCommand * aCmd,const SectionCommand * bCmd)766 static bool compareSections(const SectionCommand *aCmd,
767                             const SectionCommand *bCmd) {
768   const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;
769   const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;
770 
771   if (a->sortRank != b->sortRank)
772     return a->sortRank < b->sortRank;
773 
774   if (!(a->sortRank & RF_NOT_ADDR_SET))
775     return config->sectionStartMap.lookup(a->name) <
776            config->sectionStartMap.lookup(b->name);
777   return false;
778 }
779 
add(OutputSection * sec)780 void PhdrEntry::add(OutputSection *sec) {
781   lastSec = sec;
782   if (!firstSec)
783     firstSec = sec;
784   p_align = std::max(p_align, sec->addralign);
785   if (p_type == PT_LOAD)
786     sec->ptLoad = this;
787 }
788 
789 // A statically linked position-dependent executable should only contain
790 // IRELATIVE relocations and no other dynamic relocations. Encapsulation symbols
791 // __rel[a]_iplt_{start,end} will be defined for .rel[a].dyn, to be
792 // processed by the libc runtime. Other executables or DSOs use dynamic tags
793 // instead.
addRelIpltSymbols()794 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
795   if (config->isPic)
796     return;
797 
798   // __rela_iplt_{start,end} are initially defined relative to dummy section 0.
799   // We'll override Out::elfHeader with relaDyn later when we are sure that
800   // .rela.dyn will be present in the output.
801   std::string name = config->isRela ? "__rela_iplt_start" : "__rel_iplt_start";
802   ElfSym::relaIpltStart =
803       addOptionalRegular(name, Out::elfHeader, 0, STV_HIDDEN);
804   name.replace(name.size() - 5, 5, "end");
805   ElfSym::relaIpltEnd = addOptionalRegular(name, Out::elfHeader, 0, STV_HIDDEN);
806 }
807 
808 // This function generates assignments for predefined symbols (e.g. _end or
809 // _etext) and inserts them into the commands sequence to be processed at the
810 // appropriate time. This ensures that the value is going to be correct by the
811 // time any references to these symbols are processed and is equivalent to
812 // defining these symbols explicitly in the linker script.
setReservedSymbolSections()813 template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
814   if (ElfSym::globalOffsetTable) {
815     // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
816     // to the start of the .got or .got.plt section.
817     InputSection *sec = in.gotPlt.get();
818     if (!target->gotBaseSymInGotPlt)
819       sec = in.mipsGot ? cast<InputSection>(in.mipsGot.get())
820                        : cast<InputSection>(in.got.get());
821     ElfSym::globalOffsetTable->section = sec;
822   }
823 
824   // .rela_iplt_{start,end} mark the start and the end of .rel[a].dyn.
825   if (ElfSym::relaIpltStart && mainPart->relaDyn->isNeeded()) {
826     ElfSym::relaIpltStart->section = mainPart->relaDyn.get();
827     ElfSym::relaIpltEnd->section = mainPart->relaDyn.get();
828     ElfSym::relaIpltEnd->value = mainPart->relaDyn->getSize();
829   }
830 
831   PhdrEntry *last = nullptr;
832   OutputSection *lastRO = nullptr;
833   auto isLarge = [](OutputSection *osec) {
834     return config->emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
835   };
836   for (Partition &part : partitions) {
837     for (PhdrEntry *p : part.phdrs) {
838       if (p->p_type != PT_LOAD)
839         continue;
840       last = p;
841       if (!(p->p_flags & PF_W) && p->lastSec && !isLarge(p->lastSec))
842         lastRO = p->lastSec;
843     }
844   }
845 
846   if (lastRO) {
847     // _etext is the first location after the last read-only loadable segment
848     // that does not contain large sections.
849     if (ElfSym::etext1)
850       ElfSym::etext1->section = lastRO;
851     if (ElfSym::etext2)
852       ElfSym::etext2->section = lastRO;
853   }
854 
855   if (last) {
856     // _edata points to the end of the last non-large mapped initialized
857     // section.
858     OutputSection *edata = nullptr;
859     for (OutputSection *os : outputSections) {
860       if (os->type != SHT_NOBITS && !isLarge(os))
861         edata = os;
862       if (os == last->lastSec)
863         break;
864     }
865 
866     if (ElfSym::edata1)
867       ElfSym::edata1->section = edata;
868     if (ElfSym::edata2)
869       ElfSym::edata2->section = edata;
870 
871     // _end is the first location after the uninitialized data region.
872     if (ElfSym::end1)
873       ElfSym::end1->section = last->lastSec;
874     if (ElfSym::end2)
875       ElfSym::end2->section = last->lastSec;
876   }
877 
878   if (ElfSym::bss) {
879     // On RISC-V, set __bss_start to the start of .sbss if present.
880     OutputSection *sbss =
881         config->emachine == EM_RISCV ? findSection(".sbss") : nullptr;
882     ElfSym::bss->section = sbss ? sbss : findSection(".bss");
883   }
884 
885   // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
886   // be equal to the _gp symbol's value.
887   if (ElfSym::mipsGp) {
888     // Find GP-relative section with the lowest address
889     // and use this address to calculate default _gp value.
890     for (OutputSection *os : outputSections) {
891       if (os->flags & SHF_MIPS_GPREL) {
892         ElfSym::mipsGp->section = os;
893         ElfSym::mipsGp->value = 0x7ff0;
894         break;
895       }
896     }
897   }
898 }
899 
900 // We want to find how similar two ranks are.
901 // The more branches in getSectionRank that match, the more similar they are.
902 // Since each branch corresponds to a bit flag, we can just use
903 // countLeadingZeros.
getRankProximity(OutputSection * a,SectionCommand * b)904 static int getRankProximity(OutputSection *a, SectionCommand *b) {
905   auto *osd = dyn_cast<OutputDesc>(b);
906   return (osd && osd->osec.hasInputSections)
907              ? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank)
908              : -1;
909 }
910 
911 // When placing orphan sections, we want to place them after symbol assignments
912 // so that an orphan after
913 //   begin_foo = .;
914 //   foo : { *(foo) }
915 //   end_foo = .;
916 // doesn't break the intended meaning of the begin/end symbols.
917 // We don't want to go over sections since findOrphanPos is the
918 // one in charge of deciding the order of the sections.
919 // We don't want to go over changes to '.', since doing so in
920 //  rx_sec : { *(rx_sec) }
921 //  . = ALIGN(0x1000);
922 //  /* The RW PT_LOAD starts here*/
923 //  rw_sec : { *(rw_sec) }
924 // would mean that the RW PT_LOAD would become unaligned.
shouldSkip(SectionCommand * cmd)925 static bool shouldSkip(SectionCommand *cmd) {
926   if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
927     return assign->name != ".";
928   return false;
929 }
930 
931 // We want to place orphan sections so that they share as much
932 // characteristics with their neighbors as possible. For example, if
933 // both are rw, or both are tls.
934 static SmallVectorImpl<SectionCommand *>::iterator
findOrphanPos(SmallVectorImpl<SectionCommand * >::iterator b,SmallVectorImpl<SectionCommand * >::iterator e)935 findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
936               SmallVectorImpl<SectionCommand *>::iterator e) {
937   // Place non-alloc orphan sections at the end. This matches how we assign file
938   // offsets to non-alloc sections.
939   OutputSection *sec = &cast<OutputDesc>(*e)->osec;
940   if (!(sec->flags & SHF_ALLOC))
941     return e;
942 
943   // As a special case, place .relro_padding before the SymbolAssignment using
944   // DATA_SEGMENT_RELRO_END, if present.
945   if (in.relroPadding && sec == in.relroPadding->getParent()) {
946     auto i = std::find_if(b, e, [=](SectionCommand *a) {
947       if (auto *assign = dyn_cast<SymbolAssignment>(a))
948         return assign->dataSegmentRelroEnd;
949       return false;
950     });
951     if (i != e)
952       return i;
953   }
954 
955   // Find the most similar output section as the anchor. Rank Proximity is a
956   // value in the range [-1, 32] where [0, 32] indicates potential anchors (0:
957   // least similar; 32: identical). -1 means not an anchor.
958   //
959   // In the event of proximity ties, we select the first or last section
960   // depending on whether the orphan's rank is smaller.
961   int maxP = 0;
962   auto i = e;
963   for (auto j = b; j != e; ++j) {
964     int p = getRankProximity(sec, *j);
965     if (p > maxP ||
966         (p == maxP && cast<OutputDesc>(*j)->osec.sortRank <= sec->sortRank)) {
967       maxP = p;
968       i = j;
969     }
970   }
971   if (i == e)
972     return e;
973 
974   auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
975     auto *osd = dyn_cast<OutputDesc>(cmd);
976     return osd && osd->osec.hasInputSections;
977   };
978 
979   // Then, scan backward or forward through the script for a suitable insertion
980   // point. If i's rank is larger, the orphan section can be placed before i.
981   //
982   // However, don't do this if custom program headers are defined. Otherwise,
983   // adding the orphan to a previous segment can change its flags, for example,
984   // making a read-only segment writable. If memory regions are defined, an
985   // orphan section should continue the same region as the found section to
986   // better resemble the behavior of GNU ld.
987   bool mustAfter = script->hasPhdrsCommands() || !script->memoryRegions.empty();
988   if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {
989     for (auto j = ++i; j != e; ++j) {
990       if (!isOutputSecWithInputSections(*j))
991         continue;
992       if (getRankProximity(sec, *j) != maxP)
993         break;
994       i = j + 1;
995     }
996   } else {
997     for (; i != b; --i)
998       if (isOutputSecWithInputSections(i[-1]))
999         break;
1000   }
1001 
1002   // As a special case, if the orphan section is the last section, put
1003   // it at the very end, past any other commands.
1004   // This matches bfd's behavior and is convenient when the linker script fully
1005   // specifies the start of the file, but doesn't care about the end (the non
1006   // alloc sections for example).
1007   if (std::find_if(i, e, isOutputSecWithInputSections) == e)
1008     return e;
1009 
1010   while (i != e && shouldSkip(*i))
1011     ++i;
1012   return i;
1013 }
1014 
1015 // Adds random priorities to sections not already in the map.
maybeShuffle(DenseMap<const InputSectionBase *,int> & order)1016 static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) {
1017   if (config->shuffleSections.empty())
1018     return;
1019 
1020   SmallVector<InputSectionBase *, 0> matched, sections = ctx.inputSections;
1021   matched.reserve(sections.size());
1022   for (const auto &patAndSeed : config->shuffleSections) {
1023     matched.clear();
1024     for (InputSectionBase *sec : sections)
1025       if (patAndSeed.first.match(sec->name))
1026         matched.push_back(sec);
1027     const uint32_t seed = patAndSeed.second;
1028     if (seed == UINT32_MAX) {
1029       // If --shuffle-sections <section-glob>=-1, reverse the section order. The
1030       // section order is stable even if the number of sections changes. This is
1031       // useful to catch issues like static initialization order fiasco
1032       // reliably.
1033       std::reverse(matched.begin(), matched.end());
1034     } else {
1035       std::mt19937 g(seed ? seed : std::random_device()());
1036       llvm::shuffle(matched.begin(), matched.end(), g);
1037     }
1038     size_t i = 0;
1039     for (InputSectionBase *&sec : sections)
1040       if (patAndSeed.first.match(sec->name))
1041         sec = matched[i++];
1042   }
1043 
1044   // Existing priorities are < 0, so use priorities >= 0 for the missing
1045   // sections.
1046   int prio = 0;
1047   for (InputSectionBase *sec : sections) {
1048     if (order.try_emplace(sec, prio).second)
1049       ++prio;
1050   }
1051 }
1052 
1053 // Builds section order for handling --symbol-ordering-file.
buildSectionOrder()1054 static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
1055   DenseMap<const InputSectionBase *, int> sectionOrder;
1056   // Use the rarely used option --call-graph-ordering-file to sort sections.
1057   if (!config->callGraphProfile.empty())
1058     return computeCallGraphProfileOrder();
1059 
1060   if (config->symbolOrderingFile.empty())
1061     return sectionOrder;
1062 
1063   struct SymbolOrderEntry {
1064     int priority;
1065     bool present;
1066   };
1067 
1068   // Build a map from symbols to their priorities. Symbols that didn't
1069   // appear in the symbol ordering file have the lowest priority 0.
1070   // All explicitly mentioned symbols have negative (higher) priorities.
1071   DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
1072   int priority = -config->symbolOrderingFile.size();
1073   for (StringRef s : config->symbolOrderingFile)
1074     symbolOrder.insert({CachedHashStringRef(s), {priority++, false}});
1075 
1076   // Build a map from sections to their priorities.
1077   auto addSym = [&](Symbol &sym) {
1078     auto it = symbolOrder.find(CachedHashStringRef(sym.getName()));
1079     if (it == symbolOrder.end())
1080       return;
1081     SymbolOrderEntry &ent = it->second;
1082     ent.present = true;
1083 
1084     maybeWarnUnorderableSymbol(&sym);
1085 
1086     if (auto *d = dyn_cast<Defined>(&sym)) {
1087       if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
1088         int &priority = sectionOrder[cast<InputSectionBase>(sec)];
1089         priority = std::min(priority, ent.priority);
1090       }
1091     }
1092   };
1093 
1094   // We want both global and local symbols. We get the global ones from the
1095   // symbol table and iterate the object files for the local ones.
1096   for (Symbol *sym : symtab.getSymbols())
1097     addSym(*sym);
1098 
1099   for (ELFFileBase *file : ctx.objectFiles)
1100     for (Symbol *sym : file->getLocalSymbols())
1101       addSym(*sym);
1102 
1103   if (config->warnSymbolOrdering)
1104     for (auto orderEntry : symbolOrder)
1105       if (!orderEntry.second.present)
1106         warn("symbol ordering file: no such symbol: " + orderEntry.first.val());
1107 
1108   return sectionOrder;
1109 }
1110 
1111 // Sorts the sections in ISD according to the provided section order.
1112 static void
sortISDBySectionOrder(InputSectionDescription * isd,const DenseMap<const InputSectionBase *,int> & order,bool executableOutputSection)1113 sortISDBySectionOrder(InputSectionDescription *isd,
1114                       const DenseMap<const InputSectionBase *, int> &order,
1115                       bool executableOutputSection) {
1116   SmallVector<InputSection *, 0> unorderedSections;
1117   SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
1118   uint64_t unorderedSize = 0;
1119   uint64_t totalSize = 0;
1120 
1121   for (InputSection *isec : isd->sections) {
1122     if (executableOutputSection)
1123       totalSize += isec->getSize();
1124     auto i = order.find(isec);
1125     if (i == order.end()) {
1126       unorderedSections.push_back(isec);
1127       unorderedSize += isec->getSize();
1128       continue;
1129     }
1130     orderedSections.push_back({isec, i->second});
1131   }
1132   llvm::sort(orderedSections, llvm::less_second());
1133 
1134   // Find an insertion point for the ordered section list in the unordered
1135   // section list. On targets with limited-range branches, this is the mid-point
1136   // of the unordered section list. This decreases the likelihood that a range
1137   // extension thunk will be needed to enter or exit the ordered region. If the
1138   // ordered section list is a list of hot functions, we can generally expect
1139   // the ordered functions to be called more often than the unordered functions,
1140   // making it more likely that any particular call will be within range, and
1141   // therefore reducing the number of thunks required.
1142   //
1143   // For example, imagine that you have 8MB of hot code and 32MB of cold code.
1144   // If the layout is:
1145   //
1146   // 8MB hot
1147   // 32MB cold
1148   //
1149   // only the first 8-16MB of the cold code (depending on which hot function it
1150   // is actually calling) can call the hot code without a range extension thunk.
1151   // However, if we use this layout:
1152   //
1153   // 16MB cold
1154   // 8MB hot
1155   // 16MB cold
1156   //
1157   // both the last 8-16MB of the first block of cold code and the first 8-16MB
1158   // of the second block of cold code can call the hot code without a thunk. So
1159   // we effectively double the amount of code that could potentially call into
1160   // the hot code without a thunk.
1161   //
1162   // The above is not necessary if total size of input sections in this "isd"
1163   // is small. Note that we assume all input sections are executable if the
1164   // output section is executable (which is not always true but supposed to
1165   // cover most cases).
1166   size_t insPt = 0;
1167   if (executableOutputSection && !orderedSections.empty() &&
1168       target->getThunkSectionSpacing() &&
1169       totalSize >= target->getThunkSectionSpacing()) {
1170     uint64_t unorderedPos = 0;
1171     for (; insPt != unorderedSections.size(); ++insPt) {
1172       unorderedPos += unorderedSections[insPt]->getSize();
1173       if (unorderedPos > unorderedSize / 2)
1174         break;
1175     }
1176   }
1177 
1178   isd->sections.clear();
1179   for (InputSection *isec : ArrayRef(unorderedSections).slice(0, insPt))
1180     isd->sections.push_back(isec);
1181   for (std::pair<InputSection *, int> p : orderedSections)
1182     isd->sections.push_back(p.first);
1183   for (InputSection *isec : ArrayRef(unorderedSections).slice(insPt))
1184     isd->sections.push_back(isec);
1185 }
1186 
sortSection(OutputSection & osec,const DenseMap<const InputSectionBase *,int> & order)1187 static void sortSection(OutputSection &osec,
1188                         const DenseMap<const InputSectionBase *, int> &order) {
1189   StringRef name = osec.name;
1190 
1191   // Never sort these.
1192   if (name == ".init" || name == ".fini")
1193     return;
1194 
1195   // Sort input sections by priority using the list provided by
1196   // --symbol-ordering-file or --shuffle-sections=. This is a least significant
1197   // digit radix sort. The sections may be sorted stably again by a more
1198   // significant key.
1199   if (!order.empty())
1200     for (SectionCommand *b : osec.commands)
1201       if (auto *isd = dyn_cast<InputSectionDescription>(b))
1202         sortISDBySectionOrder(isd, order, osec.flags & SHF_EXECINSTR);
1203 
1204   if (script->hasSectionsCommand)
1205     return;
1206 
1207   if (name == ".init_array" || name == ".fini_array") {
1208     osec.sortInitFini();
1209   } else if (name == ".ctors" || name == ".dtors") {
1210     osec.sortCtorsDtors();
1211   } else if (config->emachine == EM_PPC64 && name == ".toc") {
1212     // .toc is allocated just after .got and is accessed using GOT-relative
1213     // relocations. Object files compiled with small code model have an
1214     // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1215     // To reduce the risk of relocation overflow, .toc contents are sorted so
1216     // that sections having smaller relocation offsets are at beginning of .toc
1217     assert(osec.commands.size() == 1);
1218     auto *isd = cast<InputSectionDescription>(osec.commands[0]);
1219     llvm::stable_sort(isd->sections,
1220                       [](const InputSection *a, const InputSection *b) -> bool {
1221                         return a->file->ppc64SmallCodeModelTocRelocs &&
1222                                !b->file->ppc64SmallCodeModelTocRelocs;
1223                       });
1224   }
1225 }
1226 
1227 // If no layout was provided by linker script, we want to apply default
1228 // sorting for special input sections. This also handles --symbol-ordering-file.
sortInputSections()1229 template <class ELFT> void Writer<ELFT>::sortInputSections() {
1230   // Build the order once since it is expensive.
1231   DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1232   maybeShuffle(order);
1233   for (SectionCommand *cmd : script->sectionCommands)
1234     if (auto *osd = dyn_cast<OutputDesc>(cmd))
1235       sortSection(osd->osec, order);
1236 }
1237 
sortSections()1238 template <class ELFT> void Writer<ELFT>::sortSections() {
1239   llvm::TimeTraceScope timeScope("Sort sections");
1240 
1241   // Don't sort if using -r. It is not necessary and we want to preserve the
1242   // relative order for SHF_LINK_ORDER sections.
1243   if (config->relocatable) {
1244     script->adjustOutputSections();
1245     return;
1246   }
1247 
1248   sortInputSections();
1249 
1250   for (SectionCommand *cmd : script->sectionCommands)
1251     if (auto *osd = dyn_cast<OutputDesc>(cmd))
1252       osd->osec.sortRank = getSectionRank(osd->osec);
1253   if (!script->hasSectionsCommand) {
1254     // OutputDescs are mostly contiguous, but may be interleaved with
1255     // SymbolAssignments in the presence of INSERT commands.
1256     auto mid = std::stable_partition(
1257         script->sectionCommands.begin(), script->sectionCommands.end(),
1258         [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); });
1259     std::stable_sort(script->sectionCommands.begin(), mid, compareSections);
1260   }
1261 
1262   // Process INSERT commands and update output section attributes. From this
1263   // point onwards the order of script->sectionCommands is fixed.
1264   script->processInsertCommands();
1265   script->adjustOutputSections();
1266 
1267   if (script->hasSectionsCommand)
1268     sortOrphanSections();
1269 
1270   script->adjustSectionsAfterSorting();
1271 }
1272 
sortOrphanSections()1273 template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
1274   // Orphan sections are sections present in the input files which are
1275   // not explicitly placed into the output file by the linker script.
1276   //
1277   // The sections in the linker script are already in the correct
1278   // order. We have to figuere out where to insert the orphan
1279   // sections.
1280   //
1281   // The order of the sections in the script is arbitrary and may not agree with
1282   // compareSections. This means that we cannot easily define a strict weak
1283   // ordering. To see why, consider a comparison of a section in the script and
1284   // one not in the script. We have a two simple options:
1285   // * Make them equivalent (a is not less than b, and b is not less than a).
1286   //   The problem is then that equivalence has to be transitive and we can
1287   //   have sections a, b and c with only b in a script and a less than c
1288   //   which breaks this property.
1289   // * Use compareSectionsNonScript. Given that the script order doesn't have
1290   //   to match, we can end up with sections a, b, c, d where b and c are in the
1291   //   script and c is compareSectionsNonScript less than b. In which case d
1292   //   can be equivalent to c, a to b and d < a. As a concrete example:
1293   //   .a (rx) # not in script
1294   //   .b (rx) # in script
1295   //   .c (ro) # in script
1296   //   .d (ro) # not in script
1297   //
1298   // The way we define an order then is:
1299   // *  Sort only the orphan sections. They are in the end right now.
1300   // *  Move each orphan section to its preferred position. We try
1301   //    to put each section in the last position where it can share
1302   //    a PT_LOAD.
1303   //
1304   // There is some ambiguity as to where exactly a new entry should be
1305   // inserted, because Commands contains not only output section
1306   // commands but also other types of commands such as symbol assignment
1307   // expressions. There's no correct answer here due to the lack of the
1308   // formal specification of the linker script. We use heuristics to
1309   // determine whether a new output command should be added before or
1310   // after another commands. For the details, look at shouldSkip
1311   // function.
1312 
1313   auto i = script->sectionCommands.begin();
1314   auto e = script->sectionCommands.end();
1315   auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {
1316     if (auto *osd = dyn_cast<OutputDesc>(cmd))
1317       return osd->osec.sectionIndex == UINT32_MAX;
1318     return false;
1319   });
1320 
1321   // Sort the orphan sections.
1322   std::stable_sort(nonScriptI, e, compareSections);
1323 
1324   // As a horrible special case, skip the first . assignment if it is before any
1325   // section. We do this because it is common to set a load address by starting
1326   // the script with ". = 0xabcd" and the expectation is that every section is
1327   // after that.
1328   auto firstSectionOrDotAssignment =
1329       std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });
1330   if (firstSectionOrDotAssignment != e &&
1331       isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1332     ++firstSectionOrDotAssignment;
1333   i = firstSectionOrDotAssignment;
1334 
1335   while (nonScriptI != e) {
1336     auto pos = findOrphanPos(i, nonScriptI);
1337     OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;
1338 
1339     // As an optimization, find all sections with the same sort rank
1340     // and insert them with one rotate.
1341     unsigned rank = orphan->sortRank;
1342     auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {
1343       return cast<OutputDesc>(cmd)->osec.sortRank != rank;
1344     });
1345     std::rotate(pos, nonScriptI, end);
1346     nonScriptI = end;
1347   }
1348 }
1349 
compareByFilePosition(InputSection * a,InputSection * b)1350 static bool compareByFilePosition(InputSection *a, InputSection *b) {
1351   InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1352   InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1353   // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1354   // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1355   if (!la || !lb)
1356     return la && !lb;
1357   OutputSection *aOut = la->getParent();
1358   OutputSection *bOut = lb->getParent();
1359 
1360   if (aOut == bOut)
1361     return la->outSecOff < lb->outSecOff;
1362   if (aOut->addr == bOut->addr)
1363     return aOut->sectionIndex < bOut->sectionIndex;
1364   return aOut->addr < bOut->addr;
1365 }
1366 
resolveShfLinkOrder()1367 template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1368   llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
1369   for (OutputSection *sec : outputSections) {
1370     if (!(sec->flags & SHF_LINK_ORDER))
1371       continue;
1372 
1373     // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1374     // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1375     if (!config->relocatable && config->emachine == EM_ARM &&
1376         sec->type == SHT_ARM_EXIDX)
1377       continue;
1378 
1379     // Link order may be distributed across several InputSectionDescriptions.
1380     // Sorting is performed separately.
1381     SmallVector<InputSection **, 0> scriptSections;
1382     SmallVector<InputSection *, 0> sections;
1383     for (SectionCommand *cmd : sec->commands) {
1384       auto *isd = dyn_cast<InputSectionDescription>(cmd);
1385       if (!isd)
1386         continue;
1387       bool hasLinkOrder = false;
1388       scriptSections.clear();
1389       sections.clear();
1390       for (InputSection *&isec : isd->sections) {
1391         if (isec->flags & SHF_LINK_ORDER) {
1392           InputSection *link = isec->getLinkOrderDep();
1393           if (link && !link->getParent())
1394             error(toString(isec) + ": sh_link points to discarded section " +
1395                   toString(link));
1396           hasLinkOrder = true;
1397         }
1398         scriptSections.push_back(&isec);
1399         sections.push_back(isec);
1400       }
1401       if (hasLinkOrder && errorCount() == 0) {
1402         llvm::stable_sort(sections, compareByFilePosition);
1403         for (int i = 0, n = sections.size(); i != n; ++i)
1404           *scriptSections[i] = sections[i];
1405       }
1406     }
1407   }
1408 }
1409 
finalizeSynthetic(SyntheticSection * sec)1410 static void finalizeSynthetic(SyntheticSection *sec) {
1411   if (sec && sec->isNeeded() && sec->getParent()) {
1412     llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
1413     sec->finalizeContents();
1414   }
1415 }
1416 
1417 // We need to generate and finalize the content that depends on the address of
1418 // InputSections. As the generation of the content may also alter InputSection
1419 // addresses we must converge to a fixed point. We do that here. See the comment
1420 // in Writer<ELFT>::finalizeSections().
finalizeAddressDependentContent()1421 template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1422   llvm::TimeTraceScope timeScope("Finalize address dependent content");
1423   ThunkCreator tc;
1424   AArch64Err843419Patcher a64p;
1425   ARMErr657417Patcher a32p;
1426   script->assignAddresses();
1427 
1428   // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
1429   // do require the relative addresses of OutputSections because linker scripts
1430   // can assign Virtual Addresses to OutputSections that are not monotonically
1431   // increasing. Anything here must be repeatable, since spilling may change
1432   // section order.
1433   const auto finalizeOrderDependentContent = [this] {
1434     for (Partition &part : partitions)
1435       finalizeSynthetic(part.armExidx.get());
1436     resolveShfLinkOrder();
1437   };
1438   finalizeOrderDependentContent();
1439 
1440   // Converts call x@GDPLT to call __tls_get_addr
1441   if (config->emachine == EM_HEXAGON)
1442     hexagonTLSSymbolUpdate(outputSections);
1443 
1444   uint32_t pass = 0, assignPasses = 0;
1445   for (;;) {
1446     bool changed = target->needsThunks ? tc.createThunks(pass, outputSections)
1447                                        : target->relaxOnce(pass);
1448     bool spilled = script->spillSections();
1449     changed |= spilled;
1450     ++pass;
1451 
1452     // With Thunk Size much smaller than branch range we expect to
1453     // converge quickly; if we get to 30 something has gone wrong.
1454     if (changed && pass >= 30) {
1455       error(target->needsThunks ? "thunk creation not converged"
1456                                 : "relaxation not converged");
1457       break;
1458     }
1459 
1460     if (config->fixCortexA53Errata843419) {
1461       if (changed)
1462         script->assignAddresses();
1463       changed |= a64p.createFixes();
1464     }
1465     if (config->fixCortexA8) {
1466       if (changed)
1467         script->assignAddresses();
1468       changed |= a32p.createFixes();
1469     }
1470 
1471     finalizeSynthetic(in.got.get());
1472     if (in.mipsGot)
1473       in.mipsGot->updateAllocSize();
1474 
1475     for (Partition &part : partitions) {
1476       // The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
1477       // encode the signing schema. We've put relocations in .relr.auth.dyn
1478       // during RelocationScanner::processAux, but the target VA for some of
1479       // them might be wider than 32 bits. We can only know the final VA at this
1480       // point, so move relocations with large values from .relr.auth.dyn to
1481       // .rela.dyn. See also AArch64::relocate.
1482       if (part.relrAuthDyn) {
1483         auto it = llvm::remove_if(
1484             part.relrAuthDyn->relocs, [&part](const RelativeReloc &elem) {
1485               const Relocation &reloc = elem.inputSec->relocs()[elem.relocIdx];
1486               if (isInt<32>(reloc.sym->getVA(reloc.addend)))
1487                 return false;
1488               part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec,
1489                                       reloc.offset,
1490                                       DynamicReloc::AddendOnlyWithTargetVA,
1491                                       *reloc.sym, reloc.addend, R_ABS});
1492               return true;
1493             });
1494         changed |= (it != part.relrAuthDyn->relocs.end());
1495         part.relrAuthDyn->relocs.erase(it, part.relrAuthDyn->relocs.end());
1496       }
1497       if (part.relaDyn)
1498         changed |= part.relaDyn->updateAllocSize();
1499       if (part.relrDyn)
1500         changed |= part.relrDyn->updateAllocSize();
1501       if (part.relrAuthDyn)
1502         changed |= part.relrAuthDyn->updateAllocSize();
1503       if (part.memtagGlobalDescriptors)
1504         changed |= part.memtagGlobalDescriptors->updateAllocSize();
1505     }
1506 
1507     std::pair<const OutputSection *, const Defined *> changes =
1508         script->assignAddresses();
1509     if (!changed) {
1510       // Some symbols may be dependent on section addresses. When we break the
1511       // loop, the symbol values are finalized because a previous
1512       // assignAddresses() finalized section addresses.
1513       if (!changes.first && !changes.second)
1514         break;
1515       if (++assignPasses == 5) {
1516         if (changes.first)
1517           errorOrWarn("address (0x" + Twine::utohexstr(changes.first->addr) +
1518                       ") of section '" + changes.first->name +
1519                       "' does not converge");
1520         if (changes.second)
1521           errorOrWarn("assignment to symbol " + toString(*changes.second) +
1522                       " does not converge");
1523         break;
1524       }
1525     } else if (spilled) {
1526       // Spilling can change relative section order.
1527       finalizeOrderDependentContent();
1528     }
1529   }
1530   if (!config->relocatable)
1531     target->finalizeRelax(pass);
1532 
1533   if (config->relocatable)
1534     for (OutputSection *sec : outputSections)
1535       sec->addr = 0;
1536 
1537   // If addrExpr is set, the address may not be a multiple of the alignment.
1538   // Warn because this is error-prone.
1539   for (SectionCommand *cmd : script->sectionCommands)
1540     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
1541       OutputSection *osec = &osd->osec;
1542       if (osec->addr % osec->addralign != 0)
1543         warn("address (0x" + Twine::utohexstr(osec->addr) + ") of section " +
1544              osec->name + " is not a multiple of alignment (" +
1545              Twine(osec->addralign) + ")");
1546     }
1547 
1548   // Sizes are no longer allowed to grow, so all allowable spills have been
1549   // taken. Remove any leftover potential spills.
1550   script->erasePotentialSpillSections();
1551 }
1552 
1553 // If Input Sections have been shrunk (basic block sections) then
1554 // update symbol values and sizes associated with these sections.  With basic
1555 // block sections, input sections can shrink when the jump instructions at
1556 // the end of the section are relaxed.
fixSymbolsAfterShrinking()1557 static void fixSymbolsAfterShrinking() {
1558   for (InputFile *File : ctx.objectFiles) {
1559     parallelForEach(File->getSymbols(), [&](Symbol *Sym) {
1560       auto *def = dyn_cast<Defined>(Sym);
1561       if (!def)
1562         return;
1563 
1564       const SectionBase *sec = def->section;
1565       if (!sec)
1566         return;
1567 
1568       const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec);
1569       if (!inputSec || !inputSec->bytesDropped)
1570         return;
1571 
1572       const size_t OldSize = inputSec->content().size();
1573       const size_t NewSize = OldSize - inputSec->bytesDropped;
1574 
1575       if (def->value > NewSize && def->value <= OldSize) {
1576         LLVM_DEBUG(llvm::dbgs()
1577                    << "Moving symbol " << Sym->getName() << " from "
1578                    << def->value << " to "
1579                    << def->value - inputSec->bytesDropped << " bytes\n");
1580         def->value -= inputSec->bytesDropped;
1581         return;
1582       }
1583 
1584       if (def->value + def->size > NewSize && def->value <= OldSize &&
1585           def->value + def->size <= OldSize) {
1586         LLVM_DEBUG(llvm::dbgs()
1587                    << "Shrinking symbol " << Sym->getName() << " from "
1588                    << def->size << " to " << def->size - inputSec->bytesDropped
1589                    << " bytes\n");
1590         def->size -= inputSec->bytesDropped;
1591       }
1592     });
1593   }
1594 }
1595 
1596 // If basic block sections exist, there are opportunities to delete fall thru
1597 // jumps and shrink jump instructions after basic block reordering.  This
1598 // relaxation pass does that.  It is only enabled when --optimize-bb-jumps
1599 // option is used.
optimizeBasicBlockJumps()1600 template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
1601   assert(config->optimizeBBJumps);
1602   SmallVector<InputSection *, 0> storage;
1603 
1604   script->assignAddresses();
1605   // For every output section that has executable input sections, this
1606   // does the following:
1607   //   1. Deletes all direct jump instructions in input sections that
1608   //      jump to the following section as it is not required.
1609   //   2. If there are two consecutive jump instructions, it checks
1610   //      if they can be flipped and one can be deleted.
1611   for (OutputSection *osec : outputSections) {
1612     if (!(osec->flags & SHF_EXECINSTR))
1613       continue;
1614     ArrayRef<InputSection *> sections = getInputSections(*osec, storage);
1615     size_t numDeleted = 0;
1616     // Delete all fall through jump instructions.  Also, check if two
1617     // consecutive jump instructions can be flipped so that a fall
1618     // through jmp instruction can be deleted.
1619     for (size_t i = 0, e = sections.size(); i != e; ++i) {
1620       InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;
1621       InputSection &sec = *sections[i];
1622       numDeleted += target->deleteFallThruJmpInsn(sec, sec.file, next);
1623     }
1624     if (numDeleted > 0) {
1625       script->assignAddresses();
1626       LLVM_DEBUG(llvm::dbgs()
1627                  << "Removing " << numDeleted << " fall through jumps\n");
1628     }
1629   }
1630 
1631   fixSymbolsAfterShrinking();
1632 
1633   for (OutputSection *osec : outputSections)
1634     for (InputSection *is : getInputSections(*osec, storage))
1635       is->trim();
1636 }
1637 
1638 // In order to allow users to manipulate linker-synthesized sections,
1639 // we had to add synthetic sections to the input section list early,
1640 // even before we make decisions whether they are needed. This allows
1641 // users to write scripts like this: ".mygot : { .got }".
1642 //
1643 // Doing it has an unintended side effects. If it turns out that we
1644 // don't need a .got (for example) at all because there's no
1645 // relocation that needs a .got, we don't want to emit .got.
1646 //
1647 // To deal with the above problem, this function is called after
1648 // scanRelocations is called to remove synthetic sections that turn
1649 // out to be empty.
removeUnusedSyntheticSections()1650 static void removeUnusedSyntheticSections() {
1651   // All input synthetic sections that can be empty are placed after
1652   // all regular ones. Reverse iterate to find the first synthetic section
1653   // after a non-synthetic one which will be our starting point.
1654   auto start =
1655       llvm::find_if(llvm::reverse(ctx.inputSections), [](InputSectionBase *s) {
1656         return !isa<SyntheticSection>(s);
1657       }).base();
1658 
1659   // Remove unused synthetic sections from ctx.inputSections;
1660   DenseSet<InputSectionBase *> unused;
1661   auto end =
1662       std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) {
1663         auto *sec = cast<SyntheticSection>(s);
1664         if (sec->getParent() && sec->isNeeded())
1665           return false;
1666         // .relr.auth.dyn relocations may be moved to .rela.dyn in
1667         // finalizeAddressDependentContent, making .rela.dyn no longer empty.
1668         // Conservatively keep .rela.dyn. .relr.auth.dyn can be made empty, but
1669         // we would fail to remove it here.
1670         if (config->emachine == EM_AARCH64 && config->relrPackDynRelocs)
1671           if (auto *relSec = dyn_cast<RelocationBaseSection>(sec))
1672             if (relSec == mainPart->relaDyn.get())
1673               return false;
1674         unused.insert(sec);
1675         return true;
1676       });
1677   ctx.inputSections.erase(end, ctx.inputSections.end());
1678 
1679   // Remove unused synthetic sections from the corresponding input section
1680   // description and orphanSections.
1681   for (auto *sec : unused)
1682     if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())
1683       for (SectionCommand *cmd : osec->commands)
1684         if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
1685           llvm::erase_if(isd->sections, [&](InputSection *isec) {
1686             return unused.count(isec);
1687           });
1688   llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
1689     return unused.count(sec);
1690   });
1691 }
1692 
1693 // Create output section objects and add them to OutputSections.
finalizeSections()1694 template <class ELFT> void Writer<ELFT>::finalizeSections() {
1695   if (!config->relocatable) {
1696     Out::preinitArray = findSection(".preinit_array");
1697     Out::initArray = findSection(".init_array");
1698     Out::finiArray = findSection(".fini_array");
1699 
1700     // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1701     // symbols for sections, so that the runtime can get the start and end
1702     // addresses of each section by section name. Add such symbols.
1703     addStartEndSymbols();
1704     for (SectionCommand *cmd : script->sectionCommands)
1705       if (auto *osd = dyn_cast<OutputDesc>(cmd))
1706         addStartStopSymbols(osd->osec);
1707 
1708     // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1709     // It should be okay as no one seems to care about the type.
1710     // Even the author of gold doesn't remember why gold behaves that way.
1711     // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1712     if (mainPart->dynamic->parent) {
1713       Symbol *s = symtab.addSymbol(Defined{
1714           ctx.internalFile, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1715           /*value=*/0, /*size=*/0, mainPart->dynamic.get()});
1716       s->isUsedInRegularObj = true;
1717     }
1718 
1719     // Define __rel[a]_iplt_{start,end} symbols if needed.
1720     addRelIpltSymbols();
1721 
1722     // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
1723     // should only be defined in an executable. If .sdata does not exist, its
1724     // value/section does not matter but it has to be relative, so set its
1725     // st_shndx arbitrarily to 1 (Out::elfHeader).
1726     if (config->emachine == EM_RISCV) {
1727       ElfSym::riscvGlobalPointer = nullptr;
1728       if (!config->shared) {
1729         OutputSection *sec = findSection(".sdata");
1730         addOptionalRegular(
1731             "__global_pointer$", sec ? sec : Out::elfHeader, 0x800, STV_DEFAULT);
1732         // Set riscvGlobalPointer to be used by the optional global pointer
1733         // relaxation.
1734         if (config->relaxGP) {
1735           Symbol *s = symtab.find("__global_pointer$");
1736           if (s && s->isDefined())
1737             ElfSym::riscvGlobalPointer = cast<Defined>(s);
1738         }
1739       }
1740     }
1741 
1742     if (config->emachine == EM_386 || config->emachine == EM_X86_64) {
1743       // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
1744       // way that:
1745       //
1746       // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
1747       // computes 0.
1748       // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address
1749       // in the TLS block).
1750       //
1751       // 2) is special cased in @tpoff computation. To satisfy 1), we define it
1752       // as an absolute symbol of zero. This is different from GNU linkers which
1753       // define _TLS_MODULE_BASE_ relative to the first TLS section.
1754       Symbol *s = symtab.find("_TLS_MODULE_BASE_");
1755       if (s && s->isUndefined()) {
1756         s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL,
1757                            STV_HIDDEN, STT_TLS, /*value=*/0, 0,
1758                            /*section=*/nullptr});
1759         ElfSym::tlsModuleBase = cast<Defined>(s);
1760       }
1761     }
1762 
1763     // This responsible for splitting up .eh_frame section into
1764     // pieces. The relocation scan uses those pieces, so this has to be
1765     // earlier.
1766     {
1767       llvm::TimeTraceScope timeScope("Finalize .eh_frame");
1768       for (Partition &part : partitions)
1769         finalizeSynthetic(part.ehFrame.get());
1770     }
1771   }
1772 
1773   demoteSymbolsAndComputeIsPreemptible();
1774 
1775   if (config->copyRelocs && config->discard != DiscardPolicy::None)
1776     markUsedLocalSymbols<ELFT>();
1777   demoteAndCopyLocalSymbols();
1778 
1779   if (config->copyRelocs)
1780     addSectionSymbols();
1781 
1782   // Change values of linker-script-defined symbols from placeholders (assigned
1783   // by declareSymbols) to actual definitions.
1784   script->processSymbolAssignments();
1785 
1786   if (!config->relocatable) {
1787     llvm::TimeTraceScope timeScope("Scan relocations");
1788     // Scan relocations. This must be done after every symbol is declared so
1789     // that we can correctly decide if a dynamic relocation is needed. This is
1790     // called after processSymbolAssignments() because it needs to know whether
1791     // a linker-script-defined symbol is absolute.
1792     ppc64noTocRelax.clear();
1793     scanRelocations<ELFT>();
1794     reportUndefinedSymbols();
1795     postScanRelocations();
1796 
1797     if (in.plt && in.plt->isNeeded())
1798       in.plt->addSymbols();
1799     if (in.iplt && in.iplt->isNeeded())
1800       in.iplt->addSymbols();
1801 
1802     if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
1803       auto diagnose =
1804           config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
1805               ? errorOrWarn
1806               : warn;
1807       // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1808       // entries are seen. These cases would otherwise lead to runtime errors
1809       // reported by the dynamic linker.
1810       //
1811       // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker
1812       // to catch more cases. That is too much for us. Our approach resembles
1813       // the one used in ld.gold, achieves a good balance to be useful but not
1814       // too smart.
1815       //
1816       // If a DSO reference is resolved by a SharedSymbol, but the SharedSymbol
1817       // is overridden by a hidden visibility Defined (which is later discarded
1818       // due to GC), don't report the diagnostic. However, this may indicate an
1819       // unintended SharedSymbol.
1820       for (SharedFile *file : ctx.sharedFiles) {
1821         bool allNeededIsKnown =
1822             llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1823               return symtab.soNames.count(CachedHashStringRef(needed));
1824             });
1825         if (!allNeededIsKnown)
1826           continue;
1827         for (Symbol *sym : file->requiredSymbols) {
1828           if (sym->dsoDefined)
1829             continue;
1830           if (sym->isUndefined() && !sym->isWeak()) {
1831             diagnose("undefined reference: " + toString(*sym) +
1832                      "\n>>> referenced by " + toString(file) +
1833                      " (disallowed by --no-allow-shlib-undefined)");
1834           } else if (sym->isDefined() && sym->computeBinding() == STB_LOCAL) {
1835             diagnose("non-exported symbol '" + toString(*sym) + "' in '" +
1836                      toString(sym->file) + "' is referenced by DSO '" +
1837                      toString(file) + "'");
1838           }
1839         }
1840       }
1841     }
1842   }
1843 
1844   {
1845     llvm::TimeTraceScope timeScope("Add symbols to symtabs");
1846     // Now that we have defined all possible global symbols including linker-
1847     // synthesized ones. Visit all symbols to give the finishing touches.
1848     for (Symbol *sym : symtab.getSymbols()) {
1849       if (!sym->isUsedInRegularObj || !includeInSymtab(*sym))
1850         continue;
1851       if (!config->relocatable)
1852         sym->binding = sym->computeBinding();
1853       if (in.symTab)
1854         in.symTab->addSymbol(sym);
1855 
1856       if (sym->includeInDynsym()) {
1857         partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1858         if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
1859           if (file->isNeeded && !sym->isUndefined())
1860             addVerneed(sym);
1861       }
1862     }
1863 
1864     // We also need to scan the dynamic relocation tables of the other
1865     // partitions and add any referenced symbols to the partition's dynsym.
1866     for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
1867       DenseSet<Symbol *> syms;
1868       for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
1869         syms.insert(e.sym);
1870       for (DynamicReloc &reloc : part.relaDyn->relocs)
1871         if (reloc.sym && reloc.needsDynSymIndex() &&
1872             syms.insert(reloc.sym).second)
1873           part.dynSymTab->addSymbol(reloc.sym);
1874     }
1875   }
1876 
1877   if (in.mipsGot)
1878     in.mipsGot->build();
1879 
1880   removeUnusedSyntheticSections();
1881   script->diagnoseOrphanHandling();
1882   script->diagnoseMissingSGSectionAddress();
1883 
1884   sortSections();
1885 
1886   // Create a list of OutputSections, assign sectionIndex, and populate
1887   // in.shStrTab.
1888   for (SectionCommand *cmd : script->sectionCommands)
1889     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
1890       OutputSection *osec = &osd->osec;
1891       outputSections.push_back(osec);
1892       osec->sectionIndex = outputSections.size();
1893       osec->shName = in.shStrTab->addString(osec->name);
1894     }
1895 
1896   // Prefer command line supplied address over other constraints.
1897   for (OutputSection *sec : outputSections) {
1898     auto i = config->sectionStartMap.find(sec->name);
1899     if (i != config->sectionStartMap.end())
1900       sec->addrExpr = [=] { return i->second; };
1901   }
1902 
1903   // With the outputSections available check for GDPLT relocations
1904   // and add __tls_get_addr symbol if needed.
1905   if (config->emachine == EM_HEXAGON && hexagonNeedsTLSSymbol(outputSections)) {
1906     Symbol *sym =
1907         symtab.addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",
1908                                    STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
1909     sym->isPreemptible = true;
1910     partitions[0].dynSymTab->addSymbol(sym);
1911   }
1912 
1913   // This is a bit of a hack. A value of 0 means undef, so we set it
1914   // to 1 to make __ehdr_start defined. The section number is not
1915   // particularly relevant.
1916   Out::elfHeader->sectionIndex = 1;
1917   Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
1918 
1919   // Binary and relocatable output does not have PHDRS.
1920   // The headers have to be created before finalize as that can influence the
1921   // image base and the dynamic section on mips includes the image base.
1922   if (!config->relocatable && !config->oFormatBinary) {
1923     for (Partition &part : partitions) {
1924       part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs()
1925                                               : createPhdrs(part);
1926       if (config->emachine == EM_ARM) {
1927         // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1928         addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
1929       }
1930       if (config->emachine == EM_MIPS) {
1931         // Add separate segments for MIPS-specific sections.
1932         addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
1933         addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
1934         addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
1935       }
1936       if (config->emachine == EM_RISCV)
1937         addPhdrForSection(part, SHT_RISCV_ATTRIBUTES, PT_RISCV_ATTRIBUTES,
1938                           PF_R);
1939     }
1940     Out::programHeaders->size = sizeof(Elf_Phdr) * mainPart->phdrs.size();
1941 
1942     // Find the TLS segment. This happens before the section layout loop so that
1943     // Android relocation packing can look up TLS symbol addresses. We only need
1944     // to care about the main partition here because all TLS symbols were moved
1945     // to the main partition (see MarkLive.cpp).
1946     for (PhdrEntry *p : mainPart->phdrs)
1947       if (p->p_type == PT_TLS)
1948         Out::tlsPhdr = p;
1949   }
1950 
1951   // Some symbols are defined in term of program headers. Now that we
1952   // have the headers, we can find out which sections they point to.
1953   setReservedSymbolSections();
1954 
1955   if (script->noCrossRefs.size()) {
1956     llvm::TimeTraceScope timeScope("Check NOCROSSREFS");
1957     checkNoCrossRefs<ELFT>();
1958   }
1959 
1960   {
1961     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
1962 
1963     finalizeSynthetic(in.bss.get());
1964     finalizeSynthetic(in.bssRelRo.get());
1965     finalizeSynthetic(in.symTabShndx.get());
1966     finalizeSynthetic(in.shStrTab.get());
1967     finalizeSynthetic(in.strTab.get());
1968     finalizeSynthetic(in.got.get());
1969     finalizeSynthetic(in.mipsGot.get());
1970     finalizeSynthetic(in.igotPlt.get());
1971     finalizeSynthetic(in.gotPlt.get());
1972     finalizeSynthetic(in.relaPlt.get());
1973     finalizeSynthetic(in.plt.get());
1974     finalizeSynthetic(in.iplt.get());
1975     finalizeSynthetic(in.ppc32Got2.get());
1976     finalizeSynthetic(in.partIndex.get());
1977 
1978     // Dynamic section must be the last one in this list and dynamic
1979     // symbol table section (dynSymTab) must be the first one.
1980     for (Partition &part : partitions) {
1981       if (part.relaDyn) {
1982         part.relaDyn->mergeRels();
1983         // Compute DT_RELACOUNT to be used by part.dynamic.
1984         part.relaDyn->partitionRels();
1985         finalizeSynthetic(part.relaDyn.get());
1986       }
1987       if (part.relrDyn) {
1988         part.relrDyn->mergeRels();
1989         finalizeSynthetic(part.relrDyn.get());
1990       }
1991       if (part.relrAuthDyn) {
1992         part.relrAuthDyn->mergeRels();
1993         finalizeSynthetic(part.relrAuthDyn.get());
1994       }
1995 
1996       finalizeSynthetic(part.dynSymTab.get());
1997       finalizeSynthetic(part.gnuHashTab.get());
1998       finalizeSynthetic(part.hashTab.get());
1999       finalizeSynthetic(part.verDef.get());
2000       finalizeSynthetic(part.ehFrameHdr.get());
2001       finalizeSynthetic(part.verSym.get());
2002       finalizeSynthetic(part.verNeed.get());
2003       finalizeSynthetic(part.dynamic.get());
2004     }
2005   }
2006 
2007   if (!script->hasSectionsCommand && !config->relocatable)
2008     fixSectionAlignments();
2009 
2010   // This is used to:
2011   // 1) Create "thunks":
2012   //    Jump instructions in many ISAs have small displacements, and therefore
2013   //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
2014   //    JAL instruction can target only +-1 MiB from PC. It is a linker's
2015   //    responsibility to create and insert small pieces of code between
2016   //    sections to extend the ranges if jump targets are out of range. Such
2017   //    code pieces are called "thunks".
2018   //
2019   //    We add thunks at this stage. We couldn't do this before this point
2020   //    because this is the earliest point where we know sizes of sections and
2021   //    their layouts (that are needed to determine if jump targets are in
2022   //    range).
2023   //
2024   // 2) Update the sections. We need to generate content that depends on the
2025   //    address of InputSections. For example, MIPS GOT section content or
2026   //    android packed relocations sections content.
2027   //
2028   // 3) Assign the final values for the linker script symbols. Linker scripts
2029   //    sometimes using forward symbol declarations. We want to set the correct
2030   //    values. They also might change after adding the thunks.
2031   finalizeAddressDependentContent();
2032 
2033   // All information needed for OutputSection part of Map file is available.
2034   if (errorCount())
2035     return;
2036 
2037   {
2038     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2039     // finalizeAddressDependentContent may have added local symbols to the
2040     // static symbol table.
2041     finalizeSynthetic(in.symTab.get());
2042     finalizeSynthetic(in.debugNames.get());
2043     finalizeSynthetic(in.ppc64LongBranchTarget.get());
2044     finalizeSynthetic(in.armCmseSGSection.get());
2045   }
2046 
2047   // Relaxation to delete inter-basic block jumps created by basic block
2048   // sections. Run after in.symTab is finalized as optimizeBasicBlockJumps
2049   // can relax jump instructions based on symbol offset.
2050   if (config->optimizeBBJumps)
2051     optimizeBasicBlockJumps();
2052 
2053   // Fill other section headers. The dynamic table is finalized
2054   // at the end because some tags like RELSZ depend on result
2055   // of finalizing other sections.
2056   for (OutputSection *sec : outputSections)
2057     sec->finalize();
2058 
2059   script->checkFinalScriptConditions();
2060 
2061   if (config->emachine == EM_ARM && !config->isLE && config->armBe8) {
2062     addArmInputSectionMappingSymbols();
2063     sortArmMappingSymbols();
2064   }
2065 }
2066 
2067 // Ensure data sections are not mixed with executable sections when
2068 // --execute-only is used. --execute-only make pages executable but not
2069 // readable.
checkExecuteOnly()2070 template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
2071   if (!config->executeOnly)
2072     return;
2073 
2074   SmallVector<InputSection *, 0> storage;
2075   for (OutputSection *osec : outputSections)
2076     if (osec->flags & SHF_EXECINSTR)
2077       for (InputSection *isec : getInputSections(*osec, storage))
2078         if (!(isec->flags & SHF_EXECINSTR))
2079           error("cannot place " + toString(isec) + " into " +
2080                 toString(osec->name) +
2081                 ": --execute-only does not support intermingling data and code");
2082 }
2083 
2084 // The linker is expected to define SECNAME_start and SECNAME_end
2085 // symbols for a few sections. This function defines them.
addStartEndSymbols()2086 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
2087   // If the associated output section does not exist, there is ambiguity as to
2088   // how we define _start and _end symbols for an init/fini section. Users
2089   // expect no "undefined symbol" linker errors and loaders expect equal
2090   // st_value but do not particularly care whether the symbols are defined or
2091   // not. We retain the output section so that the section indexes will be
2092   // correct.
2093   auto define = [=](StringRef start, StringRef end, OutputSection *os) {
2094     if (os) {
2095       Defined *startSym = addOptionalRegular(start, os, 0);
2096       Defined *stopSym = addOptionalRegular(end, os, -1);
2097       if (startSym || stopSym)
2098         os->usedInExpression = true;
2099     } else {
2100       addOptionalRegular(start, Out::elfHeader, 0);
2101       addOptionalRegular(end, Out::elfHeader, 0);
2102     }
2103   };
2104 
2105   define("__preinit_array_start", "__preinit_array_end", Out::preinitArray);
2106   define("__init_array_start", "__init_array_end", Out::initArray);
2107   define("__fini_array_start", "__fini_array_end", Out::finiArray);
2108 
2109   // As a special case, don't unnecessarily retain .ARM.exidx, which would
2110   // create an empty PT_ARM_EXIDX.
2111   if (OutputSection *sec = findSection(".ARM.exidx"))
2112     define("__exidx_start", "__exidx_end", sec);
2113 }
2114 
2115 // If a section name is valid as a C identifier (which is rare because of
2116 // the leading '.'), linkers are expected to define __start_<secname> and
2117 // __stop_<secname> symbols. They are at beginning and end of the section,
2118 // respectively. This is not requested by the ELF standard, but GNU ld and
2119 // gold provide the feature, and used by many programs.
2120 template <class ELFT>
addStartStopSymbols(OutputSection & osec)2121 void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
2122   StringRef s = osec.name;
2123   if (!isValidCIdentifier(s))
2124     return;
2125   Defined *startSym = addOptionalRegular(saver().save("__start_" + s), &osec, 0,
2126                                          config->zStartStopVisibility);
2127   Defined *stopSym = addOptionalRegular(saver().save("__stop_" + s), &osec, -1,
2128                                         config->zStartStopVisibility);
2129   if (startSym || stopSym)
2130     osec.usedInExpression = true;
2131 }
2132 
needsPtLoad(OutputSection * sec)2133 static bool needsPtLoad(OutputSection *sec) {
2134   if (!(sec->flags & SHF_ALLOC))
2135     return false;
2136 
2137   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
2138   // responsible for allocating space for them, not the PT_LOAD that
2139   // contains the TLS initialization image.
2140   if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)
2141     return false;
2142   return true;
2143 }
2144 
2145 // Adjust phdr flags according to certain options.
computeFlags(uint64_t flags)2146 static uint64_t computeFlags(uint64_t flags) {
2147   if (config->omagic)
2148     return PF_R | PF_W | PF_X;
2149   if (config->executeOnly && (flags & PF_X))
2150     return flags & ~PF_R;
2151   return flags;
2152 }
2153 
2154 // Decide which program headers to create and which sections to include in each
2155 // one.
2156 template <class ELFT>
createPhdrs(Partition & part)2157 SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
2158   SmallVector<PhdrEntry *, 0> ret;
2159   auto addHdr = [&](unsigned type, unsigned flags) -> PhdrEntry * {
2160     ret.push_back(make<PhdrEntry>(type, flags));
2161     return ret.back();
2162   };
2163 
2164   unsigned partNo = part.getNumber();
2165   bool isMain = partNo == 1;
2166 
2167   // Add the first PT_LOAD segment for regular output sections.
2168   uint64_t flags = computeFlags(PF_R);
2169   PhdrEntry *load = nullptr;
2170 
2171   // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
2172   // PT_LOAD.
2173   if (!config->nmagic && !config->omagic) {
2174     // The first phdr entry is PT_PHDR which describes the program header
2175     // itself.
2176     if (isMain)
2177       addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
2178     else
2179       addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());
2180 
2181     // PT_INTERP must be the second entry if exists.
2182     if (OutputSection *cmd = findSection(".interp", partNo))
2183       addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);
2184 
2185     // Add the headers. We will remove them if they don't fit.
2186     // In the other partitions the headers are ordinary sections, so they don't
2187     // need to be added here.
2188     if (isMain) {
2189       load = addHdr(PT_LOAD, flags);
2190       load->add(Out::elfHeader);
2191       load->add(Out::programHeaders);
2192     }
2193   }
2194 
2195   // PT_GNU_RELRO includes all sections that should be marked as
2196   // read-only by dynamic linker after processing relocations.
2197   // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give
2198   // an error message if more than one PT_GNU_RELRO PHDR is required.
2199   PhdrEntry *relRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
2200   bool inRelroPhdr = false;
2201   OutputSection *relroEnd = nullptr;
2202   for (OutputSection *sec : outputSections) {
2203     if (sec->partition != partNo || !needsPtLoad(sec))
2204       continue;
2205     if (isRelroSection(sec)) {
2206       inRelroPhdr = true;
2207       if (!relroEnd)
2208         relRo->add(sec);
2209       else
2210         error("section: " + sec->name + " is not contiguous with other relro" +
2211               " sections");
2212     } else if (inRelroPhdr) {
2213       inRelroPhdr = false;
2214       relroEnd = sec;
2215     }
2216   }
2217   relRo->p_align = 1;
2218 
2219   for (OutputSection *sec : outputSections) {
2220     if (!needsPtLoad(sec))
2221       continue;
2222 
2223     // Normally, sections in partitions other than the current partition are
2224     // ignored. But partition number 255 is a special case: it contains the
2225     // partition end marker (.part.end). It needs to be added to the main
2226     // partition so that a segment is created for it in the main partition,
2227     // which will cause the dynamic loader to reserve space for the other
2228     // partitions.
2229     if (sec->partition != partNo) {
2230       if (isMain && sec->partition == 255)
2231         addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec);
2232       continue;
2233     }
2234 
2235     // Segments are contiguous memory regions that has the same attributes
2236     // (e.g. executable or writable). There is one phdr for each segment.
2237     // Therefore, we need to create a new phdr when the next section has
2238     // incompatible flags or is loaded at a discontiguous address or memory
2239     // region using AT or AT> linker script command, respectively.
2240     //
2241     // As an exception, we don't create a separate load segment for the ELF
2242     // headers, even if the first "real" output has an AT or AT> attribute.
2243     //
2244     // In addition, NOBITS sections should only be placed at the end of a LOAD
2245     // segment (since it's represented as p_filesz < p_memsz). If we have a
2246     // not-NOBITS section after a NOBITS, we create a new LOAD for the latter
2247     // even if flags match, so as not to require actually writing the
2248     // supposed-to-be-NOBITS section to the output file. (However, we cannot do
2249     // so when hasSectionsCommand, since we cannot introduce the extra alignment
2250     // needed to create a new LOAD)
2251     uint64_t newFlags = computeFlags(sec->getPhdrFlags());
2252     // When --no-rosegment is specified, RO and RX sections are compatible.
2253     uint32_t incompatible = flags ^ newFlags;
2254     if (config->singleRoRx && !(newFlags & PF_W))
2255       incompatible &= ~PF_X;
2256     if (incompatible)
2257       load = nullptr;
2258 
2259     bool sameLMARegion =
2260         load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;
2261     if (load && sec != relroEnd &&
2262         sec->memRegion == load->firstSec->memRegion &&
2263         (sameLMARegion || load->lastSec == Out::programHeaders) &&
2264         (script->hasSectionsCommand || sec->type == SHT_NOBITS ||
2265          load->lastSec->type != SHT_NOBITS)) {
2266       load->p_flags |= newFlags;
2267     } else {
2268       load = addHdr(PT_LOAD, newFlags);
2269       flags = newFlags;
2270     }
2271 
2272     load->add(sec);
2273   }
2274 
2275   // Add a TLS segment if any.
2276   PhdrEntry *tlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
2277   for (OutputSection *sec : outputSections)
2278     if (sec->partition == partNo && sec->flags & SHF_TLS)
2279       tlsHdr->add(sec);
2280   if (tlsHdr->firstSec)
2281     ret.push_back(tlsHdr);
2282 
2283   // Add an entry for .dynamic.
2284   if (OutputSection *sec = part.dynamic->getParent())
2285     addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);
2286 
2287   if (relRo->firstSec)
2288     ret.push_back(relRo);
2289 
2290   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
2291   if (part.ehFrame->isNeeded() && part.ehFrameHdr &&
2292       part.ehFrame->getParent() && part.ehFrameHdr->getParent())
2293     addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
2294         ->add(part.ehFrameHdr->getParent());
2295 
2296   if (config->osabi == ELFOSABI_OPENBSD) {
2297     // PT_OPENBSD_MUTABLE makes the dynamic linker fill the segment with
2298     // zero data, like bss, but it can be treated differently.
2299     if (OutputSection *cmd = findSection(".openbsd.mutable", partNo))
2300       addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);
2301 
2302     // PT_OPENBSD_RANDOMIZE makes the dynamic linker fill the segment
2303     // with random data.
2304     if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
2305       addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
2306 
2307     // PT_OPENBSD_SYSCALLS makes the kernel and dynamic linker register
2308     // system call sites.
2309     if (OutputSection *cmd = findSection(".openbsd.syscalls", partNo))
2310       addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);
2311   }
2312 
2313   if (config->zGnustack != GnuStackKind::None) {
2314     // PT_GNU_STACK is a special section to tell the loader to make the
2315     // pages for the stack non-executable. If you really want an executable
2316     // stack, you can pass -z execstack, but that's not recommended for
2317     // security reasons.
2318     unsigned perm = PF_R | PF_W;
2319     if (config->zGnustack == GnuStackKind::Exec)
2320       perm |= PF_X;
2321     addHdr(PT_GNU_STACK, perm)->p_memsz = config->zStackSize;
2322   }
2323 
2324   // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
2325   // is expected to perform W^X violations, such as calling mprotect(2) or
2326   // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
2327   // OpenBSD.
2328   if (config->zWxneeded)
2329     addHdr(PT_OPENBSD_WXNEEDED, PF_X);
2330 
2331   if (OutputSection *cmd = findSection(".note.gnu.property", partNo))
2332     addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
2333 
2334   // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
2335   // same alignment.
2336   PhdrEntry *note = nullptr;
2337   for (OutputSection *sec : outputSections) {
2338     if (sec->partition != partNo)
2339       continue;
2340     if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {
2341       if (!note || sec->lmaExpr || note->lastSec->addralign != sec->addralign)
2342         note = addHdr(PT_NOTE, PF_R);
2343       note->add(sec);
2344     } else {
2345       note = nullptr;
2346     }
2347   }
2348   return ret;
2349 }
2350 
2351 template <class ELFT>
addPhdrForSection(Partition & part,unsigned shType,unsigned pType,unsigned pFlags)2352 void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
2353                                      unsigned pType, unsigned pFlags) {
2354   unsigned partNo = part.getNumber();
2355   auto i = llvm::find_if(outputSections, [=](OutputSection *cmd) {
2356     return cmd->partition == partNo && cmd->type == shType;
2357   });
2358   if (i == outputSections.end())
2359     return;
2360 
2361   PhdrEntry *entry = make<PhdrEntry>(pType, pFlags);
2362   entry->add(*i);
2363   part.phdrs.push_back(entry);
2364 }
2365 
2366 // Place the first section of each PT_LOAD to a different page (of maxPageSize).
2367 // This is achieved by assigning an alignment expression to addrExpr of each
2368 // such section.
fixSectionAlignments()2369 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
2370   const PhdrEntry *prev;
2371   auto pageAlign = [&](const PhdrEntry *p) {
2372     OutputSection *cmd = p->firstSec;
2373     if (!cmd)
2374       return;
2375     cmd->alignExpr = [align = cmd->addralign]() { return align; };
2376     if (!cmd->addrExpr) {
2377       // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid
2378       // padding in the file contents.
2379       //
2380       // When -z separate-code is used we must not have any overlap in pages
2381       // between an executable segment and a non-executable segment. We align to
2382       // the next maximum page size boundary on transitions between executable
2383       // and non-executable segments.
2384       //
2385       // SHT_LLVM_PART_EHDR marks the start of a partition. The partition
2386       // sections will be extracted to a separate file. Align to the next
2387       // maximum page size boundary so that we can find the ELF header at the
2388       // start. We cannot benefit from overlapping p_offset ranges with the
2389       // previous segment anyway.
2390       if (config->zSeparate == SeparateSegmentKind::Loadable ||
2391           (config->zSeparate == SeparateSegmentKind::Code && prev &&
2392            (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
2393           cmd->type == SHT_LLVM_PART_EHDR)
2394         cmd->addrExpr = [] {
2395           return alignToPowerOf2(script->getDot(), config->maxPageSize);
2396         };
2397       // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
2398       // it must be the RW. Align to p_align(PT_TLS) to make sure
2399       // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if
2400       // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)
2401       // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not
2402       // be congruent to 0 modulo p_align(PT_TLS).
2403       //
2404       // Technically this is not required, but as of 2019, some dynamic loaders
2405       // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and
2406       // x86-64) doesn't make runtime address congruent to p_vaddr modulo
2407       // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same
2408       // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
2409       // blocks correctly. We need to keep the workaround for a while.
2410       else if (Out::tlsPhdr && Out::tlsPhdr->firstSec == p->firstSec)
2411         cmd->addrExpr = [] {
2412           return alignToPowerOf2(script->getDot(), config->maxPageSize) +
2413                  alignToPowerOf2(script->getDot() % config->maxPageSize,
2414                                  Out::tlsPhdr->p_align);
2415         };
2416       else
2417         cmd->addrExpr = [] {
2418           return alignToPowerOf2(script->getDot(), config->maxPageSize) +
2419                  script->getDot() % config->maxPageSize;
2420         };
2421     }
2422   };
2423 
2424   for (Partition &part : partitions) {
2425     prev = nullptr;
2426     for (const PhdrEntry *p : part.phdrs)
2427       if (p->p_type == PT_LOAD && p->firstSec) {
2428         pageAlign(p);
2429         prev = p;
2430       }
2431   }
2432 }
2433 
2434 // Compute an in-file position for a given section. The file offset must be the
2435 // same with its virtual address modulo the page size, so that the loader can
2436 // load executables without any address adjustment.
computeFileOffset(OutputSection * os,uint64_t off)2437 static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
2438   // The first section in a PT_LOAD has to have congruent offset and address
2439   // modulo the maximum page size.
2440   if (os->ptLoad && os->ptLoad->firstSec == os)
2441     return alignTo(off, os->ptLoad->p_align, os->addr);
2442 
2443   // File offsets are not significant for .bss sections other than the first one
2444   // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
2445   // increasing rather than setting to zero.
2446   if (os->type == SHT_NOBITS &&
2447       (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os))
2448      return off;
2449 
2450   // If the section is not in a PT_LOAD, we just have to align it.
2451   if (!os->ptLoad)
2452      return alignToPowerOf2(off, os->addralign);
2453 
2454   // If two sections share the same PT_LOAD the file offset is calculated
2455   // using this formula: Off2 = Off1 + (VA2 - VA1).
2456   OutputSection *first = os->ptLoad->firstSec;
2457   return first->offset + os->addr - first->addr;
2458 }
2459 
assignFileOffsetsBinary()2460 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2461   // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2462   auto needsOffset = [](OutputSection &sec) {
2463     return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2464   };
2465   uint64_t minAddr = UINT64_MAX;
2466   for (OutputSection *sec : outputSections)
2467     if (needsOffset(*sec)) {
2468       sec->offset = sec->getLMA();
2469       minAddr = std::min(minAddr, sec->offset);
2470     }
2471 
2472   // Sections are laid out at LMA minus minAddr.
2473   fileSize = 0;
2474   for (OutputSection *sec : outputSections)
2475     if (needsOffset(*sec)) {
2476       sec->offset -= minAddr;
2477       fileSize = std::max(fileSize, sec->offset + sec->size);
2478     }
2479 }
2480 
rangeToString(uint64_t addr,uint64_t len)2481 static std::string rangeToString(uint64_t addr, uint64_t len) {
2482   return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";
2483 }
2484 
2485 // Assign file offsets to output sections.
assignFileOffsets()2486 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
2487   Out::programHeaders->offset = Out::elfHeader->size;
2488   uint64_t off = Out::elfHeader->size + Out::programHeaders->size;
2489 
2490   PhdrEntry *lastRX = nullptr;
2491   for (Partition &part : partitions)
2492     for (PhdrEntry *p : part.phdrs)
2493       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
2494         lastRX = p;
2495 
2496   // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
2497   // will not occupy file offsets contained by a PT_LOAD.
2498   for (OutputSection *sec : outputSections) {
2499     if (!(sec->flags & SHF_ALLOC))
2500       continue;
2501     off = computeFileOffset(sec, off);
2502     sec->offset = off;
2503     if (sec->type != SHT_NOBITS)
2504       off += sec->size;
2505 
2506     // If this is a last section of the last executable segment and that
2507     // segment is the last loadable segment, align the offset of the
2508     // following section to avoid loading non-segments parts of the file.
2509     if (config->zSeparate != SeparateSegmentKind::None && lastRX &&
2510         lastRX->lastSec == sec)
2511       off = alignToPowerOf2(off, config->maxPageSize);
2512   }
2513   for (OutputSection *osec : outputSections) {
2514     if (osec->flags & SHF_ALLOC)
2515       continue;
2516     osec->offset = alignToPowerOf2(off, osec->addralign);
2517     off = osec->offset + osec->size;
2518   }
2519 
2520   sectionHeaderOff = alignToPowerOf2(off, config->wordsize);
2521   fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr);
2522 
2523   // Our logic assumes that sections have rising VA within the same segment.
2524   // With use of linker scripts it is possible to violate this rule and get file
2525   // offset overlaps or overflows. That should never happen with a valid script
2526   // which does not move the location counter backwards and usually scripts do
2527   // not do that. Unfortunately, there are apps in the wild, for example, Linux
2528   // kernel, which control segment distribution explicitly and move the counter
2529   // backwards, so we have to allow doing that to support linking them. We
2530   // perform non-critical checks for overlaps in checkSectionOverlap(), but here
2531   // we want to prevent file size overflows because it would crash the linker.
2532   for (OutputSection *sec : outputSections) {
2533     if (sec->type == SHT_NOBITS)
2534       continue;
2535     if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))
2536       error("unable to place section " + sec->name + " at file offset " +
2537             rangeToString(sec->offset, sec->size) +
2538             "; check your linker script for overflows");
2539   }
2540 }
2541 
2542 // Finalize the program headers. We call this function after we assign
2543 // file offsets and VAs to all sections.
setPhdrs(Partition & part)2544 template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {
2545   for (PhdrEntry *p : part.phdrs) {
2546     OutputSection *first = p->firstSec;
2547     OutputSection *last = p->lastSec;
2548 
2549     // .ARM.exidx sections may not be within a single .ARM.exidx
2550     // output section. We always want to describe just the
2551     // SyntheticSection.
2552     if (part.armExidx && p->p_type == PT_ARM_EXIDX) {
2553       p->p_filesz = part.armExidx->getSize();
2554       p->p_memsz = part.armExidx->getSize();
2555       p->p_offset = first->offset + part.armExidx->outSecOff;
2556       p->p_vaddr = first->addr + part.armExidx->outSecOff;
2557       p->p_align = part.armExidx->addralign;
2558       if (part.elfHeader)
2559         p->p_offset -= part.elfHeader->getParent()->offset;
2560 
2561       if (!p->hasLMA)
2562         p->p_paddr = first->getLMA() + part.armExidx->outSecOff;
2563       return;
2564     }
2565 
2566     if (first) {
2567       p->p_filesz = last->offset - first->offset;
2568       if (last->type != SHT_NOBITS)
2569         p->p_filesz += last->size;
2570 
2571       p->p_memsz = last->addr + last->size - first->addr;
2572       p->p_offset = first->offset;
2573       p->p_vaddr = first->addr;
2574 
2575       // File offsets in partitions other than the main partition are relative
2576       // to the offset of the ELF headers. Perform that adjustment now.
2577       if (part.elfHeader)
2578         p->p_offset -= part.elfHeader->getParent()->offset;
2579 
2580       if (!p->hasLMA)
2581         p->p_paddr = first->getLMA();
2582     }
2583   }
2584 }
2585 
2586 // A helper struct for checkSectionOverlap.
2587 namespace {
2588 struct SectionOffset {
2589   OutputSection *sec;
2590   uint64_t offset;
2591 };
2592 } // namespace
2593 
2594 // Check whether sections overlap for a specific address range (file offsets,
2595 // load and virtual addresses).
checkOverlap(StringRef name,std::vector<SectionOffset> & sections,bool isVirtualAddr)2596 static void checkOverlap(StringRef name, std::vector<SectionOffset> &sections,
2597                          bool isVirtualAddr) {
2598   llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {
2599     return a.offset < b.offset;
2600   });
2601 
2602   // Finding overlap is easy given a vector is sorted by start position.
2603   // If an element starts before the end of the previous element, they overlap.
2604   for (size_t i = 1, end = sections.size(); i < end; ++i) {
2605     SectionOffset a = sections[i - 1];
2606     SectionOffset b = sections[i];
2607     if (b.offset >= a.offset + a.sec->size)
2608       continue;
2609 
2610     // If both sections are in OVERLAY we allow the overlapping of virtual
2611     // addresses, because it is what OVERLAY was designed for.
2612     if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
2613       continue;
2614 
2615     errorOrWarn("section " + a.sec->name + " " + name +
2616                 " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name +
2617                 " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " +
2618                 b.sec->name + " range is " +
2619                 rangeToString(b.offset, b.sec->size));
2620   }
2621 }
2622 
2623 // Check for overlapping sections and address overflows.
2624 //
2625 // In this function we check that none of the output sections have overlapping
2626 // file offsets. For SHF_ALLOC sections we also check that the load address
2627 // ranges and the virtual address ranges don't overlap
checkSections()2628 template <class ELFT> void Writer<ELFT>::checkSections() {
2629   // First, check that section's VAs fit in available address space for target.
2630   for (OutputSection *os : outputSections)
2631     if ((os->addr + os->size < os->addr) ||
2632         (!ELFT::Is64Bits && os->addr + os->size > uint64_t(UINT32_MAX) + 1))
2633       errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) +
2634                   " of size 0x" + utohexstr(os->size) +
2635                   " exceeds available address space");
2636 
2637   // Check for overlapping file offsets. In this case we need to skip any
2638   // section marked as SHT_NOBITS. These sections don't actually occupy space in
2639   // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
2640   // binary is specified only add SHF_ALLOC sections are added to the output
2641   // file so we skip any non-allocated sections in that case.
2642   std::vector<SectionOffset> fileOffs;
2643   for (OutputSection *sec : outputSections)
2644     if (sec->size > 0 && sec->type != SHT_NOBITS &&
2645         (!config->oFormatBinary || (sec->flags & SHF_ALLOC)))
2646       fileOffs.push_back({sec, sec->offset});
2647   checkOverlap("file", fileOffs, false);
2648 
2649   // When linking with -r there is no need to check for overlapping virtual/load
2650   // addresses since those addresses will only be assigned when the final
2651   // executable/shared object is created.
2652   if (config->relocatable)
2653     return;
2654 
2655   // Checking for overlapping virtual and load addresses only needs to take
2656   // into account SHF_ALLOC sections since others will not be loaded.
2657   // Furthermore, we also need to skip SHF_TLS sections since these will be
2658   // mapped to other addresses at runtime and can therefore have overlapping
2659   // ranges in the file.
2660   std::vector<SectionOffset> vmas;
2661   for (OutputSection *sec : outputSections)
2662     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
2663       vmas.push_back({sec, sec->addr});
2664   checkOverlap("virtual address", vmas, true);
2665 
2666   // Finally, check that the load addresses don't overlap. This will usually be
2667   // the same as the virtual addresses but can be different when using a linker
2668   // script with AT().
2669   std::vector<SectionOffset> lmas;
2670   for (OutputSection *sec : outputSections)
2671     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
2672       lmas.push_back({sec, sec->getLMA()});
2673   checkOverlap("load address", lmas, false);
2674 }
2675 
2676 // The entry point address is chosen in the following ways.
2677 //
2678 // 1. the '-e' entry command-line option;
2679 // 2. the ENTRY(symbol) command in a linker control script;
2680 // 3. the value of the symbol _start, if present;
2681 // 4. the number represented by the entry symbol, if it is a number;
2682 // 5. the address 0.
getEntryAddr()2683 static uint64_t getEntryAddr() {
2684   // Case 1, 2 or 3
2685   if (Symbol *b = symtab.find(config->entry))
2686     return b->getVA();
2687 
2688   // Case 4
2689   uint64_t addr;
2690   if (to_integer(config->entry, addr))
2691     return addr;
2692 
2693   // Case 5
2694   if (config->warnMissingEntry)
2695     warn("cannot find entry symbol " + config->entry +
2696          "; not setting start address");
2697   return 0;
2698 }
2699 
getELFType()2700 static uint16_t getELFType() {
2701   if (config->isPic)
2702     return ET_DYN;
2703   if (config->relocatable)
2704     return ET_REL;
2705   return ET_EXEC;
2706 }
2707 
writeHeader()2708 template <class ELFT> void Writer<ELFT>::writeHeader() {
2709   writeEhdr<ELFT>(Out::bufferStart, *mainPart);
2710   writePhdrs<ELFT>(Out::bufferStart + sizeof(Elf_Ehdr), *mainPart);
2711 
2712   auto *eHdr = reinterpret_cast<Elf_Ehdr *>(Out::bufferStart);
2713   eHdr->e_type = getELFType();
2714   eHdr->e_entry = getEntryAddr();
2715   eHdr->e_shoff = sectionHeaderOff;
2716 
2717   // Write the section header table.
2718   //
2719   // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
2720   // and e_shstrndx fields. When the value of one of these fields exceeds
2721   // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
2722   // use fields in the section header at index 0 to store
2723   // the value. The sentinel values and fields are:
2724   // e_shnum = 0, SHdrs[0].sh_size = number of sections.
2725   // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
2726   auto *sHdrs = reinterpret_cast<Elf_Shdr *>(Out::bufferStart + eHdr->e_shoff);
2727   size_t num = outputSections.size() + 1;
2728   if (num >= SHN_LORESERVE)
2729     sHdrs->sh_size = num;
2730   else
2731     eHdr->e_shnum = num;
2732 
2733   uint32_t strTabIndex = in.shStrTab->getParent()->sectionIndex;
2734   if (strTabIndex >= SHN_LORESERVE) {
2735     sHdrs->sh_link = strTabIndex;
2736     eHdr->e_shstrndx = SHN_XINDEX;
2737   } else {
2738     eHdr->e_shstrndx = strTabIndex;
2739   }
2740 
2741   for (OutputSection *sec : outputSections)
2742     sec->writeHeaderTo<ELFT>(++sHdrs);
2743 }
2744 
2745 // Open a result file.
openFile()2746 template <class ELFT> void Writer<ELFT>::openFile() {
2747   uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX;
2748   if (fileSize != size_t(fileSize) || maxSize < fileSize) {
2749     std::string msg;
2750     raw_string_ostream s(msg);
2751     s << "output file too large: " << Twine(fileSize) << " bytes\n"
2752       << "section sizes:\n";
2753     for (OutputSection *os : outputSections)
2754       s << os->name << ' ' << os->size << "\n";
2755     error(s.str());
2756     return;
2757   }
2758 
2759   unlinkAsync(config->outputFile);
2760   unsigned flags = 0;
2761   if (!config->relocatable)
2762     flags |= FileOutputBuffer::F_executable;
2763   if (!config->mmapOutputFile)
2764     flags |= FileOutputBuffer::F_no_mmap;
2765   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
2766       FileOutputBuffer::create(config->outputFile, fileSize, flags);
2767 
2768   if (!bufferOrErr) {
2769     error("failed to open " + config->outputFile + ": " +
2770           llvm::toString(bufferOrErr.takeError()));
2771     return;
2772   }
2773   buffer = std::move(*bufferOrErr);
2774   Out::bufferStart = buffer->getBufferStart();
2775 }
2776 
writeSectionsBinary()2777 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
2778   parallel::TaskGroup tg;
2779   for (OutputSection *sec : outputSections)
2780     if (sec->flags & SHF_ALLOC)
2781       sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
2782 }
2783 
fillTrap(uint8_t * i,uint8_t * end)2784 static void fillTrap(uint8_t *i, uint8_t *end) {
2785   for (; i + 4 <= end; i += 4)
2786     memcpy(i, &target->trapInstr, 4);
2787 }
2788 
2789 // Fill the last page of executable segments with trap instructions
2790 // instead of leaving them as zero. Even though it is not required by any
2791 // standard, it is in general a good thing to do for security reasons.
2792 //
2793 // We'll leave other pages in segments as-is because the rest will be
2794 // overwritten by output sections.
writeTrapInstr()2795 template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
2796   for (Partition &part : partitions) {
2797     // Fill the last page.
2798     for (PhdrEntry *p : part.phdrs)
2799       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
2800         fillTrap(Out::bufferStart +
2801                      alignDown(p->firstSec->offset + p->p_filesz, 4),
2802                  Out::bufferStart +
2803                      alignToPowerOf2(p->firstSec->offset + p->p_filesz,
2804                                      config->maxPageSize));
2805 
2806     // Round up the file size of the last segment to the page boundary iff it is
2807     // an executable segment to ensure that other tools don't accidentally
2808     // trim the instruction padding (e.g. when stripping the file).
2809     PhdrEntry *last = nullptr;
2810     for (PhdrEntry *p : part.phdrs)
2811       if (p->p_type == PT_LOAD)
2812         last = p;
2813 
2814     if (last && (last->p_flags & PF_X))
2815       last->p_memsz = last->p_filesz =
2816           alignToPowerOf2(last->p_filesz, config->maxPageSize);
2817   }
2818 }
2819 
2820 // Write section contents to a mmap'ed file.
writeSections()2821 template <class ELFT> void Writer<ELFT>::writeSections() {
2822   llvm::TimeTraceScope timeScope("Write sections");
2823 
2824   {
2825     // In -r or --emit-relocs mode, write the relocation sections first as in
2826     // ELf_Rel targets we might find out that we need to modify the relocated
2827     // section while doing it.
2828     parallel::TaskGroup tg;
2829     for (OutputSection *sec : outputSections)
2830       if (isStaticRelSecType(sec->type))
2831         sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
2832   }
2833   {
2834     parallel::TaskGroup tg;
2835     for (OutputSection *sec : outputSections)
2836       if (!isStaticRelSecType(sec->type))
2837         sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
2838   }
2839 
2840   // Finally, check that all dynamic relocation addends were written correctly.
2841   if (config->checkDynamicRelocs && config->writeAddends) {
2842     for (OutputSection *sec : outputSections)
2843       if (isStaticRelSecType(sec->type))
2844         sec->checkDynRelAddends(Out::bufferStart);
2845   }
2846 }
2847 
2848 // Computes a hash value of Data using a given hash function.
2849 // In order to utilize multiple cores, we first split data into 1MB
2850 // chunks, compute a hash for each chunk, and then compute a hash value
2851 // of the hash values.
2852 static void
computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,llvm::ArrayRef<uint8_t> data,std::function<void (uint8_t * dest,ArrayRef<uint8_t> arr)> hashFn)2853 computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
2854             llvm::ArrayRef<uint8_t> data,
2855             std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
2856   std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
2857   const size_t hashesSize = chunks.size() * hashBuf.size();
2858   std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);
2859 
2860   // Compute hash values.
2861   parallelFor(0, chunks.size(), [&](size_t i) {
2862     hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);
2863   });
2864 
2865   // Write to the final output buffer.
2866   hashFn(hashBuf.data(), ArrayRef(hashes.get(), hashesSize));
2867 }
2868 
writeBuildId()2869 template <class ELFT> void Writer<ELFT>::writeBuildId() {
2870   if (!mainPart->buildId || !mainPart->buildId->getParent())
2871     return;
2872 
2873   if (config->buildId == BuildIdKind::Hexstring) {
2874     for (Partition &part : partitions)
2875       part.buildId->writeBuildId(config->buildIdVector);
2876     return;
2877   }
2878 
2879   // Compute a hash of all sections of the output file.
2880   size_t hashSize = mainPart->buildId->hashSize;
2881   std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);
2882   MutableArrayRef<uint8_t> output(buildId.get(), hashSize);
2883   llvm::ArrayRef<uint8_t> input{Out::bufferStart, size_t(fileSize)};
2884 
2885   // Fedora introduced build ID as "approximation of true uniqueness across all
2886   // binaries that might be used by overlapping sets of people". It does not
2887   // need some security goals that some hash algorithms strive to provide, e.g.
2888   // (second-)preimage and collision resistance. In practice people use 'md5'
2889   // and 'sha1' just for different lengths. Implement them with the more
2890   // efficient BLAKE3.
2891   switch (config->buildId) {
2892   case BuildIdKind::Fast:
2893     computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
2894       write64le(dest, xxh3_64bits(arr));
2895     });
2896     break;
2897   case BuildIdKind::Md5:
2898     computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
2899       memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize);
2900     });
2901     break;
2902   case BuildIdKind::Sha1:
2903     computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
2904       memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize);
2905     });
2906     break;
2907   case BuildIdKind::Uuid:
2908     if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))
2909       error("entropy source failure: " + ec.message());
2910     break;
2911   default:
2912     llvm_unreachable("unknown BuildIdKind");
2913   }
2914   for (Partition &part : partitions)
2915     part.buildId->writeBuildId(output);
2916 }
2917 
2918 template void elf::writeResult<ELF32LE>();
2919 template void elf::writeResult<ELF32BE>();
2920 template void elf::writeResult<ELF64LE>();
2921 template void elf::writeResult<ELF64BE>();
2922