[PATCH] D55324: [LLD][ELF] - Linker script: accept using a file name without a list of sections.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 5 07:08:14 PST 2018


grimar created this revision.
grimar added a reviewer: ruiu.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.

This is a part of
https://bugs.llvm.org/show_bug.cgi?id=39885

Linker script specification says:
"You can specify a file name to include sections from a particular file. You would do this
if one or more of your files contain special data that needs to be at a particular location in memory."
(https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html#INPUT-SECTION)

LLD did not accept this yet. The patch implements it.


https://reviews.llvm.org/D55324

Files:
  ELF/LinkerScript.h
  ELF/ScriptParser.cpp
  test/ELF/linkerscript/no-filename-spec.s


Index: test/ELF/linkerscript/no-filename-spec.s
===================================================================
--- test/ELF/linkerscript/no-filename-spec.s
+++ test/ELF/linkerscript/no-filename-spec.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: echo '.section .bar, "a"; .quad 1;' | \
+# RUN:   llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %tfile1.o
+# RUN: echo '.section .zed, "a"; .quad 2;' | \
+# RUN:   llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %tfile2.o
+
+## We have a file name and no input sections description. In that case, all
+## sections from the file specified should be included. Check that.
+# RUN: ld.lld -o %t --script %s %tfile1.o %tfile2.o
+# RUN: llvm-objdump -s %t | FileCheck %s
+
+# CHECK:      Contents of section .foo:
+# CHECK-NEXT:  01000000 00000000 02000000 00000000
+
+SECTIONS {
+ .foo : { *file1.o *file2.o }
+}
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -854,7 +854,12 @@
     } else if (peek() == "(") {
       Cmd->SectionCommands.push_back(readInputSectionDescription(Tok));
     } else {
-      setError("unknown command " + Tok);
+      // We have a file name and no input sections description. It is not a
+      // commonly used syntax, but still acceptable. In that case, all sections
+      // from the file will be included.
+      InputSectionDescription *ISD = make<InputSectionDescription>(Tok);
+      ISD->SectionPatterns.push_back({{}, StringMatcher({"*"})});
+      Cmd->SectionCommands.push_back(ISD);
     }
   }
 
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -146,7 +146,9 @@
 // Also it may be surrounded with SORT() command, so contains sorting rules.
 struct SectionPattern {
   SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
-      : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
+      : ExcludedFilePat(Pat1), SectionPat(Pat2),
+        SortOuter(SortSectionPolicy::Default),
+        SortInner(SortSectionPolicy::Default) {}
 
   StringMatcher ExcludedFilePat;
   StringMatcher SectionPat;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55324.176814.patch
Type: text/x-patch
Size: 2183 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181205/289de915/attachment.bin>


More information about the llvm-commits mailing list