[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

llvm-svn: 281646
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index e81c685..7ae61e9 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -110,6 +110,10 @@
          !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 std::function<bool(InputSectionData *, InputSectionData *)>
 getComparator(SortKind K) {
+  if (K == SortByPriority)
+    return comparePriority;
   if (K == SortByName)
     return compareName;
   return compareAlignment;
@@ -967,6 +973,8 @@
     return SortByName;
   if (skip("SORT_BY_ALIGNMENT"))
     return SortByAlignment;
+  if (skip("SORT_BY_INIT_PRIORITY"))
+    return SortByPriority;
   return SortNone;
 }