1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1998-2000 Doug Rabson 5 * Copyright (c) 2004 Peter Wemm 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #include "opt_ddb.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/fcntl.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/linker.h> 40 #include <sys/mutex.h> 41 #include <sys/mount.h> 42 #include <sys/namei.h> 43 #include <sys/proc.h> 44 #include <sys/rwlock.h> 45 #include <sys/sysctl.h> 46 #include <sys/vnode.h> 47 48 #include <machine/elf.h> 49 50 #include <net/vnet.h> 51 52 #include <security/mac/mac_framework.h> 53 54 #include <vm/vm.h> 55 #include <vm/vm_param.h> 56 #include <vm/pmap.h> 57 #include <vm/vm_extern.h> 58 #include <vm/vm_kern.h> 59 #include <vm/vm_map.h> 60 #include <vm/vm_object.h> 61 #include <vm/vm_page.h> 62 #include <vm/vm_pager.h> 63 64 #include <sys/link_elf.h> 65 66 #ifdef DDB_CTF 67 #include <contrib/zlib/zlib.h> 68 #endif 69 70 #include "linker_if.h" 71 72 typedef struct { 73 void *addr; 74 Elf_Off size; 75 int flags; /* Section flags. */ 76 int sec; /* Original section number. */ 77 char *name; 78 } Elf_progent; 79 80 typedef struct { 81 Elf_Rel *rel; 82 int nrel; 83 int sec; 84 } Elf_relent; 85 86 typedef struct { 87 Elf_Rela *rela; 88 int nrela; 89 int sec; 90 } Elf_relaent; 91 92 typedef struct elf_file { 93 struct linker_file lf; /* Common fields */ 94 95 int preloaded; 96 caddr_t address; /* Relocation address */ 97 vm_object_t object; /* VM object to hold file pages */ 98 Elf_Shdr *e_shdr; 99 100 Elf_progent *progtab; 101 u_int nprogtab; 102 103 Elf_relaent *relatab; 104 u_int nrelatab; 105 106 Elf_relent *reltab; 107 int nreltab; 108 109 Elf_Sym *ddbsymtab; /* The symbol table we are using */ 110 long ddbsymcnt; /* Number of symbols */ 111 caddr_t ddbstrtab; /* String table */ 112 long ddbstrcnt; /* number of bytes in string table */ 113 114 caddr_t shstrtab; /* Section name string table */ 115 long shstrcnt; /* number of bytes in string table */ 116 117 caddr_t ctftab; /* CTF table */ 118 long ctfcnt; /* number of bytes in CTF table */ 119 caddr_t ctfoff; /* CTF offset table */ 120 caddr_t typoff; /* Type offset table */ 121 long typlen; /* Number of type entries. */ 122 123 } *elf_file_t; 124 125 #include <kern/kern_ctf.c> 126 127 static int link_elf_link_preload(linker_class_t cls, 128 const char *, linker_file_t *); 129 static int link_elf_link_preload_finish(linker_file_t); 130 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *); 131 static int link_elf_lookup_symbol(linker_file_t, const char *, 132 c_linker_sym_t *); 133 static int link_elf_lookup_debug_symbol(linker_file_t, const char *, 134 c_linker_sym_t *); 135 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, 136 linker_symval_t *); 137 static int link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t, 138 linker_symval_t *); 139 static int link_elf_search_symbol(linker_file_t, caddr_t value, 140 c_linker_sym_t *sym, long *diffp); 141 142 static void link_elf_unload_file(linker_file_t); 143 static int link_elf_lookup_set(linker_file_t, const char *, 144 void ***, void ***, int *); 145 static int link_elf_each_function_name(linker_file_t, 146 int (*)(const char *, void *), void *); 147 static int link_elf_each_function_nameval(linker_file_t, 148 linker_function_nameval_callback_t, 149 void *); 150 static int link_elf_reloc_local(linker_file_t, bool); 151 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **); 152 static long link_elf_strtab_get(linker_file_t, caddr_t *); 153 154 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, 155 Elf_Addr *); 156 157 static kobj_method_t link_elf_methods[] = { 158 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol), 159 KOBJMETHOD(linker_lookup_debug_symbol, link_elf_lookup_debug_symbol), 160 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values), 161 KOBJMETHOD(linker_debug_symbol_values, link_elf_debug_symbol_values), 162 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol), 163 KOBJMETHOD(linker_unload, link_elf_unload_file), 164 KOBJMETHOD(linker_load_file, link_elf_load_file), 165 KOBJMETHOD(linker_link_preload, link_elf_link_preload), 166 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish), 167 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set), 168 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name), 169 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval), 170 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get), 171 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get), 172 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get), 173 KOBJMETHOD_END 174 }; 175 176 static struct linker_class link_elf_class = { 177 #if ELF_TARG_CLASS == ELFCLASS32 178 "elf32_obj", 179 #else 180 "elf64_obj", 181 #endif 182 link_elf_methods, sizeof(struct elf_file) 183 }; 184 185 static bool link_elf_obj_leak_locals = true; 186 SYSCTL_BOOL(_debug, OID_AUTO, link_elf_obj_leak_locals, 187 CTLFLAG_RWTUN, &link_elf_obj_leak_locals, 0, 188 "Allow local symbols to participate in global module symbol resolution"); 189 190 static int relocate_file(elf_file_t ef); 191 static void elf_obj_cleanup_globals_cache(elf_file_t); 192 193 static void 194 link_elf_error(const char *filename, const char *s) 195 { 196 if (filename == NULL) 197 printf("kldload: %s\n", s); 198 else 199 printf("kldload: %s: %s\n", filename, s); 200 } 201 202 static void 203 link_elf_init(void *arg) 204 { 205 206 linker_add_class(&link_elf_class); 207 } 208 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL); 209 210 static void 211 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end, 212 vm_prot_t prot) 213 { 214 int error __unused; 215 216 KASSERT(start <= end && start >= (vm_offset_t)ef->address && 217 end <= round_page((vm_offset_t)ef->address + ef->lf.size), 218 ("link_elf_protect_range: invalid range %#jx-%#jx", 219 (uintmax_t)start, (uintmax_t)end)); 220 221 if (start == end) 222 return; 223 if (ef->preloaded) { 224 #ifdef __amd64__ 225 error = pmap_change_prot(start, end - start, prot); 226 KASSERT(error == 0, 227 ("link_elf_protect_range: pmap_change_prot() returned %d", 228 error)); 229 #endif 230 return; 231 } 232 error = vm_map_protect(kernel_map, start, end, prot, 0, 233 VM_MAP_PROTECT_SET_PROT); 234 KASSERT(error == KERN_SUCCESS, 235 ("link_elf_protect_range: vm_map_protect() returned %d", error)); 236 } 237 238 /* 239 * Restrict permissions on linker file memory based on section flags. 240 * Sections need not be page-aligned, so overlap within a page is possible. 241 */ 242 static void 243 link_elf_protect(elf_file_t ef) 244 { 245 vm_offset_t end, segend, segstart, start; 246 vm_prot_t gapprot, prot, segprot; 247 int i; 248 249 /* 250 * If the file was preloaded, the last page may contain other preloaded 251 * data which may need to be writeable. ELF files are always 252 * page-aligned, but other preloaded data, such as entropy or CPU 253 * microcode may be loaded with a smaller alignment. 254 */ 255 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ; 256 257 start = end = (vm_offset_t)ef->address; 258 prot = VM_PROT_READ; 259 for (i = 0; i < ef->nprogtab; i++) { 260 /* 261 * VNET and DPCPU sections have their memory allocated by their 262 * respective subsystems. 263 */ 264 if (ef->progtab[i].name != NULL && ( 265 #ifdef VIMAGE 266 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 || 267 #endif 268 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0)) 269 continue; 270 271 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr); 272 segend = round_page((vm_offset_t)ef->progtab[i].addr + 273 ef->progtab[i].size); 274 segprot = VM_PROT_READ; 275 if ((ef->progtab[i].flags & SHF_WRITE) != 0) 276 segprot |= VM_PROT_WRITE; 277 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0) 278 segprot |= VM_PROT_EXECUTE; 279 280 if (end <= segstart) { 281 /* 282 * Case 1: there is no overlap between the previous 283 * segment and this one. Apply protections to the 284 * previous segment, and protect the gap between the 285 * previous and current segments, if any. 286 */ 287 link_elf_protect_range(ef, start, end, prot); 288 link_elf_protect_range(ef, end, segstart, gapprot); 289 290 start = segstart; 291 end = segend; 292 prot = segprot; 293 } else if (start < segstart && end == segend) { 294 /* 295 * Case 2: the current segment is a subrange of the 296 * previous segment. Apply protections to the 297 * non-overlapping portion of the previous segment. 298 */ 299 link_elf_protect_range(ef, start, segstart, prot); 300 301 start = segstart; 302 prot |= segprot; 303 } else if (end < segend) { 304 /* 305 * Case 3: there is partial overlap between the previous 306 * and current segments. Apply protections to the 307 * non-overlapping portion of the previous segment, and 308 * then the overlap, which must use the union of the two 309 * segments' protections. 310 */ 311 link_elf_protect_range(ef, start, segstart, prot); 312 link_elf_protect_range(ef, segstart, end, 313 prot | segprot); 314 start = end; 315 end = segend; 316 prot = segprot; 317 } else { 318 /* 319 * Case 4: the two segments reside in the same page. 320 */ 321 prot |= segprot; 322 } 323 } 324 325 /* 326 * Fix up the last unprotected segment and trailing data. 327 */ 328 link_elf_protect_range(ef, start, end, prot); 329 link_elf_protect_range(ef, end, 330 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot); 331 } 332 333 static int 334 link_elf_link_preload(linker_class_t cls, const char *filename, 335 linker_file_t *result) 336 { 337 Elf_Ehdr *hdr; 338 Elf_Shdr *shdr; 339 Elf_Sym *es; 340 void *modptr, *baseptr, *sizeptr; 341 char *type; 342 elf_file_t ef; 343 linker_file_t lf; 344 Elf_Addr off; 345 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex; 346 347 /* Look to see if we have the file preloaded */ 348 modptr = preload_search_by_name(filename); 349 if (modptr == NULL) 350 return ENOENT; 351 352 type = (char *)preload_search_info(modptr, MODINFO_TYPE); 353 baseptr = preload_search_info(modptr, MODINFO_ADDR); 354 sizeptr = preload_search_info(modptr, MODINFO_SIZE); 355 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA | 356 MODINFOMD_ELFHDR); 357 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA | 358 MODINFOMD_SHDR); 359 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) 360 " obj module") != 0 && 361 strcmp(type, "elf obj module") != 0)) { 362 return (EFTYPE); 363 } 364 if (baseptr == NULL || sizeptr == NULL || hdr == NULL || 365 shdr == NULL) 366 return (EINVAL); 367 368 lf = linker_make_file(filename, &link_elf_class); 369 if (lf == NULL) 370 return (ENOMEM); 371 372 ef = (elf_file_t)lf; 373 ef->preloaded = 1; 374 ef->address = *(caddr_t *)baseptr; 375 lf->address = *(caddr_t *)baseptr; 376 lf->size = *(size_t *)sizeptr; 377 378 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 379 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 380 hdr->e_ident[EI_VERSION] != EV_CURRENT || 381 hdr->e_version != EV_CURRENT || 382 hdr->e_type != ET_REL || 383 hdr->e_machine != ELF_TARG_MACH) { 384 error = EFTYPE; 385 goto out; 386 } 387 ef->e_shdr = shdr; 388 389 /* Scan the section header for information and table sizing. */ 390 symtabindex = -1; 391 symstrindex = -1; 392 for (i = 0; i < hdr->e_shnum; i++) { 393 switch (shdr[i].sh_type) { 394 case SHT_PROGBITS: 395 case SHT_NOBITS: 396 #ifdef __amd64__ 397 case SHT_X86_64_UNWIND: 398 #endif 399 case SHT_INIT_ARRAY: 400 case SHT_FINI_ARRAY: 401 /* Ignore sections not loaded by the loader. */ 402 if (shdr[i].sh_addr == 0) 403 break; 404 ef->nprogtab++; 405 break; 406 case SHT_SYMTAB: 407 symtabindex = i; 408 symstrindex = shdr[i].sh_link; 409 break; 410 case SHT_REL: 411 /* 412 * Ignore relocation tables for sections not 413 * loaded by the loader. 414 */ 415 if (shdr[shdr[i].sh_info].sh_addr == 0) 416 break; 417 ef->nreltab++; 418 break; 419 case SHT_RELA: 420 if (shdr[shdr[i].sh_info].sh_addr == 0) 421 break; 422 ef->nrelatab++; 423 break; 424 } 425 } 426 427 shstrindex = hdr->e_shstrndx; 428 if (ef->nprogtab == 0 || symstrindex < 0 || 429 symstrindex >= hdr->e_shnum || 430 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 || 431 shstrindex >= hdr->e_shnum || 432 shdr[shstrindex].sh_type != SHT_STRTAB) { 433 printf("%s: bad/missing section headers\n", filename); 434 error = ENOEXEC; 435 goto out; 436 } 437 438 /* Allocate space for tracking the load chunks */ 439 if (ef->nprogtab != 0) 440 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab), 441 M_LINKER, M_WAITOK | M_ZERO); 442 if (ef->nreltab != 0) 443 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab), 444 M_LINKER, M_WAITOK | M_ZERO); 445 if (ef->nrelatab != 0) 446 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab), 447 M_LINKER, M_WAITOK | M_ZERO); 448 if ((ef->nprogtab != 0 && ef->progtab == NULL) || 449 (ef->nreltab != 0 && ef->reltab == NULL) || 450 (ef->nrelatab != 0 && ef->relatab == NULL)) { 451 error = ENOMEM; 452 goto out; 453 } 454 455 /* XXX, relocate the sh_addr fields saved by the loader. */ 456 off = 0; 457 for (i = 0; i < hdr->e_shnum; i++) { 458 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off)) 459 off = shdr[i].sh_addr; 460 } 461 for (i = 0; i < hdr->e_shnum; i++) { 462 if (shdr[i].sh_addr != 0) 463 shdr[i].sh_addr = shdr[i].sh_addr - off + 464 (Elf_Addr)ef->address; 465 } 466 467 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 468 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr; 469 ef->ddbstrcnt = shdr[symstrindex].sh_size; 470 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr; 471 ef->shstrcnt = shdr[shstrindex].sh_size; 472 ef->shstrtab = (char *)shdr[shstrindex].sh_addr; 473 474 /* Now fill out progtab and the relocation tables. */ 475 pb = 0; 476 rl = 0; 477 ra = 0; 478 for (i = 0; i < hdr->e_shnum; i++) { 479 switch (shdr[i].sh_type) { 480 case SHT_PROGBITS: 481 case SHT_NOBITS: 482 #ifdef __amd64__ 483 case SHT_X86_64_UNWIND: 484 #endif 485 case SHT_INIT_ARRAY: 486 case SHT_FINI_ARRAY: 487 if (shdr[i].sh_addr == 0) 488 break; 489 ef->progtab[pb].addr = (void *)shdr[i].sh_addr; 490 if (shdr[i].sh_type == SHT_PROGBITS) 491 ef->progtab[pb].name = "<<PROGBITS>>"; 492 #ifdef __amd64__ 493 else if (shdr[i].sh_type == SHT_X86_64_UNWIND) 494 ef->progtab[pb].name = "<<UNWIND>>"; 495 #endif 496 else if (shdr[i].sh_type == SHT_INIT_ARRAY) 497 ef->progtab[pb].name = "<<INIT_ARRAY>>"; 498 else if (shdr[i].sh_type == SHT_FINI_ARRAY) 499 ef->progtab[pb].name = "<<FINI_ARRAY>>"; 500 else 501 ef->progtab[pb].name = "<<NOBITS>>"; 502 ef->progtab[pb].size = shdr[i].sh_size; 503 ef->progtab[pb].flags = shdr[i].sh_flags; 504 ef->progtab[pb].sec = i; 505 if (ef->shstrtab && shdr[i].sh_name != 0) 506 ef->progtab[pb].name = 507 ef->shstrtab + shdr[i].sh_name; 508 if (ef->progtab[pb].name != NULL && 509 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) { 510 void *dpcpu; 511 512 dpcpu = dpcpu_alloc(shdr[i].sh_size); 513 if (dpcpu == NULL) { 514 printf("%s: pcpu module space is out " 515 "of space; cannot allocate %#jx " 516 "for %s\n", __func__, 517 (uintmax_t)shdr[i].sh_size, 518 filename); 519 error = ENOSPC; 520 goto out; 521 } 522 memcpy(dpcpu, ef->progtab[pb].addr, 523 ef->progtab[pb].size); 524 dpcpu_copy(dpcpu, shdr[i].sh_size); 525 ef->progtab[pb].addr = dpcpu; 526 #ifdef VIMAGE 527 } else if (ef->progtab[pb].name != NULL && 528 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) { 529 void *vnet_data; 530 531 vnet_data = vnet_data_alloc(shdr[i].sh_size); 532 if (vnet_data == NULL) { 533 printf("%s: vnet module space is out " 534 "of space; cannot allocate %#jx " 535 "for %s\n", __func__, 536 (uintmax_t)shdr[i].sh_size, 537 filename); 538 error = ENOSPC; 539 goto out; 540 } 541 memcpy(vnet_data, ef->progtab[pb].addr, 542 ef->progtab[pb].size); 543 vnet_data_copy(vnet_data, shdr[i].sh_size); 544 ef->progtab[pb].addr = vnet_data; 545 #endif 546 } else if ((ef->progtab[pb].name != NULL && 547 strcmp(ef->progtab[pb].name, ".ctors") == 0) || 548 shdr[i].sh_type == SHT_INIT_ARRAY) { 549 if (lf->ctors_addr != 0) { 550 printf( 551 "%s: multiple ctor sections in %s\n", 552 __func__, filename); 553 } else { 554 lf->ctors_addr = ef->progtab[pb].addr; 555 lf->ctors_size = shdr[i].sh_size; 556 } 557 } else if ((ef->progtab[pb].name != NULL && 558 strcmp(ef->progtab[pb].name, ".dtors") == 0) || 559 shdr[i].sh_type == SHT_FINI_ARRAY) { 560 if (lf->dtors_addr != 0) { 561 printf( 562 "%s: multiple dtor sections in %s\n", 563 __func__, filename); 564 } else { 565 lf->dtors_addr = ef->progtab[pb].addr; 566 lf->dtors_size = shdr[i].sh_size; 567 } 568 } 569 570 /* Update all symbol values with the offset. */ 571 for (j = 0; j < ef->ddbsymcnt; j++) { 572 es = &ef->ddbsymtab[j]; 573 if (es->st_shndx != i) 574 continue; 575 es->st_value += (Elf_Addr)ef->progtab[pb].addr; 576 } 577 pb++; 578 break; 579 case SHT_REL: 580 if (shdr[shdr[i].sh_info].sh_addr == 0) 581 break; 582 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr; 583 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); 584 ef->reltab[rl].sec = shdr[i].sh_info; 585 rl++; 586 break; 587 case SHT_RELA: 588 if (shdr[shdr[i].sh_info].sh_addr == 0) 589 break; 590 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr; 591 ef->relatab[ra].nrela = 592 shdr[i].sh_size / sizeof(Elf_Rela); 593 ef->relatab[ra].sec = shdr[i].sh_info; 594 ra++; 595 break; 596 } 597 } 598 if (pb != ef->nprogtab) { 599 printf("%s: lost progbits\n", filename); 600 error = ENOEXEC; 601 goto out; 602 } 603 if (rl != ef->nreltab) { 604 printf("%s: lost reltab\n", filename); 605 error = ENOEXEC; 606 goto out; 607 } 608 if (ra != ef->nrelatab) { 609 printf("%s: lost relatab\n", filename); 610 error = ENOEXEC; 611 goto out; 612 } 613 614 /* 615 * The file needs to be writeable and executable while applying 616 * relocations. Mapping protections are applied once relocation 617 * processing is complete. 618 */ 619 link_elf_protect_range(ef, (vm_offset_t)ef->address, 620 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL); 621 622 /* Local intra-module relocations */ 623 error = link_elf_reloc_local(lf, false); 624 if (error != 0) 625 goto out; 626 *result = lf; 627 return (0); 628 629 out: 630 /* preload not done this way */ 631 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 632 return (error); 633 } 634 635 static void 636 link_elf_invoke_cbs(caddr_t addr, size_t size) 637 { 638 void (**ctor)(void); 639 size_t i, cnt; 640 641 if (addr == NULL || size == 0) 642 return; 643 cnt = size / sizeof(*ctor); 644 ctor = (void *)addr; 645 for (i = 0; i < cnt; i++) { 646 if (ctor[i] != NULL) 647 (*ctor[i])(); 648 } 649 } 650 651 static void 652 link_elf_invoke_ctors(linker_file_t lf) 653 { 654 KASSERT(lf->ctors_invoked == LF_NONE, 655 ("%s: file %s ctor state %d", 656 __func__, lf->filename, lf->ctors_invoked)); 657 658 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size); 659 lf->ctors_invoked = LF_CTORS; 660 } 661 662 static void 663 link_elf_invoke_dtors(linker_file_t lf) 664 { 665 KASSERT(lf->ctors_invoked != LF_DTORS, 666 ("%s: file %s ctor state %d", 667 __func__, lf->filename, lf->ctors_invoked)); 668 669 if (lf->ctors_invoked == LF_CTORS) { 670 link_elf_invoke_cbs(lf->dtors_addr, lf->dtors_size); 671 lf->ctors_invoked = LF_DTORS; 672 } 673 } 674 675 static int 676 link_elf_link_preload_finish(linker_file_t lf) 677 { 678 elf_file_t ef; 679 int error; 680 681 ef = (elf_file_t)lf; 682 error = relocate_file(ef); 683 if (error) 684 return (error); 685 686 /* Notify MD code that a module is being loaded. */ 687 error = elf_cpu_load_file(lf); 688 if (error) 689 return (error); 690 691 #if defined(__i386__) || defined(__amd64__) 692 /* Now ifuncs. */ 693 error = link_elf_reloc_local(lf, true); 694 if (error != 0) 695 return (error); 696 #endif 697 698 /* Apply protections now that relocation processing is complete. */ 699 link_elf_protect(ef); 700 701 link_elf_invoke_ctors(lf); 702 return (0); 703 } 704 705 static int 706 link_elf_load_file(linker_class_t cls, const char *filename, 707 linker_file_t *result) 708 { 709 struct nameidata *nd; 710 struct thread *td = curthread; /* XXX */ 711 Elf_Ehdr *hdr; 712 Elf_Shdr *shdr; 713 Elf_Sym *es; 714 int nbytes, i, j; 715 vm_offset_t mapbase; 716 size_t mapsize; 717 int error = 0; 718 ssize_t resid; 719 int flags; 720 elf_file_t ef; 721 linker_file_t lf; 722 int symtabindex; 723 int symstrindex; 724 int shstrindex; 725 int nsym; 726 int pb, rl, ra; 727 int alignmask; 728 729 shdr = NULL; 730 lf = NULL; 731 mapsize = 0; 732 hdr = NULL; 733 734 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK); 735 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename); 736 flags = FREAD; 737 error = vn_open(nd, &flags, 0, NULL); 738 if (error) { 739 free(nd, M_TEMP); 740 return error; 741 } 742 NDFREE_PNBUF(nd); 743 if (nd->ni_vp->v_type != VREG) { 744 error = ENOEXEC; 745 goto out; 746 } 747 #ifdef MAC 748 error = mac_kld_check_load(td->td_ucred, nd->ni_vp); 749 if (error) { 750 goto out; 751 } 752 #endif 753 754 /* Read the elf header from the file. */ 755 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK); 756 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0, 757 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 758 &resid, td); 759 if (error) 760 goto out; 761 if (resid != 0){ 762 error = ENOEXEC; 763 goto out; 764 } 765 766 if (!IS_ELF(*hdr)) { 767 error = ENOEXEC; 768 goto out; 769 } 770 771 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS 772 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) { 773 link_elf_error(filename, "Unsupported file layout"); 774 error = ENOEXEC; 775 goto out; 776 } 777 if (hdr->e_ident[EI_VERSION] != EV_CURRENT 778 || hdr->e_version != EV_CURRENT) { 779 link_elf_error(filename, "Unsupported file version"); 780 error = ENOEXEC; 781 goto out; 782 } 783 if (hdr->e_type != ET_REL) { 784 error = ENOSYS; 785 goto out; 786 } 787 if (hdr->e_machine != ELF_TARG_MACH) { 788 link_elf_error(filename, "Unsupported machine"); 789 error = ENOEXEC; 790 goto out; 791 } 792 793 lf = linker_make_file(filename, &link_elf_class); 794 if (!lf) { 795 error = ENOMEM; 796 goto out; 797 } 798 ef = (elf_file_t) lf; 799 ef->nprogtab = 0; 800 ef->e_shdr = 0; 801 ef->nreltab = 0; 802 ef->nrelatab = 0; 803 804 /* Allocate and read in the section header */ 805 nbytes = hdr->e_shnum * hdr->e_shentsize; 806 if (nbytes == 0 || hdr->e_shoff == 0 || 807 hdr->e_shentsize != sizeof(Elf_Shdr)) { 808 error = ENOEXEC; 809 goto out; 810 } 811 shdr = malloc(nbytes, M_LINKER, M_WAITOK); 812 ef->e_shdr = shdr; 813 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes, 814 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 815 NOCRED, &resid, td); 816 if (error) 817 goto out; 818 if (resid) { 819 error = ENOEXEC; 820 goto out; 821 } 822 823 /* Scan the section header for information and table sizing. */ 824 nsym = 0; 825 symtabindex = -1; 826 symstrindex = -1; 827 for (i = 0; i < hdr->e_shnum; i++) { 828 if (shdr[i].sh_size == 0) 829 continue; 830 switch (shdr[i].sh_type) { 831 case SHT_PROGBITS: 832 case SHT_NOBITS: 833 #ifdef __amd64__ 834 case SHT_X86_64_UNWIND: 835 #endif 836 case SHT_INIT_ARRAY: 837 case SHT_FINI_ARRAY: 838 if ((shdr[i].sh_flags & SHF_ALLOC) == 0) 839 break; 840 ef->nprogtab++; 841 break; 842 case SHT_SYMTAB: 843 nsym++; 844 symtabindex = i; 845 symstrindex = shdr[i].sh_link; 846 break; 847 case SHT_REL: 848 /* 849 * Ignore relocation tables for unallocated 850 * sections. 851 */ 852 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0) 853 break; 854 ef->nreltab++; 855 break; 856 case SHT_RELA: 857 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0) 858 break; 859 ef->nrelatab++; 860 break; 861 case SHT_STRTAB: 862 break; 863 } 864 } 865 if (ef->nprogtab == 0) { 866 link_elf_error(filename, "file has no contents"); 867 error = ENOEXEC; 868 goto out; 869 } 870 if (nsym != 1) { 871 /* Only allow one symbol table for now */ 872 link_elf_error(filename, 873 "file must have exactly one symbol table"); 874 error = ENOEXEC; 875 goto out; 876 } 877 if (symstrindex < 0 || symstrindex > hdr->e_shnum || 878 shdr[symstrindex].sh_type != SHT_STRTAB) { 879 link_elf_error(filename, "file has invalid symbol strings"); 880 error = ENOEXEC; 881 goto out; 882 } 883 884 /* Allocate space for tracking the load chunks */ 885 if (ef->nprogtab != 0) 886 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab), 887 M_LINKER, M_WAITOK | M_ZERO); 888 if (ef->nreltab != 0) 889 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab), 890 M_LINKER, M_WAITOK | M_ZERO); 891 if (ef->nrelatab != 0) 892 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab), 893 M_LINKER, M_WAITOK | M_ZERO); 894 895 if (symtabindex == -1) { 896 link_elf_error(filename, "lost symbol table index"); 897 error = ENOEXEC; 898 goto out; 899 } 900 /* Allocate space for and load the symbol table */ 901 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 902 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK); 903 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab, 904 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset, 905 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 906 &resid, td); 907 if (error) 908 goto out; 909 if (resid != 0){ 910 error = EINVAL; 911 goto out; 912 } 913 914 /* Allocate space for and load the symbol strings */ 915 ef->ddbstrcnt = shdr[symstrindex].sh_size; 916 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK); 917 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab, 918 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset, 919 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 920 &resid, td); 921 if (error) 922 goto out; 923 if (resid != 0){ 924 error = EINVAL; 925 goto out; 926 } 927 928 /* Do we have a string table for the section names? */ 929 shstrindex = -1; 930 if (hdr->e_shstrndx != 0 && 931 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) { 932 shstrindex = hdr->e_shstrndx; 933 ef->shstrcnt = shdr[shstrindex].sh_size; 934 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER, 935 M_WAITOK); 936 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab, 937 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset, 938 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 939 &resid, td); 940 if (error) 941 goto out; 942 if (resid != 0){ 943 error = EINVAL; 944 goto out; 945 } 946 } 947 948 /* Size up code/data(progbits) and bss(nobits). */ 949 alignmask = 0; 950 for (i = 0; i < hdr->e_shnum; i++) { 951 if (shdr[i].sh_size == 0) 952 continue; 953 switch (shdr[i].sh_type) { 954 case SHT_PROGBITS: 955 case SHT_NOBITS: 956 #ifdef __amd64__ 957 case SHT_X86_64_UNWIND: 958 #endif 959 case SHT_INIT_ARRAY: 960 case SHT_FINI_ARRAY: 961 if ((shdr[i].sh_flags & SHF_ALLOC) == 0) 962 break; 963 alignmask = shdr[i].sh_addralign - 1; 964 mapsize += alignmask; 965 mapsize &= ~alignmask; 966 mapsize += shdr[i].sh_size; 967 break; 968 } 969 } 970 971 /* 972 * We know how much space we need for the text/data/bss/etc. 973 * This stuff needs to be in a single chunk so that profiling etc 974 * can get the bounds and gdb can associate offsets with modules 975 */ 976 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize), 977 VM_PROT_ALL, 0, thread0.td_ucred); 978 if (ef->object == NULL) { 979 error = ENOMEM; 980 goto out; 981 } 982 #if VM_NRESERVLEVEL > 0 983 vm_object_color(ef->object, 0); 984 #endif 985 986 /* 987 * In order to satisfy amd64's architectural requirements on the 988 * location of code and data in the kernel's address space, request a 989 * mapping that is above the kernel. 990 * 991 * Protections will be restricted once relocations are applied. 992 */ 993 #ifdef __amd64__ 994 mapbase = KERNBASE; 995 #else 996 mapbase = VM_MIN_KERNEL_ADDRESS; 997 #endif 998 error = vm_map_find(kernel_map, ef->object, 0, &mapbase, 999 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL, 1000 VM_PROT_ALL, 0); 1001 if (error != KERN_SUCCESS) { 1002 vm_object_deallocate(ef->object); 1003 ef->object = NULL; 1004 error = ENOMEM; 1005 goto out; 1006 } 1007 1008 /* Wire the pages */ 1009 error = vm_map_wire(kernel_map, mapbase, 1010 mapbase + round_page(mapsize), 1011 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); 1012 if (error != KERN_SUCCESS) { 1013 error = ENOMEM; 1014 goto out; 1015 } 1016 1017 /* Inform the kld system about the situation */ 1018 lf->address = ef->address = (caddr_t)mapbase; 1019 lf->size = mapsize; 1020 1021 /* 1022 * Now load code/data(progbits), zero bss(nobits), allocate space for 1023 * and load relocs 1024 */ 1025 pb = 0; 1026 rl = 0; 1027 ra = 0; 1028 alignmask = 0; 1029 for (i = 0; i < hdr->e_shnum; i++) { 1030 if (shdr[i].sh_size == 0) 1031 continue; 1032 switch (shdr[i].sh_type) { 1033 case SHT_PROGBITS: 1034 case SHT_NOBITS: 1035 #ifdef __amd64__ 1036 case SHT_X86_64_UNWIND: 1037 #endif 1038 case SHT_INIT_ARRAY: 1039 case SHT_FINI_ARRAY: 1040 if ((shdr[i].sh_flags & SHF_ALLOC) == 0) 1041 break; 1042 alignmask = shdr[i].sh_addralign - 1; 1043 mapbase += alignmask; 1044 mapbase &= ~alignmask; 1045 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) { 1046 ef->progtab[pb].name = 1047 ef->shstrtab + shdr[i].sh_name; 1048 if (!strcmp(ef->progtab[pb].name, ".ctors") || 1049 shdr[i].sh_type == SHT_INIT_ARRAY) { 1050 if (lf->ctors_addr != 0) { 1051 printf( 1052 "%s: multiple ctor sections in %s\n", 1053 __func__, filename); 1054 } else { 1055 lf->ctors_addr = 1056 (caddr_t)mapbase; 1057 lf->ctors_size = 1058 shdr[i].sh_size; 1059 } 1060 } else if (!strcmp(ef->progtab[pb].name, 1061 ".dtors") || 1062 shdr[i].sh_type == SHT_FINI_ARRAY) { 1063 if (lf->dtors_addr != 0) { 1064 printf( 1065 "%s: multiple dtor sections in %s\n", 1066 __func__, filename); 1067 } else { 1068 lf->dtors_addr = 1069 (caddr_t)mapbase; 1070 lf->dtors_size = 1071 shdr[i].sh_size; 1072 } 1073 } 1074 } else if (shdr[i].sh_type == SHT_PROGBITS) 1075 ef->progtab[pb].name = "<<PROGBITS>>"; 1076 #ifdef __amd64__ 1077 else if (shdr[i].sh_type == SHT_X86_64_UNWIND) 1078 ef->progtab[pb].name = "<<UNWIND>>"; 1079 #endif 1080 else 1081 ef->progtab[pb].name = "<<NOBITS>>"; 1082 if (ef->progtab[pb].name != NULL && 1083 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) { 1084 ef->progtab[pb].addr = 1085 dpcpu_alloc(shdr[i].sh_size); 1086 if (ef->progtab[pb].addr == NULL) { 1087 printf("%s: pcpu module space is out " 1088 "of space; cannot allocate %#jx " 1089 "for %s\n", __func__, 1090 (uintmax_t)shdr[i].sh_size, 1091 filename); 1092 } 1093 } 1094 #ifdef VIMAGE 1095 else if (ef->progtab[pb].name != NULL && 1096 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) { 1097 ef->progtab[pb].addr = 1098 vnet_data_alloc(shdr[i].sh_size); 1099 if (ef->progtab[pb].addr == NULL) { 1100 printf("%s: vnet module space is out " 1101 "of space; cannot allocate %#jx " 1102 "for %s\n", __func__, 1103 (uintmax_t)shdr[i].sh_size, 1104 filename); 1105 } 1106 } 1107 #endif 1108 else 1109 ef->progtab[pb].addr = 1110 (void *)(uintptr_t)mapbase; 1111 if (ef->progtab[pb].addr == NULL) { 1112 error = ENOSPC; 1113 goto out; 1114 } 1115 ef->progtab[pb].size = shdr[i].sh_size; 1116 ef->progtab[pb].flags = shdr[i].sh_flags; 1117 ef->progtab[pb].sec = i; 1118 if (shdr[i].sh_type == SHT_PROGBITS 1119 #ifdef __amd64__ 1120 || shdr[i].sh_type == SHT_X86_64_UNWIND 1121 #endif 1122 ) { 1123 error = vn_rdwr(UIO_READ, nd->ni_vp, 1124 ef->progtab[pb].addr, 1125 shdr[i].sh_size, shdr[i].sh_offset, 1126 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 1127 NOCRED, &resid, td); 1128 if (error) 1129 goto out; 1130 if (resid != 0){ 1131 error = EINVAL; 1132 goto out; 1133 } 1134 /* Initialize the per-cpu or vnet area. */ 1135 if (ef->progtab[pb].addr != (void *)mapbase && 1136 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) 1137 dpcpu_copy(ef->progtab[pb].addr, 1138 shdr[i].sh_size); 1139 #ifdef VIMAGE 1140 else if (ef->progtab[pb].addr != 1141 (void *)mapbase && 1142 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) 1143 vnet_data_copy(ef->progtab[pb].addr, 1144 shdr[i].sh_size); 1145 #endif 1146 } else 1147 bzero(ef->progtab[pb].addr, shdr[i].sh_size); 1148 1149 /* Update all symbol values with the offset. */ 1150 for (j = 0; j < ef->ddbsymcnt; j++) { 1151 es = &ef->ddbsymtab[j]; 1152 if (es->st_shndx != i) 1153 continue; 1154 es->st_value += (Elf_Addr)ef->progtab[pb].addr; 1155 } 1156 mapbase += shdr[i].sh_size; 1157 pb++; 1158 break; 1159 case SHT_REL: 1160 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0) 1161 break; 1162 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER, 1163 M_WAITOK); 1164 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); 1165 ef->reltab[rl].sec = shdr[i].sh_info; 1166 error = vn_rdwr(UIO_READ, nd->ni_vp, 1167 (void *)ef->reltab[rl].rel, 1168 shdr[i].sh_size, shdr[i].sh_offset, 1169 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 1170 &resid, td); 1171 if (error) 1172 goto out; 1173 if (resid != 0){ 1174 error = EINVAL; 1175 goto out; 1176 } 1177 rl++; 1178 break; 1179 case SHT_RELA: 1180 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0) 1181 break; 1182 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER, 1183 M_WAITOK); 1184 ef->relatab[ra].nrela = 1185 shdr[i].sh_size / sizeof(Elf_Rela); 1186 ef->relatab[ra].sec = shdr[i].sh_info; 1187 error = vn_rdwr(UIO_READ, nd->ni_vp, 1188 (void *)ef->relatab[ra].rela, 1189 shdr[i].sh_size, shdr[i].sh_offset, 1190 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 1191 &resid, td); 1192 if (error) 1193 goto out; 1194 if (resid != 0){ 1195 error = EINVAL; 1196 goto out; 1197 } 1198 ra++; 1199 break; 1200 } 1201 } 1202 if (pb != ef->nprogtab) { 1203 link_elf_error(filename, "lost progbits"); 1204 error = ENOEXEC; 1205 goto out; 1206 } 1207 if (rl != ef->nreltab) { 1208 link_elf_error(filename, "lost reltab"); 1209 error = ENOEXEC; 1210 goto out; 1211 } 1212 if (ra != ef->nrelatab) { 1213 link_elf_error(filename, "lost relatab"); 1214 error = ENOEXEC; 1215 goto out; 1216 } 1217 if (mapbase != (vm_offset_t)ef->address + mapsize) { 1218 printf( 1219 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n", 1220 filename != NULL ? filename : "<none>", 1221 (u_long)mapbase, ef->address, (u_long)mapsize, 1222 (u_long)(vm_offset_t)ef->address + mapsize); 1223 error = ENOMEM; 1224 goto out; 1225 } 1226 1227 /* Local intra-module relocations */ 1228 error = link_elf_reloc_local(lf, false); 1229 if (error != 0) 1230 goto out; 1231 1232 /* Pull in dependencies */ 1233 VOP_UNLOCK(nd->ni_vp); 1234 error = linker_load_dependencies(lf); 1235 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY); 1236 if (error) 1237 goto out; 1238 1239 /* External relocations */ 1240 error = relocate_file(ef); 1241 if (error) 1242 goto out; 1243 1244 /* Notify MD code that a module is being loaded. */ 1245 error = elf_cpu_load_file(lf); 1246 if (error) 1247 goto out; 1248 1249 #if defined(__i386__) || defined(__amd64__) 1250 /* Now ifuncs. */ 1251 error = link_elf_reloc_local(lf, true); 1252 if (error != 0) 1253 goto out; 1254 #endif 1255 1256 link_elf_protect(ef); 1257 link_elf_invoke_ctors(lf); 1258 *result = lf; 1259 1260 out: 1261 VOP_UNLOCK(nd->ni_vp); 1262 vn_close(nd->ni_vp, FREAD, td->td_ucred, td); 1263 free(nd, M_TEMP); 1264 if (error && lf) 1265 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1266 free(hdr, M_LINKER); 1267 1268 return error; 1269 } 1270 1271 static void 1272 link_elf_unload_file(linker_file_t file) 1273 { 1274 elf_file_t ef = (elf_file_t) file; 1275 u_int i; 1276 1277 link_elf_invoke_dtors(file); 1278 1279 /* Notify MD code that a module is being unloaded. */ 1280 elf_cpu_unload_file(file); 1281 1282 if (ef->progtab) { 1283 for (i = 0; i < ef->nprogtab; i++) { 1284 if (ef->progtab[i].size == 0) 1285 continue; 1286 if (ef->progtab[i].name == NULL) 1287 continue; 1288 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME)) 1289 dpcpu_free(ef->progtab[i].addr, 1290 ef->progtab[i].size); 1291 #ifdef VIMAGE 1292 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME)) 1293 vnet_data_free(ef->progtab[i].addr, 1294 ef->progtab[i].size); 1295 #endif 1296 } 1297 } 1298 if (ef->preloaded) { 1299 free(ef->reltab, M_LINKER); 1300 free(ef->relatab, M_LINKER); 1301 free(ef->progtab, M_LINKER); 1302 free(ef->ctftab, M_LINKER); 1303 free(ef->ctfoff, M_LINKER); 1304 free(ef->typoff, M_LINKER); 1305 if (file->pathname != NULL) 1306 preload_delete_name(file->pathname); 1307 return; 1308 } 1309 1310 for (i = 0; i < ef->nreltab; i++) 1311 free(ef->reltab[i].rel, M_LINKER); 1312 for (i = 0; i < ef->nrelatab; i++) 1313 free(ef->relatab[i].rela, M_LINKER); 1314 free(ef->reltab, M_LINKER); 1315 free(ef->relatab, M_LINKER); 1316 free(ef->progtab, M_LINKER); 1317 1318 if (ef->object != NULL) 1319 vm_map_remove(kernel_map, (vm_offset_t)ef->address, 1320 (vm_offset_t)ef->address + ptoa(ef->object->size)); 1321 free(ef->e_shdr, M_LINKER); 1322 free(ef->ddbsymtab, M_LINKER); 1323 free(ef->ddbstrtab, M_LINKER); 1324 free(ef->shstrtab, M_LINKER); 1325 free(ef->ctftab, M_LINKER); 1326 free(ef->ctfoff, M_LINKER); 1327 free(ef->typoff, M_LINKER); 1328 } 1329 1330 static const char * 1331 symbol_name(elf_file_t ef, Elf_Size r_info) 1332 { 1333 const Elf_Sym *ref; 1334 1335 if (ELF_R_SYM(r_info)) { 1336 ref = ef->ddbsymtab + ELF_R_SYM(r_info); 1337 return ef->ddbstrtab + ref->st_name; 1338 } else 1339 return NULL; 1340 } 1341 1342 static Elf_Addr 1343 findbase(elf_file_t ef, int sec) 1344 { 1345 int i; 1346 Elf_Addr base = 0; 1347 1348 for (i = 0; i < ef->nprogtab; i++) { 1349 if (sec == ef->progtab[i].sec) { 1350 base = (Elf_Addr)ef->progtab[i].addr; 1351 break; 1352 } 1353 } 1354 return base; 1355 } 1356 1357 static int 1358 relocate_file1(elf_file_t ef, bool ifuncs) 1359 { 1360 const Elf_Rel *rellim; 1361 const Elf_Rel *rel; 1362 const Elf_Rela *relalim; 1363 const Elf_Rela *rela; 1364 const char *symname; 1365 const Elf_Sym *sym; 1366 int i; 1367 Elf_Size symidx; 1368 Elf_Addr base; 1369 1370 /* Perform relocations without addend if there are any: */ 1371 for (i = 0; i < ef->nreltab; i++) { 1372 rel = ef->reltab[i].rel; 1373 if (rel == NULL) { 1374 link_elf_error(ef->lf.filename, "lost a reltab!"); 1375 return (ENOEXEC); 1376 } 1377 rellim = rel + ef->reltab[i].nrel; 1378 base = findbase(ef, ef->reltab[i].sec); 1379 if (base == 0) { 1380 link_elf_error(ef->lf.filename, "lost base for reltab"); 1381 return (ENOEXEC); 1382 } 1383 for ( ; rel < rellim; rel++) { 1384 symidx = ELF_R_SYM(rel->r_info); 1385 if (symidx >= ef->ddbsymcnt) 1386 continue; 1387 sym = ef->ddbsymtab + symidx; 1388 /* Local relocs are already done */ 1389 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 1390 continue; 1391 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC || 1392 elf_is_ifunc_reloc(rel->r_info)) != ifuncs) 1393 continue; 1394 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL, 1395 elf_obj_lookup)) { 1396 symname = symbol_name(ef, rel->r_info); 1397 printf("link_elf_obj: symbol %s undefined\n", 1398 symname); 1399 return (ENOENT); 1400 } 1401 } 1402 } 1403 1404 /* Perform relocations with addend if there are any: */ 1405 for (i = 0; i < ef->nrelatab; i++) { 1406 rela = ef->relatab[i].rela; 1407 if (rela == NULL) { 1408 link_elf_error(ef->lf.filename, "lost a relatab!"); 1409 return (ENOEXEC); 1410 } 1411 relalim = rela + ef->relatab[i].nrela; 1412 base = findbase(ef, ef->relatab[i].sec); 1413 if (base == 0) { 1414 link_elf_error(ef->lf.filename, 1415 "lost base for relatab"); 1416 return (ENOEXEC); 1417 } 1418 for ( ; rela < relalim; rela++) { 1419 symidx = ELF_R_SYM(rela->r_info); 1420 if (symidx >= ef->ddbsymcnt) 1421 continue; 1422 sym = ef->ddbsymtab + symidx; 1423 /* Local relocs are already done */ 1424 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 1425 continue; 1426 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC || 1427 elf_is_ifunc_reloc(rela->r_info)) != ifuncs) 1428 continue; 1429 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA, 1430 elf_obj_lookup)) { 1431 symname = symbol_name(ef, rela->r_info); 1432 printf("link_elf_obj: symbol %s undefined\n", 1433 symname); 1434 return (ENOENT); 1435 } 1436 } 1437 } 1438 1439 /* 1440 * Only clean SHN_FBSD_CACHED for successful return. If we 1441 * modified symbol table for the object but found an 1442 * unresolved symbol, there is no reason to roll back. 1443 */ 1444 elf_obj_cleanup_globals_cache(ef); 1445 1446 return (0); 1447 } 1448 1449 static int 1450 relocate_file(elf_file_t ef) 1451 { 1452 int error; 1453 1454 error = relocate_file1(ef, false); 1455 if (error == 0) 1456 error = relocate_file1(ef, true); 1457 return (error); 1458 } 1459 1460 static int 1461 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym, 1462 bool see_local) 1463 { 1464 elf_file_t ef = (elf_file_t)lf; 1465 const Elf_Sym *symp; 1466 const char *strp; 1467 int i; 1468 1469 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1470 strp = ef->ddbstrtab + symp->st_name; 1471 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) { 1472 if (see_local || 1473 ELF_ST_BIND(symp->st_info) == STB_GLOBAL) { 1474 *sym = (c_linker_sym_t) symp; 1475 return (0); 1476 } 1477 return (ENOENT); 1478 } 1479 } 1480 return (ENOENT); 1481 } 1482 1483 static int 1484 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym) 1485 { 1486 return (link_elf_lookup_symbol1(lf, name, sym, 1487 link_elf_obj_leak_locals)); 1488 } 1489 1490 static int 1491 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name, 1492 c_linker_sym_t *sym) 1493 { 1494 return (link_elf_lookup_symbol1(lf, name, sym, true)); 1495 } 1496 1497 static int 1498 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym, 1499 linker_symval_t *symval, bool see_local) 1500 { 1501 elf_file_t ef; 1502 const Elf_Sym *es; 1503 caddr_t val; 1504 1505 ef = (elf_file_t) lf; 1506 es = (const Elf_Sym*) sym; 1507 val = (caddr_t)es->st_value; 1508 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) { 1509 if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL) 1510 return (ENOENT); 1511 symval->name = ef->ddbstrtab + es->st_name; 1512 val = (caddr_t)es->st_value; 1513 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC) 1514 val = ((caddr_t (*)(void))val)(); 1515 symval->value = val; 1516 symval->size = es->st_size; 1517 return (0); 1518 } 1519 return (ENOENT); 1520 } 1521 1522 static int 1523 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, 1524 linker_symval_t *symval) 1525 { 1526 return (link_elf_symbol_values1(lf, sym, symval, 1527 link_elf_obj_leak_locals)); 1528 } 1529 1530 static int 1531 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym, 1532 linker_symval_t *symval) 1533 { 1534 return (link_elf_symbol_values1(lf, sym, symval, true)); 1535 } 1536 1537 static int 1538 link_elf_search_symbol(linker_file_t lf, caddr_t value, 1539 c_linker_sym_t *sym, long *diffp) 1540 { 1541 elf_file_t ef = (elf_file_t)lf; 1542 u_long off = (uintptr_t)(void *)value; 1543 u_long diff = off; 1544 u_long st_value; 1545 const Elf_Sym *es; 1546 const Elf_Sym *best = NULL; 1547 int i; 1548 1549 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) { 1550 if (es->st_name == 0) 1551 continue; 1552 st_value = es->st_value; 1553 if (off >= st_value) { 1554 if (off - st_value < diff) { 1555 diff = off - st_value; 1556 best = es; 1557 if (diff == 0) 1558 break; 1559 } else if (off - st_value == diff) { 1560 best = es; 1561 } 1562 } 1563 } 1564 if (best == NULL) 1565 *diffp = off; 1566 else 1567 *diffp = diff; 1568 *sym = (c_linker_sym_t) best; 1569 1570 return (0); 1571 } 1572 1573 /* 1574 * Look up a linker set on an ELF system. 1575 */ 1576 static int 1577 link_elf_lookup_set(linker_file_t lf, const char *name, 1578 void ***startp, void ***stopp, int *countp) 1579 { 1580 elf_file_t ef = (elf_file_t)lf; 1581 void **start, **stop; 1582 int i, count; 1583 1584 /* Relative to section number */ 1585 for (i = 0; i < ef->nprogtab; i++) { 1586 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) && 1587 strcmp(ef->progtab[i].name + 4, name) == 0) { 1588 start = (void **)ef->progtab[i].addr; 1589 stop = (void **)((char *)ef->progtab[i].addr + 1590 ef->progtab[i].size); 1591 count = stop - start; 1592 if (startp) 1593 *startp = start; 1594 if (stopp) 1595 *stopp = stop; 1596 if (countp) 1597 *countp = count; 1598 return (0); 1599 } 1600 } 1601 return (ESRCH); 1602 } 1603 1604 static int 1605 link_elf_each_function_name(linker_file_t file, 1606 int (*callback)(const char *, void *), void *opaque) 1607 { 1608 elf_file_t ef = (elf_file_t)file; 1609 const Elf_Sym *symp; 1610 int i, error; 1611 1612 /* Exhaustive search */ 1613 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1614 if (symp->st_value != 0 && 1615 (ELF_ST_TYPE(symp->st_info) == STT_FUNC || 1616 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) { 1617 error = callback(ef->ddbstrtab + symp->st_name, opaque); 1618 if (error) 1619 return (error); 1620 } 1621 } 1622 return (0); 1623 } 1624 1625 static int 1626 link_elf_each_function_nameval(linker_file_t file, 1627 linker_function_nameval_callback_t callback, void *opaque) 1628 { 1629 linker_symval_t symval; 1630 elf_file_t ef = (elf_file_t)file; 1631 const Elf_Sym *symp; 1632 int i, error; 1633 1634 /* Exhaustive search */ 1635 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1636 if (symp->st_value != 0 && 1637 (ELF_ST_TYPE(symp->st_info) == STT_FUNC || 1638 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) { 1639 error = link_elf_debug_symbol_values(file, 1640 (c_linker_sym_t)symp, &symval); 1641 if (error == 0) 1642 error = callback(file, i, &symval, opaque); 1643 if (error != 0) 1644 return (error); 1645 } 1646 } 1647 return (0); 1648 } 1649 1650 static void 1651 elf_obj_cleanup_globals_cache(elf_file_t ef) 1652 { 1653 Elf_Sym *sym; 1654 Elf_Size i; 1655 1656 for (i = 0; i < ef->ddbsymcnt; i++) { 1657 sym = ef->ddbsymtab + i; 1658 if (sym->st_shndx == SHN_FBSD_CACHED) { 1659 sym->st_shndx = SHN_UNDEF; 1660 sym->st_value = 0; 1661 } 1662 } 1663 } 1664 1665 /* 1666 * Symbol lookup function that can be used when the symbol index is known (ie 1667 * in relocations). It uses the symbol index instead of doing a fully fledged 1668 * hash table based lookup when such is valid. For example for local symbols. 1669 * This is not only more efficient, it's also more correct. It's not always 1670 * the case that the symbol can be found through the hash table. 1671 */ 1672 static int 1673 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res) 1674 { 1675 elf_file_t ef = (elf_file_t)lf; 1676 Elf_Sym *sym; 1677 const char *symbol; 1678 Elf_Addr res1; 1679 1680 /* Don't even try to lookup the symbol if the index is bogus. */ 1681 if (symidx >= ef->ddbsymcnt) { 1682 *res = 0; 1683 return (EINVAL); 1684 } 1685 1686 sym = ef->ddbsymtab + symidx; 1687 1688 /* Quick answer if there is a definition included. */ 1689 if (sym->st_shndx != SHN_UNDEF) { 1690 res1 = (Elf_Addr)sym->st_value; 1691 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC) 1692 res1 = ((Elf_Addr (*)(void))res1)(); 1693 *res = res1; 1694 return (0); 1695 } 1696 1697 /* If we get here, then it is undefined and needs a lookup. */ 1698 switch (ELF_ST_BIND(sym->st_info)) { 1699 case STB_LOCAL: 1700 /* Local, but undefined? huh? */ 1701 *res = 0; 1702 return (EINVAL); 1703 1704 case STB_GLOBAL: 1705 case STB_WEAK: 1706 /* Relative to Data or Function name */ 1707 symbol = ef->ddbstrtab + sym->st_name; 1708 1709 /* Force a lookup failure if the symbol name is bogus. */ 1710 if (*symbol == 0) { 1711 *res = 0; 1712 return (EINVAL); 1713 } 1714 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps); 1715 1716 /* 1717 * Cache global lookups during module relocation. The failure 1718 * case is particularly expensive for callers, who must scan 1719 * through the entire globals table doing strcmp(). Cache to 1720 * avoid doing such work repeatedly. 1721 * 1722 * After relocation is complete, undefined globals will be 1723 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(), 1724 * above. 1725 */ 1726 if (res1 != 0) { 1727 sym->st_shndx = SHN_FBSD_CACHED; 1728 sym->st_value = res1; 1729 *res = res1; 1730 return (0); 1731 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 1732 sym->st_value = 0; 1733 *res = 0; 1734 return (0); 1735 } 1736 return (EINVAL); 1737 1738 default: 1739 return (EINVAL); 1740 } 1741 } 1742 1743 static void 1744 link_elf_fix_link_set(elf_file_t ef) 1745 { 1746 static const char startn[] = "__start_"; 1747 static const char stopn[] = "__stop_"; 1748 Elf_Sym *sym; 1749 const char *sym_name, *linkset_name; 1750 Elf_Addr startp, stopp; 1751 Elf_Size symidx; 1752 int start, i; 1753 1754 startp = stopp = 0; 1755 for (symidx = 1 /* zero entry is special */; 1756 symidx < ef->ddbsymcnt; symidx++) { 1757 sym = ef->ddbsymtab + symidx; 1758 if (sym->st_shndx != SHN_UNDEF) 1759 continue; 1760 1761 sym_name = ef->ddbstrtab + sym->st_name; 1762 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) { 1763 start = 1; 1764 linkset_name = sym_name + sizeof(startn) - 1; 1765 } 1766 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) { 1767 start = 0; 1768 linkset_name = sym_name + sizeof(stopn) - 1; 1769 } 1770 else 1771 continue; 1772 1773 for (i = 0; i < ef->nprogtab; i++) { 1774 if (strcmp(ef->progtab[i].name, linkset_name) == 0) { 1775 startp = (Elf_Addr)ef->progtab[i].addr; 1776 stopp = (Elf_Addr)(startp + ef->progtab[i].size); 1777 break; 1778 } 1779 } 1780 if (i == ef->nprogtab) 1781 continue; 1782 1783 sym->st_value = start ? startp : stopp; 1784 sym->st_shndx = i; 1785 } 1786 } 1787 1788 static int 1789 link_elf_reloc_local(linker_file_t lf, bool ifuncs) 1790 { 1791 elf_file_t ef = (elf_file_t)lf; 1792 const Elf_Rel *rellim; 1793 const Elf_Rel *rel; 1794 const Elf_Rela *relalim; 1795 const Elf_Rela *rela; 1796 const Elf_Sym *sym; 1797 Elf_Addr base; 1798 int i; 1799 Elf_Size symidx; 1800 1801 link_elf_fix_link_set(ef); 1802 1803 /* Perform relocations without addend if there are any: */ 1804 for (i = 0; i < ef->nreltab; i++) { 1805 rel = ef->reltab[i].rel; 1806 if (rel == NULL) { 1807 link_elf_error(ef->lf.filename, "lost a reltab"); 1808 return (ENOEXEC); 1809 } 1810 rellim = rel + ef->reltab[i].nrel; 1811 base = findbase(ef, ef->reltab[i].sec); 1812 if (base == 0) { 1813 link_elf_error(ef->lf.filename, "lost base for reltab"); 1814 return (ENOEXEC); 1815 } 1816 for ( ; rel < rellim; rel++) { 1817 symidx = ELF_R_SYM(rel->r_info); 1818 if (symidx >= ef->ddbsymcnt) 1819 continue; 1820 sym = ef->ddbsymtab + symidx; 1821 /* Only do local relocs */ 1822 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 1823 continue; 1824 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC || 1825 elf_is_ifunc_reloc(rel->r_info)) != ifuncs) 1826 continue; 1827 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL, 1828 elf_obj_lookup) != 0) 1829 return (ENOEXEC); 1830 } 1831 } 1832 1833 /* Perform relocations with addend if there are any: */ 1834 for (i = 0; i < ef->nrelatab; i++) { 1835 rela = ef->relatab[i].rela; 1836 if (rela == NULL) { 1837 link_elf_error(ef->lf.filename, "lost a relatab!"); 1838 return (ENOEXEC); 1839 } 1840 relalim = rela + ef->relatab[i].nrela; 1841 base = findbase(ef, ef->relatab[i].sec); 1842 if (base == 0) { 1843 link_elf_error(ef->lf.filename, "lost base for reltab"); 1844 return (ENOEXEC); 1845 } 1846 for ( ; rela < relalim; rela++) { 1847 symidx = ELF_R_SYM(rela->r_info); 1848 if (symidx >= ef->ddbsymcnt) 1849 continue; 1850 sym = ef->ddbsymtab + symidx; 1851 /* Only do local relocs */ 1852 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 1853 continue; 1854 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC || 1855 elf_is_ifunc_reloc(rela->r_info)) != ifuncs) 1856 continue; 1857 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA, 1858 elf_obj_lookup) != 0) 1859 return (ENOEXEC); 1860 } 1861 } 1862 return (0); 1863 } 1864 1865 static long 1866 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab) 1867 { 1868 elf_file_t ef = (elf_file_t)lf; 1869 1870 *symtab = ef->ddbsymtab; 1871 if (*symtab == NULL) 1872 return (0); 1873 return (ef->ddbsymcnt); 1874 } 1875 1876 static long 1877 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab) 1878 { 1879 elf_file_t ef = (elf_file_t)lf; 1880 1881 *strtab = ef->ddbstrtab; 1882 if (*strtab == NULL) 1883 return (0); 1884 return (ef->ddbstrcnt); 1885 } 1886