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