Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active January 5, 2025 07:09
Show Gist options
  • Save oguz-ismail/72e34550af13e3841ed58e291096adde to your computer and use it in GitHub Desktop.
Save oguz-ismail/72e34550af13e3841ed58e291096adde to your computer and use it in GitHub Desktop.
cc -nostdlib hello.c syscall.S -static -no-pie -lc
cc -nostdlib -o /usr/lib/libc.so.0 syscall.S -shared -Wl,--soname=libc.so.0
rm libcx.so
ln -s /usr/lib/libc.so.0 libcx.so
cc -nostdlib hello.c -L . -lcx -Wl,-Bstatic -lc
#include <string.h>
#include <sys/syscall.h>
long syscall(int, ...);
void
_start(void) {
void *p = __builtin_frame_address(0);
int argc = ((int*)p)[2];
char **argv = &((char**)p)[2];
static char msg[64] = "Hello, ";
strcat(msg, argc > 1 ? argv[1] : "world");
strcat(msg, "!\n");
syscall(SYS_write, 1, msg, strlen(msg));
syscall(SYS_exit, 0);
}
#if __PIC__
asm (
".pushsection .note.openbsd.ident, \"a\"\n"
".long 8, 4, 1\n"
".string \"OpenBSD\"\n"
".long 0\n"
".popsection\n"
);
#endif
.globl errno
.globl syscall
.bss
errno:
.long 0
.text
syscall:
mov %rdi, %rax
mov %rsi, %rdi
mov %rdx, %rsi
mov %rcx, %rdx
mov %r8, %r10
mov %r9, %r8
mov 8(%rsp), %r9
99: syscall
jnc 1f
#if __PIC__
mov errno@GOTPCREL(%rip), %rdi
mov %eax, (%rdi)
#else
mov %eax, errno
#endif
mov $-1, %eax
1: ret
.section .openbsd.syscalls, ""
.long 99b, 1
.long 99b, 4
.section .note.openbsd.ident, "a"
.long 8, 4, 1
.string "OpenBSD"
.long 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment