ld: implement support for exposing ld functions to loaded executables
This commit is contained in:
@@ -12,8 +12,12 @@ target_link_libraries(ld
|
|||||||
interface::fs)
|
interface::fs)
|
||||||
|
|
||||||
target_compile_options(ld PRIVATE
|
target_compile_options(ld PRIVATE
|
||||||
|
-fvisibility=hidden
|
||||||
-fPIC -fno-stack-protector -nostdlib -ffreestanding)
|
-fPIC -fno-stack-protector -nostdlib -ffreestanding)
|
||||||
target_link_options(ld PRIVATE
|
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)
|
-fPIC -nostdlib -ffreestanding -Wl,-shared)
|
||||||
|
|
||||||
sysroot_add_program(
|
sysroot_add_program(
|
||||||
|
|||||||
+255
-29
@@ -1,30 +1,256 @@
|
|||||||
ENTRY(_start)
|
/* Copyright (C) 2014-2025 Free Software Foundation, Inc.
|
||||||
|
Copying and distribution of this script, with or without modification,
|
||||||
SECTIONS {
|
are permitted in any medium without royalty provided the copyright
|
||||||
.text ALIGN(4K) : {
|
notice and this notice are preserved. */
|
||||||
*(.text)
|
OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64")
|
||||||
*(.rodata)
|
OUTPUT_ARCH(i386:x86-64)
|
||||||
}
|
ENTRY(_ld_start)
|
||||||
|
SECTIONS
|
||||||
.rodata ALIGN(4K) : {
|
{
|
||||||
|
/* Read-only sections, merged into text segment: */
|
||||||
}
|
PROVIDE(_ld_image_base = . + 1024);
|
||||||
|
. = SEGMENT_START("text-segment", 0) + SIZEOF_HEADERS;
|
||||||
.data ALIGN(4K) : {
|
/* Place the build-id as close to the ELF headers as possible. This
|
||||||
*(.data)
|
maximises the chance the build-id will be present in core files,
|
||||||
*(.bss)
|
which GDB can then use to locate the associated debuginfo file. */
|
||||||
*(.dynamic)
|
.note.gnu.build-id : { *(.note.gnu.build-id) }
|
||||||
}
|
.hash : { *(.hash) }
|
||||||
|
.gnu.hash : { *(.gnu.hash) }
|
||||||
.dynamic ALIGN(8) : {
|
.dynsym : { *(.dynsym) }
|
||||||
*(.dynamic)
|
.dynstr : { *(.dynstr) }
|
||||||
}
|
.gnu.version : { *(.gnu.version) }
|
||||||
|
.gnu.version_d : { *(.gnu.version_d) }
|
||||||
.got.plt ALIGN(4K) : {
|
.gnu.version_r : { *(.gnu.version_r) }
|
||||||
*(.got.plt)
|
.rela.dyn :
|
||||||
}
|
{
|
||||||
|
*(.rela.init)
|
||||||
/DISCARD/ : {
|
*(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
|
||||||
*(.interp)
|
*(.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) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
.code64
|
.code64
|
||||||
|
|
||||||
.global _start
|
.global _ld_start
|
||||||
.type _start, @function
|
.type _ld_start, @function
|
||||||
|
|
||||||
.extern main
|
.extern main
|
||||||
.type main, @function
|
.type main, @function
|
||||||
@@ -9,7 +9,10 @@
|
|||||||
.extern task_exit
|
.extern task_exit
|
||||||
.type task_exit, @function
|
.type task_exit, @function
|
||||||
|
|
||||||
_start:
|
_ld_start:
|
||||||
|
nop
|
||||||
|
mov $0xFFFFFFFF, %rbp
|
||||||
|
|
||||||
call main
|
call main
|
||||||
mov %rax, %rdi
|
mov %rax, %rdi
|
||||||
call task_exit
|
call task_exit
|
||||||
|
|||||||
+28
-9
@@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <mango/config.h>
|
#include <magenta/config.h>
|
||||||
#include <mango/handle.h>
|
#include <magenta/handle.h>
|
||||||
#include <mango/log.h>
|
#include <magenta/log.h>
|
||||||
#include <mango/vm.h>
|
#include <magenta/vm.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -112,14 +112,17 @@ static int map_image(struct elf_image *image)
|
|||||||
int r = read(image->e_fd, &phdr, sizeof phdr);
|
int r = read(image->e_fd, &phdr, sizeof phdr);
|
||||||
|
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
|
kern_tracef("PHDR read failed");
|
||||||
return -r;
|
return -r;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r != sizeof phdr) {
|
if (r != sizeof phdr) {
|
||||||
|
kern_tracef("PHDR bad size");
|
||||||
return ENOEXEC;
|
return ENOEXEC;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (phdr.p_type != PT_LOAD) {
|
if (phdr.p_type != PT_LOAD) {
|
||||||
|
kern_tracef("PHDR != PT_LOAD");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +166,8 @@ static int map_image(struct elf_image *image)
|
|||||||
fd,
|
fd,
|
||||||
offset);
|
offset);
|
||||||
if (p == MAP_FAILED) {
|
if (p == MAP_FAILED) {
|
||||||
return EIO;
|
kern_tracef("PHDR map failed (%d)", errno);
|
||||||
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
kern_tracef(
|
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);
|
kern_tracef("ld: image %s -> %zx", image->e_leaf.l_name, image->e_base);
|
||||||
return SUCCESS;
|
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)
|
static int do_rela(struct elf_image *image, elf_rela_t *rela, bool lazy)
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"do_rela(%p, %d, %d, %d)",
|
"do_rela(%p, %d, %d, %d)",
|
||||||
image,
|
image,
|
||||||
rela->r_info,
|
rela->r_info,
|
||||||
rela->r_addend,
|
rela->r_addend,
|
||||||
rela->r_offset);
|
rela->r_offset);
|
||||||
|
#endif
|
||||||
int type = ELF64_R_TYPE(rela->r_info);
|
int type = ELF64_R_TYPE(rela->r_info);
|
||||||
elf_sym_t *sym = NULL;
|
elf_sym_t *sym = NULL;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case R_X86_64_JUMP_SLOT:
|
case R_X86_64_JUMP_SLOT:
|
||||||
*(uint64_t *)(image->e_base + rela->r_offset) += image->e_base;
|
*(uint64_t *)(image->e_base + rela->r_offset) += image->e_base;
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"JUMP_SLOT: offset=%zx, symbol=%zu, addend=%zx",
|
"JUMP_SLOT: offset=%zx, symbol=%zu, addend=%zx",
|
||||||
rela->r_offset,
|
rela->r_offset,
|
||||||
ELF64_R_SYM(rela->r_info),
|
ELF64_R_SYM(rela->r_info),
|
||||||
rela->r_addend);
|
rela->r_addend);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
case R_X86_64_GLOB_DAT:
|
case R_X86_64_GLOB_DAT:
|
||||||
sym = get_dynsym(image, ELF64_R_SYM(rela->r_info));
|
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)
|
*(uint64_t *)(image->e_base + rela->r_offset)
|
||||||
= image->e_base + sym->st_value + rela->r_addend;
|
= image->e_base + sym->st_value + rela->r_addend;
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"GLOB_DAT: offset=%zx, symbol=%zu, addend=%zx",
|
"GLOB_DAT: offset=%zx, symbol=%zu, addend=%zx",
|
||||||
rela->r_offset,
|
rela->r_offset,
|
||||||
ELF64_R_SYM(rela->r_info),
|
ELF64_R_SYM(rela->r_info),
|
||||||
rela->r_addend);
|
rela->r_addend);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
case R_X86_64_64:
|
case R_X86_64_64:
|
||||||
sym = get_dynsym(image, ELF64_R_SYM(rela->r_info));
|
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)
|
*(uint64_t *)(image->e_base + rela->r_offset)
|
||||||
= image->e_base + sym->st_value + rela->r_addend;
|
= image->e_base + sym->st_value + rela->r_addend;
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"64: offset=%zx, symbol=%zu, addend=%zx",
|
"64: offset=%zx, symbol=%zu, addend=%zx",
|
||||||
rela->r_offset,
|
rela->r_offset,
|
||||||
ELF64_R_SYM(rela->r_info),
|
ELF64_R_SYM(rela->r_info),
|
||||||
rela->r_addend);
|
rela->r_addend);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
case R_X86_64_RELATIVE:
|
case R_X86_64_RELATIVE:
|
||||||
*(uint64_t *)(image->e_base + rela->r_offset)
|
*(uint64_t *)(image->e_base + rela->r_offset)
|
||||||
= image->e_base + rela->r_addend;
|
= image->e_base + rela->r_addend;
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"RELATIVE: offset=%zx, addend=%zx",
|
"RELATIVE: offset=%zx, addend=%zx",
|
||||||
rela->r_offset,
|
rela->r_offset,
|
||||||
rela->r_addend);
|
rela->r_addend);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
kern_trace("Unknown relocation type");
|
kern_trace("Unknown relocation type");
|
||||||
@@ -409,13 +425,14 @@ static int load_dependency(struct elf_image *image, const char *name)
|
|||||||
return ENOEXEC;
|
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) {
|
if (image->e_dynamic.p_type != PT_DYNAMIC) {
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
image->e_dyn = (elf_dyn_t *)(image->e_base + image->e_dynamic.p_vaddr);
|
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;
|
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 int reserve_exec_region(struct elf_image *image)
|
||||||
{
|
{
|
||||||
|
static virt_addr_t next_base = 0x10000000;
|
||||||
void *base
|
void *base
|
||||||
= mmap(NULL,
|
= mmap((void *)next_base,
|
||||||
image->e_length,
|
image->e_length,
|
||||||
PROT_NONE,
|
PROT_NONE,
|
||||||
MAP_ANONYMOUS | MAP_PRIVATE,
|
MAP_ANONYMOUS | MAP_PRIVATE,
|
||||||
@@ -518,6 +536,7 @@ static int reserve_exec_region(struct elf_image *image)
|
|||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
next_base += 0x10000000;
|
||||||
image->e_base = (virt_addr_t)base;
|
image->e_base = (virt_addr_t)base;
|
||||||
return KERN_OK;
|
return KERN_OK;
|
||||||
}
|
}
|
||||||
@@ -613,7 +632,7 @@ int elf_image_load(struct elf_image *img)
|
|||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
e = parse_dynamic(img);
|
e = elf_image_parse_dynamic(img);
|
||||||
if (e != SUCCESS) {
|
if (e != SUCCESS) {
|
||||||
return e;
|
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)
|
void elf_image_close(struct elf_image *image)
|
||||||
{
|
{
|
||||||
if (image->e_fd) {
|
if (image->e_fd != -1) {
|
||||||
close(image->e_fd);
|
close(image->e_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "image-list.h"
|
#include "image-list.h"
|
||||||
|
|
||||||
#include <mango/types.h>
|
#include <magenta/types.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
enum elf_image_status {
|
enum elf_image_status {
|
||||||
@@ -360,6 +360,7 @@ extern int elf_image_collect_dependencies(
|
|||||||
extern int elf_image_add_dependency(
|
extern int elf_image_add_dependency(
|
||||||
struct elf_image *img,
|
struct elf_image *img,
|
||||||
struct elf_image *dep);
|
struct elf_image *dep);
|
||||||
|
extern int elf_image_parse_dynamic(struct elf_image *image);
|
||||||
extern int elf_image_link(struct elf_image *img);
|
extern int elf_image_link(struct elf_image *img);
|
||||||
extern void elf_image_close(struct elf_image *img);
|
extern void elf_image_close(struct elf_image *img);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include "self.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <magenta/log.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef EXEC_H_
|
||||||
|
#define EXEC_H_
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
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
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
|
||||||
#include <mango/log.h>
|
#include <magenta/log.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
+20
-13
@@ -2,15 +2,16 @@
|
|||||||
#define MSG_NO_MALLOC
|
#define MSG_NO_MALLOC
|
||||||
|
|
||||||
#include "elf.h"
|
#include "elf.h"
|
||||||
|
#include "self.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <heap/heap.h>
|
#include <heap/heap.h>
|
||||||
#include <mango/log.h>
|
#include <magenta/log.h>
|
||||||
#include <mango/msg.h>
|
#include <magenta/msg.h>
|
||||||
#include <mango/task.h>
|
#include <magenta/task.h>
|
||||||
#include <mango/types.h>
|
#include <magenta/types.h>
|
||||||
#include <mango/vm.h>
|
#include <magenta/vm.h>
|
||||||
#include <rosetta/bootstrap.h>
|
#include <rosetta/bootstrap.h>
|
||||||
#include <rosetta/fs.h>
|
#include <rosetta/fs.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@@ -101,7 +102,7 @@ static int load_images(const char *task_name, struct image_list *list)
|
|||||||
report_error(
|
report_error(
|
||||||
task_name,
|
task_name,
|
||||||
status,
|
status,
|
||||||
"error while loading %s",
|
"C error while loading %s",
|
||||||
image->e_leaf.l_name);
|
image->e_leaf.l_name);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -112,7 +113,7 @@ static int load_images(const char *task_name, struct image_list *list)
|
|||||||
report_error(
|
report_error(
|
||||||
task_name,
|
task_name,
|
||||||
status,
|
status,
|
||||||
"error while loading %s",
|
"B error while loading %s",
|
||||||
image->e_leaf.l_name);
|
image->e_leaf.l_name);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -123,8 +124,8 @@ static int load_images(const char *task_name, struct image_list *list)
|
|||||||
report_error(
|
report_error(
|
||||||
task_name,
|
task_name,
|
||||||
status,
|
status,
|
||||||
"error while loading %s",
|
"A error while loading %s",
|
||||||
image->e_leaf.l_name);
|
image);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
case ELF_IMAGE_LOADED:
|
case ELF_IMAGE_LOADED:
|
||||||
@@ -139,7 +140,7 @@ static int load_images(const char *task_name, struct image_list *list)
|
|||||||
report_error(
|
report_error(
|
||||||
task_name,
|
task_name,
|
||||||
-new_dependencies,
|
-new_dependencies,
|
||||||
"error while loading %s",
|
"F error while loading %s",
|
||||||
image->e_leaf.l_name);
|
image->e_leaf.l_name);
|
||||||
return -new_dependencies;
|
return -new_dependencies;
|
||||||
}
|
}
|
||||||
@@ -174,7 +175,7 @@ static int link_images(const char *task_name, struct image_list *list)
|
|||||||
report_error(
|
report_error(
|
||||||
task_name,
|
task_name,
|
||||||
status,
|
status,
|
||||||
"error while loading %s",
|
"E error while loading %s",
|
||||||
image->e_leaf.l_name);
|
image->e_leaf.l_name);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -221,6 +222,9 @@ int main(const struct rosetta_bootstrap *bs)
|
|||||||
const char *task_name = bs->bs_argv[2];
|
const char *task_name = bs->bs_argv[2];
|
||||||
const char *image_name = get_image_name(exec_path);
|
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);
|
image_list_init(&images);
|
||||||
|
|
||||||
struct elf_image *exec = NULL;
|
struct elf_image *exec = NULL;
|
||||||
@@ -229,7 +233,7 @@ int main(const struct rosetta_bootstrap *bs)
|
|||||||
report_error(
|
report_error(
|
||||||
task_name,
|
task_name,
|
||||||
err,
|
err,
|
||||||
"error while loading %s",
|
"D error while loading %s",
|
||||||
exec_path);
|
exec_path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -251,6 +255,9 @@ int main(const struct rosetta_bootstrap *bs)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct elf_image *self = get_self();
|
||||||
|
image_list_put(&images, &self->e_leaf);
|
||||||
|
|
||||||
struct image_list_iterator it;
|
struct image_list_iterator it;
|
||||||
image_list_iterator_begin(&it, &images);
|
image_list_iterator_begin(&it, &images);
|
||||||
|
|
||||||
@@ -265,7 +272,7 @@ int main(const struct rosetta_bootstrap *bs)
|
|||||||
image_list_iterator_move_next(&it);
|
image_list_iterator_move_next(&it);
|
||||||
}
|
}
|
||||||
|
|
||||||
dump_loaded_images(&images);
|
// dump_loaded_images(&images);
|
||||||
kern_tracef("ld finished");
|
kern_tracef("ld finished");
|
||||||
struct rosetta_bootstrap exec_bsinfo;
|
struct rosetta_bootstrap exec_bsinfo;
|
||||||
memcpy(&exec_bsinfo, bs, sizeof exec_bsinfo);
|
memcpy(&exec_bsinfo, bs, sizeof exec_bsinfo);
|
||||||
|
|||||||
+7
-3
@@ -1,6 +1,6 @@
|
|||||||
#include "elf.h"
|
#include "elf.h"
|
||||||
|
|
||||||
#include <mango/log.h>
|
#include <magenta/log.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -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
|
= (elf_rela_t *)((virt_addr_t)img->e_base
|
||||||
+ img->e_rel_offset[ELF_RT_PLTREL]);
|
+ img->e_rel_offset[ELF_RT_PLTREL]);
|
||||||
elf_rela_t *rela_sym = &rela[sym_id];
|
elf_rela_t *rela_sym = &rela[sym_id];
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"rela %u -> .dynsym %u, .got %u",
|
"rela %u -> .dynsym %u, .got %u",
|
||||||
sym_id,
|
sym_id,
|
||||||
ELF64_R_SYM(rela_sym->r_info),
|
ELF64_R_SYM(rela_sym->r_info),
|
||||||
slot_id);
|
slot_id);
|
||||||
|
#endif
|
||||||
sym_id = ELF64_R_SYM(rela_sym->r_info);
|
sym_id = ELF64_R_SYM(rela_sym->r_info);
|
||||||
/* the first three entries in the GOT are not symbols. */
|
/* the first three entries in the GOT are not symbols. */
|
||||||
slot_id += 3;
|
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);
|
= (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
|
const char *sym_name = (const char *)img->e_base + img->e_strtab
|
||||||
+ sym[sym_id].st_name;
|
+ sym[sym_id].st_name;
|
||||||
|
#if 0
|
||||||
kern_tracef(
|
kern_tracef(
|
||||||
"%s: request for symbol %u: %s",
|
"%s: request for symbol %u: %s",
|
||||||
img->e_leaf.l_name,
|
img->e_leaf.l_name,
|
||||||
sym_id,
|
sym_id,
|
||||||
sym_name);
|
sym_name);
|
||||||
|
#endif
|
||||||
#if 0
|
#if 0
|
||||||
virt_addr_t sym_addr = elf_image_find_linked_symbol(img, sym_name);
|
virt_addr_t sym_addr = elf_image_find_linked_symbol(img, sym_name);
|
||||||
#else
|
#else
|
||||||
virt_addr_t sym_addr = find_global_symbol(sym_name);
|
virt_addr_t sym_addr = find_global_symbol(sym_name);
|
||||||
#endif
|
#endif
|
||||||
kern_tracef("symbol %s = %zx", sym_name, sym_addr);
|
// kern_logf("symbol %s = %zx", sym_name, sym_addr);
|
||||||
kern_tracef("slot %s = %zx", sym_name, &got[slot_id]);
|
// kern_tracef("slot %s = %zx", sym_name, &got[slot_id]);
|
||||||
got[slot_id] = sym_addr;
|
got[slot_id] = sym_addr;
|
||||||
return sym_addr;
|
return sym_addr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#include "elf.h"
|
||||||
|
|
||||||
|
#include <magenta/config.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user