[llvm] 488a187 - [JITLink][ELF] Improve ELF section start/end symbol handling.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 24 12:54:22 PDT 2024
Author: Lang Hames
Date: 2024-03-24T14:54:15-05:00
New Revision: 488a18738f5c275093bcc5459da69e9b4c9de074
URL: https://github.com/llvm/llvm-project/commit/488a18738f5c275093bcc5459da69e9b4c9de074
DIFF: https://github.com/llvm/llvm-project/commit/488a18738f5c275093bcc5459da69e9b4c9de074.diff
LOG: [JITLink][ELF] Improve ELF section start/end symbol handling.
This commit adds section start and stop symbol handling to ELF/aarch64, and
fixes the section symbol prefixes (using `__start_` and `__stop_`, rather than
`__start` and `__end`). It also adds a testcase for handling of these symbols.
Added:
llvm/test/ExecutionEngine/JITLink/AArch64/ELF_section_start_and_stop_symbols.s
Modified:
llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
llvm/lib/ExecutionEngine/JITLink/DefineExternalSectionStartAndEndSymbols.h
llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
index 30a9383dc0bc15..4ce0c8d9fd17ed 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
@@ -567,7 +567,7 @@ class Symbol {
orc::ExecutorAddrDiff getOffset() const { return Offset; }
void setOffset(orc::ExecutorAddrDiff NewOffset) {
- assert(NewOffset < getBlock().getSize() && "Offset out of range");
+ assert(NewOffset <= getBlock().getSize() && "Offset out of range");
Offset = NewOffset;
}
diff --git a/llvm/lib/ExecutionEngine/JITLink/DefineExternalSectionStartAndEndSymbols.h b/llvm/lib/ExecutionEngine/JITLink/DefineExternalSectionStartAndEndSymbols.h
index 159880e4b15294..aef441d093a2d3 100644
--- a/llvm/lib/ExecutionEngine/JITLink/DefineExternalSectionStartAndEndSymbols.h
+++ b/llvm/lib/ExecutionEngine/JITLink/DefineExternalSectionStartAndEndSymbols.h
@@ -108,6 +108,25 @@ createDefineExternalSectionStartAndEndSymbolsPass(
std::forward<SymbolIdentifierFunction>(F));
}
+/// ELF section start/end symbol detection.
+inline SectionRangeSymbolDesc
+identifyELFSectionStartAndEndSymbols(LinkGraph &G, Symbol &Sym) {
+ constexpr StringRef StartSymbolPrefix = "__start_";
+ constexpr StringRef EndSymbolPrefix = "__stop_";
+
+ auto SymName = Sym.getName();
+ if (SymName.starts_with(StartSymbolPrefix)) {
+ if (auto *Sec =
+ G.findSectionByName(SymName.drop_front(StartSymbolPrefix.size())))
+ return {*Sec, true};
+ } else if (SymName.starts_with(EndSymbolPrefix)) {
+ if (auto *Sec =
+ G.findSectionByName(SymName.drop_front(EndSymbolPrefix.size())))
+ return {*Sec, false};
+ }
+ return {};
+}
+
} // end namespace jitlink
} // end namespace llvm
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
index f17b2c626ac250..4f64d83162b2c2 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
@@ -20,6 +20,8 @@
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/Endian.h"
+#include "DefineExternalSectionStartAndEndSymbols.h"
+
#define DEBUG_TYPE "jitlink"
using namespace llvm;
@@ -611,6 +613,11 @@ void link_ELF_aarch64(std::unique_ptr<LinkGraph> G,
else
Config.PrePrunePasses.push_back(markAllSymbolsLive);
+ // Resolve any external section start / end symbols.
+ Config.PostAllocationPasses.push_back(
+ createDefineExternalSectionStartAndEndSymbolsPass(
+ identifyELFSectionStartAndEndSymbols));
+
// Add an in-place GOT/TLS/Stubs build pass.
Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
}
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
index a1fe9c5fcd733a..52dd83d9040fe8 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
@@ -343,24 +343,6 @@ createLinkGraphFromELFObject_x86_64(MemoryBufferRef ObjectBuffer) {
.buildGraph();
}
-static SectionRangeSymbolDesc
-identifyELFSectionStartAndEndSymbols(LinkGraph &G, Symbol &Sym) {
- constexpr StringRef StartSymbolPrefix = "__start";
- constexpr StringRef EndSymbolPrefix = "__end";
-
- auto SymName = Sym.getName();
- if (SymName.starts_with(StartSymbolPrefix)) {
- if (auto *Sec =
- G.findSectionByName(SymName.drop_front(StartSymbolPrefix.size())))
- return {*Sec, true};
- } else if (SymName.starts_with(EndSymbolPrefix)) {
- if (auto *Sec =
- G.findSectionByName(SymName.drop_front(EndSymbolPrefix.size())))
- return {*Sec, false};
- }
- return {};
-}
-
void link_ELF_x86_64(std::unique_ptr<LinkGraph> G,
std::unique_ptr<JITLinkContext> Ctx) {
PassConfiguration Config;
diff --git a/llvm/test/ExecutionEngine/JITLink/AArch64/ELF_section_start_and_stop_symbols.s b/llvm/test/ExecutionEngine/JITLink/AArch64/ELF_section_start_and_stop_symbols.s
new file mode 100644
index 00000000000000..f8e7ba96f00684
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/AArch64/ELF_section_start_and_stop_symbols.s
@@ -0,0 +1,43 @@
+# RUN: llvm-mc -triple=aarch64-unknown-linux-gnu -position-independent \
+# RUN: -filetype=obj -o %t.o %s
+# RUN: llvm-jitlink -noexec -check %s %t.o
+
+ .text
+ .file "elf_section_start_stop.c"
+ .globl main
+ .p2align 2
+ .type main, at function
+main:
+ adrp x8, z
+ adrp x9, y
+ ldr w8, [x8, :lo12:z]
+ ldr w9, [x9, :lo12:y]
+ sub w0, w8, w9
+ ret
+.Lfunc_end0:
+ .size main, .Lfunc_end0-main
+
+ .type x, at object
+ .section custom_section,"aw", at progbits
+ .globl x
+ .p2align 2
+x:
+ .word 42
+ .size x, 4
+
+# jitlink-check: *{8}z = (*{8}y) + 4
+
+ .type y, at object
+ .data
+ .globl y
+ .p2align 3, 0x0
+y:
+ .xword __start_custom_section
+ .size y, 8
+
+ .type z, at object
+ .globl z
+ .p2align 3, 0x0
+z:
+ .xword __stop_custom_section
+ .size z, 8
More information about the llvm-commits
mailing list