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