Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -83,6 +83,7 @@ struct InputSectionDescription : BaseCommand { InputSectionDescription() : BaseCommand(InputSectionKind) {} static bool classof(const BaseCommand *C); + StringRef IncludedFiles; std::vector ExcludedFiles; std::vector Patterns; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -92,6 +92,13 @@ return Ret; } +static bool fileMatches(ArrayRef ExcludedFiles, + StringRef IncludedFiles, StringRef FileName) { + if (!globMatch(IncludedFiles, FileName)) + return false; + return ExcludedFiles.empty() || !match(ExcludedFiles, FileName); +} + // Returns input sections filtered by given glob patterns. template std::vector *> @@ -100,12 +107,14 @@ ArrayRef ExcludedFiles = I->ExcludedFiles; std::vector *> Ret; for (const std::unique_ptr> &F : - Symtab::X->getObjectFiles()) + Symtab::X->getObjectFiles()) { + if (!fileMatches(I->ExcludedFiles, I->IncludedFiles, + sys::path::filename(F->getName()))) + continue; for (InputSectionBase *S : F->getSections()) if (!isDiscarded(S) && !S->OutSec && match(Patterns, S->getSectionName())) - if (ExcludedFiles.empty() || - !match(ExcludedFiles, sys::path::filename(F->getName()))) - Ret.push_back(S); + Ret.push_back(S); + } return Ret; } @@ -424,9 +433,7 @@ void readAsNeeded(); void readEntry(); void readExtern(); - std::unique_ptr readFilePattern(); void readGroup(); - void readKeep(OutputSectionCommand *Cmd); void readInclude(); void readNothing() {} void readOutput(); @@ -439,6 +446,9 @@ SymbolAssignment *readAssignment(StringRef Name); void readOutputSectionDescription(StringRef OutSec); std::vector readOutputSectionPhdrs(); + void readInputSectionDescription(InputSectionDescription *InCmd, bool Keep); + void continueReadInputSectionDescription(InputSectionDescription *InCmd, + bool Keep); unsigned readPhdrType(); void readProvide(bool Hidden); void readAlign(OutputSectionCommand *Cmd); @@ -667,32 +677,36 @@ .Default(-1); } -std::unique_ptr ScriptParser::readFilePattern() { - expect("*"); - expect("("); - - auto InCmd = llvm::make_unique(); +void ScriptParser::continueReadInputSectionDescription( + InputSectionDescription *InCmd, bool Keep) { + if (skip("SORT")) { + expect("("); + InCmd->Sort = true; + continueReadInputSectionDescription(InCmd, Keep); + expect(")"); + return; + } if (skip("EXCLUDE_FILE")) { expect("("); while (!Error && !skip(")")) InCmd->ExcludedFiles.push_back(next()); + continueReadInputSectionDescription(InCmd, Keep); + return; + } + + while (!Error && !skip(")")) { + if (Keep) + Opt.KeptSections.push_back(peek()); InCmd->Patterns.push_back(next()); - expect(")"); - } else { - while (!Error && !skip(")")) - InCmd->Patterns.push_back(next()); } - return InCmd; } -void ScriptParser::readKeep(OutputSectionCommand *Cmd) { +void ScriptParser::readInputSectionDescription(InputSectionDescription *InCmd, + bool Keep) { + InCmd->IncludedFiles = next(); expect("("); - std::unique_ptr InCmd = readFilePattern(); - Opt.KeptSections.insert(Opt.KeptSections.end(), InCmd->Patterns.begin(), - InCmd->Patterns.end()); - Cmd->Commands.push_back(std::move(InCmd)); - expect(")"); + continueReadInputSectionDescription(InCmd, Keep); } void ScriptParser::readAlign(OutputSectionCommand *Cmd) { @@ -723,19 +737,30 @@ expect("{"); while (!Error && !skip("}")) { - StringRef Tok = next(); - if (Tok == "*") { + if ((!peek().empty() && peek()[0] == '*') || peek() == "KEEP") { auto *InCmd = new InputSectionDescription(); Cmd->Commands.emplace_back(InCmd); - expect("("); - while (!Error && !skip(")")) - InCmd->Patterns.push_back(next()); - } else if (Tok == "KEEP") { - readKeep(Cmd); - } else if (Tok == "PROVIDE") { + // Input section wildcard can be surrounded by KEEP. + // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep + if (skip("KEEP")) { + expect("("); + readInputSectionDescription(InCmd, true); + expect(")"); + } else { + readInputSectionDescription(InCmd, false); + } + continue; + } + + StringRef Tok = next(); + if (Tok == "PROVIDE") { readProvide(false); } else if (Tok == "PROVIDE_HIDDEN") { readProvide(true); + } else if (skip("SORT")) { + expect("("); + expect("CONSTRUCTORS"); + expect(")"); } else { setError("unknown command " + Tok); } Index: test/ELF/linkerscript/Inputs/linkerscript-filename-spec.s =================================================================== --- test/ELF/linkerscript/Inputs/linkerscript-filename-spec.s +++ test/ELF/linkerscript/Inputs/linkerscript-filename-spec.s @@ -0,0 +1,2 @@ +.section .foo,"a" + .quad 0x11 Index: test/ELF/linkerscript/linkerscript-filename-spec.s =================================================================== --- test/ELF/linkerscript/linkerscript-filename-spec.s +++ test/ELF/linkerscript/linkerscript-filename-spec.s @@ -0,0 +1,41 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tfirst.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \ +# RUN: %p/Inputs/linkerscript-filename-spec.s -o %tsecond.o + +# RUN: echo "SECTIONS { .foo : { \ +# RUN: KEEP(*first.o(.foo)) \ +# RUN: KEEP(*second.o(.foo)) } }" > %t1.script +# RUN: ld.lld -o %t1 --script %t1.script %tfirst.o %tsecond.o +# RUN: llvm-objdump -s %t1 | FileCheck --check-prefix=FIRSTSECOND %s +# FIRSTSECOND: Contents of section .foo: +# FIRSTSECOND-NEXT: 0120 01000000 00000000 11000000 00000000 + +# RUN: echo "SECTIONS { .foo : { \ +# RUN: KEEP(*second.o(.foo)) \ +# RUN: KEEP(*first.o(.foo)) } }" > %t2.script +# RUN: ld.lld -o %t2 --script %t2.script %tfirst.o %tsecond.o +# RUN: llvm-objdump -s %t2 | FileCheck --check-prefix=SECONDFIRST %s +# SECONDFIRST: Contents of section .foo: +# SECONDFIRST-NEXT: 0120 11000000 00000000 01000000 00000000 + +## Now the same tests but without KEEP. Checking that file name inside +## KEEP is parsed fine. +# RUN: echo "SECTIONS { .foo : { \ +# RUN: *first.o(.foo) \ +# RUN: *second.o(.foo) } }" > %t3.script +# RUN: ld.lld -o %t3 --script %t3.script %tfirst.o %tsecond.o +# RUN: llvm-objdump -s %t3 | FileCheck --check-prefix=FIRSTSECOND %s + +# RUN: echo "SECTIONS { .foo : { \ +# RUN: *second.o(.foo) \ +# RUN: *first.o(.foo) } }" > %t4.script +# RUN: ld.lld -o %t4 --script %t4.script %tfirst.o %tsecond.o +# RUN: llvm-objdump -s %t4 | FileCheck --check-prefix=SECONDFIRST %s + +.global _start +_start: + nop + +.section .foo,"a" + .quad 1