[lld] r311713 - Currently lld uses base names of files to match against file patterns in

Dmitry Mikulin via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 24 15:01:41 PDT 2017


Author: dmikulin
Date: Thu Aug 24 15:01:40 2017
New Revision: 311713

URL: http://llvm.org/viewvc/llvm-project?rev=311713&view=rev
Log:
Currently lld uses base names of files to match against file patterns in
linker script SECTION rules. This patch extends it to use a fully specified
file name as it appears in --trace output to match agains, i.e,
"<path>/<objname>.o" or "<path>/<libname>.a(<objname>.o)".

Differential Revision: https://reviews.llvm.org/D37031

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript/filename-spec.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=311713&r1=311712&r2=311713&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Aug 24 15:01:40 2017
@@ -174,18 +174,22 @@ bool BytesDataCommand::classof(const Bas
   return C->Kind == BytesDataKind;
 }
 
-static StringRef basename(InputSectionBase *S) {
-  if (S->File)
-    return sys::path::filename(S->File->getName());
-  return "";
+static std::string filename(InputSectionBase *S) {
+  if (!S->File)
+    return "";
+  if (S->File->ArchiveName.empty())
+    return S->File->getName();
+  return (S->File->ArchiveName + "(" + S->File->getName() + ")").str();
 }
 
 bool LinkerScript::shouldKeep(InputSectionBase *S) {
-  for (InputSectionDescription *ID : Opt.KeptSections)
-    if (ID->FilePat.match(basename(S)))
+  for (InputSectionDescription *ID : Opt.KeptSections) {
+    std::string Filename = filename(S);
+    if (ID->FilePat.match(Filename))
       for (SectionPattern &P : ID->SectionPatterns)
         if (P.SectionPat.match(S->Name))
           return true;
+  }
   return false;
 }
 
@@ -280,7 +284,7 @@ LinkerScript::computeInputSections(const
       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
         continue;
 
-      StringRef Filename = basename(Sec);
+      std::string Filename = filename(Sec);
       if (!Cmd->FilePat.match(Filename) ||
           Pat.ExcludedFilePat.match(Filename) ||
           !Pat.SectionPat.match(Sec->Name))

Modified: lld/trunk/test/ELF/linkerscript/filename-spec.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/filename-spec.s?rev=311713&r1=311712&r2=311713&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/filename-spec.s (original)
+++ lld/trunk/test/ELF/linkerscript/filename-spec.s Thu Aug 24 15:01:40 2017
@@ -33,24 +33,63 @@
 # RUN: ld.lld -o %t4 --script %t4.script %tfirst.o %tsecond.o
 # RUN: llvm-objdump -s %t4 | FileCheck --check-prefix=SECONDFIRST %s
 
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %T/filename-spec1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o filename-spec1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
-# RUN:   %p/Inputs/filename-spec.s -o %T/filename-spec2.o
+# RUN:   %p/Inputs/filename-spec.s -o filename-spec2.o
 
 # RUN: echo "SECTIONS { .foo : { \
 # RUN:   filename-spec2.o(.foo) \
 # RUN:   filename-spec1.o(.foo) } }" > %t5.script
 # RUN: ld.lld -o %t5 --script %t5.script \
-# RUN:   %T/filename-spec1.o %T/filename-spec2.o
+# RUN:   filename-spec1.o filename-spec2.o
 # RUN: llvm-objdump -s %t5 | FileCheck --check-prefix=SECONDFIRST %s
 
 # RUN: echo "SECTIONS { .foo : { \
 # RUN:   filename-spec1.o(.foo) \
 # RUN:   filename-spec2.o(.foo) } }" > %t6.script
 # RUN: ld.lld -o %t6 --script %t6.script \
-# RUN:   %T/filename-spec1.o %T/filename-spec2.o
+# RUN:   filename-spec1.o filename-spec2.o
 # RUN: llvm-objdump -s %t6 | FileCheck --check-prefix=FIRSTSECOND %s
 
+# RUN: mkdir -p %t.testdir1 %t.testdir2
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.testdir1/filename-spec1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
+# RUN:   %p/Inputs/filename-spec.s -o %t.testdir2/filename-spec2.o
+# RUN: llvm-ar rsc %t.testdir1/lib1.a %t.testdir1/filename-spec1.o
+# RUN: llvm-ar rsc %t.testdir2/lib2.a %t.testdir2/filename-spec2.o
+
+# Verify matching of archive library names.
+# RUN: echo "SECTIONS { .foo : { \
+# RUN:   *lib2*(.foo) \
+# RUN:   *lib1*(.foo) } }" > %t7.script
+# RUN: ld.lld -o %t7 --script %t7.script --whole-archive \
+# RUN:   %t.testdir1/lib1.a %t.testdir2/lib2.a
+# RUN: llvm-objdump -s %t7 | FileCheck --check-prefix=SECONDFIRST %s
+
+# Verify matching directories.
+# RUN: echo "SECTIONS { .foo : { \
+# RUN:   *testdir2*(.foo) \
+# RUN:   *testdir1*(.foo) } }" > %t8.script
+# RUN: ld.lld -o %t8 --script %t8.script --whole-archive \
+# RUN:   %t.testdir1/lib1.a %t.testdir2/lib2.a
+# RUN: llvm-objdump -s %t8 | FileCheck --check-prefix=SECONDFIRST %s
+
+# Verify matching of archive library names in KEEP.
+# RUN: echo "SECTIONS { .foo : { \
+# RUN:   KEEP(*lib2*(.foo)) \
+# RUN:   KEEP(*lib1*(.foo)) } }" > %t9.script
+# RUN: ld.lld -o %t9 --script %t9.script --whole-archive \
+# RUN:   %t.testdir1/lib1.a %t.testdir2/lib2.a
+# RUN: llvm-objdump -s %t9 | FileCheck --check-prefix=SECONDFIRST %s
+
+# Verify matching directories in KEEP.
+# RUN: echo "SECTIONS { .foo : { \
+# RUN:   KEEP(*testdir2*(.foo)) \
+# RUN:   KEEP(*testdir1*(.foo)) } }" > %t10.script
+# RUN: ld.lld -o %t10 --script %t10.script --whole-archive \
+# RUN:   %t.testdir1/lib1.a %t.testdir2/lib2.a
+# RUN: llvm-objdump -s %t10 | FileCheck --check-prefix=SECONDFIRST %s
+
 .global _start
 _start:
  nop




More information about the llvm-commits mailing list