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