<div dir="ltr">Thanks!</div><div class="gmail_extra"><br><div class="gmail_quote">On 15 September 2016 at 15:15, George Rimar via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: grimar<br>
Date: Thu Sep 15 14:15:12 2016<br>
New Revision: 281646<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=281646&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=281646&view=rev</a><br>
Log:<br>
[ELF] - Linkerscript: implemented SORT_BY_INIT_PRIORITY.<br>
<br>
This is PR30386,<br>
<br>
SORT_BY_INIT_PRIORITY is a keyword can be used to sort sections by numerical value of the<br>
GCC init_priority attribute encoded in the section name.<br>
<br>
Differential revision: <a href="https://reviews.llvm.org/D24611" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D24611</a><br>
<br>
Added:<br>
    lld/trunk/test/ELF/<wbr>linkerscript/sort-init.s<br>
Modified:<br>
    lld/trunk/ELF/LinkerScript.cpp<br>
    lld/trunk/ELF/LinkerScript.h<br>
    lld/trunk/ELF/OutputSections.<wbr>cpp<br>
    lld/trunk/ELF/Strings.cpp<br>
    lld/trunk/ELF/Strings.h<br>
<br>
Modified: lld/trunk/ELF/LinkerScript.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281646&r1=281645&r2=281646&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/<wbr>LinkerScript.cpp?rev=281646&<wbr>r1=281645&r2=281646&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/LinkerScript.cpp (original)<br>
+++ lld/trunk/ELF/LinkerScript.cpp Thu Sep 15 14:15:12 2016<br>
@@ -110,6 +110,10 @@ static bool fileMatches(const InputSecti<br>
          !const_cast<Regex &>(Desc->ExcludedFileRe).<wbr>match(Filename);<br>
 }<br>
<br>
+static bool comparePriority(<wbr>InputSectionData *A, InputSectionData *B) {<br>
+  return getPriority(A->Name) < getPriority(B->Name);<br>
+}<br>
+<br>
 static bool compareName(InputSectionData *A, InputSectionData *B) {<br>
   return A->Name < B->Name;<br>
 }<br>
@@ -123,6 +127,8 @@ static bool compareAlignment(InputSectio<br>
<br>
 static std::function<bool(<wbr>InputSectionData *, InputSectionData *)><br>
 getComparator(SortKind K) {<br>
+  if (K == SortByPriority)<br>
+    return comparePriority;<br>
   if (K == SortByName)<br>
     return compareName;<br>
   return compareAlignment;<br>
@@ -967,6 +973,8 @@ SortKind ScriptParser::readSortKind() {<br>
     return SortByName;<br>
   if (skip("SORT_BY_ALIGNMENT"))<br>
     return SortByAlignment;<br>
+  if (skip("SORT_BY_INIT_PRIORITY")<wbr>)<br>
+    return SortByPriority;<br>
   return SortNone;<br>
 }<br>
<br>
<br>
Modified: lld/trunk/ELF/LinkerScript.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=281646&r1=281645&r2=281646&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/<wbr>LinkerScript.h?rev=281646&r1=<wbr>281645&r2=281646&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/LinkerScript.h (original)<br>
+++ lld/trunk/ELF/LinkerScript.h Thu Sep 15 14:15:12 2016<br>
@@ -95,7 +95,7 @@ struct OutputSectionCommand : BaseComman<br>
   ConstraintKind Constraint = ConstraintKind::NoConstraint;<br>
 };<br>
<br>
-enum SortKind { SortNone, SortByName, SortByAlignment };<br>
+enum SortKind { SortNone, SortByPriority, SortByName, SortByAlignment };<br>
<br>
 struct InputSectionDescription : BaseCommand {<br>
   InputSectionDescription(<wbr>StringRef FilePattern)<br>
<br>
Modified: lld/trunk/ELF/OutputSections.<wbr>cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=281646&r1=281645&r2=281646&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/<wbr>OutputSections.cpp?rev=281646&<wbr>r1=281645&r2=281646&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/OutputSections.<wbr>cpp (original)<br>
+++ lld/trunk/ELF/OutputSections.<wbr>cpp Thu Sep 15 14:15:12 2016<br>
@@ -895,19 +895,6 @@ void OutputSection<ELFT>::<wbr>addSection(Inp<br>
   this->updateAlignment(S-><wbr>Alignment);<br>
 }<br>
<br>
-// If an input string is in the form of "foo.N" where N is a number,<br>
-// return N. Otherwise, returns 65536, which is one greater than the<br>
-// lowest priority.<br>
-static int getPriority(StringRef S) {<br>
-  size_t Pos = S.rfind('.');<br>
-  if (Pos == StringRef::npos)<br>
-    return 65536;<br>
-  int V;<br>
-  if (S.substr(Pos + 1).getAsInteger(10, V))<br>
-    return 65536;<br>
-  return V;<br>
-}<br>
-<br>
 // This function is called after we sort input sections<br>
 // and scan relocations to setup sections' offsets.<br>
 template <class ELFT> void OutputSection<ELFT>::<wbr>assignOffsets() {<br>
<br>
Modified: lld/trunk/ELF/Strings.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.cpp?rev=281646&r1=281645&r2=281646&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Strings.<wbr>cpp?rev=281646&r1=281645&r2=<wbr>281646&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/Strings.cpp (original)<br>
+++ lld/trunk/ELF/Strings.cpp Thu Sep 15 14:15:12 2016<br>
@@ -20,6 +20,19 @@ using namespace llvm;<br>
 using namespace lld;<br>
 using namespace lld::elf;<br>
<br>
+// If an input string is in the form of "foo.N" where N is a number,<br>
+// return N. Otherwise, returns 65536, which is one greater than the<br>
+// lowest priority.<br>
+int elf::getPriority(StringRef S) {<br>
+  size_t Pos = S.rfind('.');<br>
+  if (Pos == StringRef::npos)<br>
+    return 65536;<br>
+  int V;<br>
+  if (S.substr(Pos + 1).getAsInteger(10, V))<br>
+    return 65536;<br>
+  return V;<br>
+}<br>
+<br>
 bool elf::hasWildcard(StringRef S) {<br>
   return S.find_first_of("?*[") != StringRef::npos;<br>
 }<br>
<br>
Modified: lld/trunk/ELF/Strings.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.h?rev=281646&r1=281645&r2=281646&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Strings.<wbr>h?rev=281646&r1=281645&r2=<wbr>281646&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/Strings.h (original)<br>
+++ lld/trunk/ELF/Strings.h Thu Sep 15 14:15:12 2016<br>
@@ -17,6 +17,7 @@<br>
 namespace lld {<br>
 namespace elf {<br>
 llvm::Regex compileGlobPatterns(ArrayRef<<wbr>StringRef> V);<br>
+int getPriority(StringRef S);<br>
 bool hasWildcard(StringRef S);<br>
 std::vector<uint8_t> parseHex(StringRef S);<br>
 bool isValidCIdentifier(StringRef S);<br>
<br>
Added: lld/trunk/test/ELF/<wbr>linkerscript/sort-init.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/sort-init.s?rev=281646&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/test/ELF/<wbr>linkerscript/sort-init.s?rev=<wbr>281646&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/test/ELF/<wbr>linkerscript/sort-init.s (added)<br>
+++ lld/trunk/test/ELF/<wbr>linkerscript/sort-init.s Thu Sep 15 14:15:12 2016<br>
@@ -0,0 +1,24 @@<br>
+# REQUIRES: x86<br>
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o<br>
+# RUN: echo "SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_<wbr>array.*)) } }" > %t1.script<br>
+# RUN: ld.lld --script %t1.script %t1.o -o %t2<br>
+# RUN: llvm-objdump -s %t2 | FileCheck %s<br>
+<br>
+# CHECK:      Contents of section .init_array:<br>
+# CHECK-NEXT: 03020000 00000000 010405<br>
+<br>
+.globl _start<br>
+_start:<br>
+  nop<br>
+<br>
+.section .init_array, "aw", @init_array<br>
+  .align 8<br>
+  .byte 1<br>
+.section .init_array.100, "aw", @init_array<br>
+  .long 2<br>
+.section .init_array.5, "aw", @init_array<br>
+  .byte 3<br>
+.section .init_array, "aw", @init_array<br>
+  .byte 4<br>
+.section .init_array, "aw", @init_array<br>
+  .byte 5<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>