Skip to content

Instantly share code, notes, and snippets.

@lpsantil

lpsantil/crt0.c Secret

Forked from lunixbochs/crt0.c
Last active Aug 29, 2015
Embed
What would you like to do?
#include <sys/syscall.h>
#define sysdef(name) \
int name() { \
__asm__ __volatile__( \
"movq %0, %%rax;" \
"mov %%rcx, %%r10;" \
"syscall;" \
:: "i"(SYS_##name) : "rax" \
); \
}
void _start() {
__asm__ __volatile__ (
"pop %%rbp;" // C compiler will push rbp
"pop %%rdi;" // argc
"mov %%rsp, %%rsi;" // argv
"andq $-16, %%rsp;"
"call main;"
"movq %%rax, %%rdi;" // exit
"movq %0, %%rax;"
"syscall;"
:: "i"(SYS_exit)
);
}
// example program
sysdef(write);
int main(int argc, char **argv) {
write(1, "hello world\n", 12);
for (int i = 0; i < argc; i++) {
write(1, "arg\n", 4);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment