2002-07-09 16:24:59 +04:00
|
|
|
/* Contains the ELF loader */
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <Errors.h>
|
2002-07-12 02:21:56 +04:00
|
|
|
#include <kerrors.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <elf.h>
|
|
|
|
#include <vfs.h>
|
|
|
|
#include <vm.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <memheap.h>
|
|
|
|
#include <atomic.h>
|
2002-07-21 21:50:56 +04:00
|
|
|
#include <stdlib.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <arch/cpu.h>
|
|
|
|
|
|
|
|
#include <elf32.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
#define ELF_TRACE 0
|
|
|
|
#if ELF_TRACE
|
|
|
|
# define PRINT(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define PRINT(x)
|
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
struct elf_region {
|
|
|
|
region_id id;
|
|
|
|
addr start;
|
|
|
|
addr size;
|
|
|
|
long delta;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct elf_image_info {
|
|
|
|
struct elf_image_info *next;
|
2002-07-21 21:50:56 +04:00
|
|
|
char *name;
|
2002-07-09 16:24:59 +04:00
|
|
|
image_id id;
|
|
|
|
int ref_count;
|
|
|
|
void *vnode;
|
|
|
|
struct elf_region regions[2]; // describes the text and data regions
|
|
|
|
addr dynamic_ptr; // pointer to the dynamic section
|
|
|
|
struct elf_linked_image *linked_images;
|
|
|
|
|
|
|
|
struct Elf32_Ehdr *eheader;
|
|
|
|
|
|
|
|
// pointer to symbol participation data structures
|
|
|
|
char *needed;
|
|
|
|
unsigned int *symhash;
|
|
|
|
struct Elf32_Sym *syms;
|
|
|
|
char *strtab;
|
|
|
|
struct Elf32_Rel *rel;
|
|
|
|
int rel_len;
|
|
|
|
struct Elf32_Rela *rela;
|
|
|
|
int rela_len;
|
|
|
|
struct Elf32_Rel *pltrel;
|
|
|
|
int pltrel_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
// XXX TK this shall contain a list of linked images
|
|
|
|
// (don't know enough about ELF how to get this list)
|
|
|
|
typedef struct elf_linked_image {
|
|
|
|
struct elf_linked_image *next;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
} elf_linked_image;
|
|
|
|
|
|
|
|
static struct elf_image_info *kernel_images = NULL;
|
|
|
|
static struct elf_image_info *kernel_image = NULL;
|
|
|
|
static mutex image_lock;
|
|
|
|
static mutex image_load_lock;
|
|
|
|
static image_id next_image_id = 0;
|
|
|
|
|
|
|
|
#define STRING(image, offset) ((char *)(&(image)->strtab[(offset)]))
|
|
|
|
#define SYMNAME(image, sym) STRING(image, (sym)->st_name)
|
|
|
|
#define SYMBOL(image, num) ((struct Elf32_Sym *)&(image)->syms[num])
|
|
|
|
#define HASHTABSIZE(image) ((image)->symhash[0])
|
|
|
|
#define HASHBUCKETS(image) ((unsigned int *)&(image)->symhash[2])
|
|
|
|
#define HASHCHAINS(image) ((unsigned int *)&(image)->symhash[2+HASHTABSIZE(image)])
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
int
|
|
|
|
elf_lookup_symbol_address(addr address, addr *baseAddress, char *text, size_t length)
|
2002-07-21 21:50:56 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info **ptr;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
struct Elf32_Sym *sym;
|
|
|
|
struct elf_image_info *found_image;
|
|
|
|
struct Elf32_Sym *found_sym;
|
|
|
|
long found_delta;
|
|
|
|
int i,j,rv;
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("looking up %p\n",(void *)address));
|
2002-07-21 21:50:56 +04:00
|
|
|
|
|
|
|
mutex_lock(&image_lock);
|
|
|
|
|
|
|
|
found_sym = 0;
|
|
|
|
found_image = 0;
|
|
|
|
found_delta = 0x7fffffff;
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
for (ptr = &kernel_images; *ptr; ptr = &(*ptr)->next) {
|
2002-07-21 21:50:56 +04:00
|
|
|
image = *ptr;
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT((" image %p, base = %p, size = %p\n", image, (void *)image->regions[0].start, (void *)image->regions[0].size));
|
2002-07-21 21:50:56 +04:00
|
|
|
if ((address < image->regions[0].start) || (address >= (image->regions[0].start + image->regions[0].size)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PRINT((" searching...\n"));
|
|
|
|
found_image = image;
|
|
|
|
for (i = 0; i < HASHTABSIZE(image); i++) {
|
2002-08-14 01:42:53 +04:00
|
|
|
for (j = HASHBUCKETS(image)[i]; j != STN_UNDEF; j = HASHCHAINS(image)[j]) {
|
2002-07-21 21:50:56 +04:00
|
|
|
long d;
|
|
|
|
sym = &image->syms[j];
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT((" %p looking at %s, type = %d, bind = %d, addr = %p\n",sym,SYMNAME(image, sym),ELF32_ST_TYPE(sym->st_info),ELF32_ST_BIND(sym->st_info),(void *)sym->st_value));
|
|
|
|
PRINT((" symbol: %lx (%x + %lx)\n", sym->st_value + image->regions[0].delta, sym->st_value, image->regions[0].delta));
|
2002-07-21 21:50:56 +04:00
|
|
|
|
|
|
|
if ((ELF32_ST_TYPE(sym->st_info) != STT_FUNC) || (ELF32_ST_BIND(sym->st_info) != STB_GLOBAL))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d = (long)address - (long)(sym->st_value + image->regions[0].delta);
|
|
|
|
if ((d >= 0) && (d < found_delta)) {
|
|
|
|
found_delta = d;
|
|
|
|
found_sym = sym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
if (found_sym != 0) {
|
2002-07-21 21:50:56 +04:00
|
|
|
PRINT(("symbol at %p, in image %p, name = %s\n", found_sym, found_image, found_image->name));
|
|
|
|
PRINT(("name index %d, '%s'\n", found_sym->st_name, SYMNAME(found_image, found_sym)));
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("addr = %#lx, offset = %#lx\n",(found_sym->st_value + found_image->regions[0].delta),found_delta));
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
strlcpy(text, SYMNAME(found_image, found_sym), length);
|
|
|
|
|
|
|
|
if (baseAddress)
|
|
|
|
*baseAddress = found_sym->st_value + found_image->regions[0].delta;
|
|
|
|
|
2002-07-21 21:50:56 +04:00
|
|
|
rv = 0;
|
2002-08-14 01:42:53 +04:00
|
|
|
} else {
|
|
|
|
PRINT(("symbol not found!\n"));
|
|
|
|
strlcpy(text, "symbol not found", length);
|
|
|
|
rv = -1;
|
2002-07-21 21:50:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&image_lock);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
print_address_info(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char text[128];
|
|
|
|
long address;
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
if (argc < 2) {
|
2002-07-21 21:50:56 +04:00
|
|
|
dprintf("not enough arguments\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
address = atoul(argv[1]);
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
elf_lookup_symbol_address(address, NULL, text, sizeof(text));
|
2002-08-05 10:41:57 +04:00
|
|
|
dprintf("%p = %s\n",(void *)address,text);
|
2002-07-21 21:50:56 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
static void
|
|
|
|
insert_image_in_list(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
mutex_lock(&image_lock);
|
|
|
|
|
|
|
|
image->next = kernel_images;
|
|
|
|
kernel_images = image;
|
|
|
|
|
|
|
|
mutex_unlock(&image_lock);
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
remove_image_from_list(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info **ptr;
|
|
|
|
|
|
|
|
mutex_lock(&image_lock);
|
|
|
|
|
|
|
|
for(ptr = &kernel_images; *ptr; ptr = &(*ptr)->next) {
|
|
|
|
if(*ptr == image) {
|
|
|
|
*ptr = image->next;
|
|
|
|
image->next = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&image_lock);
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static struct elf_image_info *
|
|
|
|
find_image(image_id id)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
|
|
|
|
mutex_lock(&image_lock);
|
|
|
|
|
|
|
|
for(image = kernel_images; image; image = image->next) {
|
|
|
|
if(image->id == id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&image_lock);
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static struct elf_image_info *
|
|
|
|
find_image_by_vnode(void *vnode)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
|
|
|
|
mutex_lock(&image_lock);
|
|
|
|
|
|
|
|
for(image = kernel_images; image; image = image->next) {
|
|
|
|
if(image->vnode == vnode)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&image_lock);
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static struct elf_image_info *
|
|
|
|
create_image_struct()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
|
|
|
|
image = (struct elf_image_info *)kmalloc(sizeof(struct elf_image_info));
|
|
|
|
if(!image)
|
|
|
|
return NULL;
|
|
|
|
memset(image, 0, sizeof(struct elf_image_info));
|
|
|
|
image->regions[0].id = -1;
|
|
|
|
image->regions[1].id = -1;
|
|
|
|
image->id = atomic_add(&next_image_id, 1);
|
|
|
|
image->ref_count = 1;
|
|
|
|
image->linked_images = NULL;
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
elf_hash(const unsigned char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
unsigned long hash = 0;
|
|
|
|
unsigned long temp;
|
|
|
|
|
|
|
|
while(*name) {
|
|
|
|
hash = (hash << 4) + *name++;
|
|
|
|
if((temp = hash & 0xf0000000))
|
|
|
|
hash ^= temp >> 24;
|
|
|
|
hash &= ~temp;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
dump_image_info(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dprintf("elf_image_info at %p:\n", image);
|
|
|
|
dprintf(" next %p\n", image->next);
|
|
|
|
dprintf(" id 0x%x\n", image->id);
|
2002-07-18 23:21:40 +04:00
|
|
|
for (i = 0; i < 2; i++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf(" regions[%d].id 0x%x\n", i, image->regions[i].id);
|
|
|
|
dprintf(" regions[%d].start 0x%lx\n", i, image->regions[i].start);
|
|
|
|
dprintf(" regions[%d].size 0x%lx\n", i, image->regions[i].size);
|
|
|
|
dprintf(" regions[%d].delta %ld\n", i, image->regions[i].delta);
|
|
|
|
}
|
|
|
|
dprintf(" dynamic_ptr 0x%lx\n", image->dynamic_ptr);
|
|
|
|
dprintf(" needed %p\n", image->needed);
|
|
|
|
dprintf(" symhash %p\n", image->symhash);
|
|
|
|
dprintf(" syms %p\n", image->syms);
|
|
|
|
dprintf(" strtab %p\n", image->strtab);
|
|
|
|
dprintf(" rel %p\n", image->rel);
|
|
|
|
dprintf(" rel_len 0x%x\n", image->rel_len);
|
|
|
|
dprintf(" rela %p\n", image->rela);
|
|
|
|
dprintf(" rela_len 0x%x\n", image->rela_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - Currently unused
|
|
|
|
static void dump_symbol(struct elf_image_info *image, struct Elf32_Sym *sym)
|
|
|
|
{
|
|
|
|
|
|
|
|
dprintf("symbol at %p, in image %p\n", sym, image);
|
|
|
|
|
|
|
|
dprintf(" name index %d, '%s'\n", sym->st_name, SYMNAME(image, sym));
|
|
|
|
dprintf(" st_value 0x%x\n", sym->st_value);
|
|
|
|
dprintf(" st_size %d\n", sym->st_size);
|
|
|
|
dprintf(" st_info 0x%x\n", sym->st_info);
|
|
|
|
dprintf(" st_other 0x%x\n", sym->st_other);
|
|
|
|
dprintf(" st_shndx %d\n", sym->st_shndx);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
static struct Elf32_Sym *
|
|
|
|
elf_find_symbol(struct elf_image_info *image, const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-08-05 10:41:57 +04:00
|
|
|
uint32 hash;
|
|
|
|
uint32 i;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
if (!image->dynamic_ptr)
|
|
|
|
return NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
hash = elf_hash(name) % HASHTABSIZE(image);
|
2002-08-05 10:41:57 +04:00
|
|
|
for (i = HASHBUCKETS(image)[hash]; i != STN_UNDEF; i = HASHCHAINS(image)[i]) {
|
|
|
|
if (!strcmp(SYMNAME(image, &image->syms[i]), name))
|
2002-07-09 16:24:59 +04:00
|
|
|
return &image->syms[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
addr
|
|
|
|
elf_lookup_symbol(image_id id, const char *symbol)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
struct Elf32_Sym *sym;
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
PRINT(("elf_lookup_symbol: %s\n", symbol));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
image = find_image(id);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (!image)
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
sym = elf_find_symbol(image, symbol);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (!sym)
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (sym->st_shndx == SHN_UNDEF)
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("found: %lx (%x + %lx)\n", sym->st_value + image->regions[0].delta,
|
2002-07-18 23:21:40 +04:00
|
|
|
sym->st_value, image->regions[0].delta));
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return sym->st_value + image->regions[0].delta;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
elf_parse_dynamic_section(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct Elf32_Dyn *d;
|
|
|
|
int i;
|
|
|
|
int needed_offset = -1;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("top of elf_parse_dynamic_section\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
image->symhash = 0;
|
|
|
|
image->syms = 0;
|
|
|
|
image->strtab = 0;
|
|
|
|
|
|
|
|
d = (struct Elf32_Dyn *)image->dynamic_ptr;
|
2002-08-05 10:41:57 +04:00
|
|
|
if (!d)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_GENERAL;
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
for (i = 0; d[i].d_tag != DT_NULL; i++) {
|
|
|
|
switch (d[i].d_tag) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case DT_NEEDED:
|
|
|
|
needed_offset = d[i].d_un.d_ptr + image->regions[0].delta;
|
|
|
|
break;
|
|
|
|
case DT_HASH:
|
|
|
|
image->symhash = (unsigned int *)(d[i].d_un.d_ptr + image->regions[0].delta);
|
|
|
|
break;
|
|
|
|
case DT_STRTAB:
|
|
|
|
image->strtab = (char *)(d[i].d_un.d_ptr + image->regions[0].delta);
|
|
|
|
break;
|
|
|
|
case DT_SYMTAB:
|
|
|
|
image->syms = (struct Elf32_Sym *)(d[i].d_un.d_ptr + image->regions[0].delta);
|
|
|
|
break;
|
|
|
|
case DT_REL:
|
|
|
|
image->rel = (struct Elf32_Rel *)(d[i].d_un.d_ptr + image->regions[0].delta);
|
|
|
|
break;
|
|
|
|
case DT_RELSZ:
|
|
|
|
image->rel_len = d[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_RELA:
|
|
|
|
image->rela = (struct Elf32_Rela *)(d[i].d_un.d_ptr + image->regions[0].delta);
|
|
|
|
break;
|
|
|
|
case DT_RELASZ:
|
|
|
|
image->rela_len = d[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
// TK: procedure linkage table
|
|
|
|
case DT_JMPREL:
|
|
|
|
image->pltrel = (struct Elf32_Rel *)(d[i].d_un.d_ptr + image->regions[0].delta);
|
|
|
|
break;
|
|
|
|
case DT_PLTRELSZ:
|
|
|
|
image->pltrel_len = d[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets make sure we found all the required sections
|
2002-08-05 10:41:57 +04:00
|
|
|
if (!image->symhash || !image->syms || !image->strtab)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_GENERAL;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("needed_offset = %d\n", needed_offset));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
if (needed_offset >= 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
image->needed = STRING(image, needed_offset);
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
/* this function first tries to see if the first image and it's already resolved symbol is okay, otherwise
|
|
|
|
* it tries to link against the shared_image
|
|
|
|
* XXX gross hack and needs to be done better
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
elf_resolve_symbol(struct elf_image_info *image, struct Elf32_Sym *sym, struct elf_image_info *shared_image, const char *sym_prepend,addr *sym_addr)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct Elf32_Sym *sym2;
|
|
|
|
char new_symname[512];
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
switch (sym->st_shndx) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case SHN_UNDEF:
|
|
|
|
// patch the symbol name
|
|
|
|
strlcpy(new_symname, sym_prepend, sizeof(new_symname));
|
|
|
|
strlcat(new_symname, SYMNAME(image, sym), sizeof(new_symname));
|
|
|
|
|
|
|
|
// it's undefined, must be outside this image, try the other image
|
|
|
|
sym2 = elf_find_symbol(shared_image, new_symname);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (!sym2) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("!sym2: elf_resolve_symbol: could not resolve symbol '%s'\n", new_symname);
|
|
|
|
return ERR_ELF_RESOLVING_SYMBOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure they're the same type
|
2002-08-05 10:41:57 +04:00
|
|
|
if (ELF32_ST_TYPE(sym->st_info) != ELF32_ST_TYPE(sym2->st_info)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("elf_resolve_symbol: found symbol '%s' in shared image but wrong type\n", new_symname);
|
|
|
|
return ERR_ELF_RESOLVING_SYMBOL;
|
|
|
|
}
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
if (ELF32_ST_BIND(sym2->st_info) != STB_GLOBAL && ELF32_ST_BIND(sym2->st_info) != STB_WEAK) {
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("elf_resolve_symbol: found symbol '%s' but not exported\n", new_symname));
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_ELF_RESOLVING_SYMBOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sym_addr = sym2->st_value + shared_image->regions[0].delta;
|
|
|
|
return B_NO_ERROR;
|
|
|
|
case SHN_ABS:
|
|
|
|
*sym_addr = sym->st_value;
|
|
|
|
return B_NO_ERROR;
|
|
|
|
case SHN_COMMON:
|
|
|
|
// XXX finish this
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("elf_resolve_symbol: COMMON symbol, finish me!\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_NOT_IMPLEMENTED_YET;
|
|
|
|
default:
|
|
|
|
// standard symbol
|
|
|
|
*sym_addr = sym->st_value + image->regions[0].delta;
|
2002-07-18 23:21:40 +04:00
|
|
|
return B_NO_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
elf_relocate_rel(struct elf_image_info *image, const char *sym_prepend,
|
|
|
|
struct Elf32_Rel *rel, int rel_len )
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct Elf32_Sym *sym;
|
|
|
|
int vlErr;
|
|
|
|
addr S;
|
|
|
|
addr A;
|
|
|
|
addr P;
|
|
|
|
addr final_val;
|
2002-08-05 10:41:57 +04:00
|
|
|
int i;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
S = A = P = 0;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
for (i = 0; i * (int)sizeof(struct Elf32_Rel) < rel_len; i++) {
|
|
|
|
PRINT(("looking at rel type %d, offset 0x%x\n", ELF32_R_TYPE(rel[i].r_info), rel[i].r_offset));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// calc S
|
2002-08-05 10:41:57 +04:00
|
|
|
switch (ELF32_R_TYPE(rel[i].r_info)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case R_386_32:
|
|
|
|
case R_386_PC32:
|
|
|
|
case R_386_GLOB_DAT:
|
|
|
|
case R_386_JMP_SLOT:
|
|
|
|
case R_386_GOTOFF:
|
|
|
|
sym = SYMBOL(image, ELF32_R_SYM(rel[i].r_info));
|
|
|
|
|
|
|
|
vlErr = elf_resolve_symbol(image, sym, kernel_image, sym_prepend, &S);
|
|
|
|
if (vlErr < 0)
|
|
|
|
return vlErr;
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("S %p\n", (void *)S));
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
// calc A
|
2002-08-05 10:41:57 +04:00
|
|
|
switch (ELF32_R_TYPE(rel[i].r_info)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case R_386_32:
|
|
|
|
case R_386_PC32:
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_PLT32:
|
|
|
|
case R_386_RELATIVE:
|
|
|
|
case R_386_GOTOFF:
|
|
|
|
case R_386_GOTPC:
|
|
|
|
A = *(addr *)(image->regions[0].delta + rel[i].r_offset);
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("A %p\n", (void *)A));
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// calc P
|
2002-08-05 10:41:57 +04:00
|
|
|
switch (ELF32_R_TYPE(rel[i].r_info)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case R_386_PC32:
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_PLT32:
|
|
|
|
case R_386_GOTPC:
|
|
|
|
P = image->regions[0].delta + rel[i].r_offset;
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("P %p\n", (void *)P));
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
switch (ELF32_R_TYPE(rel[i].r_info)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case R_386_NONE:
|
|
|
|
continue;
|
|
|
|
case R_386_32:
|
|
|
|
final_val = S + A;
|
|
|
|
break;
|
|
|
|
case R_386_PC32:
|
|
|
|
final_val = S + A - P;
|
|
|
|
break;
|
|
|
|
case R_386_RELATIVE:
|
|
|
|
// B + A;
|
|
|
|
final_val = image->regions[0].delta + A;
|
|
|
|
break;
|
|
|
|
case R_386_JMP_SLOT:
|
|
|
|
case R_386_GLOB_DAT:
|
|
|
|
final_val = S;
|
|
|
|
break;
|
|
|
|
default:
|
2002-08-05 10:41:57 +04:00
|
|
|
dprintf("unhandled relocation type %d\n", ELF32_R_TYPE(rel[i].r_info));
|
2002-07-09 16:24:59 +04:00
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
*(addr *)(image->regions[0].delta + rel[i].r_offset) = final_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// XXX for now just link against the kernel
|
2002-07-18 23:21:40 +04:00
|
|
|
static int
|
|
|
|
elf_relocate(struct elf_image_info *image, const char *sym_prepend)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int res = B_NO_ERROR;
|
2002-07-19 00:42:52 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("top of elf_relocate\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// deal with the rels first
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->rel) {
|
|
|
|
dprintf("total %i relocs\n", image->rel_len / (int)sizeof(struct Elf32_Rel));
|
|
|
|
res = elf_relocate_rel(image, sym_prepend, image->rel, image->rel_len);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (res)
|
2002-07-09 16:24:59 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->pltrel) {
|
|
|
|
dprintf("total %i plt-relocs\n", image->pltrel_len / (int)sizeof(struct Elf32_Rel));
|
|
|
|
res = elf_relocate_rel(image, sym_prepend, image->pltrel, image->pltrel_len);
|
|
|
|
|
|
|
|
if (res)
|
2002-07-09 16:24:59 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->rela) {
|
|
|
|
#if ELF_TRACE
|
2002-07-19 00:42:52 +04:00
|
|
|
int i;
|
2002-07-09 16:24:59 +04:00
|
|
|
for(i = 1; i * (int)sizeof(struct Elf32_Rela) < image->rela_len; i++) {
|
|
|
|
dprintf("rela: type %d\n", ELF32_R_TYPE(image->rela[i].r_info));
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
#endif
|
2002-07-19 00:42:52 +04:00
|
|
|
dprintf("RELA relocations not supported\n");
|
2002-07-18 23:21:40 +04:00
|
|
|
return EPERM;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
verify_eheader(struct Elf32_Ehdr *eheader)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (memcmp(eheader->e_ident, ELF_MAGIC, 4) != 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_INVALID_BINARY;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (eheader->e_ident[4] != ELFCLASS32)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_INVALID_BINARY;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (eheader->e_phoff == 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_INVALID_BINARY;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (eheader->e_phentsize < sizeof(struct Elf32_Phdr))
|
2002-07-09 16:24:59 +04:00
|
|
|
return ERR_INVALID_BINARY;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
int
|
2002-08-03 04:41:27 +04:00
|
|
|
elf_load_uspace(const char *path, struct team *p, int flags, addr *entry)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct Elf32_Ehdr eheader;
|
|
|
|
struct Elf32_Phdr *pheaders = NULL;
|
2002-08-03 05:10:06 +04:00
|
|
|
char baseName[64];
|
2002-07-09 16:24:59 +04:00
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
ssize_t len;
|
|
|
|
|
2002-08-03 04:41:27 +04:00
|
|
|
dprintf("elf_load: entry path '%s', team %p\n", path, p);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-14 09:15:34 +04:00
|
|
|
fd = sys_open(path, 0);
|
2002-08-03 05:10:06 +04:00
|
|
|
if (fd < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return fd;
|
2002-08-03 05:10:06 +04:00
|
|
|
|
|
|
|
// read and verify the ELF header
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
len = sys_read(fd, 0, &eheader, sizeof(eheader));
|
2002-08-03 05:10:06 +04:00
|
|
|
if (len < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
err = len;
|
|
|
|
goto error;
|
|
|
|
}
|
2002-08-03 05:10:06 +04:00
|
|
|
|
|
|
|
if (len != sizeof(eheader)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// short read
|
|
|
|
err = ERR_INVALID_BINARY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
err = verify_eheader(&eheader);
|
2002-08-03 05:10:06 +04:00
|
|
|
if (err < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
// read program header
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
pheaders = (struct Elf32_Phdr *)kmalloc(eheader.e_phnum * eheader.e_phentsize);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (pheaders == NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error allocating space for program headers\n");
|
2002-07-14 14:15:14 +04:00
|
|
|
err = ENOMEM;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("reading in program headers at 0x%x, len 0x%x\n", eheader.e_phoff, eheader.e_phnum * eheader.e_phentsize);
|
2002-08-09 21:03:03 +04:00
|
|
|
len = sys_read(fd, eheader.e_phoff, pheaders, eheader.e_phnum * eheader.e_phentsize);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
err = len;
|
|
|
|
dprintf("error reading in program headers\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len != eheader.e_phnum * eheader.e_phentsize) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("short read while reading in program headers\n");
|
|
|
|
err = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
// construct a nice name for the region we have to create below
|
|
|
|
{
|
|
|
|
int32 length = strlen(path);
|
|
|
|
if (length > 52)
|
|
|
|
sprintf(baseName, "...%s", path + length - 52);
|
|
|
|
else
|
|
|
|
strcpy(baseName, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// map the program's segments into memory
|
|
|
|
|
|
|
|
for (i = 0; i < eheader.e_phnum; i++) {
|
|
|
|
char regionName[64];
|
|
|
|
char *regionAddress;
|
2002-07-09 16:24:59 +04:00
|
|
|
region_id id;
|
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
sprintf(regionName, "%s_seg%d", baseName, i);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
regionAddress = (char *)ROUNDOWN(pheaders[i].p_vaddr, PAGE_SIZE);
|
|
|
|
if (pheaders[i].p_flags & PF_W) {
|
2002-07-09 16:24:59 +04:00
|
|
|
/*
|
|
|
|
* rw segment
|
|
|
|
*/
|
2002-08-03 05:10:06 +04:00
|
|
|
uint32 memUpperBound = (pheaders[i].p_vaddr % PAGE_SIZE) + pheaders[i].p_memsz;
|
|
|
|
uint32 fileUpperBound = (pheaders[i].p_vaddr % PAGE_SIZE) + pheaders[i].p_filesz;
|
|
|
|
|
|
|
|
memUpperBound = ROUNDUP(memUpperBound, PAGE_SIZE);
|
|
|
|
fileUpperBound = ROUNDUP(fileUpperBound, PAGE_SIZE);
|
|
|
|
|
|
|
|
id = vm_map_file(p->_aspace_id, regionName,
|
|
|
|
(void **)®ionAddress,
|
2002-07-09 16:24:59 +04:00
|
|
|
REGION_ADDR_EXACT_ADDRESS,
|
2002-08-03 05:10:06 +04:00
|
|
|
fileUpperBound,
|
|
|
|
LOCK_RW, REGION_PRIVATE_MAP,
|
|
|
|
path, ROUNDOWN(pheaders[i].p_offset, PAGE_SIZE));
|
2002-07-18 23:21:40 +04:00
|
|
|
if (id < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error allocating region!\n");
|
|
|
|
err = ERR_INVALID_BINARY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
// clean garbage brought by mmap (the region behind the file,
|
|
|
|
// at least parts of it are the bss and have to be zeroed)
|
|
|
|
{
|
|
|
|
uint32 start = (uint32)regionAddress
|
|
|
|
+ (pheaders[i].p_vaddr % PAGE_SIZE)
|
|
|
|
+ pheaders[i].p_filesz;
|
|
|
|
uint32 amount = fileUpperBound
|
|
|
|
- (pheaders[i].p_vaddr % PAGE_SIZE)
|
|
|
|
- (pheaders[i].p_filesz);
|
|
|
|
memset((void *)start, 0, amount);
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
// Check if we need extra storage for the bss - we have to do this if
|
|
|
|
// the above region doesn't already comprise the memory size, too.
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
if (memUpperBound != fileUpperBound) {
|
|
|
|
size_t bss_size = memUpperBound - fileUpperBound;
|
|
|
|
|
|
|
|
sprintf(regionName, "%s_bss%d", baseName, 'X');
|
|
|
|
|
|
|
|
regionAddress += fileUpperBound;
|
|
|
|
id = vm_create_anonymous_region(p->_aspace_id, regionName,
|
|
|
|
(void **)®ionAddress,
|
2002-07-09 16:24:59 +04:00
|
|
|
REGION_ADDR_EXACT_ADDRESS,
|
|
|
|
bss_size,
|
2002-08-03 05:10:06 +04:00
|
|
|
REGION_WIRING_LAZY, LOCK_RW);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (id < 0) {
|
2002-08-03 05:10:06 +04:00
|
|
|
dprintf("error allocating bss region: %s!\n", strerror(id));
|
2002-07-09 16:24:59 +04:00
|
|
|
err = ERR_INVALID_BINARY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* assume rx segment
|
|
|
|
*/
|
2002-08-03 05:10:06 +04:00
|
|
|
id = vm_map_file(p->_aspace_id, regionName,
|
|
|
|
(void **)®ionAddress,
|
2002-07-09 16:24:59 +04:00
|
|
|
REGION_ADDR_EXACT_ADDRESS,
|
|
|
|
ROUNDUP(pheaders[i].p_memsz + (pheaders[i].p_vaddr % PAGE_SIZE), PAGE_SIZE),
|
2002-08-03 05:10:06 +04:00
|
|
|
LOCK_RO, REGION_PRIVATE_MAP,
|
|
|
|
path, ROUNDOWN(pheaders[i].p_offset, PAGE_SIZE));
|
2002-07-18 23:21:40 +04:00
|
|
|
if (id < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error mapping text!\n");
|
|
|
|
err = ERR_INVALID_BINARY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("elf_load: done!\n");
|
|
|
|
|
|
|
|
*entry = eheader.e_entry;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
error:
|
2002-08-03 05:10:06 +04:00
|
|
|
if (pheaders)
|
2002-07-09 16:24:59 +04:00
|
|
|
kfree(pheaders);
|
|
|
|
sys_close(fd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
image_id
|
|
|
|
elf_load_kspace(const char *path, const char *sym_prepend)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct Elf32_Ehdr *eheader;
|
|
|
|
struct Elf32_Phdr *pheaders;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
void *vnode = NULL;
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
ssize_t len;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("elf_load_kspace: entry path '%s'\n", path));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-14 09:15:34 +04:00
|
|
|
fd = sys_open(path, 0);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (fd < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return fd;
|
|
|
|
|
|
|
|
err = vfs_get_vnode_from_fd(fd, true, &vnode);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (err < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error0;
|
|
|
|
|
|
|
|
// XXX awful hack to keep someone else from trying to load this image
|
|
|
|
// probably not a bad thing, shouldn't be too many races
|
|
|
|
mutex_lock(&image_load_lock);
|
|
|
|
|
|
|
|
// make sure it's not loaded already. Search by vnode
|
|
|
|
image = find_image_by_vnode(vnode);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image) {
|
|
|
|
atomic_add(&image->ref_count, 1);
|
2002-07-09 16:24:59 +04:00
|
|
|
//err = ERR_NOT_ALLOWED;
|
|
|
|
goto done;
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
eheader = (struct Elf32_Ehdr *)kmalloc(sizeof(*eheader));
|
|
|
|
if (!eheader) {
|
2002-07-14 14:15:14 +04:00
|
|
|
err = ENOMEM;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
len = sys_read(fd, 0, eheader, sizeof(*eheader));
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
err = len;
|
|
|
|
goto error1;
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len != sizeof(*eheader)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// short read
|
|
|
|
err = ERR_INVALID_BINARY;
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
err = verify_eheader(eheader);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (err < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error1;
|
|
|
|
|
|
|
|
image = create_image_struct();
|
2002-07-18 23:21:40 +04:00
|
|
|
if (!image) {
|
2002-07-14 14:15:14 +04:00
|
|
|
err = ENOMEM;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
image->vnode = vnode;
|
|
|
|
image->eheader = eheader;
|
2002-07-21 21:50:56 +04:00
|
|
|
image->name = kstrdup(path);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
pheaders = (struct Elf32_Phdr *)kmalloc(eheader->e_phnum * eheader->e_phentsize);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (pheaders == NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error allocating space for program headers\n");
|
2002-07-14 14:15:14 +04:00
|
|
|
err = ENOMEM;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("reading in program headers at 0x%x, len 0x%x\n", eheader->e_phoff, eheader->e_phnum * eheader->e_phentsize));
|
2002-08-09 21:03:03 +04:00
|
|
|
len = sys_read(fd, eheader->e_phoff, pheaders, eheader->e_phnum * eheader->e_phentsize);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
err = len;
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("error reading in program headers\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error3;
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len != eheader->e_phnum * eheader->e_phentsize) {
|
|
|
|
PRINT(("short read while reading in program headers\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
err = -1;
|
|
|
|
goto error3;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
for (i = 0; i < eheader->e_phnum; i++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
char region_name[64];
|
|
|
|
bool ro_segment_handled = false;
|
|
|
|
bool rw_segment_handled = false;
|
|
|
|
int image_region;
|
|
|
|
int lock;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
PRINT(("looking at program header %d\n", i));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
switch (pheaders[i].p_type) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case PT_LOAD:
|
|
|
|
break;
|
|
|
|
case PT_DYNAMIC:
|
|
|
|
image->dynamic_ptr = pheaders[i].p_vaddr;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
dprintf("unhandled pheader type 0x%x\n", pheaders[i].p_type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're here, so it must be a PT_LOAD segment
|
2002-07-18 23:21:40 +04:00
|
|
|
if ((pheaders[i].p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_W)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// this is the writable segment
|
|
|
|
if(rw_segment_handled) {
|
|
|
|
// we've already created this segment
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rw_segment_handled = true;
|
|
|
|
image_region = 1;
|
|
|
|
lock = LOCK_RW|LOCK_KERNEL;
|
|
|
|
sprintf(region_name, "%s_rw", path);
|
2002-07-18 23:21:40 +04:00
|
|
|
} else if ((pheaders[i].p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_X)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// this is the non-writable segment
|
|
|
|
if(ro_segment_handled) {
|
|
|
|
// we've already created this segment
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ro_segment_handled = true;
|
|
|
|
image_region = 0;
|
|
|
|
// lock = LOCK_RO|LOCK_KERNEL;
|
|
|
|
lock = LOCK_RW|LOCK_KERNEL;
|
|
|
|
sprintf(region_name, "%s_ro", path);
|
|
|
|
} else {
|
|
|
|
dprintf("weird program header flags 0x%x\n", pheaders[i].p_flags);
|
|
|
|
continue;
|
|
|
|
}
|
2002-08-05 10:41:57 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
image->regions[image_region].size = ROUNDUP(pheaders[i].p_memsz + (pheaders[i].p_vaddr % PAGE_SIZE), PAGE_SIZE);
|
|
|
|
image->regions[image_region].id = vm_create_anonymous_region(vm_get_kernel_aspace_id(), region_name,
|
|
|
|
(void **)&image->regions[image_region].start, REGION_ADDR_ANY_ADDRESS,
|
|
|
|
image->regions[image_region].size, REGION_WIRING_WIRED, lock);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->regions[image_region].id < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error allocating region!\n");
|
|
|
|
err = ERR_INVALID_BINARY;
|
|
|
|
goto error3;
|
|
|
|
}
|
|
|
|
image->regions[image_region].delta = image->regions[image_region].start - ROUNDOWN(pheaders[i].p_vaddr, PAGE_SIZE);
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
PRINT(("elf_load_kspace: created a region at %p\n", (void *)image->regions[image_region].start));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
len = sys_read(fd, pheaders[i].p_offset,
|
|
|
|
(void *)(image->regions[image_region].start + (pheaders[i].p_vaddr % PAGE_SIZE)),
|
|
|
|
pheaders[i].p_filesz);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (len < 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
err = len;
|
|
|
|
dprintf("error reading in seg %d\n", i);
|
|
|
|
goto error4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->regions[1].start != 0
|
|
|
|
&& image->regions[0].delta != image->regions[1].delta) {
|
|
|
|
dprintf("could not load binary, fix the region problem!\n");
|
|
|
|
dump_image_info(image);
|
|
|
|
err = ENOMEM;
|
|
|
|
goto error4;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// modify the dynamic ptr by the delta of the regions
|
|
|
|
image->dynamic_ptr += image->regions[0].delta;
|
|
|
|
|
|
|
|
err = elf_parse_dynamic_section(image);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (err < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error4;
|
|
|
|
|
|
|
|
err = elf_relocate(image, sym_prepend);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (err < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error4;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
kfree(pheaders);
|
|
|
|
sys_close(fd);
|
|
|
|
|
|
|
|
insert_image_in_list(image);
|
|
|
|
|
|
|
|
done:
|
|
|
|
mutex_unlock(&image_load_lock);
|
|
|
|
|
|
|
|
return image->id;
|
|
|
|
|
|
|
|
error4:
|
2002-08-05 10:41:57 +04:00
|
|
|
if (image->regions[1].id >= 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
vm_delete_region(vm_get_kernel_aspace_id(), image->regions[1].id);
|
2002-08-05 10:41:57 +04:00
|
|
|
if (image->regions[0].id >= 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
vm_delete_region(vm_get_kernel_aspace_id(), image->regions[0].id);
|
|
|
|
error3:
|
|
|
|
kfree(image);
|
|
|
|
error2:
|
|
|
|
kfree(pheaders);
|
|
|
|
error1:
|
|
|
|
kfree(eheader);
|
|
|
|
error:
|
|
|
|
mutex_unlock(&image_load_lock);
|
|
|
|
error0:
|
2002-08-05 10:41:57 +04:00
|
|
|
if (vnode)
|
2002-07-09 16:24:59 +04:00
|
|
|
vfs_put_vnode_ptr(vnode);
|
|
|
|
sys_close(fd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
static int elf_unload_image(struct elf_image_info *image);
|
|
|
|
static int
|
|
|
|
elf_unlink_relocs(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
elf_linked_image *link, *next_link;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
for (link = image->linked_images; link; link = next_link) {
|
2002-07-09 16:24:59 +04:00
|
|
|
next_link = link->next;
|
|
|
|
elf_unload_image( link->image );
|
|
|
|
kfree( link );
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
elf_unload_image_final(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
vm_delete_region(vm_get_kernel_aspace_id(), image->regions[i].id);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->vnode)
|
|
|
|
vfs_put_vnode_ptr(image->vnode);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
remove_image_from_list(image);
|
2002-07-18 23:21:40 +04:00
|
|
|
kfree(image->eheader);
|
2002-07-21 21:50:56 +04:00
|
|
|
kfree(image->name);
|
2002-07-18 23:21:40 +04:00
|
|
|
kfree(image);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
elf_unload_image(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (atomic_add(&image->ref_count, -1) > 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_ERROR;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
elf_unlink_relocs(image);
|
|
|
|
elf_unload_image_final(image);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
elf_unload_kspace(const char *path)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
void *vnode;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
|
2002-07-14 09:15:34 +04:00
|
|
|
fd = sys_open(path, 0);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (fd < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return fd;
|
|
|
|
|
|
|
|
err = vfs_get_vnode_from_fd(fd, true, &vnode);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (err < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error0;
|
|
|
|
|
|
|
|
mutex_lock(&image_load_lock);
|
|
|
|
|
|
|
|
image = find_image_by_vnode(vnode);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (!image) {
|
|
|
|
dprintf("Tried to unload image that wasn't loaded (%s)\n", path);
|
2002-07-09 16:24:59 +04:00
|
|
|
err = ERR_NOT_FOUND;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
err = elf_unload_image(image);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
error:
|
|
|
|
mutex_unlock(&image_load_lock);
|
|
|
|
error0:
|
|
|
|
if(vnode)
|
|
|
|
vfs_put_vnode_ptr(vnode);
|
|
|
|
sys_close(fd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
elf_init(kernel_args *ka)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
vm_region_info rinfo;
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
mutex_init(&image_lock, "kimages_lock");
|
|
|
|
mutex_init(&image_load_lock, "kimages_load_lock");
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// build a image structure for the kernel, which has already been loaded
|
|
|
|
kernel_image = create_image_struct();
|
2002-07-21 21:50:56 +04:00
|
|
|
|
|
|
|
kernel_image->name = kstrdup("kernel");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// text segment
|
|
|
|
kernel_image->regions[0].id = vm_find_region_by_name(vm_get_kernel_aspace_id(), "kernel_ro");
|
|
|
|
if(kernel_image->regions[0].id < 0)
|
|
|
|
panic("elf_init: could not look up kernel text segment region\n");
|
|
|
|
vm_get_region_info(kernel_image->regions[0].id, &rinfo);
|
|
|
|
kernel_image->regions[0].start = rinfo.base;
|
|
|
|
kernel_image->regions[0].size = rinfo.size;
|
|
|
|
|
|
|
|
// data segment
|
|
|
|
kernel_image->regions[1].id = vm_find_region_by_name(vm_get_kernel_aspace_id(), "kernel_rw");
|
|
|
|
if(kernel_image->regions[1].id < 0)
|
|
|
|
panic("elf_init: could not look up kernel data segment region\n");
|
|
|
|
vm_get_region_info(kernel_image->regions[1].id, &rinfo);
|
|
|
|
kernel_image->regions[1].start = rinfo.base;
|
|
|
|
kernel_image->regions[1].size = rinfo.size;
|
|
|
|
|
|
|
|
// we know where the dynamic section is
|
|
|
|
kernel_image->dynamic_ptr = (addr)ka->kernel_dynamic_section_addr.start;
|
|
|
|
|
|
|
|
// parse the dynamic section
|
2002-07-18 23:21:40 +04:00
|
|
|
if (elf_parse_dynamic_section(kernel_image) < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("elf_init: WARNING elf_parse_dynamic_section couldn't find dynamic section.\n");
|
|
|
|
|
|
|
|
// insert it first in the list of kernel images loaded
|
|
|
|
kernel_images = NULL;
|
|
|
|
insert_image_in_list(kernel_image);
|
|
|
|
|
2002-07-21 21:50:56 +04:00
|
|
|
add_debugger_command("ls", &print_address_info, "lookup symbol for a particular address");
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|