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