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 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/mount.h> 40 #include <sys/proc.h> 41 #include <sys/namei.h> 42 #include <sys/fcntl.h> 43 #include <sys/vnode.h> 44 #include <sys/linker.h> 45 46 #include <machine/elf.h> 47 48 #include <security/mac/mac_framework.h> 49 50 #include <vm/vm.h> 51 #include <vm/vm_param.h> 52 #include <vm/vm_object.h> 53 #include <vm/vm_kern.h> 54 #include <vm/vm_extern.h> 55 #include <vm/pmap.h> 56 #include <vm/vm_map.h> 57 58 #include <sys/link_elf.h> 59 60 #ifdef DDB_CTF 61 #include <net/zlib.h> 62 #endif 63 64 #include "linker_if.h" 65 66 typedef struct { 67 void *addr; 68 Elf_Off size; 69 int flags; 70 int sec; /* Original section */ 71 char *name; 72 } Elf_progent; 73 74 typedef struct { 75 Elf_Rel *rel; 76 int nrel; 77 int sec; 78 } Elf_relent; 79 80 typedef struct { 81 Elf_Rela *rela; 82 int nrela; 83 int sec; 84 } Elf_relaent; 85 86 87 typedef struct elf_file { 88 struct linker_file lf; /* Common fields */ 89 90 int preloaded; 91 caddr_t address; /* Relocation address */ 92 vm_object_t object; /* VM object to hold file pages */ 93 Elf_Shdr *e_shdr; 94 95 Elf_progent *progtab; 96 int nprogtab; 97 98 Elf_relaent *relatab; 99 int nrelatab; 100 101 Elf_relent *reltab; 102 int nreltab; 103 104 Elf_Sym *ddbsymtab; /* The symbol table we are using */ 105 long ddbsymcnt; /* Number of symbols */ 106 caddr_t ddbstrtab; /* String table */ 107 long ddbstrcnt; /* number of bytes in string table */ 108 109 caddr_t shstrtab; /* Section name string table */ 110 long shstrcnt; /* number of bytes in string table */ 111 112 caddr_t ctftab; /* CTF table */ 113 long ctfcnt; /* number of bytes in CTF table */ 114 caddr_t ctfoff; /* CTF offset table */ 115 caddr_t typoff; /* Type offset table */ 116 long typlen; /* Number of type entries. */ 117 118 } *elf_file_t; 119 120 #include <kern/kern_ctf.c> 121 122 static int link_elf_link_preload(linker_class_t cls, 123 const char *, linker_file_t *); 124 static int link_elf_link_preload_finish(linker_file_t); 125 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *); 126 static int link_elf_lookup_symbol(linker_file_t, const char *, 127 c_linker_sym_t *); 128 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, 129 linker_symval_t *); 130 static int link_elf_search_symbol(linker_file_t, caddr_t value, 131 c_linker_sym_t *sym, long *diffp); 132 133 static void link_elf_unload_file(linker_file_t); 134 static int link_elf_lookup_set(linker_file_t, const char *, 135 void ***, void ***, int *); 136 static int link_elf_each_function_name(linker_file_t, 137 int (*)(const char *, void *), void *); 138 static int link_elf_each_function_nameval(linker_file_t, 139 linker_function_nameval_callback_t, 140 void *); 141 static void link_elf_reloc_local(linker_file_t); 142 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **); 143 static long link_elf_strtab_get(linker_file_t, caddr_t *); 144 145 static Elf_Addr elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps); 146 147 static kobj_method_t link_elf_methods[] = { 148 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol), 149 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values), 150 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol), 151 KOBJMETHOD(linker_unload, link_elf_unload_file), 152 KOBJMETHOD(linker_load_file, link_elf_load_file), 153 KOBJMETHOD(linker_link_preload, link_elf_link_preload), 154 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish), 155 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set), 156 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name), 157 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval), 158 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get), 159 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get), 160 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get), 161 { 0, 0 } 162 }; 163 164 static struct linker_class link_elf_class = { 165 #if ELF_TARG_CLASS == ELFCLASS32 166 "elf32_obj", 167 #else 168 "elf64_obj", 169 #endif 170 link_elf_methods, sizeof(struct elf_file) 171 }; 172 173 static int relocate_file(elf_file_t ef); 174 175 static void 176 link_elf_error(const char *filename, const char *s) 177 { 178 if (filename == NULL) 179 printf("kldload: %s\n", s); 180 else 181 printf("kldload: %s: %s\n", filename, s); 182 } 183 184 static void 185 link_elf_init(void *arg) 186 { 187 188 linker_add_class(&link_elf_class); 189 } 190 191 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0); 192 193 static int 194 link_elf_link_preload(linker_class_t cls, const char *filename, 195 linker_file_t *result) 196 { 197 Elf_Ehdr *hdr; 198 Elf_Shdr *shdr; 199 Elf_Sym *es; 200 void *modptr, *baseptr, *sizeptr; 201 char *type; 202 elf_file_t ef; 203 linker_file_t lf; 204 Elf_Addr off; 205 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex; 206 207 /* Look to see if we have the file preloaded */ 208 modptr = preload_search_by_name(filename); 209 if (modptr == NULL) 210 return ENOENT; 211 212 type = (char *)preload_search_info(modptr, MODINFO_TYPE); 213 baseptr = preload_search_info(modptr, MODINFO_ADDR); 214 sizeptr = preload_search_info(modptr, MODINFO_SIZE); 215 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA | 216 MODINFOMD_ELFHDR); 217 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA | 218 MODINFOMD_SHDR); 219 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) 220 " obj module") != 0 && 221 strcmp(type, "elf obj module") != 0)) { 222 return (EFTYPE); 223 } 224 if (baseptr == NULL || sizeptr == NULL || hdr == NULL || 225 shdr == NULL) 226 return (EINVAL); 227 228 lf = linker_make_file(filename, &link_elf_class); 229 if (lf == NULL) 230 return (ENOMEM); 231 232 ef = (elf_file_t)lf; 233 ef->preloaded = 1; 234 ef->address = *(caddr_t *)baseptr; 235 lf->address = *(caddr_t *)baseptr; 236 lf->size = *(size_t *)sizeptr; 237 238 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 239 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 240 hdr->e_ident[EI_VERSION] != EV_CURRENT || 241 hdr->e_version != EV_CURRENT || 242 hdr->e_type != ET_REL || 243 hdr->e_machine != ELF_TARG_MACH) { 244 error = EFTYPE; 245 goto out; 246 } 247 ef->e_shdr = shdr; 248 249 /* Scan the section header for information and table sizing. */ 250 symtabindex = -1; 251 symstrindex = -1; 252 for (i = 0; i < hdr->e_shnum; i++) { 253 switch (shdr[i].sh_type) { 254 case SHT_PROGBITS: 255 case SHT_NOBITS: 256 ef->nprogtab++; 257 break; 258 case SHT_SYMTAB: 259 symtabindex = i; 260 symstrindex = shdr[i].sh_link; 261 break; 262 case SHT_REL: 263 ef->nreltab++; 264 break; 265 case SHT_RELA: 266 ef->nrelatab++; 267 break; 268 } 269 } 270 271 shstrindex = hdr->e_shstrndx; 272 if (ef->nprogtab == 0 || symstrindex < 0 || 273 symstrindex >= hdr->e_shnum || 274 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 || 275 shstrindex >= hdr->e_shnum || 276 shdr[shstrindex].sh_type != SHT_STRTAB) { 277 printf("%s: bad/missing section headers\n", filename); 278 error = ENOEXEC; 279 goto out; 280 } 281 282 /* Allocate space for tracking the load chunks */ 283 if (ef->nprogtab != 0) 284 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab), 285 M_LINKER, M_WAITOK | M_ZERO); 286 if (ef->nreltab != 0) 287 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab), 288 M_LINKER, M_WAITOK | M_ZERO); 289 if (ef->nrelatab != 0) 290 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab), 291 M_LINKER, M_WAITOK | M_ZERO); 292 if ((ef->nprogtab != 0 && ef->progtab == NULL) || 293 (ef->nreltab != 0 && ef->reltab == NULL) || 294 (ef->nrelatab != 0 && ef->relatab == NULL)) { 295 error = ENOMEM; 296 goto out; 297 } 298 299 /* XXX, relocate the sh_addr fields saved by the loader. */ 300 off = 0; 301 for (i = 0; i < hdr->e_shnum; i++) { 302 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off)) 303 off = shdr[i].sh_addr; 304 } 305 for (i = 0; i < hdr->e_shnum; i++) { 306 if (shdr[i].sh_addr != 0) 307 shdr[i].sh_addr = shdr[i].sh_addr - off + 308 (Elf_Addr)ef->address; 309 } 310 311 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 312 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr; 313 ef->ddbstrcnt = shdr[symstrindex].sh_size; 314 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr; 315 ef->shstrcnt = shdr[shstrindex].sh_size; 316 ef->shstrtab = (char *)shdr[shstrindex].sh_addr; 317 318 /* Now fill out progtab and the relocation tables. */ 319 pb = 0; 320 rl = 0; 321 ra = 0; 322 for (i = 0; i < hdr->e_shnum; i++) { 323 switch (shdr[i].sh_type) { 324 case SHT_PROGBITS: 325 case SHT_NOBITS: 326 ef->progtab[pb].addr = (void *)shdr[i].sh_addr; 327 if (shdr[i].sh_type == SHT_PROGBITS) 328 ef->progtab[pb].name = "<<PROGBITS>>"; 329 else 330 ef->progtab[pb].name = "<<NOBITS>>"; 331 ef->progtab[pb].size = shdr[i].sh_size; 332 ef->progtab[pb].sec = i; 333 if (ef->shstrtab && shdr[i].sh_name != 0) 334 ef->progtab[pb].name = 335 ef->shstrtab + shdr[i].sh_name; 336 337 /* Update all symbol values with the offset. */ 338 for (j = 0; j < ef->ddbsymcnt; j++) { 339 es = &ef->ddbsymtab[j]; 340 if (es->st_shndx != i) 341 continue; 342 es->st_value += (Elf_Addr)ef->progtab[pb].addr; 343 } 344 pb++; 345 break; 346 case SHT_REL: 347 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr; 348 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); 349 ef->reltab[rl].sec = shdr[i].sh_info; 350 rl++; 351 break; 352 case SHT_RELA: 353 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr; 354 ef->relatab[ra].nrela = 355 shdr[i].sh_size / sizeof(Elf_Rela); 356 ef->relatab[ra].sec = shdr[i].sh_info; 357 ra++; 358 break; 359 } 360 } 361 if (pb != ef->nprogtab) 362 panic("lost progbits"); 363 if (rl != ef->nreltab) 364 panic("lost reltab"); 365 if (ra != ef->nrelatab) 366 panic("lost relatab"); 367 368 /* Local intra-module relocations */ 369 link_elf_reloc_local(lf); 370 371 *result = lf; 372 return (0); 373 374 out: 375 /* preload not done this way */ 376 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 377 return (error); 378 } 379 380 static int 381 link_elf_link_preload_finish(linker_file_t lf) 382 { 383 elf_file_t ef; 384 int error; 385 386 ef = (elf_file_t)lf; 387 error = relocate_file(ef); 388 if (error) 389 return error; 390 391 /* Notify MD code that a module is being loaded. */ 392 error = elf_cpu_load_file(lf); 393 if (error) 394 return (error); 395 396 return (0); 397 } 398 399 static int 400 link_elf_load_file(linker_class_t cls, const char *filename, 401 linker_file_t *result) 402 { 403 struct nameidata nd; 404 struct thread *td = curthread; /* XXX */ 405 Elf_Ehdr *hdr; 406 Elf_Shdr *shdr; 407 Elf_Sym *es; 408 int nbytes, i, j; 409 vm_offset_t mapbase; 410 size_t mapsize; 411 int error = 0; 412 int resid, flags; 413 elf_file_t ef; 414 linker_file_t lf; 415 int symtabindex; 416 int symstrindex; 417 int shstrindex; 418 int nsym; 419 int pb, rl, ra; 420 int alignmask; 421 int vfslocked; 422 423 shdr = NULL; 424 lf = NULL; 425 mapsize = 0; 426 hdr = NULL; 427 428 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td); 429 flags = FREAD; 430 error = vn_open(&nd, &flags, 0, NULL); 431 if (error) 432 return error; 433 vfslocked = NDHASGIANT(&nd); 434 NDFREE(&nd, NDF_ONLY_PNBUF); 435 if (nd.ni_vp->v_type != VREG) { 436 error = ENOEXEC; 437 goto out; 438 } 439 #ifdef MAC 440 error = mac_kld_check_load(td->td_ucred, nd.ni_vp); 441 if (error) { 442 goto out; 443 } 444 #endif 445 446 /* Read the elf header from the file. */ 447 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK); 448 if (hdr == NULL) { 449 error = ENOMEM; 450 goto out; 451 } 452 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0, 453 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 454 &resid, td); 455 if (error) 456 goto out; 457 if (resid != 0){ 458 error = ENOEXEC; 459 goto out; 460 } 461 462 if (!IS_ELF(*hdr)) { 463 error = ENOEXEC; 464 goto out; 465 } 466 467 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS 468 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) { 469 link_elf_error(filename, "Unsupported file layout"); 470 error = ENOEXEC; 471 goto out; 472 } 473 if (hdr->e_ident[EI_VERSION] != EV_CURRENT 474 || hdr->e_version != EV_CURRENT) { 475 link_elf_error(filename, "Unsupported file version"); 476 error = ENOEXEC; 477 goto out; 478 } 479 if (hdr->e_type != ET_REL) { 480 error = ENOSYS; 481 goto out; 482 } 483 if (hdr->e_machine != ELF_TARG_MACH) { 484 link_elf_error(filename, "Unsupported machine"); 485 error = ENOEXEC; 486 goto out; 487 } 488 489 lf = linker_make_file(filename, &link_elf_class); 490 if (!lf) { 491 error = ENOMEM; 492 goto out; 493 } 494 ef = (elf_file_t) lf; 495 ef->nprogtab = 0; 496 ef->e_shdr = 0; 497 ef->nreltab = 0; 498 ef->nrelatab = 0; 499 500 /* Allocate and read in the section header */ 501 nbytes = hdr->e_shnum * hdr->e_shentsize; 502 if (nbytes == 0 || hdr->e_shoff == 0 || 503 hdr->e_shentsize != sizeof(Elf_Shdr)) { 504 error = ENOEXEC; 505 goto out; 506 } 507 shdr = malloc(nbytes, M_LINKER, M_WAITOK); 508 if (shdr == NULL) { 509 error = ENOMEM; 510 goto out; 511 } 512 ef->e_shdr = shdr; 513 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff, 514 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); 515 if (error) 516 goto out; 517 if (resid) { 518 error = ENOEXEC; 519 goto out; 520 } 521 522 /* Scan the section header for information and table sizing. */ 523 nsym = 0; 524 symtabindex = -1; 525 symstrindex = -1; 526 for (i = 0; i < hdr->e_shnum; i++) { 527 switch (shdr[i].sh_type) { 528 case SHT_PROGBITS: 529 case SHT_NOBITS: 530 ef->nprogtab++; 531 break; 532 case SHT_SYMTAB: 533 nsym++; 534 symtabindex = i; 535 symstrindex = shdr[i].sh_link; 536 break; 537 case SHT_REL: 538 ef->nreltab++; 539 break; 540 case SHT_RELA: 541 ef->nrelatab++; 542 break; 543 case SHT_STRTAB: 544 break; 545 } 546 } 547 if (ef->nprogtab == 0) { 548 link_elf_error(filename, "file has no contents"); 549 error = ENOEXEC; 550 goto out; 551 } 552 if (nsym != 1) { 553 /* Only allow one symbol table for now */ 554 link_elf_error(filename, "file has no valid symbol table"); 555 error = ENOEXEC; 556 goto out; 557 } 558 if (symstrindex < 0 || symstrindex > hdr->e_shnum || 559 shdr[symstrindex].sh_type != SHT_STRTAB) { 560 link_elf_error(filename, "file has invalid symbol strings"); 561 error = ENOEXEC; 562 goto out; 563 } 564 565 /* Allocate space for tracking the load chunks */ 566 if (ef->nprogtab != 0) 567 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab), 568 M_LINKER, M_WAITOK | M_ZERO); 569 if (ef->nreltab != 0) 570 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab), 571 M_LINKER, M_WAITOK | M_ZERO); 572 if (ef->nrelatab != 0) 573 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab), 574 M_LINKER, M_WAITOK | M_ZERO); 575 if ((ef->nprogtab != 0 && ef->progtab == NULL) || 576 (ef->nreltab != 0 && ef->reltab == NULL) || 577 (ef->nrelatab != 0 && ef->relatab == NULL)) { 578 error = ENOMEM; 579 goto out; 580 } 581 582 if (symtabindex == -1) 583 panic("lost symbol table index"); 584 /* Allocate space for and load the symbol table */ 585 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 586 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK); 587 if (ef->ddbsymtab == NULL) { 588 error = ENOMEM; 589 goto out; 590 } 591 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab, 592 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset, 593 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 594 &resid, td); 595 if (error) 596 goto out; 597 if (resid != 0){ 598 error = EINVAL; 599 goto out; 600 } 601 602 if (symstrindex == -1) 603 panic("lost symbol string index"); 604 /* Allocate space for and load the symbol strings */ 605 ef->ddbstrcnt = shdr[symstrindex].sh_size; 606 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK); 607 if (ef->ddbstrtab == NULL) { 608 error = ENOMEM; 609 goto out; 610 } 611 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab, 612 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset, 613 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 614 &resid, td); 615 if (error) 616 goto out; 617 if (resid != 0){ 618 error = EINVAL; 619 goto out; 620 } 621 622 /* Do we have a string table for the section names? */ 623 shstrindex = -1; 624 if (hdr->e_shstrndx != 0 && 625 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) { 626 shstrindex = hdr->e_shstrndx; 627 ef->shstrcnt = shdr[shstrindex].sh_size; 628 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER, 629 M_WAITOK); 630 if (ef->shstrtab == NULL) { 631 error = ENOMEM; 632 goto out; 633 } 634 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab, 635 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset, 636 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 637 &resid, td); 638 if (error) 639 goto out; 640 if (resid != 0){ 641 error = EINVAL; 642 goto out; 643 } 644 } 645 646 /* Size up code/data(progbits) and bss(nobits). */ 647 alignmask = 0; 648 for (i = 0; i < hdr->e_shnum; i++) { 649 switch (shdr[i].sh_type) { 650 case SHT_PROGBITS: 651 case SHT_NOBITS: 652 alignmask = shdr[i].sh_addralign - 1; 653 mapsize += alignmask; 654 mapsize &= ~alignmask; 655 mapsize += shdr[i].sh_size; 656 break; 657 } 658 } 659 660 /* 661 * We know how much space we need for the text/data/bss/etc. 662 * This stuff needs to be in a single chunk so that profiling etc 663 * can get the bounds and gdb can associate offsets with modules 664 */ 665 ef->object = vm_object_allocate(OBJT_DEFAULT, 666 round_page(mapsize) >> PAGE_SHIFT); 667 if (ef->object == NULL) { 668 error = ENOMEM; 669 goto out; 670 } 671 ef->address = (caddr_t) vm_map_min(kernel_map); 672 673 /* 674 * In order to satisfy amd64's architectural requirements on the 675 * location of code and data in the kernel's address space, request a 676 * mapping that is above the kernel. 677 */ 678 mapbase = KERNBASE; 679 error = vm_map_find(kernel_map, ef->object, 0, &mapbase, 680 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE); 681 if (error) { 682 vm_object_deallocate(ef->object); 683 ef->object = 0; 684 goto out; 685 } 686 687 /* Wire the pages */ 688 error = vm_map_wire(kernel_map, mapbase, 689 mapbase + round_page(mapsize), 690 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); 691 if (error != KERN_SUCCESS) { 692 error = ENOMEM; 693 goto out; 694 } 695 696 /* Inform the kld system about the situation */ 697 lf->address = ef->address = (caddr_t)mapbase; 698 lf->size = mapsize; 699 700 /* 701 * Now load code/data(progbits), zero bss(nobits), allocate space for 702 * and load relocs 703 */ 704 pb = 0; 705 rl = 0; 706 ra = 0; 707 alignmask = 0; 708 for (i = 0; i < hdr->e_shnum; i++) { 709 switch (shdr[i].sh_type) { 710 case SHT_PROGBITS: 711 case SHT_NOBITS: 712 alignmask = shdr[i].sh_addralign - 1; 713 mapbase += alignmask; 714 mapbase &= ~alignmask; 715 ef->progtab[pb].addr = (void *)(uintptr_t)mapbase; 716 if (shdr[i].sh_type == SHT_PROGBITS) { 717 ef->progtab[pb].name = "<<PROGBITS>>"; 718 error = vn_rdwr(UIO_READ, nd.ni_vp, 719 ef->progtab[pb].addr, 720 shdr[i].sh_size, shdr[i].sh_offset, 721 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 722 NOCRED, &resid, td); 723 if (error) 724 goto out; 725 if (resid != 0){ 726 error = EINVAL; 727 goto out; 728 } 729 } else { 730 ef->progtab[pb].name = "<<NOBITS>>"; 731 bzero(ef->progtab[pb].addr, shdr[i].sh_size); 732 } 733 ef->progtab[pb].size = shdr[i].sh_size; 734 ef->progtab[pb].sec = i; 735 if (ef->shstrtab && shdr[i].sh_name != 0) 736 ef->progtab[pb].name = 737 ef->shstrtab + shdr[i].sh_name; 738 739 /* Update all symbol values with the offset. */ 740 for (j = 0; j < ef->ddbsymcnt; j++) { 741 es = &ef->ddbsymtab[j]; 742 if (es->st_shndx != i) 743 continue; 744 es->st_value += (Elf_Addr)ef->progtab[pb].addr; 745 } 746 mapbase += shdr[i].sh_size; 747 pb++; 748 break; 749 case SHT_REL: 750 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER, 751 M_WAITOK); 752 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); 753 ef->reltab[rl].sec = shdr[i].sh_info; 754 error = vn_rdwr(UIO_READ, nd.ni_vp, 755 (void *)ef->reltab[rl].rel, 756 shdr[i].sh_size, shdr[i].sh_offset, 757 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 758 &resid, td); 759 if (error) 760 goto out; 761 if (resid != 0){ 762 error = EINVAL; 763 goto out; 764 } 765 rl++; 766 break; 767 case SHT_RELA: 768 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER, 769 M_WAITOK); 770 ef->relatab[ra].nrela = 771 shdr[i].sh_size / sizeof(Elf_Rela); 772 ef->relatab[ra].sec = shdr[i].sh_info; 773 error = vn_rdwr(UIO_READ, nd.ni_vp, 774 (void *)ef->relatab[ra].rela, 775 shdr[i].sh_size, shdr[i].sh_offset, 776 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, 777 &resid, td); 778 if (error) 779 goto out; 780 if (resid != 0){ 781 error = EINVAL; 782 goto out; 783 } 784 ra++; 785 break; 786 } 787 } 788 if (pb != ef->nprogtab) 789 panic("lost progbits"); 790 if (rl != ef->nreltab) 791 panic("lost reltab"); 792 if (ra != ef->nrelatab) 793 panic("lost relatab"); 794 if (mapbase != (vm_offset_t)ef->address + mapsize) 795 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n", 796 mapbase, ef->address, mapsize, 797 (vm_offset_t)ef->address + mapsize); 798 799 /* Local intra-module relocations */ 800 link_elf_reloc_local(lf); 801 802 /* Pull in dependencies */ 803 VOP_UNLOCK(nd.ni_vp, 0); 804 error = linker_load_dependencies(lf); 805 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); 806 if (error) 807 goto out; 808 809 /* External relocations */ 810 error = relocate_file(ef); 811 if (error) 812 goto out; 813 814 /* Notify MD code that a module is being loaded. */ 815 error = elf_cpu_load_file(lf); 816 if (error) 817 goto out; 818 819 *result = lf; 820 821 out: 822 if (error && lf) 823 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 824 if (hdr) 825 free(hdr, M_LINKER); 826 VOP_UNLOCK(nd.ni_vp, 0); 827 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 828 VFS_UNLOCK_GIANT(vfslocked); 829 830 return error; 831 } 832 833 static void 834 link_elf_unload_file(linker_file_t file) 835 { 836 elf_file_t ef = (elf_file_t) file; 837 int i; 838 839 /* Notify MD code that a module is being unloaded. */ 840 elf_cpu_unload_file(file); 841 842 if (ef->preloaded) { 843 if (ef->reltab) 844 free(ef->reltab, M_LINKER); 845 if (ef->relatab) 846 free(ef->relatab, M_LINKER); 847 if (ef->progtab) 848 free(ef->progtab, M_LINKER); 849 if (ef->ctftab) 850 free(ef->ctftab, M_LINKER); 851 if (ef->ctfoff) 852 free(ef->ctfoff, M_LINKER); 853 if (ef->typoff) 854 free(ef->typoff, M_LINKER); 855 if (file->filename != NULL) 856 preload_delete_name(file->filename); 857 /* XXX reclaim module memory? */ 858 return; 859 } 860 861 for (i = 0; i < ef->nreltab; i++) 862 if (ef->reltab[i].rel) 863 free(ef->reltab[i].rel, M_LINKER); 864 for (i = 0; i < ef->nrelatab; i++) 865 if (ef->relatab[i].rela) 866 free(ef->relatab[i].rela, M_LINKER); 867 if (ef->reltab) 868 free(ef->reltab, M_LINKER); 869 if (ef->relatab) 870 free(ef->relatab, M_LINKER); 871 if (ef->progtab) 872 free(ef->progtab, M_LINKER); 873 874 if (ef->object) { 875 vm_map_remove(kernel_map, (vm_offset_t) ef->address, 876 (vm_offset_t) ef->address + 877 (ef->object->size << PAGE_SHIFT)); 878 } 879 if (ef->e_shdr) 880 free(ef->e_shdr, M_LINKER); 881 if (ef->ddbsymtab) 882 free(ef->ddbsymtab, M_LINKER); 883 if (ef->ddbstrtab) 884 free(ef->ddbstrtab, M_LINKER); 885 if (ef->shstrtab) 886 free(ef->shstrtab, M_LINKER); 887 if (ef->ctftab) 888 free(ef->ctftab, M_LINKER); 889 if (ef->ctfoff) 890 free(ef->ctfoff, M_LINKER); 891 if (ef->typoff) 892 free(ef->typoff, M_LINKER); 893 } 894 895 static const char * 896 symbol_name(elf_file_t ef, Elf_Size r_info) 897 { 898 const Elf_Sym *ref; 899 900 if (ELF_R_SYM(r_info)) { 901 ref = ef->ddbsymtab + ELF_R_SYM(r_info); 902 return ef->ddbstrtab + ref->st_name; 903 } else 904 return NULL; 905 } 906 907 static Elf_Addr 908 findbase(elf_file_t ef, int sec) 909 { 910 int i; 911 Elf_Addr base = 0; 912 913 for (i = 0; i < ef->nprogtab; i++) { 914 if (sec == ef->progtab[i].sec) { 915 base = (Elf_Addr)ef->progtab[i].addr; 916 break; 917 } 918 } 919 return base; 920 } 921 922 static int 923 relocate_file(elf_file_t ef) 924 { 925 const Elf_Rel *rellim; 926 const Elf_Rel *rel; 927 const Elf_Rela *relalim; 928 const Elf_Rela *rela; 929 const char *symname; 930 const Elf_Sym *sym; 931 int i; 932 Elf_Size symidx; 933 Elf_Addr base; 934 935 936 /* Perform relocations without addend if there are any: */ 937 for (i = 0; i < ef->nreltab; i++) { 938 rel = ef->reltab[i].rel; 939 if (rel == NULL) 940 panic("lost a reltab!"); 941 rellim = rel + ef->reltab[i].nrel; 942 base = findbase(ef, ef->reltab[i].sec); 943 if (base == 0) 944 panic("lost base for reltab"); 945 for ( ; rel < rellim; rel++) { 946 symidx = ELF_R_SYM(rel->r_info); 947 if (symidx >= ef->ddbsymcnt) 948 continue; 949 sym = ef->ddbsymtab + symidx; 950 /* Local relocs are already done */ 951 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 952 continue; 953 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL, 954 elf_obj_lookup)) { 955 symname = symbol_name(ef, rel->r_info); 956 printf("link_elf_obj: symbol %s undefined\n", 957 symname); 958 return ENOENT; 959 } 960 } 961 } 962 963 /* Perform relocations with addend if there are any: */ 964 for (i = 0; i < ef->nrelatab; i++) { 965 rela = ef->relatab[i].rela; 966 if (rela == NULL) 967 panic("lost a relatab!"); 968 relalim = rela + ef->relatab[i].nrela; 969 base = findbase(ef, ef->relatab[i].sec); 970 if (base == 0) 971 panic("lost base for relatab"); 972 for ( ; rela < relalim; rela++) { 973 symidx = ELF_R_SYM(rela->r_info); 974 if (symidx >= ef->ddbsymcnt) 975 continue; 976 sym = ef->ddbsymtab + symidx; 977 /* Local relocs are already done */ 978 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 979 continue; 980 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA, 981 elf_obj_lookup)) { 982 symname = symbol_name(ef, rela->r_info); 983 printf("link_elf_obj: symbol %s undefined\n", 984 symname); 985 return ENOENT; 986 } 987 } 988 } 989 990 return 0; 991 } 992 993 static int 994 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym) 995 { 996 elf_file_t ef = (elf_file_t) lf; 997 const Elf_Sym *symp; 998 const char *strp; 999 int i; 1000 1001 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1002 strp = ef->ddbstrtab + symp->st_name; 1003 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) { 1004 *sym = (c_linker_sym_t) symp; 1005 return 0; 1006 } 1007 } 1008 return ENOENT; 1009 } 1010 1011 static int 1012 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, 1013 linker_symval_t *symval) 1014 { 1015 elf_file_t ef = (elf_file_t) lf; 1016 const Elf_Sym *es = (const Elf_Sym*) sym; 1017 1018 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) { 1019 symval->name = ef->ddbstrtab + es->st_name; 1020 symval->value = (caddr_t)es->st_value; 1021 symval->size = es->st_size; 1022 return 0; 1023 } 1024 return ENOENT; 1025 } 1026 1027 static int 1028 link_elf_search_symbol(linker_file_t lf, caddr_t value, 1029 c_linker_sym_t *sym, long *diffp) 1030 { 1031 elf_file_t ef = (elf_file_t) lf; 1032 u_long off = (uintptr_t) (void *) value; 1033 u_long diff = off; 1034 u_long st_value; 1035 const Elf_Sym *es; 1036 const Elf_Sym *best = 0; 1037 int i; 1038 1039 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) { 1040 if (es->st_name == 0) 1041 continue; 1042 st_value = es->st_value; 1043 if (off >= st_value) { 1044 if (off - st_value < diff) { 1045 diff = off - st_value; 1046 best = es; 1047 if (diff == 0) 1048 break; 1049 } else if (off - st_value == diff) { 1050 best = es; 1051 } 1052 } 1053 } 1054 if (best == 0) 1055 *diffp = off; 1056 else 1057 *diffp = diff; 1058 *sym = (c_linker_sym_t) best; 1059 1060 return 0; 1061 } 1062 1063 /* 1064 * Look up a linker set on an ELF system. 1065 */ 1066 static int 1067 link_elf_lookup_set(linker_file_t lf, const char *name, 1068 void ***startp, void ***stopp, int *countp) 1069 { 1070 elf_file_t ef = (elf_file_t)lf; 1071 void **start, **stop; 1072 int i, count; 1073 1074 /* Relative to section number */ 1075 for (i = 0; i < ef->nprogtab; i++) { 1076 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) && 1077 strcmp(ef->progtab[i].name + 4, name) == 0) { 1078 start = (void **)ef->progtab[i].addr; 1079 stop = (void **)((char *)ef->progtab[i].addr + 1080 ef->progtab[i].size); 1081 count = stop - start; 1082 if (startp) 1083 *startp = start; 1084 if (stopp) 1085 *stopp = stop; 1086 if (countp) 1087 *countp = count; 1088 return (0); 1089 } 1090 } 1091 return (ESRCH); 1092 } 1093 1094 static int 1095 link_elf_each_function_name(linker_file_t file, 1096 int (*callback)(const char *, void *), void *opaque) 1097 { 1098 elf_file_t ef = (elf_file_t)file; 1099 const Elf_Sym *symp; 1100 int i, error; 1101 1102 /* Exhaustive search */ 1103 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1104 if (symp->st_value != 0 && 1105 ELF_ST_TYPE(symp->st_info) == STT_FUNC) { 1106 error = callback(ef->ddbstrtab + symp->st_name, opaque); 1107 if (error) 1108 return (error); 1109 } 1110 } 1111 return (0); 1112 } 1113 1114 static int 1115 link_elf_each_function_nameval(linker_file_t file, 1116 linker_function_nameval_callback_t callback, void *opaque) 1117 { 1118 linker_symval_t symval; 1119 elf_file_t ef = (elf_file_t)file; 1120 const Elf_Sym* symp; 1121 int i, error; 1122 1123 /* Exhaustive search */ 1124 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1125 if (symp->st_value != 0 && 1126 ELF_ST_TYPE(symp->st_info) == STT_FUNC) { 1127 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval); 1128 if (error) 1129 return (error); 1130 error = callback(file, i, &symval, opaque); 1131 if (error) 1132 return (error); 1133 } 1134 } 1135 return (0); 1136 } 1137 1138 /* 1139 * Symbol lookup function that can be used when the symbol index is known (ie 1140 * in relocations). It uses the symbol index instead of doing a fully fledged 1141 * hash table based lookup when such is valid. For example for local symbols. 1142 * This is not only more efficient, it's also more correct. It's not always 1143 * the case that the symbol can be found through the hash table. 1144 */ 1145 static Elf_Addr 1146 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps) 1147 { 1148 elf_file_t ef = (elf_file_t)lf; 1149 const Elf_Sym *sym; 1150 const char *symbol; 1151 Elf_Addr ret; 1152 1153 /* Don't even try to lookup the symbol if the index is bogus. */ 1154 if (symidx >= ef->ddbsymcnt) 1155 return (0); 1156 1157 sym = ef->ddbsymtab + symidx; 1158 1159 /* Quick answer if there is a definition included. */ 1160 if (sym->st_shndx != SHN_UNDEF) 1161 return (sym->st_value); 1162 1163 /* If we get here, then it is undefined and needs a lookup. */ 1164 switch (ELF_ST_BIND(sym->st_info)) { 1165 case STB_LOCAL: 1166 /* Local, but undefined? huh? */ 1167 return (0); 1168 1169 case STB_GLOBAL: 1170 /* Relative to Data or Function name */ 1171 symbol = ef->ddbstrtab + sym->st_name; 1172 1173 /* Force a lookup failure if the symbol name is bogus. */ 1174 if (*symbol == 0) 1175 return (0); 1176 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps)); 1177 return ret; 1178 1179 case STB_WEAK: 1180 printf("link_elf_obj: Weak symbols not supported\n"); 1181 return (0); 1182 1183 default: 1184 return (0); 1185 } 1186 } 1187 1188 static void 1189 link_elf_fix_link_set(elf_file_t ef) 1190 { 1191 static const char startn[] = "__start_"; 1192 static const char stopn[] = "__stop_"; 1193 Elf_Sym *sym; 1194 const char *sym_name, *linkset_name; 1195 Elf_Addr startp, stopp; 1196 Elf_Size symidx; 1197 int start, i; 1198 1199 startp = stopp = 0; 1200 for (symidx = 1 /* zero entry is special */; 1201 symidx < ef->ddbsymcnt; symidx++) { 1202 sym = ef->ddbsymtab + symidx; 1203 if (sym->st_shndx != SHN_UNDEF) 1204 continue; 1205 1206 sym_name = ef->ddbstrtab + sym->st_name; 1207 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) { 1208 start = 1; 1209 linkset_name = sym_name + sizeof(startn) - 1; 1210 } 1211 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) { 1212 start = 0; 1213 linkset_name = sym_name + sizeof(stopn) - 1; 1214 } 1215 else 1216 continue; 1217 1218 for (i = 0; i < ef->nprogtab; i++) { 1219 if (strcmp(ef->progtab[i].name, linkset_name) == 0) { 1220 startp = (Elf_Addr)ef->progtab[i].addr; 1221 stopp = (Elf_Addr)(startp + ef->progtab[i].size); 1222 break; 1223 } 1224 } 1225 if (i == ef->nprogtab) 1226 continue; 1227 1228 sym->st_value = start ? startp : stopp; 1229 sym->st_shndx = i; 1230 } 1231 } 1232 1233 static void 1234 link_elf_reloc_local(linker_file_t lf) 1235 { 1236 elf_file_t ef = (elf_file_t)lf; 1237 const Elf_Rel *rellim; 1238 const Elf_Rel *rel; 1239 const Elf_Rela *relalim; 1240 const Elf_Rela *rela; 1241 const Elf_Sym *sym; 1242 Elf_Addr base; 1243 int i; 1244 Elf_Size symidx; 1245 1246 link_elf_fix_link_set(ef); 1247 1248 /* Perform relocations without addend if there are any: */ 1249 for (i = 0; i < ef->nreltab; i++) { 1250 rel = ef->reltab[i].rel; 1251 if (rel == NULL) 1252 panic("lost a reltab!"); 1253 rellim = rel + ef->reltab[i].nrel; 1254 base = findbase(ef, ef->reltab[i].sec); 1255 if (base == 0) 1256 panic("lost base for reltab"); 1257 for ( ; rel < rellim; rel++) { 1258 symidx = ELF_R_SYM(rel->r_info); 1259 if (symidx >= ef->ddbsymcnt) 1260 continue; 1261 sym = ef->ddbsymtab + symidx; 1262 /* Only do local relocs */ 1263 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 1264 continue; 1265 elf_reloc_local(lf, base, rel, ELF_RELOC_REL, 1266 elf_obj_lookup); 1267 } 1268 } 1269 1270 /* Perform relocations with addend if there are any: */ 1271 for (i = 0; i < ef->nrelatab; i++) { 1272 rela = ef->relatab[i].rela; 1273 if (rela == NULL) 1274 panic("lost a relatab!"); 1275 relalim = rela + ef->relatab[i].nrela; 1276 base = findbase(ef, ef->relatab[i].sec); 1277 if (base == 0) 1278 panic("lost base for relatab"); 1279 for ( ; rela < relalim; rela++) { 1280 symidx = ELF_R_SYM(rela->r_info); 1281 if (symidx >= ef->ddbsymcnt) 1282 continue; 1283 sym = ef->ddbsymtab + symidx; 1284 /* Only do local relocs */ 1285 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 1286 continue; 1287 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA, 1288 elf_obj_lookup); 1289 } 1290 } 1291 } 1292 1293 static long 1294 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab) 1295 { 1296 elf_file_t ef = (elf_file_t)lf; 1297 1298 *symtab = ef->ddbsymtab; 1299 1300 if (*symtab == NULL) 1301 return (0); 1302 1303 return (ef->ddbsymcnt); 1304 } 1305 1306 static long 1307 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab) 1308 { 1309 elf_file_t ef = (elf_file_t)lf; 1310 1311 *strtab = ef->ddbstrtab; 1312 1313 if (*strtab == NULL) 1314 return (0); 1315 1316 return (ef->ddbstrcnt); 1317 } 1318