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