1 /*- 2 * Copyright (c) 1998 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 * $FreeBSD: src/sys/kern/link_elf.c,v 1.24 1999/12/24 15:33:36 bde Exp $ 28 * $DragonFly: src/sys/kern/link_elf.c,v 1.29 2008/08/01 23:11:16 dillon Exp $ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/proc.h> 36 #include <sys/nlookup.h> 37 #include <sys/fcntl.h> 38 #include <sys/vnode.h> 39 #include <sys/linker.h> 40 #include <machine/elf.h> 41 42 #include <vm/vm.h> 43 #include <vm/vm_param.h> 44 #include <vm/vm_zone.h> 45 #include <vm/vm_object.h> 46 #include <vm/vm_kern.h> 47 #include <vm/vm_extern.h> 48 #include <sys/lock.h> 49 #include <vm/pmap.h> 50 #include <vm/vm_map.h> 51 52 static int link_elf_obj_preload_file(const char *, linker_file_t *); 53 static int link_elf_obj_preload_finish(linker_file_t); 54 static int link_elf_obj_load_file(const char *, linker_file_t *); 55 static int 56 link_elf_obj_lookup_symbol(linker_file_t, const char *, 57 c_linker_sym_t *); 58 static int link_elf_obj_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t *); 59 static int 60 link_elf_obj_search_symbol(linker_file_t, caddr_t value, 61 c_linker_sym_t * sym, long *diffp); 62 63 static void link_elf_obj_unload_file(linker_file_t); 64 static int 65 link_elf_obj_lookup_set(linker_file_t, const char *, 66 void ***, void ***, int *); 67 static void link_elf_obj_reloc_local(linker_file_t lf); 68 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *); 69 70 static struct linker_class_ops link_elf_obj_class_ops = { 71 link_elf_obj_load_file, 72 link_elf_obj_preload_file, 73 }; 74 75 static struct linker_file_ops link_elf_obj_file_ops = { 76 .lookup_symbol = link_elf_obj_lookup_symbol, 77 .symbol_values = link_elf_obj_symbol_values, 78 .search_symbol = link_elf_obj_search_symbol, 79 .preload_finish = link_elf_obj_preload_finish, 80 .unload = link_elf_obj_unload_file, 81 .lookup_set = link_elf_obj_lookup_set, 82 }; 83 84 typedef struct { 85 void *addr; 86 Elf_Off size; 87 int flags; 88 int sec; /* Original section */ 89 char *name; 90 } Elf_progent; 91 92 typedef struct { 93 Elf_Rel *rel; 94 int nrel; 95 int sec; 96 } Elf_relent; 97 98 typedef struct { 99 Elf_Rela *rela; 100 int nrela; 101 int sec; 102 } Elf_relaent; 103 104 105 typedef struct elf_file { 106 int preloaded; 107 108 caddr_t address; /* Relocation address */ 109 size_t bytes; /* Chunk size in bytes */ 110 vm_object_t object; /* VM object to hold file pages */ 111 Elf_Shdr *e_shdr; 112 113 Elf_progent *progtab; 114 int nprogtab; 115 116 Elf_relaent *relatab; 117 int nrelatab; 118 119 Elf_relent *reltab; 120 int nreltab; 121 122 Elf_Sym *ddbsymtab; /* The symbol table we are using */ 123 long ddbsymcnt; /* Number of symbols */ 124 caddr_t ddbstrtab; /* String table */ 125 long ddbstrcnt; /* number of bytes in string table */ 126 127 caddr_t shstrtab; /* Section name string table */ 128 long shstrcnt; /* number of bytes in string table */ 129 130 caddr_t ctftab; /* CTF table */ 131 long ctfcnt; /* number of bytes in CTF table */ 132 caddr_t ctfoff; /* CTF offset table */ 133 caddr_t typoff; /* Type offset table */ 134 long typlen; /* Number of type entries. */ 135 136 } *elf_file_t; 137 138 static int relocate_file(linker_file_t lf); 139 140 /* 141 * The kernel symbol table starts here. 142 */ 143 extern struct _dynamic _DYNAMIC; 144 145 static void 146 link_elf_obj_init(void *arg) 147 { 148 #if ELF_TARG_CLASS == ELFCLASS32 149 linker_add_class("elf32", NULL, &link_elf_obj_class_ops); 150 #else 151 linker_add_class("elf64", NULL, &link_elf_obj_class_ops); 152 #endif 153 } 154 155 SYSINIT(link_elf, SI_BOOT2_KLD, SI_ORDER_SECOND, link_elf_obj_init, 0); 156 157 static void 158 link_elf_obj_error(const char *file, const char *s) 159 { 160 kprintf("kldload: %s: %s\n", file, s); 161 } 162 163 static int 164 link_elf_obj_preload_file(const char *filename, linker_file_t *result) 165 { 166 Elf_Ehdr *hdr; 167 Elf_Shdr *shdr; 168 Elf_Sym *es; 169 caddr_t modptr, baseptr, sizeptr; 170 char *type; 171 elf_file_t ef; 172 linker_file_t lf; 173 Elf_Addr off; 174 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex; 175 176 /* 177 * Look to see if we have the module preloaded. 178 */ 179 modptr = preload_search_by_name(filename); 180 if (modptr == NULL) 181 return ENOENT; 182 183 /* It's preloaded, check we can handle it and collect information */ 184 type = (char *)preload_search_info(modptr, MODINFO_TYPE); 185 baseptr = preload_search_info(modptr, MODINFO_ADDR); 186 sizeptr = preload_search_info(modptr, MODINFO_SIZE); 187 hdr = (Elf_Ehdr *) preload_search_info(modptr, MODINFO_METADATA | 188 MODINFOMD_ELFHDR); 189 shdr = (Elf_Shdr *) preload_search_info(modptr, MODINFO_METADATA | 190 MODINFOMD_SHDR); 191 if (type == NULL || 192 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " obj module") != 0 && 193 strcmp(type, "elf obj module") != 0)) { 194 return (EFTYPE); 195 } 196 if (baseptr == NULL || sizeptr == NULL || hdr == NULL || shdr == NULL) 197 return (EINVAL); 198 199 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO); 200 ef->preloaded = 1; 201 ef->address = *(caddr_t *) baseptr; 202 ef->bytes = 0; 203 lf = linker_make_file(filename, ef, &link_elf_obj_file_ops); 204 if (lf == NULL) { 205 kfree(ef, M_LINKER); 206 return ENOMEM; 207 } 208 lf->address = ef->address; 209 lf->size = *(size_t *) sizeptr; 210 211 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 212 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 213 hdr->e_ident[EI_VERSION] != EV_CURRENT || 214 hdr->e_version != EV_CURRENT || 215 hdr->e_type != ET_REL || 216 hdr->e_machine != ELF_TARG_MACH) { 217 error = EFTYPE; 218 goto out; 219 } 220 ef->e_shdr = shdr; 221 222 /* Scan the section header for information and table sizing. */ 223 symtabindex = -1; 224 symstrindex = -1; 225 for (i = 0; i < hdr->e_shnum; i++) { 226 switch (shdr[i].sh_type) { 227 case SHT_PROGBITS: 228 case SHT_NOBITS: 229 ef->nprogtab++; 230 break; 231 case SHT_SYMTAB: 232 symtabindex = i; 233 symstrindex = shdr[i].sh_link; 234 break; 235 case SHT_REL: 236 ef->nreltab++; 237 break; 238 case SHT_RELA: 239 ef->nrelatab++; 240 break; 241 } 242 } 243 244 shstrindex = hdr->e_shstrndx; 245 if (ef->nprogtab == 0 || symstrindex < 0 || 246 symstrindex >= hdr->e_shnum || 247 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 || 248 shstrindex >= hdr->e_shnum || 249 shdr[shstrindex].sh_type != SHT_STRTAB) { 250 error = ENOEXEC; 251 goto out; 252 } 253 /* Allocate space for tracking the load chunks */ 254 if (ef->nprogtab != 0) 255 ef->progtab = kmalloc(ef->nprogtab * sizeof(*ef->progtab), 256 M_LINKER, M_WAITOK | M_ZERO); 257 if (ef->nreltab != 0) 258 ef->reltab = kmalloc(ef->nreltab * sizeof(*ef->reltab), 259 M_LINKER, M_WAITOK | M_ZERO); 260 if (ef->nrelatab != 0) 261 ef->relatab = kmalloc(ef->nrelatab * sizeof(*ef->relatab), 262 M_LINKER, M_WAITOK | M_ZERO); 263 if ((ef->nprogtab != 0 && ef->progtab == NULL) || 264 (ef->nreltab != 0 && ef->reltab == NULL) || 265 (ef->nrelatab != 0 && ef->relatab == NULL)) { 266 error = ENOMEM; 267 goto out; 268 } 269 /* XXX, relocate the sh_addr fields saved by the loader. */ 270 off = 0; 271 for (i = 0; i < hdr->e_shnum; i++) { 272 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off)) 273 off = shdr[i].sh_addr; 274 } 275 for (i = 0; i < hdr->e_shnum; i++) { 276 if (shdr[i].sh_addr != 0) 277 shdr[i].sh_addr = shdr[i].sh_addr - off + 278 (Elf_Addr) ef->address; 279 } 280 281 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 282 ef->ddbsymtab = (Elf_Sym *) shdr[symtabindex].sh_addr; 283 ef->ddbstrcnt = shdr[symstrindex].sh_size; 284 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr; 285 ef->shstrcnt = shdr[shstrindex].sh_size; 286 ef->shstrtab = (char *)shdr[shstrindex].sh_addr; 287 288 /* Now fill out progtab and the relocation tables. */ 289 pb = 0; 290 rl = 0; 291 ra = 0; 292 for (i = 0; i < hdr->e_shnum; i++) { 293 switch (shdr[i].sh_type) { 294 case SHT_PROGBITS: 295 case SHT_NOBITS: 296 ef->progtab[pb].addr = (void *)shdr[i].sh_addr; 297 if (shdr[i].sh_type == SHT_PROGBITS) 298 ef->progtab[pb].name = "<<PROGBITS>>"; 299 else 300 ef->progtab[pb].name = "<<NOBITS>>"; 301 ef->progtab[pb].size = shdr[i].sh_size; 302 ef->progtab[pb].sec = i; 303 if (ef->shstrtab && shdr[i].sh_name != 0) 304 ef->progtab[pb].name = 305 ef->shstrtab + shdr[i].sh_name; 306 #if 0 307 if (ef->progtab[pb].name != NULL && 308 !strcmp(ef->progtab[pb].name, "set_pcpu")) { 309 void *dpcpu; 310 311 dpcpu = dpcpu_alloc(shdr[i].sh_size); 312 if (dpcpu == NULL) { 313 error = ENOSPC; 314 goto out; 315 } 316 memcpy(dpcpu, ef->progtab[pb].addr, 317 ef->progtab[pb].size); 318 dpcpu_copy(dpcpu, shdr[i].sh_size); 319 ef->progtab[pb].addr = dpcpu; 320 #ifdef VIMAGE 321 } else if (ef->progtab[pb].name != NULL && 322 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) { 323 void *vnet_data; 324 325 vnet_data = vnet_data_alloc(shdr[i].sh_size); 326 if (vnet_data == NULL) { 327 error = ENOSPC; 328 goto out; 329 } 330 memcpy(vnet_data, ef->progtab[pb].addr, 331 ef->progtab[pb].size); 332 vnet_data_copy(vnet_data, shdr[i].sh_size); 333 ef->progtab[pb].addr = vnet_data; 334 #endif 335 } 336 #endif 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_obj_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_obj_preload_finish(linker_file_t lf) 382 { 383 int error; 384 385 error = relocate_file(lf); 386 387 return (error); 388 } 389 390 static int 391 link_elf_obj_load_file(const char *filename, linker_file_t * result) 392 { 393 struct nlookupdata nd; 394 struct thread *td = curthread; /* XXX */ 395 struct proc *p = td->td_proc; 396 char *pathname; 397 struct vnode *vp; 398 Elf_Ehdr *hdr; 399 Elf_Shdr *shdr; 400 Elf_Sym *es; 401 int nbytes, i, j; 402 vm_offset_t mapbase; 403 size_t mapsize; 404 int error = 0; 405 int resid; 406 elf_file_t ef; 407 linker_file_t lf; 408 int symtabindex; 409 int symstrindex; 410 int shstrindex; 411 int nsym; 412 int pb, rl, ra; 413 int alignmask; 414 415 /* XXX Hack for firmware loading where p == NULL */ 416 if (p == NULL) { 417 p = &proc0; 418 } 419 420 KKASSERT(p != NULL); 421 if (p->p_ucred == NULL) { 422 kprintf("link_elf_obj_load_file: cannot load '%s' from filesystem" 423 " this early\n", filename); 424 return ENOENT; 425 } 426 shdr = NULL; 427 lf = NULL; 428 mapsize = 0; 429 hdr = NULL; 430 pathname = linker_search_path(filename); 431 if (pathname == NULL) 432 return ENOENT; 433 434 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW | NLC_LOCKVP); 435 if (error == 0) 436 error = vn_open(&nd, NULL, FREAD, 0); 437 kfree(pathname, M_LINKER); 438 if (error) { 439 nlookup_done(&nd); 440 return error; 441 } 442 vp = nd.nl_open_vp; 443 nd.nl_open_vp = NULL; 444 nlookup_done(&nd); 445 446 /* 447 * Read the elf header from the file. 448 */ 449 hdr = kmalloc(sizeof(*hdr), M_LINKER, M_WAITOK); 450 if (hdr == NULL) { 451 error = ENOMEM; 452 goto out; 453 } 454 error = vn_rdwr(UIO_READ, vp, (void *)hdr, sizeof(*hdr), 0, 455 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 456 if (error) 457 goto out; 458 if (resid != 0) { 459 error = ENOEXEC; 460 goto out; 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_obj_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_obj_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_obj_error(filename, "Unsupported machine"); 485 error = ENOEXEC; 486 goto out; 487 } 488 489 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO); 490 lf = linker_make_file(filename, ef, &link_elf_obj_file_ops); 491 if (lf == NULL) { 492 kfree(ef, M_LINKER); 493 error = ENOMEM; 494 goto out; 495 } 496 ef->nprogtab = 0; 497 ef->e_shdr = 0; 498 ef->nreltab = 0; 499 ef->nrelatab = 0; 500 501 /* Allocate and read in the section header */ 502 nbytes = hdr->e_shnum * hdr->e_shentsize; 503 if (nbytes == 0 || hdr->e_shoff == 0 || 504 hdr->e_shentsize != sizeof(Elf_Shdr)) { 505 error = ENOEXEC; 506 goto out; 507 } 508 shdr = kmalloc(nbytes, M_LINKER, M_WAITOK); 509 if (shdr == NULL) { 510 error = ENOMEM; 511 goto out; 512 } 513 ef->e_shdr = shdr; 514 error = vn_rdwr(UIO_READ, vp, (caddr_t) shdr, nbytes, hdr->e_shoff, 515 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 516 if (error) 517 goto out; 518 if (resid) { 519 error = ENOEXEC; 520 goto out; 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_obj_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_obj_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_obj_error(filename, "file has invalid symbol strings"); 561 error = ENOEXEC; 562 goto out; 563 } 564 /* Allocate space for tracking the load chunks */ 565 if (ef->nprogtab != 0) 566 ef->progtab = kmalloc(ef->nprogtab * sizeof(*ef->progtab), 567 M_LINKER, M_WAITOK | M_ZERO); 568 if (ef->nreltab != 0) 569 ef->reltab = kmalloc(ef->nreltab * sizeof(*ef->reltab), 570 M_LINKER, M_WAITOK | M_ZERO); 571 if (ef->nrelatab != 0) 572 ef->relatab = kmalloc(ef->nrelatab * sizeof(*ef->relatab), 573 M_LINKER, M_WAITOK | M_ZERO); 574 if ((ef->nprogtab != 0 && ef->progtab == NULL) || 575 (ef->nreltab != 0 && ef->reltab == NULL) || 576 (ef->nrelatab != 0 && ef->relatab == NULL)) { 577 error = ENOMEM; 578 goto out; 579 } 580 if (symtabindex == -1) 581 panic("lost symbol table index"); 582 /* Allocate space for and load the symbol table */ 583 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 584 ef->ddbsymtab = kmalloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK); 585 if (ef->ddbsymtab == NULL) { 586 error = ENOMEM; 587 goto out; 588 } 589 error = vn_rdwr(UIO_READ, vp, (void *)ef->ddbsymtab, 590 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset, 591 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 592 if (error) 593 goto out; 594 if (resid != 0) { 595 error = EINVAL; 596 goto out; 597 } 598 if (symstrindex == -1) 599 panic("lost symbol string index"); 600 /* Allocate space for and load the symbol strings */ 601 ef->ddbstrcnt = shdr[symstrindex].sh_size; 602 ef->ddbstrtab = kmalloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK); 603 if (ef->ddbstrtab == NULL) { 604 error = ENOMEM; 605 goto out; 606 } 607 error = vn_rdwr(UIO_READ, vp, ef->ddbstrtab, 608 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset, 609 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 610 if (error) 611 goto out; 612 if (resid != 0) { 613 error = EINVAL; 614 goto out; 615 } 616 /* Do we have a string table for the section names? */ 617 shstrindex = -1; 618 if (hdr->e_shstrndx != 0 && 619 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) { 620 shstrindex = hdr->e_shstrndx; 621 ef->shstrcnt = shdr[shstrindex].sh_size; 622 ef->shstrtab = kmalloc(shdr[shstrindex].sh_size, M_LINKER, 623 M_WAITOK); 624 if (ef->shstrtab == NULL) { 625 error = ENOMEM; 626 goto out; 627 } 628 error = vn_rdwr(UIO_READ, vp, ef->shstrtab, 629 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset, 630 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 631 if (error) 632 goto out; 633 if (resid != 0) { 634 error = EINVAL; 635 goto out; 636 } 637 } 638 /* Size up code/data(progbits) and bss(nobits). */ 639 alignmask = 0; 640 for (i = 0; i < hdr->e_shnum; i++) { 641 switch (shdr[i].sh_type) { 642 case SHT_PROGBITS: 643 case SHT_NOBITS: 644 alignmask = shdr[i].sh_addralign - 1; 645 mapsize += alignmask; 646 mapsize &= ~alignmask; 647 mapsize += shdr[i].sh_size; 648 break; 649 } 650 } 651 652 /* 653 * We know how much space we need for the text/data/bss/etc. This 654 * stuff needs to be in a single chunk so that profiling etc can get 655 * the bounds and gdb can associate offsets with modules 656 */ 657 ef->object = vm_object_allocate(OBJT_DEFAULT, 658 round_page(mapsize) >> PAGE_SHIFT); 659 if (ef->object == NULL) { 660 error = ENOMEM; 661 goto out; 662 } 663 vm_object_reference(ef->object); 664 ef->address = (caddr_t) vm_map_min(&kernel_map); 665 ef->bytes = 0; 666 667 /* 668 * In order to satisfy x86_64's architectural requirements on the 669 * location of code and data in the kernel's address space, request a 670 * mapping that is above the kernel. 671 * 672 * vkernel64's text+data is outside the managed VM space entirely. 673 */ 674 #if defined(__amd64__) && defined(_KERNEL_VIRTUAL) 675 error = vkernel_module_memory_alloc(&mapbase, round_page(mapsize)); 676 #else 677 mapbase = KERNBASE; 678 error = vm_map_find(&kernel_map, ef->object, 0, &mapbase, 679 round_page(mapsize), PAGE_SIZE, 680 TRUE, VM_MAPTYPE_NORMAL, 681 VM_PROT_ALL, VM_PROT_ALL, FALSE); 682 if (error) { 683 vm_object_deallocate(ef->object); 684 ef->object = NULL; 685 goto out; 686 } 687 /* Wire the pages */ 688 error = vm_map_wire(&kernel_map, mapbase, 689 mapbase + round_page(mapsize), 0); 690 #endif 691 if (error != KERN_SUCCESS) { 692 error = ENOMEM; 693 goto out; 694 } 695 /* Inform the kld system about the situation */ 696 lf->address = ef->address = (caddr_t) mapbase; 697 lf->size = round_page(mapsize); 698 ef->bytes = 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 if (ef->shstrtab && shdr[i].sh_name != 0) 716 ef->progtab[pb].name = 717 ef->shstrtab + shdr[i].sh_name; 718 else if (shdr[i].sh_type == SHT_PROGBITS) 719 ef->progtab[pb].name = "<<PROGBITS>>"; 720 else 721 ef->progtab[pb].name = "<<NOBITS>>"; 722 #if 0 723 if (ef->progtab[pb].name != NULL && 724 !strcmp(ef->progtab[pb].name, "set_pcpu")) 725 ef->progtab[pb].addr = 726 dpcpu_alloc(shdr[i].sh_size); 727 #ifdef VIMAGE 728 else if (ef->progtab[pb].name != NULL && 729 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) 730 ef->progtab[pb].addr = 731 vnet_data_alloc(shdr[i].sh_size); 732 #endif 733 else 734 #endif 735 ef->progtab[pb].addr = 736 (void *)(uintptr_t) mapbase; 737 if (ef->progtab[pb].addr == NULL) { 738 error = ENOSPC; 739 goto out; 740 } 741 ef->progtab[pb].size = shdr[i].sh_size; 742 ef->progtab[pb].sec = i; 743 if (shdr[i].sh_type == SHT_PROGBITS) { 744 error = vn_rdwr(UIO_READ, vp, 745 ef->progtab[pb].addr, 746 shdr[i].sh_size, shdr[i].sh_offset, 747 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, 748 &resid); 749 if (error) 750 goto out; 751 if (resid != 0) { 752 error = EINVAL; 753 goto out; 754 } 755 #if 0 756 /* Initialize the per-cpu or vnet area. */ 757 if (ef->progtab[pb].addr != (void *)mapbase && 758 !strcmp(ef->progtab[pb].name, "set_pcpu")) 759 dpcpu_copy(ef->progtab[pb].addr, 760 shdr[i].sh_size); 761 #ifdef VIMAGE 762 else if (ef->progtab[pb].addr != 763 (void *)mapbase && 764 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) 765 vnet_data_copy(ef->progtab[pb].addr, 766 shdr[i].sh_size); 767 #endif 768 #endif 769 } else 770 bzero(ef->progtab[pb].addr, shdr[i].sh_size); 771 772 /* Update all symbol values with the offset. */ 773 for (j = 0; j < ef->ddbsymcnt; j++) { 774 es = &ef->ddbsymtab[j]; 775 if (es->st_shndx != i) 776 continue; 777 es->st_value += (Elf_Addr) ef->progtab[pb].addr; 778 } 779 mapbase += shdr[i].sh_size; 780 pb++; 781 break; 782 case SHT_REL: 783 ef->reltab[rl].rel = kmalloc(shdr[i].sh_size, M_LINKER, M_WAITOK); 784 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel); 785 ef->reltab[rl].sec = shdr[i].sh_info; 786 error = vn_rdwr(UIO_READ, vp, 787 (void *)ef->reltab[rl].rel, 788 shdr[i].sh_size, shdr[i].sh_offset, 789 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 790 if (error) 791 goto out; 792 if (resid != 0) { 793 error = EINVAL; 794 goto out; 795 } 796 rl++; 797 break; 798 case SHT_RELA: 799 ef->relatab[ra].rela = kmalloc(shdr[i].sh_size, M_LINKER, M_WAITOK); 800 ef->relatab[ra].nrela = shdr[i].sh_size / sizeof(Elf_Rela); 801 ef->relatab[ra].sec = shdr[i].sh_info; 802 error = vn_rdwr(UIO_READ, vp, 803 (void *)ef->relatab[ra].rela, 804 shdr[i].sh_size, shdr[i].sh_offset, 805 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid); 806 if (error) 807 goto out; 808 if (resid != 0) { 809 error = EINVAL; 810 goto out; 811 } 812 ra++; 813 break; 814 } 815 } 816 if (pb != ef->nprogtab) 817 panic("lost progbits"); 818 if (rl != ef->nreltab) 819 panic("lost reltab"); 820 if (ra != ef->nrelatab) 821 panic("lost relatab"); 822 if (mapbase != (vm_offset_t) ef->address + mapsize) 823 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n", 824 mapbase, ef->address, mapsize, 825 (vm_offset_t) ef->address + mapsize); 826 827 /* Local intra-module relocations */ 828 link_elf_obj_reloc_local(lf); 829 830 /* Pull in dependencies */ 831 error = linker_load_dependencies(lf); 832 if (error) 833 goto out; 834 835 /* External relocations */ 836 error = relocate_file(lf); 837 if (error) 838 goto out; 839 840 *result = lf; 841 842 out: 843 if (error && lf) 844 linker_file_unload(lf /*, LINKER_UNLOAD_FORCE */); 845 if (hdr) 846 kfree(hdr, M_LINKER); 847 vn_unlock(vp); 848 vn_close(vp, FREAD); 849 850 return error; 851 } 852 853 static void 854 link_elf_obj_unload_file(linker_file_t file) 855 { 856 elf_file_t ef = file->priv; 857 int i; 858 859 if (ef->progtab) { 860 for (i = 0; i < ef->nprogtab; i++) { 861 if (ef->progtab[i].size == 0) 862 continue; 863 if (ef->progtab[i].name == NULL) 864 continue; 865 #if 0 866 if (!strcmp(ef->progtab[i].name, "set_pcpu")) 867 dpcpu_free(ef->progtab[i].addr, 868 ef->progtab[i].size); 869 #ifdef VIMAGE 870 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME)) 871 vnet_data_free(ef->progtab[i].addr, 872 ef->progtab[i].size); 873 #endif 874 #endif 875 } 876 } 877 if (ef->preloaded) { 878 if (ef->reltab) 879 kfree(ef->reltab, M_LINKER); 880 if (ef->relatab) 881 kfree(ef->relatab, M_LINKER); 882 if (ef->progtab) 883 kfree(ef->progtab, M_LINKER); 884 if (ef->ctftab) 885 kfree(ef->ctftab, M_LINKER); 886 if (ef->ctfoff) 887 kfree(ef->ctfoff, M_LINKER); 888 if (ef->typoff) 889 kfree(ef->typoff, M_LINKER); 890 if (file->filename != NULL) 891 preload_delete_name(file->filename); 892 kfree(ef, M_LINKER); 893 /* XXX reclaim module memory? */ 894 return; 895 } 896 897 for (i = 0; i < ef->nreltab; i++) 898 if (ef->reltab[i].rel) 899 kfree(ef->reltab[i].rel, M_LINKER); 900 for (i = 0; i < ef->nrelatab; i++) 901 if (ef->relatab[i].rela) 902 kfree(ef->relatab[i].rela, M_LINKER); 903 if (ef->reltab) 904 kfree(ef->reltab, M_LINKER); 905 if (ef->relatab) 906 kfree(ef->relatab, M_LINKER); 907 if (ef->progtab) 908 kfree(ef->progtab, M_LINKER); 909 910 if (ef->object) { 911 #if defined(__amd64__) && defined(_KERNEL_VIRTUAL) 912 vkernel_module_memory_free((vm_offset_t)ef->address, ef->bytes); 913 #else 914 vm_map_remove(&kernel_map, (vm_offset_t) ef->address, 915 (vm_offset_t) ef->address + 916 (ef->object->size << PAGE_SHIFT)); 917 #endif 918 vm_object_deallocate(ef->object); 919 ef->object = NULL; 920 } 921 if (ef->e_shdr) 922 kfree(ef->e_shdr, M_LINKER); 923 if (ef->ddbsymtab) 924 kfree(ef->ddbsymtab, M_LINKER); 925 if (ef->ddbstrtab) 926 kfree(ef->ddbstrtab, M_LINKER); 927 if (ef->shstrtab) 928 kfree(ef->shstrtab, M_LINKER); 929 if (ef->ctftab) 930 kfree(ef->ctftab, M_LINKER); 931 if (ef->ctfoff) 932 kfree(ef->ctfoff, M_LINKER); 933 if (ef->typoff) 934 kfree(ef->typoff, M_LINKER); 935 kfree(ef, M_LINKER); 936 } 937 938 static const char * 939 symbol_name(elf_file_t ef, Elf_Size r_info) 940 { 941 const Elf_Sym *ref; 942 943 if (ELF_R_SYM(r_info)) { 944 ref = ef->ddbsymtab + ELF_R_SYM(r_info); 945 return ef->ddbstrtab + ref->st_name; 946 } else 947 return NULL; 948 } 949 950 static Elf_Addr 951 findbase(elf_file_t ef, int sec) 952 { 953 int i; 954 Elf_Addr base = 0; 955 956 for (i = 0; i < ef->nprogtab; i++) { 957 if (sec == ef->progtab[i].sec) { 958 base = (Elf_Addr)ef->progtab[i].addr; 959 break; 960 } 961 } 962 return base; 963 } 964 965 static int 966 relocate_file(linker_file_t lf) 967 { 968 elf_file_t ef = lf->priv; 969 const Elf_Rel *rellim; 970 const Elf_Rel *rel; 971 const Elf_Rela *relalim; 972 const Elf_Rela *rela; 973 const char *symname; 974 const Elf_Sym *sym; 975 int i; 976 Elf_Size symidx; 977 Elf_Addr base; 978 979 /* Perform relocations without addend if there are any: */ 980 for (i = 0; i < ef->nreltab; i++) { 981 rel = ef->reltab[i].rel; 982 if (rel == NULL) 983 panic("lost a reltab!"); 984 rellim = rel + ef->reltab[i].nrel; 985 base = findbase(ef, ef->reltab[i].sec); 986 if (base == 0) 987 panic("lost base for reltab"); 988 for ( ; rel < rellim; rel++) { 989 symidx = ELF_R_SYM(rel->r_info); 990 if (symidx >= ef->ddbsymcnt) 991 continue; 992 sym = ef->ddbsymtab + symidx; 993 /* Local relocs are already done */ 994 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 995 continue; 996 if (elf_reloc(lf, base, rel, ELF_RELOC_REL, 997 elf_obj_lookup)) { 998 symname = symbol_name(ef, rel->r_info); 999 kprintf("link_elf_obj_obj: symbol %s undefined\n", 1000 symname); 1001 return ENOENT; 1002 } 1003 } 1004 } 1005 1006 /* Perform relocations with addend if there are any: */ 1007 for (i = 0; i < ef->nrelatab; i++) { 1008 rela = ef->relatab[i].rela; 1009 if (rela == NULL) 1010 panic("lost a relatab!"); 1011 relalim = rela + ef->relatab[i].nrela; 1012 base = findbase(ef, ef->relatab[i].sec); 1013 if (base == 0) 1014 panic("lost base for relatab"); 1015 for ( ; rela < relalim; rela++) { 1016 symidx = ELF_R_SYM(rela->r_info); 1017 if (symidx >= ef->ddbsymcnt) 1018 continue; 1019 sym = ef->ddbsymtab + symidx; 1020 /* Local relocs are already done */ 1021 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 1022 continue; 1023 if (elf_reloc(lf, base, rela, ELF_RELOC_RELA, 1024 elf_obj_lookup)) { 1025 symname = symbol_name(ef, rela->r_info); 1026 kprintf("link_elf_obj_obj: symbol %s undefined\n", 1027 symname); 1028 return ENOENT; 1029 } 1030 } 1031 } 1032 1033 return 0; 1034 } 1035 1036 static int 1037 link_elf_obj_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym) 1038 { 1039 elf_file_t ef = lf->priv; 1040 const Elf_Sym *symp; 1041 const char *strp; 1042 int i; 1043 1044 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) { 1045 strp = ef->ddbstrtab + symp->st_name; 1046 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) { 1047 *sym = (c_linker_sym_t) symp; 1048 return 0; 1049 } 1050 } 1051 return ENOENT; 1052 } 1053 1054 static int 1055 link_elf_obj_symbol_values(linker_file_t lf, c_linker_sym_t sym, 1056 linker_symval_t *symval) 1057 { 1058 elf_file_t ef = lf->priv; 1059 const Elf_Sym *es = (const Elf_Sym*) sym; 1060 1061 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) { 1062 symval->name = ef->ddbstrtab + es->st_name; 1063 symval->value = (caddr_t)es->st_value; 1064 symval->size = es->st_size; 1065 return 0; 1066 } 1067 return ENOENT; 1068 } 1069 1070 static int 1071 link_elf_obj_search_symbol(linker_file_t lf, caddr_t value, 1072 c_linker_sym_t *sym, long *diffp) 1073 { 1074 elf_file_t ef = lf->priv; 1075 u_long off = (uintptr_t) (void *) value; 1076 u_long diff = off; 1077 u_long st_value; 1078 const Elf_Sym *es; 1079 const Elf_Sym *best = 0; 1080 int i; 1081 1082 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) { 1083 if (es->st_name == 0) 1084 continue; 1085 st_value = es->st_value; 1086 if (off >= st_value) { 1087 if (off - st_value < diff) { 1088 diff = off - st_value; 1089 best = es; 1090 if (diff == 0) 1091 break; 1092 } else if (off - st_value == diff) { 1093 best = es; 1094 } 1095 } 1096 } 1097 if (best == 0) 1098 *diffp = off; 1099 else 1100 *diffp = diff; 1101 *sym = (c_linker_sym_t) best; 1102 1103 return 0; 1104 } 1105 1106 /* 1107 * Look up a linker set on an ELF system. 1108 */ 1109 static int 1110 link_elf_obj_lookup_set(linker_file_t lf, const char *name, 1111 void ***startp, void ***stopp, int *countp) 1112 { 1113 elf_file_t ef = lf->priv; 1114 void **start, **stop; 1115 int i, count; 1116 1117 /* Relative to section number */ 1118 for (i = 0; i < ef->nprogtab; i++) { 1119 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) && 1120 strcmp(ef->progtab[i].name + 4, name) == 0) { 1121 start = (void **)ef->progtab[i].addr; 1122 stop = (void **)((char *)ef->progtab[i].addr + 1123 ef->progtab[i].size); 1124 count = stop - start; 1125 if (startp) 1126 *startp = start; 1127 if (stopp) 1128 *stopp = stop; 1129 if (countp) 1130 *countp = count; 1131 return (0); 1132 } 1133 } 1134 return (ESRCH); 1135 } 1136 1137 /* 1138 * Symbol lookup function that can be used when the symbol index is known (ie 1139 * in relocations). It uses the symbol index instead of doing a fully fledged 1140 * hash table based lookup when such is valid. For example for local symbols. 1141 * This is not only more efficient, it's also more correct. It's not always 1142 * the case that the symbol can be found through the hash table. 1143 */ 1144 static int 1145 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *result) 1146 { 1147 elf_file_t ef = lf->priv; 1148 const Elf_Sym *sym; 1149 const char *symbol; 1150 1151 /* Don't even try to lookup the symbol if the index is bogus. */ 1152 if (symidx >= ef->ddbsymcnt) 1153 return (ENOENT); 1154 1155 sym = ef->ddbsymtab + symidx; 1156 1157 /* Quick answer if there is a definition included. */ 1158 if (sym->st_shndx != SHN_UNDEF) { 1159 *result = sym->st_value; 1160 return (0); 1161 } 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 (ENOENT); 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 (ENOENT); 1176 return (linker_file_lookup_symbol(lf, symbol, deps, (caddr_t *)result)); 1177 1178 case STB_WEAK: 1179 kprintf("link_elf_obj_obj: Weak symbols not supported\n"); 1180 return (ENOENT); 1181 1182 default: 1183 return (ENOENT); 1184 } 1185 } 1186 1187 static void 1188 link_elf_obj_fix_link_set(elf_file_t ef) 1189 { 1190 static const char startn[] = "__start_"; 1191 static const char stopn[] = "__stop_"; 1192 Elf_Sym *sym; 1193 const char *sym_name, *linkset_name; 1194 Elf_Addr startp, stopp; 1195 Elf_Size symidx; 1196 int start, i; 1197 1198 startp = stopp = 0; 1199 for (symidx = 1 /* zero entry is special */; 1200 symidx < ef->ddbsymcnt; symidx++) { 1201 sym = ef->ddbsymtab + symidx; 1202 if (sym->st_shndx != SHN_UNDEF) 1203 continue; 1204 1205 sym_name = ef->ddbstrtab + sym->st_name; 1206 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) { 1207 start = 1; 1208 linkset_name = sym_name + sizeof(startn) - 1; 1209 } 1210 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) { 1211 start = 0; 1212 linkset_name = sym_name + sizeof(stopn) - 1; 1213 } 1214 else 1215 continue; 1216 1217 for (i = 0; i < ef->nprogtab; i++) { 1218 if (strcmp(ef->progtab[i].name, linkset_name) == 0) { 1219 startp = (Elf_Addr)ef->progtab[i].addr; 1220 stopp = (Elf_Addr)(startp + ef->progtab[i].size); 1221 break; 1222 } 1223 } 1224 if (i == ef->nprogtab) 1225 continue; 1226 1227 sym->st_value = start ? startp : stopp; 1228 sym->st_shndx = i; 1229 } 1230 } 1231 1232 static void 1233 link_elf_obj_reloc_local(linker_file_t lf) 1234 { 1235 elf_file_t ef = lf->priv; 1236 const Elf_Rel *rellim; 1237 const Elf_Rel *rel; 1238 const Elf_Rela *relalim; 1239 const Elf_Rela *rela; 1240 const Elf_Sym *sym; 1241 Elf_Addr base; 1242 int i; 1243 Elf_Size symidx; 1244 1245 link_elf_obj_fix_link_set(ef); 1246 1247 /* Perform relocations without addend if there are any: */ 1248 for (i = 0; i < ef->nreltab; i++) { 1249 rel = ef->reltab[i].rel; 1250 if (rel == NULL) 1251 panic("lost a reltab!"); 1252 rellim = rel + ef->reltab[i].nrel; 1253 base = findbase(ef, ef->reltab[i].sec); 1254 if (base == 0) 1255 panic("lost base for reltab"); 1256 for ( ; rel < rellim; rel++) { 1257 symidx = ELF_R_SYM(rel->r_info); 1258 if (symidx >= ef->ddbsymcnt) 1259 continue; 1260 sym = ef->ddbsymtab + symidx; 1261 /* Only do local relocs */ 1262 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 1263 continue; 1264 elf_reloc_local(lf, base, rel, ELF_RELOC_REL, 1265 elf_obj_lookup); 1266 } 1267 } 1268 1269 /* Perform relocations with addend if there are any: */ 1270 for (i = 0; i < ef->nrelatab; i++) { 1271 rela = ef->relatab[i].rela; 1272 if (rela == NULL) 1273 panic("lost a relatab!"); 1274 relalim = rela + ef->relatab[i].nrela; 1275 base = findbase(ef, ef->relatab[i].sec); 1276 if (base == 0) 1277 panic("lost base for relatab"); 1278 for ( ; rela < relalim; rela++) { 1279 symidx = ELF_R_SYM(rela->r_info); 1280 if (symidx >= ef->ddbsymcnt) 1281 continue; 1282 sym = ef->ddbsymtab + symidx; 1283 /* Only do local relocs */ 1284 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 1285 continue; 1286 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA, 1287 elf_obj_lookup); 1288 } 1289 } 1290 } 1291