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