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