From cf43f179b15f9db43b0ce32994cea2bd8be0f272 Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Wed, 5 Oct 2016 09:36:59 +0000 Subject: [PATCH] [ELF] make KEEP command recognize file patterns Differential revision: https://reviews.llvm.org/D25242 llvm-svn: 283305 --- lld/ELF/LinkerScript.cpp | 15 ++++++++++----- lld/ELF/LinkerScript.h | 2 +- lld/test/ELF/linkerscript/Inputs/keep.s | 2 ++ lld/test/ELF/linkerscript/sections-keep.s | 11 +++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 lld/test/ELF/linkerscript/Inputs/keep.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 5cdee7082e1a..b0a83d8d5cf3 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -108,9 +108,15 @@ template LinkerScript::~LinkerScript() {} template bool LinkerScript::shouldKeep(InputSectionBase *S) { - for (Regex *Re : Opt.KeptSections) - if (Re->match(S->Name)) - return true; + for (InputSectionDescription *ID : Opt.KeptSections) { + StringRef Filename = S->getFile()->getName(); + if (!ID->FileRe.match(sys::path::filename(Filename))) + continue; + + for (SectionPattern &P : ID->SectionPatterns) + if (P.SectionRe.match(S->Name)) + return true; + } return false; } @@ -1247,8 +1253,7 @@ ScriptParser::readInputSectionDescription(StringRef Tok) { StringRef FilePattern = next(); InputSectionDescription *Cmd = readInputSectionRules(FilePattern); expect(")"); - for (SectionPattern &Pat : Cmd->SectionPatterns) - Opt.KeptSections.push_back(&Pat.SectionRe); + Opt.KeptSections.push_back(Cmd); return Cmd; } return readInputSectionRules(Tok); diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 47b7b683a34c..ac73d4ea7b22 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -184,7 +184,7 @@ struct ScriptConfiguration { // List of section patterns specified with KEEP commands. They will // be kept even if they are unused and --gc-sections is specified. - std::vector KeptSections; + std::vector KeptSections; }; extern ScriptConfiguration *ScriptConfig; diff --git a/lld/test/ELF/linkerscript/Inputs/keep.s b/lld/test/ELF/linkerscript/Inputs/keep.s new file mode 100644 index 000000000000..20fa41c1adff --- /dev/null +++ b/lld/test/ELF/linkerscript/Inputs/keep.s @@ -0,0 +1,2 @@ +.section .keep, "a" + .long 0x41414141 diff --git a/lld/test/ELF/linkerscript/sections-keep.s b/lld/test/ELF/linkerscript/sections-keep.s index 72837303ec18..0758007e924a 100644 --- a/lld/test/ELF/linkerscript/sections-keep.s +++ b/lld/test/ELF/linkerscript/sections-keep.s @@ -1,5 +1,6 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/keep.s -o %t2.o ## First check that section "keep" is garbage collected without using KEEP # RUN: echo "SECTIONS { \ @@ -69,6 +70,16 @@ # MIXED2-NEXT: 5 .shstrtab 0000002f 0000000000000000 # MIXED2-NEXT: 6 .strtab 00000012 0000000000000000 +# Check file pattern for kept sections. +# RUN: echo "SECTIONS { \ +# RUN: . = SIZEOF_HEADERS; \ +# RUN: .keep : { KEEP(*2.o(.keep)) } \ +# RUN: }" > %t.script +# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t2.o %t +# RUN: llvm-objdump -s %t1 | FileCheck -check-prefix=FILEMATCH %s +# FILEMATCH: Contents of section .keep: +# FILEMATCH-NEXT: 00e8 41414141 AAAA + .global _start _start: mov temp, %eax