Home | History | Annotate | Line # | Download | only in kern
subr_kobj.c revision 1.57
      1 /*	$NetBSD: subr_kobj.c,v 1.57 2016/07/20 13:36:19 maxv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software developed for The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998-2000 Doug Rabson
     34  * Copyright (c) 2004 Peter Wemm
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56  * SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * Kernel loader for ELF objects.
     61  *
     62  * TODO: adjust kmem_alloc() calls to avoid needless fragmentation.
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 __KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.57 2016/07/20 13:36:19 maxv Exp $");
     67 
     68 #ifdef _KERNEL_OPT
     69 #include "opt_modular.h"
     70 #endif
     71 
     72 #include <sys/kobj_impl.h>
     73 
     74 #ifdef MODULAR
     75 
     76 #include <sys/param.h>
     77 #include <sys/kernel.h>
     78 #include <sys/kmem.h>
     79 #include <sys/proc.h>
     80 #include <sys/ksyms.h>
     81 #include <sys/module.h>
     82 
     83 #include <uvm/uvm_extern.h>
     84 
     85 #define kobj_error(_kobj, ...) \
     86 	kobj_out(__func__, __LINE__, _kobj, __VA_ARGS__)
     87 
     88 static int	kobj_relocate(kobj_t, bool);
     89 static int	kobj_checksyms(kobj_t, bool);
     90 static void	kobj_out(const char *, int, kobj_t, const char *, ...)
     91     __printflike(4, 5);
     92 static void	kobj_jettison(kobj_t);
     93 static void	kobj_free(kobj_t, void *, size_t);
     94 static void	kobj_close(kobj_t);
     95 static int	kobj_read_mem(kobj_t, void **, size_t, off_t, bool);
     96 static void	kobj_close_mem(kobj_t);
     97 
     98 extern struct vm_map *module_map;
     99 
    100 /*
    101  * kobj_load_mem:
    102  *
    103  *	Load an object already resident in memory.  If size is not -1,
    104  *	the complete size of the object is known.
    105  */
    106 int
    107 kobj_load_mem(kobj_t *kop, const char *name, void *base, ssize_t size)
    108 {
    109 	kobj_t ko;
    110 
    111 	ko = kmem_zalloc(sizeof(*ko), KM_SLEEP);
    112 	if (ko == NULL) {
    113 		return ENOMEM;
    114 	}
    115 
    116 	ko->ko_type = KT_MEMORY;
    117 	kobj_setname(ko, name);
    118 	ko->ko_source = base;
    119 	ko->ko_memsize = size;
    120 	ko->ko_read = kobj_read_mem;
    121 	ko->ko_close = kobj_close_mem;
    122 
    123 	*kop = ko;
    124 	return kobj_load(ko);
    125 }
    126 
    127 /*
    128  * kobj_close:
    129  *
    130  *	Close an open ELF object.
    131  */
    132 static void
    133 kobj_close(kobj_t ko)
    134 {
    135 
    136 	if (ko->ko_source == NULL) {
    137 		return;
    138 	}
    139 
    140 	ko->ko_close(ko);
    141 	ko->ko_source = NULL;
    142 }
    143 
    144 static void
    145 kobj_close_mem(kobj_t ko)
    146 {
    147 
    148 	return;
    149 }
    150 
    151 /*
    152  * kobj_load:
    153  *
    154  *	Load an ELF object and prepare to link into the running kernel
    155  *	image.
    156  */
    157 int
    158 kobj_load(kobj_t ko)
    159 {
    160 	Elf_Ehdr *hdr;
    161 	Elf_Shdr *shdr;
    162 	Elf_Sym *es;
    163 	vaddr_t map_text_base;
    164 	vaddr_t map_data_base;
    165 	vaddr_t map_rodata_base;
    166 	size_t map_text_size;
    167 	size_t map_data_size;
    168 	size_t map_rodata_size;
    169 	int error;
    170 	int symtabindex;
    171 	int symstrindex;
    172 	int nsym;
    173 	int pb, rl, ra;
    174 	int alignmask;
    175 	int i, j;
    176 	void *addr;
    177 
    178 	KASSERT(ko->ko_type != KT_UNSET);
    179 	KASSERT(ko->ko_source != NULL);
    180 
    181 	shdr = NULL;
    182 	error = 0;
    183 	hdr = NULL;
    184 
    185 	/*
    186 	 * Read the elf header from the file.
    187 	 */
    188 	error = ko->ko_read(ko, (void **)&hdr, sizeof(*hdr), 0, true);
    189 	if (error != 0) {
    190 		kobj_error(ko, "read failed %d", error);
    191 		goto out;
    192 	}
    193 	if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) {
    194 		kobj_error(ko, "not an ELF object");
    195 		error = ENOEXEC;
    196 		goto out;
    197 	}
    198 
    199 	if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
    200 	    hdr->e_version != EV_CURRENT) {
    201 		kobj_error(ko, "unsupported file version %d",
    202 		    hdr->e_ident[EI_VERSION]);
    203 		error = ENOEXEC;
    204 		goto out;
    205 	}
    206 	if (hdr->e_type != ET_REL) {
    207 		kobj_error(ko, "unsupported file type %d", hdr->e_type);
    208 		error = ENOEXEC;
    209 		goto out;
    210 	}
    211 	switch (hdr->e_machine) {
    212 #if ELFSIZE == 32
    213 	ELF32_MACHDEP_ID_CASES
    214 #elif ELFSIZE == 64
    215 	ELF64_MACHDEP_ID_CASES
    216 #else
    217 #error not defined
    218 #endif
    219 	default:
    220 		kobj_error(ko, "unsupported machine %d", hdr->e_machine);
    221 		error = ENOEXEC;
    222 		goto out;
    223 	}
    224 
    225 	ko->ko_nprogtab = 0;
    226 	ko->ko_shdr = 0;
    227 	ko->ko_nrel = 0;
    228 	ko->ko_nrela = 0;
    229 
    230 	/*
    231 	 * Allocate and read in the section header.
    232 	 */
    233 	if (hdr->e_shnum == 0 || hdr->e_shnum > ELF_MAXSHNUM ||
    234 	    hdr->e_shoff == 0 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
    235 		kobj_error(ko, "bad sizes");
    236 		error = ENOEXEC;
    237 		goto out;
    238 	}
    239 	ko->ko_shdrsz = hdr->e_shnum * sizeof(Elf_Shdr);
    240 	error = ko->ko_read(ko, (void **)&shdr, ko->ko_shdrsz, hdr->e_shoff,
    241 	    true);
    242 	if (error != 0) {
    243 		kobj_error(ko, "read failed %d", error);
    244 		goto out;
    245 	}
    246 	ko->ko_shdr = shdr;
    247 
    248 	/*
    249 	 * Scan the section header for information and table sizing.
    250 	 */
    251 	nsym = 0;
    252 	symtabindex = symstrindex = -1;
    253 	for (i = 0; i < hdr->e_shnum; i++) {
    254 		switch (shdr[i].sh_type) {
    255 		case SHT_PROGBITS:
    256 		case SHT_NOBITS:
    257 			ko->ko_nprogtab++;
    258 			break;
    259 		case SHT_SYMTAB:
    260 			nsym++;
    261 			symtabindex = i;
    262 			symstrindex = shdr[i].sh_link;
    263 			break;
    264 		case SHT_REL:
    265 			if (shdr[shdr[i].sh_info].sh_type != SHT_PROGBITS)
    266 				continue;
    267 			ko->ko_nrel++;
    268 			break;
    269 		case SHT_RELA:
    270 			if (shdr[shdr[i].sh_info].sh_type != SHT_PROGBITS)
    271 				continue;
    272 			ko->ko_nrela++;
    273 			break;
    274 		case SHT_STRTAB:
    275 			break;
    276 		}
    277 	}
    278 	if (ko->ko_nprogtab == 0) {
    279 		kobj_error(ko, "file has no contents");
    280 		error = ENOEXEC;
    281 		goto out;
    282 	}
    283 	if (nsym != 1) {
    284 		/* Only allow one symbol table for now */
    285 		kobj_error(ko, "file has no valid symbol table");
    286 		error = ENOEXEC;
    287 		goto out;
    288 	}
    289 	KASSERT(symtabindex != -1);
    290 	KASSERT(symstrindex != -1);
    291 
    292 	if (symstrindex == SHN_UNDEF || symstrindex >= hdr->e_shnum ||
    293 	    shdr[symstrindex].sh_type != SHT_STRTAB) {
    294 		kobj_error(ko, "file has invalid symbol strings");
    295 		error = ENOEXEC;
    296 		goto out;
    297 	}
    298 
    299 	/*
    300 	 * Allocate space for tracking the load chunks.
    301 	 */
    302 	if (ko->ko_nprogtab != 0) {
    303 		ko->ko_progtab = kmem_zalloc(ko->ko_nprogtab *
    304 		    sizeof(*ko->ko_progtab), KM_SLEEP);
    305 		if (ko->ko_progtab == NULL) {
    306 			error = ENOMEM;
    307 			kobj_error(ko, "out of memory");
    308 			goto out;
    309 		}
    310 	}
    311 	if (ko->ko_nrel != 0) {
    312 		ko->ko_reltab = kmem_zalloc(ko->ko_nrel *
    313 		    sizeof(*ko->ko_reltab), KM_SLEEP);
    314 		if (ko->ko_reltab == NULL) {
    315 			error = ENOMEM;
    316 			kobj_error(ko, "out of memory");
    317 			goto out;
    318 		}
    319 	}
    320 	if (ko->ko_nrela != 0) {
    321 		ko->ko_relatab = kmem_zalloc(ko->ko_nrela *
    322 		    sizeof(*ko->ko_relatab), KM_SLEEP);
    323 		if (ko->ko_relatab == NULL) {
    324 			error = ENOMEM;
    325 			kobj_error(ko, "out of memory");
    326 			goto out;
    327 		}
    328 	}
    329 
    330 	/*
    331 	 * Allocate space for and load the symbol table.
    332 	 */
    333 	ko->ko_symcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
    334 	if (ko->ko_symcnt == 0) {
    335 		kobj_error(ko, "no symbol table");
    336 		error = ENOEXEC;
    337 		goto out;
    338 	}
    339 	error = ko->ko_read(ko, (void **)&ko->ko_symtab,
    340 	    ko->ko_symcnt * sizeof(Elf_Sym),
    341 	    shdr[symtabindex].sh_offset, true);
    342 	if (error != 0) {
    343 		kobj_error(ko, "read failed %d", error);
    344 		goto out;
    345 	}
    346 
    347 	/*
    348 	 * Allocate space for and load the symbol strings.
    349 	 */
    350 	ko->ko_strtabsz = shdr[symstrindex].sh_size;
    351 	if (ko->ko_strtabsz == 0) {
    352 		kobj_error(ko, "no symbol strings");
    353 		error = ENOEXEC;
    354 		goto out;
    355 	}
    356 	error = ko->ko_read(ko, (void *)&ko->ko_strtab, ko->ko_strtabsz,
    357 	    shdr[symstrindex].sh_offset, true);
    358 	if (error != 0) {
    359 		kobj_error(ko, "read failed %d", error);
    360 		goto out;
    361 	}
    362 
    363 	/*
    364 	 * Adjust module symbol namespace, if necessary (e.g. with rump)
    365 	 */
    366 	error = kobj_renamespace(ko->ko_symtab, ko->ko_symcnt,
    367 	    &ko->ko_strtab, &ko->ko_strtabsz);
    368 	if (error != 0) {
    369 		kobj_error(ko, "renamespace failed %d", error);
    370 		goto out;
    371 	}
    372 
    373 	/*
    374 	 * Do we have a string table for the section names?
    375 	 */
    376 	if (hdr->e_shstrndx != SHN_UNDEF) {
    377 		if (hdr->e_shstrndx >= hdr->e_shnum) {
    378 			kobj_error(ko, "bad shstrndx");
    379 			error = ENOEXEC;
    380 			goto out;
    381 		}
    382 		if (shdr[hdr->e_shstrndx].sh_size != 0 &&
    383 		    shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
    384 			ko->ko_shstrtabsz = shdr[hdr->e_shstrndx].sh_size;
    385 			error = ko->ko_read(ko, (void **)&ko->ko_shstrtab,
    386 			    shdr[hdr->e_shstrndx].sh_size,
    387 			    shdr[hdr->e_shstrndx].sh_offset, true);
    388 			if (error != 0) {
    389 				kobj_error(ko, "read failed %d", error);
    390 				goto out;
    391 			}
    392 		}
    393 	}
    394 
    395 	/*
    396 	 * Size up code/data(progbits) and bss(nobits).
    397 	 */
    398 	alignmask = 0;
    399 	map_text_size = 0;
    400 	map_data_size = 0;
    401 	map_rodata_size = 0;
    402 	for (i = 0; i < hdr->e_shnum; i++) {
    403 		if (shdr[i].sh_type != SHT_PROGBITS &&
    404 		    shdr[i].sh_type != SHT_NOBITS)
    405 			continue;
    406 		alignmask = shdr[i].sh_addralign - 1;
    407 		if ((shdr[i].sh_flags & SHF_EXECINSTR)) {
    408 			map_text_size += alignmask;
    409 			map_text_size &= ~alignmask;
    410 			map_text_size += shdr[i].sh_size;
    411 		} else if (!(shdr[i].sh_flags & SHF_WRITE)) {
    412 			map_rodata_size += alignmask;
    413 			map_rodata_size &= ~alignmask;
    414 			map_rodata_size += shdr[i].sh_size;
    415 		} else {
    416 			map_data_size += alignmask;
    417 			map_data_size &= ~alignmask;
    418 			map_data_size += shdr[i].sh_size;
    419 		}
    420 	}
    421 
    422 	if (map_text_size == 0) {
    423 		kobj_error(ko, "no text");
    424 		error = ENOEXEC;
    425  		goto out;
    426  	}
    427 	if (map_data_size == 0) {
    428 		kobj_error(ko, "no data/bss");
    429 		error = ENOEXEC;
    430  		goto out;
    431  	}
    432 	if (map_rodata_size == 0) {
    433 		kobj_error(ko, "no rodata");
    434 		error = ENOEXEC;
    435  		goto out;
    436  	}
    437 
    438 	map_text_base = uvm_km_alloc(module_map, round_page(map_text_size),
    439 	    0, UVM_KMF_WIRED | UVM_KMF_EXEC);
    440 	if (map_text_base == 0) {
    441 		kobj_error(ko, "out of memory");
    442 		error = ENOMEM;
    443 		goto out;
    444 	}
    445 	ko->ko_text_address = map_text_base;
    446 	ko->ko_text_size = map_text_size;
    447 
    448 	map_data_base = uvm_km_alloc(module_map, round_page(map_data_size),
    449 	    0, UVM_KMF_WIRED);
    450 	if (map_data_base == 0) {
    451 		kobj_error(ko, "out of memory");
    452 		error = ENOMEM;
    453 		goto out;
    454 	}
    455 	ko->ko_data_address = map_data_base;
    456 	ko->ko_data_size = map_data_size;
    457 
    458 	map_rodata_base = uvm_km_alloc(module_map, round_page(map_rodata_size),
    459 	    0, UVM_KMF_WIRED);
    460 	if (map_rodata_base == 0) {
    461 		kobj_error(ko, "out of memory");
    462 		error = ENOMEM;
    463 		goto out;
    464 	}
    465 	ko->ko_rodata_address = map_rodata_base;
    466 	ko->ko_rodata_size = map_rodata_size;
    467 
    468 	/*
    469 	 * Now load code/data(progbits), zero bss(nobits), allocate space
    470 	 * for and load relocs
    471 	 */
    472 	pb = 0;
    473 	rl = 0;
    474 	ra = 0;
    475 	alignmask = 0;
    476 	for (i = 0; i < hdr->e_shnum; i++) {
    477 		switch (shdr[i].sh_type) {
    478 		case SHT_PROGBITS:
    479 		case SHT_NOBITS:
    480 			alignmask = shdr[i].sh_addralign - 1;
    481 			if ((shdr[i].sh_flags & SHF_EXECINSTR)) {
    482 				map_text_base += alignmask;
    483 				map_text_base &= ~alignmask;
    484 				addr = (void *)map_text_base;
    485 				map_text_base += shdr[i].sh_size;
    486 			} else if (!(shdr[i].sh_flags & SHF_WRITE)) {
    487 				map_rodata_base += alignmask;
    488 				map_rodata_base &= ~alignmask;
    489 				addr = (void *)map_rodata_base;
    490 				map_rodata_base += shdr[i].sh_size;
    491  			} else {
    492 				map_data_base += alignmask;
    493 				map_data_base &= ~alignmask;
    494 				addr = (void *)map_data_base;
    495 				map_data_base += shdr[i].sh_size;
    496  			}
    497 
    498 			ko->ko_progtab[pb].addr = addr;
    499 			if (shdr[i].sh_type == SHT_PROGBITS) {
    500 				ko->ko_progtab[pb].name = "<<PROGBITS>>";
    501 				error = ko->ko_read(ko, &addr,
    502 				    shdr[i].sh_size, shdr[i].sh_offset, false);
    503 				if (error != 0) {
    504 					kobj_error(ko, "read failed %d", error);
    505 					goto out;
    506 				}
    507 			} else { /* SHT_NOBITS */
    508 				ko->ko_progtab[pb].name = "<<NOBITS>>";
    509 				memset(addr, 0, shdr[i].sh_size);
    510 			}
    511 
    512 			ko->ko_progtab[pb].size = shdr[i].sh_size;
    513 			ko->ko_progtab[pb].sec = i;
    514 			if (ko->ko_shstrtab != NULL && shdr[i].sh_name != 0) {
    515 				ko->ko_progtab[pb].name =
    516 				    ko->ko_shstrtab + shdr[i].sh_name;
    517 			}
    518 
    519 			/* Update all symbol values with the offset. */
    520 			for (j = 0; j < ko->ko_symcnt; j++) {
    521 				es = &ko->ko_symtab[j];
    522 				if (es->st_shndx != i) {
    523 					continue;
    524 				}
    525 				es->st_value += (Elf_Addr)addr;
    526 			}
    527 			pb++;
    528 			break;
    529 		case SHT_REL:
    530 			if (shdr[shdr[i].sh_info].sh_type != SHT_PROGBITS)
    531 				break;
    532 			ko->ko_reltab[rl].size = shdr[i].sh_size;
    533 			ko->ko_reltab[rl].size -=
    534 			    shdr[i].sh_size % sizeof(Elf_Rel);
    535 			if (ko->ko_reltab[rl].size != 0) {
    536 				ko->ko_reltab[rl].nrel =
    537 				    shdr[i].sh_size / sizeof(Elf_Rel);
    538 				ko->ko_reltab[rl].sec = shdr[i].sh_info;
    539 				error = ko->ko_read(ko,
    540 				    (void **)&ko->ko_reltab[rl].rel,
    541 				    ko->ko_reltab[rl].size,
    542 				    shdr[i].sh_offset, true);
    543 				if (error != 0) {
    544 					kobj_error(ko, "read failed %d",
    545 					    error);
    546 					goto out;
    547 				}
    548 			}
    549 			rl++;
    550 			break;
    551 		case SHT_RELA:
    552 			if (shdr[shdr[i].sh_info].sh_type != SHT_PROGBITS)
    553 				break;
    554 			ko->ko_relatab[ra].size = shdr[i].sh_size;
    555 			ko->ko_relatab[ra].size -=
    556 			    shdr[i].sh_size % sizeof(Elf_Rela);
    557 			if (ko->ko_relatab[ra].size != 0) {
    558 				ko->ko_relatab[ra].nrela =
    559 				    shdr[i].sh_size / sizeof(Elf_Rela);
    560 				ko->ko_relatab[ra].sec = shdr[i].sh_info;
    561 				error = ko->ko_read(ko,
    562 				    (void **)&ko->ko_relatab[ra].rela,
    563 				    shdr[i].sh_size,
    564 				    shdr[i].sh_offset, true);
    565 				if (error != 0) {
    566 					kobj_error(ko, "read failed %d", error);
    567 					goto out;
    568 				}
    569 			}
    570 			ra++;
    571 			break;
    572 		default:
    573 			break;
    574 		}
    575 	}
    576 	if (pb != ko->ko_nprogtab) {
    577 		panic("%s:%d: %s: lost progbits", __func__, __LINE__,
    578 		   ko->ko_name);
    579 	}
    580 	if (rl != ko->ko_nrel) {
    581 		panic("%s:%d: %s: lost rel", __func__, __LINE__,
    582 		   ko->ko_name);
    583 	}
    584 	if (ra != ko->ko_nrela) {
    585 		panic("%s:%d: %s: lost rela", __func__, __LINE__,
    586 		   ko->ko_name);
    587 	}
    588 	if (map_text_base != ko->ko_text_address + map_text_size) {
    589 		panic("%s:%d: %s: map_text_base 0x%lx != address %lx "
    590 		    "+ map_text_size %ld (0x%lx)\n",
    591 		    __func__, __LINE__, ko->ko_name, (long)map_text_base,
    592 		    (long)ko->ko_text_address, (long)map_text_size,
    593 		    (long)ko->ko_text_address + map_text_size);
    594 	}
    595 	if (map_data_base != ko->ko_data_address + map_data_size) {
    596 		panic("%s:%d: %s: map_data_base 0x%lx != address %lx "
    597 		    "+ map_data_size %ld (0x%lx)\n",
    598 		    __func__, __LINE__, ko->ko_name, (long)map_data_base,
    599 		    (long)ko->ko_data_address, (long)map_data_size,
    600 		    (long)ko->ko_data_address + map_data_size);
    601 	}
    602 	if (map_rodata_base != ko->ko_rodata_address + map_rodata_size) {
    603 		panic("%s:%d: %s: map_rodata_base 0x%lx != address %lx "
    604 		    "+ map_rodata_size %ld (0x%lx)\n",
    605 		    __func__, __LINE__, ko->ko_name, (long)map_rodata_base,
    606 		    (long)ko->ko_rodata_address, (long)map_rodata_size,
    607 		    (long)ko->ko_rodata_address + map_rodata_size);
    608 	}
    609 
    610 	/*
    611 	 * Perform local relocations only.  Relocations relating to global
    612 	 * symbols will be done by kobj_affix().
    613 	 */
    614 	error = kobj_checksyms(ko, false);
    615 	if (error == 0) {
    616 		error = kobj_relocate(ko, true);
    617 	}
    618  out:
    619 	if (hdr != NULL) {
    620 		kobj_free(ko, hdr, sizeof(*hdr));
    621 	}
    622 	kobj_close(ko);
    623 	if (error != 0) {
    624 		kobj_unload(ko);
    625 	}
    626 
    627 	return error;
    628 }
    629 
    630 /*
    631  * kobj_unload:
    632  *
    633  *	Unload an object previously loaded by kobj_load().
    634  */
    635 void
    636 kobj_unload(kobj_t ko)
    637 {
    638 	int error;
    639 
    640 	kobj_close(ko);
    641 	kobj_jettison(ko);
    642 
    643 	/*
    644 	 * Notify MD code that a module has been unloaded.
    645 	 */
    646 	if (ko->ko_loaded) {
    647 		error = kobj_machdep(ko, (void *)ko->ko_text_address,
    648 		    ko->ko_text_size, false);
    649 		if (error != 0)
    650 			kobj_error(ko, "machine dependent deinit failed (text) %d",
    651 			    error);
    652 		error = kobj_machdep(ko, (void *)ko->ko_data_address,
    653 		    ko->ko_data_size, false);
    654  		if (error != 0)
    655 			kobj_error(ko, "machine dependent deinit failed (data) %d",
    656  			    error);
    657 		error = kobj_machdep(ko, (void *)ko->ko_rodata_address,
    658 		    ko->ko_rodata_size, false);
    659  		if (error != 0)
    660 			kobj_error(ko, "machine dependent deinit failed (rodata) %d",
    661  			    error);
    662 	}
    663 	if (ko->ko_text_address != 0) {
    664 		uvm_km_free(module_map, ko->ko_text_address,
    665 		    round_page(ko->ko_text_size), UVM_KMF_WIRED);
    666 	}
    667 	if (ko->ko_data_address != 0) {
    668 		uvm_km_free(module_map, ko->ko_data_address,
    669 		    round_page(ko->ko_data_size), UVM_KMF_WIRED);
    670  	}
    671 	if (ko->ko_rodata_address != 0) {
    672 		uvm_km_free(module_map, ko->ko_rodata_address,
    673 		    round_page(ko->ko_rodata_size), UVM_KMF_WIRED);
    674  	}
    675 	if (ko->ko_ksyms == true) {
    676 		ksyms_modunload(ko->ko_name);
    677 	}
    678 	if (ko->ko_symtab != NULL) {
    679 		kobj_free(ko, ko->ko_symtab, ko->ko_symcnt * sizeof(Elf_Sym));
    680 	}
    681 	if (ko->ko_strtab != NULL) {
    682 		kobj_free(ko, ko->ko_strtab, ko->ko_strtabsz);
    683 	}
    684 	if (ko->ko_progtab != NULL) {
    685 		kobj_free(ko, ko->ko_progtab, ko->ko_nprogtab *
    686 		    sizeof(*ko->ko_progtab));
    687 		ko->ko_progtab = NULL;
    688 	}
    689 	if (ko->ko_shstrtab) {
    690 		kobj_free(ko, ko->ko_shstrtab, ko->ko_shstrtabsz);
    691 		ko->ko_shstrtab = NULL;
    692 	}
    693 
    694 	kmem_free(ko, sizeof(*ko));
    695 }
    696 
    697 /*
    698  * kobj_stat:
    699  *
    700  *	Return size and load address of an object.
    701  */
    702 int
    703 kobj_stat(kobj_t ko, vaddr_t *address, size_t *size)
    704 {
    705 
    706 	if (address != NULL) {
    707 		*address = ko->ko_text_address;
    708 	}
    709 	if (size != NULL) {
    710 		*size = ko->ko_text_size;
    711 	}
    712 	return 0;
    713 }
    714 
    715 /*
    716  * kobj_affix:
    717  *
    718  *	Set an object's name and perform global relocs.  May only be
    719  *	called after the module and any requisite modules are loaded.
    720  */
    721 int
    722 kobj_affix(kobj_t ko, const char *name)
    723 {
    724 	int error;
    725 
    726 	KASSERT(ko->ko_ksyms == false);
    727 	KASSERT(ko->ko_loaded == false);
    728 
    729 	kobj_setname(ko, name);
    730 
    731 	/* Cache addresses of undefined symbols. */
    732 	error = kobj_checksyms(ko, true);
    733 
    734 	/* Now do global relocations. */
    735 	if (error == 0)
    736 		error = kobj_relocate(ko, false);
    737 
    738 	/*
    739 	 * Now that we know the name, register the symbol table.
    740 	 * Do after global relocations because ksyms will pack
    741 	 * the table.
    742 	 */
    743 	if (error == 0) {
    744 		ksyms_modload(ko->ko_name, ko->ko_symtab, ko->ko_symcnt *
    745 		    sizeof(Elf_Sym), ko->ko_strtab, ko->ko_strtabsz);
    746 		ko->ko_ksyms = true;
    747 	}
    748 
    749 	/* Jettison unneeded memory post-link. */
    750 	kobj_jettison(ko);
    751 
    752 	/* Change the memory protections, when needed. */
    753 	uvm_km_protect(module_map, ko->ko_text_address, ko->ko_text_size,
    754 	    VM_PROT_READ|VM_PROT_EXECUTE);
    755 	uvm_km_protect(module_map, ko->ko_rodata_address, ko->ko_rodata_size,
    756 	    VM_PROT_READ);
    757 
    758 	/*
    759 	 * Notify MD code that a module has been loaded.
    760 	 *
    761 	 * Most architectures use this opportunity to flush their caches.
    762 	 */
    763 	if (error == 0) {
    764 		error = kobj_machdep(ko, (void *)ko->ko_text_address,
    765 		    ko->ko_text_size, true);
    766 		if (error != 0)
    767 			kobj_error(ko, "machine dependent init failed (text) %d",
    768 			    error);
    769 		error = kobj_machdep(ko, (void *)ko->ko_data_address,
    770 		    ko->ko_data_size, true);
    771 		if (error != 0)
    772 			kobj_error(ko, "machine dependent init failed (data) %d",
    773 			    error);
    774 		error = kobj_machdep(ko, (void *)ko->ko_rodata_address,
    775 		    ko->ko_rodata_size, true);
    776 		if (error != 0)
    777 			kobj_error(ko, "machine dependent init failed (rodata) %d",
    778 			    error);
    779 		ko->ko_loaded = true;
    780 	}
    781 
    782 	/* If there was an error, destroy the whole object. */
    783 	if (error != 0) {
    784 		kobj_unload(ko);
    785 	}
    786 
    787 	return error;
    788 }
    789 
    790 /*
    791  * kobj_find_section:
    792  *
    793  *	Given a section name, search the loaded object and return
    794  *	virtual address if present and loaded.
    795  */
    796 int
    797 kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size)
    798 {
    799 	int i;
    800 
    801 	KASSERT(ko->ko_progtab != NULL);
    802 
    803 	for (i = 0; i < ko->ko_nprogtab; i++) {
    804 		if (strcmp(ko->ko_progtab[i].name, name) == 0) {
    805 			if (addr != NULL) {
    806 				*addr = ko->ko_progtab[i].addr;
    807 			}
    808 			if (size != NULL) {
    809 				*size = ko->ko_progtab[i].size;
    810 			}
    811 			return 0;
    812 		}
    813 	}
    814 
    815 	return ENOENT;
    816 }
    817 
    818 /*
    819  * kobj_jettison:
    820  *
    821  *	Release object data not needed after performing relocations.
    822  */
    823 static void
    824 kobj_jettison(kobj_t ko)
    825 {
    826 	int i;
    827 
    828 	if (ko->ko_reltab != NULL) {
    829 		for (i = 0; i < ko->ko_nrel; i++) {
    830 			if (ko->ko_reltab[i].rel) {
    831 				kobj_free(ko, ko->ko_reltab[i].rel,
    832 				    ko->ko_reltab[i].size);
    833 			}
    834 		}
    835 		kobj_free(ko, ko->ko_reltab, ko->ko_nrel *
    836 		    sizeof(*ko->ko_reltab));
    837 		ko->ko_reltab = NULL;
    838 		ko->ko_nrel = 0;
    839 	}
    840 	if (ko->ko_relatab != NULL) {
    841 		for (i = 0; i < ko->ko_nrela; i++) {
    842 			if (ko->ko_relatab[i].rela) {
    843 				kobj_free(ko, ko->ko_relatab[i].rela,
    844 				    ko->ko_relatab[i].size);
    845 			}
    846 		}
    847 		kobj_free(ko, ko->ko_relatab, ko->ko_nrela *
    848 		    sizeof(*ko->ko_relatab));
    849 		ko->ko_relatab = NULL;
    850 		ko->ko_nrela = 0;
    851 	}
    852 	if (ko->ko_shdr != NULL) {
    853 		kobj_free(ko, ko->ko_shdr, ko->ko_shdrsz);
    854 		ko->ko_shdr = NULL;
    855 	}
    856 }
    857 
    858 /*
    859  * kobj_sym_lookup:
    860  *
    861  *	Symbol lookup function to be used when the symbol index
    862  *	is known (ie during relocation).
    863  */
    864 uintptr_t
    865 kobj_sym_lookup(kobj_t ko, uintptr_t symidx)
    866 {
    867 	const Elf_Sym *sym;
    868 	const char *symbol;
    869 
    870 	/* Don't even try to lookup the symbol if the index is bogus. */
    871 	if (symidx >= ko->ko_symcnt)
    872 		return 0;
    873 
    874 	sym = ko->ko_symtab + symidx;
    875 
    876 	/* Quick answer if there is a definition included. */
    877 	if (sym->st_shndx != SHN_UNDEF) {
    878 		return (uintptr_t)sym->st_value;
    879 	}
    880 
    881 	/* If we get here, then it is undefined and needs a lookup. */
    882 	switch (ELF_ST_BIND(sym->st_info)) {
    883 	case STB_LOCAL:
    884 		/* Local, but undefined? huh? */
    885 		kobj_error(ko, "local symbol undefined");
    886 		return 0;
    887 
    888 	case STB_GLOBAL:
    889 		/* Relative to Data or Function name */
    890 		symbol = ko->ko_strtab + sym->st_name;
    891 
    892 		/* Force a lookup failure if the symbol name is bogus. */
    893 		if (*symbol == 0) {
    894 			kobj_error(ko, "bad symbol name");
    895 			return 0;
    896 		}
    897 
    898 		return (uintptr_t)sym->st_value;
    899 
    900 	case STB_WEAK:
    901 		kobj_error(ko, "weak symbols not supported");
    902 		return 0;
    903 
    904 	default:
    905 		return 0;
    906 	}
    907 }
    908 
    909 /*
    910  * kobj_findbase:
    911  *
    912  *	Return base address of the given section.
    913  */
    914 static uintptr_t
    915 kobj_findbase(kobj_t ko, int sec)
    916 {
    917 	int i;
    918 
    919 	for (i = 0; i < ko->ko_nprogtab; i++) {
    920 		if (sec == ko->ko_progtab[i].sec) {
    921 			return (uintptr_t)ko->ko_progtab[i].addr;
    922 		}
    923 	}
    924 	return 0;
    925 }
    926 
    927 /*
    928  * kobj_checksyms:
    929  *
    930  *	Scan symbol table for duplicates or resolve references to
    931  *	exernal symbols.
    932  */
    933 static int
    934 kobj_checksyms(kobj_t ko, bool undefined)
    935 {
    936 	unsigned long rval;
    937 	Elf_Sym *sym, *ms;
    938 	const char *name;
    939 	int error;
    940 
    941 	error = 0;
    942 
    943 	for (ms = (sym = ko->ko_symtab) + ko->ko_symcnt; sym < ms; sym++) {
    944 		/* Check validity of the symbol. */
    945 		if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL ||
    946 		    sym->st_name == 0)
    947 			continue;
    948 		if (undefined != (sym->st_shndx == SHN_UNDEF)) {
    949 			continue;
    950 		}
    951 
    952 		/*
    953 		 * Look it up.  Don't need to lock, as it is known that
    954 		 * the symbol tables aren't going to change (we hold
    955 		 * module_lock).
    956 		 */
    957 		name = ko->ko_strtab + sym->st_name;
    958 		if (ksyms_getval_unlocked(NULL, name, &rval,
    959 		    KSYMS_EXTERN) != 0) {
    960 			if (undefined) {
    961 				kobj_error(ko, "symbol `%s' not found",
    962 				    name);
    963 				error = ENOEXEC;
    964 			}
    965 			continue;
    966 		}
    967 
    968 		/* Save values of undefined globals. */
    969 		if (undefined) {
    970 			sym->st_value = (Elf_Addr)rval;
    971 			continue;
    972 		}
    973 
    974 		/* Check (and complain) about differing values. */
    975 		if (sym->st_value == rval) {
    976 			continue;
    977 		}
    978 		if (strcmp(name, "_bss_start") == 0 ||
    979 		    strcmp(name, "__bss_start") == 0 ||
    980 		    strcmp(name, "_bss_end__") == 0 ||
    981 		    strcmp(name, "__bss_end__") == 0 ||
    982 		    strcmp(name, "_edata") == 0 ||
    983 		    strcmp(name, "_end") == 0 ||
    984 		    strcmp(name, "__end") == 0 ||
    985 		    strcmp(name, "__end__") == 0 ||
    986 		    strncmp(name, "__start_link_set_", 17) == 0 ||
    987 		    strncmp(name, "__stop_link_set_", 16) == 0) {
    988 		    	continue;
    989 		}
    990 		kobj_error(ko, "global symbol `%s' redefined",
    991 		    name);
    992 		error = ENOEXEC;
    993 	}
    994 
    995 	return error;
    996 }
    997 
    998 /*
    999  * kobj_relocate:
   1000  *
   1001  *	Resolve relocations for the loaded object.
   1002  */
   1003 static int
   1004 kobj_relocate(kobj_t ko, bool local)
   1005 {
   1006 	const Elf_Rel *rellim;
   1007 	const Elf_Rel *rel;
   1008 	const Elf_Rela *relalim;
   1009 	const Elf_Rela *rela;
   1010 	const Elf_Sym *sym;
   1011 	uintptr_t base;
   1012 	int i, error;
   1013 	uintptr_t symidx;
   1014 
   1015 	/*
   1016 	 * Perform relocations without addend if there are any.
   1017 	 */
   1018 	for (i = 0; i < ko->ko_nrel; i++) {
   1019 		rel = ko->ko_reltab[i].rel;
   1020 		if (rel == NULL) {
   1021 			continue;
   1022 		}
   1023 		rellim = rel + ko->ko_reltab[i].nrel;
   1024 		base = kobj_findbase(ko, ko->ko_reltab[i].sec);
   1025 		if (base == 0) {
   1026 			panic("%s:%d: %s: lost base for e_reltab[%d] sec %d",
   1027 			   __func__, __LINE__, ko->ko_name, i,
   1028 			   ko->ko_reltab[i].sec);
   1029 		}
   1030 		for (; rel < rellim; rel++) {
   1031 			symidx = ELF_R_SYM(rel->r_info);
   1032 			if (symidx >= ko->ko_symcnt) {
   1033 				continue;
   1034 			}
   1035 			sym = ko->ko_symtab + symidx;
   1036 			if (local != (ELF_ST_BIND(sym->st_info) == STB_LOCAL)) {
   1037 				continue;
   1038 			}
   1039 			error = kobj_reloc(ko, base, rel, false, local);
   1040 			if (error != 0) {
   1041 				return ENOENT;
   1042 			}
   1043 		}
   1044 	}
   1045 
   1046 	/*
   1047 	 * Perform relocations with addend if there are any.
   1048 	 */
   1049 	for (i = 0; i < ko->ko_nrela; i++) {
   1050 		rela = ko->ko_relatab[i].rela;
   1051 		if (rela == NULL) {
   1052 			continue;
   1053 		}
   1054 		relalim = rela + ko->ko_relatab[i].nrela;
   1055 		base = kobj_findbase(ko, ko->ko_relatab[i].sec);
   1056 		if (base == 0) {
   1057 			panic("%s:%d: %s: lost base for e_relatab[%d] sec %d",
   1058 			   __func__, __LINE__, ko->ko_name, i,
   1059 			   ko->ko_relatab[i].sec);
   1060 		}
   1061 		for (; rela < relalim; rela++) {
   1062 			symidx = ELF_R_SYM(rela->r_info);
   1063 			if (symidx >= ko->ko_symcnt) {
   1064 				continue;
   1065 			}
   1066 			sym = ko->ko_symtab + symidx;
   1067 			if (local != (ELF_ST_BIND(sym->st_info) == STB_LOCAL)) {
   1068 				continue;
   1069 			}
   1070 			error = kobj_reloc(ko, base, rela, true, local);
   1071 			if (error != 0) {
   1072 				return ENOENT;
   1073 			}
   1074 		}
   1075 	}
   1076 
   1077 	return 0;
   1078 }
   1079 
   1080 /*
   1081  * kobj_out:
   1082  *
   1083  *	Utility function: log an error.
   1084  */
   1085 static void
   1086 kobj_out(const char *fname, int lnum, kobj_t ko, const char *fmt, ...)
   1087 {
   1088 	va_list ap;
   1089 
   1090 	printf("%s, %d: [%s]: linker error: ", fname, lnum, ko->ko_name);
   1091 	va_start(ap, fmt);
   1092 	vprintf(fmt, ap);
   1093 	va_end(ap);
   1094 	printf("\n");
   1095 }
   1096 
   1097 static int
   1098 kobj_read_mem(kobj_t ko, void **basep, size_t size, off_t off,
   1099     bool allocate)
   1100 {
   1101 	void *base = *basep;
   1102 	int error;
   1103 
   1104 	KASSERT(ko->ko_source != NULL);
   1105 
   1106 	if (ko->ko_memsize != -1 && off + size > ko->ko_memsize) {
   1107 		kobj_error(ko, "preloaded object short");
   1108 		error = EINVAL;
   1109 		base = NULL;
   1110 	} else if (allocate) {
   1111 		base = kmem_alloc(size, KM_SLEEP);
   1112 		error = 0;
   1113 	} else {
   1114 		error = 0;
   1115 	}
   1116 
   1117 	if (error == 0) {
   1118 		/* Copy the section */
   1119 		memcpy(base, (uint8_t *)ko->ko_source + off, size);
   1120 	}
   1121 
   1122 	if (allocate && error != 0) {
   1123 		kmem_free(base, size);
   1124 		base = NULL;
   1125 	}
   1126 
   1127 	if (allocate)
   1128 		*basep = base;
   1129 
   1130 	return error;
   1131 }
   1132 
   1133 /*
   1134  * kobj_free:
   1135  *
   1136  *	Utility function: free memory if it was allocated from the heap.
   1137  */
   1138 static void
   1139 kobj_free(kobj_t ko, void *base, size_t size)
   1140 {
   1141 
   1142 	kmem_free(base, size);
   1143 }
   1144 
   1145 extern char module_base[];
   1146 
   1147 void
   1148 kobj_setname(kobj_t ko, const char *name)
   1149 {
   1150 	const char *d = name, *dots = "";
   1151 	size_t len, dlen;
   1152 
   1153 	for (char *s = module_base; *d == *s; d++, s++)
   1154 		continue;
   1155 
   1156 	if (d == name)
   1157 		name = "";
   1158 	else
   1159 		name = "%M";
   1160 	dlen = strlen(d);
   1161 	len = dlen + strlen(name);
   1162 	if (len >= sizeof(ko->ko_name)) {
   1163 		len = (len - sizeof(ko->ko_name)) + 5; /* dots + NUL */
   1164 		if (dlen >= len) {
   1165 			d += len;
   1166 			dots = "/...";
   1167 		}
   1168 	}
   1169 	snprintf(ko->ko_name, sizeof(ko->ko_name), "%s%s%s", name, dots, d);
   1170 }
   1171 
   1172 #else	/* MODULAR */
   1173 
   1174 int
   1175 kobj_load_mem(kobj_t *kop, const char *name, void *base, ssize_t size)
   1176 {
   1177 
   1178 	return ENOSYS;
   1179 }
   1180 
   1181 void
   1182 kobj_unload(kobj_t ko)
   1183 {
   1184 
   1185 	panic("not modular");
   1186 }
   1187 
   1188 int
   1189 kobj_stat(kobj_t ko, vaddr_t *base, size_t *size)
   1190 {
   1191 
   1192 	return ENOSYS;
   1193 }
   1194 
   1195 int
   1196 kobj_affix(kobj_t ko, const char *name)
   1197 {
   1198 
   1199 	panic("not modular");
   1200 }
   1201 
   1202 int
   1203 kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size)
   1204 {
   1205 
   1206 	panic("not modular");
   1207 }
   1208 
   1209 void
   1210 kobj_setname(kobj_t ko, const char *name)
   1211 {
   1212 
   1213 	panic("not modular");
   1214 }
   1215 
   1216 #endif	/* MODULAR */
   1217