return false;
}
-static bool fileMatches(const InputSectionDescription *Desc,
- StringRef Filename) {
- return const_cast<Regex &>(Desc->FileRe).match(Filename) &&
- !const_cast<Regex &>(Desc->ExcludedFileRe).match(Filename);
+static bool fileMatches(const llvm::Regex &FileRe,
+ const llvm::Regex &ExcludedFileRe, StringRef Filename) {
+ return const_cast<Regex &>(FileRe).match(Filename) &&
+ !const_cast<Regex &>(ExcludedFileRe).match(Filename);
}
static bool comparePriority(InputSectionData *A, InputSectionData *B) {
template <class ELFT>
std::vector<InputSectionBase<ELFT> *>
LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
- const Regex &Re = I->SectionRe;
std::vector<InputSectionBase<ELFT> *> Ret;
- for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
- if (fileMatches(I, sys::path::filename(F->getName())))
- for (InputSectionBase<ELFT> *S : F->getSections())
- if (!isDiscarded(S) && !S->OutSec &&
- const_cast<Regex &>(Re).match(S->Name))
- Ret.push_back(S);
-
- if (const_cast<Regex &>(Re).match("COMMON"))
- Ret.push_back(CommonInputSection<ELFT>::X);
+ for (const std::pair<llvm::Regex, llvm::Regex> &V : I->SectionsVec) {
+ for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
+ if (fileMatches(I->FileRe, V.first, sys::path::filename(F->getName()))) {
+
+ Regex &Re = const_cast<Regex &>(V.second);
+ for (InputSectionBase<ELFT> *S : F->getSections())
+ if (!isDiscarded(S) && !S->OutSec && Re.match(S->Name))
+ Ret.push_back(S);
+
+ if (Re.match("COMMON"))
+ Ret.push_back(CommonInputSection<ELFT>::X);
+ }
+ }
+ }
return Ret;
}
std::vector<StringRef> readOutputSectionPhdrs();
InputSectionDescription *readInputSectionDescription(StringRef Tok);
Regex readFilePatterns();
+ void readSectionExcludes(InputSectionDescription *Cmd);
InputSectionDescription *readInputSectionRules(StringRef FilePattern);
unsigned readPhdrType();
SortKind readSortKind();
return SortNone;
}
+// Method reads a list of sequence of excluded files and section globs given in
+// a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
+// Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
+void ScriptParser::readSectionExcludes(InputSectionDescription *Cmd) {
+ llvm::Regex ExcludeFileRe;
+ std::vector<StringRef> V;
+
+ while (!Error) {
+ if (skip(")")) {
+ Cmd->SectionsVec.push_back(
+ {std::move(ExcludeFileRe), compileGlobPatterns(V)});
+ return;
+ }
+
+ if (skip("EXCLUDE_FILE")) {
+ if (!V.empty()) {
+ Cmd->SectionsVec.push_back(
+ {std::move(ExcludeFileRe), compileGlobPatterns(V)});
+ V.clear();
+ }
+
+ expect("(");
+ ExcludeFileRe = readFilePatterns();
+ continue;
+ }
+
+ V.push_back(next());
+ }
+}
+
InputSectionDescription *
ScriptParser::readInputSectionRules(StringRef FilePattern) {
auto *Cmd = new InputSectionDescription(FilePattern);
expect("(");
- // Read EXCLUDE_FILE().
- if (skip("EXCLUDE_FILE")) {
- expect("(");
- Cmd->ExcludedFileRe = readFilePatterns();
- }
-
// Read SORT().
if (SortKind K1 = readSortKind()) {
Cmd->SortOuter = K1;
if (SortKind K2 = readSortKind()) {
Cmd->SortInner = K2;
expect("(");
- Cmd->SectionRe = readFilePatterns();
+ Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()});
expect(")");
} else {
- Cmd->SectionRe = readFilePatterns();
+ Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()});
}
expect(")");
return Cmd;
}
- Cmd->SectionRe = readFilePatterns();
+ readSectionExcludes(Cmd);
return Cmd;
}
StringRef FilePattern = next();
InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
expect(")");
- Opt.KeptSections.push_back(&Cmd->SectionRe);
+ for (std::pair<llvm::Regex, llvm::Regex> &Regex : Cmd->SectionsVec)
+ Opt.KeptSections.push_back(&Regex.second);
return Cmd;
}
return readInputSectionRules(Tok);
--- /dev/null
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tfile1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/exclude-multiple1.s -o %tfile2.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/exclude-multiple2.s -o %tfile3.o
+# RUN: echo "SECTIONS { \
+# RUN: .foo : { *(.foo.1 EXCLUDE_FILE (*file1.o) .foo.2 EXCLUDE_FILE (*file2.o) .foo.3) } \
+# RUN: }" > %t1.script
+# RUN: ld.lld -script %t1.script %tfile1.o %tfile2.o %tfile3.o -o %t1.o
+# RUN: llvm-objdump -s %t1.o | FileCheck %s
+
+# CHECK: Contents of section .foo:
+# CHECK-NEXT: 0120 01000000 00000000 04000000 00000000
+# CHECK-NEXT: 0130 07000000 00000000 05000000 00000000
+# CHECK-NEXT: 0140 08000000 00000000 03000000 00000000
+# CHECK-NEXT: 0150 09000000 00000000
+# CHECK-NEXT: Contents of section .foo.2:
+# CHECK-NEXT: 0158 02000000 00000000
+# CHECK-NEXT: Contents of section .foo.3:
+# CHECK-NEXT: 0160 06000000 00000000
+
+.section .foo.1,"a"
+ .quad 1
+
+.section .foo.2,"a"
+ .quad 2
+
+.section .foo.3,"a"
+ .quad 3