FreeBSD/Linux Kernel Cross Reference
sys/kern/link_elf.c
1 /*-
2 * Copyright (c) 1998-2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ddb.h"
31 #include "opt_gdb.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #ifdef GPROF
36 #include <sys/gmon.h>
37 #endif
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/mount.h>
43 #include <sys/pcpu.h>
44 #include <sys/proc.h>
45 #include <sys/namei.h>
46 #include <sys/fcntl.h>
47 #include <sys/vnode.h>
48 #include <sys/linker.h>
49
50 #include <machine/elf.h>
51
52 #include <net/vnet.h>
53
54 #include <security/mac/mac_framework.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58 #ifdef SPARSE_MAPPING
59 #include <vm/vm_object.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_extern.h>
62 #endif
63 #include <vm/pmap.h>
64 #include <vm/vm_map.h>
65
66 #include <sys/link_elf.h>
67
68 #ifdef DDB_CTF
69 #include <sys/zlib.h>
70 #endif
71
72 #include "linker_if.h"
73
74 #define MAXSEGS 4
75
76 typedef struct elf_file {
77 struct linker_file lf; /* Common fields */
78 int preloaded; /* Was file pre-loaded */
79 caddr_t address; /* Relocation address */
80 #ifdef SPARSE_MAPPING
81 vm_object_t object; /* VM object to hold file pages */
82 #endif
83 Elf_Dyn *dynamic; /* Symbol table etc. */
84 Elf_Hashelt nbuckets; /* DT_HASH info */
85 Elf_Hashelt nchains;
86 const Elf_Hashelt *buckets;
87 const Elf_Hashelt *chains;
88 caddr_t hash;
89 caddr_t strtab; /* DT_STRTAB */
90 int strsz; /* DT_STRSZ */
91 const Elf_Sym *symtab; /* DT_SYMTAB */
92 Elf_Addr *got; /* DT_PLTGOT */
93 const Elf_Rel *pltrel; /* DT_JMPREL */
94 int pltrelsize; /* DT_PLTRELSZ */
95 const Elf_Rela *pltrela; /* DT_JMPREL */
96 int pltrelasize; /* DT_PLTRELSZ */
97 const Elf_Rel *rel; /* DT_REL */
98 int relsize; /* DT_RELSZ */
99 const Elf_Rela *rela; /* DT_RELA */
100 int relasize; /* DT_RELASZ */
101 caddr_t modptr;
102 const Elf_Sym *ddbsymtab; /* The symbol table we are using */
103 long ddbsymcnt; /* Number of symbols */
104 caddr_t ddbstrtab; /* String table */
105 long ddbstrcnt; /* number of bytes in string table */
106 caddr_t symbase; /* malloc'ed symbold base */
107 caddr_t strbase; /* malloc'ed string base */
108 caddr_t ctftab; /* CTF table */
109 long ctfcnt; /* number of bytes in CTF table */
110 caddr_t ctfoff; /* CTF offset table */
111 caddr_t typoff; /* Type offset table */
112 long typlen; /* Number of type entries. */
113 Elf_Addr pcpu_start; /* Pre-relocation pcpu set start. */
114 Elf_Addr pcpu_stop; /* Pre-relocation pcpu set stop. */
115 Elf_Addr pcpu_base; /* Relocated pcpu set address. */
116 #ifdef VIMAGE
117 Elf_Addr vnet_start; /* Pre-relocation vnet set start. */
118 Elf_Addr vnet_stop; /* Pre-relocation vnet set stop. */
119 Elf_Addr vnet_base; /* Relocated vnet set address. */
120 #endif
121 #ifdef GDB
122 struct link_map gdb; /* hooks for gdb */
123 #endif
124 } *elf_file_t;
125
126 struct elf_set {
127 Elf_Addr es_start;
128 Elf_Addr es_stop;
129 Elf_Addr es_base;
130 TAILQ_ENTRY(elf_set) es_link;
131 };
132
133 TAILQ_HEAD(elf_set_head, elf_set);
134
135 #include <kern/kern_ctf.c>
136
137 static int link_elf_link_common_finish(linker_file_t);
138 static int link_elf_link_preload(linker_class_t cls,
139 const char *, linker_file_t *);
140 static int link_elf_link_preload_finish(linker_file_t);
141 static int link_elf_load_file(linker_class_t, const char *,
142 linker_file_t *);
143 static int link_elf_lookup_symbol(linker_file_t, const char *,
144 c_linker_sym_t *);
145 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
146 linker_symval_t *);
147 static int link_elf_search_symbol(linker_file_t, caddr_t,
148 c_linker_sym_t *, long *);
149
150 static void link_elf_unload_file(linker_file_t);
151 static void link_elf_unload_preload(linker_file_t);
152 static int link_elf_lookup_set(linker_file_t, const char *,
153 void ***, void ***, int *);
154 static int link_elf_each_function_name(linker_file_t,
155 int (*)(const char *, void *), void *);
156 static int link_elf_each_function_nameval(linker_file_t,
157 linker_function_nameval_callback_t, void *);
158 static void link_elf_reloc_local(linker_file_t);
159 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
160 static long link_elf_strtab_get(linker_file_t, caddr_t *);
161 static int elf_lookup(linker_file_t, Elf_Size, int, Elf_Addr *);
162
163 static kobj_method_t link_elf_methods[] = {
164 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
165 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
166 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
167 KOBJMETHOD(linker_unload, link_elf_unload_file),
168 KOBJMETHOD(linker_load_file, link_elf_load_file),
169 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
170 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
171 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
172 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
173 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
174 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
175 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
176 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
177 { 0, 0 }
178 };
179
180 static struct linker_class link_elf_class = {
181 #if ELF_TARG_CLASS == ELFCLASS32
182 "elf32",
183 #else
184 "elf64",
185 #endif
186 link_elf_methods, sizeof(struct elf_file)
187 };
188
189 typedef int (*elf_reloc_fn)(linker_file_t lf, Elf_Addr relocbase,
190 const void *data, int type, elf_lookup_fn lookup);
191
192 static int parse_dynamic(elf_file_t);
193 static int relocate_file(elf_file_t);
194 static int relocate_file1(elf_file_t ef, elf_lookup_fn lookup,
195 elf_reloc_fn reloc, bool ifuncs);
196 static int link_elf_preload_parse_symbols(elf_file_t);
197
198 static struct elf_set_head set_pcpu_list;
199 #ifdef VIMAGE
200 static struct elf_set_head set_vnet_list;
201 #endif
202
203 static void
204 elf_set_add(struct elf_set_head *list, Elf_Addr start, Elf_Addr stop, Elf_Addr base)
205 {
206 struct elf_set *set, *iter;
207
208 set = malloc(sizeof(*set), M_LINKER, M_WAITOK);
209 set->es_start = start;
210 set->es_stop = stop;
211 set->es_base = base;
212
213 TAILQ_FOREACH(iter, list, es_link) {
214
215 KASSERT((set->es_start < iter->es_start && set->es_stop < iter->es_stop) ||
216 (set->es_start > iter->es_start && set->es_stop > iter->es_stop),
217 ("linker sets intersection: to insert: 0x%jx-0x%jx; inserted: 0x%jx-0x%jx",
218 (uintmax_t)set->es_start, (uintmax_t)set->es_stop,
219 (uintmax_t)iter->es_start, (uintmax_t)iter->es_stop));
220
221 if (iter->es_start > set->es_start) {
222 TAILQ_INSERT_BEFORE(iter, set, es_link);
223 break;
224 }
225 }
226
227 if (iter == NULL)
228 TAILQ_INSERT_TAIL(list, set, es_link);
229 }
230
231 static int
232 elf_set_find(struct elf_set_head *list, Elf_Addr addr, Elf_Addr *start, Elf_Addr *base)
233 {
234 struct elf_set *set;
235
236 TAILQ_FOREACH(set, list, es_link) {
237 if (addr < set->es_start)
238 return (0);
239 if (addr < set->es_stop) {
240 *start = set->es_start;
241 *base = set->es_base;
242 return (1);
243 }
244 }
245
246 return (0);
247 }
248
249 static void
250 elf_set_delete(struct elf_set_head *list, Elf_Addr start)
251 {
252 struct elf_set *set;
253
254 TAILQ_FOREACH(set, list, es_link) {
255 if (start < set->es_start)
256 break;
257 if (start == set->es_start) {
258 TAILQ_REMOVE(list, set, es_link);
259 free(set, M_LINKER);
260 return;
261 }
262 }
263 KASSERT(0, ("deleting unknown linker set (start = 0x%jx)",
264 (uintmax_t)start));
265 }
266
267 #ifdef GDB
268 static void r_debug_state(struct r_debug *, struct link_map *);
269
270 /*
271 * A list of loaded modules for GDB to use for loading symbols.
272 */
273 struct r_debug r_debug;
274
275 #define GDB_STATE(s) do { \
276 r_debug.r_state = s; r_debug_state(NULL, NULL); \
277 } while (0)
278
279 /*
280 * Function for the debugger to set a breakpoint on to gain control.
281 */
282 static void
283 r_debug_state(struct r_debug *dummy_one __unused,
284 struct link_map *dummy_two __unused)
285 {
286 }
287
288 static void
289 link_elf_add_gdb(struct link_map *l)
290 {
291 struct link_map *prev;
292
293 l->l_next = NULL;
294
295 if (r_debug.r_map == NULL) {
296 /* Add first. */
297 l->l_prev = NULL;
298 r_debug.r_map = l;
299 } else {
300 /* Append to list. */
301 for (prev = r_debug.r_map;
302 prev->l_next != NULL;
303 prev = prev->l_next)
304 ;
305 l->l_prev = prev;
306 prev->l_next = l;
307 }
308 }
309
310 static void
311 link_elf_delete_gdb(struct link_map *l)
312 {
313 if (l->l_prev == NULL) {
314 /* Remove first. */
315 if ((r_debug.r_map = l->l_next) != NULL)
316 l->l_next->l_prev = NULL;
317 } else {
318 /* Remove any but first. */
319 if ((l->l_prev->l_next = l->l_next) != NULL)
320 l->l_next->l_prev = l->l_prev;
321 }
322 }
323 #endif /* GDB */
324
325 /*
326 * The kernel symbol table starts here.
327 */
328 extern struct _dynamic _DYNAMIC;
329
330 static void
331 link_elf_error(const char *filename, const char *s)
332 {
333 if (filename == NULL)
334 printf("kldload: %s\n", s);
335 else
336 printf("kldload: %s: %s\n", filename, s);
337 }
338
339 static void
340 link_elf_invoke_ctors(caddr_t addr, size_t size)
341 {
342 void (**ctor)(void);
343 size_t i, cnt;
344
345 if (addr == NULL || size == 0)
346 return;
347 cnt = size / sizeof(*ctor);
348 ctor = (void *)addr;
349 for (i = 0; i < cnt; i++) {
350 if (ctor[i] != NULL)
351 (*ctor[i])();
352 }
353 }
354
355 /*
356 * Actions performed after linking/loading both the preloaded kernel and any
357 * modules; whether preloaded or dynamicly loaded.
358 */
359 static int
360 link_elf_link_common_finish(linker_file_t lf)
361 {
362 #ifdef GDB
363 elf_file_t ef = (elf_file_t)lf;
364 char *newfilename;
365 #endif
366 int error;
367
368 /* Notify MD code that a module is being loaded. */
369 error = elf_cpu_load_file(lf);
370 if (error != 0)
371 return (error);
372
373 #ifdef GDB
374 GDB_STATE(RT_ADD);
375 ef->gdb.l_addr = lf->address;
376 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
377 strcpy(newfilename, lf->filename);
378 ef->gdb.l_name = newfilename;
379 ef->gdb.l_ld = ef->dynamic;
380 link_elf_add_gdb(&ef->gdb);
381 GDB_STATE(RT_CONSISTENT);
382 #endif
383
384 /* Invoke .ctors */
385 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
386 return (0);
387 }
388
389 extern vm_offset_t __startkernel;
390
391 static void
392 link_elf_init(void* arg)
393 {
394 Elf_Dyn *dp;
395 Elf_Addr *ctors_addrp;
396 Elf_Size *ctors_sizep;
397 caddr_t modptr, baseptr, sizeptr;
398 elf_file_t ef;
399 char *modname;
400
401 linker_add_class(&link_elf_class);
402
403 dp = (Elf_Dyn *)&_DYNAMIC;
404 modname = NULL;
405 modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
406 if (modptr == NULL)
407 modptr = preload_search_by_type("elf kernel");
408 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
409 if (modname == NULL)
410 modname = "kernel";
411 linker_kernel_file = linker_make_file(modname, &link_elf_class);
412 if (linker_kernel_file == NULL)
413 panic("%s: Can't create linker structures for kernel",
414 __func__);
415
416 ef = (elf_file_t) linker_kernel_file;
417 ef->preloaded = 1;
418 #ifdef __powerpc__
419 ef->address = (caddr_t) (__startkernel - KERNBASE);
420 #else
421 ef->address = 0;
422 #endif
423 #ifdef SPARSE_MAPPING
424 ef->object = 0;
425 #endif
426 ef->dynamic = dp;
427
428 if (dp != NULL)
429 parse_dynamic(ef);
430 linker_kernel_file->address += KERNBASE;
431 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
432
433 if (modptr != NULL) {
434 ef->modptr = modptr;
435 baseptr = preload_search_info(modptr, MODINFO_ADDR);
436 if (baseptr != NULL)
437 linker_kernel_file->address = *(caddr_t *)baseptr;
438 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
439 if (sizeptr != NULL)
440 linker_kernel_file->size = *(size_t *)sizeptr;
441 ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
442 MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
443 ctors_sizep = (Elf_Size *)preload_search_info(modptr,
444 MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
445 if (ctors_addrp != NULL && ctors_sizep != NULL) {
446 linker_kernel_file->ctors_addr = ef->address +
447 *ctors_addrp;
448 linker_kernel_file->ctors_size = *ctors_sizep;
449 }
450 }
451 (void)link_elf_preload_parse_symbols(ef);
452
453 #ifdef GDB
454 r_debug.r_map = NULL;
455 r_debug.r_brk = r_debug_state;
456 r_debug.r_state = RT_CONSISTENT;
457 #endif
458
459 (void)link_elf_link_common_finish(linker_kernel_file);
460 linker_kernel_file->flags |= LINKER_FILE_LINKED;
461 TAILQ_INIT(&set_pcpu_list);
462 #ifdef VIMAGE
463 TAILQ_INIT(&set_vnet_list);
464 #endif
465 }
466
467 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, NULL);
468
469 static int
470 link_elf_preload_parse_symbols(elf_file_t ef)
471 {
472 caddr_t pointer;
473 caddr_t ssym, esym, base;
474 caddr_t strtab;
475 int strcnt;
476 Elf_Sym *symtab;
477 int symcnt;
478
479 if (ef->modptr == NULL)
480 return (0);
481 pointer = preload_search_info(ef->modptr,
482 MODINFO_METADATA | MODINFOMD_SSYM);
483 if (pointer == NULL)
484 return (0);
485 ssym = *(caddr_t *)pointer;
486 pointer = preload_search_info(ef->modptr,
487 MODINFO_METADATA | MODINFOMD_ESYM);
488 if (pointer == NULL)
489 return (0);
490 esym = *(caddr_t *)pointer;
491
492 base = ssym;
493
494 symcnt = *(long *)base;
495 base += sizeof(long);
496 symtab = (Elf_Sym *)base;
497 base += roundup(symcnt, sizeof(long));
498
499 if (base > esym || base < ssym) {
500 printf("Symbols are corrupt!\n");
501 return (EINVAL);
502 }
503
504 strcnt = *(long *)base;
505 base += sizeof(long);
506 strtab = base;
507 base += roundup(strcnt, sizeof(long));
508
509 if (base > esym || base < ssym) {
510 printf("Symbols are corrupt!\n");
511 return (EINVAL);
512 }
513
514 ef->ddbsymtab = symtab;
515 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
516 ef->ddbstrtab = strtab;
517 ef->ddbstrcnt = strcnt;
518
519 return (0);
520 }
521
522 static int
523 parse_dynamic(elf_file_t ef)
524 {
525 Elf_Dyn *dp;
526 int plttype = DT_REL;
527
528 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
529 switch (dp->d_tag) {
530 case DT_HASH:
531 {
532 /* From src/libexec/rtld-elf/rtld.c */
533 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
534 (ef->address + dp->d_un.d_ptr);
535 ef->nbuckets = hashtab[0];
536 ef->nchains = hashtab[1];
537 ef->buckets = hashtab + 2;
538 ef->chains = ef->buckets + ef->nbuckets;
539 break;
540 }
541 case DT_STRTAB:
542 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
543 break;
544 case DT_STRSZ:
545 ef->strsz = dp->d_un.d_val;
546 break;
547 case DT_SYMTAB:
548 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
549 break;
550 case DT_SYMENT:
551 if (dp->d_un.d_val != sizeof(Elf_Sym))
552 return (ENOEXEC);
553 break;
554 case DT_PLTGOT:
555 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
556 break;
557 case DT_REL:
558 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
559 break;
560 case DT_RELSZ:
561 ef->relsize = dp->d_un.d_val;
562 break;
563 case DT_RELENT:
564 if (dp->d_un.d_val != sizeof(Elf_Rel))
565 return (ENOEXEC);
566 break;
567 case DT_JMPREL:
568 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
569 break;
570 case DT_PLTRELSZ:
571 ef->pltrelsize = dp->d_un.d_val;
572 break;
573 case DT_RELA:
574 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
575 break;
576 case DT_RELASZ:
577 ef->relasize = dp->d_un.d_val;
578 break;
579 case DT_RELAENT:
580 if (dp->d_un.d_val != sizeof(Elf_Rela))
581 return (ENOEXEC);
582 break;
583 case DT_PLTREL:
584 plttype = dp->d_un.d_val;
585 if (plttype != DT_REL && plttype != DT_RELA)
586 return (ENOEXEC);
587 break;
588 #ifdef GDB
589 case DT_DEBUG:
590 dp->d_un.d_ptr = (Elf_Addr)&r_debug;
591 break;
592 #endif
593 }
594 }
595
596 if (plttype == DT_RELA) {
597 ef->pltrela = (const Elf_Rela *)ef->pltrel;
598 ef->pltrel = NULL;
599 ef->pltrelasize = ef->pltrelsize;
600 ef->pltrelsize = 0;
601 }
602
603 ef->ddbsymtab = ef->symtab;
604 ef->ddbsymcnt = ef->nchains;
605 ef->ddbstrtab = ef->strtab;
606 ef->ddbstrcnt = ef->strsz;
607
608 return (0);
609 }
610
611 static int
612 parse_dpcpu(elf_file_t ef)
613 {
614 int error, size;
615
616 ef->pcpu_start = 0;
617 ef->pcpu_stop = 0;
618 error = link_elf_lookup_set(&ef->lf, "pcpu", (void ***)&ef->pcpu_start,
619 (void ***)&ef->pcpu_stop, NULL);
620 /* Error just means there is no pcpu set to relocate. */
621 if (error != 0)
622 return (0);
623 size = (uintptr_t)ef->pcpu_stop - (uintptr_t)ef->pcpu_start;
624 /* Empty set? */
625 if (size < 1)
626 return (0);
627 /*
628 * Allocate space in the primary pcpu area. Copy in our
629 * initialization from the data section and then initialize
630 * all per-cpu storage from that.
631 */
632 ef->pcpu_base = (Elf_Addr)(uintptr_t)dpcpu_alloc(size);
633 if (ef->pcpu_base == 0) {
634 printf("%s: pcpu module space is out of space; "
635 "cannot allocate %d for %s\n",
636 __func__, size, ef->lf.pathname);
637 return (ENOSPC);
638 }
639 memcpy((void *)ef->pcpu_base, (void *)ef->pcpu_start, size);
640 dpcpu_copy((void *)ef->pcpu_base, size);
641 elf_set_add(&set_pcpu_list, ef->pcpu_start, ef->pcpu_stop,
642 ef->pcpu_base);
643
644 return (0);
645 }
646
647 #ifdef VIMAGE
648 static int
649 parse_vnet(elf_file_t ef)
650 {
651 int error, size;
652
653 ef->vnet_start = 0;
654 ef->vnet_stop = 0;
655 error = link_elf_lookup_set(&ef->lf, "vnet", (void ***)&ef->vnet_start,
656 (void ***)&ef->vnet_stop, NULL);
657 /* Error just means there is no vnet data set to relocate. */
658 if (error != 0)
659 return (0);
660 size = (uintptr_t)ef->vnet_stop - (uintptr_t)ef->vnet_start;
661 /* Empty set? */
662 if (size < 1)
663 return (0);
664 /*
665 * Allocate space in the primary vnet area. Copy in our
666 * initialization from the data section and then initialize
667 * all per-vnet storage from that.
668 */
669 ef->vnet_base = (Elf_Addr)(uintptr_t)vnet_data_alloc(size);
670 if (ef->vnet_base == 0) {
671 printf("%s: vnet module space is out of space; "
672 "cannot allocate %d for %s\n",
673 __func__, size, ef->lf.pathname);
674 return (ENOSPC);
675 }
676 memcpy((void *)ef->vnet_base, (void *)ef->vnet_start, size);
677 vnet_data_copy((void *)ef->vnet_base, size);
678 elf_set_add(&set_vnet_list, ef->vnet_start, ef->vnet_stop,
679 ef->vnet_base);
680
681 return (0);
682 }
683 #endif
684
685 static int
686 link_elf_link_preload(linker_class_t cls,
687 const char* filename, linker_file_t *result)
688 {
689 Elf_Addr *ctors_addrp;
690 Elf_Size *ctors_sizep;
691 caddr_t modptr, baseptr, sizeptr, dynptr;
692 char *type;
693 elf_file_t ef;
694 linker_file_t lf;
695 int error;
696 vm_offset_t dp;
697
698 /* Look to see if we have the file preloaded */
699 modptr = preload_search_by_name(filename);
700 if (modptr == NULL)
701 return (ENOENT);
702
703 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
704 baseptr = preload_search_info(modptr, MODINFO_ADDR);
705 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
706 dynptr = preload_search_info(modptr,
707 MODINFO_METADATA | MODINFOMD_DYNAMIC);
708 if (type == NULL ||
709 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
710 strcmp(type, "elf module") != 0))
711 return (EFTYPE);
712 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
713 return (EINVAL);
714
715 lf = linker_make_file(filename, &link_elf_class);
716 if (lf == NULL)
717 return (ENOMEM);
718
719 ef = (elf_file_t) lf;
720 ef->preloaded = 1;
721 ef->modptr = modptr;
722 ef->address = *(caddr_t *)baseptr;
723 #ifdef SPARSE_MAPPING
724 ef->object = 0;
725 #endif
726 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
727 ef->dynamic = (Elf_Dyn *)dp;
728 lf->address = ef->address;
729 lf->size = *(size_t *)sizeptr;
730
731 ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
732 MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
733 ctors_sizep = (Elf_Size *)preload_search_info(modptr,
734 MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
735 if (ctors_addrp != NULL && ctors_sizep != NULL) {
736 lf->ctors_addr = ef->address + *ctors_addrp;
737 lf->ctors_size = *ctors_sizep;
738 }
739
740 error = parse_dynamic(ef);
741 if (error == 0)
742 error = parse_dpcpu(ef);
743 #ifdef VIMAGE
744 if (error == 0)
745 error = parse_vnet(ef);
746 #endif
747 if (error != 0) {
748 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
749 return (error);
750 }
751 link_elf_reloc_local(lf);
752 *result = lf;
753 return (0);
754 }
755
756 static int
757 link_elf_link_preload_finish(linker_file_t lf)
758 {
759 elf_file_t ef;
760 int error;
761
762 ef = (elf_file_t) lf;
763 error = relocate_file(ef);
764 if (error != 0)
765 return (error);
766 (void)link_elf_preload_parse_symbols(ef);
767
768 return (link_elf_link_common_finish(lf));
769 }
770
771 static int
772 link_elf_load_file(linker_class_t cls, const char* filename,
773 linker_file_t* result)
774 {
775 struct nameidata nd;
776 struct thread* td = curthread; /* XXX */
777 Elf_Ehdr *hdr;
778 caddr_t firstpage;
779 int nbytes, i;
780 Elf_Phdr *phdr;
781 Elf_Phdr *phlimit;
782 Elf_Phdr *segs[MAXSEGS];
783 int nsegs;
784 Elf_Phdr *phdyn;
785 Elf_Phdr *phphdr;
786 caddr_t mapbase;
787 size_t mapsize;
788 Elf_Off base_offset;
789 Elf_Addr base_vaddr;
790 Elf_Addr base_vlimit;
791 int error = 0;
792 ssize_t resid;
793 int flags;
794 elf_file_t ef;
795 linker_file_t lf;
796 Elf_Shdr *shdr;
797 int symtabindex;
798 int symstrindex;
799 int shstrindex;
800 int symcnt;
801 int strcnt;
802 char *shstrs;
803
804 shdr = NULL;
805 lf = NULL;
806 shstrs = NULL;
807
808 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
809 flags = FREAD;
810 error = vn_open(&nd, &flags, 0, NULL);
811 if (error != 0)
812 return (error);
813 NDFREE(&nd, NDF_ONLY_PNBUF);
814 if (nd.ni_vp->v_type != VREG) {
815 error = ENOEXEC;
816 firstpage = NULL;
817 goto out;
818 }
819 #ifdef MAC
820 error = mac_kld_check_load(curthread->td_ucred, nd.ni_vp);
821 if (error != 0) {
822 firstpage = NULL;
823 goto out;
824 }
825 #endif
826
827 /*
828 * Read the elf header from the file.
829 */
830 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
831 hdr = (Elf_Ehdr *)firstpage;
832 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
833 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
834 &resid, td);
835 nbytes = PAGE_SIZE - resid;
836 if (error != 0)
837 goto out;
838
839 if (!IS_ELF(*hdr)) {
840 error = ENOEXEC;
841 goto out;
842 }
843
844 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
845 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
846 link_elf_error(filename, "Unsupported file layout");
847 error = ENOEXEC;
848 goto out;
849 }
850 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
851 hdr->e_version != EV_CURRENT) {
852 link_elf_error(filename, "Unsupported file version");
853 error = ENOEXEC;
854 goto out;
855 }
856 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
857 error = ENOSYS;
858 goto out;
859 }
860 if (hdr->e_machine != ELF_TARG_MACH) {
861 link_elf_error(filename, "Unsupported machine");
862 error = ENOEXEC;
863 goto out;
864 }
865
866 /*
867 * We rely on the program header being in the first page.
868 * This is not strictly required by the ABI specification, but
869 * it seems to always true in practice. And, it simplifies
870 * things considerably.
871 */
872 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
873 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
874 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
875 link_elf_error(filename, "Unreadable program headers");
876
877 /*
878 * Scan the program header entries, and save key information.
879 *
880 * We rely on there being exactly two load segments, text and data,
881 * in that order.
882 */
883 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
884 phlimit = phdr + hdr->e_phnum;
885 nsegs = 0;
886 phdyn = NULL;
887 phphdr = NULL;
888 while (phdr < phlimit) {
889 switch (phdr->p_type) {
890 case PT_LOAD:
891 if (nsegs == MAXSEGS) {
892 link_elf_error(filename, "Too many sections");
893 error = ENOEXEC;
894 goto out;
895 }
896 /*
897 * XXX: We just trust they come in right order ??
898 */
899 segs[nsegs] = phdr;
900 ++nsegs;
901 break;
902
903 case PT_PHDR:
904 phphdr = phdr;
905 break;
906
907 case PT_DYNAMIC:
908 phdyn = phdr;
909 break;
910
911 case PT_INTERP:
912 error = ENOSYS;
913 goto out;
914 }
915
916 ++phdr;
917 }
918 if (phdyn == NULL) {
919 link_elf_error(filename, "Object is not dynamically-linked");
920 error = ENOEXEC;
921 goto out;
922 }
923 if (nsegs == 0) {
924 link_elf_error(filename, "No sections");
925 error = ENOEXEC;
926 goto out;
927 }
928
929 /*
930 * Allocate the entire address space of the object, to stake
931 * out our contiguous region, and to establish the base
932 * address for relocation.
933 */
934 base_offset = trunc_page(segs[0]->p_offset);
935 base_vaddr = trunc_page(segs[0]->p_vaddr);
936 base_vlimit = round_page(segs[nsegs - 1]->p_vaddr +
937 segs[nsegs - 1]->p_memsz);
938 mapsize = base_vlimit - base_vaddr;
939
940 lf = linker_make_file(filename, &link_elf_class);
941 if (lf == NULL) {
942 error = ENOMEM;
943 goto out;
944 }
945
946 ef = (elf_file_t) lf;
947 #ifdef SPARSE_MAPPING
948 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
949 if (ef->object == NULL) {
950 error = ENOMEM;
951 goto out;
952 }
953 ef->address = (caddr_t) vm_map_min(kernel_map);
954 error = vm_map_find(kernel_map, ef->object, 0,
955 (vm_offset_t *) &ef->address, mapsize, 0, VMFS_OPTIMAL_SPACE,
956 VM_PROT_ALL, VM_PROT_ALL, 0);
957 if (error != 0) {
958 vm_object_deallocate(ef->object);
959 ef->object = 0;
960 goto out;
961 }
962 #else
963 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
964 #endif
965 mapbase = ef->address;
966
967 /*
968 * Read the text and data sections and zero the bss.
969 */
970 for (i = 0; i < nsegs; i++) {
971 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
972 error = vn_rdwr(UIO_READ, nd.ni_vp,
973 segbase, segs[i]->p_filesz, segs[i]->p_offset,
974 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
975 &resid, td);
976 if (error != 0)
977 goto out;
978 bzero(segbase + segs[i]->p_filesz,
979 segs[i]->p_memsz - segs[i]->p_filesz);
980
981 #ifdef SPARSE_MAPPING
982 /*
983 * Wire down the pages
984 */
985 error = vm_map_wire(kernel_map,
986 (vm_offset_t) segbase,
987 (vm_offset_t) segbase + segs[i]->p_memsz,
988 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
989 if (error != KERN_SUCCESS) {
990 error = ENOMEM;
991 goto out;
992 }
993 #endif
994 }
995
996 #ifdef GPROF
997 /* Update profiling information with the new text segment. */
998 mtx_lock(&Giant);
999 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
1000 segs[0]->p_memsz));
1001 mtx_unlock(&Giant);
1002 #endif
1003
1004 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
1005
1006 lf->address = ef->address;
1007 lf->size = mapsize;
1008
1009 error = parse_dynamic(ef);
1010 if (error != 0)
1011 goto out;
1012 error = parse_dpcpu(ef);
1013 if (error != 0)
1014 goto out;
1015 #ifdef VIMAGE
1016 error = parse_vnet(ef);
1017 if (error != 0)
1018 goto out;
1019 #endif
1020 link_elf_reloc_local(lf);
1021
1022 VOP_UNLOCK(nd.ni_vp, 0);
1023 error = linker_load_dependencies(lf);
1024 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1025 if (error != 0)
1026 goto out;
1027 error = relocate_file(ef);
1028 if (error != 0)
1029 goto out;
1030
1031 /*
1032 * Try and load the symbol table if it's present. (you can
1033 * strip it!)
1034 */
1035 nbytes = hdr->e_shnum * hdr->e_shentsize;
1036 if (nbytes == 0 || hdr->e_shoff == 0)
1037 goto nosyms;
1038 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1039 error = vn_rdwr(UIO_READ, nd.ni_vp,
1040 (caddr_t)shdr, nbytes, hdr->e_shoff,
1041 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1042 &resid, td);
1043 if (error != 0)
1044 goto out;
1045
1046 /* Read section string table */
1047 shstrindex = hdr->e_shstrndx;
1048 if (shstrindex != 0 && shdr[shstrindex].sh_type == SHT_STRTAB &&
1049 shdr[shstrindex].sh_size != 0) {
1050 nbytes = shdr[shstrindex].sh_size;
1051 shstrs = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1052 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shstrs, nbytes,
1053 shdr[shstrindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
1054 td->td_ucred, NOCRED, &resid, td);
1055 if (error)
1056 goto out;
1057 }
1058
1059 symtabindex = -1;
1060 symstrindex = -1;
1061 for (i = 0; i < hdr->e_shnum; i++) {
1062 if (shdr[i].sh_type == SHT_SYMTAB) {
1063 symtabindex = i;
1064 symstrindex = shdr[i].sh_link;
1065 } else if (shstrs != NULL && shdr[i].sh_name != 0 &&
1066 strcmp(shstrs + shdr[i].sh_name, ".ctors") == 0) {
1067 /* Record relocated address and size of .ctors. */
1068 lf->ctors_addr = mapbase + shdr[i].sh_addr - base_vaddr;
1069 lf->ctors_size = shdr[i].sh_size;
1070 }
1071 }
1072 if (symtabindex < 0 || symstrindex < 0)
1073 goto nosyms;
1074
1075 symcnt = shdr[symtabindex].sh_size;
1076 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
1077 strcnt = shdr[symstrindex].sh_size;
1078 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
1079
1080 error = vn_rdwr(UIO_READ, nd.ni_vp,
1081 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
1082 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1083 &resid, td);
1084 if (error != 0)
1085 goto out;
1086 error = vn_rdwr(UIO_READ, nd.ni_vp,
1087 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
1088 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1089 &resid, td);
1090 if (error != 0)
1091 goto out;
1092
1093 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
1094 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
1095 ef->ddbstrcnt = strcnt;
1096 ef->ddbstrtab = ef->strbase;
1097
1098 nosyms:
1099 error = link_elf_link_common_finish(lf);
1100 if (error != 0)
1101 goto out;
1102
1103 *result = lf;
1104
1105 out:
1106 VOP_UNLOCK(nd.ni_vp, 0);
1107 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1108 if (error != 0 && lf != NULL)
1109 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1110 free(shdr, M_LINKER);
1111 free(firstpage, M_LINKER);
1112 free(shstrs, M_LINKER);
1113
1114 return (error);
1115 }
1116
1117 Elf_Addr
1118 elf_relocaddr(linker_file_t lf, Elf_Addr x)
1119 {
1120 elf_file_t ef;
1121
1122 ef = (elf_file_t)lf;
1123 if (x >= ef->pcpu_start && x < ef->pcpu_stop)
1124 return ((x - ef->pcpu_start) + ef->pcpu_base);
1125 #ifdef VIMAGE
1126 if (x >= ef->vnet_start && x < ef->vnet_stop)
1127 return ((x - ef->vnet_start) + ef->vnet_base);
1128 #endif
1129 return (x);
1130 }
1131
1132
1133 static void
1134 link_elf_unload_file(linker_file_t file)
1135 {
1136 elf_file_t ef = (elf_file_t) file;
1137
1138 if (ef->pcpu_base != 0) {
1139 dpcpu_free((void *)ef->pcpu_base,
1140 ef->pcpu_stop - ef->pcpu_start);
1141 elf_set_delete(&set_pcpu_list, ef->pcpu_start);
1142 }
1143 #ifdef VIMAGE
1144 if (ef->vnet_base != 0) {
1145 vnet_data_free((void *)ef->vnet_base,
1146 ef->vnet_stop - ef->vnet_start);
1147 elf_set_delete(&set_vnet_list, ef->vnet_start);
1148 }
1149 #endif
1150 #ifdef GDB
1151 if (ef->gdb.l_ld != NULL) {
1152 GDB_STATE(RT_DELETE);
1153 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
1154 link_elf_delete_gdb(&ef->gdb);
1155 GDB_STATE(RT_CONSISTENT);
1156 }
1157 #endif
1158
1159 /* Notify MD code that a module is being unloaded. */
1160 elf_cpu_unload_file(file);
1161
1162 if (ef->preloaded) {
1163 link_elf_unload_preload(file);
1164 return;
1165 }
1166
1167 #ifdef SPARSE_MAPPING
1168 if (ef->object != NULL) {
1169 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1170 (vm_offset_t) ef->address
1171 + (ef->object->size << PAGE_SHIFT));
1172 }
1173 #else
1174 free(ef->address, M_LINKER);
1175 #endif
1176 free(ef->symbase, M_LINKER);
1177 free(ef->strbase, M_LINKER);
1178 free(ef->ctftab, M_LINKER);
1179 free(ef->ctfoff, M_LINKER);
1180 free(ef->typoff, M_LINKER);
1181 }
1182
1183 static void
1184 link_elf_unload_preload(linker_file_t file)
1185 {
1186 if (file->pathname != NULL)
1187 preload_delete_name(file->pathname);
1188 }
1189
1190 static const char *
1191 symbol_name(elf_file_t ef, Elf_Size r_info)
1192 {
1193 const Elf_Sym *ref;
1194
1195 if (ELF_R_SYM(r_info)) {
1196 ref = ef->symtab + ELF_R_SYM(r_info);
1197 return (ef->strtab + ref->st_name);
1198 }
1199 return (NULL);
1200 }
1201
1202 static int
1203 symbol_type(elf_file_t ef, Elf_Size r_info)
1204 {
1205 const Elf_Sym *ref;
1206
1207 if (ELF_R_SYM(r_info)) {
1208 ref = ef->symtab + ELF_R_SYM(r_info);
1209 return (ELF_ST_TYPE(ref->st_info));
1210 }
1211 return (STT_NOTYPE);
1212 }
1213
1214 static int
1215 relocate_file1(elf_file_t ef, elf_lookup_fn lookup, elf_reloc_fn reloc,
1216 bool ifuncs)
1217 {
1218 const Elf_Rel *rel;
1219 const Elf_Rela *rela;
1220 const char *symname;
1221
1222 #define APPLY_RELOCS(iter, tbl, tblsize, type) do { \
1223 for ((iter) = (tbl); (iter) != NULL && \
1224 (iter) < (tbl) + (tblsize) / sizeof(*(iter)); (iter)++) { \
1225 if ((symbol_type(ef, (iter)->r_info) == \
1226 STT_GNU_IFUNC || \
1227 elf_is_ifunc_reloc((iter)->r_info)) != ifuncs) \
1228 continue; \
1229 if (reloc(&ef->lf, (Elf_Addr)ef->address, \
1230 (iter), (type), lookup)) { \
1231 symname = symbol_name(ef, (iter)->r_info); \
1232 printf("link_elf: symbol %s undefined\n", \
1233 symname); \
1234 return (ENOENT); \
1235 } \
1236 } \
1237 } while (0)
1238
1239 APPLY_RELOCS(rel, ef->rel, ef->relsize, ELF_RELOC_REL);
1240 APPLY_RELOCS(rela, ef->rela, ef->relasize, ELF_RELOC_RELA);
1241 APPLY_RELOCS(rel, ef->pltrel, ef->pltrelsize, ELF_RELOC_REL);
1242 APPLY_RELOCS(rela, ef->pltrela, ef->pltrelasize, ELF_RELOC_RELA);
1243
1244 #undef APPLY_RELOCS
1245
1246 return (0);
1247 }
1248
1249 static int
1250 relocate_file(elf_file_t ef)
1251 {
1252 int error;
1253
1254 error = relocate_file1(ef, elf_lookup, elf_reloc, false);
1255 if (error == 0)
1256 error = relocate_file1(ef, elf_lookup, elf_reloc, true);
1257 return (error);
1258 }
1259
1260 /*
1261 * Hash function for symbol table lookup. Don't even think about changing
1262 * this. It is specified by the System V ABI.
1263 */
1264 static unsigned long
1265 elf_hash(const char *name)
1266 {
1267 const unsigned char *p = (const unsigned char *) name;
1268 unsigned long h = 0;
1269 unsigned long g;
1270
1271 while (*p != '\0') {
1272 h = (h << 4) + *p++;
1273 if ((g = h & 0xf0000000) != 0)
1274 h ^= g >> 24;
1275 h &= ~g;
1276 }
1277 return (h);
1278 }
1279
1280 static int
1281 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1282 {
1283 elf_file_t ef = (elf_file_t) lf;
1284 unsigned long symnum;
1285 const Elf_Sym* symp;
1286 const char *strp;
1287 unsigned long hash;
1288 int i;
1289
1290 /* If we don't have a hash, bail. */
1291 if (ef->buckets == NULL || ef->nbuckets == 0) {
1292 printf("link_elf_lookup_symbol: missing symbol hash table\n");
1293 return (ENOENT);
1294 }
1295
1296 /* First, search hashed global symbols */
1297 hash = elf_hash(name);
1298 symnum = ef->buckets[hash % ef->nbuckets];
1299
1300 while (symnum != STN_UNDEF) {
1301 if (symnum >= ef->nchains) {
1302 printf("%s: corrupt symbol table\n", __func__);
1303 return (ENOENT);
1304 }
1305
1306 symp = ef->symtab + symnum;
1307 if (symp->st_name == 0) {
1308 printf("%s: corrupt symbol table\n", __func__);
1309 return (ENOENT);
1310 }
1311
1312 strp = ef->strtab + symp->st_name;
1313
1314 if (strcmp(name, strp) == 0) {
1315 if (symp->st_shndx != SHN_UNDEF ||
1316 (symp->st_value != 0 &&
1317 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1318 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC))) {
1319 *sym = (c_linker_sym_t) symp;
1320 return (0);
1321 }
1322 return (ENOENT);
1323 }
1324
1325 symnum = ef->chains[symnum];
1326 }
1327
1328 /* If we have not found it, look at the full table (if loaded) */
1329 if (ef->symtab == ef->ddbsymtab)
1330 return (ENOENT);
1331
1332 /* Exhaustive search */
1333 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1334 strp = ef->ddbstrtab + symp->st_name;
1335 if (strcmp(name, strp) == 0) {
1336 if (symp->st_shndx != SHN_UNDEF ||
1337 (symp->st_value != 0 &&
1338 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1339 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC))) {
1340 *sym = (c_linker_sym_t) symp;
1341 return (0);
1342 }
1343 return (ENOENT);
1344 }
1345 }
1346
1347 return (ENOENT);
1348 }
1349
1350 static int
1351 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1352 linker_symval_t *symval)
1353 {
1354 elf_file_t ef;
1355 const Elf_Sym *es;
1356 caddr_t val;
1357
1358 ef = (elf_file_t)lf;
1359 es = (const Elf_Sym *)sym;
1360 if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1361 symval->name = ef->strtab + es->st_name;
1362 val = (caddr_t)ef->address + es->st_value;
1363 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1364 val = ((caddr_t (*)(void))val)();
1365 symval->value = val;
1366 symval->size = es->st_size;
1367 return (0);
1368 }
1369 if (ef->symtab == ef->ddbsymtab)
1370 return (ENOENT);
1371 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1372 symval->name = ef->ddbstrtab + es->st_name;
1373 val = (caddr_t)ef->address + es->st_value;
1374 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1375 val = ((caddr_t (*)(void))val)();
1376 symval->value = val;
1377 symval->size = es->st_size;
1378 return (0);
1379 }
1380 return (ENOENT);
1381 }
1382
1383 static int
1384 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1385 c_linker_sym_t *sym, long *diffp)
1386 {
1387 elf_file_t ef = (elf_file_t) lf;
1388 u_long off = (uintptr_t) (void *) value;
1389 u_long diff = off;
1390 u_long st_value;
1391 const Elf_Sym* es;
1392 const Elf_Sym* best = NULL;
1393 int i;
1394
1395 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1396 if (es->st_name == 0)
1397 continue;
1398 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1399 if (off >= st_value) {
1400 if (off - st_value < diff) {
1401 diff = off - st_value;
1402 best = es;
1403 if (diff == 0)
1404 break;
1405 } else if (off - st_value == diff) {
1406 best = es;
1407 }
1408 }
1409 }
1410 if (best == NULL)
1411 *diffp = off;
1412 else
1413 *diffp = diff;
1414 *sym = (c_linker_sym_t) best;
1415
1416 return (0);
1417 }
1418
1419 /*
1420 * Look up a linker set on an ELF system.
1421 */
1422 static int
1423 link_elf_lookup_set(linker_file_t lf, const char *name,
1424 void ***startp, void ***stopp, int *countp)
1425 {
1426 c_linker_sym_t sym;
1427 linker_symval_t symval;
1428 char *setsym;
1429 void **start, **stop;
1430 int len, error = 0, count;
1431
1432 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1433 setsym = malloc(len, M_LINKER, M_WAITOK);
1434
1435 /* get address of first entry */
1436 snprintf(setsym, len, "%s%s", "__start_set_", name);
1437 error = link_elf_lookup_symbol(lf, setsym, &sym);
1438 if (error != 0)
1439 goto out;
1440 link_elf_symbol_values(lf, sym, &symval);
1441 if (symval.value == 0) {
1442 error = ESRCH;
1443 goto out;
1444 }
1445 start = (void **)symval.value;
1446
1447 /* get address of last entry */
1448 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1449 error = link_elf_lookup_symbol(lf, setsym, &sym);
1450 if (error != 0)
1451 goto out;
1452 link_elf_symbol_values(lf, sym, &symval);
1453 if (symval.value == 0) {
1454 error = ESRCH;
1455 goto out;
1456 }
1457 stop = (void **)symval.value;
1458
1459 /* and the number of entries */
1460 count = stop - start;
1461
1462 /* and copy out */
1463 if (startp != NULL)
1464 *startp = start;
1465 if (stopp != NULL)
1466 *stopp = stop;
1467 if (countp != NULL)
1468 *countp = count;
1469
1470 out:
1471 free(setsym, M_LINKER);
1472 return (error);
1473 }
1474
1475 static int
1476 link_elf_each_function_name(linker_file_t file,
1477 int (*callback)(const char *, void *), void *opaque)
1478 {
1479 elf_file_t ef = (elf_file_t)file;
1480 const Elf_Sym *symp;
1481 int i, error;
1482
1483 /* Exhaustive search */
1484 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1485 if (symp->st_value != 0 &&
1486 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1487 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1488 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1489 if (error != 0)
1490 return (error);
1491 }
1492 }
1493 return (0);
1494 }
1495
1496 static int
1497 link_elf_each_function_nameval(linker_file_t file,
1498 linker_function_nameval_callback_t callback, void *opaque)
1499 {
1500 linker_symval_t symval;
1501 elf_file_t ef = (elf_file_t)file;
1502 const Elf_Sym* symp;
1503 int i, error;
1504
1505 /* Exhaustive search */
1506 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1507 if (symp->st_value != 0 &&
1508 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1509 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1510 error = link_elf_symbol_values(file,
1511 (c_linker_sym_t) symp, &symval);
1512 if (error != 0)
1513 return (error);
1514 error = callback(file, i, &symval, opaque);
1515 if (error != 0)
1516 return (error);
1517 }
1518 }
1519 return (0);
1520 }
1521
1522 const Elf_Sym *
1523 elf_get_sym(linker_file_t lf, Elf_Size symidx)
1524 {
1525 elf_file_t ef = (elf_file_t)lf;
1526
1527 if (symidx >= ef->nchains)
1528 return (NULL);
1529 return (ef->symtab + symidx);
1530 }
1531
1532 const char *
1533 elf_get_symname(linker_file_t lf, Elf_Size symidx)
1534 {
1535 elf_file_t ef = (elf_file_t)lf;
1536 const Elf_Sym *sym;
1537
1538 if (symidx >= ef->nchains)
1539 return (NULL);
1540 sym = ef->symtab + symidx;
1541 return (ef->strtab + sym->st_name);
1542 }
1543
1544 /*
1545 * Symbol lookup function that can be used when the symbol index is known (ie
1546 * in relocations). It uses the symbol index instead of doing a fully fledged
1547 * hash table based lookup when such is valid. For example for local symbols.
1548 * This is not only more efficient, it's also more correct. It's not always
1549 * the case that the symbol can be found through the hash table.
1550 */
1551 static int
1552 elf_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1553 {
1554 elf_file_t ef = (elf_file_t)lf;
1555 const Elf_Sym *sym;
1556 const char *symbol;
1557 Elf_Addr addr, start, base;
1558
1559 /* Don't even try to lookup the symbol if the index is bogus. */
1560 if (symidx >= ef->nchains) {
1561 *res = 0;
1562 return (EINVAL);
1563 }
1564
1565 sym = ef->symtab + symidx;
1566
1567 /*
1568 * Don't do a full lookup when the symbol is local. It may even
1569 * fail because it may not be found through the hash table.
1570 */
1571 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1572 /* Force lookup failure when we have an insanity. */
1573 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0) {
1574 *res = 0;
1575 return (EINVAL);
1576 }
1577 *res = ((Elf_Addr)ef->address + sym->st_value);
1578 return (0);
1579 }
1580
1581 /*
1582 * XXX we can avoid doing a hash table based lookup for global
1583 * symbols as well. This however is not always valid, so we'll
1584 * just do it the hard way for now. Performance tweaks can
1585 * always be added.
1586 */
1587
1588 symbol = ef->strtab + sym->st_name;
1589
1590 /* Force a lookup failure if the symbol name is bogus. */
1591 if (*symbol == 0) {
1592 *res = 0;
1593 return (EINVAL);
1594 }
1595
1596 addr = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1597 if (addr == 0 && ELF_ST_BIND(sym->st_info) != STB_WEAK) {
1598 *res = 0;
1599 return (EINVAL);
1600 }
1601
1602 if (elf_set_find(&set_pcpu_list, addr, &start, &base))
1603 addr = addr - start + base;
1604 #ifdef VIMAGE
1605 else if (elf_set_find(&set_vnet_list, addr, &start, &base))
1606 addr = addr - start + base;
1607 #endif
1608 *res = addr;
1609 return (0);
1610 }
1611
1612 static void
1613 link_elf_reloc_local(linker_file_t lf)
1614 {
1615 const Elf_Rel *rellim;
1616 const Elf_Rel *rel;
1617 const Elf_Rela *relalim;
1618 const Elf_Rela *rela;
1619 elf_file_t ef = (elf_file_t)lf;
1620
1621 /* Perform relocations without addend if there are any: */
1622 if ((rel = ef->rel) != NULL) {
1623 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1624 while (rel < rellim) {
1625 elf_reloc_local(lf, (Elf_Addr)ef->address, rel,
1626 ELF_RELOC_REL, elf_lookup);
1627 rel++;
1628 }
1629 }
1630
1631 /* Perform relocations with addend if there are any: */
1632 if ((rela = ef->rela) != NULL) {
1633 relalim = (const Elf_Rela *)
1634 ((const char *)ef->rela + ef->relasize);
1635 while (rela < relalim) {
1636 elf_reloc_local(lf, (Elf_Addr)ef->address, rela,
1637 ELF_RELOC_RELA, elf_lookup);
1638 rela++;
1639 }
1640 }
1641 }
1642
1643 static long
1644 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1645 {
1646 elf_file_t ef = (elf_file_t)lf;
1647
1648 *symtab = ef->ddbsymtab;
1649
1650 if (*symtab == NULL)
1651 return (0);
1652
1653 return (ef->ddbsymcnt);
1654 }
1655
1656 static long
1657 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1658 {
1659 elf_file_t ef = (elf_file_t)lf;
1660
1661 *strtab = ef->ddbstrtab;
1662
1663 if (*strtab == NULL)
1664 return (0);
1665
1666 return (ef->ddbstrcnt);
1667 }
1668
1669 #if defined(__i386__) || defined(__amd64__)
1670 /*
1671 * Use this lookup routine when performing relocations early during boot.
1672 * The generic lookup routine depends on kobj, which is not initialized
1673 * at that point.
1674 */
1675 static int
1676 elf_lookup_ifunc(linker_file_t lf, Elf_Size symidx, int deps __unused,
1677 Elf_Addr *res)
1678 {
1679 elf_file_t ef;
1680 const Elf_Sym *symp;
1681 caddr_t val;
1682
1683 ef = (elf_file_t)lf;
1684 symp = ef->symtab + symidx;
1685 if (ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC) {
1686 val = (caddr_t)ef->address + symp->st_value;
1687 *res = ((Elf_Addr (*)(void))val)();
1688 return (0);
1689 }
1690 return (ENOENT);
1691 }
1692
1693 void
1694 link_elf_ireloc(caddr_t kmdp)
1695 {
1696 struct elf_file eff;
1697 elf_file_t ef;
1698 volatile char *c;
1699 size_t i;
1700
1701 ef = &eff;
1702
1703 /* Do not use bzero/memset before ireloc is done. */
1704 for (c = (char *)ef, i = 0; i < sizeof(*ef); i++)
1705 c[i] = 0;
1706
1707 ef->modptr = kmdp;
1708 ef->dynamic = (Elf_Dyn *)&_DYNAMIC;
1709 parse_dynamic(ef);
1710 ef->address = 0;
1711 link_elf_preload_parse_symbols(ef);
1712 relocate_file1(ef, elf_lookup_ifunc, elf_reloc, true);
1713 }
1714 #endif
Cache object: d55621bdf25612f54f9b5715849bf5fb
|