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