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