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

source code of lld/ELF/Writer.cpp