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