ldso: use __ehdr_start if available to locate its own ELF headers

previously, the relative load address was used as the address at which
to find the ELF headers. this only works if two conditions are met:
ldso is linked to start at a virtual address of 0, and the linker is
cooperative and includes the main ELF headers in a loadable segment.

while in practice these are always met, modern linkers provide a
__ehdr_start symbol pointing to the ELF headers, and can in principle
use the reference to this symbol as an indication that they need to be
mapped in a segment. this also should make it possible to link for a
different starting virtual address, if that's ever desirable.
This commit is contained in:
Rich Felker 2023-11-06 12:59:34 -05:00
parent 725e17ed6d
commit 5baf2d92d3
1 changed files with 3 additions and 1 deletions

View File

@ -157,6 +157,8 @@ static struct fdpic_dummy_loadmap app_dummy_loadmap;
struct debug *_dl_debug_addr = &debug;
extern weak hidden char __ehdr_start[];
extern hidden int __malloc_replaced;
hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0;
@ -1725,7 +1727,7 @@ hidden void __dls2(unsigned char *base, size_t *sp)
} else {
ldso.base = base;
}
Ehdr *ehdr = (void *)ldso.base;
Ehdr *ehdr = __ehdr_start ? (void *)__ehdr_start : (void *)ldso.base;
ldso.name = ldso.shortname = "libc.so";
ldso.phnum = ehdr->e_phnum;
ldso.phdr = laddr(&ldso, ehdr->e_phoff);