Skip to content

Commit 488a187

Browse files
committed
[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.
1 parent 909ea28 commit 488a187

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ class Symbol {
567567
orc::ExecutorAddrDiff getOffset() const { return Offset; }
568568

569569
void setOffset(orc::ExecutorAddrDiff NewOffset) {
570-
assert(NewOffset < getBlock().getSize() && "Offset out of range");
570+
assert(NewOffset <= getBlock().getSize() && "Offset out of range");
571571
Offset = NewOffset;
572572
}
573573

llvm/lib/ExecutionEngine/JITLink/DefineExternalSectionStartAndEndSymbols.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ createDefineExternalSectionStartAndEndSymbolsPass(
108108
std::forward<SymbolIdentifierFunction>(F));
109109
}
110110

111+
/// ELF section start/end symbol detection.
112+
inline SectionRangeSymbolDesc
113+
identifyELFSectionStartAndEndSymbols(LinkGraph &G, Symbol &Sym) {
114+
constexpr StringRef StartSymbolPrefix = "__start_";
115+
constexpr StringRef EndSymbolPrefix = "__stop_";
116+
117+
auto SymName = Sym.getName();
118+
if (SymName.starts_with(StartSymbolPrefix)) {
119+
if (auto *Sec =
120+
G.findSectionByName(SymName.drop_front(StartSymbolPrefix.size())))
121+
return {*Sec, true};
122+
} else if (SymName.starts_with(EndSymbolPrefix)) {
123+
if (auto *Sec =
124+
G.findSectionByName(SymName.drop_front(EndSymbolPrefix.size())))
125+
return {*Sec, false};
126+
}
127+
return {};
128+
}
129+
111130
} // end namespace jitlink
112131
} // end namespace llvm
113132

llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "llvm/Object/ELFObjectFile.h"
2121
#include "llvm/Support/Endian.h"
2222

23+
#include "DefineExternalSectionStartAndEndSymbols.h"
24+
2325
#define DEBUG_TYPE "jitlink"
2426

2527
using namespace llvm;
@@ -611,6 +613,11 @@ void link_ELF_aarch64(std::unique_ptr<LinkGraph> G,
611613
else
612614
Config.PrePrunePasses.push_back(markAllSymbolsLive);
613615

616+
// Resolve any external section start / end symbols.
617+
Config.PostAllocationPasses.push_back(
618+
createDefineExternalSectionStartAndEndSymbolsPass(
619+
identifyELFSectionStartAndEndSymbols));
620+
614621
// Add an in-place GOT/TLS/Stubs build pass.
615622
Config.PostPrunePasses.push_back(buildTables_ELF_aarch64);
616623
}

llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -343,24 +343,6 @@ createLinkGraphFromELFObject_x86_64(MemoryBufferRef ObjectBuffer) {
343343
.buildGraph();
344344
}
345345

346-
static SectionRangeSymbolDesc
347-
identifyELFSectionStartAndEndSymbols(LinkGraph &G, Symbol &Sym) {
348-
constexpr StringRef StartSymbolPrefix = "__start";
349-
constexpr StringRef EndSymbolPrefix = "__end";
350-
351-
auto SymName = Sym.getName();
352-
if (SymName.starts_with(StartSymbolPrefix)) {
353-
if (auto *Sec =
354-
G.findSectionByName(SymName.drop_front(StartSymbolPrefix.size())))
355-
return {*Sec, true};
356-
} else if (SymName.starts_with(EndSymbolPrefix)) {
357-
if (auto *Sec =
358-
G.findSectionByName(SymName.drop_front(EndSymbolPrefix.size())))
359-
return {*Sec, false};
360-
}
361-
return {};
362-
}
363-
364346
void link_ELF_x86_64(std::unique_ptr<LinkGraph> G,
365347
std::unique_ptr<JITLinkContext> Ctx) {
366348
PassConfiguration Config;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# RUN: llvm-mc -triple=aarch64-unknown-linux-gnu -position-independent \
2+
# RUN: -filetype=obj -o %t.o %s
3+
# RUN: llvm-jitlink -noexec -check %s %t.o
4+
5+
.text
6+
.file "elf_section_start_stop.c"
7+
.globl main
8+
.p2align 2
9+
.type main,@function
10+
main:
11+
adrp x8, z
12+
adrp x9, y
13+
ldr w8, [x8, :lo12:z]
14+
ldr w9, [x9, :lo12:y]
15+
sub w0, w8, w9
16+
ret
17+
.Lfunc_end0:
18+
.size main, .Lfunc_end0-main
19+
20+
.type x,@object
21+
.section custom_section,"aw",@progbits
22+
.globl x
23+
.p2align 2
24+
x:
25+
.word 42
26+
.size x, 4
27+
28+
# jitlink-check: *{8}z = (*{8}y) + 4
29+
30+
.type y,@object
31+
.data
32+
.globl y
33+
.p2align 3, 0x0
34+
y:
35+
.xword __start_custom_section
36+
.size y, 8
37+
38+
.type z,@object
39+
.globl z
40+
.p2align 3, 0x0
41+
z:
42+
.xword __stop_custom_section
43+
.size z, 8

0 commit comments

Comments
 (0)