Skip to content

Commit

Permalink
feat(svm): initial sbfv2 work (#496)
Browse files Browse the repository at this point in the history
* svm: initial sbfv2 work

* svm: format for the style guide

* address some comments

* svm.elf: address some review comments

* svm: verify the length of the `.text` section

* svm: don't check for internal functions if an external one was already found

* svm: clarify and fixup some behaviour arround the frame pointer

* svm: correct `safeSlice`
  • Loading branch information
Rexicon226 authored Feb 3, 2025
1 parent 3e0043e commit 6ea81ac
Show file tree
Hide file tree
Showing 32 changed files with 970 additions and 333 deletions.
2 changes: 2 additions & 0 deletions data/test-elfs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.bc
*.o
Binary file added data/test-elfs/bss_section.so
Binary file not shown.
7 changes: 7 additions & 0 deletions data/test-elfs/bss_section.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// NOTE: the value is 0 in order to get the symbol into .bss
var x: u32 = 0;

export fn entrypoint() u32 {
@as(*volatile u32, &x).* = 10;
return 0;
}
Binary file added data/test-elfs/data_section.so
Binary file not shown.
6 changes: 6 additions & 0 deletions data/test-elfs/data_section.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var x: u32 = 42;

export fn entrypoint() u32 {
@as(*volatile u32, &x).* = 10;
return 0;
}
26 changes: 26 additions & 0 deletions data/test-elfs/elf.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PHDRS
{
text PT_LOAD ;
rodata PT_LOAD ;
data PT_LOAD ;
dynamic PT_DYNAMIC ;
}
ENTRY ( entrypoint )
SECTIONS
{
. = SIZEOF_HEADERS;
.text : { *(.text*) } :text
.rodata : { *(.rodata*) } :rodata
.data.rel.ro : { *(.data.rel.ro*) } :rodata
.dynamic : { *(.dynamic) } :dynamic
.dynsym : { *(.dynsym) } :data
.dynstr : { *(.dynstr) } :data
.rel.dyn : { *(.rel.dyn) } :data
.data : { *(.data*) } :data
.bss : { *(.bss*) } :data
/DISCARD/ : {
*(.eh_frame*)
*(.gnu.hash*)
*(.hash*)
}
}
Binary file added data/test-elfs/reloc_64_64.so
Binary file not shown.
3 changes: 3 additions & 0 deletions data/test-elfs/reloc_64_64.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export fn entrypoint() u64 {
return @intFromPtr(&entrypoint);
}
Binary file modified data/test-elfs/reloc_64_64_sbpfv1.so
Binary file not shown.
Binary file added data/test-elfs/reloc_64_relative.so
Binary file not shown.
3 changes: 3 additions & 0 deletions data/test-elfs/reloc_64_relative.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export fn entrypoint() u64 {
return @intFromPtr("entrypoint");
}
Binary file added data/test-elfs/reloc_64_relative_data.so
Binary file not shown.
5 changes: 5 additions & 0 deletions data/test-elfs/reloc_64_relative_data.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const DATA = "hello";

export fn entrypoint() u64 {
return @intFromPtr(&DATA);
}
Binary file modified data/test-elfs/reloc_64_relative_data_sbpfv1.so
Binary file not shown.
Binary file modified data/test-elfs/reloc_64_relative_sbpfv1.so
Binary file not shown.
Binary file added data/test-elfs/rodata_section.so
Binary file not shown.
7 changes: 7 additions & 0 deletions data/test-elfs/rodata_section.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const VAL_A: u64 = 41;
const VAL_B: u64 = 42;
const VAL_C: u64 = 43;

export fn entrypoint() u64 {
return @as(*const volatile u64, &VAL_B).*;
}
Binary file modified data/test-elfs/rodata_section_sbpfv1.so
Binary file not shown.
Binary file removed data/test-elfs/static_internal_call_sbpfv1.so
Binary file not shown.
Binary file added data/test-elfs/struct_func_pointer.so
Binary file not shown.
17 changes: 17 additions & 0 deletions data/test-elfs/struct_func_pointer.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const PubkeyLutEntry = extern struct {
fp: *const fn (u8) callconv(.C) u8,
key: u64,
};

fn f1(a: u8) callconv(.C) u8 {
return a + 1;
}

export const entry: PubkeyLutEntry linksection(".data.rel.ro") = .{
.fp = f1,
.key = 0x0102030405060708,
};

export fn entrypoint() u64 {
return entry.key;
}
Binary file modified data/test-elfs/syscall_reloc_64_32.so
Binary file not shown.
6 changes: 6 additions & 0 deletions data/test-elfs/syscall_reloc_64_32.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern fn log(msg: [*]const u8, len: u64) void;

export fn entrypoint() u64 {
log("foo\n", 4);
return 0;
}
Binary file added data/test-elfs/syscall_static.so
Binary file not shown.
43 changes: 43 additions & 0 deletions scripts/gen_svm_elfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

CC="../toolchain/llvm/bin/clang"
LD="../toolchain/llvm/bin/ld.lld"
ZIG="zig"


LD_FLAGS="-z notext -shared --Bdynamic --script data/test-elfs/elf.ld"
C_BASE_FLAGS="-target sbf-solana-solana \
-fno-builtin \
-fPIC -fno-unwind-tables \
-fomit-frame-pointer -fno-exceptions \
-fno-asynchronous-unwind-tables \
-std=c23 \
-O2 \
-Werror \
-Wno-override-module"
C_FLAGS="${C_BASE_FLAGS} -mcpu=v1"
C_FLAGS_V2="${C_BASE_FLAGS} -mcpu=sbfv2"

CC_V1="${CC} ${C_FLAGS}"
CC_V2="${CC} ${C_FLAGS_V2}"

LD_V1="${LD} ${LD_FLAGS}"
LD_V2="${LD_V1} --section-start=.text=0x100000000"

V1_FILES=(reloc_64_64 reloc_64_relative reloc_64_relative_data rodata_section)

for ZIG_FILE in data/test-elfs/*.zig; do
BASE_NAME=$(basename "$ZIG_FILE" .zig)

$ZIG build-obj "$ZIG_FILE" -OReleaseSmall -fstrip -fno-emit-bin -femit-llvm-bc="data/test-elfs/${BASE_NAME}.bc"
$CC_V2 "data/test-elfs/${BASE_NAME}.bc" -c -o "data/test-elfs/${BASE_NAME}.o"
$LD_V2 "data/test-elfs/${BASE_NAME}.o" -o "data/test-elfs/${BASE_NAME}.so"

if [[ " ${V1_FILES[@]} " =~ " ${BASE_NAME} " ]]; then
$CC_V1 "data/test-elfs/${BASE_NAME}.bc" -c -o "data/test-elfs/${BASE_NAME}_sbpfv1.o"
$LD_V1 "data/test-elfs/${BASE_NAME}_sbpfv1.o" -o "data/test-elfs/${BASE_NAME}_sbpfv1.so"
fi
done

rm data/test-elfs/*.o
rm data/test-elfs/*.bc
Loading

0 comments on commit 6ea81ac

Please sign in to comment.