2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2008-03-04 15:36:53 +03:00
|
|
|
* Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
2005-01-19 05:59:00 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-10-20 04:19:38 +04:00
|
|
|
/* Contains the ELF loader */
|
2004-04-27 15:55:31 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <elf.h>
|
2007-09-27 16:21:33 +04:00
|
|
|
|
|
|
|
#include <OS.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <debug.h>
|
2008-01-19 19:33:03 +03:00
|
|
|
#include <kernel.h>
|
2004-05-11 19:04:36 +04:00
|
|
|
#include <kimage.h>
|
2004-07-02 06:56:06 +04:00
|
|
|
#include <syscalls.h>
|
2007-09-27 16:21:33 +04:00
|
|
|
#include <team.h>
|
|
|
|
#include <thread.h>
|
2008-01-19 19:33:03 +03:00
|
|
|
#include <runtime_loader.h>
|
2007-09-27 16:21:33 +04:00
|
|
|
#include <util/khash.h>
|
|
|
|
#include <vfs.h>
|
|
|
|
#include <vm.h>
|
|
|
|
#include <vm_address_space.h>
|
2008-01-19 19:33:03 +03:00
|
|
|
#include <vm_types.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <arch/cpu.h>
|
2002-12-30 16:33:22 +03:00
|
|
|
#include <arch/elf.h>
|
|
|
|
#include <elf_priv.h>
|
2004-04-27 15:55:31 +04:00
|
|
|
#include <boot/elf.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
//#define TRACE_ELF
|
|
|
|
#ifdef TRACE_ELF
|
2002-12-30 16:33:22 +03:00
|
|
|
# define TRACE(x) dprintf x
|
2002-07-18 23:21:40 +04:00
|
|
|
#else
|
2002-12-30 16:33:22 +03:00
|
|
|
# define TRACE(x) ;
|
2002-07-18 23:21:40 +04:00
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
// ToDo: this shall contain a list of linked images (one day)
|
|
|
|
// this is a preparation for shared libraries in the kernel
|
|
|
|
// and not yet used.
|
|
|
|
#if 0
|
2002-07-09 16:24:59 +04:00
|
|
|
typedef struct elf_linked_image {
|
|
|
|
struct elf_linked_image *next;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
} elf_linked_image;
|
2004-05-11 21:29:19 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define IMAGE_HASH_SIZE 16
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
static hash_table *sImagesHash;
|
|
|
|
|
|
|
|
static struct elf_image_info *sKernelImage = NULL;
|
2007-04-01 03:06:09 +04:00
|
|
|
static mutex sImageMutex; // guards sImagesHash
|
|
|
|
static mutex sImageLoadMutex; // serializes loading/unloading add-ons
|
|
|
|
// locking order sImageLoadMutex -> sImageMutex
|
2005-04-04 17:40:10 +04:00
|
|
|
static bool sInitialized = false;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
/*! Calculates hash for an image using its ID */
|
2004-05-11 21:18:02 +04:00
|
|
|
static uint32
|
|
|
|
image_hash(void *_image, const void *_key, uint32 range)
|
|
|
|
{
|
|
|
|
struct elf_image_info *image = (struct elf_image_info *)_image;
|
|
|
|
image_id id = (image_id)_key;
|
|
|
|
|
|
|
|
if (image != NULL)
|
|
|
|
return image->id % range;
|
|
|
|
|
2005-08-26 06:02:33 +04:00
|
|
|
return (uint32)id % range;
|
2004-05-11 21:18:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
/*! Compares an image to a given ID */
|
2004-05-11 21:18:02 +04:00
|
|
|
static int
|
|
|
|
image_compare(void *_image, const void *_key)
|
|
|
|
{
|
|
|
|
struct elf_image_info *image = (struct elf_image_info *)_image;
|
|
|
|
image_id id = (image_id)_key;
|
|
|
|
|
|
|
|
return id - image->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
unregister_elf_image(struct elf_image_info *image)
|
|
|
|
{
|
2004-05-11 21:30:18 +04:00
|
|
|
unregister_image(team_get_kernel_team(), image->id);
|
2004-05-11 21:18:02 +04:00
|
|
|
hash_remove(sImagesHash, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
register_elf_image(struct elf_image_info *image)
|
|
|
|
{
|
|
|
|
image_info imageInfo;
|
|
|
|
|
|
|
|
memset(&imageInfo, 0, sizeof(image_info));
|
|
|
|
imageInfo.id = image->id;
|
|
|
|
imageInfo.type = B_SYSTEM_IMAGE;
|
|
|
|
strlcpy(imageInfo.name, image->name, sizeof(imageInfo.name));
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
imageInfo.text = (void *)image->text_region.start;
|
|
|
|
imageInfo.text_size = image->text_region.size;
|
|
|
|
imageInfo.data = (void *)image->data_region.start;
|
|
|
|
imageInfo.data_size = image->data_region.size;
|
2004-05-11 21:18:02 +04:00
|
|
|
|
2004-06-21 04:10:55 +04:00
|
|
|
image->id = register_image(team_get_kernel_team(), &imageInfo, sizeof(image_info));
|
2004-05-11 21:18:02 +04:00
|
|
|
hash_insert(sImagesHash, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
/*! Note, you must lock the image mutex when you call this function. */
|
2004-06-01 01:47:34 +04:00
|
|
|
static struct elf_image_info *
|
|
|
|
find_image_at_address(addr_t address)
|
|
|
|
{
|
|
|
|
struct hash_iterator iterator;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
|
|
|
|
ASSERT_LOCKED_MUTEX(&sImageMutex);
|
|
|
|
|
|
|
|
hash_open(sImagesHash, &iterator);
|
|
|
|
|
|
|
|
// get image that may contain the address
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
while ((image = (elf_image_info *)hash_next(sImagesHash, &iterator)) != NULL) {
|
2004-06-26 06:15:48 +04:00
|
|
|
if ((address >= image->text_region.start
|
|
|
|
&& address <= (image->text_region.start + image->text_region.size))
|
|
|
|
|| (address >= image->data_region.start
|
|
|
|
&& address <= (image->data_region.start + image->data_region.size)))
|
2004-06-01 01:47:34 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_close(sImagesHash, &iterator, false);
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-04 15:36:53 +03:00
|
|
|
static int
|
2004-06-01 01:47:34 +04:00
|
|
|
dump_address_info(int argc, char **argv)
|
2002-07-21 21:50:56 +04:00
|
|
|
{
|
2004-06-01 01:47:34 +04:00
|
|
|
const char *symbol, *imageName;
|
|
|
|
bool exactMatch;
|
2004-06-26 07:08:36 +04:00
|
|
|
addr_t address, baseAddress;
|
2002-07-21 21:50:56 +04:00
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
if (argc < 2) {
|
2005-08-23 00:35:42 +04:00
|
|
|
kprintf("usage: ls <address>\n");
|
2002-07-21 21:50:56 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
address = strtoul(argv[1], NULL, 16);
|
|
|
|
|
2005-08-23 00:35:42 +04:00
|
|
|
if (elf_debug_lookup_symbol_address(address, &baseAddress, &symbol,
|
|
|
|
&imageName, &exactMatch) == B_OK) {
|
|
|
|
kprintf("%p = %s + 0x%lx (%s)%s\n", (void *)address, symbol,
|
|
|
|
address - baseAddress, imageName, exactMatch ? "" : " (nearest)");
|
2004-06-26 07:08:36 +04:00
|
|
|
} else
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("There is no image loaded at this address!\n");
|
2002-08-14 01:42:53 +04:00
|
|
|
|
2002-07-21 21:50:56 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2006-03-26 19:58:43 +04:00
|
|
|
return (elf_image_info *)hash_lookup(sImagesHash, (void *)id);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-05-11 21:18:02 +04:00
|
|
|
struct hash_iterator iterator;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct elf_image_info *image;
|
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
mutex_lock(&sImageMutex);
|
2004-05-11 21:18:02 +04:00
|
|
|
hash_open(sImagesHash, &iterator);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
while ((image = (elf_image_info *)hash_next(sImagesHash, &iterator)) != NULL) {
|
2002-10-30 02:07:06 +03:00
|
|
|
if (image->vnode == vnode)
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-05-11 21:18:02 +04:00
|
|
|
|
|
|
|
hash_close(sImagesHash, &iterator, false);
|
2004-05-11 21:29:19 +04:00
|
|
|
mutex_unlock(&sImageMutex);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2004-05-11 18:22:00 +04:00
|
|
|
struct elf_image_info *image = (struct elf_image_info *)malloc(sizeof(struct elf_image_info));
|
|
|
|
if (image == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
2004-05-11 18:22:00 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
memset(image, 0, sizeof(struct elf_image_info));
|
2004-05-11 18:22:00 +04:00
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
image->text_region.id = -1;
|
|
|
|
image->data_region.id = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
image->ref_count = 1;
|
2004-05-11 18:22:00 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
static uint32
|
2006-01-08 15:39:06 +03:00
|
|
|
elf_hash(const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2006-01-08 15:39:06 +03:00
|
|
|
uint32 hash = 0;
|
|
|
|
uint32 temp;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
while (*name) {
|
2006-01-08 15:39:06 +03:00
|
|
|
hash = (hash << 4) + (uint8)*name++;
|
|
|
|
if ((temp = hash & 0xf0000000) != 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
hash ^= temp >> 24;
|
|
|
|
hash &= ~temp;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
static const char *
|
|
|
|
get_symbol_type_string(struct Elf32_Sym *symbol)
|
|
|
|
{
|
|
|
|
switch (ELF32_ST_TYPE(symbol->st_info)) {
|
|
|
|
case STT_FUNC:
|
|
|
|
return "func";
|
|
|
|
case STT_OBJECT:
|
|
|
|
return " obj";
|
|
|
|
case STT_FILE:
|
|
|
|
return "file";
|
|
|
|
default:
|
|
|
|
return "----";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_symbol_bind_string(struct Elf32_Sym *symbol)
|
|
|
|
{
|
|
|
|
switch (ELF32_ST_BIND(symbol->st_info)) {
|
|
|
|
case STB_LOCAL:
|
|
|
|
return "loc ";
|
|
|
|
case STB_GLOBAL:
|
|
|
|
return "glob";
|
|
|
|
case STB_WEAK:
|
|
|
|
return "weak";
|
|
|
|
default:
|
|
|
|
return "----";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
dump_symbols(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct elf_image_info *image = NULL;
|
|
|
|
struct hash_iterator iterator;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
// if the argument looks like a hex number, treat it as such
|
|
|
|
if (argc > 1) {
|
|
|
|
if (isdigit(argv[1][0])) {
|
|
|
|
uint32 num = strtoul(argv[1], NULL, 0);
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
if (IS_KERNEL_ADDRESS(num)) {
|
|
|
|
// find image at address
|
|
|
|
|
|
|
|
hash_open(sImagesHash, &iterator);
|
2006-03-26 19:58:43 +04:00
|
|
|
while ((image = (elf_image_info *)hash_next(sImagesHash, &iterator)) != NULL) {
|
2004-06-09 06:55:54 +04:00
|
|
|
if (image->text_region.start <= num
|
|
|
|
&& image->text_region.start + image->text_region.size >= num)
|
2004-06-01 01:47:34 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
hash_close(sImagesHash, &iterator, false);
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
if (image == NULL)
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("No image covers 0x%lx in the kernel!\n", num);
|
2004-06-01 01:47:34 +04:00
|
|
|
} else {
|
2006-03-26 19:58:43 +04:00
|
|
|
image = (elf_image_info *)hash_lookup(sImagesHash, (void *)num);
|
2004-06-01 01:47:34 +04:00
|
|
|
if (image == NULL)
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("image 0x%lx doesn't exist in the kernel!\n", num);
|
2004-06-01 01:47:34 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// look for image by name
|
|
|
|
hash_open(sImagesHash, &iterator);
|
2006-03-26 19:58:43 +04:00
|
|
|
while ((image = (elf_image_info *)hash_next(sImagesHash, &iterator)) != NULL) {
|
2004-06-01 01:47:34 +04:00
|
|
|
if (!strcmp(image->name, argv[1]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hash_close(sImagesHash, &iterator, false);
|
|
|
|
|
|
|
|
if (image == NULL)
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("No image \"%s\" found in kernel!\n", argv[1]);
|
2004-06-01 01:47:34 +04:00
|
|
|
}
|
|
|
|
} else {
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("usage: %s image_name/image_id/address_in_image\n", argv[0]);
|
2004-06-01 01:47:34 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// dump symbols
|
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("Symbols of image %ld \"%s\":\nAddress Type Size Name\n", image->id, image->name);
|
2004-06-01 01:47:34 +04:00
|
|
|
|
|
|
|
if (image->num_debug_symbols > 0) {
|
|
|
|
// search extended debug symbol table (contains static symbols)
|
|
|
|
for (i = 0; i < image->num_debug_symbols; i++) {
|
|
|
|
struct Elf32_Sym *symbol = &image->debug_symbols[i];
|
|
|
|
|
2004-06-01 04:35:55 +04:00
|
|
|
if (symbol->st_value == 0
|
2004-06-09 06:55:54 +04:00
|
|
|
|| symbol->st_size >= image->text_region.size + image->data_region.size)
|
2004-06-01 04:27:15 +04:00
|
|
|
continue;
|
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("%08lx %s/%s %5ld %s\n", symbol->st_value + image->text_region.delta,
|
2004-06-01 01:47:34 +04:00
|
|
|
get_symbol_type_string(symbol), get_symbol_bind_string(symbol), symbol->st_size,
|
|
|
|
image->debug_string_table + symbol->st_name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int32 j;
|
|
|
|
|
|
|
|
// search standard symbol lookup table
|
|
|
|
for (i = 0; i < HASHTABSIZE(image); i++) {
|
|
|
|
for (j = HASHBUCKETS(image)[i]; j != STN_UNDEF; j = HASHCHAINS(image)[j]) {
|
|
|
|
struct Elf32_Sym *symbol = &image->syms[j];
|
|
|
|
|
2004-06-01 04:35:55 +04:00
|
|
|
if (symbol->st_value == 0
|
2004-06-09 06:55:54 +04:00
|
|
|
|| symbol->st_size >= image->text_region.size + image->data_region.size)
|
2004-06-01 04:27:15 +04:00
|
|
|
continue;
|
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("%08lx %s/%s %5ld %s\n", symbol->st_value + image->text_region.delta,
|
2004-06-01 01:47:34 +04:00
|
|
|
get_symbol_type_string(symbol), get_symbol_bind_string(symbol),
|
|
|
|
symbol->st_size, SYMNAME(image, symbol));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
static void
|
2004-06-09 06:55:54 +04:00
|
|
|
dump_elf_region(struct elf_region *region, const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf(" %s.id 0x%lx\n", name, region->id);
|
|
|
|
kprintf(" %s.start 0x%lx\n", name, region->start);
|
|
|
|
kprintf(" %s.size 0x%lx\n", name, region->size);
|
|
|
|
kprintf(" %s.delta %ld\n", name, region->delta);
|
2004-06-09 06:55:54 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
dump_image_info(struct elf_image_info *image)
|
|
|
|
{
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("elf_image_info at %p:\n", image);
|
|
|
|
kprintf(" next %p\n", image->next);
|
|
|
|
kprintf(" id 0x%lx\n", image->id);
|
2004-06-09 06:55:54 +04:00
|
|
|
dump_elf_region(&image->text_region, "text");
|
|
|
|
dump_elf_region(&image->data_region, "data");
|
2006-03-26 19:58:43 +04:00
|
|
|
kprintf(" dynamic_section 0x%lx\n", image->dynamic_section);
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf(" needed %p\n", image->needed);
|
|
|
|
kprintf(" symhash %p\n", image->symhash);
|
|
|
|
kprintf(" syms %p\n", image->syms);
|
|
|
|
kprintf(" strtab %p\n", image->strtab);
|
|
|
|
kprintf(" rel %p\n", image->rel);
|
|
|
|
kprintf(" rel_len 0x%x\n", image->rel_len);
|
|
|
|
kprintf(" rela %p\n", image->rela);
|
|
|
|
kprintf(" rela_len 0x%x\n", image->rela_len);
|
|
|
|
kprintf(" pltrel %p\n", image->pltrel);
|
|
|
|
kprintf(" pltrel_len 0x%x\n", image->pltrel_len);
|
2008-01-04 19:59:30 +03:00
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf(" debug_symbols %p (%ld)\n", image->debug_symbols, image->num_debug_symbols);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-05-11 21:18:02 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
dump_image(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct hash_iterator iterator;
|
|
|
|
struct elf_image_info *image;
|
|
|
|
|
|
|
|
// if the argument looks like a hex number, treat it as such
|
|
|
|
if (argc > 1) {
|
|
|
|
uint32 num = strtoul(argv[1], NULL, 0);
|
|
|
|
|
|
|
|
if (IS_KERNEL_ADDRESS(num)) {
|
|
|
|
// semi-hack
|
|
|
|
dump_image_info((struct elf_image_info *)num);
|
|
|
|
} else {
|
2006-03-26 19:58:43 +04:00
|
|
|
image = (elf_image_info *)hash_lookup(sImagesHash, (void *)num);
|
2004-05-11 21:18:02 +04:00
|
|
|
if (image == NULL)
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("image 0x%lx doesn't exist in the kernel!\n", num);
|
2004-05-11 21:18:02 +04:00
|
|
|
else
|
|
|
|
dump_image_info(image);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("loaded kernel images:\n");
|
2004-05-11 21:18:02 +04:00
|
|
|
|
|
|
|
hash_open(sImagesHash, &iterator);
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
while ((image = (elf_image_info *)hash_next(sImagesHash, &iterator)) != NULL) {
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("%p (%ld) %s\n", image, image->id, image->name);
|
2004-05-11 21:18:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hash_close(sImagesHash, &iterator, false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
// Currently unused
|
|
|
|
#if 0
|
|
|
|
static
|
|
|
|
void dump_symbol(struct elf_image_info *image, struct Elf32_Sym *sym)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf("symbol at %p, in image %p\n", sym, image);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-08-02 20:28:20 +04:00
|
|
|
kprintf(" name index %d, '%s'\n", sym->st_name, SYMNAME(image, sym));
|
|
|
|
kprintf(" st_value 0x%x\n", sym->st_value);
|
|
|
|
kprintf(" st_size %d\n", sym->st_size);
|
|
|
|
kprintf(" st_info 0x%x\n", sym->st_info);
|
|
|
|
kprintf(" st_other 0x%x\n", sym->st_other);
|
|
|
|
kprintf(" st_shndx %d\n", sym->st_shndx);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-05-11 21:29:19 +04:00
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
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
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (!image->dynamic_section)
|
2002-08-05 10:41:57 +04:00
|
|
|
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
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
static status_t
|
2002-07-18 23:21:40 +04:00
|
|
|
elf_parse_dynamic_section(struct elf_image_info *image)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct Elf32_Dyn *d;
|
2006-03-26 19:58:43 +04:00
|
|
|
int32 neededOffset = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
TRACE(("top of elf_parse_dynamic_section\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
image->symhash = 0;
|
|
|
|
image->syms = 0;
|
|
|
|
image->strtab = 0;
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
d = (struct Elf32_Dyn *)image->dynamic_section;
|
2002-08-05 10:41:57 +04:00
|
|
|
if (!d)
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
for (int32 i = 0; d[i].d_tag != DT_NULL; i++) {
|
2002-08-05 10:41:57 +04:00
|
|
|
switch (d[i].d_tag) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case DT_NEEDED:
|
2006-03-26 19:58:43 +04:00
|
|
|
neededOffset = d[i].d_un.d_ptr + image->text_region.delta;
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_HASH:
|
2008-01-06 17:54:57 +03:00
|
|
|
image->symhash = (uint32 *)(d[i].d_un.d_ptr
|
|
|
|
+ image->text_region.delta);
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_STRTAB:
|
2008-01-06 17:54:57 +03:00
|
|
|
image->strtab = (char *)(d[i].d_un.d_ptr
|
|
|
|
+ image->text_region.delta);
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_SYMTAB:
|
2008-01-06 17:54:57 +03:00
|
|
|
image->syms = (struct Elf32_Sym *)(d[i].d_un.d_ptr
|
|
|
|
+ image->text_region.delta);
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_REL:
|
2008-01-06 17:54:57 +03:00
|
|
|
image->rel = (struct Elf32_Rel *)(d[i].d_un.d_ptr
|
|
|
|
+ image->text_region.delta);
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_RELSZ:
|
|
|
|
image->rel_len = d[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_RELA:
|
2008-01-06 17:54:57 +03:00
|
|
|
image->rela = (struct Elf32_Rela *)(d[i].d_un.d_ptr
|
|
|
|
+ image->text_region.delta);
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_RELASZ:
|
|
|
|
image->rela_len = d[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_JMPREL:
|
2008-01-06 17:54:57 +03:00
|
|
|
image->pltrel = (struct Elf32_Rel *)(d[i].d_un.d_ptr
|
|
|
|
+ image->text_region.delta);
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
case DT_PLTRELSZ:
|
|
|
|
image->pltrel_len = d[i].d_un.d_val;
|
|
|
|
break;
|
2002-12-30 16:33:22 +03:00
|
|
|
case DT_PLTREL:
|
|
|
|
image->pltrel_type = d[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
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)
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
TRACE(("needed_offset = %ld\n", neededOffset));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (neededOffset >= 0)
|
|
|
|
image->needed = STRING(image, neededOffset);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
/*! Resolves the \a symbol by linking against \a sharedImage if necessary.
|
|
|
|
Returns the resolved symbol's address in \a _symbolAddress.
|
|
|
|
TODO: eventually get rid of "symbolPrepend"
|
|
|
|
*/
|
2004-04-27 15:55:31 +04:00
|
|
|
status_t
|
2008-01-06 17:54:57 +03:00
|
|
|
elf_resolve_symbol(struct elf_image_info *image, struct Elf32_Sym *symbol,
|
|
|
|
struct elf_image_info *sharedImage, const char *symbolPrepend,
|
|
|
|
addr_t *_symbolAddress)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2008-01-06 17:54:57 +03:00
|
|
|
switch (symbol->st_shndx) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case SHN_UNDEF:
|
2008-01-06 17:54:57 +03:00
|
|
|
{
|
|
|
|
struct Elf32_Sym *newSymbol;
|
|
|
|
char newNameBuffer[368];
|
|
|
|
char *newName;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// patch the symbol name
|
2008-01-06 17:54:57 +03:00
|
|
|
if (symbolPrepend) {
|
|
|
|
newName = newNameBuffer;
|
|
|
|
strlcpy(newName, symbolPrepend, sizeof(newName));
|
|
|
|
strlcat(newName, SYMNAME(image, symbol), sizeof(newName));
|
|
|
|
} else
|
|
|
|
newName = SYMNAME(image, symbol);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// it's undefined, must be outside this image, try the other image
|
2008-01-06 17:54:57 +03:00
|
|
|
newSymbol = elf_find_symbol(sharedImage, newName);
|
|
|
|
if (newSymbol == NULL) {
|
2005-05-29 16:53:56 +04:00
|
|
|
dprintf("\"%s\": could not resolve symbol '%s'\n",
|
2008-01-06 17:54:57 +03:00
|
|
|
image->name, newName);
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_MISSING_SYMBOL;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure they're the same type
|
2008-01-06 17:54:57 +03:00
|
|
|
if (ELF32_ST_TYPE(symbol->st_info)
|
|
|
|
!= ELF32_ST_TYPE(newSymbol->st_info)) {
|
|
|
|
dprintf("elf_resolve_symbol: found symbol '%s' in shared image but wrong type\n", newName);
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_MISSING_SYMBOL;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
if (ELF32_ST_BIND(newSymbol->st_info) != STB_GLOBAL
|
|
|
|
&& ELF32_ST_BIND(newSymbol->st_info) != STB_WEAK) {
|
|
|
|
TRACE(("elf_resolve_symbol: found symbol '%s' but not exported\n", newName));
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_MISSING_SYMBOL;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
*_symbolAddress = newSymbol->st_value
|
|
|
|
+ sharedImage->text_region.delta;
|
|
|
|
return B_OK;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
case SHN_ABS:
|
2008-01-06 17:54:57 +03:00
|
|
|
*_symbolAddress = symbol->st_value;
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
case SHN_COMMON:
|
2004-04-27 15:55:31 +04:00
|
|
|
// ToDo: finish this
|
2004-05-11 21:29:19 +04:00
|
|
|
TRACE(("elf_resolve_symbol: COMMON symbol, finish me!\n"));
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_ERROR;
|
2008-01-06 17:54:57 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
default:
|
|
|
|
// standard symbol
|
2008-01-06 17:54:57 +03:00
|
|
|
*_symbolAddress = symbol->st_value + image->text_region.delta;
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
/*! Until we have shared library support, just links against the kernel */
|
2002-07-18 23:21:40 +04:00
|
|
|
static int
|
2008-01-06 17:54:57 +03:00
|
|
|
elf_relocate(struct elf_image_info *image, const char *symbolPrepend)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-30 16:33:22 +03:00
|
|
|
int status = B_NO_ERROR;
|
2002-07-19 00:42:52 +04:00
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
TRACE(("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) {
|
2008-01-06 17:54:57 +03:00
|
|
|
TRACE(("total %i relocs\n",
|
|
|
|
image->rel_len / (int)sizeof(struct Elf32_Rel)));
|
2002-12-30 16:33:22 +03:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
status = arch_elf_relocate_rel(image, symbolPrepend, sKernelImage,
|
|
|
|
image->rel, image->rel_len);
|
2002-12-30 16:33:22 +03:00
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->pltrel) {
|
2008-01-06 17:54:57 +03:00
|
|
|
TRACE(("total %i plt-relocs\n",
|
|
|
|
image->pltrel_len / (int)sizeof(struct Elf32_Rel)));
|
2002-12-30 16:33:22 +03:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
if (image->pltrel_type == DT_REL) {
|
|
|
|
status = arch_elf_relocate_rel(image, symbolPrepend, sKernelImage,
|
|
|
|
image->pltrel, image->pltrel_len);
|
|
|
|
} else {
|
|
|
|
status = arch_elf_relocate_rela(image, symbolPrepend, sKernelImage,
|
|
|
|
(struct Elf32_Rela *)image->pltrel, image->pltrel_len);
|
|
|
|
}
|
2002-12-30 16:33:22 +03:00
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
if (image->rela) {
|
2008-01-06 17:54:57 +03:00
|
|
|
status = arch_elf_relocate_rela(image, symbolPrepend, sKernelImage,
|
|
|
|
image->rela, image->rela_len);
|
2002-12-30 16:33:22 +03:00
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-12-30 16:33:22 +03:00
|
|
|
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static int
|
2006-03-26 19:58:43 +04:00
|
|
|
verify_eheader(struct Elf32_Ehdr *elfHeader)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2006-03-26 19:58:43 +04:00
|
|
|
if (memcmp(elfHeader->e_ident, ELF_MAGIC, 4) != 0)
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (elfHeader->e_ident[4] != ELFCLASS32)
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (elfHeader->e_phoff == 0)
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (elfHeader->e_phentsize < sizeof(struct Elf32_Phdr))
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
#if 0
|
|
|
|
static int
|
|
|
|
elf_unlink_relocs(struct elf_image_info *image)
|
|
|
|
{
|
|
|
|
elf_linked_image *link, *next_link;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
for (link = image->linked_images; link; link = next_link) {
|
|
|
|
next_link = link->next;
|
|
|
|
elf_unload_image(link->image);
|
|
|
|
free(link);
|
|
|
|
}
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
unload_elf_image(struct elf_image_info *image)
|
|
|
|
{
|
2008-03-04 15:36:53 +03:00
|
|
|
if (atomic_add(&image->ref_count, -1) > 1)
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
TRACE(("unload image %ld, %s\n", image->id, image->name));
|
2004-05-11 23:52:38 +04:00
|
|
|
|
|
|
|
//elf_unlink_relocs(image);
|
|
|
|
// not yet used
|
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
delete_area(image->text_region.id);
|
|
|
|
delete_area(image->data_region.id);
|
2004-05-11 23:52:38 +04:00
|
|
|
|
|
|
|
if (image->vnode)
|
2005-08-01 18:17:41 +04:00
|
|
|
vfs_put_vnode(image->vnode);
|
2004-05-11 23:52:38 +04:00
|
|
|
|
|
|
|
unregister_elf_image(image);
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
free(image->elf_header);
|
2008-03-04 15:36:53 +03:00
|
|
|
free(image->name);
|
2004-05-11 23:52:38 +04:00
|
|
|
free(image);
|
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2004-06-01 04:27:15 +04:00
|
|
|
load_elf_symbol_table(int fd, struct elf_image_info *image)
|
2004-05-11 23:52:38 +04:00
|
|
|
{
|
2006-03-26 19:58:43 +04:00
|
|
|
struct Elf32_Ehdr *elfHeader = image->elf_header;
|
2004-06-01 04:27:15 +04:00
|
|
|
struct Elf32_Sym *symbolTable = NULL;
|
|
|
|
struct Elf32_Shdr *stringHeader = NULL;
|
|
|
|
uint32 numSymbols = 0;
|
|
|
|
char *stringTable;
|
2004-05-11 23:52:38 +04:00
|
|
|
status_t status;
|
2004-06-01 04:27:15 +04:00
|
|
|
ssize_t length;
|
|
|
|
int32 i;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 04:27:15 +04:00
|
|
|
// get section headers
|
|
|
|
|
|
|
|
ssize_t size = elfHeader->e_shnum * elfHeader->e_shentsize;
|
|
|
|
struct Elf32_Shdr *sectionHeaders = (struct Elf32_Shdr *)malloc(size);
|
|
|
|
if (sectionHeaders == NULL) {
|
|
|
|
dprintf("error allocating space for section headers\n");
|
2004-05-11 23:52:38 +04:00
|
|
|
return B_NO_MEMORY;
|
2004-06-01 04:27:15 +04:00
|
|
|
}
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 04:27:15 +04:00
|
|
|
length = read_pos(fd, elfHeader->e_shoff, sectionHeaders, size);
|
|
|
|
if (length < size) {
|
|
|
|
TRACE(("error reading in program headers\n"));
|
|
|
|
status = B_ERROR;
|
2004-05-11 23:52:38 +04:00
|
|
|
goto error1;
|
2004-06-01 04:27:15 +04:00
|
|
|
}
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 04:27:15 +04:00
|
|
|
// find symbol table in section headers
|
|
|
|
|
|
|
|
for (i = 0; i < elfHeader->e_shnum; i++) {
|
|
|
|
if (sectionHeaders[i].sh_type == SHT_SYMTAB) {
|
|
|
|
stringHeader = §ionHeaders[sectionHeaders[i].sh_link];
|
|
|
|
|
|
|
|
if (stringHeader->sh_type != SHT_STRTAB) {
|
|
|
|
TRACE(("doesn't link to string table\n"));
|
|
|
|
status = B_BAD_DATA;
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read in symbol table
|
|
|
|
symbolTable = (struct Elf32_Sym *)malloc(size = sectionHeaders[i].sh_size);
|
|
|
|
if (symbolTable == NULL) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = read_pos(fd, sectionHeaders[i].sh_offset, symbolTable, size);
|
|
|
|
if (length < size) {
|
|
|
|
TRACE(("error reading in symbol table\n"));
|
|
|
|
status = B_ERROR;
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
|
|
|
|
numSymbols = size / sizeof(struct Elf32_Sym);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symbolTable == NULL) {
|
|
|
|
TRACE(("no symbol table\n"));
|
|
|
|
status = B_BAD_VALUE;
|
2004-05-11 23:52:38 +04:00
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
|
2004-06-01 04:27:15 +04:00
|
|
|
// read in string table
|
|
|
|
|
|
|
|
stringTable = (char *)malloc(size = stringHeader->sh_size);
|
|
|
|
if (stringTable == NULL) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = read_pos(fd, stringHeader->sh_offset, stringTable, size);
|
|
|
|
if (length < size) {
|
|
|
|
TRACE(("error reading in string table\n"));
|
|
|
|
status = B_ERROR;
|
|
|
|
goto error3;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(("loaded debug %ld symbols\n", numSymbols));
|
|
|
|
|
|
|
|
// insert tables into image
|
|
|
|
image->debug_symbols = symbolTable;
|
|
|
|
image->num_debug_symbols = numSymbols;
|
|
|
|
image->debug_string_table = stringTable;
|
|
|
|
|
|
|
|
free(sectionHeaders);
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
error3:
|
|
|
|
free(stringTable);
|
|
|
|
error2:
|
|
|
|
free(symbolTable);
|
|
|
|
error1:
|
|
|
|
free(sectionHeaders);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
insert_preloaded_image(struct preloaded_image *preloadedImage, bool kernel)
|
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
status = verify_eheader(&preloadedImage->elf_header);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
image = create_image_struct();
|
|
|
|
if (image == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
image->name = strdup(preloadedImage->name);
|
2006-03-26 19:58:43 +04:00
|
|
|
image->dynamic_section = preloadedImage->dynamic_section.start;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
image->text_region = preloadedImage->text_region;
|
|
|
|
image->data_region = preloadedImage->data_region;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
|
|
|
status = elf_parse_dynamic_section(image);
|
|
|
|
if (status < B_OK)
|
2004-06-01 04:27:15 +04:00
|
|
|
goto error1;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 02:51:01 +04:00
|
|
|
if (!kernel) {
|
2008-01-06 17:54:57 +03:00
|
|
|
status = elf_relocate(image, NULL);
|
2004-06-01 02:51:01 +04:00
|
|
|
if (status < B_OK)
|
2004-06-01 04:27:15 +04:00
|
|
|
goto error1;
|
2004-06-01 02:51:01 +04:00
|
|
|
} else
|
|
|
|
sKernelImage = image;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
image->debug_symbols = preloadedImage->debug_symbols;
|
|
|
|
image->num_debug_symbols = preloadedImage->num_debug_symbols;
|
|
|
|
image->debug_string_table = preloadedImage->debug_string_table;
|
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
register_elf_image(image);
|
2004-05-12 03:43:30 +04:00
|
|
|
preloadedImage->id = image->id;
|
|
|
|
// modules_init() uses this information to get the preloaded images
|
|
|
|
|
2006-11-02 23:38:30 +03:00
|
|
|
// we now no longer need to write to the text area anymore
|
|
|
|
set_area_protection(image->text_region.id,
|
|
|
|
B_KERNEL_READ_AREA | B_KERNEL_EXECUTE_AREA);
|
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
error1:
|
2004-06-01 04:27:15 +04:00
|
|
|
free(image);
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-05-12 03:43:30 +04:00
|
|
|
// clean up preloaded image resources (this image won't be used anymore)
|
|
|
|
delete_area(preloadedImage->text_region.id);
|
|
|
|
delete_area(preloadedImage->data_region.id);
|
|
|
|
preloadedImage->id = -1;
|
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
// #pragma mark - userland symbol lookup
|
|
|
|
|
|
|
|
|
|
|
|
class UserSymbolLookup {
|
|
|
|
public:
|
|
|
|
static UserSymbolLookup& Default()
|
|
|
|
{
|
|
|
|
return sLookup;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Init(struct team* team)
|
|
|
|
{
|
|
|
|
// find the runtime loader debug area
|
|
|
|
vm_area* area = team->address_space->areas;
|
|
|
|
while (area != NULL) {
|
|
|
|
if (strcmp(area->name, RUNTIME_LOADER_DEBUG_AREA_NAME) == 0)
|
|
|
|
break;
|
|
|
|
area = area->address_space_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (area == NULL)
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
// copy the runtime loader data structure
|
|
|
|
if (!_Read((runtime_loader_debug_area*)area->base, fDebugArea))
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t LookupSymbolAddress(addr_t address, addr_t *_baseAddress,
|
|
|
|
const char **_symbolName, const char **_imageName, bool *_exactMatch)
|
|
|
|
{
|
|
|
|
// Note, that this function doesn't find all symbols that we would like
|
|
|
|
// to find. E.g. static functions do not appear in the symbol table
|
|
|
|
// as function symbols, but as sections without name and size. The .symtab
|
|
|
|
// section together with the .strtab section, which apparently differ from
|
|
|
|
// the tables referred to by the .dynamic section, also contain proper names
|
|
|
|
// and sizes for those symbols. Therefore, to get completely satisfying
|
|
|
|
// results, we would need to read those tables from the shared object.
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
// get the image for the address
|
|
|
|
image_t image;
|
|
|
|
status_t error = _FindImageAtAddress(address, image);
|
|
|
|
if (error != B_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
strlcpy(fImageName, image.name, sizeof(fImageName));
|
|
|
|
|
|
|
|
// symbol hash table size
|
|
|
|
uint32 hashTabSize;
|
|
|
|
if (!_Read(image.symhash, hashTabSize))
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
// remote pointers to hash buckets and chains
|
|
|
|
const uint32* hashBuckets = image.symhash + 2;
|
|
|
|
const uint32* hashChains = image.symhash + 2 + hashTabSize;
|
|
|
|
|
|
|
|
const elf_region_t& textRegion = image.regions[0];
|
|
|
|
|
|
|
|
// search the image for the symbol
|
|
|
|
Elf32_Sym symbolFound;
|
|
|
|
addr_t deltaFound = INT_MAX;
|
|
|
|
bool exactMatch = false;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
for (uint32 i = 0; i < hashTabSize; i++) {
|
|
|
|
uint32 bucket;
|
|
|
|
if (!_Read(&hashBuckets[i], bucket))
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
for (uint32 j = bucket; j != STN_UNDEF;
|
|
|
|
_Read(&hashChains[j], j) ? 0 : j = STN_UNDEF) {
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
Elf32_Sym symbol;
|
|
|
|
if (!_Read(image.syms + j, symbol))
|
|
|
|
continue;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
// The symbol table contains not only symbols referring to functions
|
|
|
|
// and data symbols within the shared object, but also referenced
|
|
|
|
// symbols of other shared objects, as well as section and file
|
|
|
|
// references. We ignore everything but function and data symbols
|
|
|
|
// that have an st_value != 0 (0 seems to be an indication for a
|
|
|
|
// symbol defined elsewhere -- couldn't verify that in the specs
|
|
|
|
// though).
|
|
|
|
if ((ELF32_ST_TYPE(symbol.st_info) != STT_FUNC
|
|
|
|
&& ELF32_ST_TYPE(symbol.st_info) != STT_OBJECT)
|
|
|
|
|| symbol.st_value == 0
|
|
|
|
|| symbol.st_value + symbol.st_size + textRegion.delta
|
|
|
|
> textRegion.vmstart + textRegion.size) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
// skip symbols starting after the given address
|
|
|
|
addr_t symbolAddress = symbol.st_value + textRegion.delta;
|
|
|
|
if (symbolAddress > address)
|
|
|
|
continue;
|
|
|
|
addr_t symbolDelta = address - symbolAddress;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
if (symbolDelta < deltaFound) {
|
|
|
|
deltaFound = symbolDelta;
|
|
|
|
symbolFound = symbol;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
if (symbolDelta >= 0 && symbolDelta < symbol.st_size) {
|
|
|
|
// exact match
|
|
|
|
exactMatch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
if (_imageName)
|
|
|
|
*_imageName = fImageName;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
if (_symbolName) {
|
|
|
|
*_symbolName = NULL;
|
|
|
|
|
|
|
|
if (deltaFound < INT_MAX) {
|
|
|
|
if (_ReadString(image, symbolFound.st_name, fSymbolName,
|
|
|
|
sizeof(fSymbolName))) {
|
|
|
|
*_symbolName = fSymbolName;
|
|
|
|
} else {
|
|
|
|
// we can't get its name, so forget the symbol
|
|
|
|
deltaFound = INT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
if (_baseAddress) {
|
|
|
|
if (deltaFound < INT_MAX)
|
|
|
|
*_baseAddress = symbolFound.st_value + textRegion.delta;
|
|
|
|
else
|
|
|
|
*_baseAddress = textRegion.vmstart;
|
|
|
|
}
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
if (_exactMatch)
|
|
|
|
*_exactMatch = exactMatch;
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t _FindImageAtAddress(addr_t address, image_t& image)
|
|
|
|
{
|
|
|
|
image_queue_t imageQueue;
|
|
|
|
if (!_Read(fDebugArea.loaded_images, imageQueue))
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
image_t* imageAddress = imageQueue.head;
|
|
|
|
while (imageAddress != NULL) {
|
|
|
|
if (!_Read(imageAddress, image))
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
if (image.regions[0].vmstart <= address
|
|
|
|
&& address < image.regions[0].vmstart + image.regions[0].size) {
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
imageAddress = image.next;
|
|
|
|
}
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2008-01-19 19:33:03 +03:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _ReadString(const image_t& image, uint32 offset, char* buffer,
|
|
|
|
size_t bufferSize)
|
|
|
|
{
|
|
|
|
const char* address = image.strtab + offset;
|
|
|
|
|
|
|
|
if (!IS_USER_ADDRESS(address))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return user_strlcpy(buffer, address, bufferSize) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> bool _Read(const T* address, T& data);
|
|
|
|
// gcc 2.95.3 doesn't like it defined in-place
|
|
|
|
|
|
|
|
private:
|
|
|
|
runtime_loader_debug_area fDebugArea;
|
|
|
|
char fImageName[B_OS_NAME_LENGTH];
|
|
|
|
char fSymbolName[256];
|
|
|
|
static UserSymbolLookup sLookup;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
|
|
|
UserSymbolLookup::_Read(const T* address, T& data)
|
|
|
|
{
|
|
|
|
if (!IS_USER_ADDRESS(address))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return user_memcpy(&data, address, sizeof(T)) == B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UserSymbolLookup UserSymbolLookup::sLookup;
|
|
|
|
// doesn't need construction, but has an Init() method
|
|
|
|
|
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
// #pragma mark - public kernel API
|
2004-05-11 23:52:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
get_image_symbol(image_id id, const char *name, int32 sclass, void **_symbol)
|
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
struct Elf32_Sym *symbol;
|
|
|
|
status_t status = B_OK;
|
|
|
|
|
|
|
|
TRACE(("get_image_symbol(%s)\n", name));
|
|
|
|
|
|
|
|
mutex_lock(&sImageMutex);
|
|
|
|
|
|
|
|
image = find_image(id);
|
|
|
|
if (image == NULL) {
|
|
|
|
status = B_BAD_IMAGE_ID;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol = elf_find_symbol(image, name);
|
|
|
|
if (symbol == NULL || symbol->st_shndx == SHN_UNDEF) {
|
|
|
|
status = B_ENTRY_NOT_FOUND;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToDo: support the "sclass" parameter!
|
|
|
|
|
2008-03-04 15:36:53 +03:00
|
|
|
TRACE(("found: %lx (%lx + %lx)\n", symbol->st_value + image->text_region.delta,
|
2004-06-09 06:55:54 +04:00
|
|
|
symbol->st_value, image->text_region.delta));
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
*_symbol = (void *)(symbol->st_value + image->text_region.delta);
|
2004-05-11 23:52:38 +04:00
|
|
|
|
|
|
|
done:
|
|
|
|
mutex_unlock(&sImageMutex);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
// #pragma mark - kernel private API
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
/*! Looks up a symbol by address in all images loaded in kernel space.
|
|
|
|
Note, if you need to call this function outside a debugger, make
|
|
|
|
sure you fix locking and the way it returns its information, first!
|
|
|
|
*/
|
2004-05-11 23:52:38 +04:00
|
|
|
status_t
|
2005-08-23 00:35:42 +04:00
|
|
|
elf_debug_lookup_symbol_address(addr_t address, addr_t *_baseAddress,
|
|
|
|
const char **_symbolName, const char **_imageName, bool *_exactMatch)
|
2004-05-11 23:52:38 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
2004-06-01 01:47:34 +04:00
|
|
|
struct Elf32_Sym *symbolFound = NULL;
|
|
|
|
const char *symbolName = NULL;
|
|
|
|
addr_t deltaFound = INT_MAX;
|
|
|
|
bool exactMatch = false;
|
|
|
|
status_t status;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
TRACE(("looking up %p\n", (void *)address));
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2005-04-04 17:40:10 +04:00
|
|
|
if (!sInitialized)
|
|
|
|
return B_ERROR;
|
|
|
|
|
2005-08-23 00:35:42 +04:00
|
|
|
//mutex_lock(&sImageMutex);
|
2004-12-14 02:02:18 +03:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
image = find_image_at_address(address);
|
|
|
|
// get image that may contain the address
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
if (image != NULL) {
|
|
|
|
addr_t symbolDelta;
|
|
|
|
uint32 i;
|
|
|
|
int32 j;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
TRACE((" image %p, base = %p, size = %p\n", image,
|
2004-06-09 06:55:54 +04:00
|
|
|
(void *)image->text_region.start, (void *)image->text_region.size));
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
if (image->debug_symbols != NULL) {
|
|
|
|
// search extended debug symbol table (contains static symbols)
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
TRACE((" searching debug symbols...\n"));
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
for (i = 0; i < image->num_debug_symbols; i++) {
|
|
|
|
struct Elf32_Sym *symbol = &image->debug_symbols[i];
|
|
|
|
|
2004-06-01 04:35:55 +04:00
|
|
|
if (symbol->st_value == 0
|
2004-06-09 06:55:54 +04:00
|
|
|
|| symbol->st_size >= image->text_region.size + image->data_region.size)
|
2004-05-11 23:52:38 +04:00
|
|
|
continue;
|
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
symbolDelta = address - (symbol->st_value + image->text_region.delta);
|
2004-06-01 01:47:34 +04:00
|
|
|
if (symbolDelta >= 0 && symbolDelta < symbol->st_size)
|
|
|
|
exactMatch = true;
|
|
|
|
|
|
|
|
if (exactMatch || symbolDelta < deltaFound) {
|
|
|
|
deltaFound = symbolDelta;
|
|
|
|
symbolFound = symbol;
|
|
|
|
symbolName = image->debug_string_table + symbol->st_name;
|
|
|
|
|
|
|
|
if (exactMatch)
|
|
|
|
break;
|
2004-05-11 23:52:38 +04:00
|
|
|
}
|
|
|
|
}
|
2004-06-01 01:47:34 +04:00
|
|
|
} else {
|
|
|
|
// search standard symbol lookup table
|
|
|
|
|
|
|
|
TRACE((" searching standard symbols...\n"));
|
|
|
|
|
|
|
|
for (i = 0; i < HASHTABSIZE(image); i++) {
|
|
|
|
for (j = HASHBUCKETS(image)[i]; j != STN_UNDEF; j = HASHCHAINS(image)[j]) {
|
|
|
|
struct Elf32_Sym *symbol = &image->syms[j];
|
|
|
|
|
2004-06-01 04:35:55 +04:00
|
|
|
if (symbol->st_value == 0
|
2004-06-09 06:55:54 +04:00
|
|
|
|| symbol->st_size >= image->text_region.size + image->data_region.size)
|
2004-06-01 01:47:34 +04:00
|
|
|
continue;
|
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
symbolDelta = address - (long)(symbol->st_value + image->text_region.delta);
|
2004-06-01 01:47:34 +04:00
|
|
|
if (symbolDelta >= 0 && symbolDelta < symbol->st_size)
|
|
|
|
exactMatch = true;
|
|
|
|
|
|
|
|
if (exactMatch || symbolDelta < deltaFound) {
|
|
|
|
deltaFound = symbolDelta;
|
|
|
|
symbolFound = symbol;
|
|
|
|
symbolName = SYMNAME(image, symbol);
|
|
|
|
|
|
|
|
if (exactMatch)
|
2004-08-16 19:49:53 +04:00
|
|
|
goto symbol_found;
|
2004-06-01 01:47:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-11 23:52:38 +04:00
|
|
|
}
|
|
|
|
}
|
2004-08-16 19:49:53 +04:00
|
|
|
symbol_found:
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
if (symbolFound != NULL) {
|
|
|
|
if (_symbolName)
|
|
|
|
*_symbolName = symbolName;
|
|
|
|
if (_imageName)
|
|
|
|
*_imageName = image->name;
|
|
|
|
if (_baseAddress)
|
2004-06-09 06:55:54 +04:00
|
|
|
*_baseAddress = symbolFound->st_value + image->text_region.delta;
|
2004-06-01 01:47:34 +04:00
|
|
|
if (_exactMatch)
|
|
|
|
*_exactMatch = exactMatch;
|
|
|
|
|
|
|
|
status = B_OK;
|
|
|
|
} else if (image != NULL) {
|
|
|
|
TRACE(("symbol not found!\n"));
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
if (_symbolName)
|
|
|
|
*_symbolName = NULL;
|
|
|
|
if (_imageName)
|
|
|
|
*_imageName = image->name;
|
|
|
|
if (_baseAddress)
|
2004-06-09 06:55:54 +04:00
|
|
|
*_baseAddress = image->text_region.start;
|
2004-06-01 01:47:34 +04:00
|
|
|
if (_exactMatch)
|
|
|
|
*_exactMatch = false;
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
status = B_OK;
|
2004-05-11 23:52:38 +04:00
|
|
|
} else {
|
2004-06-01 01:47:34 +04:00
|
|
|
TRACE(("image not found!\n"));
|
|
|
|
status = B_ENTRY_NOT_FOUND;
|
2004-05-11 23:52:38 +04:00
|
|
|
}
|
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
// Note, theoretically, all information we return back to our caller
|
|
|
|
// would have to be locked - but since this function is only called
|
|
|
|
// from the debugger, it's safe to do it this way
|
|
|
|
|
2005-08-23 00:35:42 +04:00
|
|
|
//mutex_unlock(&sImageMutex);
|
2004-05-11 23:52:38 +04:00
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
return status;
|
2004-05-11 23:52:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-01 15:11:02 +03:00
|
|
|
/*! Tries to find a matching user symbol for the given address.
|
|
|
|
Note that the given team's address must already be in effect.
|
|
|
|
*/
|
2008-01-19 19:33:03 +03:00
|
|
|
status_t
|
|
|
|
elf_debug_lookup_user_symbol_address(struct team* team, addr_t address,
|
|
|
|
addr_t *_baseAddress, const char **_symbolName, const char **_imageName,
|
|
|
|
bool *_exactMatch)
|
|
|
|
{
|
|
|
|
UserSymbolLookup& lookup = UserSymbolLookup::Default();
|
|
|
|
status_t error = lookup.Init(team);
|
|
|
|
if (error != B_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
return lookup.LookupSymbolAddress(address, _baseAddress, _symbolName,
|
|
|
|
_imageName, _exactMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
status_t
|
2005-12-20 16:29:11 +03:00
|
|
|
elf_load_user_image(const char *path, struct team *team, int flags, addr_t *entry)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2006-03-26 19:58:43 +04:00
|
|
|
struct Elf32_Ehdr elfHeader;
|
|
|
|
struct Elf32_Phdr *programHeaders = NULL;
|
2005-12-21 19:47:46 +03:00
|
|
|
char baseName[B_OS_NAME_LENGTH];
|
2006-03-26 19:58:43 +04:00
|
|
|
status_t status;
|
|
|
|
ssize_t length;
|
2002-07-09 16:24:59 +04:00
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
|
2005-12-20 16:29:11 +03:00
|
|
|
TRACE(("elf_load: entry path '%s', team %p\n", path, team));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-02-02 09:21:31 +03:00
|
|
|
fd = _kern_open(-1, path, O_RDONLY, 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
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
length = _kern_read(fd, 0, &elfHeader, sizeof(elfHeader));
|
|
|
|
if (length < B_OK) {
|
|
|
|
status = length;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
2002-08-03 05:10:06 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (length != sizeof(elfHeader)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// short read
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
2006-03-26 19:58:43 +04:00
|
|
|
status = verify_eheader(&elfHeader);
|
|
|
|
if (status < B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
// read program header
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
programHeaders = (struct Elf32_Phdr *)malloc(elfHeader.e_phnum * elfHeader.e_phentsize);
|
|
|
|
if (programHeaders == NULL) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error allocating space for program headers\n");
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
TRACE(("reading in program headers at 0x%lx, length 0x%x\n", elfHeader.e_phoff, elfHeader.e_phnum * elfHeader.e_phentsize));
|
|
|
|
length = _kern_read(fd, elfHeader.e_phoff, programHeaders, elfHeader.e_phnum * elfHeader.e_phentsize);
|
|
|
|
if (length < B_OK) {
|
|
|
|
status = length;
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("error reading in program headers\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2006-03-26 19:58:43 +04:00
|
|
|
if (length != elfHeader.e_phnum * elfHeader.e_phentsize) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dprintf("short read while reading in program headers\n");
|
2006-03-26 19:58:43 +04:00
|
|
|
status = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-08-03 05:10:06 +04:00
|
|
|
// construct a nice name for the region we have to create below
|
|
|
|
{
|
2005-12-21 19:47:46 +03:00
|
|
|
int32 length;
|
|
|
|
|
|
|
|
const char *leaf = strrchr(path, '/');
|
|
|
|
if (leaf == NULL)
|
|
|
|
leaf = path;
|
|
|
|
else
|
|
|
|
leaf++;
|
|
|
|
|
|
|
|
length = strlen(leaf);
|
|
|
|
if (length > B_OS_NAME_LENGTH - 8)
|
|
|
|
sprintf(baseName, "...%s", leaf + length + 8 - B_OS_NAME_LENGTH);
|
2002-08-03 05:10:06 +04:00
|
|
|
else
|
2005-12-21 19:47:46 +03:00
|
|
|
strcpy(baseName, leaf);
|
2002-08-03 05:10:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// map the program's segments into memory
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
for (i = 0; i < elfHeader.e_phnum; i++) {
|
2005-12-21 19:47:46 +03:00
|
|
|
char regionName[B_OS_NAME_LENGTH];
|
2002-08-03 05:10:06 +04:00
|
|
|
char *regionAddress;
|
2004-11-08 17:11:46 +03:00
|
|
|
area_id id;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (programHeaders[i].p_type != PT_LOAD)
|
2004-08-18 04:22:57 +04:00
|
|
|
continue;
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
regionAddress = (char *)ROUNDOWN(programHeaders[i].p_vaddr, B_PAGE_SIZE);
|
|
|
|
if (programHeaders[i].p_flags & PF_WRITE) {
|
2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2004-06-08 13:56:51 +04:00
|
|
|
* rw/data segment
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2006-03-26 19:58:43 +04:00
|
|
|
uint32 memUpperBound = (programHeaders[i].p_vaddr % B_PAGE_SIZE) + programHeaders[i].p_memsz;
|
|
|
|
uint32 fileUpperBound = (programHeaders[i].p_vaddr % B_PAGE_SIZE) + programHeaders[i].p_filesz;
|
2002-08-03 05:10:06 +04:00
|
|
|
|
2004-10-20 04:19:38 +04:00
|
|
|
memUpperBound = ROUNDUP(memUpperBound, B_PAGE_SIZE);
|
|
|
|
fileUpperBound = ROUNDUP(fileUpperBound, B_PAGE_SIZE);
|
2002-08-03 05:10:06 +04:00
|
|
|
|
2004-06-08 14:17:32 +04:00
|
|
|
sprintf(regionName, "%s_seg%drw", baseName, i);
|
2004-06-08 13:56:51 +04:00
|
|
|
|
2005-12-20 16:29:11 +03:00
|
|
|
id = vm_map_file(team->id, regionName,
|
2002-08-03 05:10:06 +04:00
|
|
|
(void **)®ionAddress,
|
2004-06-08 13:33:51 +04:00
|
|
|
B_EXACT_ADDRESS,
|
2002-08-03 05:10:06 +04:00
|
|
|
fileUpperBound,
|
2004-06-08 12:24:53 +04:00
|
|
|
B_READ_AREA | B_WRITE_AREA, REGION_PRIVATE_MAP,
|
2006-03-26 19:58:43 +04:00
|
|
|
path, ROUNDOWN(programHeaders[i].p_offset, B_PAGE_SIZE));
|
2005-01-19 05:59:00 +03:00
|
|
|
if (id < B_OK) {
|
|
|
|
dprintf("error mapping file data: %s!\n", strerror(id));
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
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
|
2006-03-26 19:58:43 +04:00
|
|
|
+ (programHeaders[i].p_vaddr % B_PAGE_SIZE)
|
|
|
|
+ programHeaders[i].p_filesz;
|
2002-08-03 05:10:06 +04:00
|
|
|
uint32 amount = fileUpperBound
|
2006-03-26 19:58:43 +04:00
|
|
|
- (programHeaders[i].p_vaddr % B_PAGE_SIZE)
|
|
|
|
- (programHeaders[i].p_filesz);
|
2002-08-03 05:10:06 +04:00
|
|
|
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;
|
|
|
|
|
2005-12-21 19:47:46 +03:00
|
|
|
snprintf(regionName, B_OS_NAME_LENGTH, "%s_bss%d", baseName, i);
|
2002-08-03 05:10:06 +04:00
|
|
|
|
|
|
|
regionAddress += fileUpperBound;
|
2005-12-20 16:29:11 +03:00
|
|
|
id = create_area_etc(team, regionName, (void **)®ionAddress,
|
2003-08-20 19:55:56 +04:00
|
|
|
B_EXACT_ADDRESS, bss_size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
|
2005-01-19 05:59:00 +03:00
|
|
|
if (id < B_OK) {
|
|
|
|
dprintf("error allocating bss area: %s!\n", strerror(id));
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
2004-06-08 13:56:51 +04:00
|
|
|
* assume ro/text segment
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2005-12-21 19:47:46 +03:00
|
|
|
snprintf(regionName, B_OS_NAME_LENGTH, "%s_seg%dro", baseName, i);
|
2004-06-08 13:56:51 +04:00
|
|
|
|
2005-12-20 16:29:11 +03:00
|
|
|
id = vm_map_file(team->id, regionName,
|
2002-08-03 05:10:06 +04:00
|
|
|
(void **)®ionAddress,
|
2004-06-08 13:33:51 +04:00
|
|
|
B_EXACT_ADDRESS,
|
2006-03-26 19:58:43 +04:00
|
|
|
ROUNDUP(programHeaders[i].p_memsz + (programHeaders[i].p_vaddr % B_PAGE_SIZE), B_PAGE_SIZE),
|
2004-06-08 12:24:53 +04:00
|
|
|
B_READ_AREA | B_EXECUTE_AREA, REGION_PRIVATE_MAP,
|
2006-03-26 19:58:43 +04:00
|
|
|
path, ROUNDOWN(programHeaders[i].p_offset, B_PAGE_SIZE));
|
2005-01-19 05:59:00 +03:00
|
|
|
if (id < B_OK) {
|
|
|
|
dprintf("error mapping file text: %s!\n", strerror(id));
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-31 08:56:15 +04:00
|
|
|
TRACE(("elf_load: done!\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
*entry = elfHeader.e_entry;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
error:
|
2006-03-26 19:58:43 +04:00
|
|
|
if (programHeaders)
|
|
|
|
free(programHeaders);
|
2004-06-15 19:26:22 +04:00
|
|
|
_kern_close(fd);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
image_id
|
2004-05-11 23:52:38 +04:00
|
|
|
load_kernel_add_on(const char *path)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2006-03-26 19:58:43 +04:00
|
|
|
struct Elf32_Phdr *programHeaders;
|
|
|
|
struct Elf32_Ehdr *elfHeader;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct elf_image_info *image;
|
2004-11-01 18:24:39 +03:00
|
|
|
const char *fileName;
|
2004-06-09 06:24:51 +04:00
|
|
|
void *reservedAddress;
|
|
|
|
addr_t start;
|
|
|
|
size_t reservedSize;
|
2006-03-26 19:58:43 +04:00
|
|
|
status_t status;
|
|
|
|
ssize_t length;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
TRACE(("elf_load_kspace: entry path '%s'\n", path));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2007-10-06 19:33:12 +04:00
|
|
|
int fd = _kern_open(-1, path, O_RDONLY, 0);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (fd < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
return fd;
|
|
|
|
|
2007-10-06 19:33:12 +04:00
|
|
|
struct vnode *vnode;
|
2006-03-26 19:58:43 +04:00
|
|
|
status = vfs_get_vnode_from_fd(fd, true, &vnode);
|
|
|
|
if (status < B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error0;
|
|
|
|
|
2004-11-01 18:24:39 +03:00
|
|
|
// get the file name
|
|
|
|
fileName = strrchr(path, '/');
|
|
|
|
if (fileName == NULL)
|
|
|
|
fileName = path;
|
|
|
|
else
|
|
|
|
fileName++;
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
// Prevent someone else from trying to load this image
|
2004-05-11 21:29:19 +04:00
|
|
|
mutex_lock(&sImageLoadMutex);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// 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
|
|
|
goto done;
|
|
|
|
}
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
elfHeader = (struct Elf32_Ehdr *)malloc(sizeof(*elfHeader));
|
|
|
|
if (!elfHeader) {
|
|
|
|
status = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
length = _kern_read(fd, 0, elfHeader, sizeof(*elfHeader));
|
|
|
|
if (length < B_OK) {
|
|
|
|
status = length;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error1;
|
|
|
|
}
|
2006-03-26 19:58:43 +04:00
|
|
|
if (length != sizeof(*elfHeader)) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// short read
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NOT_AN_EXECUTABLE;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error1;
|
|
|
|
}
|
2006-03-26 19:58:43 +04:00
|
|
|
status = verify_eheader(elfHeader);
|
|
|
|
if (status < B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error1;
|
|
|
|
|
|
|
|
image = create_image_struct();
|
2002-07-18 23:21:40 +04:00
|
|
|
if (!image) {
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
image->vnode = vnode;
|
2006-03-26 19:58:43 +04:00
|
|
|
image->elf_header = elfHeader;
|
2002-10-30 02:07:06 +03:00
|
|
|
image->name = strdup(path);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2008-01-04 19:59:30 +03:00
|
|
|
programHeaders = (struct Elf32_Phdr *)malloc(elfHeader->e_phnum
|
|
|
|
* elfHeader->e_phentsize);
|
2006-03-26 19:58:43 +04:00
|
|
|
if (programHeaders == NULL) {
|
2006-03-25 00:27:01 +03:00
|
|
|
dprintf("%s: error allocating space for program headers\n", fileName);
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
2008-01-04 19:59:30 +03:00
|
|
|
TRACE(("reading in program headers at 0x%lx, length 0x%x\n",
|
|
|
|
elfHeader->e_phoff, elfHeader->e_phnum * elfHeader->e_phentsize));
|
|
|
|
|
|
|
|
length = _kern_read(fd, elfHeader->e_phoff, programHeaders,
|
|
|
|
elfHeader->e_phnum * elfHeader->e_phentsize);
|
2006-03-26 19:58:43 +04:00
|
|
|
if (length < B_OK) {
|
|
|
|
status = length;
|
2006-03-25 00:27:01 +03:00
|
|
|
TRACE(("%s: error reading in program headers\n", fileName));
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error3;
|
|
|
|
}
|
2006-03-26 19:58:43 +04:00
|
|
|
if (length != elfHeader->e_phnum * elfHeader->e_phentsize) {
|
2006-03-25 00:27:01 +03:00
|
|
|
TRACE(("%s: short read while reading in program headers\n", fileName));
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
goto error3;
|
|
|
|
}
|
|
|
|
|
2004-06-09 06:24:51 +04:00
|
|
|
// determine how much space we need for all loaded segments
|
|
|
|
|
|
|
|
reservedSize = 0;
|
2006-03-26 19:58:43 +04:00
|
|
|
length = 0;
|
|
|
|
|
|
|
|
for (int32 i = 0; i < elfHeader->e_phnum; i++) {
|
|
|
|
size_t end;
|
2004-06-09 06:24:51 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
if (programHeaders[i].p_type != PT_LOAD)
|
2004-06-09 06:24:51 +04:00
|
|
|
continue;
|
|
|
|
|
2008-01-04 19:59:30 +03:00
|
|
|
length += ROUNDUP(programHeaders[i].p_memsz
|
|
|
|
+ (programHeaders[i].p_vaddr % B_PAGE_SIZE), B_PAGE_SIZE);
|
2006-03-26 22:45:54 +04:00
|
|
|
|
2008-01-04 19:59:30 +03:00
|
|
|
end = ROUNDUP(programHeaders[i].p_memsz + programHeaders[i].p_vaddr,
|
|
|
|
B_PAGE_SIZE);
|
2006-03-26 19:58:43 +04:00
|
|
|
if (end > reservedSize)
|
|
|
|
reservedSize = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the segments have an unreasonable amount of unused space
|
|
|
|
// inbetween.
|
|
|
|
if ((ssize_t)reservedSize > length + 8 * 1024) {
|
|
|
|
status = B_BAD_DATA;
|
|
|
|
goto error1;
|
2004-06-09 06:24:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// reserve that space and allocate the areas from that one
|
2005-12-20 16:29:11 +03:00
|
|
|
if (vm_reserve_address_range(vm_kernel_address_space_id(), &reservedAddress,
|
2008-01-04 19:59:30 +03:00
|
|
|
B_ANY_KERNEL_ADDRESS, reservedSize, 0) < B_OK) {
|
|
|
|
status = B_NO_MEMORY;
|
2004-06-09 06:24:51 +04:00
|
|
|
goto error3;
|
2008-01-04 19:59:30 +03:00
|
|
|
}
|
2004-06-09 06:24:51 +04:00
|
|
|
|
|
|
|
start = (addr_t)reservedAddress;
|
2006-03-26 19:58:43 +04:00
|
|
|
image->data_region.size = 0;
|
|
|
|
image->text_region.size = 0;
|
2004-06-09 06:24:51 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
for (int32 i = 0; i < elfHeader->e_phnum; i++) {
|
2004-06-09 06:24:51 +04:00
|
|
|
char regionName[B_OS_NAME_LENGTH];
|
2004-06-09 06:55:54 +04:00
|
|
|
elf_region *region;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
TRACE(("looking at program header %ld\n", i));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
switch (programHeaders[i].p_type) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case PT_LOAD:
|
|
|
|
break;
|
|
|
|
case PT_DYNAMIC:
|
2006-03-26 19:58:43 +04:00
|
|
|
image->dynamic_section = programHeaders[i].p_vaddr;
|
2002-07-09 16:24:59 +04:00
|
|
|
continue;
|
|
|
|
default:
|
2008-01-04 19:59:30 +03:00
|
|
|
dprintf("%s: unhandled pheader type 0x%lx\n", fileName,
|
|
|
|
programHeaders[i].p_type);
|
2002-07-09 16:24:59 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're here, so it must be a PT_LOAD segment
|
2006-03-26 19:58:43 +04:00
|
|
|
if (programHeaders[i].IsReadWrite()) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// this is the writable segment
|
2006-03-26 19:58:43 +04:00
|
|
|
if (image->data_region.size != 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// we've already created this segment
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-09 06:55:54 +04:00
|
|
|
region = &image->data_region;
|
|
|
|
|
2004-11-01 18:24:39 +03:00
|
|
|
snprintf(regionName, B_OS_NAME_LENGTH, "%s_data", fileName);
|
2006-03-26 19:58:43 +04:00
|
|
|
} else if (programHeaders[i].IsExecutable()) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// this is the non-writable segment
|
2006-03-26 19:58:43 +04:00
|
|
|
if (image->text_region.size != 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// we've already created this segment
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-09 06:55:54 +04:00
|
|
|
region = &image->text_region;
|
2003-08-19 18:28:11 +04:00
|
|
|
|
2004-11-01 18:24:39 +03:00
|
|
|
snprintf(regionName, B_OS_NAME_LENGTH, "%s_text", fileName);
|
2002-07-09 16:24:59 +04:00
|
|
|
} else {
|
2006-03-26 19:58:43 +04:00
|
|
|
dprintf("%s: weird program header flags 0x%lx\n", fileName,
|
|
|
|
programHeaders[i].p_flags);
|
2002-07-09 16:24:59 +04:00
|
|
|
continue;
|
|
|
|
}
|
2002-08-05 10:41:57 +04:00
|
|
|
|
2008-01-04 19:59:30 +03:00
|
|
|
region->start = (addr_t)reservedAddress + ROUNDOWN(
|
|
|
|
programHeaders[i].p_vaddr, B_PAGE_SIZE);
|
2006-03-26 19:58:43 +04:00
|
|
|
region->size = ROUNDUP(programHeaders[i].p_memsz
|
|
|
|
+ (programHeaders[i].p_vaddr % B_PAGE_SIZE), B_PAGE_SIZE);
|
2008-01-04 19:59:30 +03:00
|
|
|
region->id = create_area(regionName, (void **)®ion->start,
|
|
|
|
B_EXACT_ADDRESS, region->size, B_FULL_LOCK,
|
|
|
|
B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA);
|
2005-01-19 05:59:00 +03:00
|
|
|
if (region->id < B_OK) {
|
2008-01-04 19:59:30 +03:00
|
|
|
dprintf("%s: error allocating area: %s\n", fileName,
|
|
|
|
strerror(region->id));
|
2006-03-26 19:58:43 +04:00
|
|
|
status = B_NOT_AN_EXECUTABLE;
|
2004-06-09 06:24:51 +04:00
|
|
|
goto error4;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2006-03-26 19:58:43 +04:00
|
|
|
region->delta = -ROUNDOWN(programHeaders[i].p_vaddr, B_PAGE_SIZE);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-25 00:27:01 +03:00
|
|
|
TRACE(("elf_load_kspace: created area \"%s\" at %p\n",
|
|
|
|
regionName, (void *)region->start));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
length = _kern_read(fd, programHeaders[i].p_offset,
|
|
|
|
(void *)(region->start + (programHeaders[i].p_vaddr % B_PAGE_SIZE)),
|
|
|
|
programHeaders[i].p_filesz);
|
|
|
|
if (length < B_OK) {
|
|
|
|
status = length;
|
|
|
|
dprintf("%s: error reading in segment %ld\n", fileName, i);
|
2004-06-09 06:24:51 +04:00
|
|
|
goto error5;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
// get the segment order
|
|
|
|
elf_region *firstRegion;
|
|
|
|
elf_region *secondRegion;
|
|
|
|
if (image->text_region.start < image->data_region.start) {
|
|
|
|
firstRegion = &image->text_region;
|
|
|
|
secondRegion = &image->data_region;
|
|
|
|
} else {
|
|
|
|
firstRegion = &image->data_region;
|
|
|
|
secondRegion = &image->text_region;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
image->data_region.delta += image->data_region.start;
|
|
|
|
image->text_region.delta += image->text_region.start;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// modify the dynamic ptr by the delta of the regions
|
2006-03-26 19:58:43 +04:00
|
|
|
image->dynamic_section += image->text_region.delta;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
status = elf_parse_dynamic_section(image);
|
|
|
|
if (status < B_OK)
|
2004-06-09 06:24:51 +04:00
|
|
|
goto error5;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2008-01-06 17:54:57 +03:00
|
|
|
status = elf_relocate(image, NULL);
|
2006-03-26 19:58:43 +04:00
|
|
|
if (status < B_OK)
|
2004-06-09 06:24:51 +04:00
|
|
|
goto error5;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-06-09 06:55:54 +04:00
|
|
|
// We needed to read in the contents of the "text" area, but
|
2004-06-26 06:15:48 +04:00
|
|
|
// now we can protect it read-only/execute
|
2008-01-04 19:59:30 +03:00
|
|
|
set_area_protection(image->text_region.id,
|
|
|
|
B_KERNEL_READ_AREA | B_KERNEL_EXECUTE_AREA);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
// There might be a hole between the two segments, and we don't need to
|
|
|
|
// reserve this any longer
|
2008-01-04 19:59:30 +03:00
|
|
|
vm_unreserve_address_range(vm_kernel_address_space_id(), reservedAddress,
|
|
|
|
reservedSize);
|
2006-03-26 19:58:43 +04:00
|
|
|
|
2004-06-01 04:27:15 +04:00
|
|
|
// ToDo: this should be enabled by kernel settings!
|
|
|
|
if (1)
|
|
|
|
load_elf_symbol_table(fd, image);
|
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
free(programHeaders);
|
2008-02-19 01:54:12 +03:00
|
|
|
mutex_lock(&sImageMutex);
|
2004-05-11 21:18:02 +04:00
|
|
|
register_elf_image(image);
|
2008-02-19 01:54:12 +03:00
|
|
|
mutex_unlock(&sImageMutex);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
done:
|
2005-10-05 20:41:04 +04:00
|
|
|
_kern_close(fd);
|
2004-05-11 21:29:19 +04:00
|
|
|
mutex_unlock(&sImageLoadMutex);
|
2004-10-07 21:17:04 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return image->id;
|
|
|
|
|
2004-06-09 06:24:51 +04:00
|
|
|
error5:
|
2004-06-09 06:55:54 +04:00
|
|
|
delete_area(image->data_region.id);
|
|
|
|
delete_area(image->text_region.id);
|
2004-06-09 06:24:51 +04:00
|
|
|
error4:
|
2008-01-04 19:59:30 +03:00
|
|
|
vm_unreserve_address_range(vm_kernel_address_space_id(), reservedAddress,
|
|
|
|
reservedSize);
|
2002-07-09 16:24:59 +04:00
|
|
|
error3:
|
2006-03-26 19:58:43 +04:00
|
|
|
free(programHeaders);
|
2004-05-11 18:22:00 +04:00
|
|
|
error2:
|
|
|
|
free(image);
|
2002-07-09 16:24:59 +04:00
|
|
|
error1:
|
2006-03-26 19:58:43 +04:00
|
|
|
free(elfHeader);
|
2002-07-09 16:24:59 +04:00
|
|
|
error:
|
2004-05-11 21:29:19 +04:00
|
|
|
mutex_unlock(&sImageLoadMutex);
|
2002-07-09 16:24:59 +04:00
|
|
|
error0:
|
2008-01-04 19:59:30 +03:00
|
|
|
dprintf("Could not load kernel add-on \"%s\": %s\n", path,
|
|
|
|
strerror(status));
|
|
|
|
|
2002-08-05 10:41:57 +04:00
|
|
|
if (vnode)
|
2005-08-01 18:17:41 +04:00
|
|
|
vfs_put_vnode(vnode);
|
2004-06-15 19:26:22 +04:00
|
|
|
_kern_close(fd);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-03-26 19:58:43 +04:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
status_t
|
2004-05-11 23:52:38 +04:00
|
|
|
unload_kernel_add_on(image_id id)
|
2004-05-11 18:22:00 +04:00
|
|
|
{
|
|
|
|
struct elf_image_info *image;
|
|
|
|
status_t status;
|
|
|
|
|
2007-04-01 03:06:09 +04:00
|
|
|
mutex_lock(&sImageLoadMutex);
|
2004-05-11 23:52:38 +04:00
|
|
|
mutex_lock(&sImageMutex);
|
2008-03-04 15:36:53 +03:00
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
image = find_image(id);
|
2007-04-01 03:06:09 +04:00
|
|
|
if (image != NULL)
|
2004-05-11 23:52:38 +04:00
|
|
|
status = unload_elf_image(image);
|
2007-04-01 03:06:09 +04:00
|
|
|
else
|
2004-05-11 23:52:38 +04:00
|
|
|
status = B_BAD_IMAGE_ID;
|
2004-05-11 18:22:00 +04:00
|
|
|
|
2008-03-04 15:36:53 +03:00
|
|
|
mutex_unlock(&sImageMutex);
|
2007-04-01 03:06:09 +04:00
|
|
|
mutex_unlock(&sImageLoadMutex);
|
|
|
|
|
2004-05-11 18:22:00 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
status_t
|
2005-12-16 20:35:03 +03:00
|
|
|
elf_init(kernel_args *args)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-04-27 15:55:31 +04:00
|
|
|
struct preloaded_image *image;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 19:04:36 +04:00
|
|
|
image_init();
|
|
|
|
|
2004-05-11 21:29:19 +04:00
|
|
|
mutex_init(&sImageMutex, "kimages_lock");
|
|
|
|
mutex_init(&sImageLoadMutex, "kimages_load_lock");
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-05-11 21:18:02 +04:00
|
|
|
sImagesHash = hash_init(IMAGE_HASH_SIZE, 0, image_compare, image_hash);
|
|
|
|
if (sImagesHash == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
// Build a image structure for the kernel, which has already been loaded.
|
2004-06-01 02:51:01 +04:00
|
|
|
// The preloaded_images were already prepared by the VM.
|
2005-12-16 20:35:03 +03:00
|
|
|
if (insert_preloaded_image(&args->kernel_image, true) < B_OK)
|
2004-06-01 02:51:01 +04:00
|
|
|
panic("could not create kernel image.\n");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-27 15:55:31 +04:00
|
|
|
// Build image structures for all preloaded images.
|
2005-12-16 20:35:03 +03:00
|
|
|
for (image = args->preloaded_images; image != NULL; image = image->next) {
|
2004-06-01 02:51:01 +04:00
|
|
|
insert_preloaded_image(image, false);
|
2004-04-27 15:55:31 +04:00
|
|
|
}
|
|
|
|
|
2004-06-01 01:47:34 +04:00
|
|
|
add_debugger_command("ls", &dump_address_info, "lookup symbol for a particular address");
|
|
|
|
add_debugger_command("symbols", &dump_symbols, "dump symbols for image");
|
2004-05-11 21:18:02 +04:00
|
|
|
add_debugger_command("image", &dump_image, "dump image info");
|
2005-04-04 17:40:10 +04:00
|
|
|
|
|
|
|
sInitialized = true;
|
2004-04-27 15:55:31 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|