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