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