1998-09-30 19:38:26 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
|
|
|
* Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-09-30 19:38:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/exec.h>
|
1998-10-09 23:18:43 +00:00
|
|
|
#include <sys/linker.h>
|
2000-05-01 17:41:25 +00:00
|
|
|
#include <sys/module.h>
|
1998-09-30 19:38:26 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <machine/elf.h>
|
|
|
|
#include <stand.h>
|
|
|
|
#define FREEBSD_ELF
|
|
|
|
#include <link.h>
|
|
|
|
|
|
|
|
#include "bootstrap.h"
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct elf_file {
|
|
|
|
Elf_Phdr *ph;
|
|
|
|
Elf_Ehdr *ehdr;
|
|
|
|
Elf_Sym *symtab;
|
2002-04-06 04:09:42 +00:00
|
|
|
Elf_Hashelt *hashtab;
|
|
|
|
Elf_Hashelt nbuckets;
|
|
|
|
Elf_Hashelt nchains;
|
|
|
|
Elf_Hashelt *buckets;
|
|
|
|
Elf_Hashelt *chains;
|
2003-01-21 05:46:46 +00:00
|
|
|
Elf_Rela *rela;
|
|
|
|
size_t relasz;
|
2000-05-01 17:41:25 +00:00
|
|
|
char *strtab;
|
|
|
|
size_t strsz;
|
|
|
|
int fd;
|
|
|
|
caddr_t firstpage;
|
2000-08-03 09:14:02 +00:00
|
|
|
size_t firstlen;
|
2000-05-01 17:41:25 +00:00
|
|
|
int kernel;
|
|
|
|
vm_offset_t off;
|
|
|
|
} *elf_file_t;
|
|
|
|
|
|
|
|
static int elf_loadimage(struct preloaded_file *mp, elf_file_t ef, vm_offset_t loadaddr);
|
|
|
|
static int elf_lookup_symbol(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
|
2003-01-21 05:46:46 +00:00
|
|
|
#ifdef __sparc__
|
|
|
|
static void elf_reloc_ptr(struct preloaded_file *mp, elf_file_t ef,
|
|
|
|
void *p, void *val, size_t len);
|
|
|
|
#endif
|
2000-05-01 17:41:25 +00:00
|
|
|
static int elf_parse_modmetadata(struct preloaded_file *mp, elf_file_t ef);
|
2000-08-03 09:14:02 +00:00
|
|
|
static char *fake_modname(const char *name);
|
1998-09-30 19:38:26 +00:00
|
|
|
|
2000-08-03 09:14:02 +00:00
|
|
|
const char *elf_kerneltype = "elf kernel";
|
|
|
|
const char *elf_moduletype = "elf module";
|
1998-09-30 19:38:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to load the file (file) as an ELF module. It will be stored at
|
|
|
|
* (dest), and a pointer to a module structure describing the loaded object
|
|
|
|
* will be saved in (result).
|
|
|
|
*/
|
|
|
|
int
|
2000-05-01 17:41:25 +00:00
|
|
|
elf_loadfile(char *filename, vm_offset_t dest, struct preloaded_file **result)
|
1998-09-30 19:38:26 +00:00
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct preloaded_file *fp, *kfp;
|
|
|
|
struct elf_file ef;
|
|
|
|
Elf_Ehdr *ehdr;
|
|
|
|
int err;
|
1998-09-30 19:38:26 +00:00
|
|
|
u_int pad;
|
2000-08-03 09:14:02 +00:00
|
|
|
ssize_t bytes_read;
|
1998-09-30 19:38:26 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
fp = NULL;
|
|
|
|
bzero(&ef, sizeof(struct elf_file));
|
1998-09-30 19:38:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the image, read and validate the ELF header
|
|
|
|
*/
|
|
|
|
if (filename == NULL) /* can't handle nameless */
|
|
|
|
return(EFTYPE);
|
2000-05-01 17:41:25 +00:00
|
|
|
if ((ef.fd = open(filename, O_RDONLY)) == -1)
|
1998-09-30 19:38:26 +00:00
|
|
|
return(errno);
|
2000-05-01 17:41:25 +00:00
|
|
|
ef.firstpage = malloc(PAGE_SIZE);
|
|
|
|
if (ef.firstpage == NULL) {
|
|
|
|
close(ef.fd);
|
1998-10-17 03:06:38 +00:00
|
|
|
return(ENOMEM);
|
2000-05-01 17:41:25 +00:00
|
|
|
}
|
2000-08-03 09:14:02 +00:00
|
|
|
bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
|
|
|
|
ef.firstlen = (size_t)bytes_read;
|
|
|
|
if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EFTYPE; /* could be EIO, but may be small file */
|
|
|
|
goto oerr;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
|
1998-09-30 19:38:26 +00:00
|
|
|
|
|
|
|
/* Is it ELF? */
|
1998-10-17 03:06:38 +00:00
|
|
|
if (!IS_ELF(*ehdr)) {
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EFTYPE;
|
|
|
|
goto oerr;
|
|
|
|
}
|
1998-10-17 03:06:38 +00:00
|
|
|
if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
|
|
|
|
ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
|
|
|
|
ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
|
|
|
|
ehdr->e_version != EV_CURRENT ||
|
|
|
|
ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EFTYPE;
|
|
|
|
goto oerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see what sort of module we are.
|
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
kfp = file_findfile(NULL, NULL);
|
1998-10-17 03:06:38 +00:00
|
|
|
if (ehdr->e_type == ET_DYN) {
|
1998-09-30 19:38:26 +00:00
|
|
|
/* Looks like a kld module */
|
2000-05-01 17:41:25 +00:00
|
|
|
if (kfp == NULL) {
|
|
|
|
printf("elf_loadfile: can't load module before kernel\n");
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
if (strcmp(elf_kerneltype, kfp->f_type)) {
|
|
|
|
printf("elf_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
|
|
|
}
|
|
|
|
/* Looks OK, got ahead */
|
2000-05-01 17:41:25 +00:00
|
|
|
ef.kernel = 0;
|
1998-09-30 19:38:26 +00:00
|
|
|
|
|
|
|
/* Page-align the load address */
|
1998-10-09 23:18:43 +00:00
|
|
|
pad = (u_int)dest & PAGE_MASK;
|
1998-09-30 19:38:26 +00:00
|
|
|
if (pad != 0) {
|
|
|
|
pad = PAGE_SIZE - pad;
|
1998-10-09 23:18:43 +00:00
|
|
|
dest += pad;
|
1998-09-30 19:38:26 +00:00
|
|
|
}
|
1998-10-17 03:06:38 +00:00
|
|
|
} else if (ehdr->e_type == ET_EXEC) {
|
1998-09-30 19:38:26 +00:00
|
|
|
/* Looks like a kernel */
|
2000-05-01 17:41:25 +00:00
|
|
|
if (kfp != NULL) {
|
|
|
|
printf("elf_loadfile: kernel already loaded\n");
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Calculate destination address based on kernel entrypoint
|
|
|
|
*/
|
1998-10-17 03:06:38 +00:00
|
|
|
dest = (vm_offset_t) ehdr->e_entry;
|
1998-09-30 19:38:26 +00:00
|
|
|
if (dest == 0) {
|
2000-05-01 17:41:25 +00:00
|
|
|
printf("elf_loadfile: not a kernel (maybe static binary?)\n");
|
1998-09-30 19:38:26 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto oerr;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
ef.kernel = 1;
|
1998-09-30 19:38:26 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
err = EFTYPE;
|
|
|
|
goto oerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we think we should handle this.
|
|
|
|
*/
|
2000-05-01 17:41:25 +00:00
|
|
|
fp = file_alloc();
|
|
|
|
if (fp == NULL) {
|
|
|
|
printf("elf_loadfile: cannot allocate module info\n");
|
1998-10-09 23:18:43 +00:00
|
|
|
err = EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
if (ef.kernel)
|
1998-10-09 23:18:43 +00:00
|
|
|
setenv("kernelname", filename, 1);
|
2001-09-11 01:09:24 +00:00
|
|
|
fp->f_name = strdup(filename);
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_type = strdup(ef.kernel ? elf_kerneltype : elf_moduletype);
|
1998-09-30 19:38:26 +00:00
|
|
|
|
1998-10-13 09:25:27 +00:00
|
|
|
#ifdef ELF_VERBOSE
|
2000-05-01 17:41:25 +00:00
|
|
|
if (ef.kernel)
|
1998-10-12 09:13:50 +00:00
|
|
|
printf("%s entry at %p\n", filename, (void *) dest);
|
1998-10-14 00:41:17 +00:00
|
|
|
#else
|
|
|
|
printf("%s ", filename);
|
1998-10-13 09:25:27 +00:00
|
|
|
#endif
|
1998-09-30 19:38:26 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_size = elf_loadimage(fp, &ef, dest);
|
|
|
|
if (fp->f_size == 0 || fp->f_addr == 0)
|
1998-09-30 19:38:26 +00:00
|
|
|
goto ioerr;
|
|
|
|
|
|
|
|
/* save exec header as metadata */
|
2000-05-01 17:41:25 +00:00
|
|
|
file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
|
1998-09-30 19:38:26 +00:00
|
|
|
|
|
|
|
/* Load OK, return module pointer */
|
2000-05-01 17:41:25 +00:00
|
|
|
*result = (struct preloaded_file *)fp;
|
1998-09-30 19:38:26 +00:00
|
|
|
err = 0;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ioerr:
|
|
|
|
err = EIO;
|
|
|
|
oerr:
|
2000-05-01 17:41:25 +00:00
|
|
|
file_discard(fp);
|
1998-09-30 19:38:26 +00:00
|
|
|
out:
|
2000-05-01 17:41:25 +00:00
|
|
|
if (ef.firstpage)
|
|
|
|
free(ef.firstpage);
|
|
|
|
close(ef.fd);
|
1998-09-30 19:38:26 +00:00
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With the file (fd) open on the image, and (ehdr) containing
|
1998-10-09 23:18:43 +00:00
|
|
|
* the Elf header, load the image at (off)
|
1998-09-30 19:38:26 +00:00
|
|
|
*/
|
|
|
|
static int
|
2000-05-01 17:41:25 +00:00
|
|
|
elf_loadimage(struct preloaded_file *fp, elf_file_t ef, vm_offset_t off)
|
1998-09-30 19:38:26 +00:00
|
|
|
{
|
2000-08-03 09:14:02 +00:00
|
|
|
int i;
|
|
|
|
u_int j;
|
2000-05-01 17:41:25 +00:00
|
|
|
Elf_Ehdr *ehdr;
|
|
|
|
Elf_Phdr *phdr, *php;
|
1998-10-02 08:04:56 +00:00
|
|
|
Elf_Shdr *shdr;
|
1998-09-30 19:38:26 +00:00
|
|
|
int ret;
|
|
|
|
vm_offset_t firstaddr;
|
|
|
|
vm_offset_t lastaddr;
|
|
|
|
void *buf;
|
1998-10-02 08:04:56 +00:00
|
|
|
size_t resid, chunk;
|
2000-08-03 09:14:02 +00:00
|
|
|
ssize_t result;
|
1998-10-02 08:04:56 +00:00
|
|
|
vm_offset_t dest;
|
|
|
|
vm_offset_t ssym, esym;
|
1998-10-09 23:18:43 +00:00
|
|
|
Elf_Dyn *dp;
|
|
|
|
int ndp;
|
1998-10-12 09:13:50 +00:00
|
|
|
int symstrindex;
|
|
|
|
int symtabindex;
|
|
|
|
long size;
|
2000-08-03 09:14:02 +00:00
|
|
|
u_int fpcopy;
|
1998-09-30 19:38:26 +00:00
|
|
|
|
1998-10-09 23:18:43 +00:00
|
|
|
dp = NULL;
|
|
|
|
shdr = NULL;
|
1998-09-30 19:38:26 +00:00
|
|
|
ret = 0;
|
|
|
|
firstaddr = lastaddr = 0;
|
2000-05-01 17:41:25 +00:00
|
|
|
ehdr = ef->ehdr;
|
|
|
|
if (ef->kernel) {
|
1998-09-30 19:38:26 +00:00
|
|
|
#ifdef __i386__
|
1999-02-15 17:36:58 +00:00
|
|
|
off = - (off & 0xff000000u); /* i386 relocates after locore */
|
1998-09-30 19:38:26 +00:00
|
|
|
#else
|
|
|
|
off = 0; /* alpha is direct mapped for kernels */
|
|
|
|
#endif
|
1998-10-09 23:18:43 +00:00
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
ef->off = off;
|
1998-09-30 19:38:26 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
|
1998-10-17 03:06:38 +00:00
|
|
|
printf("elf_loadimage: program header not within first page\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
|
1998-10-17 03:06:38 +00:00
|
|
|
|
1998-09-30 19:38:26 +00:00
|
|
|
for (i = 0; i < ehdr->e_phnum; i++) {
|
|
|
|
/* We want to load PT_LOAD segments only.. */
|
|
|
|
if (phdr[i].p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
|
1998-10-12 09:13:50 +00:00
|
|
|
#ifdef ELF_VERBOSE
|
1998-10-02 08:04:56 +00:00
|
|
|
printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
|
1998-09-30 19:38:26 +00:00
|
|
|
(long)phdr[i].p_filesz, (long)phdr[i].p_offset,
|
|
|
|
(long)(phdr[i].p_vaddr + off),
|
1998-10-02 08:04:56 +00:00
|
|
|
(long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
|
1998-10-12 09:13:50 +00:00
|
|
|
#else
|
1998-10-13 09:25:27 +00:00
|
|
|
if ((phdr[i].p_flags & PF_W) == 0) {
|
|
|
|
printf("text=0x%lx ", (long)phdr[i].p_filesz);
|
|
|
|
} else {
|
|
|
|
printf("data=0x%lx", (long)phdr[i].p_filesz);
|
|
|
|
if (phdr[i].p_filesz < phdr[i].p_memsz)
|
|
|
|
printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
|
|
|
|
printf(" ");
|
|
|
|
}
|
1998-10-12 09:13:50 +00:00
|
|
|
#endif
|
1998-10-17 03:06:38 +00:00
|
|
|
fpcopy = 0;
|
2000-05-01 17:41:25 +00:00
|
|
|
if (ef->firstlen > phdr[i].p_offset) {
|
|
|
|
fpcopy = ef->firstlen - phdr[i].p_offset;
|
|
|
|
archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
|
1998-10-17 03:06:38 +00:00
|
|
|
phdr[i].p_vaddr + off, fpcopy);
|
1998-09-30 19:38:26 +00:00
|
|
|
}
|
1998-10-17 03:06:38 +00:00
|
|
|
if (phdr[i].p_filesz > fpcopy) {
|
2000-08-03 09:14:02 +00:00
|
|
|
if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy),
|
|
|
|
SEEK_SET) == -1) {
|
1998-10-17 03:06:38 +00:00
|
|
|
printf("\nelf_loadexec: cannot seek\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy,
|
2000-08-03 09:14:02 +00:00
|
|
|
phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) {
|
1998-10-17 03:06:38 +00:00
|
|
|
printf("\nelf_loadexec: archsw.readin failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
1998-09-30 19:38:26 +00:00
|
|
|
}
|
|
|
|
/* clear space from oversized segments; eg: bss */
|
|
|
|
if (phdr[i].p_filesz < phdr[i].p_memsz) {
|
1998-10-12 09:13:50 +00:00
|
|
|
#ifdef ELF_VERBOSE
|
1998-10-02 08:04:56 +00:00
|
|
|
printf(" (bss: 0x%lx-0x%lx)",
|
1998-09-30 19:38:26 +00:00
|
|
|
(long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
|
|
|
|
(long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
|
1998-10-12 09:13:50 +00:00
|
|
|
#endif
|
1998-09-30 19:38:26 +00:00
|
|
|
|
1998-10-02 08:04:56 +00:00
|
|
|
/* no archsw.arch_bzero */
|
1998-09-30 19:38:26 +00:00
|
|
|
buf = malloc(PAGE_SIZE);
|
2000-05-01 17:41:25 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
printf("\nelf_loadimage: malloc() failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
1998-09-30 19:38:26 +00:00
|
|
|
bzero(buf, PAGE_SIZE);
|
|
|
|
resid = phdr[i].p_memsz - phdr[i].p_filesz;
|
|
|
|
dest = phdr[i].p_vaddr + off + phdr[i].p_filesz;
|
|
|
|
while (resid > 0) {
|
|
|
|
chunk = min(PAGE_SIZE, resid);
|
|
|
|
archsw.arch_copyin(buf, dest, chunk);
|
|
|
|
resid -= chunk;
|
|
|
|
dest += chunk;
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
1998-10-12 09:13:50 +00:00
|
|
|
#ifdef ELF_VERBOSE
|
1998-10-02 08:04:56 +00:00
|
|
|
printf("\n");
|
1998-10-12 09:13:50 +00:00
|
|
|
#endif
|
1998-09-30 19:38:26 +00:00
|
|
|
|
1998-10-09 23:18:43 +00:00
|
|
|
if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
|
1998-09-30 19:38:26 +00:00
|
|
|
firstaddr = phdr[i].p_vaddr + off;
|
1998-10-09 23:18:43 +00:00
|
|
|
if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
|
1998-09-30 19:38:26 +00:00
|
|
|
lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
|
|
|
|
}
|
1998-10-12 09:13:50 +00:00
|
|
|
lastaddr = roundup(lastaddr, sizeof(long));
|
1998-09-30 19:38:26 +00:00
|
|
|
|
1998-10-02 08:04:56 +00:00
|
|
|
/*
|
|
|
|
* Now grab the symbol tables. This isn't easy if we're reading a
|
|
|
|
* .gz file. I think the rule is going to have to be that you must
|
|
|
|
* strip a file to remove symbols before gzipping it so that we do not
|
1998-10-12 09:13:50 +00:00
|
|
|
* try to lseek() on it.
|
1998-10-02 08:04:56 +00:00
|
|
|
*/
|
|
|
|
chunk = ehdr->e_shnum * ehdr->e_shentsize;
|
1998-10-13 09:25:27 +00:00
|
|
|
if (chunk == 0 || ehdr->e_shoff == 0)
|
|
|
|
goto nosyms;
|
1998-10-02 08:04:56 +00:00
|
|
|
shdr = malloc(chunk);
|
|
|
|
if (shdr == NULL)
|
|
|
|
goto nosyms;
|
2000-08-03 09:14:02 +00:00
|
|
|
if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) {
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("\nelf_loadimage: cannot lseek() to section headers");
|
1998-10-02 08:04:56 +00:00
|
|
|
goto nosyms;
|
|
|
|
}
|
2000-08-03 09:14:02 +00:00
|
|
|
result = read(ef->fd, shdr, chunk);
|
|
|
|
if (result < 0 || (size_t)result != chunk) {
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("\nelf_loadimage: read section headers failed");
|
1998-10-02 08:04:56 +00:00
|
|
|
goto nosyms;
|
|
|
|
}
|
1998-10-12 09:13:50 +00:00
|
|
|
symtabindex = -1;
|
|
|
|
symstrindex = -1;
|
1998-10-02 08:04:56 +00:00
|
|
|
for (i = 0; i < ehdr->e_shnum; i++) {
|
1998-10-12 09:13:50 +00:00
|
|
|
if (shdr[i].sh_type != SHT_SYMTAB)
|
1998-10-09 23:18:43 +00:00
|
|
|
continue;
|
1998-10-02 08:04:56 +00:00
|
|
|
for (j = 0; j < ehdr->e_phnum; j++) {
|
|
|
|
if (phdr[j].p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
if (shdr[i].sh_offset >= phdr[j].p_offset &&
|
|
|
|
(shdr[i].sh_offset + shdr[i].sh_size <=
|
|
|
|
phdr[j].p_offset + phdr[j].p_filesz)) {
|
|
|
|
shdr[i].sh_offset = 0;
|
|
|
|
shdr[i].sh_size = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
|
|
|
|
continue; /* alread loaded in a PT_LOAD above */
|
1998-10-12 09:13:50 +00:00
|
|
|
/* Save it for loading below */
|
|
|
|
symtabindex = i;
|
|
|
|
symstrindex = shdr[i].sh_link;
|
|
|
|
}
|
|
|
|
if (symtabindex < 0 || symstrindex < 0)
|
|
|
|
goto nosyms;
|
|
|
|
|
|
|
|
/* Ok, committed to a load. */
|
|
|
|
#ifndef ELF_VERBOSE
|
1998-10-13 09:25:27 +00:00
|
|
|
printf("syms=[");
|
1998-10-12 09:13:50 +00:00
|
|
|
#endif
|
|
|
|
ssym = lastaddr;
|
|
|
|
for (i = symtabindex; i >= 0; i = symstrindex) {
|
|
|
|
#ifdef ELF_VERBOSE
|
|
|
|
char *secname;
|
|
|
|
|
|
|
|
switch(shdr[i].sh_type) {
|
|
|
|
case SHT_SYMTAB: /* Symbol table */
|
|
|
|
secname = "symtab";
|
|
|
|
break;
|
|
|
|
case SHT_STRTAB: /* String table */
|
|
|
|
secname = "strtab";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
secname = "WHOA!!";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size = shdr[i].sh_size;
|
|
|
|
archsw.arch_copyin(&size, lastaddr, sizeof(size));
|
|
|
|
lastaddr += sizeof(long);
|
1998-10-02 08:04:56 +00:00
|
|
|
|
1998-10-12 09:13:50 +00:00
|
|
|
#ifdef ELF_VERBOSE
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname,
|
1998-10-02 08:04:56 +00:00
|
|
|
shdr[i].sh_size, shdr[i].sh_offset,
|
|
|
|
lastaddr, lastaddr + shdr[i].sh_size);
|
1998-10-12 09:13:50 +00:00
|
|
|
#else
|
1998-10-13 09:25:27 +00:00
|
|
|
if (i == symstrindex)
|
|
|
|
printf("+");
|
1998-10-16 03:04:15 +00:00
|
|
|
printf("0x%lx+0x%lx", (long)sizeof(size), size);
|
1998-10-12 09:13:50 +00:00
|
|
|
#endif
|
|
|
|
|
2000-08-03 09:14:02 +00:00
|
|
|
if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("\nelf_loadimage: could not seek for symbols - skipped!");
|
1998-10-12 09:13:50 +00:00
|
|
|
lastaddr = ssym;
|
|
|
|
ssym = 0;
|
|
|
|
goto nosyms;
|
1998-10-02 08:04:56 +00:00
|
|
|
}
|
2000-08-03 09:14:02 +00:00
|
|
|
result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
|
|
|
|
if (result < 0 || (size_t)result != shdr[i].sh_size) {
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("\nelf_loadimage: could not read symbols - skipped!");
|
1998-10-12 09:13:50 +00:00
|
|
|
lastaddr = ssym;
|
|
|
|
ssym = 0;
|
|
|
|
goto nosyms;
|
1998-10-02 08:04:56 +00:00
|
|
|
}
|
|
|
|
/* Reset offsets relative to ssym */
|
|
|
|
lastaddr += shdr[i].sh_size;
|
|
|
|
lastaddr = roundup(lastaddr, sizeof(long));
|
1998-10-12 09:13:50 +00:00
|
|
|
if (i == symtabindex)
|
|
|
|
symtabindex = -1;
|
|
|
|
else if (i == symstrindex)
|
|
|
|
symstrindex = -1;
|
1998-10-02 08:04:56 +00:00
|
|
|
}
|
|
|
|
esym = lastaddr;
|
1998-10-12 09:13:50 +00:00
|
|
|
#ifndef ELF_VERBOSE
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("]");
|
1998-10-12 09:13:50 +00:00
|
|
|
#endif
|
1998-10-02 08:04:56 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
|
|
|
|
file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
|
1998-10-02 08:04:56 +00:00
|
|
|
|
|
|
|
nosyms:
|
1999-01-04 18:37:41 +00:00
|
|
|
printf("\n");
|
1998-10-02 08:04:56 +00:00
|
|
|
|
1998-09-30 19:38:26 +00:00
|
|
|
ret = lastaddr - firstaddr;
|
2000-05-01 17:41:25 +00:00
|
|
|
fp->f_addr = firstaddr;
|
1998-10-09 23:18:43 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
php = NULL;
|
1998-10-09 23:18:43 +00:00
|
|
|
for (i = 0; i < ehdr->e_phnum; i++) {
|
|
|
|
if (phdr[i].p_type == PT_DYNAMIC) {
|
2000-05-01 17:41:25 +00:00
|
|
|
php = phdr + i;
|
|
|
|
dp = (Elf_Dyn *)(php->p_vaddr);
|
|
|
|
file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(dp), &dp);
|
1998-10-09 23:18:43 +00:00
|
|
|
dp = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
|
1998-10-09 23:18:43 +00:00
|
|
|
goto out;
|
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
ndp = php->p_filesz / sizeof(Elf_Dyn);
|
|
|
|
if (ndp == 0)
|
|
|
|
goto out;
|
|
|
|
dp = malloc(php->p_filesz);
|
|
|
|
if (dp == NULL)
|
1998-10-09 23:18:43 +00:00
|
|
|
goto out;
|
2000-05-01 17:41:25 +00:00
|
|
|
archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
|
|
|
|
|
|
|
|
ef->strsz = 0;
|
1998-10-09 23:18:43 +00:00
|
|
|
for (i = 0; i < ndp; i++) {
|
|
|
|
if (dp[i].d_tag == NULL)
|
|
|
|
break;
|
|
|
|
switch (dp[i].d_tag) {
|
2000-05-01 17:41:25 +00:00
|
|
|
case DT_HASH:
|
2002-04-06 04:09:42 +00:00
|
|
|
ef->hashtab = (Elf_Hashelt*)(dp[i].d_un.d_ptr + off);
|
2000-05-01 17:41:25 +00:00
|
|
|
break;
|
1998-10-09 23:18:43 +00:00
|
|
|
case DT_STRTAB:
|
2000-05-01 17:41:25 +00:00
|
|
|
ef->strtab = (char *)(dp[i].d_un.d_ptr + off);
|
1998-10-09 23:18:43 +00:00
|
|
|
break;
|
|
|
|
case DT_STRSZ:
|
2000-05-01 17:41:25 +00:00
|
|
|
ef->strsz = dp[i].d_un.d_val;
|
|
|
|
break;
|
|
|
|
case DT_SYMTAB:
|
|
|
|
ef->symtab = (Elf_Sym*)(dp[i].d_un.d_ptr + off);
|
1998-10-09 23:18:43 +00:00
|
|
|
break;
|
2003-01-21 05:46:46 +00:00
|
|
|
case DT_RELA:
|
|
|
|
ef->rela = (Elf_Rela *)(dp[i].d_un.d_ptr + off);
|
|
|
|
break;
|
|
|
|
case DT_RELASZ:
|
|
|
|
ef->relasz = dp[i].d_un.d_val;
|
|
|
|
break;
|
1998-10-09 23:18:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
if (ef->hashtab == NULL || ef->symtab == NULL ||
|
|
|
|
ef->strtab == NULL || ef->strsz == 0)
|
|
|
|
goto out;
|
|
|
|
COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
|
|
|
|
COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
|
|
|
|
ef->buckets = ef->hashtab + 2;
|
|
|
|
ef->chains = ef->buckets + ef->nbuckets;
|
|
|
|
if (elf_parse_modmetadata(fp, ef) == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (ef->kernel) /* kernel must not depend on anything */
|
1998-10-09 23:18:43 +00:00
|
|
|
goto out;
|
|
|
|
|
1998-09-30 19:38:26 +00:00
|
|
|
out:
|
1998-10-09 23:18:43 +00:00
|
|
|
if (dp)
|
|
|
|
free(dp);
|
|
|
|
if (shdr)
|
|
|
|
free(shdr);
|
1998-09-30 19:38:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2000-05-01 17:41:25 +00:00
|
|
|
|
|
|
|
static char invalid_name[] = "bad";
|
2001-09-11 01:09:24 +00:00
|
|
|
|
2000-05-01 17:41:25 +00:00
|
|
|
char *
|
2001-06-19 06:48:58 +00:00
|
|
|
fake_modname(const char *name)
|
|
|
|
{
|
2001-06-24 12:08:47 +00:00
|
|
|
const char *sp, *ep;
|
|
|
|
char *fp;
|
2000-08-03 09:14:02 +00:00
|
|
|
size_t len;
|
2000-05-01 17:41:25 +00:00
|
|
|
|
|
|
|
sp = strrchr(name, '/');
|
|
|
|
if (sp)
|
|
|
|
sp++;
|
|
|
|
else
|
|
|
|
sp = name;
|
|
|
|
ep = strrchr(name, '.');
|
|
|
|
if (ep) {
|
|
|
|
if (ep == name) {
|
|
|
|
sp = invalid_name;
|
|
|
|
ep = invalid_name + sizeof(invalid_name) - 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ep = name + strlen(name);
|
|
|
|
len = ep - sp;
|
2001-06-24 12:08:47 +00:00
|
|
|
fp = malloc(len + 1);
|
|
|
|
if (fp == NULL)
|
2000-05-01 17:41:25 +00:00
|
|
|
return NULL;
|
2001-06-24 12:08:47 +00:00
|
|
|
memcpy(fp, sp, len);
|
|
|
|
fp[len] = '\0';
|
|
|
|
return fp;
|
2000-05-01 17:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-06-19 06:48:58 +00:00
|
|
|
elf_parse_modmetadata(struct preloaded_file *fp, elf_file_t ef)
|
|
|
|
{
|
2000-05-01 17:41:25 +00:00
|
|
|
struct mod_metadata md;
|
2001-09-11 01:09:24 +00:00
|
|
|
struct mod_depend *mdepend;
|
|
|
|
struct mod_version mver;
|
2000-05-01 17:41:25 +00:00
|
|
|
Elf_Sym sym;
|
2001-06-19 07:41:07 +00:00
|
|
|
char *s, *v, **p, **p_stop;
|
2001-09-11 01:09:24 +00:00
|
|
|
int modcnt, minfolen;
|
2000-05-01 17:41:25 +00:00
|
|
|
|
2001-06-19 07:41:07 +00:00
|
|
|
if (elf_lookup_symbol(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
|
2000-05-01 17:41:25 +00:00
|
|
|
return ENOENT;
|
2001-06-19 07:41:07 +00:00
|
|
|
p = (char **)(sym.st_value + ef->off);
|
|
|
|
if (elf_lookup_symbol(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
|
|
|
|
return ENOENT;
|
|
|
|
p_stop = (char **)(sym.st_value + ef->off);
|
2000-05-01 17:41:25 +00:00
|
|
|
|
|
|
|
modcnt = 0;
|
2001-06-19 07:41:07 +00:00
|
|
|
while (p < p_stop) {
|
2003-01-21 05:46:46 +00:00
|
|
|
COPYOUT(p, &v, sizeof(v));
|
|
|
|
#ifdef __sparc64__
|
|
|
|
elf_reloc_ptr(fp, ef, p, &v, sizeof(v));
|
|
|
|
#else
|
|
|
|
v += ef->off;
|
|
|
|
#endif
|
|
|
|
COPYOUT(v, &md, sizeof(md));
|
|
|
|
#ifdef __sparc64__
|
|
|
|
elf_reloc_ptr(fp, ef, v, &md, sizeof(md));
|
|
|
|
#else
|
|
|
|
md.md_cval += ef->off;
|
|
|
|
md.md_data += ef->off;
|
|
|
|
#endif
|
|
|
|
p++;
|
2000-05-01 17:41:25 +00:00
|
|
|
switch(md.md_type) {
|
|
|
|
case MDT_DEPEND:
|
|
|
|
if (ef->kernel) /* kernel must not depend on anything */
|
|
|
|
break;
|
2003-01-21 05:46:46 +00:00
|
|
|
s = strdupout((vm_offset_t)md.md_cval);
|
2001-09-11 01:09:24 +00:00
|
|
|
minfolen = sizeof(*mdepend) + strlen(s) + 1;
|
|
|
|
mdepend = malloc(minfolen);
|
|
|
|
if (mdepend == NULL)
|
|
|
|
return ENOMEM;
|
2003-01-21 05:46:46 +00:00
|
|
|
COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
|
2001-09-11 01:09:24 +00:00
|
|
|
strcpy((char*)(mdepend + 1), s);
|
2000-05-01 17:41:25 +00:00
|
|
|
free(s);
|
2001-09-11 01:09:24 +00:00
|
|
|
file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
|
|
|
|
free(mdepend);
|
2000-05-01 17:41:25 +00:00
|
|
|
break;
|
|
|
|
case MDT_VERSION:
|
2003-01-21 05:46:46 +00:00
|
|
|
s = strdupout((vm_offset_t)md.md_cval);
|
|
|
|
COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
|
2001-09-11 01:09:24 +00:00
|
|
|
file_addmodule(fp, s, mver.mv_version, NULL);
|
2000-05-01 17:41:25 +00:00
|
|
|
free(s);
|
|
|
|
modcnt++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (modcnt == 0) {
|
|
|
|
s = fake_modname(fp->f_name);
|
2001-09-11 01:09:24 +00:00
|
|
|
file_addmodule(fp, s, 1, NULL);
|
2000-05-01 17:41:25 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
elf_hash(const char *name)
|
|
|
|
{
|
|
|
|
const unsigned char *p = (const unsigned char *) name;
|
|
|
|
unsigned long h = 0;
|
|
|
|
unsigned long g;
|
|
|
|
|
|
|
|
while (*p != '\0') {
|
|
|
|
h = (h << 4) + *p++;
|
|
|
|
if ((g = h & 0xf0000000) != 0)
|
|
|
|
h ^= g >> 24;
|
|
|
|
h &= ~g;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char elf_bad_symtable[] = "elf_lookup_symbol: corrupt symbol table\n";
|
|
|
|
int
|
|
|
|
elf_lookup_symbol(struct preloaded_file *fp, elf_file_t ef, const char* name,
|
|
|
|
Elf_Sym *symp)
|
|
|
|
{
|
2002-04-09 00:05:46 +00:00
|
|
|
Elf_Hashelt symnum;
|
2000-05-01 17:41:25 +00:00
|
|
|
Elf_Sym sym;
|
|
|
|
char *strp;
|
|
|
|
unsigned long hash;
|
|
|
|
|
|
|
|
hash = elf_hash(name);
|
|
|
|
COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
|
|
|
|
|
|
|
|
while (symnum != STN_UNDEF) {
|
|
|
|
if (symnum >= ef->nchains) {
|
|
|
|
printf(elf_bad_symtable);
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
|
|
|
|
if (sym.st_name == 0) {
|
|
|
|
printf(elf_bad_symtable);
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
|
|
|
|
if (strcmp(name, strp) == 0) {
|
|
|
|
free(strp);
|
|
|
|
if (sym.st_shndx != SHN_UNDEF ||
|
|
|
|
(sym.st_value != 0 &&
|
|
|
|
ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
|
|
|
|
*symp = sym;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
free(strp);
|
|
|
|
COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
|
|
|
|
}
|
|
|
|
return ENOENT;
|
|
|
|
}
|
2003-01-21 05:46:46 +00:00
|
|
|
|
|
|
|
#ifdef __sparc__
|
|
|
|
/*
|
|
|
|
* Apply any intra-module relocations to the value. *p is the load address
|
|
|
|
* of the value and val/len is the value to be modified. This does NOT modify
|
|
|
|
* the image in-place, because this is done by kern_linker later on.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
elf_reloc_ptr(struct preloaded_file *mp, elf_file_t ef,
|
|
|
|
void *p, void *val, size_t len)
|
|
|
|
{
|
|
|
|
Elf_Addr off = (Elf_Addr)p - ef->off, word;
|
|
|
|
size_t n;
|
|
|
|
Elf_Rela r;
|
|
|
|
|
|
|
|
for (n = 0; n < ef->relasz / sizeof(r); n++) {
|
|
|
|
COPYOUT(ef->rela + n, &r, sizeof(r));
|
|
|
|
|
|
|
|
if (r.r_offset >= off && r.r_offset < off + len &&
|
|
|
|
ELF_R_TYPE(r.r_info) == R_SPARC_RELATIVE) {
|
|
|
|
word = ef->off + r.r_addend;
|
|
|
|
bcopy(&word, (char *)val + (r.r_offset - off),
|
|
|
|
sizeof(word));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|