Skip to content

Commit 38b7045

Browse files
committed
short: synthesize PLATFORM_DATA in ELF (not stub)
1 parent 21dbb4d commit 38b7045

File tree

5 files changed

+11
-23
lines changed

5 files changed

+11
-23
lines changed

scripts/static-pie-gen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@
6868
sol = "".join(sol)
6969

7070
# binary (raw)
71-
code_raw_b91 = base91.encode(memory_bin, use_rle=True).decode('ascii')
71+
code_raw = memory_bin[:-8]
72+
code_raw += (len(code_raw) + 8 - loader_fdict['entrypoint_offset']).to_bytes(8, byteorder='little')
73+
code_raw_b91 = base91.encode(code_raw, use_rle=True).decode('ascii')
7274
code_raw_b91_len = len(code_raw_b91)
7375
code_raw_b91 = '"' + code_raw_b91 + '"'
7476

scripts/static-pie-prestub-amd64-shorter.asm

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ BITS 64
1111
ORG 0
1212
section .text
1313

14-
; Align stack to 16 byte boundary
15-
; [rsp+ 32, rsp+120): PLATFORM_DATA
16-
; [rsp+ 0, rsp+ 32): (shadow space for win64 calling convention)
17-
enter 56, 0
18-
1914
; svc_alloc_rwx for Linux
2015
_svc_alloc_rwx:
2116
push 9
@@ -34,15 +29,7 @@ _svc_alloc_rwx:
3429
syscall
3530
pop rsi ; restore rsi
3631

37-
; PLATFORM_DATA
38-
_t: ; PLATFORM_DATA[32..39] = ptr_alloc_rwx
39-
push rdx ; PLATFORM_DATA[24..31] = win_GetProcAddress
40-
push rax ; PLATFORM_DATA[16..23] = win_kernel32
41-
push 1 ; PLATFORM_DATA[ 8..15] = env_flags (0=None, 1=ENV_FLAGS_LINUX_STYLE_CHKSTK)
42-
push 2 ; PLATFORM_DATA[ 0.. 7] = env_id (1=Windows, 2=Linux)
43-
4432
; Current state: rax = new buffer
45-
push rax
4633
xchg rax, rdi ; rdi = new buffer
4734

4835
; Base91 decoder
@@ -78,8 +65,5 @@ _decode_zeros:
7865

7966
; Jump to entrypoint
8067
_jump_to_entrypoint:
81-
pop rax
82-
add rax, qword [rdi-8]
83-
push rsp
84-
pop rcx
85-
call rax
68+
sub rdi, qword [rdi-8]
69+
jmp rdi
-14 Bytes
Binary file not shown.

scripts/static-pie-template-amd64-shorter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
$$$$solution_src$$$$
66
}
77
// SOLUTION END
8-
#[no_link]extern crate std;#[no_mangle]unsafe fn _start(){std::arch::asm!(".quad 9958096a000038c8h,16aff3156c93145h,6a5a41226a07b25eh,50525e050f5841ffh,0b0974850026a016ah,99232cac0de0c11fh,0ac92c8fe16742572h,0aad0015bc06b242ch,0f77510c4f608e8c1h,48ff4fb60f92dfebh,58d5eb92aaf3cfffh,0d0ff5954f8470348h",in("rsi")r$$$$binary_raw_base91$$$$.as_ptr())}
8+
#[no_link]extern crate std;#[no_mangle]unsafe fn _start(){std::arch::asm!(".quad 56c931459958096ah,6a07b25e016aff31h,0f5841ff6a5a4122h,0e0c11fb097485e05h,74257299232cac0dh,6b242cac92c8fe16h,8e8c1aad0015bc0h,92dfebf77510c4f6h,0f3cfff48ff4fb60fh,0f87f2b48d5eb92aah,59391",in("rsi")r$$$$binary_raw_base91$$$$.as_ptr())}

src/bin/codegen.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ unsafe extern "win64" fn _start() -> ! {
8686
#[cfg(feature = "short")]
8787
asm!(
8888
"clc", // Not needed but packager wants it
89-
"push rcx", // Align stack
90-
"mov rbx, rcx", // Save PLATFORM_DATA table
89+
"sub rsp, 80", // 16 + 80 = 96 = 16*6 -> stack alignment preserved
90+
"push 1", // env_flags = 1 (ENV_FLAGS_LINUX_STYLE_CHKSTK)
91+
"push 2", // env_id = 2 (ENV_ID_LINUX)
9192
"lea rdi, [rip + __ehdr_start]",
9293
"lea rsi, [rip + _DYNAMIC]",
9394
"call {0}",
94-
"mov rdi, rbx",
95+
"push rsp",
96+
"pop rcx",
9597
"call {1}", // This won't return since on Linux we invoke SYS_exitgroup in binary
9698
sym loader::amd64_elf::relocate,
9799
sym _start_rust,

0 commit comments

Comments
 (0)