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/Parallel.h"
28#include "lld/Common/Strings.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/StringMap.h"
32#include "llvm/Support/BLAKE3.h"
33#include "llvm/Support/Parallel.h"
34#include "llvm/Support/Path.h"
35#include "llvm/Support/RandomNumberGenerator.h"
36#include "llvm/Support/TimeProfiler.h"
37#include "llvm/Support/xxhash.h"
38#include <climits>
39
40#define DEBUG_TYPE "lld"
41
42using namespace llvm;
43using namespace llvm::ELF;
44using namespace llvm::object;
45using namespace llvm::support;
46using namespace llvm::support::endian;
47using namespace lld;
48using namespace lld::elf;
49
50namespace {
51// The writer writes a SymbolTable result to a file.
52template <class ELFT> class Writer {
53public:
54 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
55
56 Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.e.outputBuffer), tc(ctx) {}
57
58 void run();
59
60private:
61 void addSectionSymbols();
62 void sortSections();
63 void resolveShfLinkOrder();
64 void finalizeAddressDependentContent();
65 void optimizeBasicBlockJumps();
66 void sortInputSections();
67 void sortOrphanSections();
68 void finalizeSections();
69 void checkExecuteOnly();
70 void checkExecuteOnlyReport();
71 void setReservedSymbolSections();
72
73 SmallVector<std::unique_ptr<PhdrEntry>, 0> createPhdrs();
74 void addPhdrForSection(unsigned shType, unsigned pType, unsigned pFlags);
75 void assignFileOffsets();
76 void assignFileOffsetsBinary();
77 void setPhdrs();
78 void checkSections();
79 void fixSectionAlignments();
80 void openFile();
81 void writeTrapInstr();
82 void writeHeader();
83 void writeSections();
84 void writeSectionsBinary();
85 void writeBuildId();
86
87 Ctx &ctx;
88 std::unique_ptr<FileOutputBuffer> &buffer;
89 // ThunkCreator holds Thunks that are used at writeTo time.
90 ThunkCreator tc;
91
92 void addRelIpltSymbols();
93 void addStartEndSymbols();
94 void addStartStopSymbols(OutputSection &osec);
95
96 uint64_t fileSize;
97 uint64_t sectionHeaderOff;
98};
99} // anonymous namespace
100
101template <class ELFT> void elf::writeResult(Ctx &ctx) {
102 Writer<ELFT>(ctx).run();
103}
104
105static void
106removeEmptyPTLoad(Ctx &ctx, SmallVector<std::unique_ptr<PhdrEntry>, 0> &phdrs) {
107 auto it = std::stable_partition(first: phdrs.begin(), last: phdrs.end(), pred: [&](auto &p) {
108 if (p->p_type != PT_LOAD)
109 return true;
110 if (!p->firstSec)
111 return false;
112 uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
113 return size != 0;
114 });
115
116 // Clear OutputSection::ptLoad for sections contained in removed
117 // segments.
118 DenseSet<PhdrEntry *> removed;
119 for (auto it2 = it; it2 != phdrs.end(); ++it2)
120 removed.insert(V: it2->get());
121 for (OutputSection *sec : ctx.outputSections)
122 if (removed.contains(V: sec->ptLoad))
123 sec->ptLoad = nullptr;
124 phdrs.erase(CS: it, CE: phdrs.end());
125}
126
127static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
128 uint64_t val, uint8_t stOther = STV_HIDDEN) {
129 Symbol *s = ctx.symtab->find(name);
130 if (!s || s->isDefined() || s->isCommon())
131 return nullptr;
132
133 ctx.synthesizedSymbols.push_back(Elt: s);
134 s->resolve(ctx, other: Defined{ctx.internalFile, StringRef(), STB_GLOBAL, stOther,
135 STT_NOTYPE, val,
136 /*size=*/0, sec});
137 s->isUsedInRegularObj = true;
138 return cast<Defined>(Val: s);
139}
140
141// The linker is expected to define some symbols depending on
142// the linking result. This function defines such symbols.
143void elf::addReservedSymbols(Ctx &ctx) {
144 if (ctx.arg.emachine == EM_MIPS) {
145 auto addAbsolute = [&](StringRef name) {
146 Symbol *sym =
147 ctx.symtab->addSymbol(newSym: Defined{ctx.internalFile, name, STB_GLOBAL,
148 STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});
149 sym->isUsedInRegularObj = true;
150 return cast<Defined>(Val: sym);
151 };
152 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
153 // so that it points to an absolute address which by default is relative
154 // to GOT. Default offset is 0x7ff0.
155 // See "Global Data Symbols" in Chapter 6 in the following document:
156 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
157 ctx.sym.mipsGp = addAbsolute("_gp");
158
159 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
160 // start of function and 'gp' pointer into GOT.
161 if (ctx.symtab->find(name: "_gp_disp"))
162 ctx.sym.mipsGpDisp = addAbsolute("_gp_disp");
163
164 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
165 // pointer. This symbol is used in the code generated by .cpload pseudo-op
166 // in case of using -mno-shared option.
167 // https://sourceware.org/ml/binutils/2004-12/msg00094.html
168 if (ctx.symtab->find(name: "__gnu_local_gp"))
169 ctx.sym.mipsLocalGp = addAbsolute("__gnu_local_gp");
170 } else if (ctx.arg.emachine == EM_PPC) {
171 // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
172 // support Small Data Area, define it arbitrarily as 0.
173 addOptionalRegular(ctx, name: "_SDA_BASE_", sec: nullptr, val: 0, stOther: STV_HIDDEN);
174 } else if (ctx.arg.emachine == EM_PPC64) {
175 addPPC64SaveRestore(ctx);
176 }
177
178 // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
179 // combines the typical ELF GOT with the small data sections. It commonly
180 // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
181 // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
182 // represent the TOC base which is offset by 0x8000 bytes from the start of
183 // the .got section.
184 // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
185 // correctness of some relocations depends on its value.
186 StringRef gotSymName =
187 (ctx.arg.emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
188
189 if (Symbol *s = ctx.symtab->find(name: gotSymName)) {
190 if (s->isDefined()) {
191 ErrAlways(ctx) << s->file << " cannot redefine linker defined symbol '"
192 << gotSymName << "'";
193 return;
194 }
195
196 uint64_t gotOff = 0;
197 if (ctx.arg.emachine == EM_PPC64)
198 gotOff = 0x8000;
199
200 s->resolve(ctx, other: Defined{ctx.internalFile, StringRef(), STB_GLOBAL,
201 STV_HIDDEN, STT_NOTYPE, gotOff, /*size=*/0,
202 ctx.out.elfHeader.get()});
203 ctx.sym.globalOffsetTable = cast<Defined>(Val: s);
204 }
205
206 // __ehdr_start is the location of ELF file headers. Note that we define
207 // this symbol unconditionally even when using a linker script, which
208 // differs from the behavior implemented by GNU linker which only define
209 // this symbol if ELF headers are in the memory mapped segment.
210 addOptionalRegular(ctx, name: "__ehdr_start", sec: ctx.out.elfHeader.get(), val: 0,
211 stOther: STV_HIDDEN);
212
213 // __executable_start is not documented, but the expectation of at
214 // least the Android libc is that it points to the ELF header.
215 addOptionalRegular(ctx, name: "__executable_start", sec: ctx.out.elfHeader.get(), val: 0,
216 stOther: STV_HIDDEN);
217
218 // __dso_handle symbol is passed to cxa_finalize as a marker to identify
219 // each DSO. The address of the symbol doesn't matter as long as they are
220 // different in different DSOs, so we chose the start address of the DSO.
221 addOptionalRegular(ctx, name: "__dso_handle", sec: ctx.out.elfHeader.get(), val: 0,
222 stOther: STV_HIDDEN);
223
224 // If linker script do layout we do not need to create any standard symbols.
225 if (ctx.script->hasSectionsCommand)
226 return;
227
228 auto add = [&](StringRef s, int64_t pos) {
229 return addOptionalRegular(ctx, name: s, sec: ctx.out.elfHeader.get(), val: pos,
230 stOther: STV_DEFAULT);
231 };
232
233 ctx.sym.bss = add("__bss_start", 0);
234 ctx.sym.end1 = add("end", -1);
235 ctx.sym.end2 = add("_end", -1);
236 ctx.sym.etext1 = add("etext", -1);
237 ctx.sym.etext2 = add("_etext", -1);
238 ctx.sym.edata1 = add("edata", -1);
239 ctx.sym.edata2 = add("_edata", -1);
240}
241
242static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
243 if (map.empty())
244 for (auto [i, sec] : llvm::enumerate(First: sym.file->getSections()))
245 map.try_emplace(Key: sec, Args&: i);
246 // Change WEAK to GLOBAL so that if a scanned relocation references sym,
247 // maybeReportUndefined will report an error.
248 uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;
249 Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
250 /*discardedSecIdx=*/map.lookup(Val: sym.section))
251 .overwrite(sym);
252 // Eliminate from the symbol table, otherwise we would leave an undefined
253 // symbol if the symbol is unreferenced in the absence of GC.
254 sym.isUsedInRegularObj = false;
255}
256
257// If all references to a DSO happen to be weak, the DSO is not added to
258// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
259// dangling references to an unneeded DSO. Use a weak binding to avoid
260// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
261//
262// In addition, demote symbols defined in discarded sections, so that
263// references to /DISCARD/ discarded symbols will lead to errors.
264static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
265 llvm::TimeTraceScope timeScope("Demote symbols");
266 ArrayRef<Symbol *> syms = ctx.symtab->getSymbols();
267 constexpr size_t chunkSize = 4096;
268 parallelFor(Begin: 0, End: (syms.size() + chunkSize - 1) / chunkSize, Fn: [&](size_t c) {
269 DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
270 size_t begin = c * chunkSize;
271 for (Symbol *sym :
272 syms.slice(N: begin, M: std::min(a: chunkSize, b: syms.size() - begin))) {
273 if (auto *d = dyn_cast<Defined>(Val: sym)) {
274 if (d->section && !d->section->isLive())
275 demoteDefined(sym&: *d, map&: sectionIndexMap[d->file]);
276 } else {
277 auto *s = dyn_cast<SharedSymbol>(Val: sym);
278 if (sym->isLazy() || (s && !cast<SharedFile>(Val: s->file)->isNeeded)) {
279 uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
280 Undefined(ctx.internalFile, sym->getName(), binding, sym->stOther,
281 sym->type)
282 .overwrite(sym&: *sym);
283 sym->versionId = VER_NDX_GLOBAL;
284 }
285 }
286
287 sym->isPreemptible = (sym->isUndefined() || sym->isExported) &&
288 computeIsPreemptible(ctx, sym: *sym);
289 }
290 });
291}
292
293static OutputSection *findSection(Ctx &ctx, StringRef name) {
294 for (SectionCommand *cmd : ctx.script->sectionCommands)
295 if (auto *osd = dyn_cast<OutputDesc>(Val: cmd))
296 if (osd->osec.name == name)
297 return &osd->osec;
298 return nullptr;
299}
300
301// The main function of the writer.
302template <class ELFT> void Writer<ELFT>::run() {
303 // Now that we have a complete set of output sections. This function
304 // completes section contents. For example, we need to add strings
305 // to the string table, and add entries to .got and .plt.
306 // finalizeSections does that.
307 finalizeSections();
308 checkExecuteOnly();
309 checkExecuteOnlyReport();
310
311 // If --compressed-debug-sections is specified, compress .debug_* sections.
312 // Do it right now because it changes the size of output sections.
313 for (OutputSection *sec : ctx.outputSections)
314 sec->maybeCompress<ELFT>(ctx);
315
316 if (ctx.script->hasSectionsCommand)
317 ctx.script->allocateHeaders(phdrs&: ctx.phdrs);
318
319 // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
320 // 0 sized region. This has to be done late since only after assignAddresses
321 // we know the size of the sections.
322 removeEmptyPTLoad(ctx, phdrs&: ctx.phdrs);
323
324 if (!ctx.arg.oFormatBinary)
325 assignFileOffsets();
326 else
327 assignFileOffsetsBinary();
328
329 setPhdrs();
330
331 // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
332 // because the files may be useful in case checkSections() or openFile()
333 // fails, for example, due to an erroneous file size.
334 writeMapAndCref(ctx);
335
336 // Handle --print-memory-usage option.
337 if (ctx.arg.printMemoryUsage)
338 ctx.script->printMemoryUsage(os&: ctx.e.outs());
339
340 if (ctx.arg.checkSections)
341 checkSections();
342
343 // It does not make sense try to open the file if we have error already.
344 if (errCount(ctx))
345 return;
346
347 {
348 llvm::TimeTraceScope timeScope("Write output file");
349 // Write the result down to a file.
350 openFile();
351 if (errCount(ctx))
352 return;
353
354 if (!ctx.arg.oFormatBinary) {
355 if (ctx.arg.zSeparate != SeparateSegmentKind::None)
356 writeTrapInstr();
357 writeHeader();
358 writeSections();
359 } else {
360 writeSectionsBinary();
361 }
362
363 // Backfill .note.gnu.build-id section content. This is done at last
364 // because the content is usually a hash value of the entire output file.
365 writeBuildId();
366 if (errCount(ctx))
367 return;
368
369 // Capture output for the embedded unoptimized dynamic debugging relocatable
370 // link.
371 // Otherwise, with -o -, write to lld::outs() (the stdoutOS argument of
372 // link()) instead of committing the buffer, which would write to the
373 // process's stdout.
374 if (ctx.inDynDbgLink)
375 ctx.dynDbgOutput = std::move(buffer);
376 else if (ctx.arg.outputFile == "-") {
377 ctx.e.outs() << StringRef(
378 reinterpret_cast<const char *>(buffer->getBufferStart()),
379 buffer->getBufferSize());
380 } else if (!ctx.e.disableOutput) {
381 if (auto e = buffer->commit())
382 Err(ctx) << "failed to write output '" << buffer->getPath()
383 << "': " << std::move(e);
384 }
385
386 if (!ctx.arg.cmseOutputLib.empty())
387 writeARMCmseImportLib<ELFT>(ctx);
388 }
389}
390
391static bool retainKeepsInSymtab(Ctx &ctx, const Symbol &sym) {
392 if (sym.hasFlag(bit: USED) && ctx.arg.copyRelocs)
393 return true;
394 return ctx.arg.retainSymbols->contains(V: sym.getName());
395}
396
397static bool shouldKeepInSymtab(Ctx &ctx, const Defined &sym) {
398 if (sym.isSection())
399 return false;
400
401 // If --emit-reloc or -r is given, preserve symbols referenced by relocations
402 // from live sections.
403 if (sym.hasFlag(bit: USED) && ctx.arg.copyRelocs)
404 return true;
405
406 // Exclude local symbols pointing to .ARM.exidx sections.
407 // They are probably mapping symbols "$d", which are optional for these
408 // sections. After merging the .ARM.exidx sections, some of these symbols
409 // may become dangling. The easiest way to avoid the issue is not to add
410 // them to the symbol table from the beginning.
411 if (ctx.arg.emachine == EM_ARM && sym.section &&
412 sym.section->type == SHT_ARM_EXIDX)
413 return false;
414
415 if (ctx.arg.discard == DiscardPolicy::None)
416 return true;
417 if (ctx.arg.discard == DiscardPolicy::All)
418 return false;
419
420 // In ELF assembly .L symbols are normally discarded by the assembler.
421 // If the assembler fails to do so, the linker discards them if
422 // * --discard-locals is used.
423 // * The symbol is in a SHF_MERGE section, which is normally the reason for
424 // the assembler keeping the .L symbol.
425 if (sym.getName().starts_with(Prefix: ".L") &&
426 (ctx.arg.discard == DiscardPolicy::Locals ||
427 (sym.section && (sym.section->flags & SHF_MERGE))))
428 return false;
429 // If --retain-symbols-file= is specified, keep in .symtab only listed symbols
430 // plus those referenced by emitted relocations.
431 if (LLVM_UNLIKELY(ctx.arg.retainSymbols))
432 return retainKeepsInSymtab(ctx, sym);
433 return true;
434}
435
436bool elf::includeInSymtab(Ctx &ctx, const Symbol &b) {
437 if (auto *d = dyn_cast<Defined>(Val: &b)) {
438 // Always include absolute symbols.
439 SectionBase *sec = d->section;
440 if (!sec)
441 return true;
442 assert(sec->isLive());
443
444 if (auto *s = dyn_cast<MergeInputSection>(Val: sec))
445 return d->isSection() || s->getSectionPiece(offset: d->value).live;
446 return true;
447 }
448 return b.hasFlag(bit: USED) || !ctx.arg.gcSections;
449}
450
451// Scan local symbols to:
452//
453// - demote symbols defined relative to /DISCARD/ discarded input sections so
454// that relocations referencing them will lead to errors.
455// - copy eligible symbols to .symTab
456static void demoteAndCopyLocalSymbols(Ctx &ctx) {
457 llvm::TimeTraceScope timeScope("Add local symbols");
458 auto symsVec =
459 std::make_unique<SmallVector<Symbol *, 0>[]>(num: ctx.objectFiles.size());
460 parallelForFineGrained(end: ctx.objectFiles.size(), fn: [&](size_t i) {
461 DenseMap<SectionBase *, size_t> sectionIndexMap;
462 for (Symbol *b : ctx.objectFiles[i]->getLocalSymbols()) {
463 assert(b->isLocal() && "should have been caught in initializeSymbols()");
464 auto *dr = dyn_cast<Defined>(Val: b);
465 if (!dr)
466 continue;
467
468 if (dr->section && !dr->section->isLive())
469 demoteDefined(sym&: *dr, map&: sectionIndexMap);
470 else if (ctx.in.symTab && includeInSymtab(ctx, b: *b) &&
471 shouldKeepInSymtab(ctx, sym: *dr))
472 symsVec[i].push_back(Elt: b);
473 }
474 });
475 for (size_t i = 0, e = ctx.objectFiles.size(); i != e; ++i) {
476 // For -r, synthesize an STT_FILE named after the input file for an input
477 // that contributes local symbols but no STT_FILE, so that its symbols are
478 // not attributed to another file's STT_FILE (matching GNU ld).
479 // --discard-all discards STT_FILE symbols.
480 auto &syms = symsVec[i];
481 if (ctx.arg.relocatable && ctx.arg.discard != DiscardPolicy::All &&
482 !syms.empty() &&
483 llvm::none_of(Range&: syms, P: [](Symbol *s) { return s->isFile(); })) {
484 InputFile *file = ctx.objectFiles[i];
485 ctx.in.symTab->addSymbol(
486 sym: makeDefined(args&: file, args: sys::path::filename(path: file->getName()), args: STB_LOCAL,
487 /*stOther=*/args: 0, args: STT_FILE, /*value=*/args: 0,
488 /*size=*/args: 0, args: nullptr));
489 }
490 for (Symbol *sym : syms)
491 ctx.in.symTab->addSymbol(sym);
492 }
493}
494
495// Create a section symbol for each output section so that we can represent
496// relocations that point to the section. If we know that no relocation is
497// referring to a section (that happens if the section is a synthetic one), we
498// don't create a section symbol for that section.
499template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
500 for (SectionCommand *cmd : ctx.script->sectionCommands) {
501 auto *osd = dyn_cast<OutputDesc>(Val: cmd);
502 if (!osd)
503 continue;
504 OutputSection &osec = osd->osec;
505 InputSectionBase *isec = nullptr;
506 // Iterate over all input sections and add a STT_SECTION symbol if any input
507 // section may be a relocation target.
508 for (SectionCommand *cmd : osec.commands) {
509 auto *isd = dyn_cast<InputSectionDescription>(Val: cmd);
510 if (!isd)
511 continue;
512 for (InputSectionBase *s : isd->sections) {
513 // Relocations are not using REL[A] section symbols.
514 if (isStaticRelSecType(type: s->type))
515 continue;
516
517 // Unlike other synthetic sections, mergeable output sections contain
518 // data copied from input sections, and there may be a relocation
519 // pointing to its contents if -r or --emit-reloc is given.
520 if (isa<SyntheticSection>(Val: s) && !(s->flags & SHF_MERGE))
521 continue;
522
523 isec = s;
524 break;
525 }
526 }
527 if (!isec)
528 continue;
529
530 // Set the symbol to be relative to the output section so that its st_value
531 // equals the output section address. Note, there may be a gap between the
532 // start of the output section and isec.
533 ctx.in.symTab->addSymbol(sym: makeDefined(args&: isec->file, args: "", args: STB_LOCAL,
534 /*stOther=*/args: 0, args: STT_SECTION,
535 /*value=*/args: 0, /*size=*/args: 0, args: &osec));
536 }
537}
538
539// Returns true if this is a variant of .data.rel.ro.
540static bool isRelRoDataSection(Ctx &ctx, StringRef secName) {
541 if (!secName.consume_front(Prefix: ".data.rel.ro"))
542 return false;
543 if (secName.empty())
544 return true;
545 // If -z keep-data-section-prefix is specified, additionally allow
546 // '.data.rel.ro.hot' and '.data.rel.ro.unlikely'.
547 if (ctx.arg.zKeepDataSectionPrefix)
548 return secName == ".hot" || secName == ".unlikely";
549 return false;
550}
551
552// Today's loaders have a feature to make segments read-only after
553// processing dynamic relocations to enhance security. PT_GNU_RELRO
554// is defined for that.
555//
556// This function returns true if a section needs to be put into a
557// PT_GNU_RELRO segment.
558static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {
559 if (!ctx.arg.zRelro)
560 return false;
561 if (sec->relro)
562 return true;
563
564 uint64_t flags = sec->flags;
565
566 // Non-allocatable or non-writable sections don't need RELRO because
567 // they are not writable or not even mapped to memory in the first place.
568 // RELRO is for sections that are essentially read-only but need to
569 // be writable only at process startup to allow dynamic linker to
570 // apply relocations.
571 if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
572 return false;
573
574 // Once initialized, TLS data segments are used as data templates
575 // for a thread-local storage. For each new thread, runtime
576 // allocates memory for a TLS and copy templates there. No thread
577 // are supposed to use templates directly. Thus, it can be in RELRO.
578 if (flags & SHF_TLS)
579 return true;
580
581 // .init_array, .preinit_array and .fini_array contain pointers to
582 // functions that are executed on process startup or exit. These
583 // pointers are set by the static linker, and they are not expected
584 // to change at runtime. But if you are an attacker, you could do
585 // interesting things by manipulating pointers in .fini_array, for
586 // example. So they are put into RELRO.
587 uint32_t type = sec->type;
588 if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
589 type == SHT_PREINIT_ARRAY)
590 return true;
591
592 // .got contains pointers to external symbols. They are resolved by
593 // the dynamic linker when a module is loaded into memory, and after
594 // that they are not expected to change. So, it can be in RELRO.
595 if (ctx.in.got && sec == ctx.in.got->getParent())
596 return true;
597
598 // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
599 // through r2 register, which is reserved for that purpose. Since r2 is used
600 // for accessing .got as well, .got and .toc need to be close enough in the
601 // virtual address space. Usually, .toc comes just after .got. Since we place
602 // .got into RELRO, .toc needs to be placed into RELRO too.
603 if (sec->name == ".toc")
604 return true;
605
606 // .got.plt contains pointers to external function symbols. They are
607 // by default resolved lazily, so we usually cannot put it into RELRO.
608 // However, if "-z now" is given, the lazy symbol resolution is
609 // disabled, which enables us to put it into RELRO.
610 if (sec == ctx.in.gotPlt->getParent())
611 return ctx.arg.zNow;
612
613 if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent())
614 return true;
615
616 // .dynamic section contains data for the dynamic linker, and
617 // there's no need to write to it at runtime, so it's better to put
618 // it into RELRO.
619 if (sec->name == ".dynamic")
620 return true;
621
622 // Sections with some special names are put into RELRO. This is a
623 // bit unfortunate because section names shouldn't be significant in
624 // ELF in spirit. But in reality many linker features depend on
625 // magic section names.
626 StringRef s = sec->name;
627
628 bool abiAgnostic = isRelRoDataSection(ctx, secName: s) || s == ".bss.rel.ro" ||
629 s == ".ctors" || s == ".dtors" || s == ".jcr" ||
630 s == ".eh_frame" || s == ".fini_array" ||
631 s == ".init_array" || s == ".preinit_array";
632
633 bool abiSpecific =
634 ctx.arg.osabi == ELFOSABI_OPENBSD && s == ".openbsd.randomdata";
635
636 return abiAgnostic || abiSpecific;
637}
638
639// We compute a rank for each section. The rank indicates where the
640// section should be placed in the file. Instead of using simple
641// numbers (0,1,2...), we use a series of flags. One for each decision
642// point when placing the section.
643// Using flags has two key properties:
644// * It is easy to check if a give branch was taken.
645// * It is easy two see how similar two ranks are (see getRankProximity).
646enum RankFlags {
647 RF_NOT_ADDR_SET = 1 << 27,
648 RF_NOT_ALLOC = 1 << 26,
649 RF_LARGE_EXEC_WRITE = 1 << 16,
650 RF_LARGE_ALT = 1 << 15,
651 RF_WRITE = 1 << 14,
652 RF_EXEC_WRITE = 1 << 13,
653 RF_EXEC = 1 << 12,
654 RF_RODATA = 1 << 11,
655 RF_LARGE_EXEC = 1 << 10,
656 RF_LARGE = 1 << 9,
657 RF_NOT_RELRO = 1 << 8,
658 RF_NOT_TLS = 1 << 7,
659 RF_BSS = 1 << 6,
660};
661
662unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
663 unsigned rank = 0;
664
665 // We want to put section specified by -T option first, so we
666 // can start assigning VA starting from them later.
667 if (ctx.arg.sectionStartMap.contains(Key: osec.name))
668 return rank;
669 rank |= RF_NOT_ADDR_SET;
670
671 // Allocatable sections go first to reduce the total PT_LOAD size and
672 // so debug info doesn't change addresses in actual code.
673 if (!(osec.flags & SHF_ALLOC))
674 return rank | RF_NOT_ALLOC;
675
676 // Sort sections based on their access permission in the following
677 // order: R, RX, RXW, RW(RELRO), RW(non-RELRO).
678 //
679 // Read-only sections come first such that they go in the PT_LOAD covering the
680 // program headers at the start of the file.
681 //
682 // The layout for writable sections is PT_LOAD(PT_GNU_RELRO(.data.rel.ro
683 // .bss.rel.ro) | .data .bss), where | marks where page alignment happens.
684 // An alternative ordering is PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro
685 // .bss.rel.ro) | .bss), but it may waste more bytes due to 2 alignment
686 // places.
687 bool isExec = osec.flags & SHF_EXECINSTR;
688 bool isWrite = osec.flags & SHF_WRITE;
689 bool isLarge = osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64;
690
691 if (!isWrite && !isExec) {
692 // Among PROGBITS sections, place .lrodata further from .text.
693 // For -z lrodata-after-bss, place .lrodata after .lbss like GNU ld. This
694 // layout has one extra PT_LOAD, but alleviates relocation overflow
695 // pressure for absolute relocations referencing small data from -fno-pic
696 // relocatable files.
697 if (isLarge)
698 rank |= ctx.arg.zLrodataAfterBss ? RF_LARGE_ALT : 0;
699 else
700 rank |= ctx.arg.zLrodataAfterBss ? 0 : RF_LARGE;
701
702 if (osec.name == ".interp")
703 rank |= 1;
704 // Put .note sections at the beginning so that they are likely to be
705 // included in a truncate core file. In particular, .note.gnu.build-id, if
706 // available, can identify the object file.
707 else if (osec.type == SHT_NOTE)
708 rank |= 2;
709 // Make PROGBITS sections (e.g .rodata .eh_frame) closer to .text to
710 // alleviate relocation overflow pressure. Large special sections such as
711 // .dynstr and .dynsym can be away from .text.
712 else if (osec.type != SHT_PROGBITS)
713 rank |= 3;
714 else
715 rank |= RF_RODATA;
716 } else if (isExec) {
717 // Place readonly .ltext before .lrodata and writable .ltext after .lbss to
718 // keep writable and readonly segments separate.
719 if (isLarge) {
720 rank |= isWrite ? RF_LARGE_EXEC_WRITE : RF_LARGE_EXEC;
721 } else {
722 rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
723 }
724 } else {
725 rank |= RF_WRITE;
726 // The TLS initialization block needs to be a single contiguous block. Place
727 // TLS sections directly before the other RELRO sections.
728 if (!(osec.flags & SHF_TLS))
729 rank |= RF_NOT_TLS;
730 if (isRelroSection(ctx, sec: &osec))
731 osec.relro = true;
732 else
733 rank |= RF_NOT_RELRO;
734 // Place .ldata and .lbss after .bss. Making .bss closer to .text
735 // alleviates relocation overflow pressure.
736 // For -z lrodata-after-bss, place .lbss/.lrodata/.ldata after .bss.
737 // .bss/.lbss being adjacent reuses the NOBITS size optimization.
738 if (isLarge) {
739 rank |= ctx.arg.zLrodataAfterBss
740 ? (osec.type == SHT_NOBITS ? 1 : RF_LARGE_ALT)
741 : RF_LARGE;
742 }
743 }
744
745 // Within TLS sections, or within other RelRo sections, or within non-RelRo
746 // sections, place non-NOBITS sections first.
747 if (osec.type == SHT_NOBITS)
748 rank |= RF_BSS;
749
750 // Some architectures have additional ordering restrictions for sections
751 // within the same PT_LOAD.
752 if (ctx.arg.emachine == EM_PPC64) {
753 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
754 // that we would like to make sure appear is a specific order to maximize
755 // their coverage by a single signed 16-bit offset from the TOC base
756 // pointer.
757 StringRef name = osec.name;
758 if (name == ".got")
759 rank |= 1;
760 else if (name == ".toc")
761 rank |= 2;
762 }
763
764 if (ctx.arg.emachine == EM_MIPS) {
765 if (osec.name != ".got")
766 rank |= 1;
767 // All sections with SHF_MIPS_GPREL flag should be grouped together
768 // because data in these sections is addressable with a gp relative address.
769 if (osec.flags & SHF_MIPS_GPREL)
770 rank |= 2;
771 }
772
773 if (ctx.arg.emachine == EM_RISCV) {
774 // .sdata and .sbss are placed closer to make GP relaxation more profitable
775 // and match GNU ld.
776 StringRef name = osec.name;
777 if (name == ".sdata" || (osec.type == SHT_NOBITS && name != ".sbss"))
778 rank |= 1;
779 }
780
781 return rank;
782}
783
784static bool compareSections(Ctx &ctx, const SectionCommand *aCmd,
785 const SectionCommand *bCmd) {
786 const OutputSection *a = &cast<OutputDesc>(Val: aCmd)->osec;
787 const OutputSection *b = &cast<OutputDesc>(Val: bCmd)->osec;
788
789 if (a->sortRank != b->sortRank)
790 return a->sortRank < b->sortRank;
791
792 if (!(a->sortRank & RF_NOT_ADDR_SET))
793 return ctx.arg.sectionStartMap.lookup(Key: a->name) <
794 ctx.arg.sectionStartMap.lookup(Key: b->name);
795 return false;
796}
797
798void PhdrEntry::add(OutputSection *sec) {
799 lastSec = sec;
800 if (!firstSec)
801 firstSec = sec;
802 p_align = std::max(a: p_align, b: sec->addralign);
803 if (p_type == PT_LOAD)
804 sec->ptLoad = this;
805}
806
807// A statically linked position-dependent executable should only contain
808// IRELATIVE relocations and no other dynamic relocations. Encapsulation symbols
809// __rel[a]_iplt_{start,end} will be defined for .rel[a].dyn, to be
810// processed by the libc runtime. Other executables or DSOs use dynamic tags
811// instead.
812template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
813 if (ctx.arg.isPic)
814 return;
815
816 // __rela_iplt_{start,end} are initially defined relative to dummy section 0.
817 // We'll override ctx.out.elfHeader with relaDyn later when we are sure that
818 // .rela.dyn will be present in the output.
819 std::string name = ctx.arg.isRela ? "__rela_iplt_start" : "__rel_iplt_start";
820 ctx.sym.relaIpltStart =
821 addOptionalRegular(ctx, name, sec: ctx.out.elfHeader.get(), val: 0, stOther: STV_HIDDEN);
822 name.replace(pos: name.size() - 5, n1: 5, s: "end");
823 ctx.sym.relaIpltEnd =
824 addOptionalRegular(ctx, name, sec: ctx.out.elfHeader.get(), val: 0, stOther: STV_HIDDEN);
825}
826
827static bool updateRelIpltSymbols(Ctx &ctx) {
828 if (ctx.sym.relaIpltStart) {
829 auto &dyn = getIRelativeSection(ctx);
830 if (dyn.isNeeded()) {
831 SectionBase *oldSec = ctx.sym.relaIpltEnd->section;
832 uint64_t oldVal = ctx.sym.relaIpltEnd->value;
833 ctx.sym.relaIpltStart->section = &dyn;
834 ctx.sym.relaIpltEnd->section = &dyn;
835 ctx.sym.relaIpltEnd->value = dyn.getSize();
836 return (oldSec != ctx.sym.relaIpltEnd->section ||
837 oldVal != ctx.sym.relaIpltEnd->value);
838 }
839 }
840 return false;
841}
842
843// This function generates assignments for predefined symbols (e.g. _end or
844// _etext) and inserts them into the commands sequence to be processed at the
845// appropriate time. This ensures that the value is going to be correct by the
846// time any references to these symbols are processed and is equivalent to
847// defining these symbols explicitly in the linker script.
848template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
849 if (ctx.sym.globalOffsetTable) {
850 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
851 // to the start of the .got or .got.plt section.
852 InputSection *sec = ctx.in.gotPlt.get();
853 if (!ctx.target->gotBaseSymInGotPlt)
854 sec = ctx.in.mipsGot ? cast<InputSection>(Val: ctx.in.mipsGot.get())
855 : cast<InputSection>(Val: ctx.in.got.get());
856 ctx.sym.globalOffsetTable->section = sec;
857 }
858
859 // .rela_iplt_{start,end} mark the start and the end of the section containing
860 // IRELATIVE relocations.
861 (void)updateRelIpltSymbols(ctx);
862
863 PhdrEntry *last = nullptr;
864 OutputSection *lastRO = nullptr;
865 auto isLarge = [&ctx = ctx](OutputSection *osec) {
866 return ctx.arg.emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
867 };
868 for (auto &p : ctx.phdrs) {
869 if (p->p_type != PT_LOAD)
870 continue;
871 last = p.get();
872 if (!(p->p_flags & PF_W) && p->lastSec && !isLarge(p->lastSec))
873 lastRO = p->lastSec;
874 }
875
876 if (lastRO) {
877 // _etext is the first location after the last read-only loadable segment
878 // that does not contain large sections.
879 if (ctx.sym.etext1)
880 ctx.sym.etext1->section = lastRO;
881 if (ctx.sym.etext2)
882 ctx.sym.etext2->section = lastRO;
883 }
884
885 if (last) {
886 // _edata points to the end of the last non-large mapped initialized
887 // section.
888 OutputSection *edata = nullptr;
889 for (OutputSection *os : ctx.outputSections) {
890 if (os->type != SHT_NOBITS && !isLarge(os))
891 edata = os;
892 if (os == last->lastSec)
893 break;
894 }
895
896 if (ctx.sym.edata1)
897 ctx.sym.edata1->section = edata;
898 if (ctx.sym.edata2)
899 ctx.sym.edata2->section = edata;
900
901 // _end is the first location after the uninitialized data region.
902 if (ctx.sym.end1)
903 ctx.sym.end1->section = last->lastSec;
904 if (ctx.sym.end2)
905 ctx.sym.end2->section = last->lastSec;
906 }
907
908 if (ctx.sym.bss) {
909 // On RISC-V, set __bss_start to the start of .sbss if present.
910 OutputSection *sbss =
911 ctx.arg.emachine == EM_RISCV ? findSection(ctx, name: ".sbss") : nullptr;
912 ctx.sym.bss->section = sbss ? sbss : findSection(ctx, name: ".bss");
913 }
914
915 // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
916 // be equal to the _gp symbol's value.
917 if (ctx.sym.mipsGp) {
918 // Find GP-relative section with the lowest address
919 // and use this address to calculate default _gp value.
920 for (OutputSection *os : ctx.outputSections) {
921 if (os->flags & SHF_MIPS_GPREL) {
922 ctx.sym.mipsGp->section = os;
923 ctx.sym.mipsGp->value = 0x7ff0;
924 break;
925 }
926 }
927 }
928}
929
930// We want to find how similar two ranks are.
931// The more branches in getSectionRank that match, the more similar they are.
932// Since each branch corresponds to a bit flag, we can just use
933// countLeadingZeros.
934static int getRankProximity(OutputSection *a, SectionCommand *b) {
935 auto *osd = dyn_cast<OutputDesc>(Val: b);
936 return (osd && osd->osec.hasInputSections)
937 ? llvm::countl_zero(Val: a->sortRank ^ osd->osec.sortRank)
938 : -1;
939}
940
941// When placing orphan sections, we want to place them after symbol assignments
942// so that an orphan after
943// begin_foo = .;
944// foo : { *(foo) }
945// end_foo = .;
946// doesn't break the intended meaning of the begin/end symbols.
947// We don't want to go over sections since findOrphanPos is the
948// one in charge of deciding the order of the sections.
949// We don't want to go over changes to '.', since doing so in
950// rx_sec : { *(rx_sec) }
951// . = ALIGN(0x1000);
952// /* The RW PT_LOAD starts here*/
953// rw_sec : { *(rw_sec) }
954// would mean that the RW PT_LOAD would become unaligned.
955static bool shouldSkip(SectionCommand *cmd) {
956 if (auto *assign = dyn_cast<SymbolAssignment>(Val: cmd))
957 return assign->name != ".";
958 return false;
959}
960
961// We want to place orphan sections so that they share as much
962// characteristics with their neighbors as possible. For example, if
963// both are rw, or both are tls.
964static SmallVectorImpl<SectionCommand *>::iterator
965findOrphanPos(Ctx &ctx, SmallVectorImpl<SectionCommand *>::iterator b,
966 SmallVectorImpl<SectionCommand *>::iterator e) {
967 // Place non-alloc orphan sections at the end. This matches how we assign file
968 // offsets to non-alloc sections.
969 OutputSection *sec = &cast<OutputDesc>(Val: *e)->osec;
970 if (!(sec->flags & SHF_ALLOC))
971 return e;
972
973 // As a special case, place .relro_padding before the SymbolAssignment using
974 // DATA_SEGMENT_RELRO_END, if present.
975 if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent()) {
976 auto i = std::find_if(first: b, last: e, pred: [=](SectionCommand *a) {
977 if (auto *assign = dyn_cast<SymbolAssignment>(Val: a))
978 return assign->dataSegmentRelroEnd;
979 return false;
980 });
981 if (i != e)
982 return i;
983 }
984
985 // Find the most similar output section as the anchor. Rank Proximity is a
986 // value in the range [-1, 32] where [0, 32] indicates potential anchors (0:
987 // least similar; 32: identical). -1 means not an anchor.
988 //
989 // In the event of proximity ties, we select the first or last section
990 // depending on whether the orphan's rank is smaller.
991 int maxP = 0;
992 auto i = e;
993 for (auto j = b; j != e; ++j) {
994 int p = getRankProximity(a: sec, b: *j);
995 if (p > maxP ||
996 (p == maxP && cast<OutputDesc>(Val: *j)->osec.sortRank <= sec->sortRank)) {
997 maxP = p;
998 i = j;
999 }
1000 }
1001 if (i == e)
1002 return e;
1003
1004 auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
1005 auto *osd = dyn_cast<OutputDesc>(Val: cmd);
1006 return osd && osd->osec.hasInputSections;
1007 };
1008
1009 // Then, scan backward or forward through the script for a suitable insertion
1010 // point. If i's rank is larger, the orphan section can be placed before i.
1011 //
1012 // However, don't do this if custom program headers are defined. Otherwise,
1013 // adding the orphan to a previous segment can change its flags, for example,
1014 // making a read-only segment writable. If memory regions are defined, an
1015 // orphan section should continue the same region as the found section to
1016 // better resemble the behavior of GNU ld.
1017 bool mustAfter =
1018 ctx.script->hasPhdrsCommands() || !ctx.script->memoryRegions.empty();
1019 if (cast<OutputDesc>(Val: *i)->osec.sortRank <= sec->sortRank || mustAfter) {
1020 for (auto j = ++i; j != e; ++j) {
1021 if (!isOutputSecWithInputSections(*j))
1022 continue;
1023 if (getRankProximity(a: sec, b: *j) != maxP)
1024 break;
1025 i = j + 1;
1026 }
1027 } else {
1028 for (; i != b; --i)
1029 if (isOutputSecWithInputSections(i[-1]))
1030 break;
1031 }
1032
1033 // As a special case, if the orphan section is the last section, put
1034 // it at the very end, past any other commands.
1035 // This matches bfd's behavior and is convenient when the linker script fully
1036 // specifies the start of the file, but doesn't care about the end (the non
1037 // alloc sections for example).
1038 if (std::none_of(first: i, last: e, pred: isOutputSecWithInputSections))
1039 return e;
1040
1041 while (i != e && shouldSkip(cmd: *i))
1042 ++i;
1043 return i;
1044}
1045
1046// Adds random priorities to sections not already in the map.
1047static void maybeShuffle(Ctx &ctx,
1048 DenseMap<const InputSectionBase *, int> &order) {
1049 if (ctx.arg.shuffleSections.empty())
1050 return;
1051
1052 SmallVector<InputSectionBase *, 0> matched, sections = ctx.inputSections;
1053 matched.reserve(N: sections.size());
1054 for (const auto &patAndSeed : ctx.arg.shuffleSections) {
1055 matched.clear();
1056 for (InputSectionBase *sec : sections)
1057 if (patAndSeed.first.match(S: sec->name))
1058 matched.push_back(Elt: sec);
1059 const uint32_t seed = patAndSeed.second;
1060 if (seed == UINT32_MAX) {
1061 // If --shuffle-sections <section-glob>=-1, reverse the section order. The
1062 // section order is stable even if the number of sections changes. This is
1063 // useful to catch issues like static initialization order fiasco
1064 // reliably.
1065 std::reverse(first: matched.begin(), last: matched.end());
1066 } else {
1067 std::mt19937 g(seed ? seed : std::random_device()());
1068 llvm::shuffle(first: matched.begin(), last: matched.end(), g);
1069 }
1070 size_t i = 0;
1071 for (InputSectionBase *&sec : sections)
1072 if (patAndSeed.first.match(S: sec->name))
1073 sec = matched[i++];
1074 }
1075
1076 // Existing priorities are < 0, so use priorities >= 0 for the missing
1077 // sections.
1078 int prio = 0;
1079 for (InputSectionBase *sec : sections) {
1080 if (order.try_emplace(Key: sec, Args&: prio).second)
1081 ++prio;
1082 }
1083}
1084
1085// Return section order within an InputSectionDescription.
1086// If both --symbol-ordering-file and call graph profile are present, the order
1087// file takes precedence, but the call graph profile is still used for symbols
1088// that don't appear in the order file.
1089static DenseMap<const InputSectionBase *, int> buildSectionOrder(Ctx &ctx) {
1090 DenseMap<const InputSectionBase *, int> sectionOrder;
1091 if (ctx.arg.bpStartupFunctionSort || ctx.arg.bpFunctionOrderForCompression ||
1092 ctx.arg.bpDataOrderForCompression ||
1093 !ctx.arg.bpCompressionSortSpecs.empty()) {
1094 TimeTraceScope timeScope("Balanced Partitioning Section Orderer");
1095 sectionOrder = runBalancedPartitioning(
1096 ctx, profilePath: ctx.arg.bpStartupFunctionSort ? ctx.arg.irpgoProfilePath : "",
1097 compressionSortSpecs: ctx.arg.bpCompressionSortSpecs, forFunctionCompression: ctx.arg.bpFunctionOrderForCompression,
1098 forDataCompression: ctx.arg.bpDataOrderForCompression,
1099 compressionSortStartupFunctions: ctx.arg.bpCompressionSortStartupFunctions,
1100 verbose: ctx.arg.bpVerboseSectionOrderer);
1101 } else if (!ctx.arg.callGraphProfile.empty()) {
1102 sectionOrder = computeCallGraphProfileOrder(ctx);
1103 }
1104
1105 if (ctx.arg.symbolOrderingFile.empty())
1106 return sectionOrder;
1107
1108 struct SymbolOrderEntry {
1109 int priority;
1110 bool present;
1111 };
1112
1113 // Build a map from symbols to their priorities. Symbols that didn't
1114 // appear in the symbol ordering file have the lowest priority 0.
1115 // All explicitly mentioned symbols have negative (higher) priorities.
1116 DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
1117 int priority = -sectionOrder.size() - ctx.arg.symbolOrderingFile.size();
1118 for (StringRef s : ctx.arg.symbolOrderingFile)
1119 symbolOrder.insert(KV: {CachedHashStringRef(s), {.priority: priority++, .present: false}});
1120
1121 // Build a map from sections to their priorities.
1122 auto addSym = [&](Symbol &sym) {
1123 auto it = symbolOrder.find(Val: CachedHashStringRef(sym.getName()));
1124 if (it == symbolOrder.end())
1125 return;
1126 SymbolOrderEntry &ent = it->second;
1127 ent.present = true;
1128
1129 maybeWarnUnorderableSymbol(ctx, sym: &sym);
1130
1131 if (auto *d = dyn_cast<Defined>(Val: &sym)) {
1132 if (auto *sec = dyn_cast_or_null<InputSectionBase>(Val: d->section)) {
1133 int &priority = sectionOrder[cast<InputSectionBase>(Val: sec)];
1134 priority = std::min(a: priority, b: ent.priority);
1135 }
1136 }
1137 };
1138
1139 // We want both global and local symbols. We get the global ones from the
1140 // symbol table and iterate the object files for the local ones.
1141 for (Symbol *sym : ctx.symtab->getSymbols())
1142 addSym(*sym);
1143
1144 for (ELFFileBase *file : ctx.objectFiles)
1145 for (Symbol *sym : file->getLocalSymbols())
1146 addSym(*sym);
1147
1148 if (ctx.arg.warnSymbolOrdering)
1149 for (auto orderEntry : symbolOrder)
1150 if (!orderEntry.second.present)
1151 Warn(ctx) << "symbol ordering file: no such symbol: "
1152 << orderEntry.first.val();
1153
1154 return sectionOrder;
1155}
1156
1157// Sorts the sections in ISD according to the provided section order.
1158static void
1159sortISDBySectionOrder(Ctx &ctx, InputSectionDescription *isd,
1160 const DenseMap<const InputSectionBase *, int> &order,
1161 bool executableOutputSection) {
1162 SmallVector<InputSection *, 0> unorderedSections;
1163 SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
1164 uint64_t unorderedSize = 0;
1165 uint64_t totalSize = 0;
1166
1167 for (InputSection *isec : isd->sections) {
1168 if (executableOutputSection)
1169 totalSize += isec->getSize();
1170 auto i = order.find(Val: isec);
1171 if (i == order.end()) {
1172 unorderedSections.push_back(Elt: isec);
1173 unorderedSize += isec->getSize();
1174 continue;
1175 }
1176 orderedSections.push_back(Elt: {isec, i->second});
1177 }
1178 llvm::sort(C&: orderedSections, Comp: llvm::less_second());
1179
1180 // Find an insertion point for the ordered section list in the unordered
1181 // section list. On targets with limited-range branches, this is the mid-point
1182 // of the unordered section list. This decreases the likelihood that a range
1183 // extension thunk will be needed to enter or exit the ordered region. If the
1184 // ordered section list is a list of hot functions, we can generally expect
1185 // the ordered functions to be called more often than the unordered functions,
1186 // making it more likely that any particular call will be within range, and
1187 // therefore reducing the number of thunks required.
1188 //
1189 // For example, imagine that you have 8MB of hot code and 32MB of cold code.
1190 // If the layout is:
1191 //
1192 // 8MB hot
1193 // 32MB cold
1194 //
1195 // only the first 8-16MB of the cold code (depending on which hot function it
1196 // is actually calling) can call the hot code without a range extension thunk.
1197 // However, if we use this layout:
1198 //
1199 // 16MB cold
1200 // 8MB hot
1201 // 16MB cold
1202 //
1203 // both the last 8-16MB of the first block of cold code and the first 8-16MB
1204 // of the second block of cold code can call the hot code without a thunk. So
1205 // we effectively double the amount of code that could potentially call into
1206 // the hot code without a thunk.
1207 //
1208 // The above is not necessary if total size of input sections in this "isd"
1209 // is small. Note that we assume all input sections are executable if the
1210 // output section is executable (which is not always true but supposed to
1211 // cover most cases).
1212 size_t insPt = 0;
1213 if (executableOutputSection && !orderedSections.empty() &&
1214 ctx.target->getThunkSectionSpacing() &&
1215 totalSize >= ctx.target->getThunkSectionSpacing()) {
1216 uint64_t unorderedPos = 0;
1217 for (; insPt != unorderedSections.size(); ++insPt) {
1218 unorderedPos += unorderedSections[insPt]->getSize();
1219 if (unorderedPos > unorderedSize / 2)
1220 break;
1221 }
1222 }
1223
1224 isd->sections.clear();
1225 for (InputSection *isec : ArrayRef(unorderedSections).slice(N: 0, M: insPt))
1226 isd->sections.push_back(Elt: isec);
1227 for (std::pair<InputSection *, int> p : orderedSections)
1228 isd->sections.push_back(Elt: p.first);
1229 for (InputSection *isec : ArrayRef(unorderedSections).slice(N: insPt))
1230 isd->sections.push_back(Elt: isec);
1231}
1232
1233static void sortSection(Ctx &ctx, OutputSection &osec,
1234 const DenseMap<const InputSectionBase *, int> &order) {
1235 StringRef name = osec.name;
1236
1237 // Never sort these.
1238 if (name == ".init" || name == ".fini")
1239 return;
1240
1241 // Sort input sections by priority using the list provided by
1242 // --symbol-ordering-file or --shuffle-sections=. This is a least significant
1243 // digit radix sort. The sections may be sorted stably again by a more
1244 // significant key.
1245 if (!order.empty())
1246 for (SectionCommand *b : osec.commands)
1247 if (auto *isd = dyn_cast<InputSectionDescription>(Val: b))
1248 sortISDBySectionOrder(ctx, isd, order, executableOutputSection: osec.flags & SHF_EXECINSTR);
1249
1250 if (ctx.script->hasSectionsCommand)
1251 return;
1252
1253 if (name == ".init_array" || name == ".fini_array") {
1254 osec.sortInitFini();
1255 } else if (name == ".ctors" || name == ".dtors") {
1256 osec.sortCtorsDtors();
1257 } else if (ctx.arg.emachine == EM_PPC64 && name == ".toc") {
1258 // .toc is allocated just after .got and is accessed using GOT-relative
1259 // relocations. Object files compiled with small code model have an
1260 // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1261 // To reduce the risk of relocation overflow, .toc contents are sorted so
1262 // that sections having smaller relocation offsets are at beginning of .toc
1263 assert(osec.commands.size() == 1);
1264 auto *isd = cast<InputSectionDescription>(Val: osec.commands[0]);
1265 llvm::stable_sort(Range&: isd->sections,
1266 C: [](const InputSection *a, const InputSection *b) -> bool {
1267 return a->file->ppc64SmallCodeModelTocRelocs &&
1268 !b->file->ppc64SmallCodeModelTocRelocs;
1269 });
1270 }
1271}
1272
1273// Sort sections within each InputSectionDescription.
1274template <class ELFT> void Writer<ELFT>::sortInputSections() {
1275 // Assign negative priorities.
1276 DenseMap<const InputSectionBase *, int> order = buildSectionOrder(ctx);
1277 // Assign non-negative priorities due to --shuffle-sections.
1278 maybeShuffle(ctx, order);
1279 for (SectionCommand *cmd : ctx.script->sectionCommands)
1280 if (auto *osd = dyn_cast<OutputDesc>(Val: cmd))
1281 sortSection(ctx, osec&: osd->osec, order);
1282}
1283
1284template <class ELFT> void Writer<ELFT>::sortSections() {
1285 llvm::TimeTraceScope timeScope("Sort sections");
1286
1287 // Don't sort if using -r. It is not necessary and we want to preserve the
1288 // relative order for SHF_LINK_ORDER sections.
1289 if (ctx.arg.relocatable) {
1290 ctx.script->adjustOutputSections();
1291 return;
1292 }
1293
1294 sortInputSections();
1295
1296 for (SectionCommand *cmd : ctx.script->sectionCommands)
1297 if (auto *osd = dyn_cast<OutputDesc>(Val: cmd))
1298 osd->osec.sortRank = getSectionRank(ctx, osec&: osd->osec);
1299 if (!ctx.script->hasSectionsCommand) {
1300 // OutputDescs are mostly contiguous, but may be interleaved with
1301 // SymbolAssignments in the presence of INSERT commands.
1302 auto mid = std::stable_partition(
1303 ctx.script->sectionCommands.begin(), ctx.script->sectionCommands.end(),
1304 [](SectionCommand *cmd) { return isa<OutputDesc>(Val: cmd); });
1305 std::stable_sort(
1306 ctx.script->sectionCommands.begin(), mid,
1307 [&ctx = ctx](auto *l, auto *r) { return compareSections(ctx, l, r); });
1308 }
1309
1310 // Process INSERT commands and update output section attributes. From this
1311 // point onwards the order of script->sectionCommands is fixed.
1312 ctx.script->processInsertCommands();
1313 ctx.script->adjustOutputSections();
1314
1315 if (ctx.script->hasSectionsCommand)
1316 sortOrphanSections();
1317
1318 ctx.script->adjustSectionsAfterSorting();
1319}
1320
1321template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
1322 // Orphan sections are sections present in the input files which are
1323 // not explicitly placed into the output file by the linker script.
1324 //
1325 // The sections in the linker script are already in the correct
1326 // order. We have to figuere out where to insert the orphan
1327 // sections.
1328 //
1329 // The order of the sections in the script is arbitrary and may not agree with
1330 // compareSections. This means that we cannot easily define a strict weak
1331 // ordering. To see why, consider a comparison of a section in the script and
1332 // one not in the script. We have a two simple options:
1333 // * Make them equivalent (a is not less than b, and b is not less than a).
1334 // The problem is then that equivalence has to be transitive and we can
1335 // have sections a, b and c with only b in a script and a less than c
1336 // which breaks this property.
1337 // * Use compareSectionsNonScript. Given that the script order doesn't have
1338 // to match, we can end up with sections a, b, c, d where b and c are in the
1339 // script and c is compareSectionsNonScript less than b. In which case d
1340 // can be equivalent to c, a to b and d < a. As a concrete example:
1341 // .a (rx) # not in script
1342 // .b (rx) # in script
1343 // .c (ro) # in script
1344 // .d (ro) # not in script
1345 //
1346 // The way we define an order then is:
1347 // * Sort only the orphan sections. They are in the end right now.
1348 // * Move each orphan section to its preferred position. We try
1349 // to put each section in the last position where it can share
1350 // a PT_LOAD.
1351 //
1352 // There is some ambiguity as to where exactly a new entry should be
1353 // inserted, because Commands contains not only output section
1354 // commands but also other types of commands such as symbol assignment
1355 // expressions. There's no correct answer here due to the lack of the
1356 // formal specification of the linker script. We use heuristics to
1357 // determine whether a new output command should be added before or
1358 // after another commands. For the details, look at shouldSkip
1359 // function.
1360
1361 auto i = ctx.script->sectionCommands.begin();
1362 auto e = ctx.script->sectionCommands.end();
1363 auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {
1364 if (auto *osd = dyn_cast<OutputDesc>(Val: cmd))
1365 return osd->osec.sectionIndex == UINT32_MAX;
1366 return false;
1367 });
1368
1369 // Sort the orphan sections.
1370 std::stable_sort(nonScriptI, e, [&ctx = ctx](auto *l, auto *r) {
1371 return compareSections(ctx, l, r);
1372 });
1373
1374 // As a horrible special case, skip the first . assignment if it is before any
1375 // section. We do this because it is common to set a load address by starting
1376 // the script with ". = 0xabcd" and the expectation is that every section is
1377 // after that.
1378 auto firstSectionOrDotAssignment =
1379 std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });
1380 if (firstSectionOrDotAssignment != e &&
1381 isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1382 ++firstSectionOrDotAssignment;
1383 i = firstSectionOrDotAssignment;
1384
1385 while (nonScriptI != e) {
1386 auto pos = findOrphanPos(ctx, i, nonScriptI);
1387 OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;
1388
1389 // As an optimization, find all sections with the same sort rank
1390 // and insert them with one rotate.
1391 unsigned rank = orphan->sortRank;
1392 auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {
1393 return cast<OutputDesc>(Val: cmd)->osec.sortRank != rank;
1394 });
1395 std::rotate(pos, nonScriptI, end);
1396 nonScriptI = end;
1397 }
1398}
1399
1400static bool compareByFilePosition(InputSection *a, InputSection *b) {
1401 InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1402 InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1403 // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1404 // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1405 if (!la || !lb)
1406 return la && !lb;
1407 OutputSection *aOut = la->getParent();
1408 OutputSection *bOut = lb->getParent();
1409
1410 if (aOut == bOut)
1411 return la->outSecOff < lb->outSecOff;
1412 if (aOut->addr == bOut->addr)
1413 return aOut->sectionIndex < bOut->sectionIndex;
1414 return aOut->addr < bOut->addr;
1415}
1416
1417template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1418 llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
1419 for (OutputSection *sec : ctx.outputSections) {
1420 if (!(sec->flags & SHF_LINK_ORDER))
1421 continue;
1422
1423 // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1424 // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1425 if (!ctx.arg.relocatable && ctx.arg.emachine == EM_ARM &&
1426 sec->type == SHT_ARM_EXIDX)
1427 continue;
1428
1429 // Link order may be distributed across several InputSectionDescriptions.
1430 // Sorting is performed separately.
1431 SmallVector<InputSection **, 0> scriptSections;
1432 SmallVector<InputSection *, 0> sections;
1433 for (SectionCommand *cmd : sec->commands) {
1434 auto *isd = dyn_cast<InputSectionDescription>(Val: cmd);
1435 if (!isd)
1436 continue;
1437 bool hasLinkOrder = false;
1438 scriptSections.clear();
1439 sections.clear();
1440 for (InputSection *&isec : isd->sections) {
1441 if (isec->flags & SHF_LINK_ORDER) {
1442 InputSection *link = isec->getLinkOrderDep();
1443 if (link && !link->getParent())
1444 ErrAlways(ctx) << isec << ": sh_link points to discarded section "
1445 << link;
1446 hasLinkOrder = true;
1447 }
1448 scriptSections.push_back(Elt: &isec);
1449 sections.push_back(Elt: isec);
1450 }
1451 if (hasLinkOrder && errCount(ctx) == 0) {
1452 llvm::stable_sort(Range&: sections, C: compareByFilePosition);
1453 for (int i = 0, n = sections.size(); i != n; ++i)
1454 *scriptSections[i] = sections[i];
1455 }
1456 }
1457 }
1458}
1459
1460static void finalizeSynthetic(Ctx &ctx, SyntheticSection *sec) {
1461 if (sec && sec->isNeeded() && sec->getParent()) {
1462 llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
1463 sec->finalizeContents();
1464 }
1465}
1466
1467static bool canInsertPadding(OutputSection *sec) {
1468 StringRef s = sec->name;
1469 return s == ".bss" || s == ".data" || s == ".data.rel.ro" || s == ".lbss" ||
1470 s == ".ldata" || s == ".lrodata" || s == ".ltext" || s == ".rodata" ||
1471 s.starts_with(Prefix: ".text");
1472}
1473
1474static void randomizeSectionPadding(Ctx &ctx) {
1475 std::mt19937 g(*ctx.arg.randomizeSectionPadding);
1476 PhdrEntry *curPtLoad = nullptr;
1477 for (OutputSection *os : ctx.outputSections) {
1478 if (!canInsertPadding(sec: os))
1479 continue;
1480 for (SectionCommand *bc : os->commands) {
1481 if (auto *isd = dyn_cast<InputSectionDescription>(Val: bc)) {
1482 SmallVector<InputSection *, 0> tmp;
1483 if (os->ptLoad != curPtLoad) {
1484 tmp.push_back(
1485 Elt: make<PaddingSection>(args&: ctx, args: g() % ctx.arg.maxPageSize, args&: os));
1486 curPtLoad = os->ptLoad;
1487 }
1488 for (InputSection *isec : isd->sections) {
1489 // Probability of inserting padding is 1 in 16.
1490 if (g() % 16 == 0)
1491 tmp.push_back(Elt: make<PaddingSection>(args&: ctx, args&: isec->addralign, args&: os));
1492 tmp.push_back(Elt: isec);
1493 }
1494 isd->sections = std::move(tmp);
1495 }
1496 }
1497 }
1498}
1499
1500// We need to generate and finalize the content that depends on the address of
1501// InputSections. As the generation of the content may also alter InputSection
1502// addresses we must converge to a fixed point. We do that here. See the comment
1503// in Writer<ELFT>::finalizeSections().
1504template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1505 llvm::TimeTraceScope timeScope("Finalize address dependent content");
1506 AArch64Err843419Patcher a64p(ctx);
1507 ARMErr657417Patcher a32p(ctx);
1508
1509 // Transforms that move input sections without reading addresses go here.
1510 // After the first assignAddresses, only the loop below may change layout.
1511 if (ctx.arg.randomizeSectionPadding)
1512 randomizeSectionPadding(ctx);
1513 if (ctx.arg.branchToBranch)
1514 ctx.target->relaxCFIJumpTables();
1515 ctx.script->assignAddresses();
1516
1517 // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
1518 // do require the relative addresses of OutputSections because linker scripts
1519 // can assign Virtual Addresses to OutputSections that are not monotonically
1520 // increasing. Anything here must be repeatable, since spilling may change
1521 // section order.
1522 const auto finalizeOrderDependentContent = [this] {
1523 finalizeSynthetic(ctx, sec: ctx.in.armExidx.get());
1524 resolveShfLinkOrder();
1525 };
1526 finalizeOrderDependentContent();
1527
1528 // Iterate until a fixed point is reached, skipping relocatable links since
1529 // the final addresses are unavailable.
1530 uint32_t pass = 0, assignPasses = 0;
1531 while (!ctx.arg.relocatable) {
1532 bool changed = ctx.target->needsThunks
1533 ? tc.createThunks(pass, outputSections: ctx.outputSections)
1534 : ctx.target->relaxOnce(pass);
1535 bool spilled = ctx.script->spillSections();
1536 changed |= spilled;
1537 ++pass;
1538
1539 // With Thunk Size much smaller than branch range we expect to
1540 // converge quickly; if we get to 30 something has gone wrong.
1541 if (changed && pass >= 30) {
1542 Err(ctx) << "address assignment did not converge";
1543 break;
1544 }
1545
1546 if (ctx.arg.fixCortexA53Errata843419) {
1547 if (changed)
1548 ctx.script->assignAddresses();
1549 changed |= a64p.createFixes();
1550 }
1551 if (ctx.arg.fixCortexA8) {
1552 if (changed)
1553 ctx.script->assignAddresses();
1554 changed |= a32p.createFixes();
1555 }
1556
1557 finalizeSynthetic(ctx, sec: ctx.in.got.get());
1558 if (ctx.in.mipsGot)
1559 ctx.in.mipsGot->updateAllocSize(ctx);
1560
1561 // The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
1562 // encode the signing schema. We've put relocations in .relr.auth.dyn
1563 // during RelocationScanner::processAux, but the target VA for some of
1564 // them might be wider than 32 bits. We can only know the final VA at this
1565 // point, so move relocations with large values from .relr.auth.dyn to
1566 // .rela.dyn. See also AArch64::relocate.
1567 if (ctx.in.relrAuthDyn) {
1568 auto it = llvm::remove_if(
1569 ctx.in.relrAuthDyn->relocs, [this](const RelativeReloc &elem) {
1570 Relocation &reloc = elem.inputSec->relocs()[elem.relocIdx];
1571 if (isInt<32>(x: reloc.sym->getVA(ctx, addend: reloc.addend)))
1572 return false;
1573 reloc.expr = R_NONE;
1574 ctx.in.relaDyn->addReloc(reloc: {R_AARCH64_AUTH_RELATIVE, elem.inputSec,
1575 reloc.offset, false, *reloc.sym,
1576 reloc.addend, R_ABS});
1577 return true;
1578 });
1579 changed |= (it != ctx.in.relrAuthDyn->relocs.end());
1580 ctx.in.relrAuthDyn->relocs.erase(it, ctx.in.relrAuthDyn->relocs.end());
1581 }
1582 if (ctx.in.relaDyn)
1583 changed |= ctx.in.relaDyn->updateAllocSize(ctx);
1584 if (ctx.in.relrDyn)
1585 changed |= ctx.in.relrDyn->updateAllocSize(ctx);
1586 if (ctx.in.relrAuthDyn)
1587 changed |= ctx.in.relrAuthDyn->updateAllocSize(ctx);
1588 if (ctx.in.dynamic && ctx.in.dynamic->getParent()) {
1589 size_t oldSize = ctx.in.dynamic->getSize();
1590 finalizeSynthetic(ctx, sec: ctx.in.dynamic.get());
1591 changed |= (oldSize != ctx.in.dynamic->getSize());
1592 }
1593
1594 // .rela_iplt_{start,end} mark the start and the end of the section
1595 // containing IRELATIVE relocations. Update them on each iteration because
1596 // they might be affected by the above move of relocations from
1597 // .relr.auth.dyn to .rela.dyn.
1598 changed |= updateRelIpltSymbols(ctx);
1599
1600 if (ctx.in.memtagGlobalDescriptors)
1601 changed |= ctx.in.memtagGlobalDescriptors->updateAllocSize(ctx);
1602 if (ctx.in.ehFrameHdr && ctx.in.ehFrameHdr->isNeeded())
1603 changed |= ctx.in.ehFrameHdr->updateAllocSize(ctx);
1604
1605 std::pair<const OutputSection *, const Defined *> changes =
1606 ctx.script->assignAddresses();
1607 if (!changed) {
1608 // Some symbols may be dependent on section addresses. When we break the
1609 // loop, the symbol values are finalized because a previous
1610 // assignAddresses() finalized section addresses.
1611 if (!changes.first && !changes.second)
1612 break;
1613 if (++assignPasses == 5) {
1614 if (changes.first)
1615 Err(ctx) << "address (0x" << Twine::utohexstr(Val: changes.first->addr)
1616 << ") of section '" << changes.first->name
1617 << "' does not converge";
1618 if (changes.second)
1619 Err(ctx) << "assignment to symbol " << changes.second
1620 << " does not converge";
1621 break;
1622 }
1623 } else if (spilled) {
1624 // Spilling can change relative section order.
1625 finalizeOrderDependentContent();
1626 }
1627 // 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.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.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(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_in_memory;
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