From f2be833f8249326e2228be6fc2774e18ea370772 Mon Sep 17 00:00:00 2001 From: flysand7 Date: Sun, 23 Jul 2023 02:23:39 +1100 Subject: [PATCH] Fix build scripts, align some code, make a workable test --- build.sh | 1 + src/linux/crt_entry.asm | 2 +- src/linux/entry.c | 8 +++ src/linux/syscall.c | 124 +++++++++++++++++++++------------------- 4 files changed, 74 insertions(+), 61 deletions(-) diff --git a/build.sh b/build.sh index 1c5a562..a87dd3d 100755 --- a/build.sh +++ b/build.sh @@ -10,6 +10,7 @@ clang -fPIC -nostdlib -I "include" -g "src/ciabatta.c" -c -o "bin/ciabatta.o" rm "$LIB_FILE" 2> /dev/null if [ "$1" != "-shared" ]; then + [ -f "lib/ciabatta.a" ] && rm "lib/ciabatta.a" llvm-ar -q "lib/ciabatta.a" "bin/crt_ctors.o" "bin/crt_entry.o" "bin/ciabatta.o" else clang -fPIC -nostdlib -shared -o "lib/ciabatta.so" "bin/ciabatta.o" diff --git a/src/linux/crt_entry.asm b/src/linux/crt_entry.asm index 530c357..9e37153 100644 --- a/src/linux/crt_entry.asm +++ b/src/linux/crt_entry.asm @@ -24,7 +24,7 @@ _start: pop rsi mov rdx, qword [rsp] ;; Align stack to 16, push junk and stack ptr - and rsi, ~0xf + and rsp, ~0xf push rax push rsp ;; Push fini and init sections diff --git a/src/linux/entry.c b/src/linux/entry.c index 3295a0e..d900c06 100644 --- a/src/linux/entry.c +++ b/src/linux/entry.c @@ -1,4 +1,10 @@ + +int __stack_chk_fial() { + // TODO: implement proper stack protector support + return 0; +} + void __libc_start_main( int (*main)(int, char**, char**), int argc, char **argv, @@ -7,5 +13,7 @@ void __libc_start_main( void (*rtld_fini)(void), void *stack_end ) { + static char string[] = "Hello, world!\n"; + syscall_write(STDOUT_FILENO, string, sizeof string); syscall_exit(0); } diff --git a/src/linux/syscall.c b/src/linux/syscall.c index 1133361..c11889e 100644 --- a/src/linux/syscall.c +++ b/src/linux/syscall.c @@ -1,67 +1,71 @@ #if os_is_linux() -#define SYS_read 0 -#define SYS_write 1 -#define SYS_open 2 -#define SYS_close 3 -#define SYS_stat 4 -#define SYS_fstat 5 -#define SYS_lstat 6 -#define SYS_poll 7 -#define SYS_lseek 8 -#define SYS_mmap 9 -#define SYS_mprotect 10 -#define SYS_munmap 11 -#define SYS_brk 12 -#define SYS_rt_sigaction 13 +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +#define SYS_read 0 +#define SYS_write 1 +#define SYS_open 2 +#define SYS_close 3 +#define SYS_stat 4 +#define SYS_fstat 5 +#define SYS_lstat 6 +#define SYS_poll 7 +#define SYS_lseek 8 +#define SYS_mmap 9 +#define SYS_mprotect 10 +#define SYS_munmap 11 +#define SYS_brk 12 +#define SYS_rt_sigaction 13 #define SYS_rt_sigprocmask 14 -#define SYS_rt_sigreturn 15 -#define SYS_ioctl 16 -#define SYS_pread64 17 -#define SYS_pwrite64 18 -#define SYS_readv 19 -#define SYS_writev 20 -#define SYS_access 21 -#define SYS_pipe 22 -#define SYS_select 23 -#define SYS_sched_yield 24 -#define SYS_mremap 25 -#define SYS_msync 26 -#define SYS_mincore 27 -#define SYS_madvise 28 -#define SYS_shmget 29 -#define SYS_shmat 30 -#define SYS_shmctl 31 -#define SYS_dup 32 -#define SYS_dup2 33 -#define SYS_pause 34 -#define SYS_nanosleep 35 -#define SYS_getitimer 36 -#define SYS_alarm 37 -#define SYS_setitimer 38 -#define SYS_getpid 39 -#define SYS_sendfile 40 -#define SYS_socket 41 -#define SYS_connect 42 -#define SYS_accept 43 -#define SYS_sendto 44 -#define SYS_recvfrom 45 -#define SYS_sendmsg 46 -#define SYS_recvmsg 47 -#define SYS_shutdown 48 -#define SYS_bind 49 -#define SYS_listen 50 -#define SYS_getsockname 51 -#define SYS_getpeername 52 -#define SYS_socketpair 53 -#define SYS_setsockopt 54 -#define SYS_getsockopt 55 -#define SYS_clone 56 -#define SYS_fork 57 -#define SYS_vfork 58 -#define SYS_execve 59 -#define SYS_exit 60 +#define SYS_rt_sigreturn 15 +#define SYS_ioctl 16 +#define SYS_pread64 17 +#define SYS_pwrite64 18 +#define SYS_readv 19 +#define SYS_writev 20 +#define SYS_access 21 +#define SYS_pipe 22 +#define SYS_select 23 +#define SYS_sched_yield 24 +#define SYS_mremap 25 +#define SYS_msync 26 +#define SYS_mincore 27 +#define SYS_madvise 28 +#define SYS_shmget 29 +#define SYS_shmat 30 +#define SYS_shmctl 31 +#define SYS_dup 32 +#define SYS_dup2 33 +#define SYS_pause 34 +#define SYS_nanosleep 35 +#define SYS_getitimer 36 +#define SYS_alarm 37 +#define SYS_setitimer 38 +#define SYS_getpid 39 +#define SYS_sendfile 40 +#define SYS_socket 41 +#define SYS_connect 42 +#define SYS_accept 43 +#define SYS_sendto 44 +#define SYS_recvfrom 45 +#define SYS_sendmsg 46 +#define SYS_recvmsg 47 +#define SYS_shutdown 48 +#define SYS_bind 49 +#define SYS_listen 50 +#define SYS_getsockname 51 +#define SYS_getpeername 52 +#define SYS_socketpair 53 +#define SYS_setsockopt 54 +#define SYS_getsockopt 55 +#define SYS_clone 56 +#define SYS_fork 57 +#define SYS_vfork 58 +#define SYS_execve 59 +#define SYS_exit 60 // Syscall stubs