Home | History | Annotate | Line # | Download | only in kern
subr_kobj.c revision 1.2
      1 /*	$NetBSD: subr_kobj.c,v 1.2 2008/01/04 14:53:33 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the NetBSD
     18  *	Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*-
     37  * Copyright (c) 1998-2000 Doug Rabson
     38  * Copyright (c) 2004 Peter Wemm
     39  * All rights reserved.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  */
     62 
     63 /*
     64  * Kernel loader for ELF objects.
     65  *
     66  * TODO: adjust kmem_alloc() calls to avoid needless fragmentation.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.2 2008/01/04 14:53:33 ad Exp $");
     71 
     72 #define	ELFSIZE		ARCH_ELFSIZE
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/kernel.h>
     77 #include <sys/kmem.h>
     78 #include <sys/proc.h>
     79 #include <sys/namei.h>
     80 #include <sys/vnode.h>
     81 #include <sys/fcntl.h>
     82 #include <sys/kobj.h>
     83 #include <sys/ksyms.h>
     84 #include <sys/lkm.h>
     85 #include <sys/exec.h>
     86 #include <sys/exec_elf.h>
     87 
     88 #include <machine/stdarg.h>
     89 
     90 #include <uvm/uvm_extern.h>
     91 
     92 typedef struct {
     93 	void		*addr;
     94 	Elf_Off		size;
     95 	int		flags;
     96 	int		sec;		/* Original section */
     97 	const char	*name;
     98 } progent_t;
     99 
    100 typedef struct {
    101 	Elf_Rel		*rel;
    102 	int 		nrel;
    103 	int 		sec;
    104 	size_t		size;
    105 } relent_t;
    106 
    107 typedef struct {
    108 	Elf_Rela	*rela;
    109 	int		nrela;
    110 	int		sec;
    111 	size_t		size;
    112 } relaent_t;
    113 
    114 struct kobj {
    115 	char		ko_name[MAXLKMNAME];
    116 	vaddr_t		ko_address;	/* Relocation address */
    117 	Elf_Shdr	*ko_shdr;
    118 	progent_t	*ko_progtab;
    119 	relaent_t	*ko_relatab;
    120 	relent_t	*ko_reltab;
    121 	Elf_Sym		*ko_symtab;	/* Symbol table */
    122 	char		*ko_strtab;	/* String table */
    123 	uintptr_t	ko_entry;	/* Entry point */
    124 	size_t		ko_size;	/* Size of text/data/bss */
    125 	size_t		ko_symcnt;	/* Number of symbols */
    126 	size_t		ko_strtabsz;	/* Number of bytes in string table */
    127 	size_t		ko_shdrsz;
    128 	int		ko_nrel;
    129 	int		ko_nrela;
    130 	int		ko_nprogtab;
    131 	bool		ko_ksyms;
    132 };
    133 
    134 static int	kobj_relocate(kobj_t);
    135 static void	kobj_error(const char *, ...);
    136 static int	kobj_read(vnode_t *, void *, size_t, off_t);
    137 static void	kobj_release_mem(kobj_t);
    138 
    139 extern struct vm_map *lkm_map;
    140 static const char	*kobj_path = "/modules";	/* XXX ??? */
    141 
    142 /*
    143  * kobj_load:
    144  *
    145  *	Load an ELF object from the file system and link into the
    146  *	running	kernel image.
    147  */
    148 int
    149 kobj_load(const char *name, kobj_t *kop)
    150 {
    151 	struct nameidata nd;
    152 	kauth_cred_t cred;
    153 	Elf_Ehdr *hdr;
    154 	Elf_Shdr *shdr;
    155 	Elf_Sym *es;
    156 	vaddr_t mapbase;
    157 	size_t mapsize;
    158 	char *path;
    159 	int error;
    160 	int symtabindex;
    161 	int symstrindex;
    162 	int nsym;
    163 	int pb, rl, ra;
    164 	int alignmask;
    165 	int i, j;
    166 	kobj_t ko;
    167 
    168 	cred = kauth_cred_get();
    169 	shdr = NULL;
    170 	mapsize = 0;
    171 	error = 0;
    172 	hdr = NULL;
    173 
    174 	ko = kmem_zalloc(sizeof(*ko), KM_SLEEP);
    175 	if (ko == NULL) {
    176 		return ENOMEM;
    177 	}
    178 
    179 	/*
    180 	 * XXXAD should:
    181 	 * - have lkm code strip module name out of pathname
    182 	 * - take both name and pathname arguments
    183 	 * - be smarter about where to look - needs a policy
    184 	 */
    185 	if (strlcpy(ko->ko_name, name, sizeof(ko->ko_name)) >=
    186 	    sizeof(ko->ko_name)) {
    187 	    	error = EINVAL;
    188 	    	goto out;
    189 	}
    190 
    191 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name);
    192 	error = vn_open(&nd, FREAD, 0);
    193 	if (error != 0) {
    194 		if (error != ENOENT) {
    195 			goto out;
    196 		}
    197 		path = PNBUF_GET();
    198 		snprintf(path, MAXPATHLEN, "%s/%s", kobj_path, name);
    199 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path);
    200 		error = vn_open(&nd, FREAD, 0);
    201 		PNBUF_PUT(path);
    202 		if (error != 0) {
    203 			goto out;
    204 		}
    205 	}
    206 
    207 	/*
    208 	 * Read the elf header from the file.
    209 	 */
    210 	hdr = kmem_alloc(sizeof(*hdr), KM_SLEEP);
    211 	if (hdr == NULL) {
    212 		error = ENOMEM;
    213 		goto out;
    214 	}
    215 	error = kobj_read(nd.ni_vp, hdr, sizeof(*hdr), 0);
    216 	if (error != 0)
    217 		goto out;
    218 	if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) {
    219 		error = ENOEXEC;
    220 		goto out;
    221 	}
    222 
    223 	if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
    224 	    hdr->e_version != EV_CURRENT) {
    225 		kobj_error("unsupported file version");
    226 		error = ENOEXEC;
    227 		goto out;
    228 	}
    229 	if (hdr->e_type != ET_REL) {
    230 		kobj_error("unsupported file type");
    231 		error = ENOEXEC;
    232 		goto out;
    233 	}
    234 	switch (hdr->e_machine) {
    235 #if ELFSIZE == 32
    236 	ELF32_MACHDEP_ID_CASES
    237 #else
    238 	ELF64_MACHDEP_ID_CASES
    239 #endif
    240 	default:
    241 		kobj_error("unsupported machine");
    242 		error = ENOEXEC;
    243 		goto out;
    244 	}
    245 
    246 	ko->ko_nprogtab = 0;
    247 	ko->ko_shdr = 0;
    248 	ko->ko_nrel = 0;
    249 	ko->ko_nrela = 0;
    250 
    251 	/*
    252 	 * Allocate and read in the section header.
    253 	 */
    254 	ko->ko_shdrsz = hdr->e_shnum * hdr->e_shentsize;
    255 	if (ko->ko_shdrsz == 0 || hdr->e_shoff == 0 ||
    256 	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
    257 		error = ENOEXEC;
    258 		goto out;
    259 	}
    260 	shdr = kmem_alloc(ko->ko_shdrsz, KM_SLEEP);
    261 	if (shdr == NULL) {
    262 		error = ENOMEM;
    263 		goto out;
    264 	}
    265 	ko->ko_shdr = shdr;
    266 	error = kobj_read(nd.ni_vp, shdr, ko->ko_shdrsz, hdr->e_shoff);
    267 	if (error != 0) {
    268 		goto out;
    269 	}
    270 
    271 	/*
    272 	 * Scan the section header for information and table sizing.
    273 	 */
    274 	nsym = 0;
    275 	symtabindex = -1;
    276 	symstrindex = -1;
    277 	for (i = 0; i < hdr->e_shnum; i++) {
    278 		switch (shdr[i].sh_type) {
    279 		case SHT_PROGBITS:
    280 		case SHT_NOBITS:
    281 			ko->ko_nprogtab++;
    282 			break;
    283 		case SHT_SYMTAB:
    284 			nsym++;
    285 			symtabindex = i;
    286 			symstrindex = shdr[i].sh_link;
    287 			break;
    288 		case SHT_REL:
    289 			ko->ko_nrel++;
    290 			break;
    291 		case SHT_RELA:
    292 			ko->ko_nrela++;
    293 			break;
    294 		case SHT_STRTAB:
    295 			break;
    296 		}
    297 	}
    298 	if (ko->ko_nprogtab == 0) {
    299 		kobj_error("file has no contents");
    300 		error = ENOEXEC;
    301 		goto out;
    302 	}
    303 	if (nsym != 1) {
    304 		/* Only allow one symbol table for now */
    305 		kobj_error("file has no valid symbol table");
    306 		error = ENOEXEC;
    307 		goto out;
    308 	}
    309 	if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
    310 	    shdr[symstrindex].sh_type != SHT_STRTAB) {
    311 		kobj_error("file has invalid symbol strings");
    312 		error = ENOEXEC;
    313 		goto out;
    314 	}
    315 
    316 	/*
    317 	 * Allocate space for tracking the load chunks.
    318 	 */
    319 	if (ko->ko_nprogtab != 0) {
    320 		ko->ko_progtab = kmem_zalloc(ko->ko_nprogtab *
    321 		    sizeof(*ko->ko_progtab), KM_SLEEP);
    322 		if (ko->ko_progtab == NULL) {
    323 			error = ENOMEM;
    324 			goto out;
    325 		}
    326 	}
    327 	if (ko->ko_nrel != 0) {
    328 		ko->ko_reltab = kmem_zalloc(ko->ko_nrel *
    329 		    sizeof(*ko->ko_reltab), KM_SLEEP);
    330 		if (ko->ko_reltab == NULL) {
    331 			error = ENOMEM;
    332 			goto out;
    333 		}
    334 	}
    335 	if (ko->ko_nrela != 0) {
    336 		ko->ko_relatab = kmem_zalloc(ko->ko_nrela *
    337 		    sizeof(*ko->ko_relatab), KM_SLEEP);
    338 		if (ko->ko_relatab == NULL) {
    339 			error = ENOMEM;
    340 			goto out;
    341 		}
    342 	}
    343 	if (symtabindex == -1) {
    344 		kobj_error("lost symbol table index");
    345 		goto out;
    346 	}
    347 
    348 	/*
    349 	 * Allocate space for and load the symbol table.
    350 	 */
    351 	ko->ko_symcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
    352 	if (ko->ko_symcnt == 0) {
    353 		kobj_error("no symbol table");
    354 		goto out;
    355 	}
    356 	ko->ko_symtab = kmem_alloc(ko->ko_symcnt * sizeof(Elf_Sym), KM_SLEEP);
    357 	if (ko->ko_symtab == NULL) {
    358 		error = ENOMEM;
    359 		goto out;
    360 	}
    361 	error = kobj_read(nd.ni_vp, ko->ko_symtab, shdr[symtabindex].sh_size,
    362 	    shdr[symtabindex].sh_offset);
    363 	if (error != 0) {
    364 		goto out;
    365 	}
    366 
    367 	/*
    368 	 * Allocate space for and load the symbol strings.
    369 	 */
    370 	ko->ko_strtabsz = shdr[symstrindex].sh_size;
    371 	if (ko->ko_strtabsz == 0) {
    372 		kobj_error("no symbol strings");
    373 		goto out;
    374 	}
    375 	ko->ko_strtab = kmem_alloc(ko->ko_strtabsz, KM_SLEEP);
    376 	if (ko->ko_strtab == NULL) {
    377 		error = ENOMEM;
    378 		goto out;
    379 	}
    380 	error = kobj_read(nd.ni_vp, ko->ko_strtab, shdr[symstrindex].sh_size,
    381 	    shdr[symstrindex].sh_offset);
    382 	if (error != 0) {
    383 		goto out;
    384 	}
    385 
    386 	/*
    387 	 * Size up code/data(progbits) and bss(nobits).
    388 	 */
    389 	alignmask = 0;
    390 	for (i = 0; i < hdr->e_shnum; i++) {
    391 		switch (shdr[i].sh_type) {
    392 		case SHT_PROGBITS:
    393 		case SHT_NOBITS:
    394 			alignmask = shdr[i].sh_addralign - 1;
    395 			mapsize += alignmask;
    396 			mapsize &= ~alignmask;
    397 			mapsize += shdr[i].sh_size;
    398 			break;
    399 		}
    400 	}
    401 
    402 	/*
    403 	 * We know how much space we need for the text/data/bss/etc.
    404 	 * This stuff needs to be in a single chunk so that profiling etc
    405 	 * can get the bounds and gdb can associate offsets with modules.
    406 	 */
    407 	if (mapsize == 0) {
    408 		kobj_error("no text/data/bss");
    409 		goto out;
    410 	}
    411 	mapbase = uvm_km_alloc(lkm_map, round_page(mapsize), 0,
    412 	    UVM_KMF_WIRED | UVM_KMF_EXEC);
    413 	if (mapbase == 0) {
    414 		error = ENOMEM;
    415 		goto out;
    416 	}
    417 	ko->ko_address = mapbase;
    418 	ko->ko_size = mapsize;
    419 	ko->ko_entry = mapbase + hdr->e_entry;
    420 
    421 	/*
    422 	 * Now load code/data(progbits), zero bss(nobits), allocate space
    423 	 * for and load relocs
    424 	 */
    425 	pb = 0;
    426 	rl = 0;
    427 	ra = 0;
    428 	alignmask = 0;
    429 	for (i = 0; i < hdr->e_shnum; i++) {
    430 		switch (shdr[i].sh_type) {
    431 		case SHT_PROGBITS:
    432 		case SHT_NOBITS:
    433 			alignmask = shdr[i].sh_addralign - 1;
    434 			mapbase += alignmask;
    435 			mapbase &= ~alignmask;
    436 			ko->ko_progtab[pb].addr = (void *)mapbase;
    437 			if (shdr[i].sh_type == SHT_PROGBITS) {
    438 				ko->ko_progtab[pb].name = "<<PROGBITS>>";
    439 				error = kobj_read(nd.ni_vp,
    440 				    ko->ko_progtab[pb].addr, shdr[i].sh_size,
    441 				    shdr[i].sh_offset);
    442 				if (error != 0) {
    443 					goto out;
    444 				}
    445 			} else {
    446 				ko->ko_progtab[pb].name = "<<NOBITS>>";
    447 				memset(ko->ko_progtab[pb].addr, 0,
    448 				    shdr[i].sh_size);
    449 			}
    450 			ko->ko_progtab[pb].size = shdr[i].sh_size;
    451 			ko->ko_progtab[pb].sec = i;
    452 
    453 			/* Update all symbol values with the offset. */
    454 			for (j = 0; j < ko->ko_symcnt; j++) {
    455 				es = &ko->ko_symtab[j];
    456 				if (es->st_shndx != i) {
    457 					continue;
    458 				}
    459 				es->st_value +=
    460 				    (Elf_Addr)ko->ko_progtab[pb].addr;
    461 			}
    462 			mapbase += shdr[i].sh_size;
    463 			pb++;
    464 			break;
    465 		case SHT_REL:
    466 			ko->ko_reltab[rl].size = shdr[i].sh_size;
    467 			ko->ko_reltab[rl].size -=
    468 			    shdr[i].sh_size % sizeof(Elf_Rel);
    469 			if (ko->ko_reltab[rl].size != 0) {
    470 				ko->ko_reltab[rl].rel =
    471 				    kmem_alloc(ko->ko_reltab[rl].size,
    472 				    KM_SLEEP);
    473 				ko->ko_reltab[rl].nrel =
    474 				    shdr[i].sh_size / sizeof(Elf_Rel);
    475 				ko->ko_reltab[rl].sec = shdr[i].sh_info;
    476 				error = kobj_read(nd.ni_vp,
    477 				    ko->ko_reltab[rl].rel,
    478 				    ko->ko_reltab[rl].size,
    479 				    shdr[i].sh_offset);
    480 				if (error != 0) {
    481 					goto out;
    482 				}
    483 			}
    484 			rl++;
    485 			break;
    486 		case SHT_RELA:
    487 			ko->ko_relatab[ra].size = shdr[i].sh_size;
    488 			ko->ko_relatab[ra].size -=
    489 			    shdr[i].sh_size % sizeof(Elf_Rela);
    490 			if (ko->ko_relatab[ra].size != 0) {
    491 				ko->ko_relatab[ra].rela =
    492 				    kmem_alloc(ko->ko_relatab[ra].size,
    493 				    KM_SLEEP);
    494 				ko->ko_relatab[ra].nrela =
    495 				    shdr[i].sh_size / sizeof(Elf_Rela);
    496 				ko->ko_relatab[ra].sec = shdr[i].sh_info;
    497 				error = kobj_read(nd.ni_vp,
    498 				    ko->ko_relatab[ra].rela,
    499 				    shdr[i].sh_size,
    500 				    shdr[i].sh_offset);
    501 				if (error != 0) {
    502 					goto out;
    503 				}
    504 			}
    505 			ra++;
    506 			break;
    507 		}
    508 	}
    509 	if (pb != ko->ko_nprogtab) {
    510 		panic("lost progbits");
    511 	}
    512 	if (rl != ko->ko_nrel) {
    513 		panic("lost rel");
    514 	}
    515 	if (ra != ko->ko_nrela) {
    516 		panic("lost rela");
    517 	}
    518 	if (mapbase != ko->ko_address + mapsize) {
    519 		panic("mapbase 0x%lx != address %lx + mapsize 0x%lx (0x%lx)\n",
    520 		    (long)mapbase, (long)ko->ko_address, (long)mapsize,
    521 		    (long)ko->ko_address + mapsize);
    522 	}
    523 
    524 	/*
    525 	 * Perform relocations.  Done before registering with ksyms,
    526 	 * which will pack our symbol table.
    527 	 */
    528 	error = kobj_relocate(ko);
    529 	if (error != 0) {
    530 		goto out;
    531 	}
    532 
    533 	/*
    534 	 * Register symbol table with ksyms.
    535 	 */
    536 	error = ksyms_addsymtab(ko->ko_name, ko->ko_symtab, ko->ko_symcnt *
    537 	    sizeof(Elf_Sym), ko->ko_strtab, ko->ko_strtabsz);
    538 	if (error != 0) {
    539 		kobj_error("unable to register module symbol table");
    540 		goto out;
    541 	}
    542 	ko->ko_ksyms = true;
    543 
    544 	/*
    545 	 * Notify MD code that a module has been loaded.
    546 	 */
    547 	error = kobj_machdep(ko, (void *)ko->ko_address, ko->ko_size, true);
    548 	if (error != 0) {
    549 		kobj_error("machine dependent init failed");
    550 		goto out;
    551 	}
    552 	*kop = ko;
    553  out:
    554 	kobj_release_mem(ko);
    555 	if (error != 0 && ko)
    556 		kobj_unload(ko);
    557 	if (hdr != NULL)
    558 		kmem_free(hdr, sizeof(*hdr));
    559 	if (nd.ni_vp != NULL) {
    560 		VOP_UNLOCK(nd.ni_vp, 0);
    561 		vn_close(nd.ni_vp, FREAD, cred, curlwp);
    562 	}
    563 
    564 	return error;
    565 }
    566 
    567 /*
    568  * kobj_unload:
    569  *
    570  *	Unload an object previously loaded by kobj_load().
    571  */
    572 void
    573 kobj_unload(kobj_t ko)
    574 {
    575 	int error;
    576 
    577 	if (ko->ko_address != 0) {
    578 		uvm_km_free(lkm_map, ko->ko_address, round_page(ko->ko_size),
    579 		    UVM_KMF_WIRED);
    580 	}
    581 	if (ko->ko_ksyms == true) {
    582 		ksyms_delsymtab(ko->ko_name);
    583 	}
    584 	if (ko->ko_symtab != NULL) {
    585 		kmem_free(ko->ko_symtab, ko->ko_symcnt * sizeof(Elf_Sym));
    586 	}
    587 	if (ko->ko_strtab != NULL) {
    588 		kmem_free(ko->ko_strtab, ko->ko_strtabsz);
    589 	}
    590 	kmem_free(ko, sizeof(*ko));
    591 
    592 	/*
    593 	 * Notify MD code that a module has been unloaded.
    594 	 */
    595 	error = kobj_machdep(ko, (void *)ko->ko_address, ko->ko_size, false);
    596 	if (error != 0) {
    597 		kobj_error("machine dependent deinit failed");
    598 	}
    599 }
    600 
    601 /*
    602  * kobj_stat:
    603  *
    604  *	Return size and load address of an object.
    605  */
    606 void
    607 kobj_stat(kobj_t ko, vaddr_t *address, size_t *size, uintptr_t *entry)
    608 {
    609 
    610 	if (address != NULL) {
    611 		*address = ko->ko_address;
    612 	}
    613 	if (size != NULL) {
    614 		*size = ko->ko_size;
    615 	}
    616 	if (entry != NULL) {
    617 		*entry = ko->ko_entry;
    618 	}
    619 }
    620 
    621 /*
    622  * kobj_release_mem:
    623  *
    624  *	Release object data not needed after loading.
    625  */
    626 static void
    627 kobj_release_mem(kobj_t ko)
    628 {
    629 	int i;
    630 
    631 	for (i = 0; i < ko->ko_nrel; i++) {
    632 		if (ko->ko_reltab[i].rel) {
    633 			kmem_free(ko->ko_reltab[i].rel,
    634 			    ko->ko_reltab[i].size);
    635 		}
    636 	}
    637 	for (i = 0; i < ko->ko_nrela; i++) {
    638 		if (ko->ko_relatab[i].rela) {
    639 			kmem_free(ko->ko_relatab[i].rela,
    640 			    ko->ko_relatab[i].size);
    641 		}
    642 	}
    643 	if (ko->ko_reltab != NULL) {
    644 		kmem_free(ko->ko_reltab, ko->ko_nrel *
    645 		    sizeof(*ko->ko_reltab));
    646 		ko->ko_reltab = NULL;
    647 		ko->ko_nrel = 0;
    648 	}
    649 	if (ko->ko_relatab != NULL) {
    650 		kmem_free(ko->ko_relatab, ko->ko_nrela *
    651 		    sizeof(*ko->ko_relatab));
    652 		ko->ko_relatab = NULL;
    653 		ko->ko_nrela = 0;
    654 	}
    655 	if (ko->ko_progtab != NULL) {
    656 		kmem_free(ko->ko_progtab, ko->ko_nprogtab *
    657 		    sizeof(*ko->ko_progtab));
    658 		ko->ko_progtab = NULL;
    659 	}
    660 	if (ko->ko_shdr != NULL) {
    661 		kmem_free(ko->ko_shdr, ko->ko_shdrsz);
    662 		ko->ko_shdr = NULL;
    663 	}
    664 }
    665 
    666 /*
    667  * kobj_sym_lookup:
    668  *
    669  *	Symbol lookup function to be used when the symbol index
    670  *	is known (ie during relocation).
    671  */
    672 uintptr_t
    673 kobj_sym_lookup(kobj_t ko, uintptr_t symidx)
    674 {
    675 	const Elf_Sym *sym;
    676 	const char *symbol;
    677 	int error;
    678 	u_long addr;
    679 
    680 	/* Don't even try to lookup the symbol if the index is bogus. */
    681 	if (symidx >= ko->ko_symcnt)
    682 		return 0;
    683 
    684 	sym = ko->ko_symtab + symidx;
    685 
    686 	/* Quick answer if there is a definition included. */
    687 	if (sym->st_shndx != SHN_UNDEF) {
    688 		return sym->st_value;
    689 	}
    690 
    691 	/* If we get here, then it is undefined and needs a lookup. */
    692 	switch (ELF_ST_BIND(sym->st_info)) {
    693 	case STB_LOCAL:
    694 		/* Local, but undefined? huh? */
    695 		kobj_error("local symbol undefined");
    696 		return 0;
    697 
    698 	case STB_GLOBAL:
    699 		/* Relative to Data or Function name */
    700 		symbol = ko->ko_strtab + sym->st_name;
    701 
    702 		/* Force a lookup failure if the symbol name is bogus. */
    703 		if (*symbol == 0) {
    704 			kobj_error("bad symbol name");
    705 			return 0;
    706 		}
    707 
    708 		error = ksyms_getval(NULL, symbol, &addr, KSYMS_ANY);
    709 		if (error != 0) {
    710 			kobj_error("symbol %s undefined", symbol);
    711 			return (uintptr_t)0;
    712 		}
    713 		return (uintptr_t)addr;
    714 
    715 	case STB_WEAK:
    716 		kobj_error("weak symbols not supported\n");
    717 		return 0;
    718 
    719 	default:
    720 		return 0;
    721 	}
    722 }
    723 
    724 /*
    725  * kobj_findbase:
    726  *
    727  *	Return base address of the given section.
    728  */
    729 static uintptr_t
    730 kobj_findbase(kobj_t ko, int sec)
    731 {
    732 	int i;
    733 
    734 	for (i = 0; i < ko->ko_nprogtab; i++) {
    735 		if (sec == ko->ko_progtab[i].sec) {
    736 			return (uintptr_t)ko->ko_progtab[i].addr;
    737 		}
    738 	}
    739 	return 0;
    740 }
    741 
    742 /*
    743  * kobj_relocate:
    744  *
    745  *	Resolve all relocations for the loaded object.
    746  */
    747 static int
    748 kobj_relocate(kobj_t ko)
    749 {
    750 	const Elf_Rel *rellim;
    751 	const Elf_Rel *rel;
    752 	const Elf_Rela *relalim;
    753 	const Elf_Rela *rela;
    754 	const Elf_Sym *sym;
    755 	uintptr_t base;
    756 	int i;
    757 	uintptr_t symidx;
    758 
    759 	/*
    760 	 * Perform relocations without addend if there are any.
    761 	 */
    762 	for (i = 0; i < ko->ko_nrel; i++) {
    763 		rel = ko->ko_reltab[i].rel;
    764 		if (rel == NULL) {
    765 			continue;
    766 		}
    767 		rellim = rel + ko->ko_reltab[i].nrel;
    768 		base = kobj_findbase(ko, ko->ko_reltab[i].sec);
    769 		if (base == 0) {
    770 			panic("lost base for e_reltab");
    771 		}
    772 		for (; rel < rellim; rel++) {
    773 			symidx = ELF_R_SYM(rel->r_info);
    774 			if (symidx >= ko->ko_symcnt) {
    775 				continue;
    776 			}
    777 			sym = ko->ko_symtab + symidx;
    778 			if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
    779 				kobj_reloc(ko, base, rel, false, true);
    780 				continue;
    781 			}
    782 			if (kobj_reloc(ko, base, rel, false, false)) {
    783 				return ENOENT;
    784 			}
    785 		}
    786 	}
    787 
    788 	/*
    789 	 * Perform relocations with addend if there are any.
    790 	 */
    791 	for (i = 0; i < ko->ko_nrela; i++) {
    792 		rela = ko->ko_relatab[i].rela;
    793 		if (rela == NULL) {
    794 			continue;
    795 		}
    796 		relalim = rela + ko->ko_relatab[i].nrela;
    797 		base = kobj_findbase(ko, ko->ko_relatab[i].sec);
    798 		if (base == 0) {
    799 			panic("lost base for e_relatab");
    800 		}
    801 		for (; rela < relalim; rela++) {
    802 			symidx = ELF_R_SYM(rela->r_info);
    803 			if (symidx >= ko->ko_symcnt) {
    804 				continue;
    805 			}
    806 			sym = ko->ko_symtab + symidx;
    807 			if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
    808 				kobj_reloc(ko, base, rela, true, true);
    809 				continue;
    810 			}
    811 			if (kobj_reloc(ko, base, rela, true, false)) {
    812 				return ENOENT;
    813 			}
    814 		}
    815 	}
    816 
    817 	return 0;
    818 }
    819 
    820 /*
    821  * kobj_error:
    822  *
    823  *	Utility function: log an error.
    824  */
    825 static void
    826 kobj_error(const char *fmt, ...)
    827 {
    828 	va_list ap;
    829 
    830 	va_start(ap, fmt);
    831 	printf("WARNING: linker error: ");
    832 	vprintf(fmt, ap);
    833 	printf("\n");
    834 	va_end(ap);
    835 }
    836 
    837 /*
    838  * kobj_read:
    839  *
    840  *	Utility function: read from the object.
    841  */
    842 static int
    843 kobj_read(vnode_t *vp, void *base, size_t size, off_t off)
    844 {
    845 	size_t resid;
    846 	int error;
    847 
    848 	error = vn_rdwr(UIO_READ, vp, base, size, off, UIO_SYSSPACE,
    849 	    IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
    850 	if (error == 0 && resid != 0)
    851 		error = EINVAL;
    852 	return error;
    853 }
    854