xref: /netbsd-src/sys/kern/subr_kobj.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: subr_kobj.c,v 1.11 2008/04/28 20:24:04 martin 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 1998-2000 Doug Rabson
31  * Copyright (c) 2004 Peter Wemm
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 /*
57  * Kernel loader for ELF objects.
58  *
59  * TODO: adjust kmem_alloc() calls to avoid needless fragmentation.
60  */
61 
62 #include "opt_modular.h"
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.11 2008/04/28 20:24:04 martin Exp $");
66 
67 #define	ELFSIZE		ARCH_ELFSIZE
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/kmem.h>
73 #include <sys/proc.h>
74 #include <sys/namei.h>
75 #include <sys/vnode.h>
76 #include <sys/fcntl.h>
77 #include <sys/kobj.h>
78 #include <sys/ksyms.h>
79 #include <sys/lkm.h>
80 #include <sys/exec.h>
81 #include <sys/exec_elf.h>
82 
83 #include <machine/stdarg.h>
84 
85 #include <uvm/uvm_extern.h>
86 
87 #ifdef MODULAR
88 
89 typedef struct {
90 	void		*addr;
91 	Elf_Off		size;
92 	int		flags;
93 	int		sec;		/* Original section */
94 	const char	*name;
95 } progent_t;
96 
97 typedef struct {
98 	Elf_Rel		*rel;
99 	int 		nrel;
100 	int 		sec;
101 	size_t		size;
102 } relent_t;
103 
104 typedef struct {
105 	Elf_Rela	*rela;
106 	int		nrela;
107 	int		sec;
108 	size_t		size;
109 } relaent_t;
110 
111 typedef enum kobjtype {
112 	KT_UNSET,
113 	KT_VNODE,
114 	KT_MEMORY
115 } kobjtype_t;
116 
117 struct kobj {
118 	char		ko_name[MAXLKMNAME];
119 	kobjtype_t	ko_type;
120 	void		*ko_source;
121 	ssize_t		ko_memsize;
122 	vaddr_t		ko_address;	/* Relocation address */
123 	Elf_Shdr	*ko_shdr;
124 	progent_t	*ko_progtab;
125 	relaent_t	*ko_relatab;
126 	relent_t	*ko_reltab;
127 	Elf_Sym		*ko_symtab;	/* Symbol table */
128 	char		*ko_strtab;	/* String table */
129 	char		*ko_shstrtab;	/* Section name string table */
130 	size_t		ko_size;	/* Size of text/data/bss */
131 	size_t		ko_symcnt;	/* Number of symbols */
132 	size_t		ko_strtabsz;	/* Number of bytes in string table */
133 	size_t		ko_shstrtabsz;	/* Number of bytes in scn str table */
134 	size_t		ko_shdrsz;
135 	int		ko_nrel;
136 	int		ko_nrela;
137 	int		ko_nprogtab;
138 	bool		ko_ksyms;
139 	bool		ko_loaded;
140 };
141 
142 static int	kobj_relocate(kobj_t);
143 static void	kobj_error(const char *, ...);
144 static int	kobj_read(kobj_t, void *, size_t, off_t);
145 static void	kobj_release_mem(kobj_t);
146 
147 extern struct vm_map *lkm_map;
148 static const char	*kobj_path = "/modules";	/* XXX ??? */
149 
150 /*
151  * kobj_open_file:
152  *
153  *	Open an object located in the file system.
154  */
155 int
156 kobj_open_file(kobj_t *kop, const char *filename)
157 {
158 	struct nameidata nd;
159 	kauth_cred_t cred;
160 	char *path;
161 	int error;
162 	kobj_t ko;
163 
164 	cred = kauth_cred_get();
165 
166 	ko = kmem_zalloc(sizeof(*ko), KM_SLEEP);
167 	if (ko == NULL) {
168 		return ENOMEM;
169 	}
170 
171 	/* XXX where to look? */
172 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
173 	error = vn_open(&nd, FREAD, 0);
174 	if (error != 0) {
175 		if (error != ENOENT) {
176 			goto out;
177 		}
178 		path = PNBUF_GET();
179 		snprintf(path, MAXPATHLEN - 1, "%s/%s", kobj_path,
180 		    filename);
181 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path);
182 		error = vn_open(&nd, FREAD, 0);
183 		if (error != 0) {
184 			strlcat(path, ".o", MAXPATHLEN);
185 			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path);
186 			error = vn_open(&nd, FREAD, 0);
187 		}
188 		PNBUF_PUT(path);
189 		if (error != 0) {
190 			goto out;
191 		}
192 	}
193 
194  out:
195  	if (error != 0) {
196 	 	kmem_free(ko, sizeof(*ko));
197 	} else {
198 		ko->ko_type = KT_VNODE;
199 		ko->ko_source = nd.ni_vp;
200 		*kop = ko;
201 	}
202 	return error;
203 }
204 
205 /*
206  * kobj_open_mem:
207  *
208  *	Open a pre-loaded object already resident in memory.  If size
209  *	is not -1, the complete size of the object is known.
210  */
211 int
212 kobj_open_mem(kobj_t *kop, void *base, ssize_t size)
213 {
214 	kobj_t ko;
215 
216 	ko = kmem_zalloc(sizeof(*ko), KM_SLEEP);
217 	if (ko == NULL) {
218 		return ENOMEM;
219 	}
220 
221 	ko->ko_type = KT_MEMORY;
222 	ko->ko_source = base;
223 	ko->ko_memsize = size;
224 	*kop = ko;
225 
226 	return 0;
227 }
228 
229 /*
230  * kobj_close:
231  *
232  *	Close an open ELF object.  If the object was not successfully
233  *	loaded, it will be destroyed.
234  */
235 void
236 kobj_close(kobj_t ko)
237 {
238 
239 	KASSERT(ko->ko_source != NULL);
240 
241 	switch (ko->ko_type) {
242 	case KT_VNODE:
243 		VOP_UNLOCK(ko->ko_source, 0);
244 		vn_close(ko->ko_source, FREAD, kauth_cred_get());
245 		break;
246 	case KT_MEMORY:
247 		/* nothing */
248 		break;
249 	default:
250 		panic("kobj_close: unknown type");
251 		break;
252 	}
253 
254 	ko->ko_source = NULL;
255 	ko->ko_type = KT_UNSET;
256 
257 	/* Program table and section strings are no longer needed. */
258 	if (ko->ko_progtab != NULL) {
259 		kmem_free(ko->ko_progtab, ko->ko_nprogtab *
260 		    sizeof(*ko->ko_progtab));
261 		ko->ko_progtab = NULL;
262 	}
263 	if (ko->ko_shstrtab) {
264 		kmem_free(ko->ko_shstrtab, ko->ko_shstrtabsz);
265 		ko->ko_shstrtab = NULL;
266 	}
267 
268 	/* If the object hasn't been loaded, then destroy it. */
269 	if (!ko->ko_loaded) {
270 		kobj_unload(ko);
271 	}
272 }
273 
274 /*
275  * kobj_load:
276  *
277  *	Load an ELF object from the file system and link into the
278  *	running	kernel image.
279  */
280 int
281 kobj_load(kobj_t ko)
282 {
283 	Elf_Ehdr *hdr;
284 	Elf_Shdr *shdr;
285 	Elf_Sym *es;
286 	vaddr_t mapbase;
287 	size_t mapsize;
288 	int error;
289 	int symtabindex;
290 	int symstrindex;
291 	int nsym;
292 	int pb, rl, ra;
293 	int alignmask;
294 	int i, j;
295 
296 	KASSERT(ko->ko_type != KT_UNSET);
297 	KASSERT(ko->ko_source != NULL);
298 
299 	shdr = NULL;
300 	mapsize = 0;
301 	error = 0;
302 	hdr = NULL;
303 
304 	/*
305 	 * Read the elf header from the file.
306 	 */
307 	hdr = kmem_alloc(sizeof(*hdr), KM_SLEEP);
308 	if (hdr == NULL) {
309 		error = ENOMEM;
310 		goto out;
311 	}
312 	error = kobj_read(ko, hdr, sizeof(*hdr), 0);
313 	if (error != 0)
314 		goto out;
315 	if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) {
316 		kobj_error("not an ELF object");
317 		error = ENOEXEC;
318 		goto out;
319 	}
320 
321 	if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
322 	    hdr->e_version != EV_CURRENT) {
323 		kobj_error("unsupported file version");
324 		error = ENOEXEC;
325 		goto out;
326 	}
327 	if (hdr->e_type != ET_REL) {
328 		kobj_error("unsupported file type");
329 		error = ENOEXEC;
330 		goto out;
331 	}
332 	switch (hdr->e_machine) {
333 #if ELFSIZE == 32
334 	ELF32_MACHDEP_ID_CASES
335 #else
336 	ELF64_MACHDEP_ID_CASES
337 #endif
338 	default:
339 		kobj_error("unsupported machine");
340 		error = ENOEXEC;
341 		goto out;
342 	}
343 
344 	ko->ko_nprogtab = 0;
345 	ko->ko_shdr = 0;
346 	ko->ko_nrel = 0;
347 	ko->ko_nrela = 0;
348 
349 	/*
350 	 * Allocate and read in the section header.
351 	 */
352 	ko->ko_shdrsz = hdr->e_shnum * hdr->e_shentsize;
353 	if (ko->ko_shdrsz == 0 || hdr->e_shoff == 0 ||
354 	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
355 		error = ENOEXEC;
356 		goto out;
357 	}
358 	shdr = kmem_alloc(ko->ko_shdrsz, KM_SLEEP);
359 	if (shdr == NULL) {
360 		error = ENOMEM;
361 		goto out;
362 	}
363 	ko->ko_shdr = shdr;
364 	error = kobj_read(ko, shdr, ko->ko_shdrsz, hdr->e_shoff);
365 	if (error != 0) {
366 		goto out;
367 	}
368 
369 	/*
370 	 * Scan the section header for information and table sizing.
371 	 */
372 	nsym = 0;
373 	symtabindex = -1;
374 	symstrindex = -1;
375 	for (i = 0; i < hdr->e_shnum; i++) {
376 		switch (shdr[i].sh_type) {
377 		case SHT_PROGBITS:
378 		case SHT_NOBITS:
379 			ko->ko_nprogtab++;
380 			break;
381 		case SHT_SYMTAB:
382 			nsym++;
383 			symtabindex = i;
384 			symstrindex = shdr[i].sh_link;
385 			break;
386 		case SHT_REL:
387 			ko->ko_nrel++;
388 			break;
389 		case SHT_RELA:
390 			ko->ko_nrela++;
391 			break;
392 		case SHT_STRTAB:
393 			break;
394 		}
395 	}
396 	if (ko->ko_nprogtab == 0) {
397 		kobj_error("file has no contents");
398 		error = ENOEXEC;
399 		goto out;
400 	}
401 	if (nsym != 1) {
402 		/* Only allow one symbol table for now */
403 		kobj_error("file has no valid symbol table");
404 		error = ENOEXEC;
405 		goto out;
406 	}
407 	if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
408 	    shdr[symstrindex].sh_type != SHT_STRTAB) {
409 		kobj_error("file has invalid symbol strings");
410 		error = ENOEXEC;
411 		goto out;
412 	}
413 
414 	/*
415 	 * Allocate space for tracking the load chunks.
416 	 */
417 	if (ko->ko_nprogtab != 0) {
418 		ko->ko_progtab = kmem_zalloc(ko->ko_nprogtab *
419 		    sizeof(*ko->ko_progtab), KM_SLEEP);
420 		if (ko->ko_progtab == NULL) {
421 			error = ENOMEM;
422 			goto out;
423 		}
424 	}
425 	if (ko->ko_nrel != 0) {
426 		ko->ko_reltab = kmem_zalloc(ko->ko_nrel *
427 		    sizeof(*ko->ko_reltab), KM_SLEEP);
428 		if (ko->ko_reltab == NULL) {
429 			error = ENOMEM;
430 			goto out;
431 		}
432 	}
433 	if (ko->ko_nrela != 0) {
434 		ko->ko_relatab = kmem_zalloc(ko->ko_nrela *
435 		    sizeof(*ko->ko_relatab), KM_SLEEP);
436 		if (ko->ko_relatab == NULL) {
437 			error = ENOMEM;
438 			goto out;
439 		}
440 	}
441 	if (symtabindex == -1) {
442 		kobj_error("lost symbol table index");
443 		goto out;
444 	}
445 
446 	/*
447 	 * Allocate space for and load the symbol table.
448 	 */
449 	ko->ko_symcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
450 	if (ko->ko_symcnt == 0) {
451 		kobj_error("no symbol table");
452 		goto out;
453 	}
454 	ko->ko_symtab = kmem_alloc(ko->ko_symcnt * sizeof(Elf_Sym), KM_SLEEP);
455 	if (ko->ko_symtab == NULL) {
456 		error = ENOMEM;
457 		goto out;
458 	}
459 	error = kobj_read(ko, ko->ko_symtab, shdr[symtabindex].sh_size,
460 	    shdr[symtabindex].sh_offset);
461 	if (error != 0) {
462 		goto out;
463 	}
464 
465 	/*
466 	 * Allocate space for and load the symbol strings.
467 	 */
468 	ko->ko_strtabsz = shdr[symstrindex].sh_size;
469 	if (ko->ko_strtabsz == 0) {
470 		kobj_error("no symbol strings");
471 		goto out;
472 	}
473 	ko->ko_strtab = kmem_alloc(ko->ko_strtabsz, KM_SLEEP);
474 	if (ko->ko_strtab == NULL) {
475 		error = ENOMEM;
476 		goto out;
477 	}
478 	error = kobj_read(ko, ko->ko_strtab, shdr[symstrindex].sh_size,
479 	    shdr[symstrindex].sh_offset);
480 	if (error != 0) {
481 		goto out;
482 	}
483 
484 	/*
485 	 * Do we have a string table for the section names?
486 	 */
487 	if (hdr->e_shstrndx != 0 && shdr[hdr->e_shstrndx].sh_size != 0 &&
488 	    shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
489 		ko->ko_shstrtabsz = shdr[hdr->e_shstrndx].sh_size;
490 		ko->ko_shstrtab = kmem_alloc(shdr[hdr->e_shstrndx].sh_size,
491 		    KM_SLEEP);
492 		if (ko->ko_shstrtab == NULL) {
493 			error = ENOMEM;
494 			goto out;
495 		}
496 		error = kobj_read(ko, ko->ko_shstrtab,
497 		    shdr[hdr->e_shstrndx].sh_size,
498 		    shdr[hdr->e_shstrndx].sh_offset);
499 		if (error != 0) {
500 			goto out;
501 		}
502 	}
503 
504 	/*
505 	 * Size up code/data(progbits) and bss(nobits).
506 	 */
507 	alignmask = 0;
508 	for (i = 0; i < hdr->e_shnum; i++) {
509 		switch (shdr[i].sh_type) {
510 		case SHT_PROGBITS:
511 		case SHT_NOBITS:
512 			alignmask = shdr[i].sh_addralign - 1;
513 			mapsize += alignmask;
514 			mapsize &= ~alignmask;
515 			mapsize += shdr[i].sh_size;
516 			break;
517 		}
518 	}
519 
520 	/*
521 	 * We know how much space we need for the text/data/bss/etc.
522 	 * This stuff needs to be in a single chunk so that profiling etc
523 	 * can get the bounds and gdb can associate offsets with modules.
524 	 */
525 	if (mapsize == 0) {
526 		kobj_error("no text/data/bss");
527 		goto out;
528 	}
529 	mapbase = uvm_km_alloc(lkm_map, round_page(mapsize), 0,
530 	    UVM_KMF_WIRED | UVM_KMF_EXEC);
531 	if (mapbase == 0) {
532 		error = ENOMEM;
533 		goto out;
534 	}
535 	ko->ko_address = mapbase;
536 	ko->ko_size = mapsize;
537 
538 	/*
539 	 * Now load code/data(progbits), zero bss(nobits), allocate space
540 	 * for and load relocs
541 	 */
542 	pb = 0;
543 	rl = 0;
544 	ra = 0;
545 	alignmask = 0;
546 	for (i = 0; i < hdr->e_shnum; i++) {
547 		switch (shdr[i].sh_type) {
548 		case SHT_PROGBITS:
549 		case SHT_NOBITS:
550 			alignmask = shdr[i].sh_addralign - 1;
551 			mapbase += alignmask;
552 			mapbase &= ~alignmask;
553 			ko->ko_progtab[pb].addr = (void *)mapbase;
554 			if (shdr[i].sh_type == SHT_PROGBITS) {
555 				ko->ko_progtab[pb].name = "<<PROGBITS>>";
556 				error = kobj_read(ko,
557 				    ko->ko_progtab[pb].addr, shdr[i].sh_size,
558 				    shdr[i].sh_offset);
559 				if (error != 0) {
560 					goto out;
561 				}
562 			} else {
563 				ko->ko_progtab[pb].name = "<<NOBITS>>";
564 				memset(ko->ko_progtab[pb].addr, 0,
565 				    shdr[i].sh_size);
566 			}
567 			ko->ko_progtab[pb].size = shdr[i].sh_size;
568 			ko->ko_progtab[pb].sec = i;
569 			if (ko->ko_shstrtab != NULL && shdr[i].sh_name != 0) {
570 				ko->ko_progtab[pb].name =
571 				    ko->ko_shstrtab + shdr[i].sh_name;
572 			}
573 
574 			/* Update all symbol values with the offset. */
575 			for (j = 0; j < ko->ko_symcnt; j++) {
576 				es = &ko->ko_symtab[j];
577 				if (es->st_shndx != i) {
578 					continue;
579 				}
580 				es->st_value +=
581 				    (Elf_Addr)ko->ko_progtab[pb].addr;
582 			}
583 			mapbase += shdr[i].sh_size;
584 			pb++;
585 			break;
586 		case SHT_REL:
587 			ko->ko_reltab[rl].size = shdr[i].sh_size;
588 			ko->ko_reltab[rl].size -=
589 			    shdr[i].sh_size % sizeof(Elf_Rel);
590 			if (ko->ko_reltab[rl].size != 0) {
591 				ko->ko_reltab[rl].rel =
592 				    kmem_alloc(ko->ko_reltab[rl].size,
593 				    KM_SLEEP);
594 				ko->ko_reltab[rl].nrel =
595 				    shdr[i].sh_size / sizeof(Elf_Rel);
596 				ko->ko_reltab[rl].sec = shdr[i].sh_info;
597 				error = kobj_read(ko,
598 				    ko->ko_reltab[rl].rel,
599 				    ko->ko_reltab[rl].size,
600 				    shdr[i].sh_offset);
601 				if (error != 0) {
602 					goto out;
603 				}
604 			}
605 			rl++;
606 			break;
607 		case SHT_RELA:
608 			ko->ko_relatab[ra].size = shdr[i].sh_size;
609 			ko->ko_relatab[ra].size -=
610 			    shdr[i].sh_size % sizeof(Elf_Rela);
611 			if (ko->ko_relatab[ra].size != 0) {
612 				ko->ko_relatab[ra].rela =
613 				    kmem_alloc(ko->ko_relatab[ra].size,
614 				    KM_SLEEP);
615 				ko->ko_relatab[ra].nrela =
616 				    shdr[i].sh_size / sizeof(Elf_Rela);
617 				ko->ko_relatab[ra].sec = shdr[i].sh_info;
618 				error = kobj_read(ko,
619 				    ko->ko_relatab[ra].rela,
620 				    shdr[i].sh_size,
621 				    shdr[i].sh_offset);
622 				if (error != 0) {
623 					goto out;
624 				}
625 			}
626 			ra++;
627 			break;
628 		}
629 	}
630 	if (pb != ko->ko_nprogtab) {
631 		panic("lost progbits");
632 	}
633 	if (rl != ko->ko_nrel) {
634 		panic("lost rel");
635 	}
636 	if (ra != ko->ko_nrela) {
637 		panic("lost rela");
638 	}
639 	if (mapbase != ko->ko_address + mapsize) {
640 		panic("mapbase 0x%lx != address %lx + mapsize 0x%lx (0x%lx)\n",
641 		    (long)mapbase, (long)ko->ko_address, (long)mapsize,
642 		    (long)ko->ko_address + mapsize);
643 	}
644 
645 	/*
646 	 * Perform relocations.  Done before registering with ksyms,
647 	 * which will pack our symbol table.
648 	 */
649 	error = kobj_relocate(ko);
650 	if (error != 0) {
651 		goto out;
652 	}
653 
654 	/*
655 	 * Notify MD code that a module has been loaded.
656 	 */
657 	error = kobj_machdep(ko, (void *)ko->ko_address, ko->ko_size, true);
658 	if (error != 0) {
659 		kobj_error("machine dependent init failed");
660 		goto out;
661 	}
662 	ko->ko_loaded = true;
663  out:
664 	kobj_release_mem(ko);
665 	if (hdr != NULL) {
666 		kmem_free(hdr, sizeof(*hdr));
667 	}
668 
669 	return error;
670 }
671 
672 /*
673  * kobj_unload:
674  *
675  *	Unload an object previously loaded by kobj_load().
676  */
677 void
678 kobj_unload(kobj_t ko)
679 {
680 	int error;
681 
682 	KASSERT(ko->ko_progtab == NULL);
683 	KASSERT(ko->ko_shstrtab == NULL);
684 
685 	if (ko->ko_address != 0) {
686 		uvm_km_free(lkm_map, ko->ko_address, round_page(ko->ko_size),
687 		    UVM_KMF_WIRED);
688 	}
689 	if (ko->ko_ksyms == true) {
690 		ksyms_delsymtab(ko->ko_name);
691 	}
692 	if (ko->ko_symtab != NULL) {
693 		kmem_free(ko->ko_symtab, ko->ko_symcnt * sizeof(Elf_Sym));
694 	}
695 	if (ko->ko_strtab != NULL) {
696 		kmem_free(ko->ko_strtab, ko->ko_strtabsz);
697 	}
698 
699 	/*
700 	 * Notify MD code that a module has been unloaded.
701 	 */
702 	if (ko->ko_loaded) {
703 		error = kobj_machdep(ko, (void *)ko->ko_address, ko->ko_size,
704 		    false);
705 		if (error != 0) {
706 			kobj_error("machine dependent deinit failed");
707 		}
708 	}
709 
710 	kmem_free(ko, sizeof(*ko));
711 }
712 
713 /*
714  * kobj_stat:
715  *
716  *	Return size and load address of an object.
717  */
718 void
719 kobj_stat(kobj_t ko, vaddr_t *address, size_t *size)
720 {
721 
722 	if (address != NULL) {
723 		*address = ko->ko_address;
724 	}
725 	if (size != NULL) {
726 		*size = ko->ko_size;
727 	}
728 }
729 
730 /*
731  * kobj_set_name:
732  *
733  *	Set an object's name.  Used only for symbol table lookups.
734  *	May only be called after the module is loaded.
735  */
736 int
737 kobj_set_name(kobj_t ko, const char *name)
738 {
739 	int error;
740 
741 	KASSERT(ko->ko_loaded);
742 
743 	strlcpy(ko->ko_name, name, sizeof(ko->ko_name));
744 
745 	/*
746 	 * Now that we know the name, register the symbol table.
747 	 */
748 	error = ksyms_addsymtab(ko->ko_name, ko->ko_symtab, ko->ko_symcnt *
749 	    sizeof(Elf_Sym), ko->ko_strtab, ko->ko_strtabsz);
750 	if (error != 0) {
751 		kobj_error("unable to register module symbol table");
752 	} else {
753 		ko->ko_ksyms = true;
754 	}
755 
756 	return error;
757 }
758 
759 /*
760  * kobj_find_section:
761  *
762  *	Given a section name, search the loaded object and return
763  *	virtual address if present and loaded.
764  */
765 int
766 kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size)
767 {
768 	int i;
769 
770 	KASSERT(ko->ko_progtab != NULL);
771 
772 	for (i = 0; i < ko->ko_nprogtab; i++) {
773 		if (strcmp(ko->ko_progtab[i].name, name) == 0) {
774 			if (addr != NULL) {
775 				*addr = ko->ko_progtab[i].addr;
776 			}
777 			if (size != NULL) {
778 				*size = ko->ko_progtab[i].size;
779 			}
780 			return 0;
781 		}
782 	}
783 
784 	return ENOENT;
785 }
786 
787 /*
788  * kobj_release_mem:
789  *
790  *	Release object data not needed after loading.
791  */
792 static void
793 kobj_release_mem(kobj_t ko)
794 {
795 	int i;
796 
797 	for (i = 0; i < ko->ko_nrel; i++) {
798 		if (ko->ko_reltab[i].rel) {
799 			kmem_free(ko->ko_reltab[i].rel,
800 			    ko->ko_reltab[i].size);
801 		}
802 	}
803 	for (i = 0; i < ko->ko_nrela; i++) {
804 		if (ko->ko_relatab[i].rela) {
805 			kmem_free(ko->ko_relatab[i].rela,
806 			    ko->ko_relatab[i].size);
807 		}
808 	}
809 	if (ko->ko_reltab != NULL) {
810 		kmem_free(ko->ko_reltab, ko->ko_nrel *
811 		    sizeof(*ko->ko_reltab));
812 		ko->ko_reltab = NULL;
813 		ko->ko_nrel = 0;
814 	}
815 	if (ko->ko_relatab != NULL) {
816 		kmem_free(ko->ko_relatab, ko->ko_nrela *
817 		    sizeof(*ko->ko_relatab));
818 		ko->ko_relatab = NULL;
819 		ko->ko_nrela = 0;
820 	}
821 	if (ko->ko_shdr != NULL) {
822 		kmem_free(ko->ko_shdr, ko->ko_shdrsz);
823 		ko->ko_shdr = NULL;
824 	}
825 }
826 
827 /*
828  * kobj_sym_lookup:
829  *
830  *	Symbol lookup function to be used when the symbol index
831  *	is known (ie during relocation).
832  */
833 uintptr_t
834 kobj_sym_lookup(kobj_t ko, uintptr_t symidx)
835 {
836 	const Elf_Sym *sym;
837 	const char *symbol;
838 	int error;
839 	u_long addr;
840 
841 	/* Don't even try to lookup the symbol if the index is bogus. */
842 	if (symidx >= ko->ko_symcnt)
843 		return 0;
844 
845 	sym = ko->ko_symtab + symidx;
846 
847 	/* Quick answer if there is a definition included. */
848 	if (sym->st_shndx != SHN_UNDEF) {
849 		return sym->st_value;
850 	}
851 
852 	/* If we get here, then it is undefined and needs a lookup. */
853 	switch (ELF_ST_BIND(sym->st_info)) {
854 	case STB_LOCAL:
855 		/* Local, but undefined? huh? */
856 		kobj_error("local symbol undefined");
857 		return 0;
858 
859 	case STB_GLOBAL:
860 		/* Relative to Data or Function name */
861 		symbol = ko->ko_strtab + sym->st_name;
862 
863 		/* Force a lookup failure if the symbol name is bogus. */
864 		if (*symbol == 0) {
865 			kobj_error("bad symbol name");
866 			return 0;
867 		}
868 
869 		error = ksyms_getval(NULL, symbol, &addr, KSYMS_ANY);
870 		if (error != 0) {
871 			kobj_error("symbol %s undefined", symbol);
872 			return (uintptr_t)0;
873 		}
874 		return (uintptr_t)addr;
875 
876 	case STB_WEAK:
877 		kobj_error("weak symbols not supported\n");
878 		return 0;
879 
880 	default:
881 		return 0;
882 	}
883 }
884 
885 /*
886  * kobj_findbase:
887  *
888  *	Return base address of the given section.
889  */
890 static uintptr_t
891 kobj_findbase(kobj_t ko, int sec)
892 {
893 	int i;
894 
895 	for (i = 0; i < ko->ko_nprogtab; i++) {
896 		if (sec == ko->ko_progtab[i].sec) {
897 			return (uintptr_t)ko->ko_progtab[i].addr;
898 		}
899 	}
900 	return 0;
901 }
902 
903 /*
904  * kobj_relocate:
905  *
906  *	Resolve all relocations for the loaded object.
907  */
908 static int
909 kobj_relocate(kobj_t ko)
910 {
911 	const Elf_Rel *rellim;
912 	const Elf_Rel *rel;
913 	const Elf_Rela *relalim;
914 	const Elf_Rela *rela;
915 	const Elf_Sym *sym;
916 	uintptr_t base;
917 	int i, error;
918 	uintptr_t symidx;
919 
920 	/*
921 	 * Perform relocations without addend if there are any.
922 	 */
923 	for (i = 0; i < ko->ko_nrel; i++) {
924 		rel = ko->ko_reltab[i].rel;
925 		if (rel == NULL) {
926 			continue;
927 		}
928 		rellim = rel + ko->ko_reltab[i].nrel;
929 		base = kobj_findbase(ko, ko->ko_reltab[i].sec);
930 		if (base == 0) {
931 			panic("lost base for e_reltab");
932 		}
933 		for (; rel < rellim; rel++) {
934 			symidx = ELF_R_SYM(rel->r_info);
935 			if (symidx >= ko->ko_symcnt) {
936 				continue;
937 			}
938 			sym = ko->ko_symtab + symidx;
939 			error = kobj_reloc(ko, base, rel, false,
940 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL);
941 			if (error != 0) {
942 				return ENOENT;
943 			}
944 		}
945 	}
946 
947 	/*
948 	 * Perform relocations with addend if there are any.
949 	 */
950 	for (i = 0; i < ko->ko_nrela; i++) {
951 		rela = ko->ko_relatab[i].rela;
952 		if (rela == NULL) {
953 			continue;
954 		}
955 		relalim = rela + ko->ko_relatab[i].nrela;
956 		base = kobj_findbase(ko, ko->ko_relatab[i].sec);
957 		if (base == 0) {
958 			panic("lost base for e_relatab");
959 		}
960 		for (; rela < relalim; rela++) {
961 			symidx = ELF_R_SYM(rela->r_info);
962 			if (symidx >= ko->ko_symcnt) {
963 				continue;
964 			}
965 			sym = ko->ko_symtab + symidx;
966 			error = kobj_reloc(ko, base, rela, true,
967 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL);
968 			if (error != 0) {
969 				return ENOENT;
970 			}
971 		}
972 	}
973 
974 	return 0;
975 }
976 
977 /*
978  * kobj_error:
979  *
980  *	Utility function: log an error.
981  */
982 static void
983 kobj_error(const char *fmt, ...)
984 {
985 	va_list ap;
986 
987 	va_start(ap, fmt);
988 	printf("WARNING: linker error: ");
989 	vprintf(fmt, ap);
990 	printf("\n");
991 	va_end(ap);
992 }
993 
994 /*
995  * kobj_read:
996  *
997  *	Utility function: read from the object.
998  */
999 static int
1000 kobj_read(kobj_t ko, void *base, size_t size, off_t off)
1001 {
1002 	size_t resid;
1003 	int error;
1004 
1005 	KASSERT(ko->ko_source != NULL);
1006 
1007 	switch (ko->ko_type) {
1008 	case KT_VNODE:
1009 		error = vn_rdwr(UIO_READ, ko->ko_source, base, size, off,
1010 		    UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid,
1011 		    curlwp);
1012 		if (error == 0 && resid != 0) {
1013 			error = EINVAL;
1014 		}
1015 		break;
1016 	case KT_MEMORY:
1017 		if (ko->ko_memsize != -1 && off + size > ko->ko_memsize) {
1018 			kobj_error("kobj_read: preloaded object short");
1019 			error = EINVAL;
1020 		} else {
1021 			memcpy(base, (uint8_t *)ko->ko_source + off, size);
1022 			error = 0;
1023 		}
1024 		break;
1025 	default:
1026 		panic("kobj_read: invalid type");
1027 	}
1028 
1029 	return error;
1030 }
1031 
1032 #else	/* MODULAR */
1033 
1034 int
1035 kobj_open_file(kobj_t *kop, const char *name)
1036 {
1037 
1038 	return ENOSYS;
1039 }
1040 
1041 int
1042 kobj_open_mem(kobj_t *kop, void *base, ssize_t size)
1043 {
1044 
1045 	return ENOSYS;
1046 }
1047 
1048 void
1049 kobj_close(kobj_t ko)
1050 {
1051 
1052 	panic("not modular");
1053 }
1054 
1055 int
1056 kobj_load(kobj_t ko)
1057 {
1058 
1059 	panic("not modular");
1060 }
1061 
1062 void
1063 kobj_unload(kobj_t ko)
1064 {
1065 
1066 	panic("not modular");
1067 }
1068 
1069 void
1070 kobj_stat(kobj_t ko, vaddr_t *base, size_t *size)
1071 {
1072 
1073 	panic("not modular");
1074 }
1075 
1076 int
1077 kobj_set_name(kobj_t ko, const char *name)
1078 {
1079 
1080 	panic("not modular");
1081 }
1082 
1083 int
1084 kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size)
1085 {
1086 
1087 	panic("not modular");
1088 }
1089 
1090 #endif	/* MODULAR */
1091