0,0 → 1,385 |
/* |
* Copyright (c) 2006 Sergey Bondari |
* Copyright (c) 2006 Jakub Jermar |
* Copyright (c) 2008 Jiri Svoboda |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
/** @addtogroup generic |
* @{ |
*/ |
|
/** |
* @file |
* @brief Kernel ELF loader. |
*/ |
|
#include <stdio.h> |
#include <sys/types.h> |
#include <align.h> |
#include <assert.h> |
#include <as.h> |
#include <unistd.h> |
#include <fcntl.h> |
|
#include "elf.h" |
#include "pcb.h" |
#include "elf_load.h" |
|
#define RTLD_BIAS 0x80000 |
//#define RTLD_BIAS 0 |
|
static char *error_codes[] = { |
"no error", |
"invalid image", |
"address space error", |
"incompatible image", |
"unsupported image type", |
"irrecoverable error" |
}; |
|
static unsigned int elf_load(int fd, elf_header_t *header); |
static int segment_header(int fd, elf_header_t *elf); |
static int section_header(int fd, elf_header_t *elf); |
static int load_segment(int fd, elf_segment_header_t *entry, elf_header_t *elf); |
|
typedef void (*entry_point_t)(void); |
|
int elf_load_file(char *file_name, elf_header_t *header) |
{ |
int fd; |
int rc; |
|
printf("open and read '%s'...\n", file_name); |
|
fd = open(file_name, 0); |
if (fd < 0) { |
printf("failed opening file\n"); |
return -1; |
} |
|
rc = elf_load(fd, header); |
printf("elf_load() -> %d\n", rc); |
|
close(fd); |
|
return rc; |
} |
|
void elf_run(elf_header_t *header) |
{ |
entry_point_t entry_point; |
|
entry_point = (entry_point_t)header->e_entry; |
(*entry_point)(); |
|
/* not reached */ |
} |
|
int elf_create_pcb(elf_header_t *header) |
{ |
pcb_t *pcb; |
void *a; |
|
pcb = (pcb_t *)PCB_ADDRESS; |
|
a = as_area_create(pcb, sizeof(pcb_t), AS_AREA_READ | AS_AREA_WRITE); |
if (a == (void *)(-1)) { |
printf("elf_create_pcb: memory mapping failed\n"); |
return EE_MEMORY; |
} |
|
pcb->entry = (entry_point_t)header->e_entry; |
|
return 0; |
} |
|
|
/** ELF loader |
* |
* @param header Pointer to ELF header in memory |
* @return EE_OK on success |
*/ |
static unsigned int elf_load(int fd, elf_header_t *header) |
{ |
int i, rc; |
|
rc = read(fd, header, sizeof(elf_header_t)); |
if (rc < 0) { |
printf("read error\n"); |
return EE_INVALID; |
} |
|
printf("ELF-load:"); |
/* Identify ELF */ |
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) { |
printf("invalid header\n"); |
return EE_INVALID; |
} |
|
/* Identify ELF compatibility */ |
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) { |
printf("incompatible data/version/class\n"); |
return EE_INCOMPATIBLE; |
} |
|
if (header->e_phentsize != sizeof(elf_segment_header_t)) |
return EE_INCOMPATIBLE; |
|
if (header->e_shentsize != sizeof(elf_section_header_t)) |
return EE_INCOMPATIBLE; |
|
/* Check if the object type is supported. */ |
if (header->e_type != ET_EXEC && header->e_type != ET_DYN) { |
printf("Object type %d is not supported\n", header->e_type); |
return EE_UNSUPPORTED; |
} |
if (header->e_type == ET_DYN) header->e_entry += RTLD_BIAS; |
|
printf("parse segments\n"); |
|
/* Walk through all segment headers and process them. */ |
for (i = 0; i < header->e_phnum; i++) { |
|
/* Seek to start of segment header */ |
lseek(fd, header->e_phoff + i * sizeof(elf_segment_header_t), |
SEEK_SET); |
|
rc = segment_header(fd, header); |
if (rc != EE_OK) |
return rc; |
} |
|
printf("parse sections\n"); |
|
/* Inspect all section headers and proccess them. */ |
for (i = 0; i < header->e_shnum; i++) { |
|
/* Seek to start of section header */ |
lseek(fd, header->e_shoff + i * sizeof(elf_section_header_t), |
SEEK_SET); |
|
rc = section_header(fd, header); |
if (rc != EE_OK) |
return rc; |
} |
|
printf("done\n"); |
|
return EE_OK; |
} |
|
/** Print error message according to error code. |
* |
* @param rc Return code returned by elf_load(). |
* |
* @return NULL terminated description of error. |
*/ |
char *elf_error(unsigned int rc) |
{ |
assert(rc < sizeof(error_codes) / sizeof(char *)); |
|
return error_codes[rc]; |
} |
|
/** Process segment header. |
* |
* @param entry Segment header. |
* @param elf ELF header. |
* |
* @return EE_OK on success, error code otherwise. |
*/ |
static int segment_header(int fd, elf_header_t *elf) |
{ |
static elf_segment_header_t entry_buf; |
elf_segment_header_t *entry = &entry_buf; |
int rc; |
|
rc = read(fd, entry, sizeof(elf_segment_header_t)); |
if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
|
switch (entry->p_type) { |
case PT_NULL: |
case PT_PHDR: |
break; |
case PT_LOAD: |
return load_segment(fd, entry, elf); |
break; |
case PT_DYNAMIC: |
case PT_INTERP: |
case PT_SHLIB: |
case PT_NOTE: |
case PT_LOPROC: |
case PT_HIPROC: |
default: |
printf("segment p_type %d unknown\n", entry->p_type); |
return EE_UNSUPPORTED; |
break; |
} |
return EE_OK; |
} |
|
/** Load segment described by program header entry. |
* |
* @param entry Program header entry describing segment to be loaded. |
* @param elf ELF header. |
* |
* @return EE_OK on success, error code otherwise. |
*/ |
int load_segment(int fd, elf_segment_header_t *entry, elf_header_t *elf) |
{ |
void *a; |
int flags = 0; |
uintptr_t bias; |
int rc; |
|
printf("load segment at addr 0x%x, size 0x%x\n", entry->p_vaddr, |
entry->p_memsz); |
|
bias = (elf->e_type == ET_DYN) ? RTLD_BIAS : 0; |
|
if (entry->p_align > 1) { |
if ((entry->p_offset % entry->p_align) != |
(entry->p_vaddr % entry->p_align)) { |
printf("align check 1 failed offset%%align=%d, vaddr%%align=%d\n", |
entry->p_offset % entry->p_align, |
entry->p_vaddr % entry->p_align |
); |
return EE_INVALID; |
} |
} |
|
/* 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; |
*/ |
/* FIXME: Kernel won't normally allow this, unless you "patch" it */ |
// flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_EXEC | AS_AREA_CACHEABLE; |
flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
|
/* |
* Check if the virtual address starts on page boundary. |
*/ |
if (ALIGN_UP(entry->p_vaddr, PAGE_SIZE) != entry->p_vaddr) { |
printf("align check 2 failed - not page-aligned\n"); |
printf("vaddr = 0x%x, should be 0x%x\n", |
entry->p_vaddr, ALIGN_UP(entry->p_vaddr, PAGE_SIZE)); |
return EE_UNSUPPORTED; |
} |
|
printf("map to p_vaddr=0x%x-0x%x...\n", entry->p_vaddr + bias, |
entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE)); |
|
a = as_area_create((uint8_t *)entry->p_vaddr + bias, |
entry->p_memsz, flags); |
if (a == (void *)(-1)) { |
printf("memory mapping failed\n"); |
return EE_MEMORY; |
} |
|
printf("as_area_create(0x%x, 0x%x, %d) -> 0x%x\n", |
entry->p_vaddr+bias, entry->p_memsz, flags, (unsigned)a); |
|
/* |
* Load segment data |
*/ |
printf("seek to %d\n", entry->p_offset); |
rc = lseek(fd, entry->p_offset, SEEK_SET); |
if (rc < 0) { printf("seek error\n"); return EE_INVALID; } |
|
printf("read 0x%x bytes to address 0x%x\n", entry->p_filesz, entry->p_vaddr+bias); |
/* rc = read(fd, (void *)(entry->p_vaddr + bias), entry->p_filesz); |
if (rc < 0) { printf("read error\n"); return EE_INVALID; }*/ |
unsigned left, now; |
uint8_t *dp; |
|
left = entry->p_filesz; |
dp = (uint8_t *)(entry->p_vaddr + bias); |
|
while (left > 0) { |
now = 4096; |
if (now > left) now=left; |
printf("read %d...", now); |
rc = read(fd, dp, now); |
if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
printf("->%d\n", rc); |
left -= now; |
dp += now; |
} |
|
return EE_OK; |
} |
|
/** Process section header. |
* |
* @param entry Segment header. |
* @param elf ELF header. |
* |
* @return EE_OK on success, error code otherwise. |
*/ |
static int section_header(int fd, elf_header_t *elf) |
{ |
static elf_section_header_t entry_buf; |
elf_section_header_t *entry = &entry_buf; |
int rc; |
|
rc = read(fd, entry, sizeof(elf_section_header_t)); |
if (rc < 0) { printf("read error\n"); return EE_INVALID; } |
|
switch (entry->sh_type) { |
case SHT_PROGBITS: |
if (entry->sh_flags & SHF_TLS) { |
/* .tdata */ |
} |
break; |
case SHT_NOBITS: |
if (entry->sh_flags & SHF_TLS) { |
/* .tbss */ |
} |
break; |
case SHT_DYNAMIC: |
printf("dynamic section found\n"); |
break; |
default: |
break; |
} |
|
return EE_OK; |
} |
|
/** @} |
*/ |