1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Kernel's linker/loader 31 */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/sysmacros.h> 36 #include <sys/systm.h> 37 #include <sys/user.h> 38 #include <sys/kmem.h> 39 #include <sys/reboot.h> 40 #include <sys/bootconf.h> 41 #include <sys/debug.h> 42 #include <sys/uio.h> 43 #include <sys/file.h> 44 #include <sys/vnode.h> 45 #include <sys/user.h> 46 #include <sys/mman.h> 47 #include <vm/as.h> 48 #include <vm/seg_kp.h> 49 #include <vm/seg_kmem.h> 50 #include <sys/elf.h> 51 #include <sys/elf_notes.h> 52 #include <sys/vmsystm.h> 53 #include <sys/kdi.h> 54 #include <sys/atomic.h> 55 #include <sys/kmdb.h> 56 57 #include <sys/link.h> 58 #include <sys/kobj.h> 59 #include <sys/ksyms.h> 60 #include <sys/disp.h> 61 #include <sys/modctl.h> 62 #include <sys/varargs.h> 63 #include <sys/kstat.h> 64 #include <sys/kobj_impl.h> 65 #include <sys/callb.h> 66 #include <sys/cmn_err.h> 67 #include <sys/tnf_probe.h> 68 69 #include <reloc.h> 70 #include <kobj_kdi.h> 71 #include <sys/sha1.h> 72 #include <sys/crypto/elfsign.h> 73 74 #if !defined(__sparc) 75 #include <sys/bootvfs.h> 76 #endif 77 78 /* 79 * do_symbols() error codes 80 */ 81 #define DOSYM_UNDEF -1 /* undefined symbol */ 82 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */ 83 84 static struct module *load_exec(val_t *); 85 static void load_linker(val_t *); 86 static struct modctl *add_primary(char *filename, int); 87 static int bind_primary(val_t *, int); 88 static int load_primary(struct module *, int); 89 static int load_kmdb(val_t *); 90 static int get_progbits(struct module *, struct _buf *); 91 static int get_syms(struct module *, struct _buf *); 92 static int get_ctf(struct module *, struct _buf *); 93 static void get_signature(struct module *, struct _buf *); 94 static int do_common(struct module *); 95 static void add_dependent(struct module *, struct module *); 96 static int do_dependents(struct modctl *, char *, size_t); 97 static int do_symbols(struct module *, Elf64_Addr); 98 static void module_assign(struct modctl *, struct module *); 99 static void free_module_data(struct module *); 100 static char *depends_on(struct module *); 101 static char *getmodpath(void); 102 static char *basename(char *); 103 static void attr_val(val_t *); 104 static char *find_libmacro(char *); 105 static char *expand_libmacro(char *, char *, char *); 106 static int read_bootflags(void); 107 static int kobj_boot_open(char *, int); 108 static int kobj_boot_close(int); 109 static int kobj_boot_seek(int, off_t, off_t); 110 static int kobj_boot_read(int, caddr_t, size_t); 111 112 static Sym *lookup_one(struct module *, const char *); 113 static void sym_insert(struct module *, char *, symid_t); 114 static Sym *sym_lookup(struct module *, Sym *); 115 116 /*PRINTFLIKE2*/ 117 static void kprintf(void *, const char *, ...) __KPRINTFLIKE(2); 118 119 static struct kobjopen_tctl *kobjopen_alloc(char *filename); 120 static void kobjopen_free(struct kobjopen_tctl *ltp); 121 static void kobjopen_thread(struct kobjopen_tctl *ltp); 122 123 extern int kcopy(const void *, void *, size_t); 124 extern int elf_mach_ok(Ehdr *); 125 extern int alloc_gottable(struct module *, caddr_t *, caddr_t *); 126 127 static void tnf_unsplice_probes(unsigned int, struct modctl *); 128 129 extern int modrootloaded; 130 extern int swaploaded; 131 extern int bop_io_quiesced; 132 extern int last_module_id; 133 134 #ifdef KOBJ_DEBUG 135 /* 136 * Values that can be or'd in to kobj_debug and their effects: 137 * 138 * D_DEBUG - misc. debugging information. 139 * D_SYMBOLS - list symbols and their values as they are entered 140 * into the hash table 141 * D_RELOCATIONS - display relocation processing information 142 * D_LOADING - display information about each module as it 143 * is loaded. 144 */ 145 int kobj_debug = 0; 146 #endif 147 148 #define MODPATH_PROPNAME "module-path" 149 150 #ifdef MODDIR_SUFFIX 151 static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/"; 152 #else 153 #define slash_moddir_suffix_slash "" 154 #endif 155 156 #define _moddebug get_weakish_int(&moddebug) 157 #define _modrootloaded get_weakish_int(&modrootloaded) 158 #define _swaploaded get_weakish_int(&swaploaded) 159 #define _ioquiesced get_weakish_int(&bop_io_quiesced) 160 161 #define mod(X) (struct module *)((X)->modl_modp->mod_mp) 162 163 void *romp; /* rom vector (opaque to us) */ 164 struct bootops *ops; /* bootops vector */ 165 void *dbvec; /* debug vector */ 166 167 /* 168 * kobjopen thread control structure 169 */ 170 struct kobjopen_tctl { 171 ksema_t sema; 172 char *name; /* name of file */ 173 struct vnode *vp; /* vnode return from vn_open() */ 174 int Errno; /* error return from vnopen */ 175 }; 176 177 /* 178 * Structure for defining dynamically expandable library macros 179 */ 180 181 struct lib_macro_info { 182 char *lmi_list; /* ptr to list of possible choices */ 183 char *lmi_macroname; /* pointer to macro name */ 184 ushort_t lmi_ba_index; /* index into bootaux vector */ 185 ushort_t lmi_macrolen; /* macro length */ 186 } libmacros[] = { 187 { NULL, "CPU", BA_CPU, 0 }, 188 { NULL, "MMU", BA_MMU, 0 } 189 }; 190 191 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info) 192 193 char *boot_cpu_compatible_list; /* make $CPU available */ 194 195 #ifdef MPSAS 196 void sas_prisyms(struct modctl_list *); 197 void sas_syms(struct module *); 198 #endif 199 200 vmem_t *text_arena; /* module text arena */ 201 static vmem_t *data_arena; /* module data & bss arena */ 202 static vmem_t *ctf_arena; /* CTF debug data arena */ 203 static struct modctl *kobj_modules = NULL; /* modules loaded */ 204 static char *module_path; /* module search path */ 205 int kobj_mmu_pagesize; /* system pagesize */ 206 static int lg_pagesize; /* "large" pagesize */ 207 static int kobj_last_module_id = 0; /* id assignment */ 208 static kmutex_t kobj_lock; /* protects mach memory list */ 209 210 /* 211 * The following functions have been implemented by the kernel. 212 * However, many 3rd party drivers provide their own implementations 213 * of these functions. When such drivers are loaded, messages 214 * indicateing that these symbols have been mulply defined will be 215 * emitted to the console. To avoid alarming customers for no good 216 * reason, we simply suppress such warnings for the following set of 217 * functions. 218 */ 219 static char *suppress_sym_list[] = 220 { 221 "strstr", 222 "strncat", 223 "strlcat", 224 "strlcpy", 225 "strspn", 226 "memcpy", 227 "memset", 228 "memmove", 229 "memcmp", 230 "memchr", 231 "__udivdi3", 232 "__divdi3", 233 "__umoddi3", 234 "__moddi3", 235 NULL /* This entry must exist */ 236 }; 237 238 /* indexed by KOBJ_NOTIFY_* */ 239 static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1]; 240 241 /* 242 * TNF probe management globals 243 */ 244 tnf_probe_control_t *__tnf_probe_list_head = NULL; 245 tnf_tag_data_t *__tnf_tag_list_head = NULL; 246 int tnf_changed_probe_list = 0; 247 248 /* 249 * Prefix for statically defined tracing (SDT) DTrace probes. 250 */ 251 const char *sdt_prefix = "__dtrace_probe_"; 252 253 #if defined(__sparc) 254 /* 255 * Some PROMs return SUNW,UltraSPARC when they actually have 256 * SUNW,UltraSPARC-II cpus. SInce we're now filtering out all 257 * SUNW,UltraSPARC systems during the boot phase, we can safely 258 * point the auxv CPU value at SUNW,UltraSPARC-II. This is what 259 * we point it at. 260 */ 261 const char *ultra_2 = "SUNW,UltraSPARC-II"; 262 #endif 263 264 /* 265 * Beginning and end of the kernel's 266 * dynamic text/data segments. 267 */ 268 static caddr_t _text; 269 static caddr_t _etext; 270 static caddr_t _data; 271 caddr_t _edata; 272 273 static Addr dynseg = 0; /* load address of "dynamic" segment */ 274 275 int standalone = 1; /* an unwholey kernel? */ 276 int use_iflush; /* iflush after relocations */ 277 278 /* 279 * _kobj_printf() 280 * 281 * Common printf function pointer. Can handle only one conversion 282 * specification in the format string. Some of the functions invoked 283 * through this function pointer cannot handle more that one conversion 284 * specification in the format string. 285 */ 286 void (*_kobj_printf)(void *, const char *, ...); /* printf routine */ 287 288 static kobj_stat_t kobj_stat; 289 290 #define MINALIGN 8 /* at least a double-word */ 291 292 int 293 get_weakish_int(int *ip) 294 { 295 if (standalone) 296 return (0); 297 return (ip == NULL ? 0 : *ip); 298 } 299 300 static void * 301 get_weakish_pointer(void **ptrp) 302 { 303 if (standalone) 304 return (0); 305 return (ptrp == NULL ? 0 : *ptrp); 306 } 307 308 /* 309 * XXX fix dependencies on "kernel"; this should work 310 * for other standalone binaries as well. 311 * 312 * XXX Fix hashing code to use one pointer to 313 * hash entries. 314 * |----------| 315 * | nbuckets | 316 * |----------| 317 * | nchains | 318 * |----------| 319 * | bucket[] | 320 * |----------| 321 * | chain[] | 322 * |----------| 323 */ 324 325 /* 326 * Load, bind and relocate all modules that 327 * form the primary kernel. At this point, our 328 * externals have not been relocated. 329 */ 330 void 331 kobj_init( 332 void *romvec, 333 void *dvec, 334 struct bootops *bootvec, 335 val_t *bootaux) 336 { 337 struct module *mp; 338 struct modctl *modp; 339 Addr entry; 340 341 /* 342 * Save these to pass on to 343 * the booted standalone. 344 */ 345 romp = romvec; 346 dbvec = dvec; 347 348 ops = bootvec; 349 #if defined(__i386) || defined(__amd64) 350 _kobj_printf = (void (*)(void *, const char *, ...))ops->bsys_printf; 351 #else 352 _kobj_printf = (void (*)(void *, const char *, ...))bop_putsarg; 353 #endif 354 355 #if defined(__sparc) 356 /* XXXQ should suppress this test on sun4v */ 357 if (bootaux[BA_CPU].ba_ptr) { 358 if (strcmp("SUNW,UltraSPARC", bootaux[BA_CPU].ba_ptr) == 0) { 359 bootaux[BA_CPU].ba_ptr = (void *) ultra_2; 360 } 361 } 362 #endif 363 /* 364 * Save the interesting attribute-values 365 * (scanned by kobj_boot). 366 */ 367 attr_val(bootaux); 368 369 /* 370 * Check bootops version. 371 */ 372 if (BOP_GETVERSION(ops) != BO_VERSION) { 373 _kobj_printf(ops, "Warning: Using boot version %d, ", 374 BOP_GETVERSION(ops)); 375 _kobj_printf(ops, "expected %d\n", BO_VERSION); 376 } 377 378 /* 379 * We don't support standalone debuggers anymore. The use of kadb 380 * will interfere with the later use of kmdb. Let the user mend 381 * their ways now. Users will reach this message if they still 382 * have the kadb binary on their system (perhaps they used an old 383 * bfu, or maybe they intentionally copied it there) and have 384 * specified its use in a way that eluded our checking in the boot 385 * program. 386 */ 387 if (dvec != NULL) { 388 _kobj_printf(ops, "\nWARNING: Standalone debuggers such as " 389 "kadb are no longer supported\n\n"); 390 goto fail; 391 } 392 393 #ifndef __sparc 394 { 395 /* on x86, we always boot with a ramdisk */ 396 extern int kobj_boot_mountroot(void); 397 (void) kobj_boot_mountroot(); 398 } 399 #endif 400 401 /* 402 * Set the module search path. 403 */ 404 module_path = getmodpath(); 405 406 boot_cpu_compatible_list = find_libmacro("CPU"); 407 408 /* 409 * These two modules have actually been 410 * loaded by boot, but we finish the job 411 * by introducing them into the world of 412 * loadable modules. 413 */ 414 415 mp = load_exec(bootaux); 416 load_linker(bootaux); 417 418 /* 419 * Load all the primary dependent modules. 420 */ 421 if (load_primary(mp, KOBJ_LM_PRIMARY) == -1) 422 goto fail; 423 424 /* 425 * Glue it together. 426 */ 427 if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1) 428 goto fail; 429 430 entry = bootaux[BA_ENTRY].ba_val; 431 432 #ifdef __sparc 433 /* 434 * On sparcv9, boot scratch memory is running out. 435 * Free the temporary allocations here to allow boot 436 * to continue. 437 */ 438 kobj_tmp_free(); 439 #endif 440 441 /* 442 * Get the boot flags 443 */ 444 bootflags(ops); 445 446 if (boothowto & RB_VERBOSE) 447 kobj_lm_dump(KOBJ_LM_PRIMARY); 448 449 kobj_kdi_init(); 450 451 if (boothowto & RB_KMDB) { 452 if (load_kmdb(bootaux) < 0) 453 goto fail; 454 } 455 456 /* 457 * Post setup. 458 */ 459 #ifdef MPSAS 460 sas_prisyms(kobj_lm_lookup(KOBJ_LM_PRIMARY)); 461 #endif 462 s_text = _text; 463 e_text = _etext; 464 s_data = _data; 465 e_data = _edata; 466 467 kobj_sync_instruction_memory(s_text, e_text - s_text); 468 469 #ifdef KOBJ_DEBUG 470 if (kobj_debug & D_DEBUG) 471 _kobj_printf(ops, 472 "krtld: transferring control to: 0x%p\n", entry); 473 #endif 474 475 /* 476 * Make sure the mod system knows about the modules already loaded. 477 */ 478 last_module_id = kobj_last_module_id; 479 bcopy(kobj_modules, &modules, sizeof (modules)); 480 modp = &modules; 481 do { 482 if (modp->mod_next == kobj_modules) 483 modp->mod_next = &modules; 484 if (modp->mod_prev == kobj_modules) 485 modp->mod_prev = &modules; 486 } while ((modp = modp->mod_next) != &modules); 487 488 standalone = 0; 489 490 #ifdef __sparc 491 /* 492 * On sparcv9, boot scratch memory is running out. 493 * Free the temporary allocations here to allow boot 494 * to continue. 495 */ 496 kobj_tmp_free(); 497 #endif 498 499 _kobj_printf = kprintf; 500 exitto((caddr_t)entry); 501 fail: 502 503 _kobj_printf(ops, "krtld: error during initial load/link phase\n"); 504 } 505 506 /* 507 * Set up any global information derived 508 * from attribute/values in the boot or 509 * aux vector. 510 */ 511 static void 512 attr_val(val_t *bootaux) 513 { 514 Phdr *phdr; 515 int phnum, phsize; 516 int i; 517 518 kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val; 519 lg_pagesize = bootaux[BA_LPAGESZ].ba_val; 520 use_iflush = bootaux[BA_IFLUSH].ba_val; 521 522 phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr; 523 phnum = bootaux[BA_PHNUM].ba_val; 524 phsize = bootaux[BA_PHENT].ba_val; 525 for (i = 0; i < phnum; i++) { 526 phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize); 527 528 if (phdr->p_type != PT_LOAD) 529 continue; 530 /* 531 * Bounds of the various segments. 532 */ 533 if (!(phdr->p_flags & PF_X)) { 534 dynseg = phdr->p_vaddr; 535 } else { 536 if (phdr->p_flags & PF_W) { 537 _data = (caddr_t)phdr->p_vaddr; 538 _edata = _data + phdr->p_memsz; 539 } else { 540 _text = (caddr_t)phdr->p_vaddr; 541 _etext = _text + phdr->p_memsz; 542 } 543 } 544 } 545 546 /* To do the kobj_alloc, _edata needs to be set. */ 547 for (i = 0; i < NLIBMACROS; i++) { 548 if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) { 549 libmacros[i].lmi_list = kobj_alloc( 550 strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) + 551 1, KM_WAIT); 552 (void) strcpy(libmacros[i].lmi_list, 553 bootaux[libmacros[i].lmi_ba_index].ba_ptr); 554 } 555 libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname); 556 } 557 } 558 559 /* 560 * Set up the booted executable. 561 */ 562 static struct module * 563 load_exec(val_t *bootaux) 564 { 565 char filename[MAXPATHLEN]; 566 struct modctl *cp; 567 struct module *mp; 568 Dyn *dyn; 569 Sym *sp; 570 int i, lsize, osize, nsize, allocsize; 571 char *libname, *tmp; 572 573 (void) BOP_GETPROP(ops, "whoami", filename); 574 575 cp = add_primary(filename, KOBJ_LM_PRIMARY); 576 577 mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 578 cp->mod_mp = mp; 579 580 /* 581 * We don't have the following information 582 * since this module is an executable and not 583 * a relocatable .o. 584 */ 585 mp->symtbl_section = 0; 586 mp->shdrs = NULL; 587 mp->strhdr = NULL; 588 589 /* 590 * Since this module is the only exception, 591 * we cons up some section headers. 592 */ 593 mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 594 mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 595 596 mp->symhdr->sh_type = SHT_SYMTAB; 597 mp->strhdr->sh_type = SHT_STRTAB; 598 /* 599 * Scan the dynamic structure. 600 */ 601 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 602 dyn->d_tag != DT_NULL; dyn++) { 603 switch (dyn->d_tag) { 604 case DT_SYMTAB: 605 dyn->d_un.d_ptr += dynseg; 606 mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr; 607 mp->symhdr->sh_addr = dyn->d_un.d_ptr; 608 break; 609 case DT_HASH: 610 dyn->d_un.d_ptr += dynseg; 611 mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1); 612 mp->hashsize = *(uint_t *)dyn->d_un.d_ptr; 613 break; 614 case DT_STRTAB: 615 dyn->d_un.d_ptr += dynseg; 616 mp->strings = (char *)dyn->d_un.d_ptr; 617 mp->strhdr->sh_addr = dyn->d_un.d_ptr; 618 break; 619 case DT_STRSZ: 620 mp->strhdr->sh_size = dyn->d_un.d_val; 621 break; 622 case DT_SYMENT: 623 mp->symhdr->sh_entsize = dyn->d_un.d_val; 624 break; 625 } 626 } 627 628 /* 629 * Collapse any DT_NEEDED entries into one string. 630 */ 631 nsize = osize = 0; 632 allocsize = MAXPATHLEN; 633 634 mp->depends_on = kobj_alloc(allocsize, KM_WAIT); 635 636 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 637 dyn->d_tag != DT_NULL; dyn++) 638 if (dyn->d_tag == DT_NEEDED) { 639 char *_lib; 640 641 libname = mp->strings + dyn->d_un.d_val; 642 if (strchr(libname, '$') != NULL) { 643 if ((_lib = expand_libmacro(libname, 644 filename, filename)) != NULL) 645 libname = _lib; 646 else 647 _kobj_printf(ops, "krtld: " 648 "load_exec: fail to " 649 "expand %s\n", libname); 650 } 651 lsize = strlen(libname); 652 nsize += lsize; 653 if (nsize + 1 > allocsize) { 654 tmp = kobj_alloc(allocsize + MAXPATHLEN, 655 KM_WAIT); 656 bcopy(mp->depends_on, tmp, osize); 657 kobj_free(mp->depends_on, allocsize); 658 mp->depends_on = tmp; 659 allocsize += MAXPATHLEN; 660 } 661 bcopy(libname, mp->depends_on + osize, lsize); 662 *(mp->depends_on + nsize) = ' '; /* seperate */ 663 nsize++; 664 osize = nsize; 665 } 666 if (nsize) { 667 mp->depends_on[nsize - 1] = '\0'; /* terminate the string */ 668 /* 669 * alloc with exact size and copy whatever it got over 670 */ 671 tmp = kobj_alloc(nsize, KM_WAIT); 672 bcopy(mp->depends_on, tmp, nsize); 673 kobj_free(mp->depends_on, allocsize); 674 mp->depends_on = tmp; 675 } else { 676 kobj_free(mp->depends_on, allocsize); 677 mp->depends_on = NULL; 678 } 679 680 mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */ 681 mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize; 682 /* 683 * We allocate our own table since we don't 684 * hash undefined references. 685 */ 686 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 687 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 688 689 mp->text = _text; 690 mp->data = _data; 691 cp->mod_text = mp->text; 692 cp->mod_text_size = mp->text_size; 693 694 mp->filename = cp->mod_filename; 695 696 #ifdef KOBJ_DEBUG 697 if (kobj_debug & D_LOADING) { 698 _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 699 _kobj_printf(ops, "\ttext: 0x%p", mp->text); 700 _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 701 _kobj_printf(ops, "\tdata: 0x%p", mp->data); 702 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 703 } 704 #endif /* KOBJ_DEBUG */ 705 706 /* 707 * Insert symbols into the hash table. 708 */ 709 for (i = 0; i < mp->nsyms; i++) { 710 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 711 712 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 713 continue; 714 #ifdef __sparc 715 /* 716 * Register symbols are ignored in the kernel 717 */ 718 if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) 719 continue; 720 #endif /* __sparc */ 721 722 sym_insert(mp, mp->strings + sp->st_name, i); 723 } 724 725 return (mp); 726 } 727 728 /* 729 * Set up the linker module. 730 */ 731 static void 732 load_linker(val_t *bootaux) 733 { 734 struct module *kmp = (struct module *)kobj_modules->mod_mp; 735 struct module *mp; 736 struct modctl *cp; 737 int i; 738 Shdr *shp; 739 Sym *sp; 740 int shsize; 741 char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr; 742 743 cp = add_primary(dlname, KOBJ_LM_PRIMARY); 744 745 mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 746 747 cp->mod_mp = mp; 748 mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr; 749 shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum; 750 mp->shdrs = kobj_alloc(shsize, KM_WAIT); 751 bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize); 752 753 for (i = 1; i < (int)mp->hdr.e_shnum; i++) { 754 shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize)); 755 756 if (shp->sh_flags & SHF_ALLOC) { 757 if (shp->sh_flags & SHF_WRITE) { 758 if (mp->data == NULL) 759 mp->data = (char *)shp->sh_addr; 760 } else if (mp->text == NULL) { 761 mp->text = (char *)shp->sh_addr; 762 } 763 } 764 if (shp->sh_type == SHT_SYMTAB) { 765 mp->symtbl_section = i; 766 mp->symhdr = shp; 767 mp->symspace = mp->symtbl = (char *)shp->sh_addr; 768 } 769 } 770 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 771 mp->flags = KOBJ_INTERP|KOBJ_PRIM; 772 mp->strhdr = (Shdr *) 773 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 774 mp->strings = (char *)mp->strhdr->sh_addr; 775 mp->hashsize = kobj_gethashsize(mp->nsyms); 776 777 mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) + 778 (mp->hashsize + mp->nsyms) * sizeof (symid_t); 779 780 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 781 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 782 783 mp->bss = bootaux[BA_BSS].ba_val; 784 mp->bss_align = 0; /* pre-aligned during allocation */ 785 mp->bss_size = (uintptr_t)_edata - mp->bss; 786 mp->text_size = _etext - mp->text; 787 mp->data_size = _edata - mp->data; 788 mp->filename = cp->mod_filename; 789 cp->mod_text = mp->text; 790 cp->mod_text_size = mp->text_size; 791 792 /* 793 * Now that we've figured out where the linker is, 794 * set the limits for the booted object. 795 */ 796 kmp->text_size = (size_t)(mp->text - kmp->text); 797 kmp->data_size = (size_t)(mp->data - kmp->data); 798 kobj_modules->mod_text_size = kmp->text_size; 799 800 #ifdef KOBJ_DEBUG 801 if (kobj_debug & D_LOADING) { 802 _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 803 _kobj_printf(ops, "\ttext:0x%p", mp->text); 804 _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 805 _kobj_printf(ops, "\tdata:0x%p", mp->data); 806 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 807 } 808 #endif /* KOBJ_DEBUG */ 809 810 /* 811 * Insert the symbols into the hash table. 812 */ 813 for (i = 0; i < mp->nsyms; i++) { 814 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 815 816 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 817 continue; 818 if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 819 if (sp->st_shndx == SHN_COMMON) 820 sp->st_shndx = SHN_ABS; 821 } 822 sym_insert(mp, mp->strings + sp->st_name, i); 823 } 824 825 } 826 827 static kobj_notify_list_t ** 828 kobj_notify_lookup(uint_t type) 829 { 830 ASSERT(type != 0 && type < sizeof (kobj_notifiers) / 831 sizeof (kobj_notify_list_t *)); 832 833 return (&kobj_notifiers[type]); 834 } 835 836 int 837 kobj_notify_add(kobj_notify_list_t *knp) 838 { 839 kobj_notify_list_t **knl; 840 841 knl = kobj_notify_lookup(knp->kn_type); 842 843 knp->kn_next = NULL; 844 knp->kn_prev = NULL; 845 846 mutex_enter(&kobj_lock); 847 848 if (*knl != NULL) { 849 (*knl)->kn_prev = knp; 850 knp->kn_next = *knl; 851 } 852 (*knl) = knp; 853 854 mutex_exit(&kobj_lock); 855 return (0); 856 } 857 858 int 859 kobj_notify_remove(kobj_notify_list_t *knp) 860 { 861 kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type); 862 kobj_notify_list_t *tknp; 863 864 mutex_enter(&kobj_lock); 865 866 /* LINTED */ 867 if (tknp = knp->kn_next) 868 tknp->kn_prev = knp->kn_prev; 869 870 /* LINTED */ 871 if (tknp = knp->kn_prev) 872 tknp->kn_next = knp->kn_next; 873 else 874 *knl = knp->kn_next; 875 876 mutex_exit(&kobj_lock); 877 878 return (0); 879 } 880 881 /* 882 * Notify all interested callbacks of a specified change in module state. 883 */ 884 static void 885 kobj_notify(int type, struct modctl *modp) 886 { 887 kobj_notify_list_t *knp; 888 889 if (modp->mod_loadflags & MOD_NONOTIFY || standalone) 890 return; 891 892 mutex_enter(&kobj_lock); 893 894 for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next) 895 knp->kn_func(type, modp); 896 897 /* 898 * KDI notification must be last (it has to allow for work done by the 899 * other notification callbacks), so we call it manually. 900 */ 901 kobj_kdi_mod_notify(type, modp); 902 903 mutex_exit(&kobj_lock); 904 } 905 906 /* 907 * Ask boot for the module path. 908 */ 909 static char * 910 getmodpath(void) 911 { 912 char *path; 913 int len; 914 915 if ((len = BOP_GETPROPLEN(ops, MODPATH_PROPNAME)) == -1) 916 return (MOD_DEFPATH); 917 918 path = kobj_zalloc(len, KM_WAIT); 919 920 (void) BOP_GETPROP(ops, MODPATH_PROPNAME, path); 921 922 return (*path ? path : MOD_DEFPATH); 923 } 924 925 static struct modctl * 926 add_primary(char *filename, int lmid) 927 { 928 struct modctl *cp; 929 930 cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT); 931 932 cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT); 933 934 /* 935 * For symbol lookup, we assemble our own 936 * modctl list of the primary modules. 937 */ 938 939 (void) strcpy(cp->mod_filename, filename); 940 cp->mod_modname = basename(cp->mod_filename); 941 942 /* set values for modinfo assuming that the load will work */ 943 cp->mod_prim = 1; 944 cp->mod_loaded = 1; 945 cp->mod_installed = 1; 946 cp->mod_loadcnt = 1; 947 cp->mod_loadflags = MOD_NOAUTOUNLOAD; 948 949 cp->mod_id = kobj_last_module_id++; 950 951 /* 952 * Link the module in. We'll pass this info on 953 * to the mod squad later. 954 */ 955 if (kobj_modules == NULL) { 956 kobj_modules = cp; 957 cp->mod_prev = cp->mod_next = cp; 958 } else { 959 cp->mod_prev = kobj_modules->mod_prev; 960 cp->mod_next = kobj_modules; 961 kobj_modules->mod_prev->mod_next = cp; 962 kobj_modules->mod_prev = cp; 963 } 964 965 kobj_lm_append(lmid, cp); 966 967 return (cp); 968 } 969 970 static int 971 bind_primary(val_t *bootaux, int lmid) 972 { 973 struct modctl_list *linkmap = kobj_lm_lookup(lmid); 974 struct modctl_list *lp; 975 struct module *mp; 976 Dyn *dyn; 977 Word relasz; 978 Word relaent; 979 char *rela; 980 981 /* 982 * Do common symbols. 983 */ 984 for (lp = linkmap; lp; lp = lp->modl_next) { 985 mp = mod(lp); 986 987 /* 988 * Don't do common section relocations for modules that 989 * don't need it. 990 */ 991 if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP)) 992 continue; 993 994 if (do_common(mp) < 0) 995 return (-1); 996 } 997 998 /* 999 * Resolve symbols. 1000 */ 1001 for (lp = linkmap; lp; lp = lp->modl_next) { 1002 mp = mod(lp); 1003 1004 if (do_symbols(mp, 0) < 0) 1005 return (-1); 1006 } 1007 1008 /* 1009 * Do relocations. 1010 */ 1011 for (lp = linkmap; lp; lp = lp->modl_next) { 1012 mp = mod(lp); 1013 1014 if (mp->flags & KOBJ_EXEC) { 1015 Word shtype; 1016 1017 relasz = 0; 1018 relaent = 0; 1019 rela = NULL; 1020 1021 for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr; 1022 dyn->d_tag != DT_NULL; dyn++) { 1023 switch (dyn->d_tag) { 1024 case DT_RELASZ: 1025 case DT_RELSZ: 1026 relasz = dyn->d_un.d_val; 1027 break; 1028 case DT_RELAENT: 1029 case DT_RELENT: 1030 relaent = dyn->d_un.d_val; 1031 break; 1032 case DT_RELA: 1033 shtype = SHT_RELA; 1034 rela = (char *)(dyn->d_un.d_ptr + 1035 dynseg); 1036 break; 1037 case DT_REL: 1038 shtype = SHT_REL; 1039 rela = (char *)(dyn->d_un.d_ptr + 1040 dynseg); 1041 break; 1042 } 1043 } 1044 if (relasz == 0 || 1045 relaent == 0 || rela == NULL) { 1046 _kobj_printf(ops, "krtld: bind_primary(): " 1047 "no relocation information found for " 1048 "module %s\n", mp->filename); 1049 return (-1); 1050 } 1051 1052 #ifdef KOBJ_DEBUG 1053 if (kobj_debug & D_RELOCATIONS) 1054 _kobj_printf(ops, "krtld: relocating: file=%s " 1055 "KOBJ_EXEC\n", mp->filename); 1056 #endif 1057 if (do_relocate(mp, rela, shtype, relasz/relaent, 1058 relaent, (Addr)mp->text) < 0) 1059 return (-1); 1060 } else { 1061 if (do_relocations(mp) < 0) 1062 return (-1); 1063 } 1064 1065 /* sync_instruction_memory */ 1066 kobj_sync_instruction_memory(mp->text, mp->text_size); 1067 } 1068 1069 for (lp = linkmap; lp; lp = lp->modl_next) { 1070 mp = mod(lp); 1071 1072 /* 1073 * We need to re-read the full symbol table for the boot file, 1074 * since we couldn't use the full one before. We also need to 1075 * load the CTF sections of both the boot file and the 1076 * interpreter (us). 1077 */ 1078 if (mp->flags & KOBJ_EXEC) { 1079 struct _buf *file; 1080 int n; 1081 1082 file = kobj_open_file(mp->filename); 1083 if (file == (struct _buf *)-1) 1084 return (-1); 1085 if (kobj_read_file(file, (char *)&mp->hdr, 1086 sizeof (mp->hdr), 0) < 0) 1087 return (-1); 1088 n = mp->hdr.e_shentsize * mp->hdr.e_shnum; 1089 mp->shdrs = kobj_alloc(n, KM_WAIT); 1090 if (kobj_read_file(file, mp->shdrs, n, 1091 mp->hdr.e_shoff) < 0) 1092 return (-1); 1093 if (get_syms(mp, file) < 0) 1094 return (-1); 1095 if (get_ctf(mp, file) < 0) 1096 return (-1); 1097 kobj_close_file(file); 1098 mp->flags |= KOBJ_RELOCATED; 1099 1100 } else if (mp->flags & KOBJ_INTERP) { 1101 struct _buf *file; 1102 1103 /* 1104 * The interpreter path fragment in mp->filename 1105 * will already have the module directory suffix 1106 * in it (if appropriate). 1107 */ 1108 file = kobj_open_path(mp->filename, 1, 0); 1109 if (file == (struct _buf *)-1) 1110 return (-1); 1111 if (get_ctf(mp, file) < 0) 1112 return (-1); 1113 kobj_close_file(file); 1114 mp->flags |= KOBJ_RELOCATED; 1115 } 1116 } 1117 1118 return (0); 1119 } 1120 1121 static struct modctl * 1122 mod_already_loaded(char *modname) 1123 { 1124 struct modctl *mctl = kobj_modules; 1125 1126 do { 1127 if (strcmp(modname, mctl->mod_filename) == 0) 1128 return (mctl); 1129 mctl = mctl->mod_next; 1130 1131 } while (mctl != kobj_modules); 1132 1133 return (NULL); 1134 } 1135 1136 /* 1137 * Load all the primary dependent modules. 1138 */ 1139 static int 1140 load_primary(struct module *mp, int lmid) 1141 { 1142 struct modctl *cp; 1143 struct module *dmp; 1144 char *p, *q; 1145 char modname[MODMAXNAMELEN]; 1146 1147 if ((p = mp->depends_on) == NULL) 1148 return (0); 1149 1150 /* CONSTANTCONDITION */ 1151 while (1) { 1152 /* 1153 * Skip space. 1154 */ 1155 while (*p && (*p == ' ' || *p == '\t')) 1156 p++; 1157 /* 1158 * Get module name. 1159 */ 1160 q = modname; 1161 while (*p && *p != ' ' && *p != '\t') 1162 *q++ = *p++; 1163 1164 if (q == modname) 1165 break; 1166 1167 *q = '\0'; 1168 /* 1169 * Check for dup dependencies. 1170 */ 1171 if (strcmp(modname, "dtracestubs") == 0 || 1172 mod_already_loaded(modname) != NULL) 1173 continue; 1174 1175 cp = add_primary(modname, lmid); 1176 cp->mod_busy = 1; 1177 /* 1178 * Load it. 1179 */ 1180 (void) kobj_load_module(cp, 1); 1181 cp->mod_busy = 0; 1182 1183 if ((dmp = cp->mod_mp) == NULL) { 1184 cp->mod_loaded = 0; 1185 cp->mod_installed = 0; 1186 cp->mod_loadcnt = 0; 1187 return (-1); 1188 } 1189 1190 add_dependent(mp, dmp); 1191 dmp->flags |= KOBJ_PRIM; 1192 1193 /* 1194 * Recurse. 1195 */ 1196 if (load_primary(dmp, lmid) == -1) { 1197 cp->mod_loaded = 0; 1198 cp->mod_installed = 0; 1199 cp->mod_loadcnt = 0; 1200 return (-1); 1201 } 1202 } 1203 return (0); 1204 } 1205 1206 static int 1207 load_kmdb(val_t *bootaux) 1208 { 1209 struct modctl *mctl; 1210 struct module *mp; 1211 Sym *sym; 1212 1213 _kobj_printf(ops, "Loading kmdb...\n"); 1214 1215 if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL) 1216 return (-1); 1217 1218 mctl->mod_busy = 1; 1219 (void) kobj_load_module(mctl, 1); 1220 mctl->mod_busy = 0; 1221 1222 if ((mp = mctl->mod_mp) == NULL) 1223 return (-1); 1224 1225 mp->flags |= KOBJ_PRIM; 1226 1227 if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0) 1228 return (-1); 1229 1230 if (boothowto & RB_VERBOSE) 1231 kobj_lm_dump(KOBJ_LM_DEBUGGER); 1232 1233 if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0) 1234 return (-1); 1235 1236 if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL) 1237 return (-1); 1238 1239 if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0, 1240 (const char **)kobj_kmdb_argv) < 0) 1241 return (-1); 1242 1243 return (0); 1244 } 1245 1246 /* 1247 * Return a string listing module dependencies. 1248 */ 1249 static char * 1250 depends_on(struct module *mp) 1251 { 1252 Sym *sp; 1253 char *depstr, *q; 1254 1255 /* 1256 * The module doesn't have a depends_on value, so let's try it the 1257 * old-fashioned way - via "_depends_on" 1258 */ 1259 if ((sp = lookup_one(mp, "_depends_on")) == NULL) 1260 return (NULL); 1261 1262 q = (char *)sp->st_value; 1263 1264 /* 1265 * Idiot checks. Make sure it's 1266 * in-bounds and NULL terminated. 1267 */ 1268 if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') { 1269 _kobj_printf(ops, "Error processing dependency for %s\n", 1270 mp->filename); 1271 return (NULL); 1272 } 1273 1274 depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT); 1275 (void) strcpy(depstr, q); 1276 1277 return (depstr); 1278 } 1279 1280 void 1281 kobj_getmodinfo(void *xmp, struct modinfo *modinfo) 1282 { 1283 struct module *mp; 1284 mp = (struct module *)xmp; 1285 1286 modinfo->mi_base = mp->text; 1287 modinfo->mi_size = mp->text_size + mp->data_size; 1288 } 1289 1290 /* 1291 * kobj_export_ksyms() performs the following services: 1292 * 1293 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena. 1294 * (2) Removes unneeded symbols to save space. 1295 * (3) Reduces memory footprint by using VM_BESTFIT allocations. 1296 * (4) Makes the symbol table visible to /dev/ksyms. 1297 */ 1298 static void 1299 kobj_export_ksyms(struct module *mp) 1300 { 1301 Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size); 1302 Sym *sp, *osp; 1303 char *name; 1304 size_t namelen; 1305 struct module *omp; 1306 uint_t nsyms; 1307 size_t symsize = mp->symhdr->sh_entsize; 1308 size_t locals = 1; 1309 size_t strsize; 1310 1311 /* 1312 * Make a copy of the original module structure. 1313 */ 1314 omp = kobj_alloc(sizeof (struct module), KM_WAIT); 1315 bcopy(mp, omp, sizeof (struct module)); 1316 1317 /* 1318 * Compute the sizes of the new symbol table sections. 1319 */ 1320 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) { 1321 if (osp->st_value == 0) 1322 continue; 1323 if (sym_lookup(omp, osp) == NULL) 1324 continue; 1325 name = omp->strings + osp->st_name; 1326 namelen = strlen(name); 1327 if (ELF_ST_BIND(osp->st_info) == STB_LOCAL) 1328 locals++; 1329 nsyms++; 1330 strsize += namelen + 1; 1331 } 1332 1333 mp->nsyms = nsyms; 1334 mp->hashsize = kobj_gethashsize(mp->nsyms); 1335 1336 /* 1337 * ksyms_lock must be held as writer during any operation that 1338 * modifies ksyms_arena, including allocation from same, and 1339 * must not be dropped until the arena is vmem_walk()able. 1340 */ 1341 rw_enter(&ksyms_lock, RW_WRITER); 1342 1343 /* 1344 * Allocate space for the new section headers (symtab and strtab), 1345 * symbol table, buckets, chains, and strings. 1346 */ 1347 mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) + 1348 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize; 1349 1350 if (mp->flags & KOBJ_NOKSYMS) { 1351 mp->symspace = kobj_alloc(mp->symsize, KM_WAIT); 1352 } else { 1353 mp->symspace = vmem_alloc(ksyms_arena, mp->symsize, 1354 VM_BESTFIT | VM_SLEEP); 1355 } 1356 bzero(mp->symspace, mp->symsize); 1357 1358 /* 1359 * Divvy up symspace. 1360 */ 1361 mp->shdrs = mp->symspace; 1362 mp->symhdr = (Shdr *)mp->shdrs; 1363 mp->strhdr = (Shdr *)(mp->symhdr + 1); 1364 mp->symtbl = (char *)(mp->strhdr + 1); 1365 mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize)); 1366 mp->chains = (symid_t *)(mp->buckets + mp->hashsize); 1367 mp->strings = (char *)(mp->chains + nsyms); 1368 1369 /* 1370 * Fill in the new section headers (symtab and strtab). 1371 */ 1372 mp->hdr.e_shnum = 2; 1373 mp->symtbl_section = 0; 1374 1375 mp->symhdr->sh_type = SHT_SYMTAB; 1376 mp->symhdr->sh_addr = (Addr)mp->symtbl; 1377 mp->symhdr->sh_size = nsyms * symsize; 1378 mp->symhdr->sh_link = 1; 1379 mp->symhdr->sh_info = locals; 1380 mp->symhdr->sh_addralign = sizeof (Addr); 1381 mp->symhdr->sh_entsize = symsize; 1382 1383 mp->strhdr->sh_type = SHT_STRTAB; 1384 mp->strhdr->sh_addr = (Addr)mp->strings; 1385 mp->strhdr->sh_size = strsize; 1386 mp->strhdr->sh_addralign = 1; 1387 1388 /* 1389 * Construct the new symbol table. 1390 */ 1391 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) { 1392 if (osp->st_value == 0) 1393 continue; 1394 if (sym_lookup(omp, osp) == NULL) 1395 continue; 1396 name = omp->strings + osp->st_name; 1397 namelen = strlen(name); 1398 sp = (Sym *)(mp->symtbl + symsize * nsyms); 1399 bcopy(osp, sp, symsize); 1400 bcopy(name, mp->strings + strsize, namelen); 1401 sp->st_name = strsize; 1402 sym_insert(mp, name, nsyms); 1403 nsyms++; 1404 strsize += namelen + 1; 1405 } 1406 1407 rw_exit(&ksyms_lock); 1408 1409 /* 1410 * Free the old section headers -- we'll never need them again. 1411 */ 1412 if (!(mp->flags & KOBJ_PRIM)) 1413 kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum); 1414 /* 1415 * Discard the old symbol table and our copy of the module strucure. 1416 */ 1417 if (!(mp->flags & KOBJ_PRIM)) 1418 kobj_free(omp->symspace, omp->symsize); 1419 kobj_free(omp, sizeof (struct module)); 1420 } 1421 1422 static void 1423 kobj_export_ctf(struct module *mp) 1424 { 1425 char *data = mp->ctfdata; 1426 size_t size = mp->ctfsize; 1427 1428 if (data != NULL) { 1429 if (_moddebug & MODDEBUG_NOCTF) { 1430 mp->ctfdata = NULL; 1431 mp->ctfsize = 0; 1432 } else { 1433 mp->ctfdata = vmem_alloc(ctf_arena, size, 1434 VM_BESTFIT | VM_SLEEP); 1435 bcopy(data, mp->ctfdata, size); 1436 } 1437 1438 if (!(mp->flags & KOBJ_PRIM)) 1439 kobj_free(data, size); 1440 } 1441 } 1442 1443 void 1444 kobj_export_module(struct module *mp) 1445 { 1446 kobj_export_ksyms(mp); 1447 kobj_export_ctf(mp); 1448 1449 mp->flags |= KOBJ_EXPORTED; 1450 } 1451 1452 static int 1453 process_dynamic(struct module *mp, char *dyndata, char *strdata) 1454 { 1455 char *path = NULL, *depstr = NULL; 1456 int allocsize = 0, osize = 0, nsize = 0; 1457 char *libname, *tmp; 1458 int lsize; 1459 Dyn *dynp; 1460 1461 for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) { 1462 switch (dynp->d_tag) { 1463 case DT_NEEDED: 1464 /* 1465 * Read the DT_NEEDED entries, expanding the macros they 1466 * contain (if any), and concatenating them into a 1467 * single space-separated dependency list. 1468 */ 1469 libname = (ulong_t)dynp->d_un.d_ptr + strdata; 1470 1471 if (strchr(libname, '$') != NULL) { 1472 char *_lib; 1473 1474 if (path == NULL) 1475 path = kobj_alloc(MAXPATHLEN, KM_WAIT); 1476 if ((_lib = expand_libmacro(libname, path, 1477 path)) != NULL) 1478 libname = _lib; 1479 else { 1480 _kobj_printf(ops, "krtld: " 1481 "process_dynamic: failed to expand " 1482 "%s\n", libname); 1483 } 1484 } 1485 1486 lsize = strlen(libname); 1487 nsize += lsize; 1488 if (nsize + 1 > allocsize) { 1489 tmp = kobj_alloc(allocsize + MAXPATHLEN, 1490 KM_WAIT); 1491 if (depstr != NULL) { 1492 bcopy(depstr, tmp, osize); 1493 kobj_free(depstr, allocsize); 1494 } 1495 depstr = tmp; 1496 allocsize += MAXPATHLEN; 1497 } 1498 bcopy(libname, depstr + osize, lsize); 1499 *(depstr + nsize) = ' '; /* separator */ 1500 nsize++; 1501 osize = nsize; 1502 break; 1503 1504 case DT_FLAGS_1: 1505 if (dynp->d_un.d_val & DF_1_IGNMULDEF) 1506 mp->flags |= KOBJ_IGNMULDEF; 1507 if (dynp->d_un.d_val & DF_1_NOKSYMS) 1508 mp->flags |= KOBJ_NOKSYMS; 1509 1510 break; 1511 } 1512 } 1513 1514 /* 1515 * finish up the depends string (if any) 1516 */ 1517 if (depstr != NULL) { 1518 *(depstr + nsize - 1) = '\0'; /* overwrite seperator w/term */ 1519 if (path != NULL) 1520 kobj_free(path, MAXPATHLEN); 1521 1522 tmp = kobj_alloc(nsize, KM_WAIT); 1523 bcopy(depstr, tmp, nsize); 1524 kobj_free(depstr, allocsize); 1525 depstr = tmp; 1526 1527 mp->depends_on = depstr; 1528 } 1529 1530 return (0); 1531 } 1532 1533 static int 1534 do_dynamic(struct module *mp, struct _buf *file) 1535 { 1536 Shdr *dshp, *dstrp, *shp; 1537 char *dyndata, *dstrdata; 1538 int dshn, shn, rc; 1539 1540 /* find and validate the dynamic section (if any) */ 1541 1542 for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) { 1543 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 1544 switch (shp->sh_type) { 1545 case SHT_DYNAMIC: 1546 if (dshp != NULL) { 1547 _kobj_printf(ops, "krtld: get_dynamic: %s, ", 1548 mp->filename); 1549 _kobj_printf(ops, 1550 "multiple dynamic sections\n"); 1551 return (-1); 1552 } else { 1553 dshp = shp; 1554 dshn = shn; 1555 } 1556 break; 1557 } 1558 } 1559 1560 if (dshp == NULL) 1561 return (0); 1562 1563 if (dshp->sh_link > mp->hdr.e_shnum) { 1564 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 1565 _kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link); 1566 return (-1); 1567 } 1568 dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize); 1569 1570 if (dstrp->sh_type != SHT_STRTAB) { 1571 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 1572 _kobj_printf(ops, "sh_link not a string table for section %d\n", 1573 dshn); 1574 return (-1); 1575 } 1576 1577 /* read it from disk */ 1578 1579 dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP); 1580 if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) { 1581 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 1582 _kobj_printf(ops, "error reading section %d\n", dshn); 1583 1584 kobj_free(dyndata, dshp->sh_size); 1585 return (-1); 1586 } 1587 1588 dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP); 1589 if (kobj_read_file(file, dstrdata, dstrp->sh_size, 1590 dstrp->sh_offset) < 0) { 1591 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 1592 _kobj_printf(ops, "error reading section %d\n", dshp->sh_link); 1593 1594 kobj_free(dyndata, dshp->sh_size); 1595 kobj_free(dstrdata, dstrp->sh_size); 1596 return (-1); 1597 } 1598 1599 /* pull the interesting pieces out */ 1600 1601 rc = process_dynamic(mp, dyndata, dstrdata); 1602 1603 kobj_free(dyndata, dshp->sh_size); 1604 kobj_free(dstrdata, dstrp->sh_size); 1605 1606 return (rc); 1607 } 1608 1609 void 1610 kobj_set_ctf(struct module *mp, caddr_t data, size_t size) 1611 { 1612 if (!standalone) { 1613 if (mp->ctfdata != NULL) { 1614 if (vmem_contains(ctf_arena, mp->ctfdata, 1615 mp->ctfsize)) { 1616 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize); 1617 } else { 1618 kobj_free(mp->ctfdata, mp->ctfsize); 1619 } 1620 } 1621 } 1622 1623 /* 1624 * The order is very important here. We need to make sure that 1625 * consumers, at any given instant, see a consistent state. We'd 1626 * rather they see no CTF data than the address of one buffer and the 1627 * size of another. 1628 */ 1629 mp->ctfdata = NULL; 1630 membar_producer(); 1631 mp->ctfsize = size; 1632 mp->ctfdata = data; 1633 membar_producer(); 1634 } 1635 1636 int 1637 kobj_load_module(struct modctl *modp, int use_path) 1638 { 1639 char *filename = modp->mod_filename; 1640 char *modname = modp->mod_modname; 1641 int i; 1642 int n; 1643 struct _buf *file; 1644 struct module *mp = NULL; 1645 #ifdef MODDIR_SUFFIX 1646 int no_suffixdir_drv = 0; 1647 #endif 1648 1649 mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 1650 1651 /* 1652 * We need to prevent kmdb's symbols from leaking into /dev/ksyms. 1653 * kmdb contains a bunch of symbols with well-known names, symbols 1654 * which will mask the real versions, thus causing no end of trouble 1655 * for mdb. 1656 */ 1657 if (strcmp(modp->mod_modname, "kmdbmod") == 0) 1658 mp->flags |= KOBJ_NOKSYMS; 1659 1660 file = kobj_open_path(filename, use_path, 1); 1661 if (file == (struct _buf *)-1) { 1662 #ifdef MODDIR_SUFFIX 1663 file = kobj_open_path(filename, use_path, 0); 1664 #endif 1665 if (file == (struct _buf *)-1) { 1666 kobj_free(mp, sizeof (*mp)); 1667 goto bad; 1668 } 1669 #ifdef MODDIR_SUFFIX 1670 /* 1671 * There is no driver module in the ISA specific (suffix) 1672 * subdirectory but there is a module in the parent directory. 1673 */ 1674 if (strncmp(filename, "drv/", 4) == 0) { 1675 no_suffixdir_drv = 1; 1676 } 1677 #endif 1678 } 1679 1680 mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT); 1681 (void) strcpy(mp->filename, file->_name); 1682 1683 if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) { 1684 _kobj_printf(ops, "kobj_load_module: %s read header failed\n", 1685 modname); 1686 kobj_free(mp->filename, strlen(file->_name) + 1); 1687 kobj_free(mp, sizeof (*mp)); 1688 goto bad; 1689 } 1690 for (i = 0; i < SELFMAG; i++) { 1691 if (mp->hdr.e_ident[i] != ELFMAG[i]) { 1692 if (_moddebug & MODDEBUG_ERRMSG) 1693 _kobj_printf(ops, "%s not an elf module\n", 1694 modname); 1695 kobj_free(mp->filename, strlen(file->_name) + 1); 1696 kobj_free(mp, sizeof (*mp)); 1697 goto bad; 1698 } 1699 } 1700 /* 1701 * It's ELF, but is it our ISA? Interpreting the header 1702 * from a file for a byte-swapped ISA could cause a huge 1703 * and unsatisfiable value to be passed to kobj_alloc below 1704 * and therefore hang booting. 1705 */ 1706 if (!elf_mach_ok(&mp->hdr)) { 1707 if (_moddebug & MODDEBUG_ERRMSG) 1708 _kobj_printf(ops, "%s not an elf module for this ISA\n", 1709 modname); 1710 kobj_free(mp->filename, strlen(file->_name) + 1); 1711 kobj_free(mp, sizeof (*mp)); 1712 #ifdef MODDIR_SUFFIX 1713 /* 1714 * The driver mod is not in the ISA specific subdirectory 1715 * and the module in the parent directory is not our ISA. 1716 * If it is our ISA, for now we will silently succeed. 1717 */ 1718 if (no_suffixdir_drv == 1) { 1719 cmn_err(CE_CONT, "?NOTICE: %s: 64-bit driver module" 1720 " not found\n", modname); 1721 } 1722 #endif 1723 goto bad; 1724 } 1725 1726 /* 1727 * All modules, save for unix, should be relocatable (as opposed to 1728 * dynamic). Dynamic modules come with PLTs and GOTs, which can't 1729 * currently be processed by krtld. 1730 */ 1731 if (mp->hdr.e_type != ET_REL) { 1732 if (_moddebug & MODDEBUG_ERRMSG) 1733 _kobj_printf(ops, "%s isn't a relocatable (ET_REL) " 1734 "module\n", modname); 1735 kobj_free(mp->filename, strlen(file->_name) + 1); 1736 kobj_free(mp, sizeof (*mp)); 1737 goto bad; 1738 } 1739 1740 n = mp->hdr.e_shentsize * mp->hdr.e_shnum; 1741 mp->shdrs = kobj_alloc(n, KM_WAIT); 1742 1743 if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) { 1744 _kobj_printf(ops, "kobj_load_module: %s error reading " 1745 "section headers\n", modname); 1746 kobj_free(mp->shdrs, n); 1747 kobj_free(mp->filename, strlen(file->_name) + 1); 1748 kobj_free(mp, sizeof (*mp)); 1749 goto bad; 1750 } 1751 1752 kobj_notify(KOBJ_NOTIFY_MODLOADING, modp); 1753 module_assign(modp, mp); 1754 1755 /* read in sections */ 1756 if (get_progbits(mp, file) < 0) { 1757 _kobj_printf(ops, "%s error reading sections\n", modname); 1758 goto bad; 1759 } 1760 1761 if (do_dynamic(mp, file) < 0) { 1762 _kobj_printf(ops, "%s error reading dynamic section\n", 1763 modname); 1764 goto bad; 1765 } 1766 1767 modp->mod_text = mp->text; 1768 modp->mod_text_size = mp->text_size; 1769 1770 /* read in symbols; adjust values for each section's real address */ 1771 if (get_syms(mp, file) < 0) { 1772 _kobj_printf(ops, "%s error reading symbols\n", 1773 modname); 1774 goto bad; 1775 } 1776 1777 /* 1778 * If we didn't dependency information from the dynamic section, look 1779 * for it the old-fashioned way. 1780 */ 1781 if (mp->depends_on == NULL) 1782 mp->depends_on = depends_on(mp); 1783 1784 if (get_ctf(mp, file) < 0) { 1785 _kobj_printf(ops, "%s debug information will not " 1786 "be available\n", modname); 1787 } 1788 1789 /* primary kernel modules do not have a signature section */ 1790 if (!(mp->flags & KOBJ_PRIM)) 1791 get_signature(mp, file); 1792 1793 #ifdef KOBJ_DEBUG 1794 if (kobj_debug & D_LOADING) { 1795 _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 1796 _kobj_printf(ops, "\ttext:0x%p", mp->text); 1797 _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 1798 _kobj_printf(ops, "\tdata:0x%p", mp->data); 1799 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 1800 } 1801 #endif /* KOBJ_DEBUG */ 1802 1803 /* 1804 * For primary kernel modules, we defer 1805 * symbol resolution and relocation until 1806 * all primary objects have been loaded. 1807 */ 1808 if (!standalone) { 1809 int ddrval, dcrval; 1810 char *dependent_modname; 1811 /* load all dependents */ 1812 dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT); 1813 ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN); 1814 1815 /* 1816 * resolve undefined and common symbols, 1817 * also allocates common space 1818 */ 1819 if ((dcrval = do_common(mp)) < 0) { 1820 switch (dcrval) { 1821 case DOSYM_UNSAFE: 1822 _kobj_printf(ops, "WARNING: mod_load: " 1823 "MT-unsafe module '%s' rejected\n", 1824 modname); 1825 break; 1826 case DOSYM_UNDEF: 1827 _kobj_printf(ops, "WARNING: mod_load: " 1828 "cannot load module '%s'\n", 1829 modname); 1830 if (ddrval == -1) { 1831 _kobj_printf(ops, "WARNING: %s: ", 1832 modname); 1833 _kobj_printf(ops, 1834 "unable to resolve dependency, " 1835 "module '%s' not found\n", 1836 dependent_modname); 1837 } 1838 break; 1839 } 1840 } 1841 kobj_free(dependent_modname, MODMAXNAMELEN); 1842 if (dcrval < 0) 1843 goto bad; 1844 1845 /* process relocation tables */ 1846 if (do_relocations(mp) < 0) { 1847 _kobj_printf(ops, "%s error doing relocations\n", 1848 modname); 1849 goto bad; 1850 } 1851 1852 if (mp->destination) { 1853 off_t off = (uintptr_t)mp->destination & PAGEOFFSET; 1854 caddr_t base = (caddr_t)mp->destination - off; 1855 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE); 1856 1857 hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK); 1858 vmem_free(heap_arena, base, size); 1859 } 1860 1861 /* sync_instruction_memory */ 1862 kobj_sync_instruction_memory(mp->text, mp->text_size); 1863 #ifdef MPSAS 1864 sas_syms(mp); 1865 #endif 1866 kobj_export_module(mp); 1867 kobj_notify(KOBJ_NOTIFY_MODLOADED, modp); 1868 } 1869 kobj_close_file(file); 1870 return (0); 1871 bad: 1872 if (file != (struct _buf *)-1) 1873 kobj_close_file(file); 1874 if (modp->mod_mp != NULL) 1875 free_module_data(modp->mod_mp); 1876 1877 module_assign(modp, NULL); 1878 return ((file == (struct _buf *)-1) ? ENOENT : EINVAL); 1879 } 1880 1881 int 1882 kobj_load_primary_module(struct modctl *modp) 1883 { 1884 struct modctl *dep; 1885 struct module *mp; 1886 1887 if (kobj_load_module(modp, 0) != 0) 1888 return (-1); 1889 1890 mp = modp->mod_mp; 1891 mp->flags |= KOBJ_PRIM; 1892 1893 /* Bind new module to its dependents */ 1894 if (mp->depends_on != NULL && (dep = 1895 mod_already_loaded(mp->depends_on)) == NULL) { 1896 #ifdef KOBJ_DEBUG 1897 if (kobj_debug & D_DEBUG) { 1898 _kobj_printf(ops, "krtld: failed to resolve deps " 1899 "for primary %s\n", modp->mod_modname); 1900 } 1901 #endif 1902 return (-1); 1903 } 1904 1905 add_dependent(mp, dep->mod_mp); 1906 1907 /* 1908 * Relocate it. This module may not be part of a link map, so we 1909 * can't use bind_primary. 1910 */ 1911 if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 || 1912 do_relocations(mp) < 0) { 1913 #ifdef KOBJ_DEBUG 1914 if (kobj_debug & D_DEBUG) { 1915 _kobj_printf(ops, "krtld: failed to relocate " 1916 "primary %s\n", modp->mod_modname); 1917 } 1918 #endif 1919 return (-1); 1920 } 1921 1922 return (0); 1923 } 1924 1925 static void 1926 module_assign(struct modctl *cp, struct module *mp) 1927 { 1928 if (standalone) { 1929 cp->mod_mp = mp; 1930 return; 1931 } 1932 mutex_enter(&mod_lock); 1933 cp->mod_mp = mp; 1934 cp->mod_gencount++; 1935 mutex_exit(&mod_lock); 1936 } 1937 1938 void 1939 kobj_unload_module(struct modctl *modp) 1940 { 1941 struct module *mp = modp->mod_mp; 1942 1943 if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) { 1944 _kobj_printf(ops, "text for %s ", mp->filename); 1945 _kobj_printf(ops, "was at %p\n", mp->text); 1946 mp->text = NULL; /* don't actually free it */ 1947 } 1948 1949 kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp); 1950 1951 /* 1952 * Null out mod_mp first, so consumers (debuggers) know not to look 1953 * at the module structure any more. 1954 */ 1955 mutex_enter(&mod_lock); 1956 modp->mod_mp = NULL; 1957 mutex_exit(&mod_lock); 1958 1959 kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp); 1960 free_module_data(mp); 1961 } 1962 1963 static void 1964 free_module_data(struct module *mp) 1965 { 1966 struct module_list *lp, *tmp; 1967 int ksyms_exported = 0; 1968 1969 lp = mp->head; 1970 while (lp) { 1971 tmp = lp; 1972 lp = lp->next; 1973 kobj_free((char *)tmp, sizeof (*tmp)); 1974 } 1975 1976 rw_enter(&ksyms_lock, RW_WRITER); 1977 if (mp->symspace) { 1978 if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) { 1979 vmem_free(ksyms_arena, mp->symspace, mp->symsize); 1980 ksyms_exported = 1; 1981 } else { 1982 if (mp->flags & KOBJ_NOKSYMS) 1983 ksyms_exported = 1; 1984 kobj_free(mp->symspace, mp->symsize); 1985 } 1986 } 1987 rw_exit(&ksyms_lock); 1988 1989 if (mp->ctfdata) { 1990 if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize)) 1991 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize); 1992 else 1993 kobj_free(mp->ctfdata, mp->ctfsize); 1994 } 1995 1996 if (mp->sigdata) 1997 kobj_free(mp->sigdata, mp->sigsize); 1998 1999 /* 2000 * We did not get far enough into kobj_export_ksyms() to free allocated 2001 * buffers because we encounted error conditions. Free the buffers. 2002 */ 2003 if ((ksyms_exported == 0) && (mp->shdrs != NULL)) { 2004 uint_t shn; 2005 Shdr *shp; 2006 2007 for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 2008 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 2009 switch (shp->sh_type) { 2010 case SHT_RELA: 2011 case SHT_REL: 2012 if (shp->sh_addr != 0) 2013 kobj_free((void *)shp->sh_addr, 2014 shp->sh_size); 2015 break; 2016 } 2017 } 2018 err_free_done: 2019 if (!(mp->flags & KOBJ_PRIM)) { 2020 kobj_free(mp->shdrs, 2021 mp->hdr.e_shentsize * mp->hdr.e_shnum); 2022 } 2023 } 2024 2025 if (mp->bss) 2026 vmem_free(data_arena, (void *)mp->bss, mp->bss_size); 2027 2028 if (mp->fbt_tab) 2029 kobj_texthole_free(mp->fbt_tab, mp->fbt_size); 2030 2031 if (mp->textwin_base) 2032 kobj_textwin_free(mp); 2033 2034 if (mp->sdt_probes != NULL) { 2035 sdt_probedesc_t *sdp = mp->sdt_probes, *next; 2036 2037 while (sdp != NULL) { 2038 next = sdp->sdpd_next; 2039 kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1); 2040 kobj_free(sdp, sizeof (sdt_probedesc_t)); 2041 sdp = next; 2042 } 2043 } 2044 2045 if (mp->sdt_tab) 2046 kobj_texthole_free(mp->sdt_tab, mp->sdt_size); 2047 if (mp->text) 2048 vmem_free(text_arena, mp->text, mp->text_size); 2049 if (mp->data) 2050 vmem_free(data_arena, mp->data, mp->data_size); 2051 if (mp->depends_on) 2052 kobj_free(mp->depends_on, strlen(mp->depends_on)+1); 2053 if (mp->filename) 2054 kobj_free(mp->filename, strlen(mp->filename)+1); 2055 2056 kobj_free((char *)mp, sizeof (*mp)); 2057 } 2058 2059 static int 2060 get_progbits(struct module *mp, struct _buf *file) 2061 { 2062 struct proginfo *tp, *dp, *sdp; 2063 Shdr *shp; 2064 reloc_dest_t dest = NULL; 2065 uintptr_t bits_ptr; 2066 uintptr_t text = 0, data, sdata = 0, textptr; 2067 uint_t shn; 2068 int err = -1; 2069 2070 tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 2071 dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 2072 sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 2073 /* 2074 * loop through sections to find out how much space we need 2075 * for text, data, (also bss that is already assigned) 2076 */ 2077 if (get_progbits_size(mp, tp, dp, sdp) < 0) 2078 goto done; 2079 2080 mp->text_size = tp->size; 2081 mp->data_size = dp->size; 2082 2083 if (standalone) { 2084 mp->text = kobj_segbrk(&_etext, mp->text_size, 2085 tp->align, _data); 2086 /* 2087 * If we can't grow the text segment, try the 2088 * data segment before failing. 2089 */ 2090 if (mp->text == NULL) { 2091 mp->text = kobj_segbrk(&_edata, mp->text_size, 2092 tp->align, 0); 2093 } 2094 2095 mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0); 2096 2097 if (mp->text == NULL || mp->data == NULL) 2098 goto done; 2099 2100 } else { 2101 if (text_arena == NULL) 2102 kobj_vmem_init(&text_arena, &data_arena); 2103 2104 /* 2105 * some architectures may want to load the module on a 2106 * page that is currently read only. It may not be 2107 * possible for those architectures to remap their page 2108 * on the fly. So we provide a facility for them to hang 2109 * a private hook where the memory they assign the module 2110 * is not the actual place where the module loads. 2111 * 2112 * In this case there are two addresses that deal with the 2113 * modload. 2114 * 1) the final destination of the module 2115 * 2) the address that is used to view the newly 2116 * loaded module until all the relocations relative to 1 2117 * above are completed. 2118 * 2119 * That is what dest is used for below. 2120 */ 2121 mp->text_size += tp->align; 2122 mp->data_size += dp->align; 2123 2124 mp->text = kobj_text_alloc(text_arena, mp->text_size); 2125 2126 /* 2127 * a remap is taking place. Align the text ptr relative 2128 * to the secondary mapping. That is where the bits will 2129 * be read in. 2130 */ 2131 if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena, 2132 mp->text, mp->text_size)) { 2133 off_t off = (uintptr_t)mp->text & PAGEOFFSET; 2134 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE); 2135 caddr_t map = vmem_alloc(heap_arena, size, VM_SLEEP); 2136 caddr_t orig = mp->text - off; 2137 pgcnt_t pages = size / PAGESIZE; 2138 2139 dest = (reloc_dest_t)(map + off); 2140 text = ALIGN((uintptr_t)dest, tp->align); 2141 2142 while (pages--) { 2143 hat_devload(kas.a_hat, map, PAGESIZE, 2144 hat_getpfnum(kas.a_hat, orig), 2145 PROT_READ | PROT_WRITE | PROT_EXEC, 2146 HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 2147 map += PAGESIZE; 2148 orig += PAGESIZE; 2149 } 2150 /* 2151 * Since we set up a non-cacheable mapping, we need 2152 * to flush any old entries in the cache that might 2153 * be left around from the read-only mapping. 2154 */ 2155 dcache_flushall(); 2156 } 2157 if (mp->data_size) 2158 mp->data = vmem_alloc(data_arena, mp->data_size, 2159 VM_SLEEP | VM_BESTFIT); 2160 } 2161 textptr = (uintptr_t)mp->text; 2162 textptr = ALIGN(textptr, tp->align); 2163 mp->destination = dest; 2164 2165 /* 2166 * This is the case where a remap is not being done. 2167 */ 2168 if (text == 0) 2169 text = ALIGN((uintptr_t)mp->text, tp->align); 2170 data = ALIGN((uintptr_t)mp->data, dp->align); 2171 2172 /* now loop though sections assigning addresses and loading the data */ 2173 for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 2174 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 2175 if (!(shp->sh_flags & SHF_ALLOC)) 2176 continue; 2177 2178 if ((shp->sh_flags & SHF_WRITE) == 0) 2179 bits_ptr = text; 2180 else if (shp->sh_flags & SHF_NEUT_SHORT) 2181 bits_ptr = sdata; 2182 else 2183 bits_ptr = data; 2184 2185 bits_ptr = ALIGN(bits_ptr, shp->sh_addralign); 2186 2187 if (shp->sh_type == SHT_NOBITS) { 2188 /* 2189 * Zero bss. 2190 */ 2191 bzero((caddr_t)bits_ptr, shp->sh_size); 2192 shp->sh_type = SHT_PROGBITS; 2193 } else { 2194 if (kobj_read_file(file, (char *)bits_ptr, 2195 shp->sh_size, shp->sh_offset) < 0) 2196 goto done; 2197 } 2198 2199 if (shp->sh_flags & SHF_WRITE) { 2200 shp->sh_addr = bits_ptr; 2201 } else { 2202 textptr = ALIGN(textptr, shp->sh_addralign); 2203 shp->sh_addr = textptr; 2204 textptr += shp->sh_size; 2205 } 2206 2207 bits_ptr += shp->sh_size; 2208 if ((shp->sh_flags & SHF_WRITE) == 0) 2209 text = bits_ptr; 2210 else if (shp->sh_flags & SHF_NEUT_SHORT) 2211 sdata = bits_ptr; 2212 else 2213 data = bits_ptr; 2214 } 2215 2216 err = 0; 2217 done: 2218 /* 2219 * Free and mark as freed the section headers here so that 2220 * free_module_data() does not have to worry about this buffer. 2221 * 2222 * This buffer is freed here because one of the possible reasons 2223 * for error is a section with non-zero sh_addr and in that case 2224 * free_module_data() would have no way of recognizing that this 2225 * buffer was unallocated. 2226 */ 2227 if (err != 0) { 2228 kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum); 2229 mp->shdrs = NULL; 2230 } 2231 2232 (void) kobj_free(tp, sizeof (struct proginfo)); 2233 (void) kobj_free(dp, sizeof (struct proginfo)); 2234 (void) kobj_free(sdp, sizeof (struct proginfo)); 2235 2236 return (err); 2237 } 2238 2239 /* 2240 * Go through suppress_sym_list to see if "multiply defined" 2241 * warning of this symbol should be suppressed. Return 1 if 2242 * warning should be suppressed, 0 otherwise. 2243 */ 2244 static int 2245 kobj_suppress_warning(char *symname) 2246 { 2247 int i; 2248 2249 for (i = 0; suppress_sym_list[i] != NULL; i++) { 2250 if (strcmp(suppress_sym_list[i], symname) == 0) 2251 return (1); 2252 } 2253 2254 return (0); 2255 } 2256 2257 static int 2258 get_syms(struct module *mp, struct _buf *file) 2259 { 2260 uint_t shn; 2261 Shdr *shp; 2262 uint_t i; 2263 Sym *sp, *ksp; 2264 char *symname; 2265 int dosymtab = 0; 2266 extern char stubs_base[], stubs_end[]; 2267 2268 /* 2269 * Find the interesting sections. 2270 */ 2271 for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 2272 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 2273 switch (shp->sh_type) { 2274 case SHT_SYMTAB: 2275 mp->symtbl_section = shn; 2276 mp->symhdr = shp; 2277 dosymtab++; 2278 break; 2279 2280 case SHT_RELA: 2281 case SHT_REL: 2282 /* 2283 * Already loaded. 2284 */ 2285 if (shp->sh_addr) 2286 continue; 2287 shp->sh_addr = (Addr) 2288 kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP); 2289 2290 if (kobj_read_file(file, (char *)shp->sh_addr, 2291 shp->sh_size, shp->sh_offset) < 0) { 2292 _kobj_printf(ops, "krtld: get_syms: %s, ", 2293 mp->filename); 2294 _kobj_printf(ops, "error reading section %d\n", 2295 shn); 2296 return (-1); 2297 } 2298 break; 2299 } 2300 } 2301 2302 /* 2303 * This is true for a stripped executable. In the case of 2304 * 'unix' it can be stripped but it still contains the SHT_DYNSYM, 2305 * and since that symbol information is still present everything 2306 * is just fine. 2307 */ 2308 if (!dosymtab) { 2309 if (mp->flags & KOBJ_EXEC) 2310 return (0); 2311 _kobj_printf(ops, "krtld: get_syms: %s ", 2312 mp->filename); 2313 _kobj_printf(ops, "no SHT_SYMTAB symbol table found\n"); 2314 return (-1); 2315 } 2316 2317 /* 2318 * get the associated string table header 2319 */ 2320 if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum)) 2321 return (-1); 2322 mp->strhdr = (Shdr *) 2323 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 2324 2325 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 2326 mp->hashsize = kobj_gethashsize(mp->nsyms); 2327 2328 /* 2329 * Allocate space for the symbol table, buckets, chains, and strings. 2330 */ 2331 mp->symsize = mp->symhdr->sh_size + 2332 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size; 2333 mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH); 2334 2335 mp->symtbl = mp->symspace; 2336 mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size); 2337 mp->chains = mp->buckets + mp->hashsize; 2338 mp->strings = (char *)(mp->chains + mp->nsyms); 2339 2340 if (kobj_read_file(file, mp->symtbl, 2341 mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 || 2342 kobj_read_file(file, mp->strings, 2343 mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0) 2344 return (-1); 2345 2346 /* 2347 * loop through the symbol table adjusting values to account 2348 * for where each section got loaded into memory. Also 2349 * fill in the hash table. 2350 */ 2351 for (i = 1; i < mp->nsyms; i++) { 2352 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 2353 if (sp->st_shndx < SHN_LORESERVE) { 2354 if (sp->st_shndx >= mp->hdr.e_shnum) { 2355 _kobj_printf(ops, "%s bad shndx ", 2356 file->_name); 2357 _kobj_printf(ops, "in symbol %d\n", i); 2358 return (-1); 2359 } 2360 shp = (Shdr *) 2361 (mp->shdrs + 2362 sp->st_shndx * mp->hdr.e_shentsize); 2363 if (!(mp->flags & KOBJ_EXEC)) 2364 sp->st_value += shp->sh_addr; 2365 } 2366 2367 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 2368 continue; 2369 if (sp->st_name >= mp->strhdr->sh_size) 2370 return (-1); 2371 2372 symname = mp->strings + sp->st_name; 2373 2374 if (!(mp->flags & KOBJ_EXEC) && 2375 ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 2376 ksp = kobj_lookup_all(mp, symname, 0); 2377 2378 if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL && 2379 !kobj_suppress_warning(symname) && 2380 sp->st_shndx != SHN_UNDEF && 2381 sp->st_shndx != SHN_COMMON && 2382 ksp->st_shndx != SHN_UNDEF && 2383 ksp->st_shndx != SHN_COMMON) { 2384 /* 2385 * Unless this symbol is a stub, it's multiply 2386 * defined. Multiply-defined symbols are 2387 * usually bad, but some objects (kmdb) have 2388 * a legitimate need to have their own 2389 * copies of common functions. 2390 */ 2391 if ((standalone || 2392 ksp->st_value < (uintptr_t)stubs_base || 2393 ksp->st_value >= (uintptr_t)stubs_end) && 2394 !(mp->flags & KOBJ_IGNMULDEF)) { 2395 _kobj_printf(ops, 2396 "%s symbol ", file->_name); 2397 _kobj_printf(ops, 2398 "%s multiply defined\n", symname); 2399 } 2400 } 2401 } 2402 sym_insert(mp, symname, i); 2403 } 2404 2405 return (0); 2406 } 2407 2408 static int 2409 get_ctf(struct module *mp, struct _buf *file) 2410 { 2411 char *shstrtab, *ctfdata; 2412 size_t shstrlen; 2413 Shdr *shp; 2414 uint_t i; 2415 2416 if (_moddebug & MODDEBUG_NOCTF) 2417 return (0); /* do not attempt to even load CTF data */ 2418 2419 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) { 2420 _kobj_printf(ops, "krtld: get_ctf: %s, ", 2421 mp->filename); 2422 _kobj_printf(ops, "corrupt e_shstrndx %u\n", 2423 mp->hdr.e_shstrndx); 2424 return (-1); 2425 } 2426 2427 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize); 2428 shstrlen = shp->sh_size; 2429 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP); 2430 2431 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) { 2432 _kobj_printf(ops, "krtld: get_ctf: %s, ", 2433 mp->filename); 2434 _kobj_printf(ops, "error reading section %u\n", 2435 mp->hdr.e_shstrndx); 2436 kobj_free(shstrtab, shstrlen); 2437 return (-1); 2438 } 2439 2440 for (i = 0; i < mp->hdr.e_shnum; i++) { 2441 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize); 2442 2443 if (shp->sh_size != 0 && shp->sh_name < shstrlen && 2444 strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) { 2445 ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH); 2446 2447 if (kobj_read_file(file, ctfdata, shp->sh_size, 2448 shp->sh_offset) < 0) { 2449 _kobj_printf(ops, "krtld: get_ctf: %s, error " 2450 "reading .SUNW_ctf data\n", mp->filename); 2451 kobj_free(ctfdata, shp->sh_size); 2452 kobj_free(shstrtab, shstrlen); 2453 return (-1); 2454 } 2455 2456 mp->ctfdata = ctfdata; 2457 mp->ctfsize = shp->sh_size; 2458 break; 2459 } 2460 } 2461 2462 kobj_free(shstrtab, shstrlen); 2463 return (0); 2464 } 2465 2466 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */ 2467 2468 /* 2469 * Return the hash of the ELF sections that are memory resident. 2470 * i.e. text and data. We skip a SHT_NOBITS section since it occupies 2471 * no space in the file. We use SHA1 here since libelfsign uses 2472 * it and both places need to use the same algorithm. 2473 */ 2474 static void 2475 crypto_es_hash(struct module *mp, char *hash, char *shstrtab) 2476 { 2477 uint_t shn; 2478 Shdr *shp; 2479 SHA1_CTX ctx; 2480 2481 SHA1Init(&ctx); 2482 2483 for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 2484 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 2485 if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0) 2486 continue; 2487 2488 /* 2489 * The check should ideally be shp->sh_type == SHT_NOBITS. 2490 * However, we can't do that check here as get_progbits() 2491 * resets the type. 2492 */ 2493 if (strcmp(shstrtab + shp->sh_name, ".bss") == 0) 2494 continue; 2495 #ifdef KOBJ_DEBUG 2496 if (kobj_debug & D_DEBUG) 2497 _kobj_printf(ops, 2498 "krtld: crypto_es_hash: updating hash with" 2499 " %s data size=%d\n", shstrtab + shp->sh_name, 2500 shp->sh_size); 2501 #endif 2502 ASSERT(shp->sh_addr != NULL); 2503 SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size); 2504 } 2505 2506 SHA1Final((uchar_t *)hash, &ctx); 2507 } 2508 2509 /* 2510 * Get the .SUNW_signature section for the module, it it exists. 2511 * 2512 * This section exists only for crypto modules. None of the 2513 * primary modules have this section currently. 2514 */ 2515 static void 2516 get_signature(struct module *mp, struct _buf *file) 2517 { 2518 char *shstrtab, *sigdata = NULL; 2519 size_t shstrlen; 2520 Shdr *shp; 2521 uint_t i; 2522 2523 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) { 2524 _kobj_printf(ops, "krtld: get_signature: %s, ", 2525 mp->filename); 2526 _kobj_printf(ops, "corrupt e_shstrndx %u\n", 2527 mp->hdr.e_shstrndx); 2528 return; 2529 } 2530 2531 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize); 2532 shstrlen = shp->sh_size; 2533 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP); 2534 2535 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) { 2536 _kobj_printf(ops, "krtld: get_signature: %s, ", 2537 mp->filename); 2538 _kobj_printf(ops, "error reading section %u\n", 2539 mp->hdr.e_shstrndx); 2540 kobj_free(shstrtab, shstrlen); 2541 return; 2542 } 2543 2544 for (i = 0; i < mp->hdr.e_shnum; i++) { 2545 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize); 2546 if (shp->sh_size != 0 && shp->sh_name < shstrlen && 2547 strcmp(shstrtab + shp->sh_name, 2548 ELF_SIGNATURE_SECTION) == 0) { 2549 filesig_vers_t filesig_version; 2550 size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH; 2551 sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH); 2552 2553 if (kobj_read_file(file, sigdata, shp->sh_size, 2554 shp->sh_offset) < 0) { 2555 _kobj_printf(ops, "krtld: get_signature: %s," 2556 " error reading .SUNW_signature data\n", 2557 mp->filename); 2558 kobj_free(sigdata, sigsize); 2559 kobj_free(shstrtab, shstrlen); 2560 return; 2561 } 2562 filesig_version = ((struct filesignatures *)sigdata)-> 2563 filesig_sig.filesig_version; 2564 if (!(filesig_version == FILESIG_VERSION1 || 2565 filesig_version == FILESIG_VERSION3)) { 2566 /* skip versions we don't understand */ 2567 kobj_free(sigdata, sigsize); 2568 kobj_free(shstrtab, shstrlen); 2569 return; 2570 } 2571 2572 mp->sigdata = sigdata; 2573 mp->sigsize = sigsize; 2574 break; 2575 } 2576 } 2577 2578 if (sigdata != NULL) { 2579 crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab); 2580 } 2581 2582 kobj_free(shstrtab, shstrlen); 2583 } 2584 2585 static void 2586 add_dependent(struct module *mp, struct module *dep) 2587 { 2588 struct module_list *lp; 2589 2590 for (lp = mp->head; lp; lp = lp->next) { 2591 if (lp->mp == dep) 2592 return; /* already on the list */ 2593 } 2594 2595 if (lp == NULL) { 2596 lp = kobj_zalloc(sizeof (*lp), KM_WAIT); 2597 2598 lp->mp = dep; 2599 lp->next = NULL; 2600 if (mp->tail) 2601 mp->tail->next = lp; 2602 else 2603 mp->head = lp; 2604 mp->tail = lp; 2605 } 2606 } 2607 2608 static int 2609 do_dependents(struct modctl *modp, char *modname, size_t modnamelen) 2610 { 2611 struct module *mp; 2612 struct modctl *req; 2613 char *d, *p, *q; 2614 int c; 2615 char *err_modname = NULL; 2616 2617 mp = modp->mod_mp; 2618 2619 if ((p = mp->depends_on) == NULL) 2620 return (0); 2621 2622 for (;;) { 2623 /* 2624 * Skip space. 2625 */ 2626 while (*p && (*p == ' ' || *p == '\t')) 2627 p++; 2628 /* 2629 * Get module name. 2630 */ 2631 d = p; 2632 q = modname; 2633 c = 0; 2634 while (*p && *p != ' ' && *p != '\t') { 2635 if (c < modnamelen - 1) { 2636 *q++ = *p; 2637 c++; 2638 } 2639 p++; 2640 } 2641 2642 if (q == modname) 2643 break; 2644 2645 if (c == modnamelen - 1) { 2646 char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP); 2647 2648 (void) strncpy(dep, d, p - d + 1); 2649 dep[p - d] = '\0'; 2650 2651 _kobj_printf(ops, "%s: dependency ", modp->mod_modname); 2652 _kobj_printf(ops, "'%s' too long ", dep); 2653 _kobj_printf(ops, "(max %d chars)\n", modnamelen); 2654 2655 kobj_free(dep, p - d + 1); 2656 2657 return (-1); 2658 } 2659 2660 *q = '\0'; 2661 if ((req = mod_load_requisite(modp, modname)) == NULL) { 2662 #ifndef KOBJ_DEBUG 2663 if (_moddebug & MODDEBUG_LOADMSG) { 2664 #endif /* KOBJ_DEBUG */ 2665 _kobj_printf(ops, 2666 "%s: unable to resolve dependency, ", 2667 modp->mod_modname); 2668 _kobj_printf(ops, "cannot load module '%s'\n", 2669 modname); 2670 #ifndef KOBJ_DEBUG 2671 } 2672 #endif /* KOBJ_DEBUG */ 2673 if (err_modname == NULL) { 2674 /* 2675 * This must be the same size as the modname 2676 * one. 2677 */ 2678 err_modname = kobj_zalloc(MODMAXNAMELEN, 2679 KM_WAIT); 2680 2681 /* 2682 * We can use strcpy() here without fearing 2683 * the NULL terminator because the size of 2684 * err_modname is the same as one of modname, 2685 * and it's filled with zeros. 2686 */ 2687 (void) strcpy(err_modname, modname); 2688 } 2689 continue; 2690 } 2691 2692 add_dependent(mp, req->mod_mp); 2693 mod_release_mod(req); 2694 2695 } 2696 2697 if (err_modname != NULL) { 2698 /* 2699 * Copy the first module name where you detect an error to keep 2700 * its behavior the same as before. 2701 * This way keeps minimizing the memory use for error 2702 * modules, and this might be important at boot time because 2703 * the memory usage is a crucial factor for booting in most 2704 * cases. You can expect more verbose messages when using 2705 * a debug kernel or setting a bit in moddebug. 2706 */ 2707 bzero(modname, MODMAXNAMELEN); 2708 (void) strcpy(modname, err_modname); 2709 kobj_free(err_modname, MODMAXNAMELEN); 2710 return (-1); 2711 } 2712 2713 return (0); 2714 } 2715 2716 static int 2717 do_common(struct module *mp) 2718 { 2719 int err; 2720 2721 /* 2722 * first time through, assign all symbols defined in other 2723 * modules, and count up how much common space will be needed 2724 * (bss_size and bss_align) 2725 */ 2726 if ((err = do_symbols(mp, 0)) < 0) 2727 return (err); 2728 /* 2729 * increase bss_size by the maximum delta that could be 2730 * computed by the ALIGN below 2731 */ 2732 mp->bss_size += mp->bss_align; 2733 if (mp->bss_size) { 2734 if (standalone) 2735 mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size, 2736 MINALIGN, 0); 2737 else 2738 mp->bss = (uintptr_t)vmem_alloc(data_arena, 2739 mp->bss_size, VM_SLEEP | VM_BESTFIT); 2740 bzero((void *)mp->bss, mp->bss_size); 2741 /* now assign addresses to all common symbols */ 2742 if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0) 2743 return (err); 2744 } 2745 return (0); 2746 } 2747 2748 static int 2749 do_symbols(struct module *mp, Elf64_Addr bss_base) 2750 { 2751 int bss_align; 2752 uintptr_t bss_ptr; 2753 int err; 2754 int i; 2755 Sym *sp, *sp1; 2756 char *name; 2757 int assign; 2758 int resolved = 1; 2759 2760 /* 2761 * Nothing left to do (optimization). 2762 */ 2763 if (mp->flags & KOBJ_RESOLVED) 2764 return (0); 2765 2766 assign = (bss_base) ? 1 : 0; 2767 bss_ptr = bss_base; 2768 bss_align = 0; 2769 err = 0; 2770 2771 for (i = 1; i < mp->nsyms; i++) { 2772 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i); 2773 /* 2774 * we know that st_name is in bounds, since get_sections 2775 * has already checked all of the symbols 2776 */ 2777 name = mp->strings + sp->st_name; 2778 if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON) 2779 continue; 2780 #ifdef __sparc 2781 /* 2782 * Register symbols are ignored in the kernel 2783 */ 2784 if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) { 2785 if (*name != '\0') { 2786 _kobj_printf(ops, "%s: named REGISTER symbol ", 2787 mp->filename); 2788 _kobj_printf(ops, "not supported '%s'\n", 2789 name); 2790 err = DOSYM_UNDEF; 2791 } 2792 continue; 2793 } 2794 #endif /* __sparc */ 2795 /* 2796 * TLS symbols are ignored in the kernel 2797 */ 2798 if (ELF_ST_TYPE(sp->st_info) == STT_TLS) { 2799 _kobj_printf(ops, "%s: TLS symbol ", 2800 mp->filename); 2801 _kobj_printf(ops, "not supported '%s'\n", 2802 name); 2803 err = DOSYM_UNDEF; 2804 continue; 2805 } 2806 2807 if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) { 2808 if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) { 2809 sp->st_shndx = SHN_ABS; 2810 sp->st_value = sp1->st_value; 2811 continue; 2812 } 2813 } 2814 2815 if (sp->st_shndx == SHN_UNDEF) { 2816 resolved = 0; 2817 2818 if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0) 2819 continue; 2820 2821 /* 2822 * If it's not a weak reference and it's 2823 * not a primary object, it's an error. 2824 * (Primary objects may take more than 2825 * one pass to resolve) 2826 */ 2827 if (!(mp->flags & KOBJ_PRIM) && 2828 ELF_ST_BIND(sp->st_info) != STB_WEAK) { 2829 _kobj_printf(ops, "%s: undefined symbol", 2830 mp->filename); 2831 _kobj_printf(ops, " '%s'\n", name); 2832 /* 2833 * Try to determine whether this symbol 2834 * represents a dependency on obsolete 2835 * unsafe driver support. This is just 2836 * to make the warning more informative. 2837 */ 2838 if (strcmp(name, "sleep") == 0 || 2839 strcmp(name, "unsleep") == 0 || 2840 strcmp(name, "wakeup") == 0 || 2841 strcmp(name, "bsd_compat_ioctl") == 0 || 2842 strcmp(name, "unsafe_driver") == 0 || 2843 strncmp(name, "spl", 3) == 0 || 2844 strncmp(name, "i_ddi_spl", 9) == 0) 2845 err = DOSYM_UNSAFE; 2846 if (err == 0) 2847 err = DOSYM_UNDEF; 2848 } 2849 continue; 2850 } 2851 /* 2852 * It's a common symbol - st_value is the 2853 * required alignment. 2854 */ 2855 if (sp->st_value > bss_align) 2856 bss_align = sp->st_value; 2857 bss_ptr = ALIGN(bss_ptr, sp->st_value); 2858 if (assign) { 2859 sp->st_shndx = SHN_ABS; 2860 sp->st_value = bss_ptr; 2861 } 2862 bss_ptr += sp->st_size; 2863 } 2864 if (err) 2865 return (err); 2866 if (assign == 0 && mp->bss == NULL) { 2867 mp->bss_align = bss_align; 2868 mp->bss_size = bss_ptr; 2869 } else if (resolved) { 2870 mp->flags |= KOBJ_RESOLVED; 2871 } 2872 2873 return (0); 2874 } 2875 2876 uint_t 2877 kobj_hash_name(const char *p) 2878 { 2879 unsigned int g; 2880 uint_t hval; 2881 2882 hval = 0; 2883 while (*p) { 2884 hval = (hval << 4) + *p++; 2885 if ((g = (hval & 0xf0000000)) != 0) 2886 hval ^= g >> 24; 2887 hval &= ~g; 2888 } 2889 return (hval); 2890 } 2891 2892 /* look for name in all modules */ 2893 uintptr_t 2894 kobj_getsymvalue(char *name, int kernelonly) 2895 { 2896 Sym *sp; 2897 struct modctl *modp; 2898 struct module *mp; 2899 uintptr_t value = 0; 2900 2901 if ((sp = kobj_lookup_kernel(name)) != NULL) 2902 return ((uintptr_t)sp->st_value); 2903 2904 if (kernelonly) 2905 return (0); /* didn't find it in the kernel so give up */ 2906 2907 mutex_enter(&mod_lock); 2908 modp = &modules; 2909 do { 2910 mp = (struct module *)modp->mod_mp; 2911 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded && 2912 (sp = lookup_one(mp, name))) { 2913 value = (uintptr_t)sp->st_value; 2914 break; 2915 } 2916 } while ((modp = modp->mod_next) != &modules); 2917 mutex_exit(&mod_lock); 2918 return (value); 2919 } 2920 2921 /* look for a symbol near value. */ 2922 char * 2923 kobj_getsymname(uintptr_t value, ulong_t *offset) 2924 { 2925 char *name = NULL; 2926 struct modctl *modp; 2927 2928 struct modctl_list *lp; 2929 struct module *mp; 2930 2931 /* 2932 * Loop through the primary kernel modules. 2933 */ 2934 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) { 2935 mp = mod(lp); 2936 2937 if ((name = kobj_searchsym(mp, value, offset)) != NULL) 2938 return (name); 2939 } 2940 2941 mutex_enter(&mod_lock); 2942 modp = &modules; 2943 do { 2944 mp = (struct module *)modp->mod_mp; 2945 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded && 2946 (name = kobj_searchsym(mp, value, offset))) 2947 break; 2948 } while ((modp = modp->mod_next) != &modules); 2949 mutex_exit(&mod_lock); 2950 return (name); 2951 } 2952 2953 /* return address of symbol and size */ 2954 2955 uintptr_t 2956 kobj_getelfsym(char *name, void *mp, int *size) 2957 { 2958 Sym *sp; 2959 2960 if (mp == NULL) 2961 sp = kobj_lookup_kernel(name); 2962 else 2963 sp = lookup_one(mp, name); 2964 2965 if (sp == NULL) 2966 return (0); 2967 2968 *size = (int)sp->st_size; 2969 return ((uintptr_t)sp->st_value); 2970 } 2971 2972 uintptr_t 2973 kobj_lookup(struct module *mod, const char *name) 2974 { 2975 Sym *sp; 2976 2977 sp = lookup_one(mod, name); 2978 2979 if (sp == NULL) 2980 return (0); 2981 2982 return ((uintptr_t)sp->st_value); 2983 } 2984 2985 char * 2986 kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset) 2987 { 2988 Sym *symtabptr; 2989 char *strtabptr; 2990 int symnum; 2991 Sym *sym; 2992 Sym *cursym; 2993 uintptr_t curval; 2994 2995 *offset = (ulong_t)-1l; /* assume not found */ 2996 cursym = NULL; 2997 2998 if (kobj_addrcheck(mp, (void *)value) != 0) 2999 return (NULL); /* not in this module */ 3000 3001 strtabptr = mp->strings; 3002 symtabptr = (Sym *)mp->symtbl; 3003 3004 /* 3005 * Scan the module's symbol table for a symbol <= value 3006 */ 3007 for (symnum = 1, sym = symtabptr + 1; 3008 symnum < mp->nsyms; symnum++, sym = (Sym *) 3009 ((uintptr_t)sym + mp->symhdr->sh_entsize)) { 3010 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) { 3011 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 3012 continue; 3013 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 3014 ELF_ST_TYPE(sym->st_info) != STT_FUNC) 3015 continue; 3016 } 3017 3018 curval = (uintptr_t)sym->st_value; 3019 3020 if (curval > value) 3021 continue; 3022 3023 /* 3024 * If one or both are functions... 3025 */ 3026 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL && 3027 ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) { 3028 /* Ignore if the address is out of the bounds */ 3029 if (value - sym->st_value >= sym->st_size) 3030 continue; 3031 3032 if (cursym != NULL && 3033 ELF_ST_TYPE(cursym->st_info) == STT_FUNC) { 3034 /* Prefer the function to the non-function */ 3035 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) 3036 continue; 3037 3038 /* Prefer the larger of the two functions */ 3039 if (sym->st_size <= cursym->st_size) 3040 continue; 3041 } 3042 } else if (value - curval >= *offset) { 3043 continue; 3044 } 3045 3046 *offset = (ulong_t)(value - curval); 3047 cursym = sym; 3048 } 3049 if (cursym == NULL) 3050 return (NULL); 3051 3052 return (strtabptr + cursym->st_name); 3053 } 3054 3055 Sym * 3056 kobj_lookup_all(struct module *mp, char *name, int include_self) 3057 { 3058 Sym *sp; 3059 struct module_list *mlp; 3060 struct modctl_list *clp; 3061 struct module *mmp; 3062 3063 if (include_self && (sp = lookup_one(mp, name)) != NULL) 3064 return (sp); 3065 3066 for (mlp = mp->head; mlp; mlp = mlp->next) { 3067 if ((sp = lookup_one(mlp->mp, name)) != NULL && 3068 ELF_ST_BIND(sp->st_info) != STB_LOCAL) 3069 return (sp); 3070 } 3071 3072 /* 3073 * Loop through the primary kernel modules. 3074 */ 3075 for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) { 3076 mmp = mod(clp); 3077 3078 if (mmp == NULL || mp == mmp) 3079 continue; 3080 3081 if ((sp = lookup_one(mmp, name)) != NULL && 3082 ELF_ST_BIND(sp->st_info) != STB_LOCAL) 3083 return (sp); 3084 } 3085 return (NULL); 3086 } 3087 3088 Sym * 3089 kobj_lookup_kernel(const char *name) 3090 { 3091 struct modctl_list *lp; 3092 struct module *mp; 3093 Sym *sp; 3094 3095 /* 3096 * Loop through the primary kernel modules. 3097 */ 3098 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) { 3099 mp = mod(lp); 3100 3101 if (mp == NULL) 3102 continue; 3103 3104 if ((sp = lookup_one(mp, name)) != NULL) 3105 return (sp); 3106 } 3107 return (NULL); 3108 } 3109 3110 static Sym * 3111 lookup_one(struct module *mp, const char *name) 3112 { 3113 symid_t *ip; 3114 char *name1; 3115 Sym *sp; 3116 3117 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 3118 ip = &mp->chains[*ip]) { 3119 sp = (Sym *)(mp->symtbl + 3120 mp->symhdr->sh_entsize * *ip); 3121 name1 = mp->strings + sp->st_name; 3122 if (strcmp(name, name1) == 0 && 3123 ELF_ST_TYPE(sp->st_info) != STT_FILE && 3124 sp->st_shndx != SHN_UNDEF && 3125 sp->st_shndx != SHN_COMMON) 3126 return (sp); 3127 } 3128 return (NULL); 3129 } 3130 3131 /* 3132 * Lookup a given symbol pointer in the module's symbol hash. If the symbol 3133 * is hashed, return the symbol pointer; otherwise return NULL. 3134 */ 3135 static Sym * 3136 sym_lookup(struct module *mp, Sym *ksp) 3137 { 3138 char *name = mp->strings + ksp->st_name; 3139 symid_t *ip; 3140 Sym *sp; 3141 3142 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 3143 ip = &mp->chains[*ip]) { 3144 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip); 3145 if (sp == ksp) 3146 return (ksp); 3147 } 3148 return (NULL); 3149 } 3150 3151 static void 3152 sym_insert(struct module *mp, char *name, symid_t index) 3153 { 3154 symid_t *ip; 3155 3156 #ifdef KOBJ_DEBUG 3157 if (kobj_debug & D_SYMBOLS) { 3158 static struct module *lastmp = NULL; 3159 Sym *sp; 3160 if (lastmp != mp) { 3161 _kobj_printf(ops, 3162 "krtld: symbol entry: file=%s\n", 3163 mp->filename); 3164 _kobj_printf(ops, 3165 "krtld:\tsymndx\tvalue\t\t" 3166 "symbol name\n"); 3167 lastmp = mp; 3168 } 3169 sp = (Sym *)(mp->symtbl + 3170 index * mp->symhdr->sh_entsize); 3171 _kobj_printf(ops, "krtld:\t[%3d]", index); 3172 _kobj_printf(ops, "\t0x%lx", sp->st_value); 3173 _kobj_printf(ops, "\t%s\n", name); 3174 } 3175 3176 #endif 3177 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 3178 ip = &mp->chains[*ip]) { 3179 ; 3180 } 3181 *ip = index; 3182 } 3183 3184 struct modctl * 3185 kobj_boot_mod_lookup(const char *modname) 3186 { 3187 struct modctl *mctl = kobj_modules; 3188 3189 do { 3190 if (strcmp(modname, mctl->mod_modname) == 0) 3191 return (mctl); 3192 } while ((mctl = mctl->mod_next) != kobj_modules); 3193 3194 return (NULL); 3195 } 3196 3197 /* 3198 * Determine if the module exists. 3199 */ 3200 int 3201 kobj_path_exists(char *name, int use_path) 3202 { 3203 struct _buf *file; 3204 3205 file = kobj_open_path(name, use_path, 1); 3206 #ifdef MODDIR_SUFFIX 3207 if (file == (struct _buf *)-1) 3208 file = kobj_open_path(name, use_path, 0); 3209 #endif /* MODDIR_SUFFIX */ 3210 if (file == (struct _buf *)-1) 3211 return (0); 3212 kobj_close_file(file); 3213 return (1); 3214 } 3215 3216 /* 3217 * fullname is dynamically allocated to be able to hold the 3218 * maximum size string that can be constructed from name. 3219 * path is exactly like the shell PATH variable. 3220 */ 3221 struct _buf * 3222 kobj_open_path(char *name, int use_path, int use_moddir_suffix) 3223 { 3224 char *p, *q; 3225 char *pathp; 3226 char *pathpsave; 3227 char *fullname; 3228 int maxpathlen; 3229 struct _buf *file; 3230 3231 #if !defined(MODDIR_SUFFIX) 3232 use_moddir_suffix = B_FALSE; 3233 #endif 3234 3235 if (!use_path) 3236 pathp = ""; /* use name as specified */ 3237 else 3238 pathp = module_path; /* use configured default path */ 3239 3240 pathpsave = pathp; /* keep this for error reporting */ 3241 3242 /* 3243 * Allocate enough space for the largest possible fullname. 3244 * since path is of the form <directory> : <directory> : ... 3245 * we're potentially allocating a little more than we need to 3246 * but we'll allocate the exact amount when we find the right directory. 3247 * (The + 3 below is one for NULL terminator and one for the '/' 3248 * we might have to add at the beginning of path and one for 3249 * the '/' between path and name.) 3250 */ 3251 maxpathlen = strlen(pathp) + strlen(name) + 3; 3252 /* sizeof includes null */ 3253 maxpathlen += sizeof (slash_moddir_suffix_slash) - 1; 3254 fullname = kobj_zalloc(maxpathlen, KM_WAIT); 3255 3256 for (;;) { 3257 p = fullname; 3258 if (*pathp != '\0' && *pathp != '/') 3259 *p++ = '/'; /* path must start with '/' */ 3260 while (*pathp && *pathp != ':' && *pathp != ' ') 3261 *p++ = *pathp++; 3262 if (p != fullname && p[-1] != '/') 3263 *p++ = '/'; 3264 if (use_moddir_suffix) { 3265 char *b = basename(name); 3266 char *s; 3267 3268 /* copy everything up to the base name */ 3269 q = name; 3270 while (q != b && *q) 3271 *p++ = *q++; 3272 s = slash_moddir_suffix_slash; 3273 while (*s) 3274 *p++ = *s++; 3275 /* copy the rest */ 3276 while (*b) 3277 *p++ = *b++; 3278 } else { 3279 q = name; 3280 while (*q) 3281 *p++ = *q++; 3282 } 3283 *p = 0; 3284 if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) { 3285 kobj_free(fullname, maxpathlen); 3286 return (file); 3287 } 3288 if (*pathp == 0) 3289 break; 3290 pathp++; 3291 } 3292 kobj_free(fullname, maxpathlen); 3293 if (_moddebug & MODDEBUG_ERRMSG) { 3294 _kobj_printf(ops, "can't open %s,", name); 3295 _kobj_printf(ops, " path is %s\n", pathpsave); 3296 } 3297 return ((struct _buf *)-1); 3298 } 3299 3300 intptr_t 3301 kobj_open(char *filename) 3302 { 3303 struct vnode *vp; 3304 int fd; 3305 3306 if (_modrootloaded) { 3307 struct kobjopen_tctl *ltp = kobjopen_alloc(filename); 3308 int Errno; 3309 3310 /* 3311 * Hand off the open to a thread who has a 3312 * stack size capable handling the request. 3313 */ 3314 if (curthread != &t0) { 3315 (void) thread_create(NULL, DEFAULTSTKSZ * 2, 3316 kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri); 3317 sema_p(<p->sema); 3318 Errno = ltp->Errno; 3319 vp = ltp->vp; 3320 } else { 3321 /* 3322 * 1098067: module creds should not be those of the 3323 * caller 3324 */ 3325 cred_t *saved_cred = curthread->t_cred; 3326 curthread->t_cred = kcred; 3327 Errno = vn_open(filename, UIO_SYSSPACE, FREAD, 0, &vp, 3328 0, 0); 3329 curthread->t_cred = saved_cred; 3330 } 3331 kobjopen_free(ltp); 3332 3333 if (Errno) { 3334 if (_moddebug & MODDEBUG_ERRMSG) { 3335 _kobj_printf(ops, 3336 "kobj_open: vn_open of %s fails, ", 3337 filename); 3338 _kobj_printf(ops, "Errno = %d\n", Errno); 3339 } 3340 return (-1); 3341 } else { 3342 if (_moddebug & MODDEBUG_ERRMSG) { 3343 _kobj_printf(ops, "kobj_open: '%s'", filename); 3344 _kobj_printf(ops, " vp = %p\n", vp); 3345 } 3346 return ((intptr_t)vp); 3347 } 3348 } else { 3349 fd = kobj_boot_open(filename, 0); 3350 3351 if (_moddebug & MODDEBUG_ERRMSG) { 3352 if (fd < 0) 3353 _kobj_printf(ops, 3354 "kobj_open: can't open %s\n", filename); 3355 else { 3356 _kobj_printf(ops, "kobj_open: '%s'", filename); 3357 _kobj_printf(ops, " descr = 0x%x\n", fd); 3358 } 3359 } 3360 return ((intptr_t)fd); 3361 } 3362 } 3363 3364 /* 3365 * Calls to kobj_open() are handled off to this routine as a separate thread. 3366 */ 3367 static void 3368 kobjopen_thread(struct kobjopen_tctl *ltp) 3369 { 3370 kmutex_t cpr_lk; 3371 callb_cpr_t cpr_i; 3372 3373 mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL); 3374 CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen"); 3375 ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp), 3376 0, 0); 3377 sema_v(<p->sema); 3378 mutex_enter(&cpr_lk); 3379 CALLB_CPR_EXIT(&cpr_i); 3380 mutex_destroy(&cpr_lk); 3381 thread_exit(); 3382 } 3383 3384 /* 3385 * allocate and initialize a kobjopen thread structure 3386 */ 3387 static struct kobjopen_tctl * 3388 kobjopen_alloc(char *filename) 3389 { 3390 struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP); 3391 3392 ASSERT(filename != NULL); 3393 3394 ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP); 3395 bcopy(filename, ltp->name, strlen(filename) + 1); 3396 sema_init(<p->sema, 0, NULL, SEMA_DEFAULT, NULL); 3397 return (ltp); 3398 } 3399 3400 /* 3401 * free a kobjopen thread control structure 3402 */ 3403 static void 3404 kobjopen_free(struct kobjopen_tctl *ltp) 3405 { 3406 sema_destroy(<p->sema); 3407 kmem_free(ltp->name, strlen(ltp->name) + 1); 3408 kmem_free(ltp, sizeof (*ltp)); 3409 } 3410 3411 int 3412 kobj_read(intptr_t descr, char *buf, unsigned size, unsigned offset) 3413 { 3414 int stat; 3415 ssize_t resid; 3416 3417 if (_modrootloaded) { 3418 if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size, 3419 (offset_t)offset, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), 3420 &resid)) != 0) { 3421 _kobj_printf(ops, 3422 "vn_rdwr failed with error 0x%x\n", stat); 3423 return (-1); 3424 } 3425 return (size - resid); 3426 } else { 3427 int count = 0; 3428 3429 if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) { 3430 _kobj_printf(ops, 3431 "kobj_read: seek 0x%x failed\n", offset); 3432 return (-1); 3433 } 3434 3435 count = kobj_boot_read((int)descr, buf, size); 3436 if (count < size) { 3437 if (_moddebug & MODDEBUG_ERRMSG) { 3438 _kobj_printf(ops, 3439 "kobj_read: req %d bytes, ", size); 3440 _kobj_printf(ops, "got %d\n", count); 3441 } 3442 } 3443 return (count); 3444 } 3445 } 3446 3447 void 3448 kobj_close(intptr_t descr) 3449 { 3450 if (_moddebug & MODDEBUG_ERRMSG) 3451 _kobj_printf(ops, "kobj_close: 0x%lx\n", descr); 3452 3453 if (_modrootloaded) { 3454 struct vnode *vp = (struct vnode *)descr; 3455 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED()); 3456 VN_RELE(vp); 3457 } else 3458 (void) kobj_boot_close((int)descr); 3459 } 3460 3461 struct _buf * 3462 kobj_open_file(char *name) 3463 { 3464 struct _buf *file; 3465 intptr_t fd; 3466 3467 if ((fd = kobj_open(name)) == -1) { 3468 return ((struct _buf *)-1); 3469 } 3470 3471 file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP); 3472 file->_fd = fd; 3473 file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP); 3474 file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT|KM_TMP); 3475 file->_cnt = file->_size = file->_off = 0; 3476 file->_ln = 1; 3477 file->_ptr = file->_base; 3478 (void) strcpy(file->_name, name); 3479 return (file); 3480 } 3481 3482 void 3483 kobj_close_file(struct _buf *file) 3484 { 3485 kobj_close(file->_fd); 3486 kobj_free(file->_base, MAXBSIZE); 3487 kobj_free(file->_name, strlen(file->_name)+1); 3488 kobj_free(file, sizeof (struct _buf)); 3489 } 3490 3491 int 3492 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) 3493 { 3494 int b_size, c_size; 3495 int b_off; /* Offset into buffer for start of bcopy */ 3496 int count = 0; 3497 int page_addr; 3498 3499 if (_moddebug & MODDEBUG_ERRMSG) { 3500 _kobj_printf(ops, "kobj_read_file: size=%x,", size); 3501 _kobj_printf(ops, " offset=%x at", off); 3502 _kobj_printf(ops, " buf=%x\n", buf); 3503 } 3504 3505 while (size) { 3506 page_addr = F_PAGE(off); 3507 b_size = file->_size; 3508 /* 3509 * If we have the filesystem page the caller's referring to 3510 * and we have something in the buffer, 3511 * satisfy as much of the request from the buffer as we can. 3512 */ 3513 if (page_addr == file->_off && b_size > 0) { 3514 b_off = B_OFFSET(off); 3515 c_size = b_size - b_off; 3516 /* 3517 * If there's nothing to copy, we're at EOF. 3518 */ 3519 if (c_size <= 0) 3520 break; 3521 if (c_size > size) 3522 c_size = size; 3523 if (buf) { 3524 if (_moddebug & MODDEBUG_ERRMSG) 3525 _kobj_printf(ops, "copying %x bytes\n", 3526 c_size); 3527 bcopy(file->_base+b_off, buf, c_size); 3528 size -= c_size; 3529 off += c_size; 3530 buf += c_size; 3531 count += c_size; 3532 } else { 3533 _kobj_printf(ops, "kobj_read: system error"); 3534 count = -1; 3535 break; 3536 } 3537 } else { 3538 /* 3539 * If the caller's offset is page aligned and 3540 * the caller want's at least a filesystem page and 3541 * the caller provided a buffer, 3542 * read directly into the caller's buffer. 3543 */ 3544 if (page_addr == off && 3545 (c_size = F_PAGE(size)) && buf) { 3546 c_size = kobj_read(file->_fd, buf, c_size, 3547 page_addr); 3548 if (c_size < 0) { 3549 count = -1; 3550 break; 3551 } 3552 count += c_size; 3553 if (c_size != F_PAGE(size)) 3554 break; 3555 size -= c_size; 3556 off += c_size; 3557 buf += c_size; 3558 /* 3559 * Otherwise, read into our buffer and copy next time 3560 * around the loop. 3561 */ 3562 } else { 3563 file->_off = page_addr; 3564 c_size = kobj_read(file->_fd, file->_base, 3565 MAXBSIZE, page_addr); 3566 file->_ptr = file->_base; 3567 file->_cnt = c_size; 3568 file->_size = c_size; 3569 /* 3570 * If a _filbuf call or nothing read, break. 3571 */ 3572 if (buf == NULL || c_size <= 0) { 3573 count = c_size; 3574 break; 3575 } 3576 } 3577 if (_moddebug & MODDEBUG_ERRMSG) 3578 _kobj_printf(ops, "read %x bytes\n", c_size); 3579 } 3580 } 3581 if (_moddebug & MODDEBUG_ERRMSG) 3582 _kobj_printf(ops, "count = %x\n", count); 3583 3584 return (count); 3585 } 3586 3587 int 3588 kobj_filbuf(struct _buf *f) 3589 { 3590 if (kobj_read_file(f, NULL, MAXBSIZE, f->_off + f->_size) > 0) 3591 return (kobj_getc(f)); 3592 return (-1); 3593 } 3594 3595 void 3596 kobj_free(void *address, size_t size) 3597 { 3598 if (standalone) 3599 return; 3600 3601 kmem_free(address, size); 3602 kobj_stat.nfree_calls++; 3603 kobj_stat.nfree += size; 3604 } 3605 3606 void * 3607 kobj_zalloc(size_t size, int flag) 3608 { 3609 void *v; 3610 3611 if ((v = kobj_alloc(size, flag)) != 0) { 3612 bzero(v, size); 3613 } 3614 3615 return (v); 3616 } 3617 3618 void * 3619 kobj_alloc(size_t size, int flag) 3620 { 3621 /* 3622 * If we are running standalone in the 3623 * linker, we ask boot for memory. 3624 * Either it's temporary memory that we lose 3625 * once boot is mapped out or we allocate it 3626 * permanently using the dynamic data segment. 3627 */ 3628 if (standalone) { 3629 #ifdef __sparc 3630 if (flag & KM_TMP) { 3631 return (kobj_tmp_alloc(size)); 3632 } else if (flag & KM_SCRATCH) { 3633 void *buf = kobj_bs_alloc(size); 3634 3635 if (buf != NULL) 3636 return (buf); 3637 #ifdef KOBJ_DEBUG 3638 if (kobj_debug & D_DEBUG) { 3639 _kobj_printf(ops, "krtld: failed scratch alloc " 3640 "of %u bytes -- falling back\n", size); 3641 } 3642 #endif 3643 } 3644 3645 #else /* x86 */ 3646 if (flag & (KM_TMP | KM_SCRATCH)) 3647 return (BOP_ALLOC(ops, 0, size, MINALIGN)); 3648 #endif 3649 return (kobj_segbrk(&_edata, size, MINALIGN, 0)); 3650 } 3651 3652 kobj_stat.nalloc_calls++; 3653 kobj_stat.nalloc += size; 3654 3655 return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)); 3656 } 3657 3658 /* 3659 * Allow the "mod" system to sync up with the work 3660 * already done by kobj during the initial loading 3661 * of the kernel. This also gives us a chance 3662 * to reallocate memory that belongs to boot. 3663 */ 3664 void 3665 kobj_sync(void) 3666 { 3667 struct modctl_list *lp, **lpp; 3668 3669 extern char *default_path; 3670 3671 /* 3672 * module_path can be set in /etc/system 3673 */ 3674 if (default_path != NULL) 3675 module_path = default_path; 3676 else 3677 default_path = module_path; 3678 3679 ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t), 3680 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 3681 3682 ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t), 3683 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 3684 3685 /* 3686 * Move symbol tables from boot memory to ksyms_arena. 3687 */ 3688 for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) { 3689 for (lp = *lpp; lp != NULL; lp = lp->modl_next) 3690 kobj_export_module(mod(lp)); 3691 } 3692 } 3693 3694 caddr_t 3695 kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit) 3696 { 3697 uintptr_t va, pva; 3698 size_t alloc_pgsz = kobj_mmu_pagesize; 3699 size_t alloc_align = BO_NO_ALIGN; 3700 size_t alloc_size; 3701 3702 /* 3703 * If we are using "large" mappings for the kernel, 3704 * request aligned memory from boot using the 3705 * "large" pagesize. 3706 */ 3707 if (lg_pagesize) { 3708 alloc_align = lg_pagesize; 3709 alloc_pgsz = lg_pagesize; 3710 } 3711 va = ALIGN((uintptr_t)*spp, align); 3712 pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz); 3713 /* 3714 * Need more pages? 3715 */ 3716 if (va + size > pva) { 3717 alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz); 3718 /* 3719 * Check for overlapping segments. 3720 */ 3721 if (limit && limit <= *spp + alloc_size) 3722 return ((caddr_t)0); 3723 3724 pva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva, 3725 alloc_size, alloc_align); 3726 if (pva == NULL) { 3727 _kobj_printf(ops, "BOP_ALLOC refused, 0x%x bytes ", 3728 alloc_size); 3729 _kobj_printf(ops, " at 0x%lx\n", pva); 3730 } 3731 } 3732 *spp = (caddr_t)(va + size); 3733 3734 return ((caddr_t)va); 3735 } 3736 3737 /* 3738 * Calculate the number of output hash buckets. 3739 * We use the next prime larger than n / 4, 3740 * so the average hash chain is about 4 entries. 3741 * More buckets would just be a waste of memory. 3742 */ 3743 uint_t 3744 kobj_gethashsize(uint_t n) 3745 { 3746 int f; 3747 int hsize = MAX(n / 4, 2); 3748 3749 for (f = 2; f * f <= hsize; f++) 3750 if (hsize % f == 0) 3751 hsize += f = 1; 3752 3753 return (hsize); 3754 } 3755 3756 static char * 3757 basename(char *s) 3758 { 3759 char *p, *q; 3760 3761 q = NULL; 3762 p = s; 3763 do { 3764 if (*p == '/') 3765 q = p; 3766 } while (*p++); 3767 return (q ? q + 1 : s); 3768 } 3769 3770 /*ARGSUSED*/ 3771 static void 3772 kprintf(void *op, const char *fmt, ...) 3773 { 3774 va_list adx; 3775 3776 va_start(adx, fmt); 3777 vprintf(fmt, adx); 3778 va_end(adx); 3779 } 3780 3781 void 3782 kobj_stat_get(kobj_stat_t *kp) 3783 { 3784 *kp = kobj_stat; 3785 } 3786 3787 int 3788 kobj_getpagesize() 3789 { 3790 return (lg_pagesize); 3791 } 3792 3793 void 3794 kobj_textwin_alloc(struct module *mp) 3795 { 3796 ASSERT(MUTEX_HELD(&mod_lock)); 3797 3798 if (mp->textwin != NULL) 3799 return; 3800 3801 /* 3802 * If the text is not contained in the heap, then it is not contained 3803 * by a writable mapping. (Specifically, it's on the nucleus page.) 3804 * We allocate a read/write mapping for this module's text to allow 3805 * the text to be patched without calling hot_patch_kernel_text() 3806 * (which is quite slow). 3807 */ 3808 if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) { 3809 uintptr_t text = (uintptr_t)mp->text; 3810 uintptr_t size = (uintptr_t)mp->text_size; 3811 uintptr_t i; 3812 caddr_t va; 3813 size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) - 3814 (text & PAGEMASK); 3815 3816 va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP); 3817 3818 for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) { 3819 hat_devload(kas.a_hat, va, PAGESIZE, 3820 hat_getpfnum(kas.a_hat, (caddr_t)i), 3821 PROT_READ | PROT_WRITE, 3822 HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST); 3823 va += PAGESIZE; 3824 } 3825 3826 mp->textwin = mp->textwin_base + (text & PAGEOFFSET); 3827 } else { 3828 mp->textwin = mp->text; 3829 } 3830 } 3831 3832 void 3833 kobj_textwin_free(struct module *mp) 3834 { 3835 uintptr_t text = (uintptr_t)mp->text; 3836 uintptr_t tsize = (uintptr_t)mp->text_size; 3837 size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) - 3838 (text & PAGEMASK)); 3839 3840 mp->textwin = NULL; 3841 3842 if (mp->textwin_base == NULL) 3843 return; 3844 3845 hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK); 3846 vmem_free(heap_arena, mp->textwin_base, size); 3847 mp->textwin_base = NULL; 3848 } 3849 3850 static char * 3851 find_libmacro(char *name) 3852 { 3853 int lmi; 3854 3855 for (lmi = 0; lmi < NLIBMACROS; lmi++) { 3856 if (strcmp(name, libmacros[lmi].lmi_macroname) == 0) 3857 return (libmacros[lmi].lmi_list); 3858 } 3859 return (NULL); 3860 } 3861 3862 /* 3863 * Check for $MACRO in tail (string to expand) and expand it in path at pathend 3864 * returns path if successful, else NULL 3865 * Support multiple $MACROs expansion and the first valid path will be returned 3866 * Caller's responsibility to provide enough space in path to expand 3867 */ 3868 char * 3869 expand_libmacro(char *tail, char *path, char *pathend) 3870 { 3871 char c, *p, *p1, *p2, *path2, *endp; 3872 int diff, lmi, macrolen, valid_macro, more_macro; 3873 struct _buf *file; 3874 3875 /* 3876 * check for $MACROS between nulls or slashes 3877 */ 3878 p = strchr(tail, '$'); 3879 if (p == NULL) 3880 return (NULL); 3881 for (lmi = 0; lmi < NLIBMACROS; lmi++) { 3882 macrolen = libmacros[lmi].lmi_macrolen; 3883 if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0) 3884 break; 3885 } 3886 3887 valid_macro = 0; 3888 if (lmi < NLIBMACROS) { 3889 /* 3890 * The following checks are used to restrict expansion of 3891 * macros to those that form a full directory/file name 3892 * and to keep the behavior same as before. If this 3893 * restriction is removed or no longer valid in the future, 3894 * the checks below can be deleted. 3895 */ 3896 if ((p == tail) || (*(p - 1) == '/')) { 3897 c = *(p + macrolen + 1); 3898 if (c == '/' || c == '\0') 3899 valid_macro = 1; 3900 } 3901 } 3902 3903 if (!valid_macro) { 3904 p2 = strchr(p, '/'); 3905 /* 3906 * if no more macro to expand, then just copy whatever left 3907 * and check whether it exists 3908 */ 3909 if (p2 == NULL || strchr(p2, '$') == NULL) { 3910 (void) strcpy(pathend, tail); 3911 if ((file = kobj_open_path(path, 1, 1)) != 3912 (struct _buf *)-1) { 3913 kobj_close_file(file); 3914 return (path); 3915 } else 3916 return (NULL); 3917 } else { 3918 /* 3919 * copy all chars before '/' and call expand_libmacro() 3920 * again 3921 */ 3922 diff = p2 - tail; 3923 bcopy(tail, pathend, diff); 3924 pathend += diff; 3925 *(pathend) = '\0'; 3926 return (expand_libmacro(p2, path, pathend)); 3927 } 3928 } 3929 3930 more_macro = 0; 3931 if (c != '\0') { 3932 endp = p + macrolen + 1; 3933 if (strchr(endp, '$') != NULL) 3934 more_macro = 1; 3935 } else 3936 endp = NULL; 3937 3938 /* 3939 * copy lmi_list and split it into components. 3940 * then put the part of tail before $MACRO into path 3941 * at pathend 3942 */ 3943 diff = p - tail; 3944 if (diff > 0) 3945 bcopy(tail, pathend, diff); 3946 path2 = pathend + diff; 3947 p1 = libmacros[lmi].lmi_list; 3948 while (p1 && (*p1 != '\0')) { 3949 p2 = strchr(p1, ':'); 3950 if (p2) { 3951 diff = p2 - p1; 3952 bcopy(p1, path2, diff); 3953 *(path2 + diff) = '\0'; 3954 } else { 3955 diff = strlen(p1); 3956 bcopy(p1, path2, diff + 1); 3957 } 3958 /* copy endp only if there isn't any more macro to expand */ 3959 if (!more_macro && (endp != NULL)) 3960 (void) strcat(path2, endp); 3961 file = kobj_open_path(path, 1, 1); 3962 if (file != (struct _buf *)-1) { 3963 kobj_close_file(file); 3964 /* 3965 * if more macros to expand then call expand_libmacro(), 3966 * else return path which has the whole path 3967 */ 3968 if (!more_macro || (expand_libmacro(endp, path, 3969 path2 + diff) != NULL)) { 3970 return (path); 3971 } 3972 } 3973 if (p2) 3974 p1 = ++p2; 3975 else 3976 return (NULL); 3977 } 3978 return (NULL); 3979 } 3980 3981 static void 3982 tnf_add_notifyunload(kobj_notify_f *fp) 3983 { 3984 kobj_notify_list_t *entry; 3985 3986 entry = kobj_alloc(sizeof (kobj_notify_list_t), KM_WAIT); 3987 entry->kn_type = KOBJ_NOTIFY_MODUNLOADING; 3988 entry->kn_func = fp; 3989 (void) kobj_notify_add(entry); 3990 } 3991 3992 /* ARGSUSED */ 3993 static void 3994 tnf_unsplice_probes(unsigned int what, struct modctl *mod) 3995 { 3996 extern tnf_probe_control_t *__tnf_probe_list_head; 3997 extern tnf_tag_data_t *__tnf_tag_list_head; 3998 tnf_probe_control_t **p; 3999 tnf_tag_data_t **q; 4000 struct module *mp = mod->mod_mp; 4001 4002 if (!(mp->flags & KOBJ_TNF_PROBE)) 4003 return; 4004 4005 for (p = &__tnf_probe_list_head; *p; ) 4006 if (kobj_addrcheck(mp, (char *)*p) == 0) 4007 *p = (*p)->next; 4008 else 4009 p = &(*p)->next; 4010 4011 for (q = &__tnf_tag_list_head; *q; ) 4012 if (kobj_addrcheck(mp, (char *)*q) == 0) 4013 *q = (tnf_tag_data_t *)(*q)->tag_version; 4014 else 4015 q = (tnf_tag_data_t **)&(*q)->tag_version; 4016 4017 tnf_changed_probe_list = 1; 4018 } 4019 4020 int 4021 tnf_splice_probes(int boot_load, tnf_probe_control_t *plist, 4022 tnf_tag_data_t *tlist) 4023 { 4024 int result = 0; 4025 static int add_notify = 1; 4026 4027 if (plist) { 4028 tnf_probe_control_t *pl; 4029 4030 for (pl = plist; pl->next; ) 4031 pl = pl->next; 4032 4033 if (!boot_load) 4034 mutex_enter(&mod_lock); 4035 tnf_changed_probe_list = 1; 4036 pl->next = __tnf_probe_list_head; 4037 __tnf_probe_list_head = plist; 4038 if (!boot_load) 4039 mutex_exit(&mod_lock); 4040 result = 1; 4041 } 4042 4043 if (tlist) { 4044 tnf_tag_data_t *tl; 4045 4046 for (tl = tlist; tl->tag_version; ) 4047 tl = (tnf_tag_data_t *)tl->tag_version; 4048 4049 if (!boot_load) 4050 mutex_enter(&mod_lock); 4051 tl->tag_version = (tnf_tag_version_t *)__tnf_tag_list_head; 4052 __tnf_tag_list_head = tlist; 4053 if (!boot_load) 4054 mutex_exit(&mod_lock); 4055 result = 1; 4056 } 4057 if (!boot_load && result && add_notify) { 4058 tnf_add_notifyunload(tnf_unsplice_probes); 4059 add_notify = 0; 4060 } 4061 return (result); 4062 } 4063 4064 #if defined(__x86) 4065 /* 4066 * This code is for the purpose of manually recording which files 4067 * needs to go into the boot archive on any given system. 4068 * 4069 * To enable the code, set kobj_file_bufsize in /etc/system 4070 * and reboot the system, then use mdb to look at kobj_file_buf. 4071 */ 4072 static void 4073 kobj_record_file(char *filename) 4074 { 4075 extern char *kobj_file_buf; 4076 extern int kobj_file_bufsize; 4077 static char *buf; 4078 static int size = 0; 4079 int n; 4080 4081 if (standalone) /* kernel symbol not available */ 4082 return; 4083 4084 if (kobj_file_bufsize == 0) /* don't bother */ 4085 return; 4086 4087 if (kobj_file_buf == NULL) { /* allocate buffer */ 4088 size = kobj_file_bufsize; 4089 buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP); 4090 } 4091 4092 n = snprintf(buf, size, "%s\n", filename); 4093 if (n > size) 4094 n = size; 4095 size -= n; 4096 buf += n; 4097 } 4098 #endif /* __x86 */ 4099 4100 /* 4101 * XXX these wrappers should go away when sparc is converted 4102 * boot from ramdisk 4103 */ 4104 static int 4105 kobj_boot_open(char *filename, int flags) 4106 { 4107 #if defined(__sparc) 4108 /* 4109 * If io via bootops is quiesced, it means boot is no longer 4110 * available to us. We make it look as if we can't open the 4111 * named file - which is reasonably accurate. 4112 */ 4113 if (!standalone && _ioquiesced) 4114 return (-1); 4115 4116 return (BOP_OPEN(ops, filename, flags)); 4117 #else /* x86 */ 4118 kobj_record_file(filename); 4119 return (BRD_OPEN(bfs_ops, filename, flags)); 4120 #endif 4121 } 4122 4123 static int 4124 kobj_boot_close(int fd) 4125 { 4126 #if defined(__sparc) 4127 if (!standalone && _ioquiesced) 4128 return (-1); 4129 4130 return (BOP_CLOSE(ops, fd)); 4131 #else /* x86 */ 4132 return (BRD_CLOSE(bfs_ops, fd)); 4133 #endif 4134 } 4135 4136 /*ARGSUSED*/ 4137 static int 4138 kobj_boot_seek(int fd, off_t hi, off_t lo) 4139 { 4140 #if defined(__sparc) 4141 return (BOP_SEEK(ops, fd, hi, lo)); 4142 #else 4143 return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET)); 4144 #endif 4145 } 4146 4147 static int 4148 kobj_boot_read(int fd, caddr_t buf, size_t size) 4149 { 4150 #if defined(__sparc) 4151 return (BOP_READ(ops, fd, buf, size)); 4152 #else 4153 return (BRD_READ(bfs_ops, fd, buf, size)); 4154 #endif 4155 } 4156