Created
March 24, 2023 02:55
-
-
Save skeeto/e7ff8823d8801a5b483c109fc84b6ab5 to your computer and use it in GitHub Desktop.
An assembly-free entrypoint?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if __amd64 | |
#define SYS_write 1 | |
#define SYS_exit 60 | |
__attribute__((force_align_arg_pointer)) void _start(void); | |
static long syscall3(long n, long a, long b, long c) | |
{ | |
long r; | |
__asm volatile ( | |
"syscall" | |
: "=a"(r) | |
: "a"(n), "D"(a), "S"(b), "d"(c) | |
: "rcx", "r11", "memory" | |
); | |
return r; | |
} | |
#endif | |
#if __aarch64__ | |
#define SYS_write 64 | |
#define SYS_exit 93 | |
static long syscall3(long n, long a, long b, long c) | |
{ | |
register long x0 __asm("x0") = a; | |
register long x1 __asm("x1") = b; | |
register long x2 __asm("x2") = c; | |
register long x8 __asm("x8") = n; | |
__asm volatile ( | |
"svc #0" | |
: "=r"(x0) | |
: "r"(x8), "r"(x0), "r"(x1), "r"(x2) | |
: "memory" | |
); | |
return x0; | |
} | |
#endif | |
void _start(void) | |
{ | |
long *argc_ptr = ((long *)__builtin_frame_address(0)) + 1; | |
long argc = *argc_ptr; | |
char **argv = (char **)(argc_ptr + 1); | |
char **envp = (char **)(argc_ptr + 1 + argc + 1); | |
for (int i = 0; i < argc; i++) { | |
int len = 0; | |
for (; argv[i][len]; len++); | |
syscall3(SYS_write, 1, (long)argv[i], len); | |
syscall3(SYS_write, 1, (long)"\n", 1); | |
} | |
for (char **e = envp; *e; e++) { | |
int len = 0; | |
for (; (*e)[len]; len++); | |
syscall3(SYS_write, 1, (long)*e, len); | |
syscall3(SYS_write, 1, (long)"\n", 1); | |
} | |
syscall3(SYS_exit, 0, 0, 0); | |
__builtin_unreachable(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment