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