1 /*- 2 * Copyright (c) 1998-2000 Doug Rabson 3 * Copyright (c) 2004 Peter Wemm 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_ddb.h" 32 #include "opt_mac.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/mac.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/proc.h> 42 #include <sys/namei.h> 43 #include <sys/fcntl.h> 44 #include <sys/vnode.h> 45 #include <sys/linker.h> 46 47 #include <machine/elf.h> 48 49 #include <vm/vm.h> 50 #include <vm/vm_param.h> 51 #include <vm/vm_object.h> 52 #include <vm/vm_kern.h> 53 #include <vm/vm_extern.h> 54 #include <vm/pmap.h> 55 #include <vm/vm_map.h> 56 57 #include <sys/link_elf.h> 58 59 #include "linker_if.h" 60 61 typedef struct { 62 void *addr; 63 Elf_Off size; 64 int flags; 65 int sec; /* Original section */ 66 char *name; 67 } Elf_progent; 68 69 typedef struct { 70 Elf_Rel *rel; 71 int nrel; 72 int sec; 73 } Elf_relent; 74 75 typedef struct { 76 Elf_Rela *rela; 77 int nrela; 78 int sec; 79 } Elf_relaent; 80 81 82 typedef struct elf_file { 83 struct linker_file lf; /* Common fields */ 84 85 caddr_t address; /* Relocation address */ 86 vm_object_t object; /* VM object to hold file pages */ 87 Elf_Shdr *e_shdr; 88 89 Elf_progent *progtab; 90 int nprogtab; 91 92 Elf_relaent *relatab; 93 int nrela; 94 95 Elf_relent *reltab; 96 int nrel; 97 98 Elf_Sym *ddbsymtab; /* The symbol table we are using */ 99 long ddbsymcnt; /* Number of symbols */ 100 caddr_t ddbstrtab; /* String table */ 101 long ddbstrcnt; /* number of bytes in string table */ 102 103 caddr_t shstrtab; /* Section name string table */ 104 long shstrcnt; /* number of bytes in string table */ 105 106 } *elf_file_t; 107 108 static int link_elf_link_preload(linker_class_t cls, 109 const char *, linker_file_t *); 110 static int link_elf_link_preload_finish(linker_file_t); 111 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *); 112 static int link_elf_lookup_symbol(linker_file_t, const char *, 113 c_linker_sym_t *); 114 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, 115 linker_symval_t *); 116 static int link_elf_search_symbol(linker_file_t, caddr_t value, 117 c_linker_sym_t *sym, long *diffp); 118 119 static void link_elf_unload_file(linker_file_t); 120 static int link_elf_lookup_set(linker_file_t, const char *, 121 void ***, void ***, int *); 122 static int link_elf_each_function_name(linker_file_t, 123 int (*)(const char *, void *), void *); 124 static void link_elf_reloc_local(linker_file_t); 125 126 static Elf_Addr elf_obj_lookup(linker_file_t lf, Elf_Word symidx, int deps); 127 128 static kobj_method_t link_elf_methods[] = { 129 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol), 130 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values), 131 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol), 132 KOBJMETHOD(linker_unload, link_elf_unload_file), 133 KOBJMETHOD(linker_load_file, link_elf_load_file), 134 KOBJMETHOD(linker_link_preload, link_elf_link_preload), 135 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish), 136 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set), 137 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name), 138 { 0, 0 } 139 }; 140 141 static struct linker_class link_elf_class = { 142 #if ELF_TARG_CLASS == ELFCLASS32 143 "elf32_obj", 144 #else 145 "elf64_obj", 146 #endif 147 link_elf_methods, sizeof(struct elf_file) 148 }; 149 150 static int relocate_file(elf_file_t ef); 151 152 static void 153 link_elf_error(const char *s) 154 { 155 printf("kldload: %s\n", s); 156 } 157 158 static void 159 link_elf_init(void *arg) 160 { 161 162 linker_add_class(&link_elf_class); 163 } 164 165 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0); 166 167 static int 168 link_elf_link_preload(linker_class_t cls, const char *filename, 169 linker_file_t *result) 170 { 171 /* preload not done this way */ 172 return (EFTYPE); 173 } 174 175 static int 176 link_elf_link_preload_finish(linker_file_t lf) 177 { 178 /* preload not done this way */ 179 return (EFTYPE); 180 } 181 182 static int 183 link_elf_load_file(linker_class_t cls, const char *filename, 184 linker_file_t *result) 185 { 186 struct nameidata nd; 187 struct thread *td = curthread; /* XXX */ 188 Elf_Ehdr *hdr; 189 Elf_Shdr *shdr; 190 int nbytes, i; 191 vm_offset_t mapbase; 192 size_t mapsize; 193 int error = 0; 194 int resid, flags; 195 elf_file_t ef; 196 linker_file_t lf; 197 int symtabindex; 198 int symstrindex; 199 int shstrindex; 200 int nsym; 201 int pb, rl, ra; 202 int alignmask; 203 204 GIANT_REQUIRED; 205 206 shdr = NULL; 207 lf = NULL; 208 mapsize = 0; 209 hdr = NULL; 210 211 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td); 212 flags = FREAD; 213 error = vn_open(&nd, &flags, 0, -1); 214 if (error) 215 return error; 216 NDFREE(&nd, NDF_ONLY_PNBUF); 217 #ifdef MAC 218 error = mac_check_kld_load(td->td_ucred, nd.ni_vp); 219 if (error) { 220 goto out; 221 } 222 #endif 223 224 /* Read the elf header from the file. */ 225 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK); 226 if (hdr == NULL) { 227 error = ENOMEM; 228 goto out; 229 } 230 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0, 231 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 232 &resid, td); 233 if (error) 234 goto out; 235 if (resid != 0){ 236 error = ENOEXEC; 237 goto out; 238 } 239 240 if (!IS_ELF(*hdr)) { 241 error = ENOEXEC; 242 goto out; 243 } 244 245 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS 246 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) { 247 link_elf_error("Unsupported file layout"); 248 error = ENOEXEC; 249 goto out; 250 } 251 if (hdr->e_ident[EI_VERSION] != EV_CURRENT 252 || hdr->e_version != EV_CURRENT) { 253 link_elf_error("Unsupported file version"); 254 error = ENOEXEC; 255 goto out; 256 } 257 if (hdr->e_type != ET_REL) { 258 link_elf_error("Unsupported file type"); 259 error = ENOEXEC; 260 goto out; 261 } 262 if (hdr->e_machine != ELF_TARG_MACH) { 263 link_elf_error("Unsupported machine"); 264 error = ENOEXEC; 265 goto out; 266 } 267 268 lf = linker_make_file(filename, &link_elf_class); 269 if (!lf) { 270 error = ENOMEM; 271 goto out; 272 } 273 ef = (elf_file_t) lf; 274 ef->nprogtab = 0; 275 ef->e_shdr = 0; 276 ef->nrel = 0; 277 ef->nrela = 0; 278 279 /* Allocate and read in the section header */ 280 nbytes = hdr->e_shnum * hdr->e_shentsize; 281 if (nbytes == 0 || hdr->e_shoff == 0 || 282 hdr->e_shentsize != sizeof(Elf_Shdr)) { 283 error = ENOEXEC; 284 goto out; 285 } 286 shdr = malloc(nbytes, M_LINKER, M_WAITOK); 287 if (shdr == NULL) { 288 error = ENOMEM; 289 goto out; 290 } 291 ef->e_shdr = shdr; 292 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff, 293 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); 294 if (error) 295 goto out; 296 if (resid) { 297 error = ENOEXEC; 298 goto out; 299 } 300 301 /* Scan the section header for information and table sizing. */ 302 nsym = 0; 303 symtabindex = -1; 304 symstrindex = -1; 305 for (i = 0; i < hdr->e_shnum; i++) { 306 switch (shdr[i].sh_type) { 307 case SHT_PROGBITS: 308 case SHT_NOBITS: 309 ef->nprogtab++; 310 break; 311 case SHT_SYMTAB: 312 nsym++; 313 symtabindex = i; 314 symstrindex = shdr[i].sh_link; 315 break; 316 case SHT_REL: 317 ef->nrel++; 318 break; 319 case SHT_RELA: 320 ef->nrela++; 321 break; 322 case SHT_STRTAB: 323 break; 324 } 325 } 326 if (ef->nprogtab == 0) { 327 link_elf_error("file has no contents"); 328 error = ENOEXEC; 329 goto out; 330 } 331 if (nsym != 1) { 332 /* Only allow one symbol table for now */ 333 link_elf_error("file has no valid symbol table"); 334 error = ENOEXEC; 335 goto out; 336 } 337 if (symstrindex < 0 || symstrindex > hdr->e_shnum || 338 shdr[symstrindex].sh_type != SHT_STRTAB) { 339 link_elf_error("file has invalid symbol strings"); 340 error = ENOEXEC; 341 goto out; 342 } 343 344 /* Allocate space for tracking the load chunks */ 345 if (ef->nprogtab != 0) 346 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab), 347 M_LINKER, M_WAITOK | M_ZERO); 348 if (ef->nrel != 0) 349 ef->reltab = malloc(ef->nrel * sizeof(*ef->reltab), M_LINKER, 350 M_WAITOK | M_ZERO); 351 if (ef->nrela != 0) 352 ef->relatab = malloc(ef->nrela * sizeof(*ef->relatab), M_LINKER, 353 M_WAITOK | M_ZERO); 354 if ((ef->nprogtab != 0 && ef->progtab == NULL) || 355 (ef->nrel != 0 && ef->reltab == NULL) || 356 (ef->nrela != 0 && ef->relatab == NULL)) { 357 error = ENOMEM; 358 goto out; 359 } 360 361 if (symtabindex == -1) 362 panic("lost symbol table index"); 363 /* Allocate space for and load the symbol table */ 364 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 365 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK); 366 if (ef->ddbsymtab == NULL) { 367 error = ENOMEM; 368 goto out; 369 } 370 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab, 371 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset, 372 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 373 &resid, td); 374 if (error) 375 goto out; 376 if (resid != 0){ 377 error = EINVAL; 378 goto out; 379 } 380 381 if (symstrindex == -1) 382 panic("lost symbol string index"); 383 /* Allocate space for and load the symbol strings */ 384 ef->ddbstrcnt = shdr[symstrindex].sh_size; 385 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK); 386 if (ef->ddbstrtab == NULL) { 387 error = ENOMEM; 388 goto out; 389 } 390 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab, 391 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset, 392 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 393 &resid, td); 394 if (error) 395 goto out; 396 if (resid != 0){ 397 error = EINVAL; 398 goto out; 399 } 400 401 /* Do we have a string table for the section names? */ 402 shstrindex = -1; 403 if (hdr->e_shstrndx != 0 && 404 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) { 405 shstrindex = hdr->e_shstrndx; 406 ef->shstrcnt = shdr[shstrindex].sh_size; 407 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER, 408 M_WAITOK); 409 if (ef->shstrtab == NULL) { 410 error = ENOMEM; 411 goto out; 412 } 413 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab, 414 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset, 415 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 416 &resid, td); 417 if (error) 418 goto out; 419 if (resid != 0){ 420 error = EINVAL; 421 goto out; 422 } 423 } 424 425 /* Size up code/data(progbits) and bss(nobits). */ 426 alignmask = 0; 427 for (i = 0; i < hdr->e_shnum; i++) { 428 switch (shdr[i].sh_type) { 429 case SHT_PROGBITS: 430 case SHT_NOBITS: 431 alignmask = shdr[i].sh_addralign - 1; 432 mapsize += alignmask; 433 mapsize &= ~alignmask; 434 mapsize += shdr[i].sh_size; 435 break; 436 } 437 } 438 439 /* 440 * We know how much space we need for the text/data/bss/etc. 441 * This stuff needs to be in a single chunk so that profiling etc 442 * can get the bounds and gdb can associate offsets with modules 443 */ 444 ef->object = vm_object_allocate(OBJT_DEFAULT, 445 round_page(mapsize) >> PAGE_SHIFT); 446 if (ef->object == NULL) { 447 error = ENOMEM; 448 goto out; 449 } 450 vm_object_reference(ef->object); 451 ef->address = (caddr_t) vm_map_min(kernel_map); 452 error = vm_map_find(kernel_map, ef->object, 0, &mapbase, 453 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE); 454 if (error) { 455 vm_object_deallocate(ef->object); 456 ef->object = 0; 457 goto out; 458 } 459 460 /* Wire the pages */ 461 vm_map_wire(kernel_map, mapbase, 462 mapbase + round_page(mapsize), 463 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); 464 465 /* Inform the kld system about the situation */ 466 lf->address = ef->address = (caddr_t)mapbase; 467 lf->size = mapsize; 468 469 /* 470 * Now load code/data(progbits), zero bss(nobits), allocate space for 471 * and load relocs 472 */ 473 pb = 0; 474 rl = 0; 475 ra = 0; 476 alignmask = 0; 477 for (i = 0; i < hdr->e_shnum; i++) { 478 switch (shdr[i].sh_type) { 479 case SHT_PROGBITS: 480 case SHT_NOBITS: 481 alignmask = shdr[i].sh_addralign - 1; 482 mapbase += alignmask; 483 mapbase &= ~alignmask; 484 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase; 485 if (shdr[i].sh_type == SHT_PROGBITS) { 486 ef->progtab[pb].name = "<<PROGBITS>>"; 487 error = vn_rdwr(UIO_READ, nd.ni_vp, 488 ef->progtab[pb].addr, 489 shdr[i].sh_size, shdr[i].sh_offset, 490 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 491 NOCRED, &resid, td); 492 if (error) 493 goto out; 494 if (resid != 0){ 495 error = EINVAL; 496 goto out; 497 } 498 } else { 499 ef->progtab[pb].name = "<<NOBITS>>"; 500 bzero(ef->progtab[pb].addr, shdr[i].sh_size); 501 } 502 ef->progtab[pb].size = shdr[i].sh_size; 503 ef->progtab[pb].sec = i; 504 if (ef->shstrtab && shdr[i].sh_name != 0) 505 ef->progtab[pb].name = 506 ef->shstrtab + shdr[i].sh_name; 507 mapbase += shdr[i].sh_size; 508 pb++; 509 break; 510 case SHT_REL: 511 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER, 512 M_WAITOK); 513 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); 514 ef->reltab[rl].sec = shdr[i].sh_info; 515 error = vn_rdwr(UIO_READ, nd.ni_vp, 516 (void *)ef->reltab[rl].rel, 517 shdr[i].sh_size, shdr[i].sh_offset, 518 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 519 &resid, td); 520 if (error) 521 goto out; 522 if (resid != 0){ 523 error = EINVAL; 524 goto out; 525 } 526 rl++; 527 break; 528 case SHT_RELA: 529 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER, 530 M_WAITOK); 531 ef->relatab[ra].nrela = 532 shdr[i].sh_size / sizeof(Elf_Rela); 533 ef->relatab[ra].sec = shdr[i].sh_info; 534 error = vn_rdwr(UIO_READ, nd.ni_vp, 535 (void *)ef->relatab[ra].rela, 536 shdr[i].sh_size, shdr[i].sh_offset, 537 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 538 &resid, td); 539 if (error) 540 goto out; 541 if (resid != 0){ 542 error = EINVAL; 543 goto out; 544 } 545 ra++; 546 break; 547 } 548 } 549 if (pb != ef->nprogtab) 550 panic("lost progbits"); 551 if (rl != ef->nrel) 552 panic("lost rel"); 553 if (ra != ef->nrela) 554 panic("lost rela"); 555 if (mapbase != (vm_offset_t)ef->address + mapsize) 556 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n", mapbase, ef->address, mapsize, (vm_offset_t)ef->address + mapsize); 557 558 /* Local intra-module relocations */ 559 link_elf_reloc_local(lf); 560 561 /* Pull in dependencies */ 562 error = linker_load_dependencies(lf); 563 if (error) 564 goto out; 565 566 /* External relocations */ 567 error = relocate_file(ef); 568 if (error) 569 goto out; 570 571 /* Notify MD code that a module is being loaded. */ 572 error = elf_cpu_load_file(lf); 573 if (error) 574 goto out; 575 576 *result = lf; 577 578 out: 579 if (error && lf) 580 linker_file_unload(lf); 581 if (hdr) 582 free(hdr, M_LINKER); 583 VOP_UNLOCK(nd.ni_vp, 0, td); 584 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 585 586 return error; 587 } 588 589 static void 590 link_elf_unload_file(linker_file_t file) 591 { 592 elf_file_t ef = (elf_file_t) file; 593 int i; 594 595 /* Notify MD code that a module is being unloaded. */ 596 elf_cpu_unload_file(file); 597 598 for (i = 0; i < ef->nrel; i++) 599 if (ef->reltab[i].rel) 600 free(ef->reltab[i].rel, M_LINKER); 601 for (i = 0; i < ef->nrela; i++) 602 if (ef->relatab[i].rela) 603 free(ef->relatab[i].rela, M_LINKER); 604 if (ef->reltab) 605 free(ef->reltab, M_LINKER); 606 if (ef->relatab) 607 free(ef->relatab, M_LINKER); 608 if (ef->progtab) 609 free(ef->progtab, M_LINKER); 610 611 if (ef->object) { 612 vm_map_remove(kernel_map, (vm_offset_t) ef->address, 613 (vm_offset_t) ef->address + 614 (ef->object->size << PAGE_SHIFT)); 615 vm_object_deallocate(ef->object); 616 } 617 if (ef->e_shdr) 618 free(ef->e_shdr, M_LINKER); 619 if (ef->ddbsymtab) 620 free(ef->ddbsymtab, M_LINKER); 621 if (ef->ddbstrtab) 622 free(ef->ddbstrtab, M_LINKER); 623 if (ef->shstrtab) 624 free(ef->shstrtab, M_LINKER); 625 } 626 627 static const char * 628 symbol_name(elf_file_t ef, Elf_Word r_info) 629 { 630 const Elf_Sym *ref; 631 632 if (ELF_R_SYM(r_info)) { 633 ref = ef->ddbsymtab + ELF_R_SYM(r_info); 634 return ef->ddbstrtab + ref->st_name; 635 } else 636 return NULL; 637 } 638 639 static Elf_Addr 640 findbase(elf_file_t ef, int sec) 641 { 642 int i; 643 Elf_Addr base = 0; 644 645 for (i = 0; i < ef->nprogtab; i++) { 646 if (sec == ef->progtab[i].sec) 647 base = (Elf_Addr)ef->progtab[i].addr; 648 } 649 if (base == 0) 650 base = (Elf_Addr)ef->address; 651 return base; 652 } 653 654 static int 655 relocate_file(elf_file_t ef) 656 { 657 const Elf_Rel *rellim; 658 const Elf_Rel *rel; 659 const Elf_Rela *relalim; 660 const Elf_Rela *rela; 661 const char *symname; 662 const Elf_Sym *sym; 663 int i; 664 Elf_Word symidx; 665 Elf_Addr base; 666 667 668 /* Perform relocations without addend if there are any: */ 669 for (i = 0; i < ef->nrel; i++) { 670 rel = ef->reltab[i].rel; 671 if (rel == NULL) 672 panic("lost a reltab!"); 673 rellim = rel + ef->reltab[i].nrel; 674 base = findbase(ef, ef->reltab[i].sec); 675 for ( ; rel < rellim; rel++) { 676 symidx = ELF_R_SYM(rel->r_info); 677 if (symidx >= ef->ddbsymcnt) 678 continue; 679 sym = ef->ddbsymtab + symidx; 680 /* Local relocs are already done */ 681 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 682 continue; 683 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL, 684 elf_obj_lookup)) { 685 symname = symbol_name(ef, rel->r_info); 686 printf("link_elf_obj: symbol %s undefined\n", 687 symname); 688 return ENOENT; 689 } 690 } 691 } 692 693 /* Perform relocations with addend if there are any: */ 694 for (i = 0; i < ef->nrela; i++) { 695 rela = ef->relatab[i].rela; 696 if (rela == NULL) 697 panic("lost a relatab!"); 698 relalim = rela + ef->relatab[i].nrela; 699 base = findbase(ef, ef->relatab[i].sec); 700 for ( ; rela < relalim; rela++) { 701 symidx = ELF_R_SYM(rela->r_info); 702 if (symidx >= ef->ddbsymcnt) 703 continue; 704 sym = ef->ddbsymtab + symidx; 705 /* Local relocs are already done */ 706 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 707 continue; 708 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA, 709 elf_obj_lookup)) { 710 symname = symbol_name(ef, rela->r_info); 711 printf("link_elf_obj: symbol %s undefined\n", 712 symname); 713 return ENOENT; 714 } 715 } 716 } 717 718 return 0; 719 } 720 721 static int 722 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym) 723 { 724 elf_file_t ef = (elf_file_t) lf; 725 const Elf_Sym *symp; 726 const char *strp; 727 int i; 728 729 /* XXX search for globals first */ 730 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 731 strp = ef->ddbstrtab + symp->st_name; 732 if (strcmp(name, strp) == 0) { 733 if (symp->st_shndx != SHN_UNDEF || 734 (symp->st_value != 0 && 735 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) { 736 *sym = (c_linker_sym_t) symp; 737 return 0; 738 } else 739 return ENOENT; 740 } 741 } 742 743 return ENOENT; 744 } 745 746 static int 747 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, 748 linker_symval_t *symval) 749 { 750 elf_file_t ef = (elf_file_t) lf; 751 const Elf_Sym *es = (const Elf_Sym*) sym; 752 753 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) { 754 symval->name = ef->ddbstrtab + es->st_name; 755 symval->value = (caddr_t) ef->address + es->st_value; 756 symval->size = es->st_size; 757 return 0; 758 } 759 return ENOENT; 760 } 761 762 static int 763 link_elf_search_symbol(linker_file_t lf, caddr_t value, 764 c_linker_sym_t *sym, long *diffp) 765 { 766 elf_file_t ef = (elf_file_t) lf; 767 u_long off = (uintptr_t) (void *) value; 768 u_long diff = off; 769 u_long st_value; 770 const Elf_Sym *es; 771 const Elf_Sym *best = 0; 772 int i; 773 774 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) { 775 if (es->st_name == 0) 776 continue; 777 st_value = es->st_value + (uintptr_t) (void *) ef->address; 778 if (off >= st_value) { 779 if (off - st_value < diff) { 780 diff = off - st_value; 781 best = es; 782 if (diff == 0) 783 break; 784 } else if (off - st_value == diff) { 785 best = es; 786 } 787 } 788 } 789 if (best == 0) 790 *diffp = off; 791 else 792 *diffp = diff; 793 *sym = (c_linker_sym_t) best; 794 795 return 0; 796 } 797 798 /* 799 * Look up a linker set on an ELF system. 800 */ 801 static int 802 link_elf_lookup_set(linker_file_t lf, const char *name, 803 void ***startp, void ***stopp, int *countp) 804 { 805 elf_file_t ef = (elf_file_t)lf; 806 void **start, **stop; 807 int i, count; 808 809 /* Relative to section number */ 810 for (i = 0; i < ef->nprogtab; i++) { 811 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) && 812 strcmp(ef->progtab[i].name + 4, name) == 0) { 813 start = (void **)ef->progtab[i].addr; 814 stop = (void **)((char *)ef->progtab[i].addr + 815 ef->progtab[i].size); 816 count = stop - start; 817 if (startp) 818 *startp = start; 819 if (stopp) 820 *stopp = stop; 821 if (countp) 822 *countp = count; 823 return (0); 824 } 825 } 826 return (ESRCH); 827 } 828 829 static int 830 link_elf_each_function_name(linker_file_t file, 831 int (*callback)(const char *, void *), void *opaque) 832 { 833 elf_file_t ef = (elf_file_t)file; 834 const Elf_Sym *symp; 835 int i, error; 836 837 /* Exhaustive search */ 838 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 839 if (symp->st_value != 0 && 840 ELF_ST_TYPE(symp->st_info) == STT_FUNC) { 841 error = callback(ef->ddbstrtab + symp->st_name, opaque); 842 if (error) 843 return (error); 844 } 845 } 846 return (0); 847 } 848 849 /* 850 * Symbol lookup function that can be used when the symbol index is known (ie 851 * in relocations). It uses the symbol index instead of doing a fully fledged 852 * hash table based lookup when such is valid. For example for local symbols. 853 * This is not only more efficient, it's also more correct. It's not always 854 * the case that the symbol can be found through the hash table. 855 */ 856 static Elf_Addr 857 elf_obj_lookup(linker_file_t lf, Elf_Word symidx, int deps) 858 { 859 elf_file_t ef = (elf_file_t)lf; 860 const Elf_Sym *sym; 861 const char *symbol; 862 Elf_Addr ret; 863 int i; 864 865 /* Don't even try to lookup the symbol if the index is bogus. */ 866 if (symidx >= ef->ddbsymcnt) 867 return (0); 868 869 sym = ef->ddbsymtab + symidx; 870 871 /* Quick answer if there is a definition included. */ 872 if (sym->st_shndx != SHN_UNDEF) { 873 ret = 0; 874 /* Relative to section number */ 875 for (i = 0; i < ef->nprogtab; i++) { 876 if (sym->st_shndx == ef->progtab[i].sec) { 877 ret = (Elf_Addr)ef->progtab[i].addr; 878 break; 879 } 880 } 881 return ret + sym->st_value; 882 } 883 884 /* If we get here, then it is undefined and needs a lookup. */ 885 switch (ELF_ST_BIND(sym->st_info)) { 886 case STB_LOCAL: 887 /* Local, but undefined? huh? */ 888 return (0); 889 890 case STB_GLOBAL: 891 /* Relative to Data or Function name */ 892 symbol = ef->ddbstrtab + sym->st_name; 893 894 /* Force a lookup failure if the symbol name is bogus. */ 895 if (*symbol == 0) 896 return (0); 897 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps)); 898 return ret; 899 900 case STB_WEAK: 901 printf("link_elf_obj: Weak symbols not supported\n"); 902 return (0); 903 904 default: 905 return (0); 906 } 907 } 908 909 static void 910 link_elf_reloc_local(linker_file_t lf) 911 { 912 elf_file_t ef = (elf_file_t)lf; 913 const Elf_Rel *rellim; 914 const Elf_Rel *rel; 915 const Elf_Rela *relalim; 916 const Elf_Rela *rela; 917 const Elf_Sym *sym; 918 Elf_Addr base; 919 int i; 920 Elf_Word symidx; 921 922 923 /* Perform relocations without addend if there are any: */ 924 for (i = 0; i < ef->nrel; i++) { 925 rel = ef->reltab[i].rel; 926 if (rel == NULL) 927 panic("lost a reltab!"); 928 rellim = rel + ef->reltab[i].nrel; 929 base = findbase(ef, ef->reltab[i].sec); 930 for ( ; rel < rellim; rel++) { 931 symidx = ELF_R_SYM(rel->r_info); 932 if (symidx >= ef->ddbsymcnt) 933 continue; 934 sym = ef->ddbsymtab + symidx; 935 /* Only do local relocs */ 936 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 937 continue; 938 elf_reloc_local(lf, base, rel, ELF_RELOC_REL, 939 elf_obj_lookup); 940 } 941 } 942 943 /* Perform relocations with addend if there are any: */ 944 for (i = 0; i < ef->nrela; i++) { 945 rela = ef->relatab[i].rela; 946 if (rela == NULL) 947 panic("lost a relatab!"); 948 relalim = rela + ef->relatab[i].nrela; 949 base = findbase(ef, ef->relatab[i].sec); 950 for ( ; rela < relalim; rela++) { 951 symidx = ELF_R_SYM(rela->r_info); 952 if (symidx >= ef->ddbsymcnt) 953 continue; 954 sym = ef->ddbsymtab + symidx; 955 /* Only do local relocs */ 956 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 957 continue; 958 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA, 959 elf_obj_lookup); 960 } 961 } 962 } 963