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