1 /*- 2 * Copyright (c) 1998-2000 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_gdb.h" 31 #include "opt_mac.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #ifdef GPROF 36 #include <sys/gmon.h> 37 #endif 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/mount.h> 43 #include <sys/proc.h> 44 #include <sys/namei.h> 45 #include <sys/fcntl.h> 46 #include <sys/vnode.h> 47 #include <sys/linker.h> 48 49 #include <machine/elf.h> 50 51 #include <security/mac/mac_framework.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_param.h> 55 #ifdef SPARSE_MAPPING 56 #include <vm/vm_object.h> 57 #include <vm/vm_kern.h> 58 #include <vm/vm_extern.h> 59 #endif 60 #include <vm/pmap.h> 61 #include <vm/vm_map.h> 62 63 #include <sys/link_elf.h> 64 65 #include "linker_if.h" 66 67 #define MAXSEGS 4 68 69 typedef struct elf_file { 70 struct linker_file lf; /* Common fields */ 71 int preloaded; /* Was file pre-loaded */ 72 caddr_t address; /* Relocation address */ 73 #ifdef SPARSE_MAPPING 74 vm_object_t object; /* VM object to hold file pages */ 75 #endif 76 Elf_Dyn* dynamic; /* Symbol table etc. */ 77 Elf_Hashelt nbuckets; /* DT_HASH info */ 78 Elf_Hashelt nchains; 79 const Elf_Hashelt* buckets; 80 const Elf_Hashelt* chains; 81 caddr_t hash; 82 caddr_t strtab; /* DT_STRTAB */ 83 int strsz; /* DT_STRSZ */ 84 const Elf_Sym* symtab; /* DT_SYMTAB */ 85 Elf_Addr* got; /* DT_PLTGOT */ 86 const Elf_Rel* pltrel; /* DT_JMPREL */ 87 int pltrelsize; /* DT_PLTRELSZ */ 88 const Elf_Rela* pltrela; /* DT_JMPREL */ 89 int pltrelasize; /* DT_PLTRELSZ */ 90 const Elf_Rel* rel; /* DT_REL */ 91 int relsize; /* DT_RELSZ */ 92 const Elf_Rela* rela; /* DT_RELA */ 93 int relasize; /* DT_RELASZ */ 94 caddr_t modptr; 95 const Elf_Sym* ddbsymtab; /* The symbol table we are using */ 96 long ddbsymcnt; /* Number of symbols */ 97 caddr_t ddbstrtab; /* String table */ 98 long ddbstrcnt; /* number of bytes in string table */ 99 caddr_t symbase; /* malloc'ed symbold base */ 100 caddr_t strbase; /* malloc'ed string base */ 101 #ifdef GDB 102 struct link_map gdb; /* hooks for gdb */ 103 #endif 104 } *elf_file_t; 105 106 static int link_elf_link_common_finish(linker_file_t); 107 static int link_elf_link_preload(linker_class_t cls, 108 const char*, linker_file_t*); 109 static int link_elf_link_preload_finish(linker_file_t); 110 static int link_elf_load_file(linker_class_t, const char*, linker_file_t*); 111 static int link_elf_lookup_symbol(linker_file_t, const char*, 112 c_linker_sym_t*); 113 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*); 114 static int link_elf_search_symbol(linker_file_t, caddr_t value, 115 c_linker_sym_t* sym, long* diffp); 116 117 static void link_elf_unload_file(linker_file_t); 118 static void link_elf_unload_preload(linker_file_t); 119 static int link_elf_lookup_set(linker_file_t, const char *, 120 void ***, void ***, int *); 121 static int link_elf_each_function_name(linker_file_t, 122 int (*)(const char *, void *), 123 void *); 124 static void link_elf_reloc_local(linker_file_t); 125 static Elf_Addr elf_lookup(linker_file_t lf, Elf_Size symidx, int deps); 126 127 static kobj_method_t link_elf_methods[] = { 128 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol), 129 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values), 130 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol), 131 KOBJMETHOD(linker_unload, link_elf_unload_file), 132 KOBJMETHOD(linker_load_file, link_elf_load_file), 133 KOBJMETHOD(linker_link_preload, link_elf_link_preload), 134 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish), 135 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set), 136 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name), 137 { 0, 0 } 138 }; 139 140 static struct linker_class link_elf_class = { 141 #if ELF_TARG_CLASS == ELFCLASS32 142 "elf32", 143 #else 144 "elf64", 145 #endif 146 link_elf_methods, sizeof(struct elf_file) 147 }; 148 149 static int parse_dynamic(elf_file_t ef); 150 static int relocate_file(elf_file_t ef); 151 static int link_elf_preload_parse_symbols(elf_file_t ef); 152 153 #ifdef GDB 154 static void r_debug_state(struct r_debug *dummy_one, 155 struct link_map *dummy_two); 156 157 /* 158 * A list of loaded modules for GDB to use for loading symbols. 159 */ 160 struct r_debug r_debug; 161 162 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL); 163 164 /* 165 * Function for the debugger to set a breakpoint on to gain control. 166 */ 167 static void 168 r_debug_state(struct r_debug *dummy_one __unused, 169 struct link_map *dummy_two __unused) 170 { 171 } 172 173 static void 174 link_elf_add_gdb(struct link_map *l) 175 { 176 struct link_map *prev; 177 178 l->l_next = NULL; 179 180 if (r_debug.r_map == NULL) { 181 /* Add first. */ 182 l->l_prev = NULL; 183 r_debug.r_map = l; 184 } else { 185 /* Append to list. */ 186 for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next) 187 ; 188 l->l_prev = prev; 189 prev->l_next = l; 190 } 191 } 192 193 static void 194 link_elf_delete_gdb(struct link_map *l) 195 { 196 if (l->l_prev == NULL) { 197 /* Remove first. */ 198 if ((r_debug.r_map = l->l_next) != NULL) 199 l->l_next->l_prev = NULL; 200 } else { 201 /* Remove any but first. */ 202 if ((l->l_prev->l_next = l->l_next) != NULL) 203 l->l_next->l_prev = l->l_prev; 204 } 205 } 206 #endif /* GDB */ 207 208 #ifdef __ia64__ 209 Elf_Addr link_elf_get_gp(linker_file_t); 210 #endif 211 212 /* 213 * The kernel symbol table starts here. 214 */ 215 extern struct _dynamic _DYNAMIC; 216 217 static void 218 link_elf_error(const char *s) 219 { 220 printf("kldload: %s\n", s); 221 } 222 223 /* 224 * Actions performed after linking/loading both the preloaded kernel and any 225 * modules; whether preloaded or dynamicly loaded. 226 */ 227 static int 228 link_elf_link_common_finish(linker_file_t lf) 229 { 230 #ifdef GDB 231 elf_file_t ef = (elf_file_t)lf; 232 char *newfilename; 233 #endif 234 int error; 235 236 /* Notify MD code that a module is being loaded. */ 237 error = elf_cpu_load_file(lf); 238 if (error) 239 return (error); 240 241 #ifdef GDB 242 GDB_STATE(RT_ADD); 243 ef->gdb.l_addr = lf->address; 244 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK); 245 strcpy(newfilename, lf->filename); 246 ef->gdb.l_name = newfilename; 247 ef->gdb.l_ld = ef->dynamic; 248 link_elf_add_gdb(&ef->gdb); 249 GDB_STATE(RT_CONSISTENT); 250 #endif 251 252 return (0); 253 } 254 255 static void 256 link_elf_init(void* arg) 257 { 258 Elf_Dyn *dp; 259 caddr_t modptr, baseptr, sizeptr; 260 elf_file_t ef; 261 char *modname; 262 263 linker_add_class(&link_elf_class); 264 265 dp = (Elf_Dyn*) &_DYNAMIC; 266 modname = NULL; 267 modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel"); 268 if (modptr == NULL) 269 modptr = preload_search_by_type("elf kernel"); 270 if (modptr) 271 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 272 if (modname == NULL) 273 modname = "kernel"; 274 linker_kernel_file = linker_make_file(modname, &link_elf_class); 275 if (linker_kernel_file == NULL) 276 panic("link_elf_init: Can't create linker structures for kernel"); 277 278 ef = (elf_file_t) linker_kernel_file; 279 ef->preloaded = 1; 280 ef->address = 0; 281 #ifdef SPARSE_MAPPING 282 ef->object = 0; 283 #endif 284 ef->dynamic = dp; 285 286 if (dp) 287 parse_dynamic(ef); 288 linker_kernel_file->address = (caddr_t) KERNBASE; 289 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address; 290 291 if (modptr) { 292 ef->modptr = modptr; 293 baseptr = preload_search_info(modptr, MODINFO_ADDR); 294 if (baseptr) 295 linker_kernel_file->address = *(caddr_t *)baseptr; 296 sizeptr = preload_search_info(modptr, MODINFO_SIZE); 297 if (sizeptr) 298 linker_kernel_file->size = *(size_t *)sizeptr; 299 } 300 (void)link_elf_preload_parse_symbols(ef); 301 302 #ifdef GDB 303 r_debug.r_map = NULL; 304 r_debug.r_brk = r_debug_state; 305 r_debug.r_state = RT_CONSISTENT; 306 #endif 307 308 (void)link_elf_link_common_finish(linker_kernel_file); 309 linker_kernel_file->flags |= LINKER_FILE_LINKED; 310 } 311 312 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, 0); 313 314 static int 315 link_elf_preload_parse_symbols(elf_file_t ef) 316 { 317 caddr_t pointer; 318 caddr_t ssym, esym, base; 319 caddr_t strtab; 320 int strcnt; 321 Elf_Sym* symtab; 322 int symcnt; 323 324 if (ef->modptr == NULL) 325 return 0; 326 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM); 327 if (pointer == NULL) 328 return 0; 329 ssym = *(caddr_t *)pointer; 330 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM); 331 if (pointer == NULL) 332 return 0; 333 esym = *(caddr_t *)pointer; 334 335 base = ssym; 336 337 symcnt = *(long *)base; 338 base += sizeof(long); 339 symtab = (Elf_Sym *)base; 340 base += roundup(symcnt, sizeof(long)); 341 342 if (base > esym || base < ssym) { 343 printf("Symbols are corrupt!\n"); 344 return EINVAL; 345 } 346 347 strcnt = *(long *)base; 348 base += sizeof(long); 349 strtab = base; 350 base += roundup(strcnt, sizeof(long)); 351 352 if (base > esym || base < ssym) { 353 printf("Symbols are corrupt!\n"); 354 return EINVAL; 355 } 356 357 ef->ddbsymtab = symtab; 358 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym); 359 ef->ddbstrtab = strtab; 360 ef->ddbstrcnt = strcnt; 361 362 return 0; 363 } 364 365 static int 366 parse_dynamic(elf_file_t ef) 367 { 368 Elf_Dyn *dp; 369 int plttype = DT_REL; 370 371 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) { 372 switch (dp->d_tag) { 373 case DT_HASH: 374 { 375 /* From src/libexec/rtld-elf/rtld.c */ 376 const Elf_Hashelt *hashtab = (const Elf_Hashelt *) 377 (ef->address + dp->d_un.d_ptr); 378 ef->nbuckets = hashtab[0]; 379 ef->nchains = hashtab[1]; 380 ef->buckets = hashtab + 2; 381 ef->chains = ef->buckets + ef->nbuckets; 382 break; 383 } 384 case DT_STRTAB: 385 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr); 386 break; 387 case DT_STRSZ: 388 ef->strsz = dp->d_un.d_val; 389 break; 390 case DT_SYMTAB: 391 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr); 392 break; 393 case DT_SYMENT: 394 if (dp->d_un.d_val != sizeof(Elf_Sym)) 395 return ENOEXEC; 396 break; 397 case DT_PLTGOT: 398 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr); 399 break; 400 case DT_REL: 401 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr); 402 break; 403 case DT_RELSZ: 404 ef->relsize = dp->d_un.d_val; 405 break; 406 case DT_RELENT: 407 if (dp->d_un.d_val != sizeof(Elf_Rel)) 408 return ENOEXEC; 409 break; 410 case DT_JMPREL: 411 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr); 412 break; 413 case DT_PLTRELSZ: 414 ef->pltrelsize = dp->d_un.d_val; 415 break; 416 case DT_RELA: 417 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr); 418 break; 419 case DT_RELASZ: 420 ef->relasize = dp->d_un.d_val; 421 break; 422 case DT_RELAENT: 423 if (dp->d_un.d_val != sizeof(Elf_Rela)) 424 return ENOEXEC; 425 break; 426 case DT_PLTREL: 427 plttype = dp->d_un.d_val; 428 if (plttype != DT_REL && plttype != DT_RELA) 429 return ENOEXEC; 430 break; 431 #ifdef GDB 432 case DT_DEBUG: 433 dp->d_un.d_ptr = (Elf_Addr) &r_debug; 434 break; 435 #endif 436 } 437 } 438 439 if (plttype == DT_RELA) { 440 ef->pltrela = (const Elf_Rela *) ef->pltrel; 441 ef->pltrel = NULL; 442 ef->pltrelasize = ef->pltrelsize; 443 ef->pltrelsize = 0; 444 } 445 446 ef->ddbsymtab = ef->symtab; 447 ef->ddbsymcnt = ef->nchains; 448 ef->ddbstrtab = ef->strtab; 449 ef->ddbstrcnt = ef->strsz; 450 451 return 0; 452 } 453 454 static int 455 link_elf_link_preload(linker_class_t cls, 456 const char* filename, linker_file_t *result) 457 { 458 caddr_t modptr, baseptr, sizeptr, dynptr; 459 char *type; 460 elf_file_t ef; 461 linker_file_t lf; 462 int error; 463 vm_offset_t dp; 464 465 /* Look to see if we have the file preloaded */ 466 modptr = preload_search_by_name(filename); 467 if (modptr == NULL) 468 return ENOENT; 469 470 type = (char *)preload_search_info(modptr, MODINFO_TYPE); 471 baseptr = preload_search_info(modptr, MODINFO_ADDR); 472 sizeptr = preload_search_info(modptr, MODINFO_SIZE); 473 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC); 474 if (type == NULL || 475 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 && 476 strcmp(type, "elf module") != 0)) 477 return (EFTYPE); 478 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL) 479 return (EINVAL); 480 481 lf = linker_make_file(filename, &link_elf_class); 482 if (lf == NULL) { 483 return ENOMEM; 484 } 485 486 ef = (elf_file_t) lf; 487 ef->preloaded = 1; 488 ef->modptr = modptr; 489 ef->address = *(caddr_t *)baseptr; 490 #ifdef SPARSE_MAPPING 491 ef->object = 0; 492 #endif 493 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr; 494 ef->dynamic = (Elf_Dyn *)dp; 495 lf->address = ef->address; 496 lf->size = *(size_t *)sizeptr; 497 498 error = parse_dynamic(ef); 499 if (error) { 500 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 501 return error; 502 } 503 link_elf_reloc_local(lf); 504 *result = lf; 505 return (0); 506 } 507 508 static int 509 link_elf_link_preload_finish(linker_file_t lf) 510 { 511 elf_file_t ef; 512 int error; 513 514 ef = (elf_file_t) lf; 515 #if 0 /* this will be more trouble than it's worth for now */ 516 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) { 517 if (dp->d_tag != DT_NEEDED) 518 continue; 519 modname = ef->strtab + dp->d_un.d_val; 520 error = linker_load_module(modname, lf); 521 if (error) 522 goto out; 523 } 524 #endif 525 error = relocate_file(ef); 526 if (error) 527 return error; 528 (void)link_elf_preload_parse_symbols(ef); 529 530 return (link_elf_link_common_finish(lf)); 531 } 532 533 static int 534 link_elf_load_file(linker_class_t cls, const char* filename, 535 linker_file_t* result) 536 { 537 struct nameidata nd; 538 struct thread* td = curthread; /* XXX */ 539 Elf_Ehdr *hdr; 540 caddr_t firstpage; 541 int nbytes, i; 542 Elf_Phdr *phdr; 543 Elf_Phdr *phlimit; 544 Elf_Phdr *segs[MAXSEGS]; 545 int nsegs; 546 Elf_Phdr *phdyn; 547 Elf_Phdr *phphdr; 548 caddr_t mapbase; 549 size_t mapsize; 550 Elf_Off base_offset; 551 Elf_Addr base_vaddr; 552 Elf_Addr base_vlimit; 553 int error = 0; 554 int resid, flags; 555 elf_file_t ef; 556 linker_file_t lf; 557 Elf_Shdr *shdr; 558 int symtabindex; 559 int symstrindex; 560 int symcnt; 561 int strcnt; 562 int vfslocked; 563 564 shdr = NULL; 565 lf = NULL; 566 567 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td); 568 flags = FREAD; 569 error = vn_open(&nd, &flags, 0, NULL); 570 if (error) 571 return error; 572 vfslocked = NDHASGIANT(&nd); 573 NDFREE(&nd, NDF_ONLY_PNBUF); 574 if (nd.ni_vp->v_type != VREG) { 575 error = ENOEXEC; 576 firstpage = NULL; 577 goto out; 578 } 579 #ifdef MAC 580 error = mac_kld_check_load(curthread->td_ucred, nd.ni_vp); 581 if (error) { 582 firstpage = NULL; 583 goto out; 584 } 585 #endif 586 587 /* 588 * Read the elf header from the file. 589 */ 590 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK); 591 if (firstpage == NULL) { 592 error = ENOMEM; 593 goto out; 594 } 595 hdr = (Elf_Ehdr *)firstpage; 596 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0, 597 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 598 &resid, td); 599 nbytes = PAGE_SIZE - resid; 600 if (error) 601 goto out; 602 603 if (!IS_ELF(*hdr)) { 604 error = ENOEXEC; 605 goto out; 606 } 607 608 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS 609 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) { 610 link_elf_error("Unsupported file layout"); 611 error = ENOEXEC; 612 goto out; 613 } 614 if (hdr->e_ident[EI_VERSION] != EV_CURRENT 615 || hdr->e_version != EV_CURRENT) { 616 link_elf_error("Unsupported file version"); 617 error = ENOEXEC; 618 goto out; 619 } 620 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) { 621 link_elf_error("Unsupported file type"); 622 error = ENOEXEC; 623 goto out; 624 } 625 if (hdr->e_machine != ELF_TARG_MACH) { 626 link_elf_error("Unsupported machine"); 627 error = ENOEXEC; 628 goto out; 629 } 630 631 /* 632 * We rely on the program header being in the first page. This is 633 * not strictly required by the ABI specification, but it seems to 634 * always true in practice. And, it simplifies things considerably. 635 */ 636 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) && 637 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) && 638 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes))) 639 link_elf_error("Unreadable program headers"); 640 641 /* 642 * Scan the program header entries, and save key information. 643 * 644 * We rely on there being exactly two load segments, text and data, 645 * in that order. 646 */ 647 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff); 648 phlimit = phdr + hdr->e_phnum; 649 nsegs = 0; 650 phdyn = NULL; 651 phphdr = NULL; 652 while (phdr < phlimit) { 653 switch (phdr->p_type) { 654 655 case PT_LOAD: 656 if (nsegs == MAXSEGS) { 657 link_elf_error("Too many sections"); 658 error = ENOEXEC; 659 goto out; 660 } 661 /* 662 * XXX: We just trust they come in right order ?? 663 */ 664 segs[nsegs] = phdr; 665 ++nsegs; 666 break; 667 668 case PT_PHDR: 669 phphdr = phdr; 670 break; 671 672 case PT_DYNAMIC: 673 phdyn = phdr; 674 break; 675 676 case PT_INTERP: 677 link_elf_error("Unsupported file type"); 678 error = ENOEXEC; 679 goto out; 680 } 681 682 ++phdr; 683 } 684 if (phdyn == NULL) { 685 link_elf_error("Object is not dynamically-linked"); 686 error = ENOEXEC; 687 goto out; 688 } 689 if (nsegs == 0) { 690 link_elf_error("No sections"); 691 error = ENOEXEC; 692 goto out; 693 } 694 695 /* 696 * Allocate the entire address space of the object, to stake out our 697 * contiguous region, and to establish the base address for relocation. 698 */ 699 base_offset = trunc_page(segs[0]->p_offset); 700 base_vaddr = trunc_page(segs[0]->p_vaddr); 701 base_vlimit = round_page(segs[nsegs - 1]->p_vaddr + 702 segs[nsegs - 1]->p_memsz); 703 mapsize = base_vlimit - base_vaddr; 704 705 lf = linker_make_file(filename, &link_elf_class); 706 if (!lf) { 707 error = ENOMEM; 708 goto out; 709 } 710 711 ef = (elf_file_t) lf; 712 #ifdef SPARSE_MAPPING 713 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT); 714 if (ef->object == NULL) { 715 error = ENOMEM; 716 goto out; 717 } 718 ef->address = (caddr_t) vm_map_min(kernel_map); 719 error = vm_map_find(kernel_map, ef->object, 0, 720 (vm_offset_t *) &ef->address, 721 mapsize, 1, 722 VM_PROT_ALL, VM_PROT_ALL, 0); 723 if (error) { 724 vm_object_deallocate(ef->object); 725 ef->object = 0; 726 goto out; 727 } 728 #else 729 ef->address = malloc(mapsize, M_LINKER, M_WAITOK); 730 if (!ef->address) { 731 error = ENOMEM; 732 goto out; 733 } 734 #endif 735 mapbase = ef->address; 736 737 /* 738 * Read the text and data sections and zero the bss. 739 */ 740 for (i = 0; i < nsegs; i++) { 741 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr; 742 error = vn_rdwr(UIO_READ, nd.ni_vp, 743 segbase, segs[i]->p_filesz, segs[i]->p_offset, 744 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 745 &resid, td); 746 if (error) { 747 goto out; 748 } 749 bzero(segbase + segs[i]->p_filesz, 750 segs[i]->p_memsz - segs[i]->p_filesz); 751 752 #ifdef SPARSE_MAPPING 753 /* 754 * Wire down the pages 755 */ 756 error = vm_map_wire(kernel_map, 757 (vm_offset_t) segbase, 758 (vm_offset_t) segbase + segs[i]->p_memsz, 759 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); 760 if (error != KERN_SUCCESS) { 761 error = ENOMEM; 762 goto out; 763 } 764 #endif 765 } 766 767 #ifdef GPROF 768 /* Update profiling information with the new text segment. */ 769 mtx_lock(&Giant); 770 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr + 771 segs[0]->p_memsz)); 772 mtx_unlock(&Giant); 773 #endif 774 775 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr); 776 777 lf->address = ef->address; 778 lf->size = mapsize; 779 780 error = parse_dynamic(ef); 781 if (error) 782 goto out; 783 link_elf_reloc_local(lf); 784 785 error = linker_load_dependencies(lf); 786 if (error) 787 goto out; 788 #if 0 /* this will be more trouble than it's worth for now */ 789 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) { 790 if (dp->d_tag != DT_NEEDED) 791 continue; 792 modname = ef->strtab + dp->d_un.d_val; 793 error = linker_load_module(modname, lf); 794 if (error) 795 goto out; 796 } 797 #endif 798 error = relocate_file(ef); 799 if (error) 800 goto out; 801 802 /* Try and load the symbol table if it's present. (you can strip it!) */ 803 nbytes = hdr->e_shnum * hdr->e_shentsize; 804 if (nbytes == 0 || hdr->e_shoff == 0) 805 goto nosyms; 806 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO); 807 if (shdr == NULL) { 808 error = ENOMEM; 809 goto out; 810 } 811 error = vn_rdwr(UIO_READ, nd.ni_vp, 812 (caddr_t)shdr, nbytes, hdr->e_shoff, 813 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 814 &resid, td); 815 if (error) 816 goto out; 817 symtabindex = -1; 818 symstrindex = -1; 819 for (i = 0; i < hdr->e_shnum; i++) { 820 if (shdr[i].sh_type == SHT_SYMTAB) { 821 symtabindex = i; 822 symstrindex = shdr[i].sh_link; 823 } 824 } 825 if (symtabindex < 0 || symstrindex < 0) 826 goto nosyms; 827 828 symcnt = shdr[symtabindex].sh_size; 829 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK); 830 strcnt = shdr[symstrindex].sh_size; 831 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK); 832 833 if (ef->symbase == NULL || ef->strbase == NULL) { 834 error = ENOMEM; 835 goto out; 836 } 837 error = vn_rdwr(UIO_READ, nd.ni_vp, 838 ef->symbase, symcnt, shdr[symtabindex].sh_offset, 839 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 840 &resid, td); 841 if (error) 842 goto out; 843 error = vn_rdwr(UIO_READ, nd.ni_vp, 844 ef->strbase, strcnt, shdr[symstrindex].sh_offset, 845 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 846 &resid, td); 847 if (error) 848 goto out; 849 850 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym); 851 ef->ddbsymtab = (const Elf_Sym *)ef->symbase; 852 ef->ddbstrcnt = strcnt; 853 ef->ddbstrtab = ef->strbase; 854 855 error = link_elf_link_common_finish(lf); 856 if (error) 857 goto out; 858 859 nosyms: 860 861 *result = lf; 862 863 out: 864 if (error && lf) 865 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 866 if (shdr) 867 free(shdr, M_LINKER); 868 if (firstpage) 869 free(firstpage, M_LINKER); 870 VOP_UNLOCK(nd.ni_vp, 0); 871 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 872 VFS_UNLOCK_GIANT(vfslocked); 873 874 return error; 875 } 876 877 static void 878 link_elf_unload_file(linker_file_t file) 879 { 880 elf_file_t ef = (elf_file_t) file; 881 882 #ifdef GDB 883 if (ef->gdb.l_ld) { 884 GDB_STATE(RT_DELETE); 885 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER); 886 link_elf_delete_gdb(&ef->gdb); 887 GDB_STATE(RT_CONSISTENT); 888 } 889 #endif 890 891 /* Notify MD code that a module is being unloaded. */ 892 elf_cpu_unload_file(file); 893 894 if (ef->preloaded) { 895 link_elf_unload_preload(file); 896 return; 897 } 898 899 #ifdef SPARSE_MAPPING 900 if (ef->object) { 901 vm_map_remove(kernel_map, (vm_offset_t) ef->address, 902 (vm_offset_t) ef->address 903 + (ef->object->size << PAGE_SHIFT)); 904 } 905 #else 906 if (ef->address) 907 free(ef->address, M_LINKER); 908 #endif 909 if (ef->symbase) 910 free(ef->symbase, M_LINKER); 911 if (ef->strbase) 912 free(ef->strbase, M_LINKER); 913 } 914 915 static void 916 link_elf_unload_preload(linker_file_t file) 917 { 918 if (file->filename) 919 preload_delete_name(file->filename); 920 } 921 922 static const char * 923 symbol_name(elf_file_t ef, Elf_Size r_info) 924 { 925 const Elf_Sym *ref; 926 927 if (ELF_R_SYM(r_info)) { 928 ref = ef->symtab + ELF_R_SYM(r_info); 929 return ef->strtab + ref->st_name; 930 } else 931 return NULL; 932 } 933 934 static int 935 relocate_file(elf_file_t ef) 936 { 937 const Elf_Rel *rellim; 938 const Elf_Rel *rel; 939 const Elf_Rela *relalim; 940 const Elf_Rela *rela; 941 const char *symname; 942 943 /* Perform relocations without addend if there are any: */ 944 rel = ef->rel; 945 if (rel) { 946 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize); 947 while (rel < rellim) { 948 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, 949 elf_lookup)) { 950 symname = symbol_name(ef, rel->r_info); 951 printf("link_elf: symbol %s undefined\n", symname); 952 return ENOENT; 953 } 954 rel++; 955 } 956 } 957 958 /* Perform relocations with addend if there are any: */ 959 rela = ef->rela; 960 if (rela) { 961 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize); 962 while (rela < relalim) { 963 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, 964 elf_lookup)) { 965 symname = symbol_name(ef, rela->r_info); 966 printf("link_elf: symbol %s undefined\n", symname); 967 return ENOENT; 968 } 969 rela++; 970 } 971 } 972 973 /* Perform PLT relocations without addend if there are any: */ 974 rel = ef->pltrel; 975 if (rel) { 976 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize); 977 while (rel < rellim) { 978 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, 979 elf_lookup)) { 980 symname = symbol_name(ef, rel->r_info); 981 printf("link_elf: symbol %s undefined\n", symname); 982 return ENOENT; 983 } 984 rel++; 985 } 986 } 987 988 /* Perform relocations with addend if there are any: */ 989 rela = ef->pltrela; 990 if (rela) { 991 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize); 992 while (rela < relalim) { 993 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, 994 elf_lookup)) { 995 symname = symbol_name(ef, rela->r_info); 996 printf("link_elf: symbol %s undefined\n", symname); 997 return ENOENT; 998 } 999 rela++; 1000 } 1001 } 1002 1003 return 0; 1004 } 1005 1006 /* 1007 * Hash function for symbol table lookup. Don't even think about changing 1008 * this. It is specified by the System V ABI. 1009 */ 1010 static unsigned long 1011 elf_hash(const char *name) 1012 { 1013 const unsigned char *p = (const unsigned char *) name; 1014 unsigned long h = 0; 1015 unsigned long g; 1016 1017 while (*p != '\0') { 1018 h = (h << 4) + *p++; 1019 if ((g = h & 0xf0000000) != 0) 1020 h ^= g >> 24; 1021 h &= ~g; 1022 } 1023 return h; 1024 } 1025 1026 static int 1027 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym) 1028 { 1029 elf_file_t ef = (elf_file_t) lf; 1030 unsigned long symnum; 1031 const Elf_Sym* symp; 1032 const char *strp; 1033 unsigned long hash; 1034 int i; 1035 1036 /* If we don't have a hash, bail. */ 1037 if (ef->buckets == NULL || ef->nbuckets == 0) { 1038 printf("link_elf_lookup_symbol: missing symbol hash table\n"); 1039 return ENOENT; 1040 } 1041 1042 /* First, search hashed global symbols */ 1043 hash = elf_hash(name); 1044 symnum = ef->buckets[hash % ef->nbuckets]; 1045 1046 while (symnum != STN_UNDEF) { 1047 if (symnum >= ef->nchains) { 1048 printf("link_elf_lookup_symbol: corrupt symbol table\n"); 1049 return ENOENT; 1050 } 1051 1052 symp = ef->symtab + symnum; 1053 if (symp->st_name == 0) { 1054 printf("link_elf_lookup_symbol: corrupt symbol table\n"); 1055 return ENOENT; 1056 } 1057 1058 strp = ef->strtab + symp->st_name; 1059 1060 if (strcmp(name, strp) == 0) { 1061 if (symp->st_shndx != SHN_UNDEF || 1062 (symp->st_value != 0 && 1063 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) { 1064 *sym = (c_linker_sym_t) symp; 1065 return 0; 1066 } else 1067 return ENOENT; 1068 } 1069 1070 symnum = ef->chains[symnum]; 1071 } 1072 1073 /* If we have not found it, look at the full table (if loaded) */ 1074 if (ef->symtab == ef->ddbsymtab) 1075 return ENOENT; 1076 1077 /* Exhaustive search */ 1078 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1079 strp = ef->ddbstrtab + symp->st_name; 1080 if (strcmp(name, strp) == 0) { 1081 if (symp->st_shndx != SHN_UNDEF || 1082 (symp->st_value != 0 && 1083 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) { 1084 *sym = (c_linker_sym_t) symp; 1085 return 0; 1086 } else 1087 return ENOENT; 1088 } 1089 } 1090 1091 return ENOENT; 1092 } 1093 1094 static int 1095 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval) 1096 { 1097 elf_file_t ef = (elf_file_t) lf; 1098 const Elf_Sym* es = (const Elf_Sym*) sym; 1099 1100 if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) { 1101 symval->name = ef->strtab + es->st_name; 1102 symval->value = (caddr_t) ef->address + es->st_value; 1103 symval->size = es->st_size; 1104 return 0; 1105 } 1106 if (ef->symtab == ef->ddbsymtab) 1107 return ENOENT; 1108 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) { 1109 symval->name = ef->ddbstrtab + es->st_name; 1110 symval->value = (caddr_t) ef->address + es->st_value; 1111 symval->size = es->st_size; 1112 return 0; 1113 } 1114 return ENOENT; 1115 } 1116 1117 static int 1118 link_elf_search_symbol(linker_file_t lf, caddr_t value, 1119 c_linker_sym_t* sym, long* diffp) 1120 { 1121 elf_file_t ef = (elf_file_t) lf; 1122 u_long off = (uintptr_t) (void *) value; 1123 u_long diff = off; 1124 u_long st_value; 1125 const Elf_Sym* es; 1126 const Elf_Sym* best = 0; 1127 int i; 1128 1129 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) { 1130 if (es->st_name == 0) 1131 continue; 1132 st_value = es->st_value + (uintptr_t) (void *) ef->address; 1133 if (off >= st_value) { 1134 if (off - st_value < diff) { 1135 diff = off - st_value; 1136 best = es; 1137 if (diff == 0) 1138 break; 1139 } else if (off - st_value == diff) { 1140 best = es; 1141 } 1142 } 1143 } 1144 if (best == 0) 1145 *diffp = off; 1146 else 1147 *diffp = diff; 1148 *sym = (c_linker_sym_t) best; 1149 1150 return 0; 1151 } 1152 1153 /* 1154 * Look up a linker set on an ELF system. 1155 */ 1156 static int 1157 link_elf_lookup_set(linker_file_t lf, const char *name, 1158 void ***startp, void ***stopp, int *countp) 1159 { 1160 c_linker_sym_t sym; 1161 linker_symval_t symval; 1162 char *setsym; 1163 void **start, **stop; 1164 int len, error = 0, count; 1165 1166 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */ 1167 setsym = malloc(len, M_LINKER, M_WAITOK); 1168 if (setsym == NULL) 1169 return ENOMEM; 1170 1171 /* get address of first entry */ 1172 snprintf(setsym, len, "%s%s", "__start_set_", name); 1173 error = link_elf_lookup_symbol(lf, setsym, &sym); 1174 if (error) 1175 goto out; 1176 link_elf_symbol_values(lf, sym, &symval); 1177 if (symval.value == 0) { 1178 error = ESRCH; 1179 goto out; 1180 } 1181 start = (void **)symval.value; 1182 1183 /* get address of last entry */ 1184 snprintf(setsym, len, "%s%s", "__stop_set_", name); 1185 error = link_elf_lookup_symbol(lf, setsym, &sym); 1186 if (error) 1187 goto out; 1188 link_elf_symbol_values(lf, sym, &symval); 1189 if (symval.value == 0) { 1190 error = ESRCH; 1191 goto out; 1192 } 1193 stop = (void **)symval.value; 1194 1195 /* and the number of entries */ 1196 count = stop - start; 1197 1198 /* and copy out */ 1199 if (startp) 1200 *startp = start; 1201 if (stopp) 1202 *stopp = stop; 1203 if (countp) 1204 *countp = count; 1205 1206 out: 1207 free(setsym, M_LINKER); 1208 return error; 1209 } 1210 1211 static int 1212 link_elf_each_function_name(linker_file_t file, 1213 int (*callback)(const char *, void *), void *opaque) { 1214 elf_file_t ef = (elf_file_t)file; 1215 const Elf_Sym* symp; 1216 int i, error; 1217 1218 /* Exhaustive search */ 1219 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1220 if (symp->st_value != 0 && 1221 ELF_ST_TYPE(symp->st_info) == STT_FUNC) { 1222 error = callback(ef->ddbstrtab + symp->st_name, opaque); 1223 if (error) 1224 return (error); 1225 } 1226 } 1227 return (0); 1228 } 1229 1230 #ifdef __ia64__ 1231 /* 1232 * Each KLD has its own GP. The GP value for each load module is given by 1233 * DT_PLTGOT on ia64. We need GP to construct function descriptors, but 1234 * don't have direct access to the ELF file structure. The link_elf_get_gp() 1235 * function returns the GP given a pointer to a generic linker file struct. 1236 */ 1237 Elf_Addr 1238 link_elf_get_gp(linker_file_t lf) 1239 { 1240 elf_file_t ef = (elf_file_t)lf; 1241 return (Elf_Addr)ef->got; 1242 } 1243 #endif 1244 1245 const Elf_Sym * 1246 elf_get_sym(linker_file_t lf, Elf_Size symidx) 1247 { 1248 elf_file_t ef = (elf_file_t)lf; 1249 1250 if (symidx >= ef->nchains) 1251 return (NULL); 1252 return (ef->symtab + symidx); 1253 } 1254 1255 const char * 1256 elf_get_symname(linker_file_t lf, Elf_Size symidx) 1257 { 1258 elf_file_t ef = (elf_file_t)lf; 1259 const Elf_Sym *sym; 1260 1261 if (symidx >= ef->nchains) 1262 return (NULL); 1263 sym = ef->symtab + symidx; 1264 return (ef->strtab + sym->st_name); 1265 } 1266 1267 /* 1268 * Symbol lookup function that can be used when the symbol index is known (ie 1269 * in relocations). It uses the symbol index instead of doing a fully fledged 1270 * hash table based lookup when such is valid. For example for local symbols. 1271 * This is not only more efficient, it's also more correct. It's not always 1272 * the case that the symbol can be found through the hash table. 1273 */ 1274 static Elf_Addr 1275 elf_lookup(linker_file_t lf, Elf_Size symidx, int deps) 1276 { 1277 elf_file_t ef = (elf_file_t)lf; 1278 const Elf_Sym *sym; 1279 const char *symbol; 1280 1281 /* Don't even try to lookup the symbol if the index is bogus. */ 1282 if (symidx >= ef->nchains) 1283 return (0); 1284 1285 sym = ef->symtab + symidx; 1286 1287 /* 1288 * Don't do a full lookup when the symbol is local. It may even 1289 * fail because it may not be found through the hash table. 1290 */ 1291 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 1292 /* Force lookup failure when we have an insanity. */ 1293 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0) 1294 return (0); 1295 return ((Elf_Addr)ef->address + sym->st_value); 1296 } 1297 1298 /* 1299 * XXX we can avoid doing a hash table based lookup for global 1300 * symbols as well. This however is not always valid, so we'll 1301 * just do it the hard way for now. Performance tweaks can 1302 * always be added. 1303 */ 1304 1305 symbol = ef->strtab + sym->st_name; 1306 1307 /* Force a lookup failure if the symbol name is bogus. */ 1308 if (*symbol == 0) 1309 return (0); 1310 1311 return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps)); 1312 } 1313 1314 static void 1315 link_elf_reloc_local(linker_file_t lf) 1316 { 1317 const Elf_Rel *rellim; 1318 const Elf_Rel *rel; 1319 const Elf_Rela *relalim; 1320 const Elf_Rela *rela; 1321 elf_file_t ef = (elf_file_t)lf; 1322 1323 /* Perform relocations without addend if there are any: */ 1324 if ((rel = ef->rel) != NULL) { 1325 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize); 1326 while (rel < rellim) { 1327 elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, 1328 elf_lookup); 1329 rel++; 1330 } 1331 } 1332 1333 /* Perform relocations with addend if there are any: */ 1334 if ((rela = ef->rela) != NULL) { 1335 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize); 1336 while (rela < relalim) { 1337 elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, 1338 elf_lookup); 1339 rela++; 1340 } 1341 } 1342 } 1343