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