[PATCH] D25242: [ELF] Linkerscript: fix KEEP

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 09:33:43 PDT 2016


evgeny777 created this revision.
evgeny777 added reviewers: ruiu, rafael.
evgeny777 added subscribers: grimar, ikudrin, llvm-commits.
evgeny777 set the repository for this revision to rL LLVM.
evgeny777 added a project: lld.

Our implementation of KEEP doesn't check file pattern. This patch fixes it.


Repository:
  rL LLVM

https://reviews.llvm.org/D25242

Files:
  ELF/LinkerScript.cpp
  ELF/LinkerScript.h
  test/ELF/linkerscript/Inputs/keep.s
  test/ELF/linkerscript/sections-keep.s


Index: test/ELF/linkerscript/sections-keep.s
===================================================================
--- test/ELF/linkerscript/sections-keep.s
+++ 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
Index: test/ELF/linkerscript/Inputs/keep.s
===================================================================
--- test/ELF/linkerscript/Inputs/keep.s
+++ test/ELF/linkerscript/Inputs/keep.s
@@ -0,0 +1,2 @@
+.section .keep, "a"
+ .long 0x41414141
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -184,7 +184,7 @@
 
   // List of section patterns specified with KEEP commands. They will
   // be kept even if they are unused and --gc-sections is specified.
-  std::vector<llvm::Regex *> KeptSections;
+  std::vector<InputSectionDescription *> KeptSections;
 };
 
 extern ScriptConfiguration *ScriptConfig;
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -108,9 +108,15 @@
 
 template <class ELFT>
 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *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 @@
     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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25242.73496.patch
Type: text/x-patch
Size: 2782 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161004/0c33a6cf/attachment.bin>


More information about the llvm-commits mailing list