/*
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software developed for The NetBSD Foundation
* by Andrew Doran.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Copyright (c) 1998-2000 Doug Rabson
* Copyright (c) 2004 Peter Wemm
* 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.
*/
/*
* Kernel loader for ELF objects.
*
* TODO: adjust kmem_alloc() calls to avoid needless fragmentation.
*/
/*
* kobj_load_mem:
*
* Load an object already resident in memory. If size is not -1,
* the complete size of the object is known.
*/
int
kobj_load_mem(kobj_t *kop, const char *name, void *base, ssize_t size)
{
kobj_t ko;
/*
* kobj_close:
*
* Close an open ELF object.
*/
static void
kobj_close(kobj_t ko)
{
if (ko->ko_source == NULL) {
return;
}
ko->ko_close(ko);
ko->ko_source = NULL;
}
static void
kobj_close_mem(kobj_t ko)
{
return;
}
/*
* kobj_load:
*
* Load an ELF object and prepare to link into the running kernel
* image.
*/
int
kobj_load(kobj_t ko)
{
Elf_Ehdr *hdr;
Elf_Shdr *shdr;
Elf_Sym *es;
vaddr_t map_text_base;
vaddr_t map_data_base;
vaddr_t map_rodata_base;
size_t map_text_size;
size_t map_data_size;
size_t map_rodata_size;
int error;
int symtabindex;
int symstrindex;
int nsym;
int pb, rl, ra;
int alignmask;
int i, j;
void *addr;
/*
* Scan the section header for information and table sizing.
*/
nsym = 0;
symtabindex = symstrindex = -1;
for (i = 0; i < hdr->e_shnum; i++) {
switch (shdr[i].sh_type) {
case SHT_PROGBITS:
case SHT_NOBITS:
ko->ko_nprogtab++;
break;
case SHT_SYMTAB:
nsym++;
symtabindex = i;
symstrindex = shdr[i].sh_link;
break;
case SHT_REL:
if (shdr[shdr[i].sh_info].sh_type != SHT_PROGBITS)
continue;
ko->ko_nrel++;
break;
case SHT_RELA:
if (shdr[shdr[i].sh_info].sh_type != SHT_PROGBITS)
continue;
ko->ko_nrela++;
break;
case SHT_STRTAB:
break;
}
}
if (ko->ko_nprogtab == 0) {
kobj_error(ko, "file has no contents");
error = ENOEXEC;
goto out;
}
if (nsym != 1) {
/* Only allow one symbol table for now */
kobj_error(ko, "file has no valid symbol table");
error = ENOEXEC;
goto out;
}
KASSERT(symtabindex != -1);
KASSERT(symstrindex != -1);
if (symstrindex == SHN_UNDEF || symstrindex >= hdr->e_shnum ||
shdr[symstrindex].sh_type != SHT_STRTAB) {
kobj_error(ko, "file has invalid symbol strings");
error = ENOEXEC;
goto out;
}
/*
* Allocate space for tracking the load chunks.
*/
if (ko->ko_nprogtab != 0) {
ko->ko_progtab = kmem_zalloc(ko->ko_nprogtab *
sizeof(*ko->ko_progtab), KM_SLEEP);
if (ko->ko_progtab == NULL) {
error = ENOMEM;
kobj_error(ko, "out of memory");
goto out;
}
}
if (ko->ko_nrel != 0) {
ko->ko_reltab = kmem_zalloc(ko->ko_nrel *
sizeof(*ko->ko_reltab), KM_SLEEP);
if (ko->ko_reltab == NULL) {
error = ENOMEM;
kobj_error(ko, "out of memory");
goto out;
}
}
if (ko->ko_nrela != 0) {
ko->ko_relatab = kmem_zalloc(ko->ko_nrela *
sizeof(*ko->ko_relatab), KM_SLEEP);
if (ko->ko_relatab == NULL) {
error = ENOMEM;
kobj_error(ko, "out of memory");
goto out;
}
}
/*
* Allocate space for and load the symbol table.
*/
ko->ko_symcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
if (ko->ko_symcnt == 0) {
kobj_error(ko, "no symbol table");
error = ENOEXEC;
goto out;
}
error = ko->ko_read(ko, (void **)&ko->ko_symtab,
ko->ko_symcnt * sizeof(Elf_Sym),
shdr[symtabindex].sh_offset, true);
if (error != 0) {
kobj_error(ko, "read failed %d", error);
goto out;
}
/*
* Allocate space for and load the symbol strings.
*/
ko->ko_strtabsz = shdr[symstrindex].sh_size;
if (ko->ko_strtabsz == 0) {
kobj_error(ko, "no symbol strings");
error = ENOEXEC;
goto out;
}
error = ko->ko_read(ko, (void *)&ko->ko_strtab, ko->ko_strtabsz,
shdr[symstrindex].sh_offset, true);
if (error != 0) {
kobj_error(ko, "read failed %d", error);
goto out;
}
/*
* Adjust module symbol namespace, if necessary (e.g. with rump)
*/
error = kobj_renamespace(ko->ko_symtab, ko->ko_symcnt,
&ko->ko_strtab, &ko->ko_strtabsz);
if (error != 0) {
kobj_error(ko, "renamespace failed %d", error);
goto out;
}
/*
* Do we have a string table for the section names?
*/
if (hdr->e_shstrndx != SHN_UNDEF) {
if (hdr->e_shstrndx >= hdr->e_shnum) {
kobj_error(ko, "bad shstrndx");
error = ENOEXEC;
goto out;
}
if (shdr[hdr->e_shstrndx].sh_size != 0 &&
shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
ko->ko_shstrtabsz = shdr[hdr->e_shstrndx].sh_size;
error = ko->ko_read(ko, (void **)&ko->ko_shstrtab,
shdr[hdr->e_shstrndx].sh_size,
shdr[hdr->e_shstrndx].sh_offset, true);
if (error != 0) {
kobj_error(ko, "read failed %d", error);
goto out;
}
}
}
/*
* Perform local relocations only. Relocations relating to global
* symbols will be done by kobj_affix().
*/
error = kobj_checksyms(ko, false);
if (error)
goto out;
error = kobj_relocate(ko, true);
if (error)
goto out;
out:
if (hdr != NULL) {
kobj_free(ko, hdr, sizeof(*hdr));
}
kobj_close(ko);
if (error != 0) {
kobj_unload(ko);
}
int error = kobj_machdep(ko, (void *)addr, size, false);
if (error)
kobj_error(ko, "machine dependent deinit failed (%s) %d",
note, error);
}
#define KOBJ_SEGMENT_NOTIFY(ko, what) \
kobj_unload_notify(ko, (ko)->ko_ ## what ## _address, \
(ko)->ko_ ## what ## _size, # what);
#define KOBJ_SEGMENT_FREE(ko, what) \
do \
if ((ko)->ko_ ## what ## _address != 0) \
uvm_km_free(module_map, (ko)->ko_ ## what ## _address, \
round_page((ko)->ko_ ## what ## _size), UVM_KMF_WIRED); \
while (/*CONSTCOND*/ 0)
/*
* kobj_unload:
*
* Unload an object previously loaded by kobj_load().
*/
void
kobj_unload(kobj_t ko)
{
kobj_close(ko);
kobj_jettison(ko);
/*
* Notify MD code that a module has been unloaded.
*/
if (ko->ko_loaded) {
KOBJ_SEGMENT_NOTIFY(ko, text);
KOBJ_SEGMENT_NOTIFY(ko, data);
KOBJ_SEGMENT_NOTIFY(ko, rodata);
}
/*
* kobj_affix:
*
* Set an object's name and perform global relocs. May only be
* called after the module and any requisite modules are loaded.
*/
int
kobj_affix(kobj_t ko, const char *name)
{
int error;
/* Cache addresses of undefined symbols. */
error = kobj_checksyms(ko, true);
if (error)
goto out;
/* Now do global relocations. */
error = kobj_relocate(ko, false);
if (error)
goto out;
/*
* Now that we know the name, register the symbol table.
* Do after global relocations because ksyms will pack
* the table.
*/
ksyms_modload(ko->ko_name, ko->ko_symtab,
ko->ko_symcnt * sizeof(Elf_Sym), ko->ko_strtab, ko->ko_strtabsz);
ko->ko_ksyms = true;
/*
* Notify MD code that a module has been loaded.
*
* Most architectures use this opportunity to flush their caches.
*/
if (ko->ko_text_address != 0) {
error = kobj_machdep(ko, (void *)ko->ko_text_address,
ko->ko_text_size, true);
if (error) {
kobj_error(ko, "machine dependent init failed (text)"
" %d", error);
goto out;
}
}
/* Change the memory protections, when needed. */
if (ko->ko_text_address != 0) {
uvm_km_protect(module_map, ko->ko_text_address,
ko->ko_text_size, VM_PROT_READ|VM_PROT_EXECUTE);
}
if (ko->ko_rodata_address != 0) {
uvm_km_protect(module_map, ko->ko_rodata_address,
ko->ko_rodata_size, VM_PROT_READ);
}
/* Success! */
error = 0;
out: if (error) {
/* If there was an error, destroy the whole object. */
kobj_unload(ko);
}
return error;
}
/*
* kobj_find_section:
*
* Given a section name, search the loaded object and return
* virtual address if present and loaded.
*/
int
kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size)
{
int i;
KASSERT(ko->ko_progtab != NULL);
for (i = 0; i < ko->ko_nprogtab; i++) {
if (strcmp(ko->ko_progtab[i].name, name) == 0) {
if (addr != NULL) {
*addr = ko->ko_progtab[i].addr;
}
if (size != NULL) {
*size = ko->ko_progtab[i].size;
}
return 0;
}
}
return ENOENT;
}
/*
* kobj_jettison:
*
* Release object data not needed after performing relocations.
*/
static void
kobj_jettison(kobj_t ko)
{
int i;
if (ko->ko_reltab != NULL) {
for (i = 0; i < ko->ko_nrel; i++) {
if (ko->ko_reltab[i].rel) {
kobj_free(ko, ko->ko_reltab[i].rel,
ko->ko_reltab[i].size);
}
}
kobj_free(ko, ko->ko_reltab, ko->ko_nrel *
sizeof(*ko->ko_reltab));
ko->ko_reltab = NULL;
ko->ko_nrel = 0;
}
if (ko->ko_relatab != NULL) {
for (i = 0; i < ko->ko_nrela; i++) {
if (ko->ko_relatab[i].rela) {
kobj_free(ko, ko->ko_relatab[i].rela,
ko->ko_relatab[i].size);
}
}
kobj_free(ko, ko->ko_relatab, ko->ko_nrela *
sizeof(*ko->ko_relatab));
ko->ko_relatab = NULL;
ko->ko_nrela = 0;
}
if (ko->ko_shdr != NULL) {
kobj_free(ko, ko->ko_shdr, ko->ko_shdrsz);
ko->ko_shdr = NULL;
}
}
/*
* kobj_sym_lookup:
*
* Symbol lookup function to be used when the symbol index
* is known (ie during relocation).
*/
int
kobj_sym_lookup(kobj_t ko, uintptr_t symidx, Elf_Addr *val)
{
const Elf_Sym *sym;
const char *symbol;
sym = ko->ko_symtab + symidx;
if (symidx == SHN_ABS || symidx == 0) {
*val = (uintptr_t)sym->st_value;
return 0;
} else if (symidx >= ko->ko_symcnt) {
/*
* Don't even try to lookup the symbol if the index is
* bogus.
*/
kobj_error(ko, "symbol index %ju out of range",
(uintmax_t)symidx);
return EINVAL;
}
/* Quick answer if there is a definition included. */
if (sym->st_shndx != SHN_UNDEF) {
*val = (uintptr_t)sym->st_value;
return 0;
}
/* If we get here, then it is undefined and needs a lookup. */
switch (ELF_ST_BIND(sym->st_info)) {
case STB_LOCAL:
/* Local, but undefined? huh? */
kobj_error(ko, "local symbol @%ju undefined",
(uintmax_t)symidx);
return EINVAL;
case STB_GLOBAL:
/* Relative to Data or Function name */
symbol = ko->ko_strtab + sym->st_name;
/* Force a lookup failure if the symbol name is bogus. */
if (*symbol == 0) {
kobj_error(ko, "bad symbol @%ju name",
(uintmax_t)symidx);
return EINVAL;
}
if (sym->st_value == 0) {
kobj_error(ko, "%s @%ju: bad value", symbol,
(uintmax_t)symidx);
return EINVAL;
}
*val = (uintptr_t)sym->st_value;
return 0;
case STB_WEAK:
kobj_error(ko, "weak symbol @%ju not supported",
(uintmax_t)symidx);
return EINVAL;
default:
kobj_error(ko, "bad binding %#x for symbol @%ju",
ELF_ST_BIND(sym->st_info), (uintmax_t)symidx);
return EINVAL;
}
}
/*
* kobj_findbase:
*
* Return base address of the given section.
*/
static uintptr_t
kobj_findbase(kobj_t ko, int sec)
{
int i;
for (i = 0; i < ko->ko_nprogtab; i++) {
if (sec == ko->ko_progtab[i].sec) {
return (uintptr_t)ko->ko_progtab[i].addr;
}
}
return 0;
}
/*
* kobj_checksyms:
*
* Scan symbol table for duplicates or resolve references to
* external symbols.
*/
static int
kobj_checksyms(kobj_t ko, bool undefined)
{
unsigned long rval;
Elf_Sym *sym, *ksym, *ms;
const char *name;
int error;
error = 0;
for (ms = (sym = ko->ko_symtab) + ko->ko_symcnt; sym < ms; sym++) {
/* Check validity of the symbol. */
if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL ||
sym->st_name == 0)
continue;
if (undefined != (sym->st_shndx == SHN_UNDEF)) {
continue;
}
/*
* Look it up. Don't need to lock, as it is known that
* the symbol tables aren't going to change (we hold
* module_lock).
*/
name = ko->ko_strtab + sym->st_name;
if (ksyms_getval_unlocked(NULL, name, &ksym, &rval,
KSYMS_EXTERN) != 0) {
if (undefined) {
kobj_error(ko, "symbol `%s' not found",
name);
error = ENOEXEC;
}
continue;
}
/* Save values of undefined globals. */
if (undefined) {
if (ksym->st_shndx == SHN_ABS) {
sym->st_shndx = SHN_ABS;
}
sym->st_value = (Elf_Addr)rval;
continue;
}