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