[lld] 447aa48 - [ELF] Add REVERSE input section description keyword
Justin Cady via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 7 09:45:39 PST 2023
Author: Justin Cady
Date: 2023-03-07T12:44:02-05:00
New Revision: 447aa48b4a02fa9e22fa45b2fb7a85c12df2e6c3
URL: https://github.com/llvm/llvm-project/commit/447aa48b4a02fa9e22fa45b2fb7a85c12df2e6c3
DIFF: https://github.com/llvm/llvm-project/commit/447aa48b4a02fa9e22fa45b2fb7a85c12df2e6c3.diff
LOG: [ELF] Add REVERSE input section description keyword
The `REVERSE` keyword is described here:
https://sourceware.org/bugzilla/show_bug.cgi?id=27565
It complements `SORT` by allowing the order of input sections to be reversed.
This is particularly useful for order-dependent sections such as .init_array,
where `REVERSE` can be used to either detect static initialization order fiasco
issues or as a mechanism to maintain .ctors element order while transitioning to
the modern .init_array. Such a transition is described here:
https://discourse.llvm.org/t/is-it-possible-to-manually-specify-init-array-order/68649
Differential Revision: https://reviews.llvm.org/D145381
Added:
Modified:
lld/ELF/Config.h
lld/ELF/LinkerScript.cpp
lld/ELF/ScriptParser.cpp
lld/test/ELF/linkerscript/sort-init.s
Removed:
################################################################################
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 61faea4e9bce..249fe3c30ece 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -73,7 +73,14 @@ enum class UnresolvedPolicy { ReportError, Warn, Ignore };
enum class OrphanHandlingPolicy { Place, Warn, Error };
// For --sort-section and linkerscript sorting rules.
-enum class SortSectionPolicy { Default, None, Alignment, Name, Priority };
+enum class SortSectionPolicy {
+ Default,
+ None,
+ Alignment,
+ Name,
+ Priority,
+ Reverse,
+};
// For --target2
enum class Target2Policy { Abs, Rel, GotRel };
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 668a79b5edd7..9207751cb7d2 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -457,6 +457,8 @@ static void sortSections(MutableArrayRef<InputSectionBase *> vec,
return llvm::stable_sort(vec, nameComparator);
case SortSectionPolicy::Priority:
return llvm::stable_sort(vec, priorityComparator);
+ case SortSectionPolicy::Reverse:
+ return std::reverse(vec.begin(), vec.end());
}
}
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index e83779bcebff..db7f0268cef6 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -660,6 +660,7 @@ StringMatcher ScriptParser::readFilePatterns() {
SortSectionPolicy ScriptParser::peekSortKind() {
return StringSwitch<SortSectionPolicy>(peek())
+ .Case("REVERSE", SortSectionPolicy::Reverse)
.Cases("SORT", "SORT_BY_NAME", SortSectionPolicy::Name)
.Case("SORT_BY_ALIGNMENT", SortSectionPolicy::Alignment)
.Case("SORT_BY_INIT_PRIORITY", SortSectionPolicy::Priority)
diff --git a/lld/test/ELF/linkerscript/sort-init.s b/lld/test/ELF/linkerscript/sort-init.s
index 0a349fd9e57f..e1f0c83e74e9 100644
--- a/lld/test/ELF/linkerscript/sort-init.s
+++ b/lld/test/ELF/linkerscript/sort-init.s
@@ -9,6 +9,14 @@
# CHECK: Hex dump of section '.init_array':
# CHECK-NEXT: 0x00000001 00010203 04050607
+## Test REVERSE can be used to reverse the order of .init_array and .ctors
+
+# RUN: ld.lld -T %t/reverse.lds %t.o -o %t2.out
+# RUN: llvm-readelf -x .init_array %t2.out | FileCheck %s --check-prefix=CHECK2
+
+# CHECK2: Hex dump of section '.init_array':
+# CHECK2-NEXT: 0x00000001 04030201 00050706
+
#--- asm
.globl _start
_start:
@@ -41,3 +49,11 @@ SECTIONS {
*(.init_array .ctors)
}
}
+
+#--- reverse.lds
+SECTIONS {
+ .init_array : {
+ *(REVERSE(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) SORT_BY_INIT_PRIORITY(foo*))
+ *(REVERSE(.init_array .ctors))
+ }
+}
More information about the llvm-commits
mailing list