<div dir="ltr">Are .[cd]tors still in use?</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 10, 2016 at 3:38 PM, Sean Silva <span dir="ltr"><<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Did you intend to add .[cd]tors support also? That was part of PR26538.<span class="HOEnZb"><font color="#888888"><div><br></div><div>-- Sean Silva</div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 10, 2016 at 3:20 PM, Rui Ueyama 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: ruiu<br>
Date: Wed Feb 10 17:20:42 2016<br>
New Revision: 260460<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=260460&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=260460&view=rev</a><br>
Log:<br>
ELF: Implement __attribute__((init_priority(N)) support.<br>
<br>
Added:<br>
    lld/trunk/test/ELF/init_fini_priority.s<br>
Modified:<br>
    lld/trunk/ELF/OutputSections.cpp<br>
    lld/trunk/ELF/OutputSections.h<br>
    lld/trunk/ELF/Writer.cpp<br>
<br>
Modified: lld/trunk/ELF/OutputSections.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=260460&r1=260459&r2=260460&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=260460&r1=260459&r2=260460&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/ELF/OutputSections.cpp (original)<br>
+++ lld/trunk/ELF/OutputSections.cpp Wed Feb 10 17:20:42 2016<br>
@@ -750,6 +750,44 @@ void OutputSection<ELFT>::addSection(Inp<br>
   this->Header.sh_size = Off;<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>
+// Sorts input sections by section name suffixes, so that .foo.N comes<br>
+// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.<br>
+// For more detail, read the section of the GCC's manual about init_priority.<br>
+template <class ELFT> void OutputSection<ELFT>::sortByPriority() {<br>
+  // Sort sections by priority.<br>
+  typedef std::pair<int, InputSection<ELFT> *> Pair;<br>
+  std::vector<Pair> V;<br>
+  for (InputSection<ELFT> *S : Sections)<br>
+    V.push_back({getPriority(S->getSectionName()), S});<br>
+  std::sort(V.begin(), V.end(),<br>
+            [](const Pair &A, const Pair &B) { return A.first < B.first; });<br>
+  Sections.clear();<br>
+  for (Pair &P : V)<br>
+    Sections.push_back(P.second);<br>
+<br>
+  // Reassign section addresses.<br>
+  uintX_t Off = 0;<br>
+  for (InputSection<ELFT> *S : Sections) {<br>
+    Off = alignTo(Off, S->getAlign());<br>
+    S->OutSecOff = Off;<br>
+    Off += S->getSize();<br>
+  }<br>
+  this->Header.sh_size = Off;<br>
+}<br>
+<br>
 // Returns a VA which a relocatin RI refers to. Used only for local symbols.<br>
 // For non-local symbols, use SymbolBody::getVA instead.<br>
 template <class ELFT, bool IsRela><br>
<br>
Modified: lld/trunk/ELF/OutputSections.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=260460&r1=260459&r2=260460&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=260460&r1=260459&r2=260460&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/ELF/OutputSections.h (original)<br>
+++ lld/trunk/ELF/OutputSections.h Wed Feb 10 17:20:42 2016<br>
@@ -279,6 +279,7 @@ public:<br>
   typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;<br>
   OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);<br>
   void addSection(InputSectionBase<ELFT> *C) override;<br>
+  void sortByPriority();<br>
   void writeTo(uint8_t *Buf) override;<br>
<br>
 private:<br>
<br>
Modified: lld/trunk/ELF/Writer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=260460&r1=260459&r2=260460&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=260460&r1=260459&r2=260460&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/ELF/Writer.cpp (original)<br>
+++ lld/trunk/ELF/Writer.cpp Wed Feb 10 17:20:42 2016<br>
@@ -735,6 +735,10 @@ StringRef Writer<ELFT>::getOutputSection<br>
     return ".data";<br>
   if (S.startswith(".bss."))<br>
     return ".bss";<br>
+  if (S.startswith(".init_array."))<br>
+    return ".init_array";<br>
+  if (S.startswith(".fini_array."))<br>
+    return ".fini_array";<br>
   return S;<br>
 }<br>
<br>
@@ -915,6 +919,13 @@ template <class ELFT> void Writer<ELFT>:<br>
       Symtab.addAbsolute("end", ElfSym<ELFT>::End);<br>
 }<br>
<br>
+// Sort input sections by section name suffixes for<br>
+// __attribute__((init_priority(N))).<br>
+template <class ELFT> static void sortByPriority(OutputSectionBase<ELFT> *S) {<br>
+  if (S)<br>
+    reinterpret_cast<OutputSection<ELFT> *>(S)->sortByPriority();<br>
+}<br>
+<br>
 // Create output section objects and add them to OutputSections.<br>
 template <class ELFT> bool Writer<ELFT>::createSections() {<br>
   OutputSections.push_back(Out<ELFT>::ElfHeader);<br>
@@ -962,6 +973,10 @@ template <class ELFT> bool Writer<ELFT>:<br>
   Out<ELFT>::Dynamic->FiniArraySec =<br>
       Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC);<br>
<br>
+  // Sort section contents for __attribute__((init_priority(N)).<br>
+  sortByPriority(Out<ELFT>::Dynamic->InitArraySec);<br>
+  sortByPriority(Out<ELFT>::Dynamic->FiniArraySec);<br>
+<br>
   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop<br>
   // symbols for sections, so that the runtime can get the start and end<br>
   // addresses of each section by section name. Add such symbols.<br>
<br>
Added: lld/trunk/test/ELF/init_fini_priority.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/init_fini_priority.s?rev=260460&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/init_fini_priority.s?rev=260460&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/ELF/init_fini_priority.s (added)<br>
+++ lld/trunk/test/ELF/init_fini_priority.s Wed Feb 10 17:20:42 2016<br>
@@ -0,0 +1,29 @@<br>
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t<br>
+// RUN: ld.lld %t -o %t.exe<br>
+// RUN: llvm-objdump -s %t.exe | FileCheck %s<br>
+// REQUIRES: x86<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>
+<br>
+.section .fini_array, "aw", @fini_array<br>
+  .align 8<br>
+  .byte 4<br>
+.section .fini_array.100, "aw", @fini_array<br>
+  .long 5<br>
+.section .fini_array.5, "aw", @fini_array<br>
+  .byte 6<br>
+<br>
+// CHECK:      Contents of section .init_array:<br>
+// CHECK-NEXT: 03020000 00000000 01<br>
+// CHECK:      Contents of section .fini_array:<br>
+// CHECK-NEXT: 06050000 00000000 04<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">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/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>