HelenOS sources
This source file includes following definitions.
- elf_load_file
- elf_load_file_name
- elf_load_module
- tls_program_header
- segment_header
- load_segment
#include <errno.h>
#include <stdio.h>
#include <vfs/vfs.h>
#include <stddef.h>
#include <stdint.h>
#include <align.h>
#include <assert.h>
#include <as.h>
#include <elf/elf.h>
#include <smc.h>
#include <loader/pcb.h>
#include <entry_point.h>
#include <str_error.h>
#include <stdlib.h>
#include <macros.h>
#include <elf/elf_load.h>
#define DPRINTF(...)
static errno_t elf_load_module(elf_ld_t *elf);
static errno_t segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
static errno_t load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
errno_t elf_load_file(int file, eld_flags_t flags, elf_finfo_t *info)
{
elf_ld_t elf;
int ofile;
errno_t rc = vfs_clone(file, -1, true, &ofile);
if (rc == EOK) {
rc = vfs_open(ofile, MODE_READ);
}
if (rc != EOK) {
return rc;
}
elf.fd = ofile;
elf.info = info;
elf.flags = flags;
rc = elf_load_module(&elf);
vfs_put(ofile);
return rc;
}
errno_t elf_load_file_name(const char *path, eld_flags_t flags, elf_finfo_t *info)
{
int file;
errno_t rc = vfs_lookup(path, 0, &file);
if (rc == EOK) {
rc = elf_load_file(file, flags, info);
vfs_put(file);
return rc;
} else {
return EIO;
}
}
static errno_t elf_load_module(elf_ld_t *elf)
{
elf_header_t header_buf;
elf_header_t *header = &header_buf;
aoff64_t pos = 0;
size_t nr;
int i;
errno_t rc;
rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t), &nr);
if (rc != EOK || nr != sizeof(elf_header_t)) {
DPRINTF("Read error.\n");
return EIO;
}
if (header->e_ident[EI_MAG0] != ELFMAG0 ||
header->e_ident[EI_MAG1] != ELFMAG1 ||
header->e_ident[EI_MAG2] != ELFMAG2 ||
header->e_ident[EI_MAG3] != ELFMAG3) {
DPRINTF("Invalid header.\n");
return EINVAL;
}
if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
header->e_machine != ELF_MACHINE ||
header->e_ident[EI_VERSION] != EV_CURRENT ||
header->e_version != EV_CURRENT ||
header->e_ident[EI_CLASS] != ELF_CLASS) {
DPRINTF("Incompatible data/version/class.\n");
return EINVAL;
}
if (header->e_phentsize != sizeof(elf_segment_header_t)) {
DPRINTF("e_phentsize: %u != %zu\n", header->e_phentsize,
sizeof(elf_segment_header_t));
return EINVAL;
}
if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
DPRINTF("Object type %d is not supported\n", header->e_type);
return ENOTSUP;
}
if (header->e_phoff == 0) {
DPRINTF("Program header table is not present!\n");
return ENOTSUP;
}
const int phdr_cap = 16;
elf_segment_header_t phdr[phdr_cap];
size_t phdr_len = header->e_phnum * header->e_phentsize;
elf->info->interp = NULL;
elf->info->dynamic = NULL;
if (phdr_len > sizeof(phdr)) {
DPRINTF("more than %d program headers\n", phdr_cap);
return ENOTSUP;
}
pos = header->e_phoff;
rc = vfs_read(elf->fd, &pos, phdr, phdr_len, &nr);
if (rc != EOK || nr != phdr_len) {
DPRINTF("Read error.\n");
return EIO;
}
uintptr_t module_base = UINTPTR_MAX;
uintptr_t module_top = 0;
uintptr_t base_offset = UINTPTR_MAX;
for (i = 0; i < header->e_phnum; i++) {
if (phdr[i].p_type != PT_LOAD)
continue;
if (module_base > phdr[i].p_vaddr) {
module_base = phdr[i].p_vaddr;
base_offset = phdr[i].p_offset;
}
module_top = max(module_top, phdr[i].p_vaddr + phdr[i].p_memsz);
}
if (base_offset != 0) {
DPRINTF("ELF headers not present in the text segment.\n");
return EINVAL;
}
if (header->e_type != ET_DYN) {
elf->bias = 0;
} else {
if (module_base != 0) {
DPRINTF("Unexpected shared object format.\n");
return EINVAL;
}
void *area = as_area_create(AS_AREA_ANY, module_top,
AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE |
AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
if (area == AS_MAP_FAILED) {
DPRINTF("Can't find suitable memory area.\n");
return ENOMEM;
}
elf->bias = (uintptr_t) area;
as_area_destroy(area);
}
for (i = 0; i < header->e_phnum; i++) {
if (phdr[i].p_type != PT_LOAD)
continue;
rc = load_segment(elf, &phdr[i]);
if (rc != EOK)
return rc;
}
void *base = (void *) module_base + elf->bias;
elf->info->base = base;
elf->info->tls.tdata = NULL;
elf->info->tls.tdata_size = 0;
elf->info->tls.tbss_size = 0;
elf->info->tls.tls_align = 1;
elf->info->interp = NULL;
elf->info->dynamic = NULL;
for (i = 0; i < header->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD)
continue;
rc = segment_header(elf, &phdr[i]);
if (rc != EOK)
return rc;
}
elf->info->entry =
(entry_point_t)((uint8_t *)header->e_entry + elf->bias);
DPRINTF("Done.\n");
return EOK;
}
static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
elf_tls_info_t *info)
{
info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
info->tdata_size = hdr->p_filesz;
info->tbss_size = hdr->p_memsz - hdr->p_filesz;
info->tls_align = hdr->p_align;
}
static errno_t segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
{
switch (entry->p_type) {
case PT_NULL:
case PT_PHDR:
case PT_NOTE:
break;
case PT_GNU_EH_FRAME:
case PT_GNU_STACK:
case PT_GNU_RELRO:
break;
case PT_INTERP:
elf->info->interp =
(void *)((uint8_t *)entry->p_vaddr + elf->bias);
if (entry->p_filesz == 0) {
DPRINTF("Zero-sized ELF interp string.\n");
return EINVAL;
}
if (elf->info->interp[entry->p_filesz - 1] != '\0') {
DPRINTF("Unterminated ELF interp string.\n");
return EINVAL;
}
DPRINTF("interpreter: \"%s\"\n", elf->info->interp);
break;
case PT_DYNAMIC:
elf->info->dynamic =
(void *)((uint8_t *)entry->p_vaddr + elf->bias);
DPRINTF("dynamic section found at %p\n",
(void *)elf->info->dynamic);
break;
case 0x70000000:
case 0x70000001:
case 0x70000002:
case 0x70000003:
break;
case PT_TLS:
tls_program_header(elf, entry, &elf->info->tls);
DPRINTF("TLS header found at %p\n",
(void *)((uint8_t *)entry->p_vaddr + elf->bias));
break;
case PT_SHLIB:
default:
DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
return ENOTSUP;
break;
}
return EOK;
}
errno_t load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
{
void *a;
int flags = 0;
uintptr_t bias;
uintptr_t base;
void *seg_ptr;
uintptr_t seg_addr;
size_t mem_sz;
aoff64_t pos;
errno_t rc;
size_t nr;
bias = elf->bias;
seg_addr = entry->p_vaddr + bias;
seg_ptr = (void *) seg_addr;
DPRINTF("Load segment v_addr=0x%zx at addr %p, size 0x%zx, flags %c%c%c\n",
entry->p_vaddr,
(void *) seg_addr,
entry->p_memsz,
(entry->p_flags & PF_R) ? 'r' : '-',
(entry->p_flags & PF_W) ? 'w' : '-',
(entry->p_flags & PF_X) ? 'x' : '-');
if (entry->p_align > 1) {
if ((entry->p_offset % entry->p_align) !=
(seg_addr % entry->p_align)) {
DPRINTF("Align check 1 failed offset%%align=0x%zx, "
"vaddr%%align=0x%zx align=0x%zx\n",
entry->p_offset % entry->p_align,
seg_addr % entry->p_align, entry->p_align);
return EINVAL;
}
}
if (entry->p_flags & PF_X)
flags |= AS_AREA_EXEC;
if (entry->p_flags & PF_W)
flags |= AS_AREA_WRITE;
if (entry->p_flags & PF_R)
flags |= AS_AREA_READ;
flags |= AS_AREA_CACHEABLE;
base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
mem_sz = entry->p_memsz + (entry->p_vaddr - base);
DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
(void *) (entry->p_vaddr + bias +
ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
a = as_area_create((uint8_t *) base + bias, mem_sz,
AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
AS_AREA_UNPAGED);
if (a == AS_MAP_FAILED) {
DPRINTF("memory mapping failed (%p, %zu)\n",
(void *) (base + bias), mem_sz);
return ENOMEM;
}
DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
(void *) (base + bias), mem_sz, flags, (void *) a);
pos = entry->p_offset;
rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
if (rc != EOK || nr != entry->p_filesz) {
DPRINTF("read error\n");
return EIO;
}
if ((elf->flags & ELDF_RW) != 0)
return EOK;
DPRINTF("as_area_change_flags(%p, %x)\n",
(uint8_t *) base + bias, flags);
rc = as_area_change_flags((uint8_t *) base + bias, flags);
if (rc != EOK) {
DPRINTF("Failed to set memory area flags.\n");
return ENOMEM;
}
if (flags & AS_AREA_EXEC) {
if (smc_coherence(seg_ptr, entry->p_filesz))
return ENOMEM;
}
return EOK;
}
HelenOS homepage, sources at GitHub