Retrieve envp, call main

This commit is contained in:
flysand7 2023-07-23 03:28:16 +11:00
parent f2be833f82
commit c8df2f9d42
5 changed files with 45 additions and 12 deletions

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
// Pre-C23 keyword macros // Pre-C23 keyword macros and stddef
#define static_assert _Static_assert #define static_assert _Static_assert
#define NULL ((void *)0)
// Platform macros // Platform macros
#define CIA_LINUX 1 #define CIA_LINUX 1

View File

@ -22,17 +22,26 @@ _start:
mov r9, rdx mov r9, rdx
;; Get argc and argv from the stack ;; Get argc and argv from the stack
pop rsi pop rsi
mov rdx, qword [rsp] mov rdx, rsp
;; Align stack to 16, push junk and stack ptr ;; Align stack to 16, push junk and stack ptr
and rsp, ~0xf and rsp, ~0xf
push rax push rax
push rsp push rsp
;; Push fini and init sections ;; Load fini and init initializers as function parameters
%ifdef CIA_SHARED
mov rcx, __libc_global_init wrt ..got mov rcx, __libc_global_init wrt ..got
mov r8, __libc_global_fini wrt ..got mov r8, __libc_global_fini wrt ..got
%else
mov rcx, __libc_global_init
mov r8, __libc_global_fini
%endif
mov rdi, main mov rdi, main
;; Call start main ;; Call start main
%ifdef CIA_SHARED
call __libc_start_main wrt ..plt call __libc_start_main wrt ..plt
%else
call __libc_start_main
%endif
;; No idea why halt it, I guess that's a funny ;; No idea why halt it, I guess that's a funny
;; way to crash your application if the function we called ;; way to crash your application if the function we called
;; returns instead of calling the exit syscall ;; returns instead of calling the exit syscall

View File

@ -1,6 +1,6 @@
int __stack_chk_fial() { int __stack_chk_fail() {
// TODO: implement proper stack protector support // TODO: implement proper stack protector support
return 0; return 0;
} }
@ -10,10 +10,15 @@ void __libc_start_main(
int argc, char **argv, int argc, char **argv,
int (*init)(int, char**, char**), int (*init)(int, char**, char**),
void (*fini)(void), void (*fini)(void),
void (*rtld_fini)(void), void (*runtime_ld_fini)(void),
void *stack_end void *stack_end
) { ) {
static char string[] = "Hello, world!\n"; char **envp = argv + (argc + 1);
syscall_write(STDOUT_FILENO, string, sizeof string); init(argc, argv, envp);
main(argc, argv, envp);
fini();
if(runtime_ld_fini != NULL) {
runtime_ld_fini();
}
syscall_exit(0); syscall_exit(0);
} }

View File

@ -3,6 +3,6 @@
if [ "$1" != "-shared" ]; then if [ "$1" != "-shared" ]; then
clang -static -nostdlib tests/empty.c lib/ciabatta.a -Iinclude clang -static -nostdlib tests/empty.c lib/ciabatta.a -Iinclude
else else
clang -fPIE tests/empty.c -c -o tests/empty.o clang -g -fno-stack-protector -fPIE tests/empty.c -c -o tests/empty.o
ld -no-pie -nostdlib lib/entry.o tests/empty.o lib/ciabatta.so lib/ctors.o -Iinclude ld -no-pie -nostdlib lib/entry.o tests/empty.o lib/ciabatta.so lib/ctors.o -Iinclude
fi fi

View File

@ -1,4 +1,22 @@
int main() { #include <cia_definitions.h>
#define STDOUT_FILENO 1
#define SYS_write 1
static __inline i64 __syscall3(i64 n, i64 a1, i64 a2, i64 a3) {
i64 ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3) : "rcx", "r11", "memory");
return ret;
}
static inline i64 syscall_write(u32 fd, char const *buf, u64 count) {
return __syscall3(SYS_write, (i64)fd, (i64)buf, (u64)count);
}
char string[] = "Hello, world!\n";
int main(int argc, char **argv, char **envp) {
syscall_write(STDOUT_FILENO, string, sizeof string);
return 0; return 0;
} }