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 if (shdr[i].sh_size == 0)
526 continue;
527 switch (shdr[i].sh_type) {
528 case SHT_PROGBITS:
529 case SHT_NOBITS:
530 ef->nprogtab++;
531 break;
532 case SHT_SYMTAB:
533 nsym++;
534 symtabindex = i;
535 symstrindex = shdr[i].sh_link;
536 break;
537 case SHT_REL:
538 ef->nreltab++;
539 break;
540 case SHT_RELA:
541 ef->nrelatab++;
542 break;
543 case SHT_STRTAB:
544 break;
545 }
546 }
547 if (ef->nprogtab == 0) {
548 link_elf_error(filename, "file has no contents");
549 error = ENOEXEC;
550 goto out;
551 }
552 if (nsym != 1) {
553 /* Only allow one symbol table for now */
554 link_elf_error(filename, "file has no valid symbol table");
555 error = ENOEXEC;
556 goto out;
557 }
558 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
559 shdr[symstrindex].sh_type != SHT_STRTAB) {
560 link_elf_error(filename, "file has invalid symbol strings");
561 error = ENOEXEC;
562 goto out;
563 }
564
565 /* Allocate space for tracking the load chunks */
566 if (ef->nprogtab != 0)
567 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
568 M_LINKER, M_WAITOK | M_ZERO);
569 if (ef->nreltab != 0)
570 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
571 M_LINKER, M_WAITOK | M_ZERO);
572 if (ef->nrelatab != 0)
573 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
574 M_LINKER, M_WAITOK | M_ZERO);
575 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
576 (ef->nreltab != 0 && ef->reltab == NULL) ||
577 (ef->nrelatab != 0 && ef->relatab == NULL)) {
578 error = ENOMEM;
579 goto out;
580 }
581
582 if (symtabindex == -1)
583 panic("lost symbol table index");
584 /* Allocate space for and load the symbol table */
585 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
586 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
587 if (ef->ddbsymtab == NULL) {
588 error = ENOMEM;
589 goto out;
590 }
591 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
592 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
593 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
594 &resid, td);
595 if (error)
596 goto out;
597 if (resid != 0){
598 error = EINVAL;
599 goto out;
600 }
601
602 if (symstrindex == -1)
603 panic("lost symbol string index");
604 /* Allocate space for and load the symbol strings */
605 ef->ddbstrcnt = shdr[symstrindex].sh_size;
606 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
607 if (ef->ddbstrtab == NULL) {
608 error = ENOMEM;
609 goto out;
610 }
611 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
612 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
613 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
614 &resid, td);
615 if (error)
616 goto out;
617 if (resid != 0){
618 error = EINVAL;
619 goto out;
620 }
621
622 /* Do we have a string table for the section names? */
623 shstrindex = -1;
624 if (hdr->e_shstrndx != 0 &&
625 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
626 shstrindex = hdr->e_shstrndx;
627 ef->shstrcnt = shdr[shstrindex].sh_size;
628 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
629 M_WAITOK);
630 if (ef->shstrtab == NULL) {
631 error = ENOMEM;
632 goto out;
633 }
634 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
635 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
636 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
637 &resid, td);
638 if (error)
639 goto out;
640 if (resid != 0){
641 error = EINVAL;
642 goto out;
643 }
644 }
645
646 /* Size up code/data(progbits) and bss(nobits). */
647 alignmask = 0;
648 for (i = 0; i < hdr->e_shnum; i++) {
649 if (shdr[i].sh_size == 0)
650 continue;
651 switch (shdr[i].sh_type) {
652 case SHT_PROGBITS:
653 case SHT_NOBITS:
654 alignmask = shdr[i].sh_addralign - 1;
655 mapsize += alignmask;
656 mapsize &= ~alignmask;
657 mapsize += shdr[i].sh_size;
658 break;
659 }
660 }
661
662 /*
663 * We know how much space we need for the text/data/bss/etc.
664 * This stuff needs to be in a single chunk so that profiling etc
665 * can get the bounds and gdb can associate offsets with modules
666 */
667 ef->object = vm_object_allocate(OBJT_DEFAULT,
668 round_page(mapsize) >> PAGE_SHIFT);
669 if (ef->object == NULL) {
670 error = ENOMEM;
671 goto out;
672 }
673 ef->address = (caddr_t) vm_map_min(kernel_map);
674
675 /*
676 * In order to satisfy amd64's architectural requirements on the
677 * location of code and data in the kernel's address space, request a
678 * mapping that is above the kernel.
679 */
680 mapbase = KERNBASE;
681 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
682 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE);
683 if (error) {
684 vm_object_deallocate(ef->object);
685 ef->object = 0;
686 goto out;
687 }
688
689 /* Wire the pages */
690 error = vm_map_wire(kernel_map, mapbase,
691 mapbase + round_page(mapsize),
692 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
693 if (error != KERN_SUCCESS) {
694 error = ENOMEM;
695 goto out;
696 }
697
698 /* Inform the kld system about the situation */
699 lf->address = ef->address = (caddr_t)mapbase;
700 lf->size = mapsize;
701
702 /*
703 * Now load code/data(progbits), zero bss(nobits), allocate space for
704 * and load relocs
705 */
706 pb = 0;
707 rl = 0;
708 ra = 0;
709 alignmask = 0;
710 for (i = 0; i < hdr->e_shnum; i++) {
711 if (shdr[i].sh_size == 0)
712 continue;
713 switch (shdr[i].sh_type) {
714 case SHT_PROGBITS:
715 case SHT_NOBITS:
716 alignmask = shdr[i].sh_addralign - 1;
717 mapbase += alignmask;
718 mapbase &= ~alignmask;
719 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase;
720 if (shdr[i].sh_type == SHT_PROGBITS) {
721 ef->progtab[pb].name = "<<PROGBITS>>";
722 error = vn_rdwr(UIO_READ, nd.ni_vp,
723 ef->progtab[pb].addr,
724 shdr[i].sh_size, shdr[i].sh_offset,
725 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
726 NOCRED, &resid, td);
727 if (error)
728 goto out;
729 if (resid != 0){
730 error = EINVAL;
731 goto out;
732 }
733 } else {
734 ef->progtab[pb].name = "<<NOBITS>>";
735 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
736 }
737 ef->progtab[pb].size = shdr[i].sh_size;
738 ef->progtab[pb].sec = i;
739 if (ef->shstrtab && shdr[i].sh_name != 0)
740 ef->progtab[pb].name =
741 ef->shstrtab + shdr[i].sh_name;
742
743 /* Update all symbol values with the offset. */
744 for (j = 0; j < ef->ddbsymcnt; j++) {
745 es = &ef->ddbsymtab[j];
746 if (es->st_shndx != i)
747 continue;
748 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
749 }
750 mapbase += shdr[i].sh_size;
751 pb++;
752 break;
753 case SHT_REL:
754 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
755 M_WAITOK);
756 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
757 ef->reltab[rl].sec = shdr[i].sh_info;
758 error = vn_rdwr(UIO_READ, nd.ni_vp,
759 (void *)ef->reltab[rl].rel,
760 shdr[i].sh_size, shdr[i].sh_offset,
761 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
762 &resid, td);
763 if (error)
764 goto out;
765 if (resid != 0){
766 error = EINVAL;
767 goto out;
768 }
769 rl++;
770 break;
771 case SHT_RELA:
772 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
773 M_WAITOK);
774 ef->relatab[ra].nrela =
775 shdr[i].sh_size / sizeof(Elf_Rela);
776 ef->relatab[ra].sec = shdr[i].sh_info;
777 error = vn_rdwr(UIO_READ, nd.ni_vp,
778 (void *)ef->relatab[ra].rela,
779 shdr[i].sh_size, shdr[i].sh_offset,
780 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
781 &resid, td);
782 if (error)
783 goto out;
784 if (resid != 0){
785 error = EINVAL;
786 goto out;
787 }
788 ra++;
789 break;
790 }
791 }
792 if (pb != ef->nprogtab)
793 panic("lost progbits");
794 if (rl != ef->nreltab)
795 panic("lost reltab");
796 if (ra != ef->nrelatab)
797 panic("lost relatab");
798 if (mapbase != (vm_offset_t)ef->address + mapsize)
799 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
800 mapbase, ef->address, mapsize,
801 (vm_offset_t)ef->address + mapsize);
802
803 /* Local intra-module relocations */
804 link_elf_reloc_local(lf);
805
806 /* Pull in dependencies */
807 VOP_UNLOCK(nd.ni_vp, 0, td);
808 error = linker_load_dependencies(lf);
809 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY, td);
810 if (error)
811 goto out;
812
813 /* External relocations */
814 error = relocate_file(ef);
815 if (error)
816 goto out;
817
818 /* Notify MD code that a module is being loaded. */
819 error = elf_cpu_load_file(lf);
820 if (error)
821 goto out;
822
823 *result = lf;
824
825 out:
826 if (error && lf)
827 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
828 if (hdr)
829 free(hdr, M_LINKER);
830 VOP_UNLOCK(nd.ni_vp, 0, td);
831 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
832 VFS_UNLOCK_GIANT(vfslocked);
833
834 return error;
835 }
836
837 static void
838 link_elf_unload_file(linker_file_t file)
839 {
840 elf_file_t ef = (elf_file_t) file;
841 int i;
842
843 /* Notify MD code that a module is being unloaded. */
844 elf_cpu_unload_file(file);
845
846 if (ef->preloaded) {
847 if (ef->reltab)
848 free(ef->reltab, M_LINKER);
849 if (ef->relatab)
850 free(ef->relatab, M_LINKER);
851 if (ef->progtab)
852 free(ef->progtab, M_LINKER);
853 if (ef->ctftab)
854 free(ef->ctftab, M_LINKER);
855 if (ef->ctfoff)
856 free(ef->ctfoff, M_LINKER);
857 if (ef->typoff)
858 free(ef->typoff, M_LINKER);
859 if (file->filename != NULL)
860 preload_delete_name(file->filename);
861 /* XXX reclaim module memory? */
862 return;
863 }
864
865 for (i = 0; i < ef->nreltab; i++)
866 if (ef->reltab[i].rel)
867 free(ef->reltab[i].rel, M_LINKER);
868 for (i = 0; i < ef->nrelatab; i++)
869 if (ef->relatab[i].rela)
870 free(ef->relatab[i].rela, M_LINKER);
871 if (ef->reltab)
872 free(ef->reltab, M_LINKER);
873 if (ef->relatab)
874 free(ef->relatab, M_LINKER);
875 if (ef->progtab)
876 free(ef->progtab, M_LINKER);
877
878 if (ef->object) {
879 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
880 (vm_offset_t) ef->address +
881 (ef->object->size << PAGE_SHIFT));
882 }
883 if (ef->e_shdr)
884 free(ef->e_shdr, M_LINKER);
885 if (ef->ddbsymtab)
886 free(ef->ddbsymtab, M_LINKER);
887 if (ef->ddbstrtab)
888 free(ef->ddbstrtab, M_LINKER);
889 if (ef->shstrtab)
890 free(ef->shstrtab, M_LINKER);
891 if (ef->ctftab)
892 free(ef->ctftab, M_LINKER);
893 if (ef->ctfoff)
894 free(ef->ctfoff, M_LINKER);
895 if (ef->typoff)
896 free(ef->typoff, M_LINKER);
897 }
898
899 static const char *
900 symbol_name(elf_file_t ef, Elf_Size r_info)
901 {
902 const Elf_Sym *ref;
903
904 if (ELF_R_SYM(r_info)) {
905 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
906 return ef->ddbstrtab + ref->st_name;
907 } else
908 return NULL;
909 }
910
911 static Elf_Addr
912 findbase(elf_file_t ef, int sec)
913 {
914 int i;
915 Elf_Addr base = 0;
916
917 for (i = 0; i < ef->nprogtab; i++) {
918 if (sec == ef->progtab[i].sec) {
919 base = (Elf_Addr)ef->progtab[i].addr;
920 break;
921 }
922 }
923 return base;
924 }
925
926 static int
927 relocate_file(elf_file_t ef)
928 {
929 const Elf_Rel *rellim;
930 const Elf_Rel *rel;
931 const Elf_Rela *relalim;
932 const Elf_Rela *rela;
933 const char *symname;
934 const Elf_Sym *sym;
935 int i;
936 Elf_Size symidx;
937 Elf_Addr base;
938
939
940 /* Perform relocations without addend if there are any: */
941 for (i = 0; i < ef->nreltab; i++) {
942 rel = ef->reltab[i].rel;
943 if (rel == NULL)
944 panic("lost a reltab!");
945 rellim = rel + ef->reltab[i].nrel;
946 base = findbase(ef, ef->reltab[i].sec);
947 if (base == 0)
948 panic("lost base for reltab");
949 for ( ; rel < rellim; rel++) {
950 symidx = ELF_R_SYM(rel->r_info);
951 if (symidx >= ef->ddbsymcnt)
952 continue;
953 sym = ef->ddbsymtab + symidx;
954 /* Local relocs are already done */
955 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
956 continue;
957 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
958 elf_obj_lookup)) {
959 symname = symbol_name(ef, rel->r_info);
960 printf("link_elf_obj: symbol %s undefined\n",
961 symname);
962 return ENOENT;
963 }
964 }
965 }
966
967 /* Perform relocations with addend if there are any: */
968 for (i = 0; i < ef->nrelatab; i++) {
969 rela = ef->relatab[i].rela;
970 if (rela == NULL)
971 panic("lost a relatab!");
972 relalim = rela + ef->relatab[i].nrela;
973 base = findbase(ef, ef->relatab[i].sec);
974 if (base == 0)
975 panic("lost base for relatab");
976 for ( ; rela < relalim; rela++) {
977 symidx = ELF_R_SYM(rela->r_info);
978 if (symidx >= ef->ddbsymcnt)
979 continue;
980 sym = ef->ddbsymtab + symidx;
981 /* Local relocs are already done */
982 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
983 continue;
984 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
985 elf_obj_lookup)) {
986 symname = symbol_name(ef, rela->r_info);
987 printf("link_elf_obj: symbol %s undefined\n",
988 symname);
989 return ENOENT;
990 }
991 }
992 }
993
994 return 0;
995 }
996
997 static int
998 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
999 {
1000 elf_file_t ef = (elf_file_t) lf;
1001 const Elf_Sym *symp;
1002 const char *strp;
1003 int i;
1004
1005 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1006 strp = ef->ddbstrtab + symp->st_name;
1007 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1008 *sym = (c_linker_sym_t) symp;
1009 return 0;
1010 }
1011 }
1012 return ENOENT;
1013 }
1014
1015 static int
1016 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1017 linker_symval_t *symval)
1018 {
1019 elf_file_t ef = (elf_file_t) lf;
1020 const Elf_Sym *es = (const Elf_Sym*) sym;
1021
1022 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1023 symval->name = ef->ddbstrtab + es->st_name;
1024 symval->value = (caddr_t)es->st_value;
1025 symval->size = es->st_size;
1026 return 0;
1027 }
1028 return ENOENT;
1029 }
1030
1031 static int
1032 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1033 c_linker_sym_t *sym, long *diffp)
1034 {
1035 elf_file_t ef = (elf_file_t) lf;
1036 u_long off = (uintptr_t) (void *) value;
1037 u_long diff = off;
1038 u_long st_value;
1039 const Elf_Sym *es;
1040 const Elf_Sym *best = 0;
1041 int i;
1042
1043 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1044 if (es->st_name == 0)
1045 continue;
1046 st_value = es->st_value;
1047 if (off >= st_value) {
1048 if (off - st_value < diff) {
1049 diff = off - st_value;
1050 best = es;
1051 if (diff == 0)
1052 break;
1053 } else if (off - st_value == diff) {
1054 best = es;
1055 }
1056 }
1057 }
1058 if (best == 0)
1059 *diffp = off;
1060 else
1061 *diffp = diff;
1062 *sym = (c_linker_sym_t) best;
1063
1064 return 0;
1065 }
1066
1067 /*
1068 * Look up a linker set on an ELF system.
1069 */
1070 static int
1071 link_elf_lookup_set(linker_file_t lf, const char *name,
1072 void ***startp, void ***stopp, int *countp)
1073 {
1074 elf_file_t ef = (elf_file_t)lf;
1075 void **start, **stop;
1076 int i, count;
1077
1078 /* Relative to section number */
1079 for (i = 0; i < ef->nprogtab; i++) {
1080 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1081 strcmp(ef->progtab[i].name + 4, name) == 0) {
1082 start = (void **)ef->progtab[i].addr;
1083 stop = (void **)((char *)ef->progtab[i].addr +
1084 ef->progtab[i].size);
1085 count = stop - start;
1086 if (startp)
1087 *startp = start;
1088 if (stopp)
1089 *stopp = stop;
1090 if (countp)
1091 *countp = count;
1092 return (0);
1093 }
1094 }
1095 return (ESRCH);
1096 }
1097
1098 static int
1099 link_elf_each_function_name(linker_file_t file,
1100 int (*callback)(const char *, void *), void *opaque)
1101 {
1102 elf_file_t ef = (elf_file_t)file;
1103 const Elf_Sym *symp;
1104 int i, error;
1105
1106 /* Exhaustive search */
1107 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1108 if (symp->st_value != 0 &&
1109 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1110 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1111 if (error)
1112 return (error);
1113 }
1114 }
1115 return (0);
1116 }
1117
1118 static int
1119 link_elf_each_function_nameval(linker_file_t file,
1120 linker_function_nameval_callback_t callback, void *opaque)
1121 {
1122 linker_symval_t symval;
1123 elf_file_t ef = (elf_file_t)file;
1124 const Elf_Sym* symp;
1125 int i, error;
1126
1127 /* Exhaustive search */
1128 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1129 if (symp->st_value != 0 &&
1130 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1131 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1132 if (error)
1133 return (error);
1134 error = callback(file, i, &symval, opaque);
1135 if (error)
1136 return (error);
1137 }
1138 }
1139 return (0);
1140 }
1141
1142 /*
1143 * Symbol lookup function that can be used when the symbol index is known (ie
1144 * in relocations). It uses the symbol index instead of doing a fully fledged
1145 * hash table based lookup when such is valid. For example for local symbols.
1146 * This is not only more efficient, it's also more correct. It's not always
1147 * the case that the symbol can be found through the hash table.
1148 */
1149 static Elf_Addr
1150 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1151 {
1152 elf_file_t ef = (elf_file_t)lf;
1153 const Elf_Sym *sym;
1154 const char *symbol;
1155 Elf_Addr ret;
1156
1157 /* Don't even try to lookup the symbol if the index is bogus. */
1158 if (symidx >= ef->ddbsymcnt)
1159 return (0);
1160
1161 sym = ef->ddbsymtab + symidx;
1162
1163 /* Quick answer if there is a definition included. */
1164 if (sym->st_shndx != SHN_UNDEF)
1165 return (sym->st_value);
1166
1167 /* If we get here, then it is undefined and needs a lookup. */
1168 switch (ELF_ST_BIND(sym->st_info)) {
1169 case STB_LOCAL:
1170 /* Local, but undefined? huh? */
1171 return (0);
1172
1173 case STB_GLOBAL:
1174 /* Relative to Data or Function name */
1175 symbol = ef->ddbstrtab + sym->st_name;
1176
1177 /* Force a lookup failure if the symbol name is bogus. */
1178 if (*symbol == 0)
1179 return (0);
1180 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1181 return ret;
1182
1183 case STB_WEAK:
1184 printf("link_elf_obj: Weak symbols not supported\n");
1185 return (0);
1186
1187 default:
1188 return (0);
1189 }
1190 }
1191
1192 static void
1193 link_elf_fix_link_set(elf_file_t ef)
1194 {
1195 static const char startn[] = "__start_";
1196 static const char stopn[] = "__stop_";
1197 Elf_Sym *sym;
1198 const char *sym_name, *linkset_name;
1199 Elf_Addr startp, stopp;
1200 Elf_Size symidx;
1201 int start, i;
1202
1203 startp = stopp = 0;
1204 for (symidx = 1 /* zero entry is special */;
1205 symidx < ef->ddbsymcnt; symidx++) {
1206 sym = ef->ddbsymtab + symidx;
1207 if (sym->st_shndx != SHN_UNDEF)
1208 continue;
1209
1210 sym_name = ef->ddbstrtab + sym->st_name;
1211 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1212 start = 1;
1213 linkset_name = sym_name + sizeof(startn) - 1;
1214 }
1215 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1216 start = 0;
1217 linkset_name = sym_name + sizeof(stopn) - 1;
1218 }
1219 else
1220 continue;
1221
1222 for (i = 0; i < ef->nprogtab; i++) {
1223 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1224 startp = (Elf_Addr)ef->progtab[i].addr;
1225 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1226 break;
1227 }
1228 }
1229 if (i == ef->nprogtab)
1230 continue;
1231
1232 sym->st_value = start ? startp : stopp;
1233 sym->st_shndx = i;
1234 }
1235 }
1236
1237 static void
1238 link_elf_reloc_local(linker_file_t lf)
1239 {
1240 elf_file_t ef = (elf_file_t)lf;
1241 const Elf_Rel *rellim;
1242 const Elf_Rel *rel;
1243 const Elf_Rela *relalim;
1244 const Elf_Rela *rela;
1245 const Elf_Sym *sym;
1246 Elf_Addr base;
1247 int i;
1248 Elf_Size symidx;
1249
1250 link_elf_fix_link_set(ef);
1251
1252 /* Perform relocations without addend if there are any: */
1253 for (i = 0; i < ef->nreltab; i++) {
1254 rel = ef->reltab[i].rel;
1255 if (rel == NULL)
1256 panic("lost a reltab!");
1257 rellim = rel + ef->reltab[i].nrel;
1258 base = findbase(ef, ef->reltab[i].sec);
1259 if (base == 0)
1260 panic("lost base for reltab");
1261 for ( ; rel < rellim; rel++) {
1262 symidx = ELF_R_SYM(rel->r_info);
1263 if (symidx >= ef->ddbsymcnt)
1264 continue;
1265 sym = ef->ddbsymtab + symidx;
1266 /* Only do local relocs */
1267 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1268 continue;
1269 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1270 elf_obj_lookup);
1271 }
1272 }
1273
1274 /* Perform relocations with addend if there are any: */
1275 for (i = 0; i < ef->nrelatab; i++) {
1276 rela = ef->relatab[i].rela;
1277 if (rela == NULL)
1278 panic("lost a relatab!");
1279 relalim = rela + ef->relatab[i].nrela;
1280 base = findbase(ef, ef->relatab[i].sec);
1281 if (base == 0)
1282 panic("lost base for relatab");
1283 for ( ; rela < relalim; rela++) {
1284 symidx = ELF_R_SYM(rela->r_info);
1285 if (symidx >= ef->ddbsymcnt)
1286 continue;
1287 sym = ef->ddbsymtab + symidx;
1288 /* Only do local relocs */
1289 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1290 continue;
1291 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1292 elf_obj_lookup);
1293 }
1294 }
1295 }
Cache object: fca9ebbbc06191bd5e250c36a8255903
|