1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "@(#)kobj.c 1.174 07/11/29 SMI" 27 28 /* 29 * Kernel's linker/loader 30 */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/sysmacros.h> 35 #include <sys/systm.h> 36 #include <sys/user.h> 37 #include <sys/kmem.h> 38 #include <sys/reboot.h> 39 #include <sys/bootconf.h> 40 #include <sys/debug.h> 41 #include <sys/uio.h> 42 #include <sys/file.h> 43 #include <sys/vnode.h> 44 #include <sys/user.h> 45 #include <sys/mman.h> 46 #include <vm/as.h> 47 #include <vm/seg_kp.h> 48 #include <vm/seg_kmem.h> 49 #include <sys/elf.h> 50 #include <sys/elf_notes.h> 51 #include <sys/vmsystm.h> 52 #include <sys/kdi.h> 53 #include <sys/atomic.h> 54 #include <sys/kmdb.h> 55 56 #include <sys/link.h> 57 #include <sys/kobj.h> 58 #include <sys/ksyms.h> 59 #include <sys/disp.h> 60 #include <sys/modctl.h> 61 #include <sys/varargs.h> 62 #include <sys/kstat.h> 63 #include <sys/kobj_impl.h> 64 #include <sys/fs/decomp.h> 65 #include <sys/callb.h> 66 #include <sys/cmn_err.h> 67 #include <sys/tnf_probe.h> 68 #include <sys/zmod.h> 69 70 #include <krtld/reloc.h> 71 #include <krtld/kobj_kdi.h> 72 #include <sys/sha1.h> 73 #include <sys/crypto/elfsign.h> 74 75 #if !defined(_OBP) 76 #include <sys/bootvfs.h> 77 #endif 78 79 /* 80 * do_symbols() error codes 81 */ 82 #define DOSYM_UNDEF -1 /* undefined symbol */ 83 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */ 84 85 #if !defined(_OBP) 86 static void synthetic_bootaux(char *, val_t *); 87 #endif 88 89 static struct module *load_exec(val_t *, char *); 90 static void load_linker(val_t *); 91 static struct modctl *add_primary(const char *filename, int); 92 static int bind_primary(val_t *, int); 93 static int load_primary(struct module *, int); 94 static int load_kmdb(val_t *); 95 static int get_progbits(struct module *, struct _buf *); 96 static int get_syms(struct module *, struct _buf *); 97 static int get_ctf(struct module *, struct _buf *); 98 static void get_signature(struct module *, struct _buf *); 99 static int do_common(struct module *); 100 static void add_dependent(struct module *, struct module *); 101 static int do_dependents(struct modctl *, char *, size_t); 102 static int do_symbols(struct module *, Elf64_Addr); 103 static void module_assign(struct modctl *, struct module *); 104 static void free_module_data(struct module *); 105 static char *depends_on(struct module *); 106 static char *getmodpath(const char *); 107 static char *basename(char *); 108 static void attr_val(val_t *); 109 static char *find_libmacro(char *); 110 static char *expand_libmacro(char *, char *, char *); 111 static int read_bootflags(void); 112 static int kobj_comp_setup(struct _buf *, struct compinfo *); 113 static int kobj_uncomp_blk(struct _buf *, caddr_t, uint_t); 114 static int kobj_read_blks(struct _buf *, caddr_t, uint_t, uint_t); 115 static int kobj_boot_open(char *, int); 116 static int kobj_boot_close(int); 117 static int kobj_boot_seek(int, off_t, off_t); 118 static int kobj_boot_read(int, caddr_t, size_t); 119 static int kobj_boot_fstat(int, struct bootstat *); 120 static int kobj_boot_compinfo(int, struct compinfo *); 121 122 static Sym *lookup_one(struct module *, const char *); 123 static void sym_insert(struct module *, char *, symid_t); 124 static Sym *sym_lookup(struct module *, Sym *); 125 126 static struct kobjopen_tctl *kobjopen_alloc(char *filename); 127 static void kobjopen_free(struct kobjopen_tctl *ltp); 128 static void kobjopen_thread(struct kobjopen_tctl *ltp); 129 130 extern int kcopy(const void *, void *, size_t); 131 extern int elf_mach_ok(Ehdr *); 132 extern int alloc_gottable(struct module *, caddr_t *, caddr_t *); 133 134 #if !defined(_OBP) 135 extern int kobj_boot_mountroot(void); 136 #endif 137 138 static void tnf_unsplice_probes(uint_t, struct modctl *); 139 extern tnf_probe_control_t *__tnf_probe_list_head; 140 extern tnf_tag_data_t *__tnf_tag_list_head; 141 142 extern int modrootloaded; 143 extern int swaploaded; 144 extern int bop_io_quiesced; 145 extern int last_module_id; 146 147 extern char stubs_base[]; 148 extern char stubs_end[]; 149 150 #ifdef KOBJ_DEBUG 151 /* 152 * Values that can be or'd in to kobj_debug and their effects: 153 * 154 * D_DEBUG - misc. debugging information. 155 * D_SYMBOLS - list symbols and their values as they are entered 156 * into the hash table 157 * D_RELOCATIONS - display relocation processing information 158 * D_LOADING - display information about each module as it 159 * is loaded. 160 */ 161 int kobj_debug = 0; 162 163 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \ 164 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s)) 165 #else 166 #define KOBJ_MARK(s) /* discard */ 167 #endif 168 169 #define MODPATH_PROPNAME "module-path" 170 171 #ifdef MODDIR_SUFFIX 172 static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/"; 173 #else 174 #define slash_moddir_suffix_slash "" 175 #endif 176 177 #define _moddebug get_weakish_int(&moddebug) 178 #define _modrootloaded get_weakish_int(&modrootloaded) 179 #define _swaploaded get_weakish_int(&swaploaded) 180 #define _ioquiesced get_weakish_int(&bop_io_quiesced) 181 182 #define mod(X) (struct module *)((X)->modl_modp->mod_mp) 183 184 void *romp; /* rom vector (opaque to us) */ 185 struct bootops *ops; /* bootops vector */ 186 void *dbvec; /* debug vector */ 187 188 /* 189 * kobjopen thread control structure 190 */ 191 struct kobjopen_tctl { 192 ksema_t sema; 193 char *name; /* name of file */ 194 struct vnode *vp; /* vnode return from vn_open() */ 195 int Errno; /* error return from vnopen */ 196 }; 197 198 /* 199 * Structure for defining dynamically expandable library macros 200 */ 201 202 struct lib_macro_info { 203 char *lmi_list; /* ptr to list of possible choices */ 204 char *lmi_macroname; /* pointer to macro name */ 205 ushort_t lmi_ba_index; /* index into bootaux vector */ 206 ushort_t lmi_macrolen; /* macro length */ 207 } libmacros[] = { 208 { NULL, "CPU", BA_CPU, 0 }, 209 { NULL, "MMU", BA_MMU, 0 } 210 }; 211 212 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info) 213 214 char *boot_cpu_compatible_list; /* make $CPU available */ 215 216 217 char *kobj_module_path; /* module search path */ 218 vmem_t *text_arena; /* module text arena */ 219 static vmem_t *data_arena; /* module data & bss arena */ 220 static vmem_t *ctf_arena; /* CTF debug data arena */ 221 static struct modctl *kobj_modules = NULL; /* modules loaded */ 222 int kobj_mmu_pagesize; /* system pagesize */ 223 static int lg_pagesize; /* "large" pagesize */ 224 static int kobj_last_module_id = 0; /* id assignment */ 225 static kmutex_t kobj_lock; /* protects mach memory list */ 226 227 /* 228 * The following functions have been implemented by the kernel. 229 * However, many 3rd party drivers provide their own implementations 230 * of these functions. When such drivers are loaded, messages 231 * indicating that these symbols have been multiply defined will be 232 * emitted to the console. To avoid alarming customers for no good 233 * reason, we simply suppress such warnings for the following set of 234 * functions. 235 */ 236 static char *suppress_sym_list[] = 237 { 238 "strstr", 239 "strncat", 240 "strlcat", 241 "strlcpy", 242 "strspn", 243 "memcpy", 244 "memset", 245 "memmove", 246 "memcmp", 247 "memchr", 248 "__udivdi3", 249 "__divdi3", 250 "__umoddi3", 251 "__moddi3", 252 NULL /* This entry must exist */ 253 }; 254 255 /* indexed by KOBJ_NOTIFY_* */ 256 static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1]; 257 258 /* 259 * TNF probe management globals 260 */ 261 tnf_probe_control_t *__tnf_probe_list_head = NULL; 262 tnf_tag_data_t *__tnf_tag_list_head = NULL; 263 int tnf_changed_probe_list = 0; 264 265 /* 266 * Prefix for statically defined tracing (SDT) DTrace probes. 267 */ 268 const char *sdt_prefix = "__dtrace_probe_"; 269 270 /* 271 * Beginning and end of the kernel's dynamic text/data segments. 272 */ 273 static caddr_t _text; 274 static caddr_t _etext; 275 static caddr_t _data; 276 277 /* 278 * The sparc linker doesn't create a memory location 279 * for a variable named _edata, so _edata can only be 280 * referred to, not modified. krtld needs a static 281 * variable to modify it - within krtld, of course - 282 * outside of krtld, e_data is used in all kernels. 283 */ 284 #if defined(__sparc) 285 static caddr_t _edata; 286 #else 287 extern caddr_t _edata; 288 #endif 289 290 Addr dynseg = 0; /* load address of "dynamic" segment */ 291 size_t dynsize; /* "dynamic" segment size */ 292 293 294 int standalone = 1; /* an unwholey kernel? */ 295 int use_iflush; /* iflush after relocations */ 296 297 /* 298 * _kobj_printf() 299 * 300 * Common printf function pointer. Can handle only one conversion 301 * specification in the format string. Some of the functions invoked 302 * through this function pointer cannot handle more that one conversion 303 * specification in the format string. 304 */ 305 void (*_kobj_printf)(void *, const char *, ...); /* printf routine */ 306 307 /* 308 * Standalone function pointers for use within krtld. 309 * Many platforms implement optimized platmod versions of 310 * utilities such as bcopy and any such are not yet available 311 * until the kernel is more completely stitched together. 312 * See kobj_impl.h 313 */ 314 void (*kobj_bcopy)(const void *, void *, size_t); 315 void (*kobj_bzero)(void *, size_t); 316 size_t (*kobj_strlcat)(char *, const char *, size_t); 317 318 static kobj_stat_t kobj_stat; 319 320 #define MINALIGN 8 /* at least a double-word */ 321 322 int 323 get_weakish_int(int *ip) 324 { 325 if (standalone) 326 return (0); 327 return (ip == NULL ? 0 : *ip); 328 } 329 330 static void * 331 get_weakish_pointer(void **ptrp) 332 { 333 if (standalone) 334 return (0); 335 return (ptrp == NULL ? 0 : *ptrp); 336 } 337 338 /* 339 * XXX fix dependencies on "kernel"; this should work 340 * for other standalone binaries as well. 341 * 342 * XXX Fix hashing code to use one pointer to 343 * hash entries. 344 * |----------| 345 * | nbuckets | 346 * |----------| 347 * | nchains | 348 * |----------| 349 * | bucket[] | 350 * |----------| 351 * | chain[] | 352 * |----------| 353 */ 354 355 /* 356 * Load, bind and relocate all modules that 357 * form the primary kernel. At this point, our 358 * externals have not been relocated. 359 */ 360 void 361 kobj_init( 362 void *romvec, 363 void *dvec, 364 struct bootops *bootvec, 365 val_t *bootaux) 366 { 367 struct module *mp; 368 struct modctl *modp; 369 Addr entry; 370 char filename[MAXPATHLEN]; 371 372 /* 373 * Save these to pass on to 374 * the booted standalone. 375 */ 376 romp = romvec; 377 dbvec = dvec; 378 379 ops = bootvec; 380 kobj_setup_standalone_vectors(); 381 382 KOBJ_MARK("Entered kobj_init()"); 383 384 (void) BOP_GETPROP(ops, "whoami", filename); 385 386 /* 387 * We don't support standalone debuggers anymore. The use of kadb 388 * will interfere with the later use of kmdb. Let the user mend 389 * their ways now. Users will reach this message if they still 390 * have the kadb binary on their system (perhaps they used an old 391 * bfu, or maybe they intentionally copied it there) and have 392 * specified its use in a way that eluded our checking in the boot 393 * program. 394 */ 395 if (dvec != NULL) { 396 _kobj_printf(ops, "\nWARNING: Standalone debuggers such as " 397 "kadb are no longer supported\n\n"); 398 goto fail; 399 } 400 401 #if defined(_OBP) 402 /* 403 * OBP allows us to read both the ramdisk and 404 * the underlying root fs when root is a disk. 405 * This can lower incidences of unbootable systems 406 * when the archive is out-of-date with the /etc 407 * state files. 408 */ 409 if (BOP_MOUNTROOT() != BOOT_SVC_OK) { 410 _kobj_printf(ops, "can't mount boot fs\n"); 411 goto fail; 412 } 413 #else 414 { 415 /* on x86, we always boot with a ramdisk */ 416 (void) kobj_boot_mountroot(); 417 418 /* 419 * Now that the ramdisk is mounted, finish boot property 420 * initialization. 421 */ 422 boot_prop_finish(); 423 } 424 425 #if !defined(_UNIX_KRTLD) 426 /* 427 * 'unix' is linked together with 'krtld' into one executable and 428 * the early boot code does -not- hand us any of the dynamic metadata 429 * about the executable. In particular, it does not read in, map or 430 * otherwise look at the program headers. We fake all that up now. 431 * 432 * We do this early as DTrace static probes and tnf probes both call 433 * undefined references. We have to process those relocations before 434 * calling any of them. 435 * 436 * OBP tells kobj_start() where the ELF image is in memory, so it 437 * synthesized bootaux before kobj_init() was called 438 */ 439 if (bootaux[BA_PHDR].ba_ptr == NULL) 440 synthetic_bootaux(filename, bootaux); 441 442 #endif /* !_UNIX_KRTLD */ 443 #endif /* _OBP */ 444 445 /* 446 * Save the interesting attribute-values 447 * (scanned by kobj_boot). 448 */ 449 attr_val(bootaux); 450 451 /* 452 * Set the module search path. 453 */ 454 kobj_module_path = getmodpath(filename); 455 456 boot_cpu_compatible_list = find_libmacro("CPU"); 457 458 /* 459 * These two modules have actually been 460 * loaded by boot, but we finish the job 461 * by introducing them into the world of 462 * loadable modules. 463 */ 464 465 mp = load_exec(bootaux, filename); 466 load_linker(bootaux); 467 468 /* 469 * Load all the primary dependent modules. 470 */ 471 if (load_primary(mp, KOBJ_LM_PRIMARY) == -1) 472 goto fail; 473 474 /* 475 * Glue it together. 476 */ 477 if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1) 478 goto fail; 479 480 entry = bootaux[BA_ENTRY].ba_val; 481 482 /* 483 * Get the boot flags 484 */ 485 bootflags(ops); 486 487 if (boothowto & RB_VERBOSE) 488 kobj_lm_dump(KOBJ_LM_PRIMARY); 489 490 kobj_kdi_init(); 491 492 if (boothowto & RB_KMDB) { 493 if (load_kmdb(bootaux) < 0) 494 goto fail; 495 } 496 497 /* 498 * Post setup. 499 */ 500 s_text = _text; 501 e_text = _etext; 502 s_data = _data; 503 e_data = _edata; 504 505 kobj_sync_instruction_memory(s_text, e_text - s_text); 506 507 #ifdef KOBJ_DEBUG 508 if (kobj_debug & D_DEBUG) 509 _kobj_printf(ops, 510 "krtld: transferring control to: 0x%p\n", entry); 511 #endif 512 513 /* 514 * Make sure the mod system knows about the modules already loaded. 515 */ 516 last_module_id = kobj_last_module_id; 517 bcopy(kobj_modules, &modules, sizeof (modules)); 518 modp = &modules; 519 do { 520 if (modp->mod_next == kobj_modules) 521 modp->mod_next = &modules; 522 if (modp->mod_prev == kobj_modules) 523 modp->mod_prev = &modules; 524 } while ((modp = modp->mod_next) != &modules); 525 526 standalone = 0; 527 528 #ifdef KOBJ_DEBUG 529 if (kobj_debug & D_DEBUG) 530 _kobj_printf(ops, 531 "krtld: really transferring control to: 0x%p\n", entry); 532 #endif 533 534 /* restore printf/bcopy/bzero vectors before returning */ 535 kobj_restore_vectors(); 536 537 #if defined(_DBOOT) 538 /* 539 * krtld was called from a dboot ELF section, the embedded 540 * dboot code contains the real entry via bootaux 541 */ 542 exitto((caddr_t)entry); 543 #else 544 /* 545 * krtld was directly called from startup 546 */ 547 return; 548 #endif 549 550 fail: 551 552 _kobj_printf(ops, "krtld: error during initial load/link phase\n"); 553 554 #if !defined(_UNIX_KRTLD) 555 _kobj_printf(ops, "\n"); 556 _kobj_printf(ops, "krtld could neither locate nor resolve symbols" 557 " for:\n"); 558 _kobj_printf(ops, " %s\n", filename); 559 _kobj_printf(ops, "in the boot archive. Please verify that this" 560 " file\n"); 561 _kobj_printf(ops, "matches what is found in the boot archive.\n"); 562 _kobj_printf(ops, "You may need to boot using the Solaris failsafe to" 563 " fix this.\n"); 564 bop_panic("Unable to boot"); 565 #endif 566 } 567 568 #if !defined(_UNIX_KRTLD) && !defined(_OBP) 569 /* 570 * Synthesize additional metadata that describes the executable if 571 * krtld's caller didn't do it. 572 * 573 * (When the dynamic executable has an interpreter, the boot program 574 * does all this for us. Where we don't have an interpreter, (or a 575 * even a boot program, perhaps) we have to do this for ourselves.) 576 */ 577 static void 578 synthetic_bootaux(char *filename, val_t *bootaux) 579 { 580 Ehdr ehdr; 581 caddr_t phdrbase; 582 struct _buf *file; 583 int i, n; 584 585 /* 586 * Elf header 587 */ 588 KOBJ_MARK("synthetic_bootaux()"); 589 KOBJ_MARK(filename); 590 file = kobj_open_file(filename); 591 if (file == (struct _buf *)-1) { 592 _kobj_printf(ops, "krtld: failed to open '%s'\n", filename); 593 return; 594 } 595 KOBJ_MARK("reading program headers"); 596 if (kobj_read_file(file, (char *)&ehdr, sizeof (ehdr), 0) < 0) { 597 _kobj_printf(ops, "krtld: %s: failed to read ehder\n", 598 filename); 599 return; 600 } 601 602 /* 603 * Program headers 604 */ 605 bootaux[BA_PHNUM].ba_val = ehdr.e_phnum; 606 bootaux[BA_PHENT].ba_val = ehdr.e_phentsize; 607 n = ehdr.e_phentsize * ehdr.e_phnum; 608 609 phdrbase = kobj_alloc(n, KM_WAIT | KM_TMP); 610 611 if (kobj_read_file(file, phdrbase, n, ehdr.e_phoff) < 0) { 612 _kobj_printf(ops, "krtld: %s: failed to read phdrs\n", 613 filename); 614 return; 615 } 616 bootaux[BA_PHDR].ba_ptr = phdrbase; 617 kobj_close_file(file); 618 KOBJ_MARK("closed file"); 619 620 /* 621 * Find the dynamic section address 622 */ 623 for (i = 0; i < ehdr.e_phnum; i++) { 624 Phdr *phdr = (Phdr *)(phdrbase + ehdr.e_phentsize * i); 625 626 if (phdr->p_type == PT_DYNAMIC) { 627 bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr; 628 break; 629 } 630 } 631 KOBJ_MARK("synthetic_bootaux() done"); 632 } 633 #endif /* !_UNIX_KRTLD && !_OBP */ 634 635 /* 636 * Set up any global information derived 637 * from attribute/values in the boot or 638 * aux vector. 639 */ 640 static void 641 attr_val(val_t *bootaux) 642 { 643 Phdr *phdr; 644 int phnum, phsize; 645 int i; 646 647 KOBJ_MARK("attr_val()"); 648 kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val; 649 lg_pagesize = bootaux[BA_LPAGESZ].ba_val; 650 use_iflush = bootaux[BA_IFLUSH].ba_val; 651 652 phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr; 653 phnum = bootaux[BA_PHNUM].ba_val; 654 phsize = bootaux[BA_PHENT].ba_val; 655 for (i = 0; i < phnum; i++) { 656 phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize); 657 658 if (phdr->p_type != PT_LOAD) { 659 continue; 660 } 661 /* 662 * Bounds of the various segments. 663 */ 664 if (!(phdr->p_flags & PF_X)) { 665 #if defined(_RELSEG) 666 /* 667 * sparc kernel puts the dynamic info 668 * into a separate segment, which is 669 * free'd in bop_fini() 670 */ 671 ASSERT(phdr->p_vaddr != 0); 672 dynseg = phdr->p_vaddr; 673 dynsize = phdr->p_memsz; 674 #else 675 ASSERT(phdr->p_vaddr == 0); 676 #endif 677 } else { 678 if (phdr->p_flags & PF_W) { 679 _data = (caddr_t)phdr->p_vaddr; 680 _edata = _data + phdr->p_memsz; 681 } else { 682 _text = (caddr_t)phdr->p_vaddr; 683 _etext = _text + phdr->p_memsz; 684 } 685 } 686 } 687 688 /* To do the kobj_alloc, _edata needs to be set. */ 689 for (i = 0; i < NLIBMACROS; i++) { 690 if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) { 691 libmacros[i].lmi_list = kobj_alloc( 692 strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) + 693 1, KM_WAIT); 694 (void) strcpy(libmacros[i].lmi_list, 695 bootaux[libmacros[i].lmi_ba_index].ba_ptr); 696 } 697 libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname); 698 } 699 } 700 701 /* 702 * Set up the booted executable. 703 */ 704 static struct module * 705 load_exec(val_t *bootaux, char *filename) 706 { 707 struct modctl *cp; 708 struct module *mp; 709 Dyn *dyn; 710 Sym *sp; 711 int i, lsize, osize, nsize, allocsize; 712 char *libname, *tmp; 713 char path[MAXPATHLEN]; 714 715 #ifdef KOBJ_DEBUG 716 if (kobj_debug & D_DEBUG) 717 _kobj_printf(ops, "module path '%s'\n", kobj_module_path); 718 #endif 719 720 KOBJ_MARK("add_primary"); 721 cp = add_primary(filename, KOBJ_LM_PRIMARY); 722 723 KOBJ_MARK("struct module"); 724 mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 725 cp->mod_mp = mp; 726 727 /* 728 * We don't have the following information 729 * since this module is an executable and not 730 * a relocatable .o. 731 */ 732 mp->symtbl_section = 0; 733 mp->shdrs = NULL; 734 mp->strhdr = NULL; 735 736 /* 737 * Since this module is the only exception, 738 * we cons up some section headers. 739 */ 740 KOBJ_MARK("symhdr"); 741 mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 742 743 KOBJ_MARK("strhdr"); 744 mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 745 746 mp->symhdr->sh_type = SHT_SYMTAB; 747 mp->strhdr->sh_type = SHT_STRTAB; 748 /* 749 * Scan the dynamic structure. 750 */ 751 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 752 dyn->d_tag != DT_NULL; dyn++) { 753 switch (dyn->d_tag) { 754 case DT_SYMTAB: 755 mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr; 756 mp->symhdr->sh_addr = dyn->d_un.d_ptr; 757 break; 758 case DT_HASH: 759 mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1); 760 mp->hashsize = *(uint_t *)dyn->d_un.d_ptr; 761 break; 762 case DT_STRTAB: 763 mp->strings = (char *)dyn->d_un.d_ptr; 764 mp->strhdr->sh_addr = dyn->d_un.d_ptr; 765 break; 766 case DT_STRSZ: 767 mp->strhdr->sh_size = dyn->d_un.d_val; 768 break; 769 case DT_SYMENT: 770 mp->symhdr->sh_entsize = dyn->d_un.d_val; 771 break; 772 } 773 } 774 775 /* 776 * Collapse any DT_NEEDED entries into one string. 777 */ 778 nsize = osize = 0; 779 allocsize = MAXPATHLEN; 780 781 KOBJ_MARK("depends_on"); 782 mp->depends_on = kobj_alloc(allocsize, KM_WAIT); 783 784 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 785 dyn->d_tag != DT_NULL; dyn++) 786 if (dyn->d_tag == DT_NEEDED) { 787 char *_lib; 788 789 libname = mp->strings + dyn->d_un.d_val; 790 if (strchr(libname, '$') != NULL) { 791 if ((_lib = expand_libmacro(libname, 792 path, path)) != NULL) 793 libname = _lib; 794 else 795 _kobj_printf(ops, "krtld: " 796 "load_exec: fail to " 797 "expand %s\n", libname); 798 } 799 lsize = strlen(libname); 800 nsize += lsize; 801 if (nsize + 1 > allocsize) { 802 KOBJ_MARK("grow depends_on"); 803 tmp = kobj_alloc(allocsize + MAXPATHLEN, 804 KM_WAIT); 805 bcopy(mp->depends_on, tmp, osize); 806 kobj_free(mp->depends_on, allocsize); 807 mp->depends_on = tmp; 808 allocsize += MAXPATHLEN; 809 } 810 bcopy(libname, mp->depends_on + osize, lsize); 811 *(mp->depends_on + nsize) = ' '; /* separate */ 812 nsize++; 813 osize = nsize; 814 } 815 if (nsize) { 816 mp->depends_on[nsize - 1] = '\0'; /* terminate the string */ 817 /* 818 * alloc with exact size and copy whatever it got over 819 */ 820 KOBJ_MARK("realloc depends_on"); 821 tmp = kobj_alloc(nsize, KM_WAIT); 822 bcopy(mp->depends_on, tmp, nsize); 823 kobj_free(mp->depends_on, allocsize); 824 mp->depends_on = tmp; 825 } else { 826 kobj_free(mp->depends_on, allocsize); 827 mp->depends_on = NULL; 828 } 829 830 mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */ 831 mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize; 832 /* 833 * We allocate our own table since we don't 834 * hash undefined references. 835 */ 836 KOBJ_MARK("chains"); 837 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 838 KOBJ_MARK("buckets"); 839 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 840 841 mp->text = _text; 842 mp->data = _data; 843 844 mp->text_size = _etext - _text; 845 mp->data_size = _edata - _data; 846 847 cp->mod_text = mp->text; 848 cp->mod_text_size = mp->text_size; 849 850 mp->filename = cp->mod_filename; 851 852 #ifdef KOBJ_DEBUG 853 if (kobj_debug & D_LOADING) { 854 _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 855 _kobj_printf(ops, "\ttext: 0x%p", mp->text); 856 _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 857 _kobj_printf(ops, "\tdata: 0x%p", mp->data); 858 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 859 } 860 #endif /* KOBJ_DEBUG */ 861 862 /* 863 * Insert symbols into the hash table. 864 */ 865 for (i = 0; i < mp->nsyms; i++) { 866 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 867 868 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 869 continue; 870 #if defined(__sparc) 871 /* 872 * Register symbols are ignored in the kernel 873 */ 874 if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) 875 continue; 876 #endif /* __sparc */ 877 878 sym_insert(mp, mp->strings + sp->st_name, i); 879 } 880 881 KOBJ_MARK("load_exec done"); 882 return (mp); 883 } 884 885 /* 886 * Set up the linker module (if it's compiled in, LDNAME is NULL) 887 */ 888 static void 889 load_linker(val_t *bootaux) 890 { 891 struct module *kmp = (struct module *)kobj_modules->mod_mp; 892 struct module *mp; 893 struct modctl *cp; 894 int i; 895 Shdr *shp; 896 Sym *sp; 897 int shsize; 898 char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr; 899 900 /* 901 * On some architectures, krtld is compiled into the kernel. 902 */ 903 if (dlname == NULL) 904 return; 905 906 cp = add_primary(dlname, KOBJ_LM_PRIMARY); 907 908 mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 909 910 cp->mod_mp = mp; 911 mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr; 912 shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum; 913 mp->shdrs = kobj_alloc(shsize, KM_WAIT); 914 bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize); 915 916 for (i = 1; i < (int)mp->hdr.e_shnum; i++) { 917 shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize)); 918 919 if (shp->sh_flags & SHF_ALLOC) { 920 if (shp->sh_flags & SHF_WRITE) { 921 if (mp->data == NULL) 922 mp->data = (char *)shp->sh_addr; 923 } else if (mp->text == NULL) { 924 mp->text = (char *)shp->sh_addr; 925 } 926 } 927 if (shp->sh_type == SHT_SYMTAB) { 928 mp->symtbl_section = i; 929 mp->symhdr = shp; 930 mp->symspace = mp->symtbl = (char *)shp->sh_addr; 931 } 932 } 933 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 934 mp->flags = KOBJ_INTERP|KOBJ_PRIM; 935 mp->strhdr = (Shdr *) 936 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 937 mp->strings = (char *)mp->strhdr->sh_addr; 938 mp->hashsize = kobj_gethashsize(mp->nsyms); 939 940 mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) + 941 (mp->hashsize + mp->nsyms) * sizeof (symid_t); 942 943 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 944 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 945 946 mp->bss = bootaux[BA_BSS].ba_val; 947 mp->bss_align = 0; /* pre-aligned during allocation */ 948 mp->bss_size = (uintptr_t)_edata - mp->bss; 949 mp->text_size = _etext - mp->text; 950 mp->data_size = _edata - mp->data; 951 mp->filename = cp->mod_filename; 952 cp->mod_text = mp->text; 953 cp->mod_text_size = mp->text_size; 954 955 /* 956 * Now that we've figured out where the linker is, 957 * set the limits for the booted object. 958 */ 959 kmp->text_size = (size_t)(mp->text - kmp->text); 960 kmp->data_size = (size_t)(mp->data - kmp->data); 961 kobj_modules->mod_text_size = kmp->text_size; 962 963 #ifdef KOBJ_DEBUG 964 if (kobj_debug & D_LOADING) { 965 _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 966 _kobj_printf(ops, "\ttext:0x%p", mp->text); 967 _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 968 _kobj_printf(ops, "\tdata:0x%p", mp->data); 969 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 970 } 971 #endif /* KOBJ_DEBUG */ 972 973 /* 974 * Insert the symbols into the hash table. 975 */ 976 for (i = 0; i < mp->nsyms; i++) { 977 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 978 979 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 980 continue; 981 if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 982 if (sp->st_shndx == SHN_COMMON) 983 sp->st_shndx = SHN_ABS; 984 } 985 sym_insert(mp, mp->strings + sp->st_name, i); 986 } 987 988 } 989 990 static kobj_notify_list_t ** 991 kobj_notify_lookup(uint_t type) 992 { 993 ASSERT(type != 0 && type < sizeof (kobj_notifiers) / 994 sizeof (kobj_notify_list_t *)); 995 996 return (&kobj_notifiers[type]); 997 } 998 999 int 1000 kobj_notify_add(kobj_notify_list_t *knp) 1001 { 1002 kobj_notify_list_t **knl; 1003 1004 knl = kobj_notify_lookup(knp->kn_type); 1005 1006 knp->kn_next = NULL; 1007 knp->kn_prev = NULL; 1008 1009 mutex_enter(&kobj_lock); 1010 1011 if (*knl != NULL) { 1012 (*knl)->kn_prev = knp; 1013 knp->kn_next = *knl; 1014 } 1015 (*knl) = knp; 1016 1017 mutex_exit(&kobj_lock); 1018 return (0); 1019 } 1020 1021 int 1022 kobj_notify_remove(kobj_notify_list_t *knp) 1023 { 1024 kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type); 1025 kobj_notify_list_t *tknp; 1026 1027 mutex_enter(&kobj_lock); 1028 1029 /* LINTED */ 1030 if (tknp = knp->kn_next) 1031 tknp->kn_prev = knp->kn_prev; 1032 1033 /* LINTED */ 1034 if (tknp = knp->kn_prev) 1035 tknp->kn_next = knp->kn_next; 1036 else 1037 *knl = knp->kn_next; 1038 1039 mutex_exit(&kobj_lock); 1040 1041 return (0); 1042 } 1043 1044 /* 1045 * Notify all interested callbacks of a specified change in module state. 1046 */ 1047 static void 1048 kobj_notify(int type, struct modctl *modp) 1049 { 1050 kobj_notify_list_t *knp; 1051 1052 if (modp->mod_loadflags & MOD_NONOTIFY || standalone) 1053 return; 1054 1055 mutex_enter(&kobj_lock); 1056 1057 for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next) 1058 knp->kn_func(type, modp); 1059 1060 /* 1061 * KDI notification must be last (it has to allow for work done by the 1062 * other notification callbacks), so we call it manually. 1063 */ 1064 kobj_kdi_mod_notify(type, modp); 1065 1066 mutex_exit(&kobj_lock); 1067 } 1068 1069 /* 1070 * Create the module path. 1071 */ 1072 static char * 1073 getmodpath(const char *filename) 1074 { 1075 char *path = kobj_zalloc(MAXPATHLEN, KM_WAIT); 1076 1077 /* 1078 * Platform code gets first crack, then add 1079 * the default components 1080 */ 1081 mach_modpath(path, filename); 1082 if (*path != '\0') 1083 (void) strcat(path, " "