From 08abe467ea171b05d287cf656fdd74b6e3231dd2 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 30 May 2026 10:17:43 +0100 Subject: [PATCH] ld: implement support for exposing ld functions to loaded executables --- sys/ld/CMakeLists.txt | 4 + sys/ld/arch/x86_64/layout.ld | 284 +++++++++++++++++++++++++++++++---- sys/ld/arch/x86_64/start.S | 9 +- sys/ld/elf.c | 37 +++-- sys/ld/elf.h | 3 +- sys/ld/exec.c | 35 +++++ sys/ld/exec.h | 13 ++ sys/ld/image-list.c | 2 +- sys/ld/main.c | 33 ++-- sys/ld/resolve.c | 10 +- sys/ld/self.c | 44 ++++++ sys/ld/self.h | 10 ++ 12 files changed, 425 insertions(+), 59 deletions(-) create mode 100644 sys/ld/exec.c create mode 100644 sys/ld/exec.h create mode 100644 sys/ld/self.c create mode 100644 sys/ld/self.h diff --git a/sys/ld/CMakeLists.txt b/sys/ld/CMakeLists.txt index 1f80616..d16e42a 100644 --- a/sys/ld/CMakeLists.txt +++ b/sys/ld/CMakeLists.txt @@ -12,8 +12,12 @@ target_link_libraries(ld interface::fs) target_compile_options(ld PRIVATE + -fvisibility=hidden -fPIC -fno-stack-protector -nostdlib -ffreestanding) target_link_options(ld PRIVATE + -fvisibility=hidden -Wl,--exclude-libs,ALL + # -Wl,--entry="_ld_start" + -T ${CMAKE_CURRENT_SOURCE_DIR}/arch/${CMAKE_SYSTEM_PROCESSOR}/layout.ld -fPIC -nostdlib -ffreestanding -Wl,-shared) sysroot_add_program( diff --git a/sys/ld/arch/x86_64/layout.ld b/sys/ld/arch/x86_64/layout.ld index 96a7da4..620cdde 100644 --- a/sys/ld/arch/x86_64/layout.ld +++ b/sys/ld/arch/x86_64/layout.ld @@ -1,30 +1,256 @@ -ENTRY(_start) - -SECTIONS { - .text ALIGN(4K) : { - *(.text) - *(.rodata) - } - - .rodata ALIGN(4K) : { - - } - - .data ALIGN(4K) : { - *(.data) - *(.bss) - *(.dynamic) - } - - .dynamic ALIGN(8) : { - *(.dynamic) - } - - .got.plt ALIGN(4K) : { - *(.got.plt) - } - - /DISCARD/ : { - *(.interp) - } +/* Copyright (C) 2014-2025 Free Software Foundation, Inc. + Copying and distribution of this script, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. */ +OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") +OUTPUT_ARCH(i386:x86-64) +ENTRY(_ld_start) +SECTIONS +{ + /* Read-only sections, merged into text segment: */ + PROVIDE(_ld_image_base = . + 1024); + . = SEGMENT_START("text-segment", 0) + SIZEOF_HEADERS; + /* Place the build-id as close to the ELF headers as possible. This + maximises the chance the build-id will be present in core files, + which GDB can then use to locate the associated debuginfo file. */ + .note.gnu.build-id : { *(.note.gnu.build-id) } + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .rela.dyn : + { + *(.rela.init) + *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) + *(.rela.fini) + *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) + *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) + *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) + *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) + *(.rela.ctors) + *(.rela.dtors) + *(.rela.got) + *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) + *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) + *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) + *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) + *(.rela.ifunc) + } + .rela.plt : + { + *(.rela.plt) + *(.rela.iplt) + } + .relr.dyn : { *(.relr.dyn) } + /* Start of the executable code region. */ + .init : + { + KEEP (*(SORT_NONE(.init))) + } + .plt : { *(.plt) *(.iplt) } +.plt.got : { *(.plt.got) } +.plt.sec : { *(.plt.sec) } + .text : + { + *(.text.unlikely .text.*_unlikely .text.unlikely.*) + *(.text.exit .text.exit.*) + *(.text.startup .text.startup.*) + *(.text.hot .text.hot.*) + *(SORT(.text.sorted.*)) + *(.text .stub .text.* .gnu.linkonce.t.*) + /* .gnu.warning sections are handled specially by elf.em. */ + *(.gnu.warning) + } + .fini : + { + KEEP (*(SORT_NONE(.fini))) + } + PROVIDE (__etext = .); + PROVIDE (_etext = .); + PROVIDE (etext = .); + /* Start of the Read Only Data region. */ + .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } + .rodata1 : { *(.rodata1) } + .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } + .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } + .sframe : ONLY_IF_RO { *(.sframe) *(.sframe.*) } + .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } + .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } + /* These sections are generated by the Sun/Oracle C++ compiler. */ + .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } + /* Various note sections. Placed here so that they are always included + in the read-only segment and not treated as orphan sections. The + current orphan handling algorithm does place note sections after R/O + data, but this is not guaranteed to always be the case. */ + .note.build-id : { *(.note.build-id) } + .note.GNU-stack : { *(.note.GNU-stack) } + .note.gnu-property : { *(.note.gnu-property) } + .note.ABI-tag : { *(.note.ABI-tag) } + .note.package : { *(.note.package) } + .note.dlopen : { *(.note.dlopen) } + .note.netbsd.ident : { *(.note.netbsd.ident) } + .note.openbsd.ident : { *(.note.openbsd.ident) } + /* Start of the Read Write Data region. */ + /* Adjust the address for the data segment. We want to adjust up to + the same address within the page on the next page up. */ + . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); + /* Exception handling. */ + .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } + .sframe : ONLY_IF_RW { *(.sframe) *(.sframe.*) } + .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } + .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } + .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } + /* Thread Local Storage sections. */ + .tdata : + { + *(.tdata .tdata.* .gnu.linkonce.td.*) + } + .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } + .preinit_array : + { + KEEP (*(.preinit_array)) + } + .init_array : + { + KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) + KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) + } + .fini_array : + { + KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) + KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) + } + .ctors : + { + /* gcc uses crtbegin.o to find the start of + the constructors, so we make sure it is + first. Because this is a wildcard, it + doesn't matter if the user does not + actually link against crtbegin.o; the + linker won't look for a file to match a + wildcard. The wildcard also means that it + doesn't matter which directory crtbegin.o + is in. */ + KEEP (*crtbegin.o(.ctors)) + KEEP (*crtbegin?.o(.ctors)) + /* We don't want to include the .ctor section from + the crtend.o file until after the sorted ctors. + The .ctor section from the crtend file contains the + end of ctors marker and it must be last */ + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + } + .dtors : + { + KEEP (*crtbegin.o(.dtors)) + KEEP (*crtbegin?.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + } + .jcr : { KEEP (*(.jcr)) } + .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } + .dynamic : { *(.dynamic) } + .got : { *(.got) *(.igot) } + . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); + .got.plt : { *(.got.plt) *(.igot.plt) } + .data : + { + *(.data .data.* .gnu.linkonce.d.*) + SORT(CONSTRUCTORS) + } + .data1 : { *(.data1) } + PROVIDE (_edata = .); + PROVIDE (edata = .); + . = ALIGN(ALIGNOF(NEXT_SECTION)); + PROVIDE (__bss_start = .); + .bss : + { + *(.dynbss) + *(.bss .bss.* .gnu.linkonce.b.*) + *(COMMON) + /* Align here to ensure that in the common case of there only being one + type of .bss section, the section occupies space up to _end. + Align after .bss to ensure correct alignment even if the + .bss section disappears because there are no input sections. + FIXME: Why do we need it? When there is no .bss section, we do not + pad the .data section. */ + . = ALIGN(. != 0 ? 64 / 8 : 1); + } + .lbss : + { + *(.dynlbss) + *(.lbss .lbss.* .gnu.linkonce.lb.*) + *(LARGE_COMMON) + } + . = ALIGN(64 / 8); + /* Start of the Large Data region. */ + . = SEGMENT_START("ldata-segment", .); + .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : + { + *(.lrodata .lrodata.* .gnu.linkonce.lr.*) + } + .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : + { + *(.ldata .ldata.* .gnu.linkonce.l.*) + . = ALIGN(. != 0 ? 64 / 8 : 1); + } + . = ALIGN(64 / 8); + PROVIDE (_end = .); + PROVIDE (end = .); + PROVIDE(_ld_image_limit = .); + . = DATA_SEGMENT_END (.); + /* Start of the Tiny Data region. */ + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 (INFO) : { *(.comment); LINKER_VERSION; } + .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1. */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions. */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2. */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2. */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } + /* SGI/MIPS DWARF 2 extensions. */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } + /* DWARF 3. */ + .debug_pubtypes 0 : { *(.debug_pubtypes) } + .debug_ranges 0 : { *(.debug_ranges) } + /* DWARF 5. */ + .debug_addr 0 : { *(.debug_addr) } + .debug_line_str 0 : { *(.debug_line_str) } + .debug_loclists 0 : { *(.debug_loclists) } + .debug_macro 0 : { *(.debug_macro) } + .debug_names 0 : { *(.debug_names) } + .debug_rnglists 0 : { *(.debug_rnglists) } + .debug_str_offsets 0 : { *(.debug_str_offsets) } + .debug_sup 0 : { *(.debug_sup) } + .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } + /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu_object_only) } } diff --git a/sys/ld/arch/x86_64/start.S b/sys/ld/arch/x86_64/start.S index f600396..f2247d7 100644 --- a/sys/ld/arch/x86_64/start.S +++ b/sys/ld/arch/x86_64/start.S @@ -1,7 +1,7 @@ .code64 -.global _start -.type _start, @function +.global _ld_start +.type _ld_start, @function .extern main .type main, @function @@ -9,7 +9,10 @@ .extern task_exit .type task_exit, @function -_start: +_ld_start: + nop + mov $0xFFFFFFFF, %rbp + call main mov %rax, %rdi call task_exit diff --git a/sys/ld/elf.c b/sys/ld/elf.c index 0cc3730..80f0394 100644 --- a/sys/ld/elf.c +++ b/sys/ld/elf.c @@ -6,10 +6,10 @@ #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include @@ -112,14 +112,17 @@ static int map_image(struct elf_image *image) int r = read(image->e_fd, &phdr, sizeof phdr); if (r < 0) { + kern_tracef("PHDR read failed"); return -r; } if (r != sizeof phdr) { + kern_tracef("PHDR bad size"); return ENOEXEC; } if (phdr.p_type != PT_LOAD) { + kern_tracef("PHDR != PT_LOAD"); continue; } @@ -163,7 +166,8 @@ static int map_image(struct elf_image *image) fd, offset); if (p == MAP_FAILED) { - return EIO; + kern_tracef("PHDR map failed (%d)", errno); + return errno; } kern_tracef( @@ -184,6 +188,8 @@ static int map_image(struct elf_image *image) } } + close(image->e_fd); + image->e_fd = -1; kern_tracef("ld: image %s -> %zx", image->e_leaf.l_name, image->e_base); return SUCCESS; } @@ -274,23 +280,27 @@ static void resolve_symbol(unsigned int slot) static int do_rela(struct elf_image *image, elf_rela_t *rela, bool lazy) { +#if 0 kern_tracef( "do_rela(%p, %d, %d, %d)", image, rela->r_info, rela->r_addend, rela->r_offset); +#endif int type = ELF64_R_TYPE(rela->r_info); elf_sym_t *sym = NULL; switch (type) { case R_X86_64_JUMP_SLOT: *(uint64_t *)(image->e_base + rela->r_offset) += image->e_base; +#if 0 kern_tracef( "JUMP_SLOT: offset=%zx, symbol=%zu, addend=%zx", rela->r_offset, ELF64_R_SYM(rela->r_info), rela->r_addend); +#endif break; case R_X86_64_GLOB_DAT: sym = get_dynsym(image, ELF64_R_SYM(rela->r_info)); @@ -300,11 +310,13 @@ static int do_rela(struct elf_image *image, elf_rela_t *rela, bool lazy) *(uint64_t *)(image->e_base + rela->r_offset) = image->e_base + sym->st_value + rela->r_addend; +#if 0 kern_tracef( "GLOB_DAT: offset=%zx, symbol=%zu, addend=%zx", rela->r_offset, ELF64_R_SYM(rela->r_info), rela->r_addend); +#endif break; case R_X86_64_64: sym = get_dynsym(image, ELF64_R_SYM(rela->r_info)); @@ -314,19 +326,23 @@ static int do_rela(struct elf_image *image, elf_rela_t *rela, bool lazy) *(uint64_t *)(image->e_base + rela->r_offset) = image->e_base + sym->st_value + rela->r_addend; +#if 0 kern_tracef( "64: offset=%zx, symbol=%zu, addend=%zx", rela->r_offset, ELF64_R_SYM(rela->r_info), rela->r_addend); +#endif break; case R_X86_64_RELATIVE: *(uint64_t *)(image->e_base + rela->r_offset) = image->e_base + rela->r_addend; +#if 0 kern_tracef( "RELATIVE: offset=%zx, addend=%zx", rela->r_offset, rela->r_addend); +#endif break; default: kern_trace("Unknown relocation type"); @@ -409,13 +425,14 @@ static int load_dependency(struct elf_image *image, const char *name) return ENOEXEC; } -static int parse_dynamic(struct elf_image *image) +int elf_image_parse_dynamic(struct elf_image *image) { if (image->e_dynamic.p_type != PT_DYNAMIC) { return SUCCESS; } image->e_dyn = (elf_dyn_t *)(image->e_base + image->e_dynamic.p_vaddr); + kern_tracef("%s .dynamic = %zx", image->e_leaf.l_name, image->e_dyn); int status = SUCCESS; @@ -507,8 +524,9 @@ static int parse_dynamic(struct elf_image *image) static int reserve_exec_region(struct elf_image *image) { + static virt_addr_t next_base = 0x10000000; void *base - = mmap(NULL, + = mmap((void *)next_base, image->e_length, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, @@ -518,6 +536,7 @@ static int reserve_exec_region(struct elf_image *image) return ENOMEM; } + next_base += 0x10000000; image->e_base = (virt_addr_t)base; return KERN_OK; } @@ -613,7 +632,7 @@ int elf_image_load(struct elf_image *img) return e; } - e = parse_dynamic(img); + e = elf_image_parse_dynamic(img); if (e != SUCCESS) { return e; } @@ -737,7 +756,7 @@ int elf_image_add_dependency(struct elf_image *img, struct elf_image *dep) void elf_image_close(struct elf_image *image) { - if (image->e_fd) { + if (image->e_fd != -1) { close(image->e_fd); } diff --git a/sys/ld/elf.h b/sys/ld/elf.h index 77eab23..e492474 100644 --- a/sys/ld/elf.h +++ b/sys/ld/elf.h @@ -3,7 +3,7 @@ #include "image-list.h" -#include +#include #include enum elf_image_status { @@ -360,6 +360,7 @@ extern int elf_image_collect_dependencies( extern int elf_image_add_dependency( struct elf_image *img, struct elf_image *dep); +extern int elf_image_parse_dynamic(struct elf_image *image); extern int elf_image_link(struct elf_image *img); extern void elf_image_close(struct elf_image *img); diff --git a/sys/ld/exec.c b/sys/ld/exec.c new file mode 100644 index 0000000..6208c1d --- /dev/null +++ b/sys/ld/exec.c @@ -0,0 +1,35 @@ +#include "self.h" + +#include +#include + +LD_API int ld_execl(const char *path, const char *arg, ...) +{ + return -ENOSYS; +} + +LD_API int ld_execlp(const char *file, const char *arg, ...) +{ + kern_log("ld_execlp()"); + return -ENOSYS; +} + +LD_API int ld_execle(const char *path, const char *arg, ...) +{ + return -ENOSYS; +} + +LD_API int ld_execv(const char *path, char *const argv[]) +{ + return -ENOSYS; +} + +LD_API int ld_execvp(const char *file, char *const argv[]) +{ + return -ENOSYS; +} + +LD_API int ld_execvpe(const char *file, char *const argv[], char *const envp[]) +{ + return -ENOSYS; +} diff --git a/sys/ld/exec.h b/sys/ld/exec.h new file mode 100644 index 0000000..a521302 --- /dev/null +++ b/sys/ld/exec.h @@ -0,0 +1,13 @@ +#ifndef EXEC_H_ +#define EXEC_H_ + +#include + +extern int ld_execl(const char *path, const char *arg1, va_list arg); +extern int ld_execlp(const char *file, const char *arg1, va_list arg); +extern int ld_execle(const char *path, const char *arg1, va_list arg); +extern int ld_execv(const char *path, char *const argv[]); +extern int ld_execvp(const char *file, char *const argv[]); +extern int ld_execvpe(const char *file, char *const argv[], char *const envp[]); + +#endif diff --git a/sys/ld/image-list.c b/sys/ld/image-list.c index 8df67a9..e6e5df2 100644 --- a/sys/ld/image-list.c +++ b/sys/ld/image-list.c @@ -2,7 +2,7 @@ #include "hash.h" -#include +#include #include #include #include diff --git a/sys/ld/main.c b/sys/ld/main.c index 71c376d..3ef3f06 100644 --- a/sys/ld/main.c +++ b/sys/ld/main.c @@ -2,15 +2,16 @@ #define MSG_NO_MALLOC #include "elf.h" +#include "self.h" #include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include @@ -101,7 +102,7 @@ static int load_images(const char *task_name, struct image_list *list) report_error( task_name, status, - "error while loading %s", + "C error while loading %s", image->e_leaf.l_name); return status; } @@ -112,7 +113,7 @@ static int load_images(const char *task_name, struct image_list *list) report_error( task_name, status, - "error while loading %s", + "B error while loading %s", image->e_leaf.l_name); return status; } @@ -123,8 +124,8 @@ static int load_images(const char *task_name, struct image_list *list) report_error( task_name, status, - "error while loading %s", - image->e_leaf.l_name); + "A error while loading %s", + image); return status; } case ELF_IMAGE_LOADED: @@ -139,7 +140,7 @@ static int load_images(const char *task_name, struct image_list *list) report_error( task_name, -new_dependencies, - "error while loading %s", + "F error while loading %s", image->e_leaf.l_name); return -new_dependencies; } @@ -174,7 +175,7 @@ static int link_images(const char *task_name, struct image_list *list) report_error( task_name, status, - "error while loading %s", + "E error while loading %s", image->e_leaf.l_name); return status; } @@ -221,6 +222,9 @@ int main(const struct rosetta_bootstrap *bs) const char *task_name = bs->bs_argv[2]; const char *image_name = get_image_name(exec_path); + kern_tracef("ld: exec_path=%s", exec_path); + kern_tracef("ld: task_name=%s", task_name); + image_list_init(&images); struct elf_image *exec = NULL; @@ -229,7 +233,7 @@ int main(const struct rosetta_bootstrap *bs) report_error( task_name, err, - "error while loading %s", + "D error while loading %s", exec_path); return -1; } @@ -251,6 +255,9 @@ int main(const struct rosetta_bootstrap *bs) return -1; } + struct elf_image *self = get_self(); + image_list_put(&images, &self->e_leaf); + struct image_list_iterator it; image_list_iterator_begin(&it, &images); @@ -265,7 +272,7 @@ int main(const struct rosetta_bootstrap *bs) image_list_iterator_move_next(&it); } - dump_loaded_images(&images); + // dump_loaded_images(&images); kern_tracef("ld finished"); struct rosetta_bootstrap exec_bsinfo; memcpy(&exec_bsinfo, bs, sizeof exec_bsinfo); diff --git a/sys/ld/resolve.c b/sys/ld/resolve.c index 41b7db2..8785f18 100644 --- a/sys/ld/resolve.c +++ b/sys/ld/resolve.c @@ -1,6 +1,6 @@ #include "elf.h" -#include +#include #include #include @@ -13,11 +13,13 @@ uintptr_t dl_runtime_resolve(struct elf_image *img, unsigned long sym_id) = (elf_rela_t *)((virt_addr_t)img->e_base + img->e_rel_offset[ELF_RT_PLTREL]); elf_rela_t *rela_sym = &rela[sym_id]; +#if 0 kern_tracef( "rela %u -> .dynsym %u, .got %u", sym_id, ELF64_R_SYM(rela_sym->r_info), slot_id); +#endif sym_id = ELF64_R_SYM(rela_sym->r_info); /* the first three entries in the GOT are not symbols. */ slot_id += 3; @@ -34,18 +36,20 @@ uintptr_t dl_runtime_resolve(struct elf_image *img, unsigned long sym_id) = (virt_addr_t *)((virt_addr_t)img->e_base + img->e_got_plt); const char *sym_name = (const char *)img->e_base + img->e_strtab + sym[sym_id].st_name; +#if 0 kern_tracef( "%s: request for symbol %u: %s", img->e_leaf.l_name, sym_id, sym_name); +#endif #if 0 virt_addr_t sym_addr = elf_image_find_linked_symbol(img, sym_name); #else virt_addr_t sym_addr = find_global_symbol(sym_name); #endif - kern_tracef("symbol %s = %zx", sym_name, sym_addr); - kern_tracef("slot %s = %zx", sym_name, &got[slot_id]); + // kern_logf("symbol %s = %zx", sym_name, sym_addr); + // kern_tracef("slot %s = %zx", sym_name, &got[slot_id]); got[slot_id] = sym_addr; return sym_addr; } diff --git a/sys/ld/self.c b/sys/ld/self.c new file mode 100644 index 0000000..e4b0400 --- /dev/null +++ b/sys/ld/self.c @@ -0,0 +1,44 @@ +#include "elf.h" + +#include +#include +#include + +#define LD_NAME "ld64.so" +#define LD_IMAGE_BASE_OFFSET 1024 + +extern char _DYNAMIC[]; +extern char _ld_image_base[]; +extern char _ld_image_limit[]; + +struct elf_image *get_self(void) +{ + struct elf_image *self = malloc(sizeof *self); + memset(self, 0x0, sizeof *self); + + snprintf( + self->e_leaf.l_name, + sizeof self->e_leaf.l_name, + "%s", + LD_NAME); + kern_config_get( + KERN_CFG_PAGE_SIZE, + &self->e_page_size, + sizeof self->e_page_size); + + self->e_base = (virt_addr_t)_ld_image_base - LD_IMAGE_BASE_OFFSET; + self->e_length = (virt_addr_t)_ld_image_limit - self->e_base; + if (self->e_length & (self->e_page_size - 1)) { + self->e_length &= ~(self->e_page_size - 1); + self->e_length += self->e_page_size; + } + + self->e_dynamic.p_type = PT_DYNAMIC; + self->e_dynamic.p_align = 8; + self->e_dynamic.p_memsz = self->e_dynamic.p_filesz = (size_t)-1; + self->e_dynamic.p_vaddr = (virt_addr_t)_DYNAMIC - self->e_base; + + elf_image_parse_dynamic(self); + + return self; +} diff --git a/sys/ld/self.h b/sys/ld/self.h new file mode 100644 index 0000000..a00aa5d --- /dev/null +++ b/sys/ld/self.h @@ -0,0 +1,10 @@ +#ifndef SELF_H_ +#define SELF_H_ + +#define LD_API __attribute__((visibility("default"))) + +struct elf_image; + +extern struct elf_image *get_self(void); + +#endif