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