From 4aee2f759ab7bf2ad9534941afef0195040ba5db Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 26 Jul 2022 22:40:28 +0200 Subject: user/libc: a _start that automatically selfrelocates PIEs --- src/user/app/init/main.c | 5 +---- src/user/app/testelf/main.c | 15 +++------------ src/user/bootstrap/main.c | 4 +++- src/user/lib/crt0.c | 10 ++++++++++ src/user/linker.ld | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 src/user/lib/crt0.c (limited to 'src') diff --git a/src/user/app/init/main.c b/src/user/app/init/main.c index 426933f..788fd78 100644 --- a/src/user/app/init/main.c +++ b/src/user/app/init/main.c @@ -11,10 +11,7 @@ __attribute__((visibility("hidden"))) extern char _image_base[]; -__attribute__((section(".text.startup"))) int main(void) { - elf_selfreloc(); - freopen("/kdev/com1", "a+", stdout); printf("in init (stage 2), loaded at 0x%x\n", &_image_base); @@ -73,5 +70,5 @@ int main(void) { _syscall_await(); printf("init: quitting\n"); - exit(0); + return 0; } diff --git a/src/user/app/testelf/main.c b/src/user/app/testelf/main.c index 93a5e11..a3e54b8 100644 --- a/src/user/app/testelf/main.c +++ b/src/user/app/testelf/main.c @@ -1,18 +1,9 @@ -#include #include -#include -#include -#include const char *str = "Hello!\n", *str2 = "World.\n"; -__attribute__((visibility("hidden"))) -extern char _image_base[]; - int main(void) { - elf_selfreloc(); - printf("loaded at %x\n", &_image_base); - printf(str); - printf(str2); - exit(0); + printf("elftest's &main == 0x%x\n", &main); + printf("%s%s", str, str2); + return 0; } diff --git a/src/user/bootstrap/main.c b/src/user/bootstrap/main.c index fecd38f..089fd68 100644 --- a/src/user/bootstrap/main.c +++ b/src/user/bootstrap/main.c @@ -11,7 +11,7 @@ extern char _bss_end; extern char _initrd; __attribute__((section(".text.startup"))) -int main(void) { +void _start(void) { _syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT); /* move everything provided by the kernel to /kdev */ @@ -34,3 +34,5 @@ int main(void) { } _syscall_exit(1); } + +int main(void) {return 0;} // dummy, needed to link diff --git a/src/user/lib/crt0.c b/src/user/lib/crt0.c new file mode 100644 index 0000000..91bbb05 --- /dev/null +++ b/src/user/lib/crt0.c @@ -0,0 +1,10 @@ +#include "elfload.h" +#include + +int main(); + +__attribute__((__weak__)) +void _start(void) { + elf_selfreloc(); + exit(main()); +} diff --git a/src/user/linker.ld b/src/user/linker.ld index fcb5fc8..e638289 100644 --- a/src/user/linker.ld +++ b/src/user/linker.ld @@ -1,4 +1,4 @@ -ENTRY(main) +ENTRY(_start) SECTIONS { -- cgit v1.2.3