1 /*-
2 * Copyright (c) 1998-2000 Doug Rabson
3 * Copyright (c) 2004 Peter Wemm
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_ddb.h"
32 #include "opt_mac.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/mount.h>
41 #include <sys/proc.h>
42 #include <sys/namei.h>
43 #include <sys/fcntl.h>
44 #include <sys/vnode.h>
45 #include <sys/linker.h>
46
47 #include <machine/elf.h>
48
49 #include <security/mac/mac_framework.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_extern.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58
59 #include <sys/link_elf.h>
60
61 #ifdef DDB_CTF
62 #include <net/zlib.h>
63 #endif
64
65 #include "linker_if.h"
66
67 typedef struct {
68 void *addr;
69 Elf_Off size;
70 int flags;
71 int sec; /* Original section */
72 char *name;
73 } Elf_progent;
74
75 typedef struct {
76 Elf_Rel *rel;
77 int nrel;
78 int sec;
79 } Elf_relent;
80
81 typedef struct {
82 Elf_Rela *rela;
83 int nrela;
84 int sec;
85 } Elf_relaent;
86
87
88 typedef struct elf_file {
89 struct linker_file lf; /* Common fields */
90
91 int preloaded;
92 caddr_t address; /* Relocation address */
93 vm_object_t object; /* VM object to hold file pages */
94 Elf_Shdr *e_shdr;
95
96 Elf_progent *progtab;
97 int nprogtab;
98
99 Elf_relaent *relatab;
100 int nrelatab;
101
102 Elf_relent *reltab;
103 int nreltab;
104
105 Elf_Sym *ddbsymtab; /* The symbol table we are using */
106 long ddbsymcnt; /* Number of symbols */
107 caddr_t ddbstrtab; /* String table */
108 long ddbstrcnt; /* number of bytes in string table */
109
110 caddr_t shstrtab; /* Section name string table */
111 long shstrcnt; /* number of bytes in string table */
112
113 caddr_t ctftab; /* CTF table */
114 long ctfcnt; /* number of bytes in CTF table */
115 caddr_t ctfoff; /* CTF offset table */
116 caddr_t typoff; /* Type offset table */
117 long typlen; /* Number of type entries. */
118
119 } *elf_file_t;
120
121 #include <kern/kern_ctf.c>
122
123 static int link_elf_link_preload(linker_class_t cls,
124 const char *, linker_file_t *);
125 static int link_elf_link_preload_finish(linker_file_t);
126 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
127 static int link_elf_lookup_symbol(linker_file_t, const char *,
128 c_linker_sym_t *);
129 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
130 linker_symval_t *);
131 static int link_elf_search_symbol(linker_file_t, caddr_t value,
132 c_linker_sym_t *sym, long *diffp);
133
134 static void link_elf_unload_file(linker_file_t);
135 static int link_elf_lookup_set(linker_file_t, const char *,
136 void ***, void ***, int *);
137 static int link_elf_each_function_name(linker_file_t,
138 int (*)(const char *, void *), void *);
139 static int link_elf_each_function_nameval(linker_file_t,
140 linker_function_nameval_callback_t,
141 void *);
142 static void link_elf_reloc_local(linker_file_t);
143
144 static Elf_Addr elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps);
145
146 static kobj_method_t link_elf_methods[] = {
147 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
148 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
149 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
150 KOBJMETHOD(linker_unload, link_elf_unload_file),
151 KOBJMETHOD(linker_load_file, link_elf_load_file),
152 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
153 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
154 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
155 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
156 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
157 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
158 { 0, 0 }
159 };
160
161 static struct linker_class link_elf_class = {
162 #if ELF_TARG_CLASS == ELFCLASS32
163 "elf32_obj",
164 #else
165 "elf64_obj",
166 #endif
167 link_elf_methods, sizeof(struct elf_file)
168 };
169
170 static int relocate_file(elf_file_t ef);
171
172 static void
173 link_elf_error(const char *filename, const char *s)
174 {
175 if (filename == NULL)
176 printf("kldload: %s\n", s);
177 else
178 printf("kldload: %s: %s\n", filename, s);
179 }
180
181 static void
182 link_elf_init(void *arg)
183 {
184
185 linker_add_class(&link_elf_class);
186 }
187
188 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
189
190 static int
191 link_elf_link_preload(linker_class_t cls, const char *filename,
192 linker_file_t *result)
193 {
194 Elf_Ehdr *hdr;
195 Elf_Shdr *shdr;
196 Elf_Sym *es;
197 void *modptr, *baseptr, *sizeptr;
198 char *type;
199 elf_file_t ef;
200 linker_file_t lf;
201 Elf_Addr off;
202 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
203
204 /* Look to see if we have the file preloaded */
205 modptr = preload_search_by_name(filename);
206 if (modptr == NULL)
207 return ENOENT;
208
209 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
210 baseptr = preload_search_info(modptr, MODINFO_ADDR);
211 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
212 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
213 MODINFOMD_ELFHDR);
214 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
215 MODINFOMD_SHDR);
216 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
217 " obj module") != 0 &&
218 strcmp(type, "elf obj module") != 0)) {
219 return (EFTYPE);
220 }
221 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
222 shdr == NULL)
223 return (EINVAL);
224
225 lf = linker_make_file(filename, &link_elf_class);
226 if (lf == NULL)
227 return (ENOMEM);
228
229 ef = (elf_file_t)lf;
230 ef->preloaded = 1;
231 ef->address = *(caddr_t *)baseptr;
232 lf->address = *(caddr_t *)baseptr;
233 lf->size = *(size_t *)sizeptr;
234
235 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
236 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
237 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
238 hdr->e_version != EV_CURRENT ||
239 hdr->e_type != ET_REL ||
240 hdr->e_machine != ELF_TARG_MACH) {
241 error = EFTYPE;
242 goto out;
243 }
244 ef->e_shdr = shdr;
245
246 /* Scan the section header for information and table sizing. */
247 symtabindex = -1;
248 symstrindex = -1;
249 for (i = 0; i < hdr->e_shnum; i++) {
250 switch (shdr[i].sh_type) {
251 case SHT_PROGBITS:
252 case SHT_NOBITS:
253 ef->nprogtab++;
254 break;
255 case SHT_SYMTAB:
256 symtabindex = i;
257 symstrindex = shdr[i].sh_link;
258 break;
259 case SHT_REL:
260 ef->nreltab++;
261 break;
262 case SHT_RELA:
263 ef->nrelatab++;
264 break;
265 }
266 }
267
268 shstrindex = hdr->e_shstrndx;
269 if (ef->nprogtab == 0 || symstrindex < 0 ||
270 symstrindex >= hdr->e_shnum ||
271 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
272 shstrindex >= hdr->e_shnum ||
273 shdr[shstrindex].sh_type != SHT_STRTAB) {
274 printf("%s: bad/missing section headers\n", filename);
275 error = ENOEXEC;
276 goto out;
277 }
278
279 /* Allocate space for tracking the load chunks */
280 if (ef->nprogtab != 0)
281 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
282 M_LINKER, M_WAITOK | M_ZERO);
283 if (ef->nreltab != 0)
284 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
285 M_LINKER, M_WAITOK | M_ZERO);
286 if (ef->nrelatab != 0)
287 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
288 M_LINKER, M_WAITOK | M_ZERO);
289 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
290 (ef->nreltab != 0 && ef->reltab == NULL) ||
291 (ef->nrelatab != 0 && ef->relatab == NULL)) {
292 error = ENOMEM;
293 goto out;
294 }
295
296 /* XXX, relocate the sh_addr fields saved by the loader. */
297 off = 0;
298 for (i = 0; i < hdr->e_shnum; i++) {
299 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
300 off = shdr[i].sh_addr;
301 }
302 for (i = 0; i < hdr->e_shnum; i++) {
303 if (shdr[i].sh_addr != 0)
304 shdr[i].sh_addr = shdr[i].sh_addr - off +
305 (Elf_Addr)ef->address;
306 }
307
308 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
309 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
310 ef->ddbstrcnt = shdr[symstrindex].sh_size;
311 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
312 ef->shstrcnt = shdr[shstrindex].sh_size;
313 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
314
315 /* Now fill out progtab and the relocation tables. */
316 pb = 0;
317 rl = 0;
318 ra = 0;
319 for (i = 0; i < hdr->e_shnum; i++) {
320 switch (shdr[i].sh_type) {
321 case SHT_PROGBITS:
322 case SHT_NOBITS:
323 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
324 if (shdr[i].sh_type == SHT_PROGBITS)
325 ef->progtab[pb].name = "<<PROGBITS>>";
326 else
327 ef->progtab[pb].name = "<<NOBITS>>";
328 ef->progtab[pb].size = shdr[i].sh_size;
329 ef->progtab[pb].sec = i;
330 if (ef->shstrtab && shdr[i].sh_name != 0)
331 ef->progtab[pb].name =
332 ef->shstrtab + shdr[i].sh_name;
333
334 /* Update all symbol values with the offset. */
335 for (j = 0; j < ef->ddbsymcnt; j++) {
336 es = &ef->ddbsymtab[j];
337 if (es->st_shndx != i)
338 continue;
339 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
340 }
341 pb++;
342 break;
343 case SHT_REL:
344 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
345 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
346 ef->reltab[rl].sec = shdr[i].sh_info;
347 rl++;
348 break;
349 case SHT_RELA:
350 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
351 ef->relatab[ra].nrela =
352 shdr[i].sh_size / sizeof(Elf_Rela);
353 ef->relatab[ra].sec = shdr[i].sh_info;
354 ra++;
355 break;
356 }
357 }
358 if (pb != ef->nprogtab)
359 panic("lost progbits");
360 if (rl != ef->nreltab)
361 panic("lost reltab");
362 if (ra != ef->nrelatab)
363 panic("lost relatab");
364
365 /* Local intra-module relocations */
366 link_elf_reloc_local(lf);
367
368 *result = lf;
369 return (0);
370
371 out:
372 /* preload not done this way */
373 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
374 return (error);
375 }
376
377 static int
378 link_elf_link_preload_finish(linker_file_t lf)
379 {
380 elf_file_t ef;
381 int error;
382
383 ef = (elf_file_t)lf;
384 error = relocate_file(ef);
385 if (error)
386 return error;
387
388 /* Notify MD code that a module is being loaded. */
389 error = elf_cpu_load_file(lf);
390 if (error)
391 return (error);
392
393 return (0);
394 }
395
396 static int
397 link_elf_load_file(linker_class_t cls, const char *filename,
398 linker_file_t *result)
399 {
400 struct nameidata nd;
401 struct thread *td = curthread; /* XXX */
402 Elf_Ehdr *hdr;
403 Elf_Shdr *shdr;
404 Elf_Sym *es;
405 int nbytes, i, j;
406 vm_offset_t mapbase;
407 size_t mapsize;
408 int error = 0;
409 int resid, flags;
410 elf_file_t ef;
411 linker_file_t lf;
412 int symtabindex;
413 int symstrindex;
414 int shstrindex;
415 int nsym;
416 int pb, rl, ra;
417 int alignmask;
418 int vfslocked;
419
420 shdr = NULL;
421 lf = NULL;
422 mapsize = 0;
423 hdr = NULL;
424
425 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td);
426 flags = FREAD;
427 error = vn_open(&nd, &flags, 0, NULL);
428 if (error)
429 return error;
430 vfslocked = NDHASGIANT(&nd);
431 NDFREE(&nd, NDF_ONLY_PNBUF);
432 if (nd.ni_vp->v_type != VREG) {
433 error = ENOEXEC;
434 goto out;
435 }
436 #ifdef MAC
437 error = mac_check_kld_load(td->td_ucred, nd.ni_vp);
438 if (error) {
439 goto out;
440 }
441 #endif
442
443 /* Read the elf header from the file. */
444 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
445 if (hdr == NULL) {
446 error = ENOMEM;
447 goto out;
448 }
449 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
450 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
451 &resid, td);
452 if (error)
453 goto out;
454 if (resid != 0){
455 error = ENOEXEC;
456 goto out;
457 }
458
459 if (!IS_ELF(*hdr)) {
460 error = ENOEXEC;
461 goto out;
462 }
463
464 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
465 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
466 link_elf_error(filename, "Unsupported file layout");
467 error = ENOEXEC;
468 goto out;
469 }
470 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
471 || hdr->e_version != EV_CURRENT) {
472 link_elf_error(filename, "Unsupported file version");
473 error = ENOEXEC;
474 goto out;
475 }
476 if (hdr->e_type != ET_REL) {
477 link_elf_error(filename, "Unsupported file type");
478 error = ENOEXEC;
479 goto out;
480 }
481 if (hdr->e_machine != ELF_TARG_MACH) {
482 link_elf_error(filename, "Unsupported machine");
483 error = ENOEXEC;
484 goto out;
485 }
486
487 lf = linker_make_file(filename, &link_elf_class);
488 if (!lf) {
489 error = ENOMEM;
490 goto out;
491 }
492 ef = (elf_file_t) lf;
493 ef->nprogtab = 0;
494 ef->e_shdr = 0;
495 ef->nreltab = 0;
496 ef->nrelatab = 0;
497
498 /* Allocate and read in the section header */
499 nbytes = hdr->e_shnum * hdr->e_shentsize;
500 if (nbytes == 0 || hdr->e_shoff == 0 ||
501 hdr->e_shentsize != sizeof(Elf_Shdr)) {
502 error = ENOEXEC;
503 goto out;
504 }
505 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
506 if (shdr == NULL) {
507 error = ENOMEM;
508 goto out;
509 }
510 ef->e_shdr = shdr;
511 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
512 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
513 if (error)
514 goto out;
515 if (resid) {
516 error = ENOEXEC;
517 goto out;
518 }
519
520 /* Scan the section header for information and table sizing. */
521 nsym = 0;
522 symtabindex = -1;
523 symstrindex = -1;
524 for (i = 0; i < hdr->e_shnum; i++) {
525 switch (shdr[i].sh_type) {
526 case SHT_PROGBITS:
527 case SHT_NOBITS:
528 ef->nprogtab++;
529 break;
530 case SHT_SYMTAB:
531 nsym++;
532 symtabindex = i;
533 symstrindex = shdr[i].sh_link;
534 break;
535 case SHT_REL:
536 ef->nreltab++;
537 break;
538 case SHT_RELA:
539 ef->nrelatab++;
540 break;
541 case SHT_STRTAB:
542 break;
543 }
544 }
545 if (ef->nprogtab == 0) {
546 link_elf_error(filename, "file has no contents");
547 error = ENOEXEC;
548 goto out;
549 }
550 if (nsym != 1) {
551 /* Only allow one symbol table for now */
552 link_elf_error(filename, "file has no valid symbol table");
553 error = ENOEXEC;
554 goto out;
555 }
556 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
557 shdr[symstrindex].sh_type != SHT_STRTAB) {
558 link_elf_error(filename, "file has invalid symbol strings");
559 error = ENOEXEC;
560 goto out;
561 }
562
563 /* Allocate space for tracking the load chunks */
564 if (ef->nprogtab != 0)
565 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
566 M_LINKER, M_WAITOK | M_ZERO);
567 if (ef->nreltab != 0)
568 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
569 M_LINKER, M_WAITOK | M_ZERO);
570 if (ef->nrelatab != 0)
571 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
572 M_LINKER, M_WAITOK | M_ZERO);
573 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
574 (ef->nreltab != 0 && ef->reltab == NULL) ||
575 (ef->nrelatab != 0 && ef->relatab == NULL)) {
576 error = ENOMEM;
577 goto out;
578 }
579
580 if (symtabindex == -1)
581 panic("lost symbol table index");
582 /* Allocate space for and load the symbol table */
583 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
584 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
585 if (ef->ddbsymtab == NULL) {
586 error = ENOMEM;
587 goto out;
588 }
589 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
590 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
591 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
592 &resid, td);
593 if (error)
594 goto out;
595 if (resid != 0){
596 error = EINVAL;
597 goto out;
598 }
599
600 if (symstrindex == -1)
601 panic("lost symbol string index");
602 /* Allocate space for and load the symbol strings */
603 ef->ddbstrcnt = shdr[symstrindex].sh_size;
604 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
605 if (ef->ddbstrtab == NULL) {
606 error = ENOMEM;
607 goto out;
608 }
609 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
610 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
611 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
612 &resid, td);
613 if (error)
614 goto out;
615 if (resid != 0){
616 error = EINVAL;
617 goto out;
618 }
619
620 /* Do we have a string table for the section names? */
621 shstrindex = -1;
622 if (hdr->e_shstrndx != 0 &&
623 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
624 shstrindex = hdr->e_shstrndx;
625 ef->shstrcnt = shdr[shstrindex].sh_size;
626 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
627 M_WAITOK);
628 if (ef->shstrtab == NULL) {
629 error = ENOMEM;
630 goto out;
631 }
632 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
633 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
634 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
635 &resid, td);
636 if (error)
637 goto out;
638 if (resid != 0){
639 error = EINVAL;
640 goto out;
641 }
642 }
643
644 /* Size up code/data(progbits) and bss(nobits). */
645 alignmask = 0;
646 for (i = 0; i < hdr->e_shnum; i++) {
647 switch (shdr[i].sh_type) {
648 case SHT_PROGBITS:
649 case SHT_NOBITS:
650 alignmask = shdr[i].sh_addralign - 1;
651 mapsize += alignmask;
652 mapsize &= ~alignmask;
653 mapsize += shdr[i].sh_size;
654 break;
655 }
656 }
657
658 /*
659 * We know how much space we need for the text/data/bss/etc.
660 * This stuff needs to be in a single chunk so that profiling etc
661 * can get the bounds and gdb can associate offsets with modules
662 */
663 ef->object = vm_object_allocate(OBJT_DEFAULT,
664 round_page(mapsize) >> PAGE_SHIFT);
665 if (ef->object == NULL) {
666 error = ENOMEM;
667 goto out;
668 }
669 ef->address = (caddr_t) vm_map_min(kernel_map);
670
671 /*
672 * In order to satisfy amd64's architectural requirements on the
673 * location of code and data in the kernel's address space, request a
674 * mapping that is above the kernel.
675 */
676 mapbase = KERNBASE;
677 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
678 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE);
679 if (error) {
680 vm_object_deallocate(ef->object);
681 ef->object = 0;
682 goto out;
683 }
684
685 /* Wire the pages */
686 error = vm_map_wire(kernel_map, mapbase,
687 mapbase + round_page(mapsize),
688 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
689 if (error != KERN_SUCCESS) {
690 error = ENOMEM;
691 goto out;
692 }
693
694 /* Inform the kld system about the situation */
695 lf->address = ef->address = (caddr_t)mapbase;
696 lf->size = mapsize;
697
698 /*
699 * Now load code/data(progbits), zero bss(nobits), allocate space for
700 * and load relocs
701 */
702 pb = 0;
703 rl = 0;
704 ra = 0;
705 alignmask = 0;
706 for (i = 0; i < hdr->e_shnum; i++) {
707 switch (shdr[i].sh_type) {
708 case SHT_PROGBITS:
709 case SHT_NOBITS:
710 alignmask = shdr[i].sh_addralign - 1;
711 mapbase += alignmask;
712 mapbase &= ~alignmask;
713 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
714 if (shdr[i].sh_type == SHT_PROGBITS) {
715 ef->progtab[pb].name = "<<PROGBITS>>";
716 error = vn_rdwr(UIO_READ, nd.ni_vp,
717 ef->progtab[pb].addr,
718 shdr[i].sh_size, shdr[i].sh_offset,
719 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
720 NOCRED, &resid, td);
721 if (error)
722 goto out;
723 if (resid != 0){
724 error = EINVAL;
725 goto out;
726 }
727 } else {
728 ef->progtab[pb].name = "<<NOBITS>>";
729 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
730 }
731 ef->progtab[pb].size = shdr[i].sh_size;
732 ef->progtab[pb].sec = i;
733 if (ef->shstrtab && shdr[i].sh_name != 0)
734 ef->progtab[pb].name =
735 ef->shstrtab + shdr[i].sh_name;
736
737 /* Update all symbol values with the offset. */
738 for (j = 0; j < ef->ddbsymcnt; j++) {
739 es = &ef->ddbsymtab[j];
740 if (es->st_shndx != i)
741 continue;
742 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
743 }
744 mapbase += shdr[i].sh_size;
745 pb++;
746 break;
747 case SHT_REL:
748 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
749 M_WAITOK);
750 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
751 ef->reltab[rl].sec = shdr[i].sh_info;
752 error = vn_rdwr(UIO_READ, nd.ni_vp,
753 (void *)ef->reltab[rl].rel,
754 shdr[i].sh_size, shdr[i].sh_offset,
755 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
756 &resid, td);
757 if (error)
758 goto out;
759 if (resid != 0){
760 error = EINVAL;
761 goto out;
762 }
763 rl++;
764 break;
765 case SHT_RELA:
766 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
767 M_WAITOK);
768 ef->relatab[ra].nrela =
769 shdr[i].sh_size / sizeof(Elf_Rela);
770 ef->relatab[ra].sec = shdr[i].sh_info;
771 error = vn_rdwr(UIO_READ, nd.ni_vp,
772 (void *)ef->relatab[ra].rela,
773 shdr[i].sh_size, shdr[i].sh_offset,
774 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
775 &resid, td);
776 if (error)
777 goto out;
778 if (resid != 0){
779 error = EINVAL;
780 goto out;
781 }
782 ra++;
783 break;
784 }
785 }
786 if (pb != ef->nprogtab)
787 panic("lost progbits");
788 if (rl != ef->nreltab)
789 panic("lost reltab");
790 if (ra != ef->nrelatab)
791 panic("lost relatab");
792 if (mapbase != (vm_offset_t)ef->address + mapsize)
793 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
794 mapbase, ef->address, mapsize,
795 (vm_offset_t)ef->address + mapsize);
796
797 /* Local intra-module relocations */
798 link_elf_reloc_local(lf);
799
800 /* Pull in dependencies */
801 VOP_UNLOCK(nd.ni_vp, 0, td);
802 error = linker_load_dependencies(lf);
803 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY, td);
804 if (error)
805 goto out;
806
807 /* External relocations */
808 error = relocate_file(ef);
809 if (error)
810 goto out;
811
812 /* Notify MD code that a module is being loaded. */
813 error = elf_cpu_load_file(lf);
814 if (error)
815 goto out;
816
817 *result = lf;
818
819 out:
820 if (error && lf)
821 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
822 if (hdr)
823 free(hdr, M_LINKER);
824 VOP_UNLOCK(nd.ni_vp, 0, td);
825 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
826 VFS_UNLOCK_GIANT(vfslocked);
827
828 return error;
829 }
830
831 static void
832 link_elf_unload_file(linker_file_t file)
833 {
834 elf_file_t ef = (elf_file_t) file;
835 int i;
836
837 /* Notify MD code that a module is being unloaded. */
838 elf_cpu_unload_file(file);
839
840 if (ef->preloaded) {
841 if (ef->reltab)
842 free(ef->reltab, M_LINKER);
843 if (ef->relatab)
844 free(ef->relatab, M_LINKER);
845 if (ef->progtab)
846 free(ef->progtab, M_LINKER);
847 if (ef->ctftab)
848 free(ef->ctftab, M_LINKER);
849 if (ef->ctfoff)
850 free(ef->ctfoff, M_LINKER);
851 if (ef->typoff)
852 free(ef->typoff, M_LINKER);
853 if (file->filename != NULL)
854 preload_delete_name(file->filename);
855 /* XXX reclaim module memory? */
856 return;
857 }
858
859 for (i = 0; i < ef->nreltab; i++)
860 if (ef->reltab[i].rel)
861 free(ef->reltab[i].rel, M_LINKER);
862 for (i = 0; i < ef->nrelatab; i++)
863 if (ef->relatab[i].rela)
864 free(ef->relatab[i].rela, M_LINKER);
865 if (ef->reltab)
866 free(ef->reltab, M_LINKER);
867 if (ef->relatab)
868 free(ef->relatab, M_LINKER);
869 if (ef->progtab)
870 free(ef->progtab, M_LINKER);
871
872 if (ef->object) {
873 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
874 (vm_offset_t) ef->address +
875 (ef->object->size << PAGE_SHIFT));
876 }
877 if (ef->e_shdr)
878 free(ef->e_shdr, M_LINKER);
879 if (ef->ddbsymtab)
880 free(ef->ddbsymtab, M_LINKER);
881 if (ef->ddbstrtab)
882 free(ef->ddbstrtab, M_LINKER);
883 if (ef->shstrtab)
884 free(ef->shstrtab, M_LINKER);
885 if (ef->ctftab)
886 free(ef->ctftab, M_LINKER);
887 if (ef->ctfoff)
888 free(ef->ctfoff, M_LINKER);
889 if (ef->typoff)
890 free(ef->typoff, M_LINKER);
891 }
892
893 static const char *
894 symbol_name(elf_file_t ef, Elf_Size r_info)
895 {
896 const Elf_Sym *ref;
897
898 if (ELF_R_SYM(r_info)) {
899 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
900 return ef->ddbstrtab + ref->st_name;
901 } else
902 return NULL;
903 }
904
905 static Elf_Addr
906 findbase(elf_file_t ef, int sec)
907 {
908 int i;
909 Elf_Addr base = 0;
910
911 for (i = 0; i < ef->nprogtab; i++) {
912 if (sec == ef->progtab[i].sec) {
913 base = (Elf_Addr)ef->progtab[i].addr;
914 break;
915 }
916 }
917 return base;
918 }
919
920 static int
921 relocate_file(elf_file_t ef)
922 {
923 const Elf_Rel *rellim;
924 const Elf_Rel *rel;
925 const Elf_Rela *relalim;
926 const Elf_Rela *rela;
927 const char *symname;
928 const Elf_Sym *sym;
929 int i;
930 Elf_Size symidx;
931 Elf_Addr base;
932
933
934 /* Perform relocations without addend if there are any: */
935 for (i = 0; i < ef->nreltab; i++) {
936 rel = ef->reltab[i].rel;
937 if (rel == NULL)
938 panic("lost a reltab!");
939 rellim = rel + ef->reltab[i].nrel;
940 base = findbase(ef, ef->reltab[i].sec);
941 if (base == 0)
942 panic("lost base for reltab");
943 for ( ; rel < rellim; rel++) {
944 symidx = ELF_R_SYM(rel->r_info);
945 if (symidx >= ef->ddbsymcnt)
946 continue;
947 sym = ef->ddbsymtab + symidx;
948 /* Local relocs are already done */
949 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
950 continue;
951 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
952 elf_obj_lookup)) {
953 symname = symbol_name(ef, rel->r_info);
954 printf("link_elf_obj: symbol %s undefined\n",
955 symname);
956 return ENOENT;
957 }
958 }
959 }
960
961 /* Perform relocations with addend if there are any: */
962 for (i = 0; i < ef->nrelatab; i++) {
963 rela = ef->relatab[i].rela;
964 if (rela == NULL)
965 panic("lost a relatab!");
966 relalim = rela + ef->relatab[i].nrela;
967 base = findbase(ef, ef->relatab[i].sec);
968 if (base == 0)
969 panic("lost base for relatab");
970 for ( ; rela < relalim; rela++) {
971 symidx = ELF_R_SYM(rela->r_info);
972 if (symidx >= ef->ddbsymcnt)
973 continue;
974 sym = ef->ddbsymtab + symidx;
975 /* Local relocs are already done */
976 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
977 continue;
978 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
979 elf_obj_lookup)) {
980 symname = symbol_name(ef, rela->r_info);
981 printf("link_elf_obj: symbol %s undefined\n",
982 symname);
983 return ENOENT;
984 }
985 }
986 }
987
988 return 0;
989 }
990
991 static int
992 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
993 {
994 elf_file_t ef = (elf_file_t) lf;
995 const Elf_Sym *symp;
996 const char *strp;
997 int i;
998
999 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1000 strp = ef->ddbstrtab + symp->st_name;
1001 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1002 *sym = (c_linker_sym_t) symp;
1003 return 0;
1004 }
1005 }
1006 return ENOENT;
1007 }
1008
1009 static int
1010 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1011 linker_symval_t *symval)
1012 {
1013 elf_file_t ef = (elf_file_t) lf;
1014 const Elf_Sym *es = (const Elf_Sym*) sym;
1015
1016 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1017 symval->name = ef->ddbstrtab + es->st_name;
1018 symval->value = (caddr_t)es->st_value;
1019 symval->size = es->st_size;
1020 return 0;
1021 }
1022 return ENOENT;
1023 }
1024
1025 static int
1026 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1027 c_linker_sym_t *sym, long *diffp)
1028 {
1029 elf_file_t ef = (elf_file_t) lf;
1030 u_long off = (uintptr_t) (void *) value;
1031 u_long diff = off;
1032 u_long st_value;
1033 const Elf_Sym *es;
1034 const Elf_Sym *best = 0;
1035 int i;
1036
1037 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1038 if (es->st_name == 0)
1039 continue;
1040 st_value = es->st_value;
1041 if (off >= st_value) {
1042 if (off - st_value < diff) {
1043 diff = off - st_value;
1044 best = es;
1045 if (diff == 0)
1046 break;
1047 } else if (off - st_value == diff) {
1048 best = es;
1049 }
1050 }
1051 }
1052 if (best == 0)
1053 *diffp = off;
1054 else
1055 *diffp = diff;
1056 *sym = (c_linker_sym_t) best;
1057
1058 return 0;
1059 }
1060
1061 /*
1062 * Look up a linker set on an ELF system.
1063 */
1064 static int
1065 link_elf_lookup_set(linker_file_t lf, const char *name,
1066 void ***startp, void ***stopp, int *countp)
1067 {
1068 elf_file_t ef = (elf_file_t)lf;
1069 void **start, **stop;
1070 int i, count;
1071
1072 /* Relative to section number */
1073 for (i = 0; i < ef->nprogtab; i++) {
1074 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1075 strcmp(ef->progtab[i].name + 4, name) == 0) {
1076 start = (void **)ef->progtab[i].addr;
1077 stop = (void **)((char *)ef->progtab[i].addr +
1078 ef->progtab[i].size);
1079 count = stop - start;
1080 if (startp)
1081 *startp = start;
1082 if (stopp)
1083 *stopp = stop;
1084 if (countp)
1085 *countp = count;
1086 return (0);
1087 }
1088 }
1089 return (ESRCH);
1090 }
1091
1092 static int
1093 link_elf_each_function_name(linker_file_t file,
1094 int (*callback)(const char *, void *), void *opaque)
1095 {
1096 elf_file_t ef = (elf_file_t)file;
1097 const Elf_Sym *symp;
1098 int i, error;
1099
1100 /* Exhaustive search */
1101 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1102 if (symp->st_value != 0 &&
1103 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1104 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1105 if (error)
1106 return (error);
1107 }
1108 }
1109 return (0);
1110 }
1111
1112 static int
1113 link_elf_each_function_nameval(linker_file_t file,
1114 linker_function_nameval_callback_t callback, void *opaque)
1115 {
1116 linker_symval_t symval;
1117 elf_file_t ef = (elf_file_t)file;
1118 const Elf_Sym* symp;
1119 int i, error;
1120
1121 /* Exhaustive search */
1122 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1123 if (symp->st_value != 0 &&
1124 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1125 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1126 if (error)
1127 return (error);
1128 error = callback(file, i, &symval, opaque);
1129 if (error)
1130 return (error);
1131 }
1132 }
1133 return (0);
1134 }
1135
1136 /*
1137 * Symbol lookup function that can be used when the symbol index is known (ie
1138 * in relocations). It uses the symbol index instead of doing a fully fledged
1139 * hash table based lookup when such is valid. For example for local symbols.
1140 * This is not only more efficient, it's also more correct. It's not always
1141 * the case that the symbol can be found through the hash table.
1142 */
1143 static Elf_Addr
1144 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1145 {
1146 elf_file_t ef = (elf_file_t)lf;
1147 const Elf_Sym *sym;
1148 const char *symbol;
1149 Elf_Addr ret;
1150
1151 /* Don't even try to lookup the symbol if the index is bogus. */
1152 if (symidx >= ef->ddbsymcnt)
1153 return (0);
1154
1155 sym = ef->ddbsymtab + symidx;
1156
1157 /* Quick answer if there is a definition included. */
1158 if (sym->st_shndx != SHN_UNDEF)
1159 return (sym->st_value);
1160
1161 /* If we get here, then it is undefined and needs a lookup. */
1162 switch (ELF_ST_BIND(sym->st_info)) {
1163 case STB_LOCAL:
1164 /* Local, but undefined? huh? */
1165 return (0);
1166
1167 case STB_GLOBAL:
1168 /* Relative to Data or Function name */
1169 symbol = ef->ddbstrtab + sym->st_name;
1170
1171 /* Force a lookup failure if the symbol name is bogus. */
1172 if (*symbol == 0)
1173 return (0);
1174 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1175 return ret;
1176
1177 case STB_WEAK:
1178 printf("link_elf_obj: Weak symbols not supported\n");
1179 return (0);
1180
1181 default:
1182 return (0);
1183 }
1184 }
1185
1186 static void
1187 link_elf_fix_link_set(elf_file_t ef)
1188 {
1189 static const char startn[] = "__start_";
1190 static const char stopn[] = "__stop_";
1191 Elf_Sym *sym;
1192 const char *sym_name, *linkset_name;
1193 Elf_Addr startp, stopp;
1194 Elf_Size symidx;
1195 int start, i;
1196
1197 startp = stopp = 0;
1198 for (symidx = 1 /* zero entry is special */;
1199 symidx < ef->ddbsymcnt; symidx++) {
1200 sym = ef->ddbsymtab + symidx;
1201 if (sym->st_shndx != SHN_UNDEF)
1202 continue;
1203
1204 sym_name = ef->ddbstrtab + sym->st_name;
1205 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1206 start = 1;
1207 linkset_name = sym_name + sizeof(startn) - 1;
1208 }
1209 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1210 start = 0;
1211 linkset_name = sym_name + sizeof(stopn) - 1;
1212 }
1213 else
1214 continue;
1215
1216 for (i = 0; i < ef->nprogtab; i++) {
1217 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1218 startp = (Elf_Addr)ef->progtab[i].addr;
1219 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1220 break;
1221 }
1222 }
1223 if (i == ef->nprogtab)
1224 continue;
1225
1226 sym->st_value = start ? startp : stopp;
1227 sym->st_shndx = i;
1228 }
1229 }
1230
1231 static void
1232 link_elf_reloc_local(linker_file_t lf)
1233 {
1234 elf_file_t ef = (elf_file_t)lf;
1235 const Elf_Rel *rellim;
1236 const Elf_Rel *rel;
1237 const Elf_Rela *relalim;
1238 const Elf_Rela *rela;
1239 const Elf_Sym *sym;
1240 Elf_Addr base;
1241 int i;
1242 Elf_Size symidx;
1243
1244 link_elf_fix_link_set(ef);
1245
1246 /* Perform relocations without addend if there are any: */
1247 for (i = 0; i < ef->nreltab; i++) {
1248 rel = ef->reltab[i].rel;
1249 if (rel == NULL)
1250 panic("lost a reltab!");
1251 rellim = rel + ef->reltab[i].nrel;
1252 base = findbase(ef, ef->reltab[i].sec);
1253 if (base == 0)
1254 panic("lost base for reltab");
1255 for ( ; rel < rellim; rel++) {
1256 symidx = ELF_R_SYM(rel->r_info);
1257 if (symidx >= ef->ddbsymcnt)
1258 continue;
1259 sym = ef->ddbsymtab + symidx;
1260 /* Only do local relocs */
1261 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1262 continue;
1263 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1264 elf_obj_lookup);
1265 }
1266 }
1267
1268 /* Perform relocations with addend if there are any: */
1269 for (i = 0; i < ef->nrelatab; i++) {
1270 rela = ef->relatab[i].rela;
1271 if (rela == NULL)
1272 panic("lost a relatab!");
1273 relalim = rela + ef->relatab[i].nrela;
1274 base = findbase(ef, ef->relatab[i].sec);
1275 if (base == 0)
1276 panic("lost base for relatab");
1277 for ( ; rela < relalim; rela++) {
1278 symidx = ELF_R_SYM(rela->r_info);
1279 if (symidx >= ef->ddbsymcnt)
1280 continue;
1281 sym = ef->ddbsymtab + symidx;
1282 /* Only do local relocs */
1283 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1284 continue;
1285 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1286 elf_obj_lookup);
1287 }
1288 }
1289 }
Cache object: 56139b392e44e80c81093975ad56045e
|