1 //===- LinkerScript.cpp ---------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the parser/evaluator of the linker script. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "LinkerScript.h" 15 #include "Config.h" 16 #include "InputSection.h" 17 #include "Memory.h" 18 #include "OutputSections.h" 19 #include "Strings.h" 20 #include "SymbolTable.h" 21 #include "Symbols.h" 22 #include "SyntheticSections.h" 23 #include "Target.h" 24 #include "Writer.h" 25 #include "lld/Common/Threads.h" 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/BinaryFormat/ELF.h" 29 #include "llvm/Support/Casting.h" 30 #include "llvm/Support/Endian.h" 31 #include "llvm/Support/ErrorHandling.h" 32 #include "llvm/Support/FileSystem.h" 33 #include "llvm/Support/Path.h" 34 #include <algorithm> 35 #include <cassert> 36 #include <cstddef> 37 #include <cstdint> 38 #include <iterator> 39 #include <limits> 40 #include <string> 41 #include <vector> 42 43 using namespace llvm; 44 using namespace llvm::ELF; 45 using namespace llvm::object; 46 using namespace llvm::support::endian; 47 using namespace lld; 48 using namespace lld::elf; 49 50 LinkerScript *elf::Script; 51 52 static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) { 53 if (OutputSection *OS = InputSec->getOutputSection()) 54 return OS->Addr; 55 error(Loc + ": unable to evaluate expression: input section " + 56 InputSec->Name + " has no output section assigned"); 57 return 0; 58 } 59 60 uint64_t ExprValue::getValue() const { 61 if (Sec) 62 return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc), 63 Alignment); 64 return alignTo(Val, Alignment); 65 } 66 67 uint64_t ExprValue::getSecAddr() const { 68 if (Sec) 69 return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc); 70 return 0; 71 } 72 73 uint64_t ExprValue::getSectionOffset() const { 74 // If the alignment is trivial, we don't have to compute the full 75 // value to know the offset. This allows this function to succeed in 76 // cases where the output section is not yet known. 77 if (Alignment == 1) 78 return Val; 79 return getValue() - getSecAddr(); 80 } 81 82 OutputSection *LinkerScript::createOutputSection(StringRef Name, 83 StringRef Location) { 84 OutputSection *&SecRef = NameToOutputSection[Name]; 85 OutputSection *Sec; 86 if (SecRef && SecRef->Location.empty()) { 87 // There was a forward reference. 88 Sec = SecRef; 89 } else { 90 Sec = make<OutputSection>(Name, SHT_PROGBITS, 0); 91 if (!SecRef) 92 SecRef = Sec; 93 } 94 Sec->Location = Location; 95 return Sec; 96 } 97 98 OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) { 99 OutputSection *&CmdRef = NameToOutputSection[Name]; 100 if (!CmdRef) 101 CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0); 102 return CmdRef; 103 } 104 105 void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) { 106 uint64_t Val = E().getValue(); 107 if (Val < Dot && InSec) 108 error(Loc + ": unable to move location counter backward for: " + 109 Ctx->OutSec->Name); 110 Dot = Val; 111 112 // Update to location counter means update to section size. 113 if (InSec) 114 Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr; 115 } 116 117 // This function is called from processSectionCommands, 118 // while we are fixing the output section layout. 119 void LinkerScript::addSymbol(SymbolAssignment *Cmd) { 120 if (Cmd->Name == ".") 121 return; 122 123 // If a symbol was in PROVIDE(), we need to define it only when 124 // it is a referenced undefined symbol. 125 SymbolBody *B = Symtab->find(Cmd->Name); 126 if (Cmd->Provide && (!B || B->isDefined())) 127 return; 128 129 // Define a symbol. 130 Symbol *Sym; 131 uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 132 std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility, 133 /*CanOmitFromDynSym*/ false, 134 /*File*/ nullptr); 135 Sym->Binding = STB_GLOBAL; 136 ExprValue Value = Cmd->Expression(); 137 SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec; 138 139 // When this function is called, section addresses have not been 140 // fixed yet. So, we may or may not know the value of the RHS 141 // expression. 142 // 143 // For example, if an expression is `x = 42`, we know x is always 42. 144 // However, if an expression is `x = .`, there's no way to know its 145 // value at the moment. 146 // 147 // We want to set symbol values early if we can. This allows us to 148 // use symbols as variables in linker scripts. Doing so allows us to 149 // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`. 150 uint64_t SymValue = Value.Sec ? 0 : Value.getValue(); 151 152 replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false, 153 Visibility, STT_NOTYPE, SymValue, 0, Sec); 154 Cmd->Sym = cast<DefinedRegular>(Sym->body()); 155 } 156 157 // This function is called from assignAddresses, while we are 158 // fixing the output section addresses. This function is supposed 159 // to set the final value for a given symbol assignment. 160 void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) { 161 if (Cmd->Name == ".") { 162 setDot(Cmd->Expression, Cmd->Location, InSec); 163 return; 164 } 165 166 if (!Cmd->Sym) 167 return; 168 169 ExprValue V = Cmd->Expression(); 170 if (V.isAbsolute()) { 171 Cmd->Sym->Section = nullptr; 172 Cmd->Sym->Value = V.getValue(); 173 } else { 174 Cmd->Sym->Section = V.Sec; 175 Cmd->Sym->Value = V.getSectionOffset(); 176 } 177 } 178 179 static std::string getFilename(InputFile *File) { 180 if (!File) 181 return ""; 182 if (File->ArchiveName.empty()) 183 return File->getName(); 184 return (File->ArchiveName + "(" + File->getName() + ")").str(); 185 } 186 187 bool LinkerScript::shouldKeep(InputSectionBase *S) { 188 std::string Filename = getFilename(S->File); 189 for (InputSectionDescription *ID : KeptSections) 190 if (ID->FilePat.match(Filename)) 191 for (SectionPattern &P : ID->SectionPatterns) 192 if (P.SectionPat.match(S->Name)) 193 return true; 194 return false; 195 } 196 197 // A helper function for the SORT() command. 198 static std::function<bool(InputSectionBase *, InputSectionBase *)> 199 getComparator(SortSectionPolicy K) { 200 switch (K) { 201 case SortSectionPolicy::Alignment: 202 return [](InputSectionBase *A, InputSectionBase *B) { 203 // ">" is not a mistake. Sections with larger alignments are placed 204 // before sections with smaller alignments in order to reduce the 205 // amount of padding necessary. This is compatible with GNU. 206 return A->Alignment > B->Alignment; 207 }; 208 case SortSectionPolicy::Name: 209 return [](InputSectionBase *A, InputSectionBase *B) { 210 return A->Name < B->Name; 211 }; 212 case SortSectionPolicy::Priority: 213 return [](InputSectionBase *A, InputSectionBase *B) { 214 return getPriority(A->Name) < getPriority(B->Name); 215 }; 216 default: 217 llvm_unreachable("unknown sort policy"); 218 } 219 } 220 221 // A helper function for the SORT() command. 222 static bool matchConstraints(ArrayRef<InputSection *> Sections, 223 ConstraintKind Kind) { 224 if (Kind == ConstraintKind::NoConstraint) 225 return true; 226 227 bool IsRW = llvm::any_of( 228 Sections, [](InputSection *Sec) { return Sec->Flags & SHF_WRITE; }); 229 230 return (IsRW && Kind == ConstraintKind::ReadWrite) || 231 (!IsRW && Kind == ConstraintKind::ReadOnly); 232 } 233 234 static void sortSections(MutableArrayRef<InputSection *> Vec, 235 SortSectionPolicy K) { 236 if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) 237 std::stable_sort(Vec.begin(), Vec.end(), getComparator(K)); 238 } 239 240 // Sort sections as instructed by SORT-family commands and --sort-section 241 // option. Because SORT-family commands can be nested at most two depth 242 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command 243 // line option is respected even if a SORT command is given, the exact 244 // behavior we have here is a bit complicated. Here are the rules. 245 // 246 // 1. If two SORT commands are given, --sort-section is ignored. 247 // 2. If one SORT command is given, and if it is not SORT_NONE, 248 // --sort-section is handled as an inner SORT command. 249 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. 250 // 4. If no SORT command is given, sort according to --sort-section. 251 // 5. If no SORT commands are given and --sort-section is not specified, 252 // apply sorting provided by --symbol-ordering-file if any exist. 253 static void sortInputSections( 254 MutableArrayRef<InputSection *> Vec, const SectionPattern &Pat, 255 const DenseMap<SectionBase *, int> &Order) { 256 if (Pat.SortOuter == SortSectionPolicy::None) 257 return; 258 259 if (Pat.SortOuter == SortSectionPolicy::Default && 260 Config->SortSection == SortSectionPolicy::Default) { 261 // If -symbol-ordering-file was given, sort accordingly. 262 // Usually, Order is empty. 263 if (!Order.empty()) 264 sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); }); 265 return; 266 } 267 268 if (Pat.SortInner == SortSectionPolicy::Default) 269 sortSections(Vec, Config->SortSection); 270 else 271 sortSections(Vec, Pat.SortInner); 272 sortSections(Vec, Pat.SortOuter); 273 } 274 275 // Compute and remember which sections the InputSectionDescription matches. 276 std::vector<InputSection *> 277 LinkerScript::computeInputSections(const InputSectionDescription *Cmd, 278 const DenseMap<SectionBase *, int> &Order) { 279 std::vector<InputSection *> Ret; 280 281 // Collects all sections that satisfy constraints of Cmd. 282 for (const SectionPattern &Pat : Cmd->SectionPatterns) { 283 size_t SizeBefore = Ret.size(); 284 285 for (InputSectionBase *Sec : InputSections) { 286 if (Sec->Assigned) 287 continue; 288 289 if (!Sec->Live) { 290 reportDiscarded(Sec); 291 continue; 292 } 293 294 // For -emit-relocs we have to ignore entries like 295 // .rela.dyn : { *(.rela.data) } 296 // which are common because they are in the default bfd script. 297 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA) 298 continue; 299 300 std::string Filename = getFilename(Sec->File); 301 if (!Cmd->FilePat.match(Filename) || 302 Pat.ExcludedFilePat.match(Filename) || 303 !Pat.SectionPat.match(Sec->Name)) 304 continue; 305 306 // It is safe to assume that Sec is an InputSection 307 // because mergeable or EH input sections have already been 308 // handled and eliminated. 309 Ret.push_back(cast<InputSection>(Sec)); 310 Sec->Assigned = true; 311 } 312 313 sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore), 314 Pat, Order); 315 } 316 return Ret; 317 } 318 319 void LinkerScript::discard(ArrayRef<InputSection *> V) { 320 for (InputSection *S : V) { 321 S->Live = false; 322 if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab || 323 S == InX::DynStrTab) 324 error("discarding " + S->Name + " section is not allowed"); 325 discard(S->DependentSections); 326 } 327 } 328 329 std::vector<InputSection *> LinkerScript::createInputSectionList( 330 OutputSection &OutCmd, const DenseMap<SectionBase *, int> &Order) { 331 std::vector<InputSection *> Ret; 332 333 for (BaseCommand *Base : OutCmd.SectionCommands) { 334 if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) { 335 Cmd->Sections = computeInputSections(Cmd, Order); 336 Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end()); 337 } 338 } 339 return Ret; 340 } 341 342 void LinkerScript::processSectionCommands() { 343 // A symbol can be assigned before any section is mentioned in the linker 344 // script. In an DSO, the symbol values are addresses, so the only important 345 // section values are: 346 // * SHN_UNDEF 347 // * SHN_ABS 348 // * Any value meaning a regular section. 349 // To handle that, create a dummy aether section that fills the void before 350 // the linker scripts switches to another section. It has an index of one 351 // which will map to whatever the first actual section is. 352 Aether = make<OutputSection>("", 0, SHF_ALLOC); 353 Aether->SectionIndex = 1; 354 355 // Ctx captures the local AddressState and makes it accessible deliberately. 356 // This is needed as there are some cases where we cannot just 357 // thread the current state through to a lambda function created by the 358 // script parser. 359 auto Deleter = make_unique<AddressState>(); 360 Ctx = Deleter.get(); 361 Ctx->OutSec = Aether; 362 363 DenseMap<SectionBase *, int> Order = buildSectionOrder(); 364 // Add input sections to output sections. 365 for (size_t I = 0; I < SectionCommands.size(); ++I) { 366 // Handle symbol assignments outside of any output section. 367 if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[I])) { 368 addSymbol(Cmd); 369 continue; 370 } 371 372 if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) { 373 std::vector<InputSection *> V = createInputSectionList(*Sec, Order); 374 375 // The output section name `/DISCARD/' is special. 376 // Any input section assigned to it is discarded. 377 if (Sec->Name == "/DISCARD/") { 378 discard(V); 379 continue; 380 } 381 382 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive 383 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input 384 // sections satisfy a given constraint. If not, a directive is handled 385 // as if it wasn't present from the beginning. 386 // 387 // Because we'll iterate over SectionCommands many more times, the easiest 388 // way to "make it as if it wasn't present" is to just remove it. 389 if (!matchConstraints(V, Sec->Constraint)) { 390 for (InputSectionBase *S : V) 391 S->Assigned = false; 392 SectionCommands.erase(SectionCommands.begin() + I); 393 --I; 394 continue; 395 } 396 397 // A directive may contain symbol definitions like this: 398 // ".foo : { ...; bar = .; }". Handle them. 399 for (BaseCommand *Base : Sec->SectionCommands) 400 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base)) 401 addSymbol(OutCmd); 402 403 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign 404 // is given, input sections are aligned to that value, whether the 405 // given value is larger or smaller than the original section alignment. 406 if (Sec->SubalignExpr) { 407 uint32_t Subalign = Sec->SubalignExpr().getValue(); 408 for (InputSectionBase *S : V) 409 S->Alignment = Subalign; 410 } 411 412 // Add input sections to an output section. 413 for (InputSection *S : V) 414 Sec->addSection(S); 415 } 416 } 417 Ctx = nullptr; 418 419 // Output sections are emitted in the exact same order as 420 // appeared in SECTIONS command, so we know their section indices. 421 for (size_t I = 0; I < SectionCommands.size(); ++I) { 422 auto *Sec = dyn_cast<OutputSection>(SectionCommands[I]); 423 if (!Sec) 424 continue; 425 assert(Sec->SectionIndex == INT_MAX); 426 Sec->SectionIndex = I; 427 if (Sec->Noload) 428 Sec->Type = SHT_NOBITS; 429 } 430 } 431 432 // If no SECTIONS command was given, we create simple SectionCommands 433 // as if a minimum SECTIONS command were given. This function does that. 434 void LinkerScript::fabricateDefaultCommands() { 435 // Define start address 436 uint64_t StartAddr = UINT64_MAX; 437 438 // The Sections with -T<section> have been sorted in order of ascending 439 // address. We must lower StartAddr if the lowest -T<section address> as 440 // calls to setDot() must be monotonically increasing. 441 for (auto &KV : Config->SectionStartMap) 442 StartAddr = std::min(StartAddr, KV.second); 443 444 auto Expr = [=] { 445 return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize()); 446 }; 447 SectionCommands.insert(SectionCommands.begin(), 448 make<SymbolAssignment>(".", Expr, "")); 449 } 450 451 static OutputSection *findByName(ArrayRef<BaseCommand *> Vec, 452 StringRef Name) { 453 for (BaseCommand *Base : Vec) 454 if (auto *Sec = dyn_cast<OutputSection>(Base)) 455 if (Sec->Name == Name) 456 return Sec; 457 return nullptr; 458 } 459 460 static void reportOrphan(InputSectionBase *IS, StringRef Name) { 461 if (Config->OrphanHandling == OrphanHandlingPolicy::Error) 462 error(toString(IS) + " is being placed in '" + Name + "'"); 463 else if (Config->OrphanHandling == OrphanHandlingPolicy::Warn) 464 warn(toString(IS) + " is being placed in '" + Name + "'"); 465 } 466 467 // Add sections that didn't match any sections command. 468 void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) { 469 unsigned End = SectionCommands.size(); 470 471 for (InputSectionBase *S : InputSections) { 472 if (!S->Live || S->Parent) 473 continue; 474 475 StringRef Name = getOutputSectionName(S->Name); 476 reportOrphan(S, Name); 477 478 if (OutputSection *Sec = 479 findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) { 480 Sec->addSection(cast<InputSection>(S)); 481 continue; 482 } 483 484 if (OutputSection *OS = Factory.addInputSec(S, Name)) 485 SectionCommands.push_back(OS); 486 assert(S->getOutputSection()->SectionIndex == INT_MAX); 487 } 488 } 489 490 uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) { 491 bool IsTbss = 492 (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS; 493 uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot; 494 Start = alignTo(Start, Alignment); 495 uint64_t End = Start + Size; 496 497 if (IsTbss) 498 Ctx->ThreadBssOffset = End - Dot; 499 else 500 Dot = End; 501 return End; 502 } 503 504 void LinkerScript::output(InputSection *S) { 505 uint64_t Before = advance(0, 1); 506 uint64_t Pos = advance(S->getSize(), S->Alignment); 507 S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr; 508 509 // Update output section size after adding each section. This is so that 510 // SIZEOF works correctly in the case below: 511 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } 512 Ctx->OutSec->Size = Pos - Ctx->OutSec->Addr; 513 514 // If there is a memory region associated with this input section, then 515 // place the section in that region and update the region index. 516 if (Ctx->MemRegion) { 517 uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion]; 518 CurOffset += Pos - Before; 519 uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin; 520 if (CurSize > Ctx->MemRegion->Length) { 521 uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length; 522 error("section '" + Ctx->OutSec->Name + "' will not fit in region '" + 523 Ctx->MemRegion->Name + "': overflowed by " + Twine(OverflowAmt) + 524 " bytes"); 525 } 526 } 527 } 528 529 void LinkerScript::switchTo(OutputSection *Sec) { 530 if (Ctx->OutSec == Sec) 531 return; 532 533 Ctx->OutSec = Sec; 534 Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment); 535 536 // If neither AT nor AT> is specified for an allocatable section, the linker 537 // will set the LMA such that the difference between VMA and LMA for the 538 // section is the same as the preceding output section in the same region 539 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html 540 if (Ctx->LMAOffset) 541 Ctx->OutSec->LMAOffset = Ctx->LMAOffset(); 542 } 543 544 // This function searches for a memory region to place the given output 545 // section in. If found, a pointer to the appropriate memory region is 546 // returned. Otherwise, a nullptr is returned. 547 MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) { 548 // If a memory region name was specified in the output section command, 549 // then try to find that region first. 550 if (!Sec->MemoryRegionName.empty()) { 551 auto It = MemoryRegions.find(Sec->MemoryRegionName); 552 if (It != MemoryRegions.end()) 553 return It->second; 554 error("memory region '" + Sec->MemoryRegionName + "' not declared"); 555 return nullptr; 556 } 557 558 // If at least one memory region is defined, all sections must 559 // belong to some memory region. Otherwise, we don't need to do 560 // anything for memory regions. 561 if (MemoryRegions.empty()) 562 return nullptr; 563 564 // See if a region can be found by matching section flags. 565 for (auto &Pair : MemoryRegions) { 566 MemoryRegion *M = Pair.second; 567 if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0) 568 return M; 569 } 570 571 // Otherwise, no suitable region was found. 572 if (Sec->Flags & SHF_ALLOC) 573 error("no memory region specified for section '" + Sec->Name + "'"); 574 return nullptr; 575 } 576 577 // This function assigns offsets to input sections and an output section 578 // for a single sections command (e.g. ".text { *(.text); }"). 579 void LinkerScript::assignOffsets(OutputSection *Sec) { 580 if (!(Sec->Flags & SHF_ALLOC)) 581 Dot = 0; 582 else if (Sec->AddrExpr) 583 setDot(Sec->AddrExpr, Sec->Location, false); 584 585 Ctx->MemRegion = Sec->MemRegion; 586 if (Ctx->MemRegion) 587 Dot = Ctx->MemRegionOffset[Ctx->MemRegion]; 588 589 if (Sec->LMAExpr) { 590 uint64_t D = Dot; 591 Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; }; 592 } 593 594 switchTo(Sec); 595 596 // We do not support custom layout for compressed debug sectons. 597 // At this point we already know their size and have compressed content. 598 if (Ctx->OutSec->Flags & SHF_COMPRESSED) 599 return; 600 601 // We visited SectionsCommands from processSectionCommands to 602 // layout sections. Now, we visit SectionsCommands again to fix 603 // section offsets. 604 for (BaseCommand *Base : Sec->SectionCommands) { 605 // This handles the assignments to symbol or to the dot. 606 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 607 assignSymbol(Cmd, true); 608 continue; 609 } 610 611 // Handle BYTE(), SHORT(), LONG(), or QUAD(). 612 if (auto *Cmd = dyn_cast<ByteCommand>(Base)) { 613 Cmd->Offset = Dot - Ctx->OutSec->Addr; 614 Dot += Cmd->Size; 615 Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr; 616 continue; 617 } 618 619 // Handle ASSERT(). 620 if (auto *Cmd = dyn_cast<AssertCommand>(Base)) { 621 Cmd->Expression(); 622 continue; 623 } 624 625 // Handle a single input section description command. 626 // It calculates and assigns the offsets for each section and also 627 // updates the output section size. 628 auto *Cmd = cast<InputSectionDescription>(Base); 629 for (InputSection *Sec : Cmd->Sections) { 630 // We tentatively added all synthetic sections at the beginning and 631 // removed empty ones afterwards (because there is no way to know 632 // whether they were going be empty or not other than actually running 633 // linker scripts.) We need to ignore remains of empty sections. 634 if (auto *S = dyn_cast<SyntheticSection>(Sec)) 635 if (S->empty()) 636 continue; 637 638 if (!Sec->Live) 639 continue; 640 assert(Ctx->OutSec == Sec->getParent()); 641 output(Sec); 642 } 643 } 644 } 645 646 void LinkerScript::removeEmptyCommands() { 647 // It is common practice to use very generic linker scripts. So for any 648 // given run some of the output sections in the script will be empty. 649 // We could create corresponding empty output sections, but that would 650 // clutter the output. 651 // We instead remove trivially empty sections. The bfd linker seems even 652 // more aggressive at removing them. 653 llvm::erase_if(SectionCommands, [&](BaseCommand *Base) { 654 if (auto *Sec = dyn_cast<OutputSection>(Base)) 655 return !Sec->Live; 656 return false; 657 }); 658 } 659 660 static bool isAllSectionDescription(const OutputSection &Cmd) { 661 for (BaseCommand *Base : Cmd.SectionCommands) 662 if (!isa<InputSectionDescription>(*Base)) 663 return false; 664 return true; 665 } 666 667 void LinkerScript::adjustSectionsBeforeSorting() { 668 // If the output section contains only symbol assignments, create a 669 // corresponding output section. The issue is what to do with linker script 670 // like ".foo : { symbol = 42; }". One option would be to convert it to 671 // "symbol = 42;". That is, move the symbol out of the empty section 672 // description. That seems to be what bfd does for this simple case. The 673 // problem is that this is not completely general. bfd will give up and 674 // create a dummy section too if there is a ". = . + 1" inside the section 675 // for example. 676 // Given that we want to create the section, we have to worry what impact 677 // it will have on the link. For example, if we just create a section with 678 // 0 for flags, it would change which PT_LOADs are created. 679 // We could remember that that particular section is dummy and ignore it in 680 // other parts of the linker, but unfortunately there are quite a few places 681 // that would need to change: 682 // * The program header creation. 683 // * The orphan section placement. 684 // * The address assignment. 685 // The other option is to pick flags that minimize the impact the section 686 // will have on the rest of the linker. That is why we copy the flags from 687 // the previous sections. Only a few flags are needed to keep the impact low. 688 uint64_t Flags = SHF_ALLOC; 689 690 for (BaseCommand *Cmd : SectionCommands) { 691 auto *Sec = dyn_cast<OutputSection>(Cmd); 692 if (!Sec) 693 continue; 694 if (Sec->Live) { 695 Flags = Sec->Flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR); 696 continue; 697 } 698 699 if (isAllSectionDescription(*Sec)) 700 continue; 701 702 Sec->Live = true; 703 Sec->Flags = Flags; 704 } 705 } 706 707 void LinkerScript::adjustSectionsAfterSorting() { 708 // Try and find an appropriate memory region to assign offsets in. 709 for (BaseCommand *Base : SectionCommands) { 710 if (auto *Sec = dyn_cast<OutputSection>(Base)) { 711 if (!Sec->Live) 712 continue; 713 Sec->MemRegion = findMemoryRegion(Sec); 714 // Handle align (e.g. ".foo : ALIGN(16) { ... }"). 715 if (Sec->AlignExpr) 716 Sec->Alignment = 717 std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue()); 718 } 719 } 720 721 // If output section command doesn't specify any segments, 722 // and we haven't previously assigned any section to segment, 723 // then we simply assign section to the very first load segment. 724 // Below is an example of such linker script: 725 // PHDRS { seg PT_LOAD; } 726 // SECTIONS { .aaa : { *(.aaa) } } 727 std::vector<StringRef> DefPhdrs; 728 auto FirstPtLoad = 729 std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(), 730 [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); 731 if (FirstPtLoad != PhdrsCommands.end()) 732 DefPhdrs.push_back(FirstPtLoad->Name); 733 734 // Walk the commands and propagate the program headers to commands that don't 735 // explicitly specify them. 736 for (BaseCommand *Base : SectionCommands) { 737 auto *Sec = dyn_cast<OutputSection>(Base); 738 if (!Sec) 739 continue; 740 741 if (Sec->Phdrs.empty()) { 742 // To match the bfd linker script behaviour, only propagate program 743 // headers to sections that are allocated. 744 if (Sec->Flags & SHF_ALLOC) 745 Sec->Phdrs = DefPhdrs; 746 } else { 747 DefPhdrs = Sec->Phdrs; 748 } 749 } 750 } 751 752 static OutputSection *findFirstSection(PhdrEntry *Load) { 753 for (OutputSection *Sec : OutputSections) 754 if (Sec->PtLoad == Load) 755 return Sec; 756 return nullptr; 757 } 758 759 // Try to find an address for the file and program headers output sections, 760 // which were unconditionally added to the first PT_LOAD segment earlier. 761 // 762 // When using the default layout, we check if the headers fit below the first 763 // allocated section. When using a linker script, we also check if the headers 764 // are covered by the output section. This allows omitting the headers by not 765 // leaving enough space for them in the linker script; this pattern is common 766 // in embedded systems. 767 // 768 // If there isn't enough space for these sections, we'll remove them from the 769 // PT_LOAD segment, and we'll also remove the PT_PHDR segment. 770 void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) { 771 uint64_t Min = std::numeric_limits<uint64_t>::max(); 772 for (OutputSection *Sec : OutputSections) 773 if (Sec->Flags & SHF_ALLOC) 774 Min = std::min<uint64_t>(Min, Sec->Addr); 775 776 auto It = llvm::find_if( 777 Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; }); 778 if (It == Phdrs.end()) 779 return; 780 PhdrEntry *FirstPTLoad = *It; 781 782 uint64_t HeaderSize = getHeaderSize(); 783 // When linker script with SECTIONS is being used, don't output headers 784 // unless there's a space for them. 785 uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0; 786 if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) { 787 Min = alignDown(Min - HeaderSize, Config->MaxPageSize); 788 Out::ElfHeader->Addr = Min; 789 Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size; 790 return; 791 } 792 793 Out::ElfHeader->PtLoad = nullptr; 794 Out::ProgramHeaders->PtLoad = nullptr; 795 FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad); 796 797 llvm::erase_if(Phdrs, 798 [](const PhdrEntry *E) { return E->p_type == PT_PHDR; }); 799 } 800 801 LinkerScript::AddressState::AddressState() { 802 for (auto &MRI : Script->MemoryRegions) { 803 const MemoryRegion *MR = MRI.second; 804 MemRegionOffset[MR] = MR->Origin; 805 } 806 } 807 808 // Assign addresses as instructed by linker script SECTIONS sub-commands. 809 void LinkerScript::assignAddresses() { 810 // By default linker scripts use an initial value of 0 for '.', but prefer 811 // -image-base if set. 812 Dot = Config->ImageBase ? *Config->ImageBase : 0; 813 814 auto Deleter = make_unique<AddressState>(); 815 Ctx = Deleter.get(); 816 ErrorOnMissingSection = true; 817 switchTo(Aether); 818 819 for (BaseCommand *Base : SectionCommands) { 820 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 821 assignSymbol(Cmd, false); 822 continue; 823 } 824 825 if (auto *Cmd = dyn_cast<AssertCommand>(Base)) { 826 Cmd->Expression(); 827 continue; 828 } 829 830 assignOffsets(cast<OutputSection>(Base)); 831 } 832 Ctx = nullptr; 833 } 834 835 // Creates program headers as instructed by PHDRS linker script command. 836 std::vector<PhdrEntry *> LinkerScript::createPhdrs() { 837 std::vector<PhdrEntry *> Ret; 838 839 // Process PHDRS and FILEHDR keywords because they are not 840 // real output sections and cannot be added in the following loop. 841 for (const PhdrsCommand &Cmd : PhdrsCommands) { 842 PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R); 843 844 if (Cmd.HasFilehdr) 845 Phdr->add(Out::ElfHeader); 846 if (Cmd.HasPhdrs) 847 Phdr->add(Out::ProgramHeaders); 848 849 if (Cmd.LMAExpr) { 850 Phdr->p_paddr = Cmd.LMAExpr().getValue(); 851 Phdr->HasLMA = true; 852 } 853 Ret.push_back(Phdr); 854 } 855 856 // Add output sections to program headers. 857 for (OutputSection *Sec : OutputSections) { 858 // Assign headers specified by linker script 859 for (size_t Id : getPhdrIndices(Sec)) { 860 Ret[Id]->add(Sec); 861 if (!PhdrsCommands[Id].Flags.hasValue()) 862 Ret[Id]->p_flags |= Sec->getPhdrFlags(); 863 } 864 } 865 return Ret; 866 } 867 868 // Returns true if we should emit an .interp section. 869 // 870 // We usually do. But if PHDRS commands are given, and 871 // no PT_INTERP is there, there's no place to emit an 872 // .interp, so we don't do that in that case. 873 bool LinkerScript::needsInterpSection() { 874 if (PhdrsCommands.empty()) 875 return true; 876 for (PhdrsCommand &Cmd : PhdrsCommands) 877 if (Cmd.Type == PT_INTERP) 878 return true; 879 return false; 880 } 881 882 ExprValue LinkerScript::getSymbolValue(StringRef Name, const Twine &Loc) { 883 if (Name == ".") { 884 if (Ctx) 885 return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc}; 886 error(Loc + ": unable to get location counter value"); 887 return 0; 888 } 889 890 if (auto *Sym = dyn_cast_or_null<DefinedRegular>(Symtab->find(Name))) 891 return {Sym->Section, false, Sym->Value, Loc}; 892 893 error(Loc + ": symbol not found: " + Name); 894 return 0; 895 } 896 897 // Returns the index of the segment named Name. 898 static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec, 899 StringRef Name) { 900 for (size_t I = 0; I < Vec.size(); ++I) 901 if (Vec[I].Name == Name) 902 return I; 903 return None; 904 } 905 906 // Returns indices of ELF headers containing specific section. Each index is a 907 // zero based number of ELF header listed within PHDRS {} script block. 908 std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) { 909 std::vector<size_t> Ret; 910 911 for (StringRef S : Cmd->Phdrs) { 912 if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S)) 913 Ret.push_back(*Idx); 914 else if (S != "NONE") 915 error(Cmd->Location + ": section header '" + S + 916 "' is not listed in PHDRS"); 917 } 918 return Ret; 919 } 920