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