diff options
author | dzwdz | 2022-07-24 16:34:37 +0200 |
---|---|---|
committer | dzwdz | 2022-07-24 16:34:37 +0200 |
commit | 373afdda5759198b2d5123abb578e3c649103493 (patch) | |
tree | d47c9271ac37c20630ec387ef1d2d0e2c8409024 | |
parent | 9f3fdb830f61cd8c8c1f1db9d03cba1c546c1a7e (diff) |
user: put the testelf in a sensible location in the tree
-rw-r--r-- | Makefile | 8 | ||||
l--------- | initrd/test.elf | 1 | ||||
l--------- | initrd/testelf.elf | 1 | ||||
-rw-r--r-- | src/user/app/testelf/main.c | 18 | ||||
-rw-r--r-- | src/usertestelf.c | 69 | ||||
-rw-r--r-- | src/usertestelf.ld | 23 |
6 files changed, 23 insertions, 97 deletions
@@ -64,22 +64,22 @@ out/bootstrap: src/user_bootstrap/linker.ld $(call from_sources, src/user_bootst @mkdir -p $(@D) @$(CC) $(LFLAGS) -T $^ -o $@ -initrd/test.elf: out/test.elf +initrd/testelf.elf: out/testelf.elf @# dummy initrd/init.elf: out/init.elf @# dummy -out/test.elf: src/usertestelf.ld out/obj/usertestelf.c.o out/obj/user/lib/syscall.s.o $(call from_sources, src/shared/) +out/testelf.elf: src/user/linker.ld $(call from_sources, src/user/app/testelf/) $(call from_sources, src/user/lib/) $(call from_sources, src/shared/) @mkdir -p $(@D) @$(CC) $(LFLAGS) -Wl,-pie -Wl,-no-dynamic-linker -T $^ -o $@ -out/init.elf: src/user/linker.ld $(call from_sources, src/user/) $(call from_sources, src/shared/) +out/init.elf: src/user/linker.ld $(call from_sources, src/user/app/init/) $(call from_sources, src/user/lib/) $(call from_sources, src/shared/) @mkdir -p $(@D) @$(CC) $(LFLAGS) -Wl,-pie -Wl,-no-dynamic-linker -T $^ -o $@ # TODO automatically resolve symlinks -out/initrd.tar: $(shell find initrd/) out/test.elf out/init.elf +out/initrd.tar: $(shell find initrd/) out/testelf.elf out/init.elf cd initrd; tar chf ../$@ * out/fs/boot/init: out/bootstrap out/initrd.tar diff --git a/initrd/test.elf b/initrd/test.elf deleted file mode 120000 index ca6b9fd..0000000 --- a/initrd/test.elf +++ /dev/null @@ -1 +0,0 @@ -../out/test.elf
\ No newline at end of file diff --git a/initrd/testelf.elf b/initrd/testelf.elf new file mode 120000 index 0000000..8f2f99c --- /dev/null +++ b/initrd/testelf.elf @@ -0,0 +1 @@ +../out/testelf.elf
\ No newline at end of file diff --git a/src/user/app/testelf/main.c b/src/user/app/testelf/main.c new file mode 100644 index 0000000..a80d233 --- /dev/null +++ b/src/user/app/testelf/main.c @@ -0,0 +1,18 @@ +#include <shared/printf.h> +#include <shared/syscalls.h> +#include <user/lib/elf.h> +#include <user/lib/elfload.h> +#include <user/lib/stdlib.h> + +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); + _syscall_exit(0); +} diff --git a/src/usertestelf.c b/src/usertestelf.c deleted file mode 100644 index 1cbfcc5..0000000 --- a/src/usertestelf.c +++ /dev/null @@ -1,69 +0,0 @@ -#include <shared/printf.h> -#include <user/lib/elf.h> -#include <user/lib/syscall.c> - -const char *str = "Hello!\n", *str2 = "World.\n"; - -__attribute__((visibility("hidden"))) -extern struct Elf64_Dyn _DYNAMIC[]; - -__attribute__((visibility("hidden"))) -extern char _image_base[]; - -static void printf_backend(void *arg, const char *buf, size_t len) { - (void)arg; - _syscall_write(1, buf, len, -1); - _syscall_debug_klog(buf, len); -} - -int printf(const char *fmt, ...) { - int ret = 0; - va_list argp; - va_start(argp, fmt); - ret = __printf_internal(fmt, argp, printf_backend, 0); - va_end(argp); - return ret; -} - - -static struct Elf64_Dyn *dyn_gettag(Elf64_Xword tag) { - for (size_t i = 0;; i++) { - if (_DYNAMIC[i].d_tag == tag) return &_DYNAMIC[i]; - if (_DYNAMIC[i].d_tag == DT_NULL) return NULL; - } -} - -static void reloc(void) { - // TODO DT_REL - if (dyn_gettag(DT_PLTGOT) || dyn_gettag(DT_JMPREL)) { - printf("elf: unimplemented tag in _DYNAMIC\n"); - } - - struct Elf64_Dyn *rela_tag = dyn_gettag(DT_RELA); - if (rela_tag) { - /* not checking pointer validity, - * crashing on an invalid elf is fine */ - size_t relasz = dyn_gettag(DT_RELASZ)->d_val; - size_t relaent = dyn_gettag(DT_RELAENT)->d_val; - for (size_t o = 0; o < relasz; o += relaent) { - struct Elf64_Rela *r = (void*)_image_base + rela_tag->d_ptr + o; - uintptr_t *target = (void*)_image_base + r->r_offset; - - switch (ELF64_R_TYPE(r->r_info)) { - case R_X86_64_RELATIVE: - *target = (uintptr_t)&_image_base + r->r_addend; - break; - default: - printf("elf: unsupported relocation type\n"); - } - } - } -} - -int main(void) { - printf("loaded at %x\n", &_image_base); - reloc(); - printf(str); - printf(str2); - _syscall_exit(0); -} diff --git a/src/usertestelf.ld b/src/usertestelf.ld deleted file mode 100644 index fcb5fc8..0000000 --- a/src/usertestelf.ld +++ /dev/null @@ -1,23 +0,0 @@ -ENTRY(main) - -SECTIONS -{ - _image_base = .; - .text BLOCK(4K) : ALIGN(4K) - { - *(.text) - } - .rodata BLOCK(4K) : ALIGN(4K) - { - *(.rodata) - } - .data BLOCK(4K) : ALIGN(4K) - { - *(.data) - } - .bss BLOCK(4K) : ALIGN(4K) - { - *(COMMON) - *(.bss) - } -} |