[lld] r281646 - [ELF] - Linkerscript: implemented SORT_BY_INIT_PRIORITY.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 12:15:12 PDT 2016


Author: grimar
Date: Thu Sep 15 14:15:12 2016
New Revision: 281646

URL: http://llvm.org/viewvc/llvm-project?rev=281646&view=rev
Log:
[ELF] - Linkerscript: implemented SORT_BY_INIT_PRIORITY.

This is PR30386,

SORT_BY_INIT_PRIORITY is a keyword can be used to sort sections by numerical value of the
GCC init_priority attribute encoded in the section name.

Differential revision: https://reviews.llvm.org/D24611

Added:
    lld/trunk/test/ELF/linkerscript/sort-init.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/Strings.cpp
    lld/trunk/ELF/Strings.h

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281646&r1=281645&r2=281646&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Sep 15 14:15:12 2016
@@ -110,6 +110,10 @@ static bool fileMatches(const InputSecti
          !const_cast<Regex &>(Desc->ExcludedFileRe).match(Filename);
 }
 
+static bool comparePriority(InputSectionData *A, InputSectionData *B) {
+  return getPriority(A->Name) < getPriority(B->Name);
+}
+
 static bool compareName(InputSectionData *A, InputSectionData *B) {
   return A->Name < B->Name;
 }
@@ -123,6 +127,8 @@ static bool compareAlignment(InputSectio
 
 static std::function<bool(InputSectionData *, InputSectionData *)>
 getComparator(SortKind K) {
+  if (K == SortByPriority)
+    return comparePriority;
   if (K == SortByName)
     return compareName;
   return compareAlignment;
@@ -967,6 +973,8 @@ SortKind ScriptParser::readSortKind() {
     return SortByName;
   if (skip("SORT_BY_ALIGNMENT"))
     return SortByAlignment;
+  if (skip("SORT_BY_INIT_PRIORITY"))
+    return SortByPriority;
   return SortNone;
 }
 

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=281646&r1=281645&r2=281646&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Sep 15 14:15:12 2016
@@ -95,7 +95,7 @@ struct OutputSectionCommand : BaseComman
   ConstraintKind Constraint = ConstraintKind::NoConstraint;
 };
 
-enum SortKind { SortNone, SortByName, SortByAlignment };
+enum SortKind { SortNone, SortByPriority, SortByName, SortByAlignment };
 
 struct InputSectionDescription : BaseCommand {
   InputSectionDescription(StringRef FilePattern)

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=281646&r1=281645&r2=281646&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Sep 15 14:15:12 2016
@@ -895,19 +895,6 @@ void OutputSection<ELFT>::addSection(Inp
   this->updateAlignment(S->Alignment);
 }
 
-// If an input string is in the form of "foo.N" where N is a number,
-// return N. Otherwise, returns 65536, which is one greater than the
-// lowest priority.
-static int getPriority(StringRef S) {
-  size_t Pos = S.rfind('.');
-  if (Pos == StringRef::npos)
-    return 65536;
-  int V;
-  if (S.substr(Pos + 1).getAsInteger(10, V))
-    return 65536;
-  return V;
-}
-
 // This function is called after we sort input sections
 // and scan relocations to setup sections' offsets.
 template <class ELFT> void OutputSection<ELFT>::assignOffsets() {

Modified: lld/trunk/ELF/Strings.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.cpp?rev=281646&r1=281645&r2=281646&view=diff
==============================================================================
--- lld/trunk/ELF/Strings.cpp (original)
+++ lld/trunk/ELF/Strings.cpp Thu Sep 15 14:15:12 2016
@@ -20,6 +20,19 @@ using namespace llvm;
 using namespace lld;
 using namespace lld::elf;
 
+// If an input string is in the form of "foo.N" where N is a number,
+// return N. Otherwise, returns 65536, which is one greater than the
+// lowest priority.
+int elf::getPriority(StringRef S) {
+  size_t Pos = S.rfind('.');
+  if (Pos == StringRef::npos)
+    return 65536;
+  int V;
+  if (S.substr(Pos + 1).getAsInteger(10, V))
+    return 65536;
+  return V;
+}
+
 bool elf::hasWildcard(StringRef S) {
   return S.find_first_of("?*[") != StringRef::npos;
 }

Modified: lld/trunk/ELF/Strings.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.h?rev=281646&r1=281645&r2=281646&view=diff
==============================================================================
--- lld/trunk/ELF/Strings.h (original)
+++ lld/trunk/ELF/Strings.h Thu Sep 15 14:15:12 2016
@@ -17,6 +17,7 @@
 namespace lld {
 namespace elf {
 llvm::Regex compileGlobPatterns(ArrayRef<StringRef> V);
+int getPriority(StringRef S);
 bool hasWildcard(StringRef S);
 std::vector<uint8_t> parseHex(StringRef S);
 bool isValidCIdentifier(StringRef S);

Added: lld/trunk/test/ELF/linkerscript/sort-init.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/sort-init.s?rev=281646&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/sort-init.s (added)
+++ lld/trunk/test/ELF/linkerscript/sort-init.s Thu Sep 15 14:15:12 2016
@@ -0,0 +1,24 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: echo "SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_array.*)) } }" > %t1.script
+# RUN: ld.lld --script %t1.script %t1.o -o %t2
+# RUN: llvm-objdump -s %t2 | FileCheck %s
+
+# CHECK:      Contents of section .init_array:
+# CHECK-NEXT: 03020000 00000000 010405
+
+.globl _start
+_start:
+  nop
+
+.section .init_array, "aw", @init_array
+  .align 8
+  .byte 1
+.section .init_array.100, "aw", @init_array
+  .long 2
+.section .init_array.5, "aw", @init_array
+  .byte 3
+.section .init_array, "aw", @init_array
+  .byte 4
+.section .init_array, "aw", @init_array
+  .byte 5




More information about the llvm-commits mailing list