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 "Driver.h" 17 #include "InputSection.h" 18 #include "Memory.h" 19 #include "OutputSections.h" 20 #include "ScriptParser.h" 21 #include "Strings.h" 22 #include "SymbolTable.h" 23 #include "Symbols.h" 24 #include "SyntheticSections.h" 25 #include "Target.h" 26 #include "Writer.h" 27 #include "llvm/ADT/STLExtras.h" 28 #include "llvm/ADT/SmallString.h" 29 #include "llvm/ADT/StringRef.h" 30 #include "llvm/ADT/StringSwitch.h" 31 #include "llvm/Support/Casting.h" 32 #include "llvm/Support/ELF.h" 33 #include "llvm/Support/Endian.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/FileSystem.h" 36 #include "llvm/Support/MathExtras.h" 37 #include "llvm/Support/Path.h" 38 #include <algorithm> 39 #include <cassert> 40 #include <cstddef> 41 #include <cstdint> 42 #include <iterator> 43 #include <limits> 44 #include <memory> 45 #include <string> 46 #include <tuple> 47 #include <vector> 48 49 using namespace llvm; 50 using namespace llvm::ELF; 51 using namespace llvm::object; 52 using namespace llvm::support::endian; 53 using namespace lld; 54 using namespace lld::elf; 55 56 LinkerScriptBase *elf::ScriptBase; 57 ScriptConfiguration *elf::ScriptConfig; 58 59 template <class ELFT> static void addRegular(SymbolAssignment *Cmd) { 60 uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 61 Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, Visibility, STT_NOTYPE, 62 0, 0, STB_GLOBAL, nullptr, nullptr); 63 Cmd->Sym = Sym->body(); 64 65 // If we have no SECTIONS then we don't have '.' and don't call 66 // assignAddresses(). We calculate symbol value immediately in this case. 67 if (!ScriptConfig->HasSections) 68 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0); 69 } 70 71 template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) { 72 // If we have SECTIONS block then output sections haven't been created yet. 73 const OutputSectionBase *Sec = 74 ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section(); 75 Symbol *Sym = Symtab<ELFT>::X->addSynthetic( 76 Cmd->Name, Sec, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT); 77 Cmd->Sym = Sym->body(); 78 79 // If we already know section then we can calculate symbol value immediately. 80 if (Sec) 81 cast<DefinedSynthetic>(Cmd->Sym)->Value = Cmd->Expression(0) - Sec->Addr; 82 } 83 84 static bool isUnderSysroot(StringRef Path) { 85 if (Config->Sysroot == "") 86 return false; 87 for (; !Path.empty(); Path = sys::path::parent_path(Path)) 88 if (sys::fs::equivalent(Config->Sysroot, Path)) 89 return true; 90 return false; 91 } 92 93 template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) { 94 if (Cmd->Expression.IsAbsolute()) 95 addRegular<ELFT>(Cmd); 96 else 97 addSynthetic<ELFT>(Cmd); 98 } 99 // If a symbol was in PROVIDE(), we need to define it only when 100 // it is an undefined symbol. 101 template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) { 102 if (Cmd->Name == ".") 103 return false; 104 if (!Cmd->Provide) 105 return true; 106 SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); 107 return B && B->isUndefined(); 108 } 109 110 bool SymbolAssignment::classof(const BaseCommand *C) { 111 return C->Kind == AssignmentKind; 112 } 113 114 bool OutputSectionCommand::classof(const BaseCommand *C) { 115 return C->Kind == OutputSectionKind; 116 } 117 118 bool InputSectionDescription::classof(const BaseCommand *C) { 119 return C->Kind == InputSectionKind; 120 } 121 122 bool AssertCommand::classof(const BaseCommand *C) { 123 return C->Kind == AssertKind; 124 } 125 126 bool BytesDataCommand::classof(const BaseCommand *C) { 127 return C->Kind == BytesDataKind; 128 } 129 130 template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default; 131 template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default; 132 133 template <class ELFT> static StringRef basename(InputSectionBase<ELFT> *S) { 134 if (S->getFile()) 135 return sys::path::filename(S->getFile()->getName()); 136 return ""; 137 } 138 139 template <class ELFT> 140 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { 141 for (InputSectionDescription *ID : Opt.KeptSections) 142 if (ID->FilePat.match(basename(S))) 143 for (SectionPattern &P : ID->SectionPatterns) 144 if (P.SectionPat.match(S->Name)) 145 return true; 146 return false; 147 } 148 149 static bool comparePriority(InputSectionData *A, InputSectionData *B) { 150 return getPriority(A->Name) < getPriority(B->Name); 151 } 152 153 static bool compareName(InputSectionData *A, InputSectionData *B) { 154 return A->Name < B->Name; 155 } 156 157 static bool compareAlignment(InputSectionData *A, InputSectionData *B) { 158 // ">" is not a mistake. Larger alignments are placed before smaller 159 // alignments in order to reduce the amount of padding necessary. 160 // This is compatible with GNU. 161 return A->Alignment > B->Alignment; 162 } 163 164 static std::function<bool(InputSectionData *, InputSectionData *)> 165 getComparator(SortSectionPolicy K) { 166 switch (K) { 167 case SortSectionPolicy::Alignment: 168 return compareAlignment; 169 case SortSectionPolicy::Name: 170 return compareName; 171 case SortSectionPolicy::Priority: 172 return comparePriority; 173 default: 174 llvm_unreachable("unknown sort policy"); 175 } 176 } 177 178 template <class ELFT> 179 static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections, 180 ConstraintKind Kind) { 181 if (Kind == ConstraintKind::NoConstraint) 182 return true; 183 bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) { 184 auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2); 185 return Sec->Flags & SHF_WRITE; 186 }); 187 return (IsRW && Kind == ConstraintKind::ReadWrite) || 188 (!IsRW && Kind == ConstraintKind::ReadOnly); 189 } 190 191 static void sortSections(InputSectionData **Begin, InputSectionData **End, 192 SortSectionPolicy K) { 193 if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) 194 std::stable_sort(Begin, End, getComparator(K)); 195 } 196 197 // Compute and remember which sections the InputSectionDescription matches. 198 template <class ELFT> 199 void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) { 200 // Collects all sections that satisfy constraints of I 201 // and attach them to I. 202 for (SectionPattern &Pat : I->SectionPatterns) { 203 size_t SizeBefore = I->Sections.size(); 204 205 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) { 206 if (!S->Live || S->Assigned) 207 continue; 208 209 StringRef Filename = basename(S); 210 if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename)) 211 continue; 212 if (!Pat.SectionPat.match(S->Name)) 213 continue; 214 I->Sections.push_back(S); 215 S->Assigned = true; 216 } 217 218 // Sort sections as instructed by SORT-family commands and --sort-section 219 // option. Because SORT-family commands can be nested at most two depth 220 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command 221 // line option is respected even if a SORT command is given, the exact 222 // behavior we have here is a bit complicated. Here are the rules. 223 // 224 // 1. If two SORT commands are given, --sort-section is ignored. 225 // 2. If one SORT command is given, and if it is not SORT_NONE, 226 // --sort-section is handled as an inner SORT command. 227 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. 228 // 4. If no SORT command is given, sort according to --sort-section. 229 InputSectionData **Begin = I->Sections.data() + SizeBefore; 230 InputSectionData **End = I->Sections.data() + I->Sections.size(); 231 if (Pat.SortOuter != SortSectionPolicy::None) { 232 if (Pat.SortInner == SortSectionPolicy::Default) 233 sortSections(Begin, End, Config->SortSection); 234 else 235 sortSections(Begin, End, Pat.SortInner); 236 sortSections(Begin, End, Pat.SortOuter); 237 } 238 } 239 } 240 241 template <class ELFT> 242 void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) { 243 for (InputSectionBase<ELFT> *S : V) { 244 S->Live = false; 245 reportDiscarded(S); 246 } 247 } 248 249 template <class ELFT> 250 std::vector<InputSectionBase<ELFT> *> 251 LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) { 252 std::vector<InputSectionBase<ELFT> *> Ret; 253 254 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) { 255 auto *Cmd = dyn_cast<InputSectionDescription>(Base.get()); 256 if (!Cmd) 257 continue; 258 computeInputSections(Cmd); 259 for (InputSectionData *S : Cmd->Sections) 260 Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S)); 261 } 262 263 return Ret; 264 } 265 266 template <class ELFT> 267 static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, 268 StringRef OutsecName) { 269 // When using linker script the merge rules are different. 270 // Unfortunately, linker scripts are name based. This means that expressions 271 // like *(.foo*) can refer to multiple input sections that would normally be 272 // placed in different output sections. We cannot put them in different 273 // output sections or we would produce wrong results for 274 // start = .; *(.foo.*) end = .; *(.bar) 275 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to 276 // another. The problem is that there is no way to layout those output 277 // sections such that the .foo sections are the only thing between the 278 // start and end symbols. 279 280 // An extra annoyance is that we cannot simply disable merging of the contents 281 // of SHF_MERGE sections, but our implementation requires one output section 282 // per "kind" (string or not, which size/aligment). 283 // Fortunately, creating symbols in the middle of a merge section is not 284 // supported by bfd or gold, so we can just create multiple section in that 285 // case. 286 typedef typename ELFT::uint uintX_t; 287 uintX_t Flags = C->Flags & (SHF_MERGE | SHF_STRINGS); 288 289 uintX_t Alignment = 0; 290 if (isa<MergeInputSection<ELFT>>(C)) 291 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize); 292 293 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment}; 294 } 295 296 template <class ELFT> 297 void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory, 298 InputSectionBase<ELFT> *Sec, 299 StringRef Name) { 300 OutputSectionBase *OutSec; 301 bool IsNew; 302 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec); 303 if (IsNew) 304 OutputSections->push_back(OutSec); 305 OutSec->addSection(Sec); 306 } 307 308 template <class ELFT> 309 void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) { 310 for (unsigned I = 0; I < Opt.Commands.size(); ++I) { 311 auto Iter = Opt.Commands.begin() + I; 312 const std::unique_ptr<BaseCommand> &Base1 = *Iter; 313 314 // Handle symbol assignments outside of any output section. 315 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) { 316 if (shouldDefine<ELFT>(Cmd)) 317 addSymbol<ELFT>(Cmd); 318 continue; 319 } 320 321 if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) { 322 // If we don't have SECTIONS then output sections have already been 323 // created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses 324 // will not be called, so ASSERT should be evaluated now. 325 if (!Opt.HasSections) 326 Cmd->Expression(0); 327 continue; 328 } 329 330 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) { 331 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd); 332 333 // The output section name `/DISCARD/' is special. 334 // Any input section assigned to it is discarded. 335 if (Cmd->Name == "/DISCARD/") { 336 discard(V); 337 continue; 338 } 339 340 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive 341 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input 342 // sections satisfy a given constraint. If not, a directive is handled 343 // as if it wasn't present from the beginning. 344 // 345 // Because we'll iterate over Commands many more times, the easiest 346 // way to "make it as if it wasn't present" is to just remove it. 347 if (!matchConstraints<ELFT>(V, Cmd->Constraint)) { 348 for (InputSectionBase<ELFT> *S : V) 349 S->Assigned = false; 350 Opt.Commands.erase(Iter); 351 --I; 352 continue; 353 } 354 355 // A directive may contain symbol definitions like this: 356 // ".foo : { ...; bar = .; }". Handle them. 357 for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands) 358 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) 359 if (shouldDefine<ELFT>(OutCmd)) 360 addSymbol<ELFT>(OutCmd); 361 362 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign 363 // is given, input sections are aligned to that value, whether the 364 // given value is larger or smaller than the original section alignment. 365 if (Cmd->SubalignExpr) { 366 uint32_t Subalign = Cmd->SubalignExpr(0); 367 for (InputSectionBase<ELFT> *S : V) 368 S->Alignment = Subalign; 369 } 370 371 // Add input sections to an output section. 372 for (InputSectionBase<ELFT> *S : V) 373 addSection(Factory, S, Cmd->Name); 374 } 375 } 376 } 377 378 // Add sections that didn't match any sections command. 379 template <class ELFT> 380 void LinkerScript<ELFT>::addOrphanSections( 381 OutputSectionFactory<ELFT> &Factory) { 382 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) 383 if (S->Live && !S->OutSec) 384 addSection(Factory, S, getOutputSectionName(S->Name)); 385 } 386 387 // Sets value of a section-defined symbol. Two kinds of 388 // symbols are processed: synthetic symbols, whose value 389 // is an offset from beginning of section and regular 390 // symbols whose value is absolute. 391 template <class ELFT> 392 static void assignSectionSymbol(SymbolAssignment *Cmd, 393 typename ELFT::uint Value) { 394 if (!Cmd->Sym) 395 return; 396 397 if (auto *Body = dyn_cast<DefinedSynthetic>(Cmd->Sym)) { 398 Body->Section = Cmd->Expression.Section(); 399 Body->Value = Cmd->Expression(Value) - Body->Section->Addr; 400 return; 401 } 402 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym); 403 Body->Value = Cmd->Expression(Value); 404 } 405 406 template <class ELFT> static bool isTbss(OutputSectionBase *Sec) { 407 return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS; 408 } 409 410 template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) { 411 if (!AlreadyOutputIS.insert(S).second) 412 return; 413 bool IsTbss = isTbss<ELFT>(CurOutSec); 414 415 uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot; 416 Pos = alignTo(Pos, S->Alignment); 417 S->OutSecOff = Pos - CurOutSec->Addr; 418 Pos += S->getSize(); 419 420 // Update output section size after adding each section. This is so that 421 // SIZEOF works correctly in the case below: 422 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } 423 CurOutSec->Size = Pos - CurOutSec->Addr; 424 425 if (IsTbss) 426 ThreadBssOffset = Pos - Dot; 427 else 428 Dot = Pos; 429 } 430 431 template <class ELFT> void LinkerScript<ELFT>::flush() { 432 if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second) 433 return; 434 if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) { 435 for (InputSection<ELFT> *I : OutSec->Sections) 436 output(I); 437 } else { 438 Dot += CurOutSec->Size; 439 } 440 } 441 442 template <class ELFT> 443 void LinkerScript<ELFT>::switchTo(OutputSectionBase *Sec) { 444 if (CurOutSec == Sec) 445 return; 446 if (AlreadyOutputOS.count(Sec)) 447 return; 448 449 flush(); 450 CurOutSec = Sec; 451 452 Dot = alignTo(Dot, CurOutSec->Addralign); 453 CurOutSec->Addr = isTbss<ELFT>(CurOutSec) ? Dot + ThreadBssOffset : Dot; 454 455 // If neither AT nor AT> is specified for an allocatable section, the linker 456 // will set the LMA such that the difference between VMA and LMA for the 457 // section is the same as the preceding output section in the same region 458 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html 459 CurOutSec->setLMAOffset(LMAOffset); 460 } 461 462 template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) { 463 // This handles the assignments to symbol or to a location counter (.) 464 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) { 465 if (AssignCmd->Name == ".") { 466 // Update to location counter means update to section size. 467 uintX_t Val = AssignCmd->Expression(Dot); 468 if (Val < Dot) 469 error("unable to move location counter backward for: " + 470 CurOutSec->Name); 471 Dot = Val; 472 CurOutSec->Size = Dot - CurOutSec->Addr; 473 return; 474 } 475 assignSectionSymbol<ELFT>(AssignCmd, Dot); 476 return; 477 } 478 479 // Handle BYTE(), SHORT(), LONG(), or QUAD(). 480 if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) { 481 DataCmd->Offset = Dot - CurOutSec->Addr; 482 Dot += DataCmd->Size; 483 CurOutSec->Size = Dot - CurOutSec->Addr; 484 return; 485 } 486 487 if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) { 488 AssertCmd->Expression(Dot); 489 return; 490 } 491 492 // It handles single input section description command, 493 // calculates and assigns the offsets for each section and also 494 // updates the output section size. 495 auto &ICmd = cast<InputSectionDescription>(Base); 496 for (InputSectionData *ID : ICmd.Sections) { 497 // We tentatively added all synthetic sections at the beginning and removed 498 // empty ones afterwards (because there is no way to know whether they were 499 // going be empty or not other than actually running linker scripts.) 500 // We need to ignore remains of empty sections. 501 if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID)) 502 if (Sec->empty()) 503 continue; 504 505 auto *IB = static_cast<InputSectionBase<ELFT> *>(ID); 506 switchTo(IB->OutSec); 507 if (auto *I = dyn_cast<InputSection<ELFT>>(IB)) 508 output(I); 509 else 510 flush(); 511 } 512 } 513 514 template <class ELFT> 515 static std::vector<OutputSectionBase *> 516 findSections(StringRef Name, const std::vector<OutputSectionBase *> &Sections) { 517 std::vector<OutputSectionBase *> Ret; 518 for (OutputSectionBase *Sec : Sections) 519 if (Sec->getName() == Name) 520 Ret.push_back(Sec); 521 return Ret; 522 } 523 524 // This function assigns offsets to input sections and an output section 525 // for a single sections command (e.g. ".text { *(.text); }"). 526 template <class ELFT> 527 void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) { 528 if (Cmd->LMAExpr) 529 LMAOffset = Cmd->LMAExpr(Dot) - Dot; 530 std::vector<OutputSectionBase *> Sections = 531 findSections<ELFT>(Cmd->Name, *OutputSections); 532 if (Sections.empty()) 533 return; 534 switchTo(Sections[0]); 535 536 // Find the last section output location. We will output orphan sections 537 // there so that end symbols point to the correct location. 538 auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(), 539 [](const std::unique_ptr<BaseCommand> &Cmd) { 540 return !isa<SymbolAssignment>(*Cmd); 541 }) 542 .base(); 543 for (auto I = Cmd->Commands.begin(); I != E; ++I) 544 process(**I); 545 for (OutputSectionBase *Base : Sections) 546 switchTo(Base); 547 flush(); 548 std::for_each(E, Cmd->Commands.end(), 549 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); }); 550 } 551 552 template <class ELFT> void LinkerScript<ELFT>::removeEmptyCommands() { 553 // It is common practice to use very generic linker scripts. So for any 554 // given run some of the output sections in the script will be empty. 555 // We could create corresponding empty output sections, but that would 556 // clutter the output. 557 // We instead remove trivially empty sections. The bfd linker seems even 558 // more aggressive at removing them. 559 auto Pos = std::remove_if( 560 Opt.Commands.begin(), Opt.Commands.end(), 561 [&](const std::unique_ptr<BaseCommand> &Base) { 562 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 563 return findSections<ELFT>(Cmd->Name, *OutputSections).empty(); 564 return false; 565 }); 566 Opt.Commands.erase(Pos, Opt.Commands.end()); 567 } 568 569 static bool isAllSectionDescription(const OutputSectionCommand &Cmd) { 570 for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands) 571 if (!isa<InputSectionDescription>(*I)) 572 return false; 573 return true; 574 } 575 576 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() { 577 // If the output section contains only symbol assignments, create a 578 // corresponding output section. The bfd linker seems to only create them if 579 // '.' is assigned to, but creating these section should not have any bad 580 // consequeces and gives us a section to put the symbol in. 581 uintX_t Flags = SHF_ALLOC; 582 uint32_t Type = SHT_NOBITS; 583 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 584 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 585 if (!Cmd) 586 continue; 587 std::vector<OutputSectionBase *> Secs = 588 findSections<ELFT>(Cmd->Name, *OutputSections); 589 if (!Secs.empty()) { 590 Flags = Secs[0]->Flags; 591 Type = Secs[0]->Type; 592 continue; 593 } 594 595 if (isAllSectionDescription(*Cmd)) 596 continue; 597 598 auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags); 599 OutputSections->push_back(OutSec); 600 } 601 } 602 603 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() { 604 placeOrphanSections(); 605 606 // If output section command doesn't specify any segments, 607 // and we haven't previously assigned any section to segment, 608 // then we simply assign section to the very first load segment. 609 // Below is an example of such linker script: 610 // PHDRS { seg PT_LOAD; } 611 // SECTIONS { .aaa : { *(.aaa) } } 612 std::vector<StringRef> DefPhdrs; 613 auto FirstPtLoad = 614 std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(), 615 [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); 616 if (FirstPtLoad != Opt.PhdrsCommands.end()) 617 DefPhdrs.push_back(FirstPtLoad->Name); 618 619 // Walk the commands and propagate the program headers to commands that don't 620 // explicitly specify them. 621 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 622 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 623 if (!Cmd) 624 continue; 625 if (Cmd->Phdrs.empty()) 626 Cmd->Phdrs = DefPhdrs; 627 else 628 DefPhdrs = Cmd->Phdrs; 629 } 630 631 removeEmptyCommands(); 632 } 633 634 // When placing orphan sections, we want to place them after symbol assignments 635 // so that an orphan after 636 // begin_foo = .; 637 // foo : { *(foo) } 638 // end_foo = .; 639 // doesn't break the intended meaning of the begin/end symbols. 640 // We don't want to go over sections since Writer<ELFT>::sortSections is the 641 // one in charge of deciding the order of the sections. 642 // We don't want to go over alignments, since doing so in 643 // rx_sec : { *(rx_sec) } 644 // . = ALIGN(0x1000); 645 // /* The RW PT_LOAD starts here*/ 646 // rw_sec : { *(rw_sec) } 647 // would mean that the RW PT_LOAD would become unaligned. 648 static bool shouldSkip(const BaseCommand &Cmd) { 649 if (isa<OutputSectionCommand>(Cmd)) 650 return false; 651 const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd); 652 if (!Assign) 653 return true; 654 return Assign->Name != "."; 655 } 656 657 // Orphan sections are sections present in the input files which are not 658 // explicitly placed into the output file by the linker script. This just 659 // places them in the order already decided in OutputSections. 660 template <class ELFT> void LinkerScript<ELFT>::placeOrphanSections() { 661 // The OutputSections are already in the correct order. 662 // This loops creates or moves commands as needed so that they are in the 663 // correct order. 664 int CmdIndex = 0; 665 666 // As a horrible special case, skip the first . assignment if it is before any 667 // section. We do this because it is common to set a load address by starting 668 // the script with ". = 0xabcd" and the expectation is that every section is 669 // after that. 670 auto FirstSectionOrDotAssignment = 671 std::find_if(Opt.Commands.begin(), Opt.Commands.end(), 672 [](const std::unique_ptr<BaseCommand> &Cmd) { 673 if (isa<OutputSectionCommand>(*Cmd)) 674 return true; 675 const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get()); 676 if (!Assign) 677 return false; 678 return Assign->Name == "."; 679 }); 680 if (FirstSectionOrDotAssignment != Opt.Commands.end()) { 681 CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin(); 682 if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment)) 683 ++CmdIndex; 684 } 685 686 for (OutputSectionBase *Sec : *OutputSections) { 687 StringRef Name = Sec->getName(); 688 689 // Find the last spot where we can insert a command and still get the 690 // correct result. 691 auto CmdIter = Opt.Commands.begin() + CmdIndex; 692 auto E = Opt.Commands.end(); 693 while (CmdIter != E && shouldSkip(**CmdIter)) { 694 ++CmdIter; 695 ++CmdIndex; 696 } 697 698 auto Pos = 699 std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) { 700 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 701 return Cmd && Cmd->Name == Name; 702 }); 703 if (Pos == E) { 704 Opt.Commands.insert(CmdIter, 705 llvm::make_unique<OutputSectionCommand>(Name)); 706 ++CmdIndex; 707 continue; 708 } 709 710 // Continue from where we found it. 711 CmdIndex = (Pos - Opt.Commands.begin()) + 1; 712 } 713 } 714 715 template <class ELFT> 716 void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry> &Phdrs) { 717 // Assign addresses as instructed by linker script SECTIONS sub-commands. 718 Dot = 0; 719 720 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 721 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { 722 if (Cmd->Name == ".") { 723 Dot = Cmd->Expression(Dot); 724 } else if (Cmd->Sym) { 725 assignSectionSymbol<ELFT>(Cmd, Dot); 726 } 727 continue; 728 } 729 730 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) { 731 Cmd->Expression(Dot); 732 continue; 733 } 734 735 auto *Cmd = cast<OutputSectionCommand>(Base.get()); 736 if (Cmd->AddrExpr) 737 Dot = Cmd->AddrExpr(Dot); 738 assignOffsets(Cmd); 739 } 740 741 uintX_t MinVA = std::numeric_limits<uintX_t>::max(); 742 for (OutputSectionBase *Sec : *OutputSections) { 743 if (Sec->Flags & SHF_ALLOC) 744 MinVA = std::min<uint64_t>(MinVA, Sec->Addr); 745 else 746 Sec->Addr = 0; 747 } 748 749 uintX_t HeaderSize = getHeaderSize(); 750 // If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders 751 // now that we know we have space. 752 if (HeaderSize <= MinVA && !hasPhdrsCommands()) 753 allocateHeaders<ELFT>(Phdrs, *OutputSections); 754 755 // ELF and Program headers need to be right before the first section in 756 // memory. Set their addresses accordingly. 757 MinVA = alignDown(MinVA - HeaderSize, Config->MaxPageSize); 758 Out<ELFT>::ElfHeader->Addr = MinVA; 759 Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA; 760 } 761 762 // Creates program headers as instructed by PHDRS linker script command. 763 template <class ELFT> std::vector<PhdrEntry> LinkerScript<ELFT>::createPhdrs() { 764 std::vector<PhdrEntry> Ret; 765 766 // Process PHDRS and FILEHDR keywords because they are not 767 // real output sections and cannot be added in the following loop. 768 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { 769 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); 770 PhdrEntry &Phdr = Ret.back(); 771 772 if (Cmd.HasFilehdr) 773 Phdr.add(Out<ELFT>::ElfHeader); 774 if (Cmd.HasPhdrs) 775 Phdr.add(Out<ELFT>::ProgramHeaders); 776 777 if (Cmd.LMAExpr) { 778 Phdr.p_paddr = Cmd.LMAExpr(0); 779 Phdr.HasLMA = true; 780 } 781 } 782 783 // Add output sections to program headers. 784 for (OutputSectionBase *Sec : *OutputSections) { 785 if (!(Sec->Flags & SHF_ALLOC)) 786 break; 787 788 // Assign headers specified by linker script 789 for (size_t Id : getPhdrIndices(Sec->getName())) { 790 Ret[Id].add(Sec); 791 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) 792 Ret[Id].p_flags |= Sec->getPhdrFlags(); 793 } 794 } 795 return Ret; 796 } 797 798 template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() { 799 // Ignore .interp section in case we have PHDRS specification 800 // and PT_INTERP isn't listed. 801 return !Opt.PhdrsCommands.empty() && 802 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) { 803 return Cmd.Type == PT_INTERP; 804 }) == Opt.PhdrsCommands.end(); 805 } 806 807 template <class ELFT> uint32_t LinkerScript<ELFT>::getFiller(StringRef Name) { 808 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 809 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 810 if (Cmd->Name == Name) 811 return Cmd->Filler; 812 return 0; 813 } 814 815 template <class ELFT> 816 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) { 817 const endianness E = ELFT::TargetEndianness; 818 819 switch (Size) { 820 case 1: 821 *Buf = (uint8_t)Data; 822 break; 823 case 2: 824 write16<E>(Buf, Data); 825 break; 826 case 4: 827 write32<E>(Buf, Data); 828 break; 829 case 8: 830 write64<E>(Buf, Data); 831 break; 832 default: 833 llvm_unreachable("unsupported Size argument"); 834 } 835 } 836 837 template <class ELFT> 838 void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) { 839 int I = getSectionIndex(Name); 840 if (I == INT_MAX) 841 return; 842 843 auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()); 844 for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands) 845 if (auto *Data = dyn_cast<BytesDataCommand>(Base.get())) 846 writeInt<ELFT>(Buf + Data->Offset, Data->Expression(0), Data->Size); 847 } 848 849 template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) { 850 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 851 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 852 if (Cmd->LMAExpr && Cmd->Name == Name) 853 return true; 854 return false; 855 } 856 857 // Returns the index of the given section name in linker script 858 // SECTIONS commands. Sections are laid out as the same order as they 859 // were in the script. If a given name did not appear in the script, 860 // it returns INT_MAX, so that it will be laid out at end of file. 861 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { 862 for (int I = 0, E = Opt.Commands.size(); I != E; ++I) 863 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get())) 864 if (Cmd->Name == Name) 865 return I; 866 return INT_MAX; 867 } 868 869 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { 870 return !Opt.PhdrsCommands.empty(); 871 } 872 873 template <class ELFT> 874 const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc, 875 StringRef Name) { 876 static OutputSectionBase FakeSec("", 0, 0); 877 878 for (OutputSectionBase *Sec : *OutputSections) 879 if (Sec->getName() == Name) 880 return Sec; 881 882 error(Loc + ": undefined section " + Name); 883 return &FakeSec; 884 } 885 886 // This function is essentially the same as getOutputSection(Name)->Size, 887 // but it won't print out an error message if a given section is not found. 888 // 889 // Linker script does not create an output section if its content is empty. 890 // We want to allow SIZEOF(.foo) where .foo is a section which happened to 891 // be empty. That is why this function is different from getOutputSection(). 892 template <class ELFT> 893 uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) { 894 for (OutputSectionBase *Sec : *OutputSections) 895 if (Sec->getName() == Name) 896 return Sec->Size; 897 return 0; 898 } 899 900 template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() { 901 return elf::getHeaderSize<ELFT>(); 902 } 903 904 template <class ELFT> 905 uint64_t LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) { 906 if (SymbolBody *B = Symtab<ELFT>::X->find(S)) 907 return B->getVA<ELFT>(); 908 error(Loc + ": symbol not found: " + S); 909 return 0; 910 } 911 912 template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) { 913 return Symtab<ELFT>::X->find(S) != nullptr; 914 } 915 916 template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) { 917 SymbolBody *Sym = Symtab<ELFT>::X->find(S); 918 auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym); 919 return DR && !DR->Section; 920 } 921 922 // Gets section symbol belongs to. Symbol "." doesn't belong to any 923 // specific section but isn't absolute at the same time, so we try 924 // to find suitable section for it as well. 925 template <class ELFT> 926 const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) { 927 SymbolBody *Sym = Symtab<ELFT>::X->find(S); 928 if (!Sym) { 929 if (OutputSections->empty()) 930 return nullptr; 931 return CurOutSec ? CurOutSec : (*OutputSections)[0]; 932 } 933 934 if (auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym)) 935 return DR->Section ? DR->Section->OutSec : nullptr; 936 if (auto *DS = dyn_cast_or_null<DefinedSynthetic>(Sym)) 937 return DS->Section; 938 939 return nullptr; 940 } 941 942 // Returns indices of ELF headers containing specific section, identified 943 // by Name. Each index is a zero based number of ELF header listed within 944 // PHDRS {} script block. 945 template <class ELFT> 946 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { 947 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 948 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 949 if (!Cmd || Cmd->Name != SectionName) 950 continue; 951 952 std::vector<size_t> Ret; 953 for (StringRef PhdrName : Cmd->Phdrs) 954 Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName)); 955 return Ret; 956 } 957 return {}; 958 } 959 960 template <class ELFT> 961 size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) { 962 size_t I = 0; 963 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { 964 if (Cmd.Name == PhdrName) 965 return I; 966 ++I; 967 } 968 error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS"); 969 return 0; 970 } 971 972 class elf::ScriptParser final : public ScriptParserBase { 973 typedef void (ScriptParser::*Handler)(); 974 975 public: 976 ScriptParser(MemoryBufferRef MB) 977 : ScriptParserBase(MB), 978 IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {} 979 980 void readLinkerScript(); 981 void readVersionScript(); 982 void readDynamicList(); 983 984 private: 985 void addFile(StringRef Path); 986 987 void readAsNeeded(); 988 void readEntry(); 989 void readExtern(); 990 void readGroup(); 991 void readInclude(); 992 void readOutput(); 993 void readOutputArch(); 994 void readOutputFormat(); 995 void readPhdrs(); 996 void readSearchDir(); 997 void readSections(); 998 void readVersion(); 999 void readVersionScriptCommand(); 1000 1001 SymbolAssignment *readAssignment(StringRef Name); 1002 BytesDataCommand *readBytesDataCommand(StringRef Tok); 1003 uint32_t readFill(); 1004 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec); 1005 uint32_t readOutputSectionFiller(StringRef Tok); 1006 std::vector<StringRef> readOutputSectionPhdrs(); 1007 InputSectionDescription *readInputSectionDescription(StringRef Tok); 1008 StringMatcher readFilePatterns(); 1009 std::vector<SectionPattern> readInputSectionsList(); 1010 InputSectionDescription *readInputSectionRules(StringRef FilePattern); 1011 unsigned readPhdrType(); 1012 SortSectionPolicy readSortKind(); 1013 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden); 1014 SymbolAssignment *readProvideOrAssignment(StringRef Tok); 1015 void readSort(); 1016 Expr readAssert(); 1017 1018 Expr readExpr(); 1019 Expr readExpr1(Expr Lhs, int MinPrec); 1020 StringRef readParenLiteral(); 1021 Expr readPrimary(); 1022 Expr readTernary(Expr Cond); 1023 Expr readParenExpr(); 1024 1025 // For parsing version script. 1026 std::vector<SymbolVersion> readVersionExtern(); 1027 void readAnonymousDeclaration(); 1028 void readVersionDeclaration(StringRef VerStr); 1029 std::vector<SymbolVersion> readSymbols(); 1030 1031 ScriptConfiguration &Opt = *ScriptConfig; 1032 bool IsUnderSysroot; 1033 }; 1034 1035 void ScriptParser::readDynamicList() { 1036 expect("{"); 1037 readAnonymousDeclaration(); 1038 if (!atEOF()) 1039 setError("EOF expected, but got " + next()); 1040 } 1041 1042 void ScriptParser::readVersionScript() { 1043 readVersionScriptCommand(); 1044 if (!atEOF()) 1045 setError("EOF expected, but got " + next()); 1046 } 1047 1048 void ScriptParser::readVersionScriptCommand() { 1049 if (consume("{")) { 1050 readAnonymousDeclaration(); 1051 return; 1052 } 1053 1054 while (!atEOF() && !Error && peek() != "}") { 1055 StringRef VerStr = next(); 1056 if (VerStr == "{") { 1057 setError("anonymous version definition is used in " 1058 "combination with other version definitions"); 1059 return; 1060 } 1061 expect("{"); 1062 readVersionDeclaration(VerStr); 1063 } 1064 } 1065 1066 void ScriptParser::readVersion() { 1067 expect("{"); 1068 readVersionScriptCommand(); 1069 expect("}"); 1070 } 1071 1072 void ScriptParser::readLinkerScript() { 1073 while (!atEOF()) { 1074 StringRef Tok = next(); 1075 if (Tok == ";") 1076 continue; 1077 1078 if (Tok == "ASSERT") { 1079 Opt.Commands.emplace_back(new AssertCommand(readAssert())); 1080 } else if (Tok == "ENTRY") { 1081 readEntry(); 1082 } else if (Tok == "EXTERN") { 1083 readExtern(); 1084 } else if (Tok == "GROUP" || Tok == "INPUT") { 1085 readGroup(); 1086 } else if (Tok == "INCLUDE") { 1087 readInclude(); 1088 } else if (Tok == "OUTPUT") { 1089 readOutput(); 1090 } else if (Tok == "OUTPUT_ARCH") { 1091 readOutputArch(); 1092 } else if (Tok == "OUTPUT_FORMAT") { 1093 readOutputFormat(); 1094 } else if (Tok == "PHDRS") { 1095 readPhdrs(); 1096 } else if (Tok == "SEARCH_DIR") { 1097 readSearchDir(); 1098 } else if (Tok == "SECTIONS") { 1099 readSections(); 1100 } else if (Tok == "VERSION") { 1101 readVersion(); 1102 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) { 1103 Opt.Commands.emplace_back(Cmd); 1104 } else { 1105 setError("unknown directive: " + Tok); 1106 } 1107 } 1108 } 1109 1110 void ScriptParser::addFile(StringRef S) { 1111 if (IsUnderSysroot && S.startswith("/")) { 1112 SmallString<128> PathData; 1113 StringRef Path = (Config->Sysroot + S).toStringRef(PathData); 1114 if (sys::fs::exists(Path)) { 1115 Driver->addFile(Saver.save(Path)); 1116 return; 1117 } 1118 } 1119 1120 if (sys::path::is_absolute(S)) { 1121 Driver->addFile(S); 1122 } else if (S.startswith("=")) { 1123 if (Config->Sysroot.empty()) 1124 Driver->addFile(S.substr(1)); 1125 else 1126 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); 1127 } else if (S.startswith("-l")) { 1128 Driver->addLibrary(S.substr(2)); 1129 } else if (sys::fs::exists(S)) { 1130 Driver->addFile(S); 1131 } else { 1132 if (Optional<std::string> Path = findFromSearchPaths(S)) 1133 Driver->addFile(Saver.save(*Path)); 1134 else 1135 setError("unable to find " + S); 1136 } 1137 } 1138 1139 void ScriptParser::readAsNeeded() { 1140 expect("("); 1141 bool Orig = Config->AsNeeded; 1142 Config->AsNeeded = true; 1143 while (!Error && !consume(")")) 1144 addFile(unquote(next())); 1145 Config->AsNeeded = Orig; 1146 } 1147 1148 void ScriptParser::readEntry() { 1149 // -e <symbol> takes predecence over ENTRY(<symbol>). 1150 expect("("); 1151 StringRef Tok = next(); 1152 if (Config->Entry.empty()) 1153 Config->Entry = Tok; 1154 expect(")"); 1155 } 1156 1157 void ScriptParser::readExtern() { 1158 expect("("); 1159 while (!Error && !consume(")")) 1160 Config->Undefined.push_back(next()); 1161 } 1162 1163 void ScriptParser::readGroup() { 1164 expect("("); 1165 while (!Error && !consume(")")) { 1166 StringRef Tok = next(); 1167 if (Tok == "AS_NEEDED") 1168 readAsNeeded(); 1169 else 1170 addFile(unquote(Tok)); 1171 } 1172 } 1173 1174 void ScriptParser::readInclude() { 1175 StringRef Tok = unquote(next()); 1176 // https://sourceware.org/binutils/docs/ld/File-Commands.html: 1177 // The file will be searched for in the current directory, and in any 1178 // directory specified with the -L option. 1179 auto MBOrErr = MemoryBuffer::getFile(Tok); 1180 if (!MBOrErr) 1181 if (Optional<std::string> Path = findFromSearchPaths(Tok)) 1182 MBOrErr = MemoryBuffer::getFile(*Path); 1183 if (!MBOrErr) { 1184 setError("cannot open " + Tok); 1185 return; 1186 } 1187 MemoryBufferRef MBRef = (*MBOrErr)->getMemBufferRef(); 1188 make<std::unique_ptr<MemoryBuffer>>(std::move(*MBOrErr)); // take MB ownership 1189 tokenize(MBRef); 1190 } 1191 1192 void ScriptParser::readOutput() { 1193 // -o <file> takes predecence over OUTPUT(<file>). 1194 expect("("); 1195 StringRef Tok = next(); 1196 if (Config->OutputFile.empty()) 1197 Config->OutputFile = unquote(Tok); 1198 expect(")"); 1199 } 1200 1201 void ScriptParser::readOutputArch() { 1202 // Error checking only for now. 1203 expect("("); 1204 skip(); 1205 expect(")"); 1206 } 1207 1208 void ScriptParser::readOutputFormat() { 1209 // Error checking only for now. 1210 expect("("); 1211 skip(); 1212 StringRef Tok = next(); 1213 if (Tok == ")") 1214 return; 1215 if (Tok != ",") { 1216 setError("unexpected token: " + Tok); 1217 return; 1218 } 1219 skip(); 1220 expect(","); 1221 skip(); 1222 expect(")"); 1223 } 1224 1225 void ScriptParser::readPhdrs() { 1226 expect("{"); 1227 while (!Error && !consume("}")) { 1228 StringRef Tok = next(); 1229 Opt.PhdrsCommands.push_back( 1230 {Tok, PT_NULL, false, false, UINT_MAX, nullptr}); 1231 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); 1232 1233 PhdrCmd.Type = readPhdrType(); 1234 do { 1235 Tok = next(); 1236 if (Tok == ";") 1237 break; 1238 if (Tok == "FILEHDR") 1239 PhdrCmd.HasFilehdr = true; 1240 else if (Tok == "PHDRS") 1241 PhdrCmd.HasPhdrs = true; 1242 else if (Tok == "AT") 1243 PhdrCmd.LMAExpr = readParenExpr(); 1244 else if (Tok == "FLAGS") { 1245 expect("("); 1246 // Passing 0 for the value of dot is a bit of a hack. It means that 1247 // we accept expressions like ".|1". 1248 PhdrCmd.Flags = readExpr()(0); 1249 expect(")"); 1250 } else 1251 setError("unexpected header attribute: " + Tok); 1252 } while (!Error); 1253 } 1254 } 1255 1256 void ScriptParser::readSearchDir() { 1257 expect("("); 1258 StringRef Tok = next(); 1259 if (!Config->Nostdlib) 1260 Config->SearchPaths.push_back(unquote(Tok)); 1261 expect(")"); 1262 } 1263 1264 void ScriptParser::readSections() { 1265 Opt.HasSections = true; 1266 // -no-rosegment is used to avoid placing read only non-executable sections in 1267 // their own segment. We do the same if SECTIONS command is present in linker 1268 // script. See comment for computeFlags(). 1269 Config->SingleRoRx = true; 1270 1271 expect("{"); 1272 while (!Error && !consume("}")) { 1273 StringRef Tok = next(); 1274 BaseCommand *Cmd = readProvideOrAssignment(Tok); 1275 if (!Cmd) { 1276 if (Tok == "ASSERT") 1277 Cmd = new AssertCommand(readAssert()); 1278 else 1279 Cmd = readOutputSectionDescription(Tok); 1280 } 1281 Opt.Commands.emplace_back(Cmd); 1282 } 1283 } 1284 1285 static int precedence(StringRef Op) { 1286 return StringSwitch<int>(Op) 1287 .Cases("*", "/", 5) 1288 .Cases("+", "-", 4) 1289 .Cases("<<", ">>", 3) 1290 .Cases("<", "<=", ">", ">=", "==", "!=", 2) 1291 .Cases("&", "|", 1) 1292 .Default(-1); 1293 } 1294 1295 StringMatcher ScriptParser::readFilePatterns() { 1296 std::vector<StringRef> V; 1297 while (!Error && !consume(")")) 1298 V.push_back(next()); 1299 return StringMatcher(V); 1300 } 1301 1302 SortSectionPolicy ScriptParser::readSortKind() { 1303 if (consume("SORT") || consume("SORT_BY_NAME")) 1304 return SortSectionPolicy::Name; 1305 if (consume("SORT_BY_ALIGNMENT")) 1306 return SortSectionPolicy::Alignment; 1307 if (consume("SORT_BY_INIT_PRIORITY")) 1308 return SortSectionPolicy::Priority; 1309 if (consume("SORT_NONE")) 1310 return SortSectionPolicy::None; 1311 return SortSectionPolicy::Default; 1312 } 1313 1314 // Method reads a list of sequence of excluded files and section globs given in 1315 // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+ 1316 // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3) 1317 // The semantics of that is next: 1318 // * Include .foo.1 from every file. 1319 // * Include .foo.2 from every file but a.o 1320 // * Include .foo.3 from every file but b.o 1321 std::vector<SectionPattern> ScriptParser::readInputSectionsList() { 1322 std::vector<SectionPattern> Ret; 1323 while (!Error && peek() != ")") { 1324 StringMatcher ExcludeFilePat; 1325 if (consume("EXCLUDE_FILE")) { 1326 expect("("); 1327 ExcludeFilePat = readFilePatterns(); 1328 } 1329 1330 std::vector<StringRef> V; 1331 while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE") 1332 V.push_back(next()); 1333 1334 if (!V.empty()) 1335 Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)}); 1336 else 1337 setError("section pattern is expected"); 1338 } 1339 return Ret; 1340 } 1341 1342 // Reads contents of "SECTIONS" directive. That directive contains a 1343 // list of glob patterns for input sections. The grammar is as follows. 1344 // 1345 // <patterns> ::= <section-list> 1346 // | <sort> "(" <section-list> ")" 1347 // | <sort> "(" <sort> "(" <section-list> ")" ")" 1348 // 1349 // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT" 1350 // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE" 1351 // 1352 // <section-list> is parsed by readInputSectionsList(). 1353 InputSectionDescription * 1354 ScriptParser::readInputSectionRules(StringRef FilePattern) { 1355 auto *Cmd = new InputSectionDescription(FilePattern); 1356 expect("("); 1357 while (!Error && !consume(")")) { 1358 SortSectionPolicy Outer = readSortKind(); 1359 SortSectionPolicy Inner = SortSectionPolicy::Default; 1360 std::vector<SectionPattern> V; 1361 if (Outer != SortSectionPolicy::Default) { 1362 expect("("); 1363 Inner = readSortKind(); 1364 if (Inner != SortSectionPolicy::Default) { 1365 expect("("); 1366 V = readInputSectionsList(); 1367 expect(")"); 1368 } else { 1369 V = readInputSectionsList(); 1370 } 1371 expect(")"); 1372 } else { 1373 V = readInputSectionsList(); 1374 } 1375 1376 for (SectionPattern &Pat : V) { 1377 Pat.SortInner = Inner; 1378 Pat.SortOuter = Outer; 1379 } 1380 1381 std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns)); 1382 } 1383 return Cmd; 1384 } 1385 1386 InputSectionDescription * 1387 ScriptParser::readInputSectionDescription(StringRef Tok) { 1388 // Input section wildcard can be surrounded by KEEP. 1389 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep 1390 if (Tok == "KEEP") { 1391 expect("("); 1392 StringRef FilePattern = next(); 1393 InputSectionDescription *Cmd = readInputSectionRules(FilePattern); 1394 expect(")"); 1395 Opt.KeptSections.push_back(Cmd); 1396 return Cmd; 1397 } 1398 return readInputSectionRules(Tok); 1399 } 1400 1401 void ScriptParser::readSort() { 1402 expect("("); 1403 expect("CONSTRUCTORS"); 1404 expect(")"); 1405 } 1406 1407 Expr ScriptParser::readAssert() { 1408 expect("("); 1409 Expr E = readExpr(); 1410 expect(","); 1411 StringRef Msg = unquote(next()); 1412 expect(")"); 1413 return [=](uint64_t Dot) { 1414 uint64_t V = E(Dot); 1415 if (!V) 1416 error(Msg); 1417 return V; 1418 }; 1419 } 1420 1421 // Reads a FILL(expr) command. We handle the FILL command as an 1422 // alias for =fillexp section attribute, which is different from 1423 // what GNU linkers do. 1424 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html 1425 uint32_t ScriptParser::readFill() { 1426 expect("("); 1427 uint32_t V = readOutputSectionFiller(next()); 1428 expect(")"); 1429 expect(";"); 1430 return V; 1431 } 1432 1433 OutputSectionCommand * 1434 ScriptParser::readOutputSectionDescription(StringRef OutSec) { 1435 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); 1436 Cmd->Location = getCurrentLocation(); 1437 1438 // Read an address expression. 1439 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address 1440 if (peek() != ":") 1441 Cmd->AddrExpr = readExpr(); 1442 1443 expect(":"); 1444 1445 if (consume("AT")) 1446 Cmd->LMAExpr = readParenExpr(); 1447 if (consume("ALIGN")) 1448 Cmd->AlignExpr = readParenExpr(); 1449 if (consume("SUBALIGN")) 1450 Cmd->SubalignExpr = readParenExpr(); 1451 1452 // Parse constraints. 1453 if (consume("ONLY_IF_RO")) 1454 Cmd->Constraint = ConstraintKind::ReadOnly; 1455 if (consume("ONLY_IF_RW")) 1456 Cmd->Constraint = ConstraintKind::ReadWrite; 1457 expect("{"); 1458 1459 while (!Error && !consume("}")) { 1460 StringRef Tok = next(); 1461 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) { 1462 Cmd->Commands.emplace_back(Assignment); 1463 } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) { 1464 Cmd->Commands.emplace_back(Data); 1465 } else if (Tok == "ASSERT") { 1466 Cmd->Commands.emplace_back(new AssertCommand(readAssert())); 1467 expect(";"); 1468 } else if (Tok == "FILL") { 1469 Cmd->Filler = readFill(); 1470 } else if (Tok == "SORT") { 1471 readSort(); 1472 } else if (peek() == "(") { 1473 Cmd->Commands.emplace_back(readInputSectionDescription(Tok)); 1474 } else { 1475 setError("unknown command " + Tok); 1476 } 1477 } 1478 Cmd->Phdrs = readOutputSectionPhdrs(); 1479 1480 if (consume("=")) 1481 Cmd->Filler = readOutputSectionFiller(next()); 1482 else if (peek().startswith("=")) 1483 Cmd->Filler = readOutputSectionFiller(next().drop_front()); 1484 1485 return Cmd; 1486 } 1487 1488 // Read "=<number>" where <number> is an octal/decimal/hexadecimal number. 1489 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html 1490 // 1491 // ld.gold is not fully compatible with ld.bfd. ld.bfd handles 1492 // hexstrings as blobs of arbitrary sizes, while ld.gold handles them 1493 // as 32-bit big-endian values. We will do the same as ld.gold does 1494 // because it's simpler than what ld.bfd does. 1495 uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) { 1496 uint32_t V; 1497 if (!Tok.getAsInteger(0, V)) 1498 return V; 1499 setError("invalid filler expression: " + Tok); 1500 return 0; 1501 } 1502 1503 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) { 1504 expect("("); 1505 SymbolAssignment *Cmd = readAssignment(next()); 1506 Cmd->Provide = Provide; 1507 Cmd->Hidden = Hidden; 1508 expect(")"); 1509 expect(";"); 1510 return Cmd; 1511 } 1512 1513 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) { 1514 SymbolAssignment *Cmd = nullptr; 1515 if (peek() == "=" || peek() == "+=") { 1516 Cmd = readAssignment(Tok); 1517 expect(";"); 1518 } else if (Tok == "PROVIDE") { 1519 Cmd = readProvideHidden(true, false); 1520 } else if (Tok == "HIDDEN") { 1521 Cmd = readProvideHidden(false, true); 1522 } else if (Tok == "PROVIDE_HIDDEN") { 1523 Cmd = readProvideHidden(true, true); 1524 } 1525 return Cmd; 1526 } 1527 1528 static uint64_t getSymbolValue(const Twine &Loc, StringRef S, uint64_t Dot) { 1529 if (S == ".") 1530 return Dot; 1531 return ScriptBase->getSymbolValue(Loc, S); 1532 } 1533 1534 static bool isAbsolute(StringRef S) { 1535 if (S == ".") 1536 return false; 1537 return ScriptBase->isAbsolute(S); 1538 } 1539 1540 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { 1541 StringRef Op = next(); 1542 Expr E; 1543 assert(Op == "=" || Op == "+="); 1544 if (consume("ABSOLUTE")) { 1545 // The RHS may be something like "ABSOLUTE(.) & 0xff". 1546 // Call readExpr1 to read the whole expression. 1547 E = readExpr1(readParenExpr(), 0); 1548 E.IsAbsolute = [] { return true; }; 1549 } else { 1550 E = readExpr(); 1551 } 1552 if (Op == "+=") { 1553 std::string Loc = getCurrentLocation(); 1554 E = [=](uint64_t Dot) { 1555 return getSymbolValue(Loc, Name, Dot) + E(Dot); 1556 }; 1557 } 1558 return new SymbolAssignment(Name, E); 1559 } 1560 1561 // This is an operator-precedence parser to parse a linker 1562 // script expression. 1563 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } 1564 1565 static Expr combine(StringRef Op, Expr L, Expr R) { 1566 if (Op == "*") 1567 return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; 1568 if (Op == "/") { 1569 return [=](uint64_t Dot) -> uint64_t { 1570 uint64_t RHS = R(Dot); 1571 if (RHS == 0) { 1572 error("division by zero"); 1573 return 0; 1574 } 1575 return L(Dot) / RHS; 1576 }; 1577 } 1578 if (Op == "+") 1579 return {[=](uint64_t Dot) { return L(Dot) + R(Dot); }, 1580 [=] { return L.IsAbsolute() && R.IsAbsolute(); }, 1581 [=] { 1582 const OutputSectionBase *S = L.Section(); 1583 return S ? S : R.Section(); 1584 }}; 1585 if (Op == "-") 1586 return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; 1587 if (Op == "<<") 1588 return [=](uint64_t Dot) { return L(Dot) << R(Dot); }; 1589 if (Op == ">>") 1590 return [=](uint64_t Dot) { return L(Dot) >> R(Dot); }; 1591 if (Op == "<") 1592 return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; 1593 if (Op == ">") 1594 return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; 1595 if (Op == ">=") 1596 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; 1597 if (Op == "<=") 1598 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; 1599 if (Op == "==") 1600 return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; 1601 if (Op == "!=") 1602 return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; 1603 if (Op == "&") 1604 return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; 1605 if (Op == "|") 1606 return [=](uint64_t Dot) { return L(Dot) | R(Dot); }; 1607 llvm_unreachable("invalid operator"); 1608 } 1609 1610 // This is a part of the operator-precedence parser. This function 1611 // assumes that the remaining token stream starts with an operator. 1612 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { 1613 while (!atEOF() && !Error) { 1614 // Read an operator and an expression. 1615 if (consume("?")) 1616 return readTernary(Lhs); 1617 StringRef Op1 = peek(); 1618 if (precedence(Op1) < MinPrec) 1619 break; 1620 skip(); 1621 Expr Rhs = readPrimary(); 1622 1623 // Evaluate the remaining part of the expression first if the 1624 // next operator has greater precedence than the previous one. 1625 // For example, if we have read "+" and "3", and if the next 1626 // operator is "*", then we'll evaluate 3 * ... part first. 1627 while (!atEOF()) { 1628 StringRef Op2 = peek(); 1629 if (precedence(Op2) <= precedence(Op1)) 1630 break; 1631 Rhs = readExpr1(Rhs, precedence(Op2)); 1632 } 1633 1634 Lhs = combine(Op1, Lhs, Rhs); 1635 } 1636 return Lhs; 1637 } 1638 1639 uint64_t static getConstant(StringRef S) { 1640 if (S == "COMMONPAGESIZE") 1641 return Target->PageSize; 1642 if (S == "MAXPAGESIZE") 1643 return Config->MaxPageSize; 1644 error("unknown constant: " + S); 1645 return 0; 1646 } 1647 1648 // Parses Tok as an integer. Returns true if successful. 1649 // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H") 1650 // and decimal numbers. Decimal numbers may have "K" (kilo) or 1651 // "M" (mega) prefixes. 1652 static bool readInteger(StringRef Tok, uint64_t &Result) { 1653 // Negative number 1654 if (Tok.startswith("-")) { 1655 if (!readInteger(Tok.substr(1), Result)) 1656 return false; 1657 Result = -Result; 1658 return true; 1659 } 1660 1661 // Hexadecimal 1662 if (Tok.startswith_lower("0x")) 1663 return !Tok.substr(2).getAsInteger(16, Result); 1664 if (Tok.endswith_lower("H")) 1665 return !Tok.drop_back().getAsInteger(16, Result); 1666 1667 // Decimal 1668 int Suffix = 1; 1669 if (Tok.endswith_lower("K")) { 1670 Suffix = 1024; 1671 Tok = Tok.drop_back(); 1672 } else if (Tok.endswith_lower("M")) { 1673 Suffix = 1024 * 1024; 1674 Tok = Tok.drop_back(); 1675 } 1676 if (Tok.getAsInteger(10, Result)) 1677 return false; 1678 Result *= Suffix; 1679 return true; 1680 } 1681 1682 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) { 1683 int Size = StringSwitch<unsigned>(Tok) 1684 .Case("BYTE", 1) 1685 .Case("SHORT", 2) 1686 .Case("LONG", 4) 1687 .Case("QUAD", 8) 1688 .Default(-1); 1689 if (Size == -1) 1690 return nullptr; 1691 1692 return new BytesDataCommand(readParenExpr(), Size); 1693 } 1694 1695 StringRef ScriptParser::readParenLiteral() { 1696 expect("("); 1697 StringRef Tok = next(); 1698 expect(")"); 1699 return Tok; 1700 } 1701 1702 Expr ScriptParser::readPrimary() { 1703 if (peek() == "(") 1704 return readParenExpr(); 1705 1706 StringRef Tok = next(); 1707 std::string Location = getCurrentLocation(); 1708 1709 if (Tok == "~") { 1710 Expr E = readPrimary(); 1711 return [=](uint64_t Dot) { return ~E(Dot); }; 1712 } 1713 if (Tok == "-") { 1714 Expr E = readPrimary(); 1715 return [=](uint64_t Dot) { return -E(Dot); }; 1716 } 1717 1718 // Built-in functions are parsed here. 1719 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. 1720 if (Tok == "ADDR") { 1721 StringRef Name = readParenLiteral(); 1722 return {[=](uint64_t Dot) { 1723 return ScriptBase->getOutputSection(Location, Name)->Addr; 1724 }, 1725 [=] { return false; }, 1726 [=] { return ScriptBase->getOutputSection(Location, Name); }}; 1727 } 1728 if (Tok == "LOADADDR") { 1729 StringRef Name = readParenLiteral(); 1730 return [=](uint64_t Dot) { 1731 return ScriptBase->getOutputSection(Location, Name)->getLMA(); 1732 }; 1733 } 1734 if (Tok == "ASSERT") 1735 return readAssert(); 1736 if (Tok == "ALIGN") { 1737 expect("("); 1738 Expr E = readExpr(); 1739 if (consume(",")) { 1740 Expr E2 = readExpr(); 1741 expect(")"); 1742 return [=](uint64_t Dot) { return alignTo(E(Dot), E2(Dot)); }; 1743 } 1744 expect(")"); 1745 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1746 } 1747 if (Tok == "CONSTANT") { 1748 StringRef Name = readParenLiteral(); 1749 return [=](uint64_t Dot) { return getConstant(Name); }; 1750 } 1751 if (Tok == "DEFINED") { 1752 StringRef Name = readParenLiteral(); 1753 return [=](uint64_t Dot) { return ScriptBase->isDefined(Name) ? 1 : 0; }; 1754 } 1755 if (Tok == "SEGMENT_START") { 1756 expect("("); 1757 skip(); 1758 expect(","); 1759 Expr E = readExpr(); 1760 expect(")"); 1761 return [=](uint64_t Dot) { return E(Dot); }; 1762 } 1763 if (Tok == "DATA_SEGMENT_ALIGN") { 1764 expect("("); 1765 Expr E = readExpr(); 1766 expect(","); 1767 readExpr(); 1768 expect(")"); 1769 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1770 } 1771 if (Tok == "DATA_SEGMENT_END") { 1772 expect("("); 1773 expect("."); 1774 expect(")"); 1775 return [](uint64_t Dot) { return Dot; }; 1776 } 1777 // GNU linkers implements more complicated logic to handle 1778 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to 1779 // the next page boundary for simplicity. 1780 if (Tok == "DATA_SEGMENT_RELRO_END") { 1781 expect("("); 1782 readExpr(); 1783 expect(","); 1784 readExpr(); 1785 expect(")"); 1786 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; 1787 } 1788 if (Tok == "SIZEOF") { 1789 StringRef Name = readParenLiteral(); 1790 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); }; 1791 } 1792 if (Tok == "ALIGNOF") { 1793 StringRef Name = readParenLiteral(); 1794 return [=](uint64_t Dot) { 1795 return ScriptBase->getOutputSection(Location, Name)->Addralign; 1796 }; 1797 } 1798 if (Tok == "SIZEOF_HEADERS") 1799 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); }; 1800 1801 // Tok is a literal number. 1802 uint64_t V; 1803 if (readInteger(Tok, V)) 1804 return [=](uint64_t Dot) { return V; }; 1805 1806 // Tok is a symbol name. 1807 if (Tok != "." && !isValidCIdentifier(Tok)) 1808 setError("malformed number: " + Tok); 1809 return {[=](uint64_t Dot) { return getSymbolValue(Location, Tok, Dot); }, 1810 [=] { return isAbsolute(Tok); }, 1811 [=] { return ScriptBase->getSymbolSection(Tok); }}; 1812 } 1813 1814 Expr ScriptParser::readTernary(Expr Cond) { 1815 Expr L = readExpr(); 1816 expect(":"); 1817 Expr R = readExpr(); 1818 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; 1819 } 1820 1821 Expr ScriptParser::readParenExpr() { 1822 expect("("); 1823 Expr E = readExpr(); 1824 expect(")"); 1825 return E; 1826 } 1827 1828 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { 1829 std::vector<StringRef> Phdrs; 1830 while (!Error && peek().startswith(":")) { 1831 StringRef Tok = next(); 1832 Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1)); 1833 } 1834 return Phdrs; 1835 } 1836 1837 // Read a program header type name. The next token must be a 1838 // name of a program header type or a constant (e.g. "0x3"). 1839 unsigned ScriptParser::readPhdrType() { 1840 StringRef Tok = next(); 1841 uint64_t Val; 1842 if (readInteger(Tok, Val)) 1843 return Val; 1844 1845 unsigned Ret = StringSwitch<unsigned>(Tok) 1846 .Case("PT_NULL", PT_NULL) 1847 .Case("PT_LOAD", PT_LOAD) 1848 .Case("PT_DYNAMIC", PT_DYNAMIC) 1849 .Case("PT_INTERP", PT_INTERP) 1850 .Case("PT_NOTE", PT_NOTE) 1851 .Case("PT_SHLIB", PT_SHLIB) 1852 .Case("PT_PHDR", PT_PHDR) 1853 .Case("PT_TLS", PT_TLS) 1854 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) 1855 .Case("PT_GNU_STACK", PT_GNU_STACK) 1856 .Case("PT_GNU_RELRO", PT_GNU_RELRO) 1857 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE) 1858 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED) 1859 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA) 1860 .Default(-1); 1861 1862 if (Ret == (unsigned)-1) { 1863 setError("invalid program header type: " + Tok); 1864 return PT_NULL; 1865 } 1866 return Ret; 1867 } 1868 1869 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };". 1870 void ScriptParser::readAnonymousDeclaration() { 1871 // Read global symbols first. "global:" is default, so if there's 1872 // no label, we assume global symbols. 1873 if (consume("global:") || peek() != "local:") 1874 Config->VersionScriptGlobals = readSymbols(); 1875 1876 // Next, read local symbols. 1877 if (consume("local:")) { 1878 if (consume("*")) { 1879 Config->DefaultSymbolVersion = VER_NDX_LOCAL; 1880 expect(";"); 1881 } else { 1882 setError("local symbol list for anonymous version is not supported"); 1883 } 1884 } 1885 expect("}"); 1886 expect(";"); 1887 } 1888 1889 // Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };". 1890 void ScriptParser::readVersionDeclaration(StringRef VerStr) { 1891 // Identifiers start at 2 because 0 and 1 are reserved 1892 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants. 1893 uint16_t VersionId = Config->VersionDefinitions.size() + 2; 1894 Config->VersionDefinitions.push_back({VerStr, VersionId}); 1895 1896 // Read global symbols. 1897 if (consume("global:") || peek() != "local:") 1898 Config->VersionDefinitions.back().Globals = readSymbols(); 1899 1900 // Read local symbols. 1901 if (consume("local:")) { 1902 if (consume("*")) { 1903 Config->DefaultSymbolVersion = VER_NDX_LOCAL; 1904 expect(";"); 1905 } else { 1906 for (SymbolVersion V : readSymbols()) 1907 Config->VersionScriptLocals.push_back(V); 1908 } 1909 } 1910 expect("}"); 1911 1912 // Each version may have a parent version. For example, "Ver2" 1913 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" 1914 // as a parent. This version hierarchy is, probably against your 1915 // instinct, purely for hint; the runtime doesn't care about it 1916 // at all. In LLD, we simply ignore it. 1917 if (peek() != ";") 1918 skip(); 1919 expect(";"); 1920 } 1921 1922 // Reads a list of symbols for a versions cript. 1923 std::vector<SymbolVersion> ScriptParser::readSymbols() { 1924 std::vector<SymbolVersion> Ret; 1925 for (;;) { 1926 if (consume("extern")) { 1927 for (SymbolVersion V : readVersionExtern()) 1928 Ret.push_back(V); 1929 continue; 1930 } 1931 1932 if (peek() == "}" || peek() == "local:" || Error) 1933 break; 1934 StringRef Tok = next(); 1935 Ret.push_back({unquote(Tok), false, hasWildcard(Tok)}); 1936 expect(";"); 1937 } 1938 return Ret; 1939 } 1940 1941 // Reads an "extern C++" directive, e.g., 1942 // "extern "C++" { ns::*; "f(int, double)"; };" 1943 std::vector<SymbolVersion> ScriptParser::readVersionExtern() { 1944 StringRef Tok = next(); 1945 bool IsCXX = Tok == "\"C++\""; 1946 if (!IsCXX && Tok != "\"C\"") 1947 setError("Unknown language"); 1948 expect("{"); 1949 1950 std::vector<SymbolVersion> Ret; 1951 while (!Error && peek() != "}") { 1952 StringRef Tok = next(); 1953 bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok); 1954 Ret.push_back({unquote(Tok), IsCXX, HasWildcard}); 1955 expect(";"); 1956 } 1957 1958 expect("}"); 1959 expect(";"); 1960 return Ret; 1961 } 1962 1963 void elf::readLinkerScript(MemoryBufferRef MB) { 1964 ScriptParser(MB).readLinkerScript(); 1965 } 1966 1967 void elf::readVersionScript(MemoryBufferRef MB) { 1968 ScriptParser(MB).readVersionScript(); 1969 } 1970 1971 void elf::readDynamicList(MemoryBufferRef MB) { 1972 ScriptParser(MB).readDynamicList(); 1973 } 1974 1975 template class elf::LinkerScript<ELF32LE>; 1976 template class elf::LinkerScript<ELF32BE>; 1977 template class elf::LinkerScript<ELF64LE>; 1978 template class elf::LinkerScript<ELF64BE>; 1979