10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 223446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Kernel's linker/loader 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/sysmacros.h> 350Sstevel@tonic-gate #include <sys/systm.h> 360Sstevel@tonic-gate #include <sys/user.h> 370Sstevel@tonic-gate #include <sys/kmem.h> 380Sstevel@tonic-gate #include <sys/reboot.h> 390Sstevel@tonic-gate #include <sys/bootconf.h> 400Sstevel@tonic-gate #include <sys/debug.h> 410Sstevel@tonic-gate #include <sys/uio.h> 420Sstevel@tonic-gate #include <sys/file.h> 430Sstevel@tonic-gate #include <sys/vnode.h> 440Sstevel@tonic-gate #include <sys/user.h> 450Sstevel@tonic-gate #include <sys/mman.h> 460Sstevel@tonic-gate #include <vm/as.h> 470Sstevel@tonic-gate #include <vm/seg_kp.h> 480Sstevel@tonic-gate #include <vm/seg_kmem.h> 490Sstevel@tonic-gate #include <sys/elf.h> 500Sstevel@tonic-gate #include <sys/elf_notes.h> 510Sstevel@tonic-gate #include <sys/vmsystm.h> 520Sstevel@tonic-gate #include <sys/kdi.h> 530Sstevel@tonic-gate #include <sys/atomic.h> 540Sstevel@tonic-gate #include <sys/kmdb.h> 550Sstevel@tonic-gate 560Sstevel@tonic-gate #include <sys/link.h> 570Sstevel@tonic-gate #include <sys/kobj.h> 580Sstevel@tonic-gate #include <sys/ksyms.h> 590Sstevel@tonic-gate #include <sys/disp.h> 600Sstevel@tonic-gate #include <sys/modctl.h> 610Sstevel@tonic-gate #include <sys/varargs.h> 620Sstevel@tonic-gate #include <sys/kstat.h> 630Sstevel@tonic-gate #include <sys/kobj_impl.h> 640Sstevel@tonic-gate #include <sys/callb.h> 650Sstevel@tonic-gate #include <sys/cmn_err.h> 660Sstevel@tonic-gate #include <sys/tnf_probe.h> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <reloc.h> 690Sstevel@tonic-gate #include <kobj_kdi.h> 700Sstevel@tonic-gate #include <sys/sha1.h> 710Sstevel@tonic-gate #include <sys/crypto/elfsign.h> 720Sstevel@tonic-gate 730Sstevel@tonic-gate #if !defined(__sparc) 740Sstevel@tonic-gate #include <sys/bootvfs.h> 750Sstevel@tonic-gate #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * do_symbols() error codes 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate #define DOSYM_UNDEF -1 /* undefined symbol */ 810Sstevel@tonic-gate #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */ 820Sstevel@tonic-gate 833446Smrj static void synthetic_bootaux(char *, val_t *); 843446Smrj static struct module *load_exec(val_t *, char *); 850Sstevel@tonic-gate static void load_linker(val_t *); 863446Smrj static struct modctl *add_primary(const char *filename, int); 870Sstevel@tonic-gate static int bind_primary(val_t *, int); 880Sstevel@tonic-gate static int load_primary(struct module *, int); 890Sstevel@tonic-gate static int load_kmdb(val_t *); 900Sstevel@tonic-gate static int get_progbits(struct module *, struct _buf *); 910Sstevel@tonic-gate static int get_syms(struct module *, struct _buf *); 920Sstevel@tonic-gate static int get_ctf(struct module *, struct _buf *); 930Sstevel@tonic-gate static void get_signature(struct module *, struct _buf *); 940Sstevel@tonic-gate static int do_common(struct module *); 950Sstevel@tonic-gate static void add_dependent(struct module *, struct module *); 960Sstevel@tonic-gate static int do_dependents(struct modctl *, char *, size_t); 970Sstevel@tonic-gate static int do_symbols(struct module *, Elf64_Addr); 980Sstevel@tonic-gate static void module_assign(struct modctl *, struct module *); 990Sstevel@tonic-gate static void free_module_data(struct module *); 1000Sstevel@tonic-gate static char *depends_on(struct module *); 1013446Smrj static char *getmodpath(const char *); 1020Sstevel@tonic-gate static char *basename(char *); 1030Sstevel@tonic-gate static void attr_val(val_t *); 1040Sstevel@tonic-gate static char *find_libmacro(char *); 1050Sstevel@tonic-gate static char *expand_libmacro(char *, char *, char *); 1060Sstevel@tonic-gate static int read_bootflags(void); 1070Sstevel@tonic-gate static int kobj_boot_open(char *, int); 1080Sstevel@tonic-gate static int kobj_boot_close(int); 1090Sstevel@tonic-gate static int kobj_boot_seek(int, off_t, off_t); 1100Sstevel@tonic-gate static int kobj_boot_read(int, caddr_t, size_t); 1111544Seschrock static int kobj_boot_fstat(int, struct bootstat *); 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate static Sym *lookup_one(struct module *, const char *); 1140Sstevel@tonic-gate static void sym_insert(struct module *, char *, symid_t); 1150Sstevel@tonic-gate static Sym *sym_lookup(struct module *, Sym *); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /*PRINTFLIKE2*/ 1180Sstevel@tonic-gate static void kprintf(void *, const char *, ...) __KPRINTFLIKE(2); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate static struct kobjopen_tctl *kobjopen_alloc(char *filename); 1210Sstevel@tonic-gate static void kobjopen_free(struct kobjopen_tctl *ltp); 1220Sstevel@tonic-gate static void kobjopen_thread(struct kobjopen_tctl *ltp); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate extern int kcopy(const void *, void *, size_t); 1250Sstevel@tonic-gate extern int elf_mach_ok(Ehdr *); 1260Sstevel@tonic-gate extern int alloc_gottable(struct module *, caddr_t *, caddr_t *); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate static void tnf_unsplice_probes(unsigned int, struct modctl *); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate extern int modrootloaded; 1310Sstevel@tonic-gate extern int swaploaded; 1320Sstevel@tonic-gate extern int bop_io_quiesced; 1330Sstevel@tonic-gate extern int last_module_id; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate #ifdef KOBJ_DEBUG 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * Values that can be or'd in to kobj_debug and their effects: 1380Sstevel@tonic-gate * 1390Sstevel@tonic-gate * D_DEBUG - misc. debugging information. 1400Sstevel@tonic-gate * D_SYMBOLS - list symbols and their values as they are entered 1410Sstevel@tonic-gate * into the hash table 1420Sstevel@tonic-gate * D_RELOCATIONS - display relocation processing information 1430Sstevel@tonic-gate * D_LOADING - display information about each module as it 1440Sstevel@tonic-gate * is loaded. 1450Sstevel@tonic-gate */ 1460Sstevel@tonic-gate int kobj_debug = 0; 1473446Smrj 1483446Smrj #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \ 1493446Smrj (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s)) 1503446Smrj #else 1513446Smrj #define KOBJ_MARK(s) /* discard */ 1520Sstevel@tonic-gate #endif 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate #define MODPATH_PROPNAME "module-path" 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 1570Sstevel@tonic-gate static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/"; 1580Sstevel@tonic-gate #else 1590Sstevel@tonic-gate #define slash_moddir_suffix_slash "" 1600Sstevel@tonic-gate #endif 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate #define _moddebug get_weakish_int(&moddebug) 1630Sstevel@tonic-gate #define _modrootloaded get_weakish_int(&modrootloaded) 1640Sstevel@tonic-gate #define _swaploaded get_weakish_int(&swaploaded) 1650Sstevel@tonic-gate #define _ioquiesced get_weakish_int(&bop_io_quiesced) 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate #define mod(X) (struct module *)((X)->modl_modp->mod_mp) 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate void *romp; /* rom vector (opaque to us) */ 1700Sstevel@tonic-gate struct bootops *ops; /* bootops vector */ 1710Sstevel@tonic-gate void *dbvec; /* debug vector */ 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * kobjopen thread control structure 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate struct kobjopen_tctl { 1770Sstevel@tonic-gate ksema_t sema; 1780Sstevel@tonic-gate char *name; /* name of file */ 1790Sstevel@tonic-gate struct vnode *vp; /* vnode return from vn_open() */ 1800Sstevel@tonic-gate int Errno; /* error return from vnopen */ 1810Sstevel@tonic-gate }; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* 1840Sstevel@tonic-gate * Structure for defining dynamically expandable library macros 1850Sstevel@tonic-gate */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate struct lib_macro_info { 1880Sstevel@tonic-gate char *lmi_list; /* ptr to list of possible choices */ 1890Sstevel@tonic-gate char *lmi_macroname; /* pointer to macro name */ 1900Sstevel@tonic-gate ushort_t lmi_ba_index; /* index into bootaux vector */ 1910Sstevel@tonic-gate ushort_t lmi_macrolen; /* macro length */ 1920Sstevel@tonic-gate } libmacros[] = { 1930Sstevel@tonic-gate { NULL, "CPU", BA_CPU, 0 }, 1940Sstevel@tonic-gate { NULL, "MMU", BA_MMU, 0 } 1950Sstevel@tonic-gate }; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info) 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate char *boot_cpu_compatible_list; /* make $CPU available */ 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate #ifdef MPSAS 2020Sstevel@tonic-gate void sas_prisyms(struct modctl_list *); 2030Sstevel@tonic-gate void sas_syms(struct module *); 2040Sstevel@tonic-gate #endif 2050Sstevel@tonic-gate 2063446Smrj char *kobj_module_path; /* module search path */ 2070Sstevel@tonic-gate vmem_t *text_arena; /* module text arena */ 2080Sstevel@tonic-gate static vmem_t *data_arena; /* module data & bss arena */ 2090Sstevel@tonic-gate static vmem_t *ctf_arena; /* CTF debug data arena */ 2100Sstevel@tonic-gate static struct modctl *kobj_modules = NULL; /* modules loaded */ 2110Sstevel@tonic-gate int kobj_mmu_pagesize; /* system pagesize */ 2120Sstevel@tonic-gate static int lg_pagesize; /* "large" pagesize */ 2130Sstevel@tonic-gate static int kobj_last_module_id = 0; /* id assignment */ 2140Sstevel@tonic-gate static kmutex_t kobj_lock; /* protects mach memory list */ 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * The following functions have been implemented by the kernel. 2180Sstevel@tonic-gate * However, many 3rd party drivers provide their own implementations 2190Sstevel@tonic-gate * of these functions. When such drivers are loaded, messages 2200Sstevel@tonic-gate * indicateing that these symbols have been mulply defined will be 2210Sstevel@tonic-gate * emitted to the console. To avoid alarming customers for no good 2220Sstevel@tonic-gate * reason, we simply suppress such warnings for the following set of 2230Sstevel@tonic-gate * functions. 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate static char *suppress_sym_list[] = 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate "strstr", 2280Sstevel@tonic-gate "strncat", 2290Sstevel@tonic-gate "strlcat", 2300Sstevel@tonic-gate "strlcpy", 2310Sstevel@tonic-gate "strspn", 2320Sstevel@tonic-gate "memcpy", 2330Sstevel@tonic-gate "memset", 2340Sstevel@tonic-gate "memmove", 2350Sstevel@tonic-gate "memcmp", 2360Sstevel@tonic-gate "memchr", 2370Sstevel@tonic-gate "__udivdi3", 2380Sstevel@tonic-gate "__divdi3", 2390Sstevel@tonic-gate "__umoddi3", 2400Sstevel@tonic-gate "__moddi3", 2410Sstevel@tonic-gate NULL /* This entry must exist */ 2420Sstevel@tonic-gate }; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* indexed by KOBJ_NOTIFY_* */ 2450Sstevel@tonic-gate static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1]; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * TNF probe management globals 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate tnf_probe_control_t *__tnf_probe_list_head = NULL; 2510Sstevel@tonic-gate tnf_tag_data_t *__tnf_tag_list_head = NULL; 2520Sstevel@tonic-gate int tnf_changed_probe_list = 0; 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * Prefix for statically defined tracing (SDT) DTrace probes. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate const char *sdt_prefix = "__dtrace_probe_"; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate #if defined(__sparc) 2600Sstevel@tonic-gate /* 2610Sstevel@tonic-gate * Some PROMs return SUNW,UltraSPARC when they actually have 2620Sstevel@tonic-gate * SUNW,UltraSPARC-II cpus. SInce we're now filtering out all 2630Sstevel@tonic-gate * SUNW,UltraSPARC systems during the boot phase, we can safely 2640Sstevel@tonic-gate * point the auxv CPU value at SUNW,UltraSPARC-II. This is what 2650Sstevel@tonic-gate * we point it at. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate const char *ultra_2 = "SUNW,UltraSPARC-II"; 2680Sstevel@tonic-gate #endif 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate /* 2713446Smrj * Beginning and end of the kernel's dynamic text/data segments. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate static caddr_t _text; 2740Sstevel@tonic-gate static caddr_t _etext; 2753446Smrj static caddr_t _data; 2763446Smrj 2773446Smrj /* 2783446Smrj * XXX Hmm. The sparc linker fails to define this symbol. 2793446Smrj */ 2803446Smrj #if !defined(__sparc) 2813446Smrj extern 2823446Smrj #endif 2830Sstevel@tonic-gate caddr_t _edata; 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate static Addr dynseg = 0; /* load address of "dynamic" segment */ 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate int standalone = 1; /* an unwholey kernel? */ 2880Sstevel@tonic-gate int use_iflush; /* iflush after relocations */ 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* 2910Sstevel@tonic-gate * _kobj_printf() 2920Sstevel@tonic-gate * 2930Sstevel@tonic-gate * Common printf function pointer. Can handle only one conversion 2940Sstevel@tonic-gate * specification in the format string. Some of the functions invoked 2950Sstevel@tonic-gate * through this function pointer cannot handle more that one conversion 2960Sstevel@tonic-gate * specification in the format string. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate void (*_kobj_printf)(void *, const char *, ...); /* printf routine */ 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate static kobj_stat_t kobj_stat; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate #define MINALIGN 8 /* at least a double-word */ 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate int 3050Sstevel@tonic-gate get_weakish_int(int *ip) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate if (standalone) 3080Sstevel@tonic-gate return (0); 3090Sstevel@tonic-gate return (ip == NULL ? 0 : *ip); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate static void * 3130Sstevel@tonic-gate get_weakish_pointer(void **ptrp) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate if (standalone) 3160Sstevel@tonic-gate return (0); 3170Sstevel@tonic-gate return (ptrp == NULL ? 0 : *ptrp); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * XXX fix dependencies on "kernel"; this should work 3220Sstevel@tonic-gate * for other standalone binaries as well. 3230Sstevel@tonic-gate * 3240Sstevel@tonic-gate * XXX Fix hashing code to use one pointer to 3250Sstevel@tonic-gate * hash entries. 3260Sstevel@tonic-gate * |----------| 3270Sstevel@tonic-gate * | nbuckets | 3280Sstevel@tonic-gate * |----------| 3290Sstevel@tonic-gate * | nchains | 3300Sstevel@tonic-gate * |----------| 3310Sstevel@tonic-gate * | bucket[] | 3320Sstevel@tonic-gate * |----------| 3330Sstevel@tonic-gate * | chain[] | 3340Sstevel@tonic-gate * |----------| 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Load, bind and relocate all modules that 3390Sstevel@tonic-gate * form the primary kernel. At this point, our 3400Sstevel@tonic-gate * externals have not been relocated. 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate void 3430Sstevel@tonic-gate kobj_init( 3440Sstevel@tonic-gate void *romvec, 3450Sstevel@tonic-gate void *dvec, 3460Sstevel@tonic-gate struct bootops *bootvec, 3470Sstevel@tonic-gate val_t *bootaux) 3480Sstevel@tonic-gate { 3490Sstevel@tonic-gate struct module *mp; 3500Sstevel@tonic-gate struct modctl *modp; 3510Sstevel@tonic-gate Addr entry; 3523446Smrj char filename[MAXPATHLEN]; 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * Save these to pass on to 3560Sstevel@tonic-gate * the booted standalone. 3570Sstevel@tonic-gate */ 3580Sstevel@tonic-gate romp = romvec; 3590Sstevel@tonic-gate dbvec = dvec; 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate ops = bootvec; 3620Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 3630Sstevel@tonic-gate _kobj_printf = (void (*)(void *, const char *, ...))ops->bsys_printf; 3640Sstevel@tonic-gate #else 3650Sstevel@tonic-gate _kobj_printf = (void (*)(void *, const char *, ...))bop_putsarg; 3660Sstevel@tonic-gate #endif 3673446Smrj KOBJ_MARK("Entered kobj_init()"); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate #if defined(__sparc) 3700Sstevel@tonic-gate /* XXXQ should suppress this test on sun4v */ 3710Sstevel@tonic-gate if (bootaux[BA_CPU].ba_ptr) { 3720Sstevel@tonic-gate if (strcmp("SUNW,UltraSPARC", bootaux[BA_CPU].ba_ptr) == 0) { 3730Sstevel@tonic-gate bootaux[BA_CPU].ba_ptr = (void *) ultra_2; 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate #endif 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * Check bootops version. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate if (BOP_GETVERSION(ops) != BO_VERSION) { 3820Sstevel@tonic-gate _kobj_printf(ops, "Warning: Using boot version %d, ", 3830Sstevel@tonic-gate BOP_GETVERSION(ops)); 3840Sstevel@tonic-gate _kobj_printf(ops, "expected %d\n", BO_VERSION); 3850Sstevel@tonic-gate } 3863446Smrj #ifdef KOBJ_DEBUG 3873446Smrj else if (kobj_debug & D_DEBUG) { 3883446Smrj /* 3893446Smrj * Say -something- so we know we got this far .. 3903446Smrj */ 3913446Smrj _kobj_printf(ops, "krtld: Using boot version %d.\n", 3923446Smrj BOP_GETVERSION(ops)); 3933446Smrj } 3943446Smrj #endif 3953446Smrj 3963446Smrj (void) BOP_GETPROP(ops, "whoami", filename); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * We don't support standalone debuggers anymore. The use of kadb 4000Sstevel@tonic-gate * will interfere with the later use of kmdb. Let the user mend 4010Sstevel@tonic-gate * their ways now. Users will reach this message if they still 4020Sstevel@tonic-gate * have the kadb binary on their system (perhaps they used an old 4030Sstevel@tonic-gate * bfu, or maybe they intentionally copied it there) and have 4040Sstevel@tonic-gate * specified its use in a way that eluded our checking in the boot 4050Sstevel@tonic-gate * program. 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate if (dvec != NULL) { 4080Sstevel@tonic-gate _kobj_printf(ops, "\nWARNING: Standalone debuggers such as " 4090Sstevel@tonic-gate "kadb are no longer supported\n\n"); 4100Sstevel@tonic-gate goto fail; 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate #ifndef __sparc 4140Sstevel@tonic-gate { 4150Sstevel@tonic-gate /* on x86, we always boot with a ramdisk */ 4160Sstevel@tonic-gate extern int kobj_boot_mountroot(void); 4170Sstevel@tonic-gate (void) kobj_boot_mountroot(); 4183446Smrj 4193446Smrj /* 4203446Smrj * Now that the ramdisk is mounted, finish boot property 4213446Smrj * initialization. 4223446Smrj */ 4233446Smrj boot_prop_finish(); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate #endif 4260Sstevel@tonic-gate 4273446Smrj #if !defined(_UNIX_KRTLD) 4283446Smrj /* 4293446Smrj * If 'unix' is linked together with 'krtld' into one executable, 4303446Smrj * the early boot code does -not- hand us any of the dynamic metadata 4313446Smrj * about the executable. In particular, it does not read in, map or 4323446Smrj * otherwise look at the program headers. We fake all that up now. 4333446Smrj * 4343446Smrj * We do this early as DTrace static probes and tnf probes both call 4353446Smrj * undefined references. We have to process those relocations before 4363446Smrj * calling any of them. 4373446Smrj */ 4383446Smrj if (bootaux[BA_PHDR].ba_ptr == NULL) 4393446Smrj synthetic_bootaux(filename, bootaux); 4403446Smrj #endif 4413446Smrj 4423446Smrj /* 4433446Smrj * Save the interesting attribute-values 4443446Smrj * (scanned by kobj_boot). 4453446Smrj */ 4463446Smrj attr_val(bootaux); 4473446Smrj 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * Set the module search path. 4500Sstevel@tonic-gate */ 4513446Smrj kobj_module_path = getmodpath(filename); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate boot_cpu_compatible_list = find_libmacro("CPU"); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate /* 4560Sstevel@tonic-gate * These two modules have actually been 4570Sstevel@tonic-gate * loaded by boot, but we finish the job 4580Sstevel@tonic-gate * by introducing them into the world of 4590Sstevel@tonic-gate * loadable modules. 4600Sstevel@tonic-gate */ 4610Sstevel@tonic-gate 4623446Smrj mp = load_exec(bootaux, filename); 4630Sstevel@tonic-gate load_linker(bootaux); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* 4660Sstevel@tonic-gate * Load all the primary dependent modules. 4670Sstevel@tonic-gate */ 4680Sstevel@tonic-gate if (load_primary(mp, KOBJ_LM_PRIMARY) == -1) 4690Sstevel@tonic-gate goto fail; 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate /* 4720Sstevel@tonic-gate * Glue it together. 4730Sstevel@tonic-gate */ 4740Sstevel@tonic-gate if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1) 4750Sstevel@tonic-gate goto fail; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate entry = bootaux[BA_ENTRY].ba_val; 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate #ifdef __sparc 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * On sparcv9, boot scratch memory is running out. 4820Sstevel@tonic-gate * Free the temporary allocations here to allow boot 4830Sstevel@tonic-gate * to continue. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate kobj_tmp_free(); 4860Sstevel@tonic-gate #endif 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* 4890Sstevel@tonic-gate * Get the boot flags 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate bootflags(ops); 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate if (boothowto & RB_VERBOSE) 4940Sstevel@tonic-gate kobj_lm_dump(KOBJ_LM_PRIMARY); 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate kobj_kdi_init(); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate if (boothowto & RB_KMDB) { 4990Sstevel@tonic-gate if (load_kmdb(bootaux) < 0) 5000Sstevel@tonic-gate goto fail; 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * Post setup. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate #ifdef MPSAS 5070Sstevel@tonic-gate sas_prisyms(kobj_lm_lookup(KOBJ_LM_PRIMARY)); 5080Sstevel@tonic-gate #endif 5090Sstevel@tonic-gate s_text = _text; 5100Sstevel@tonic-gate e_text = _etext; 5110Sstevel@tonic-gate s_data = _data; 5120Sstevel@tonic-gate e_data = _edata; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate kobj_sync_instruction_memory(s_text, e_text - s_text); 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate #ifdef KOBJ_DEBUG 5170Sstevel@tonic-gate if (kobj_debug & D_DEBUG) 5180Sstevel@tonic-gate _kobj_printf(ops, 5190Sstevel@tonic-gate "krtld: transferring control to: 0x%p\n", entry); 5200Sstevel@tonic-gate #endif 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * Make sure the mod system knows about the modules already loaded. 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate last_module_id = kobj_last_module_id; 5260Sstevel@tonic-gate bcopy(kobj_modules, &modules, sizeof (modules)); 5270Sstevel@tonic-gate modp = &modules; 5280Sstevel@tonic-gate do { 5290Sstevel@tonic-gate if (modp->mod_next == kobj_modules) 5300Sstevel@tonic-gate modp->mod_next = &modules; 5310Sstevel@tonic-gate if (modp->mod_prev == kobj_modules) 5320Sstevel@tonic-gate modp->mod_prev = &modules; 5330Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate standalone = 0; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate #ifdef __sparc 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * On sparcv9, boot scratch memory is running out. 5400Sstevel@tonic-gate * Free the temporary allocations here to allow boot 5410Sstevel@tonic-gate * to continue. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate kobj_tmp_free(); 5440Sstevel@tonic-gate #endif 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate _kobj_printf = kprintf; 5470Sstevel@tonic-gate exitto((caddr_t)entry); 5480Sstevel@tonic-gate fail: 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate _kobj_printf(ops, "krtld: error during initial load/link phase\n"); 551*4159Sjosephb 552*4159Sjosephb #if !defined(_UNIX_KRTLD) 553*4159Sjosephb _kobj_printf(ops, "\n"); 554*4159Sjosephb _kobj_printf(ops, "krtld could neither locate nor resolve symbols" 555*4159Sjosephb " for:\n"); 556*4159Sjosephb _kobj_printf(ops, " %s\n", filename); 557*4159Sjosephb _kobj_printf(ops, "in the boot archive. Please verify that this" 558*4159Sjosephb " file\n"); 559*4159Sjosephb _kobj_printf(ops, "matches what is found in the boot archive.\n"); 560*4159Sjosephb _kobj_printf(ops, "You may need to boot using the Solaris failsafe to" 561*4159Sjosephb " fix this.\n"); 562*4159Sjosephb bop_panic("Unable to boot"); 563*4159Sjosephb #endif 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5663446Smrj #if !defined(_UNIX_KRTLD) 5673446Smrj /* 5683446Smrj * Synthesize additional metadata that describes the executable. 5693446Smrj * 5703446Smrj * (When the dynamic executable has an interpreter, the boot program 5713446Smrj * does all this for us. Where we don't have an interpreter, (or a 5723446Smrj * even a boot program, perhaps) we have to do this for ourselves.) 5733446Smrj */ 5743446Smrj static void 5753446Smrj synthetic_bootaux(char *filename, val_t *bootaux) 5763446Smrj { 5773446Smrj Ehdr ehdr; 5783446Smrj caddr_t phdrbase; 5793446Smrj struct _buf *file; 5803446Smrj int i, n; 5813446Smrj 5823446Smrj /* 5833446Smrj * Elf header 5843446Smrj */ 5853446Smrj KOBJ_MARK("synthetic_bootaux()"); 5863446Smrj KOBJ_MARK(filename); 5873446Smrj file = kobj_open_file(filename); 5883446Smrj if (file == (struct _buf *)-1) { 5893446Smrj _kobj_printf(ops, "krtld: failed to open '%s'\n", filename); 5903446Smrj return; 5913446Smrj } 5923446Smrj KOBJ_MARK("reading program headers"); 5933446Smrj if (kobj_read_file(file, (char *)&ehdr, sizeof (ehdr), 0) < 0) { 5943446Smrj _kobj_printf(ops, "krtld: %s: failed to read ehder\n", 5953446Smrj filename); 5963446Smrj return; 5973446Smrj } 5983446Smrj 5993446Smrj /* 6003446Smrj * Program headers 6013446Smrj */ 6023446Smrj bootaux[BA_PHNUM].ba_val = ehdr.e_phnum; 6033446Smrj bootaux[BA_PHENT].ba_val = ehdr.e_phentsize; 6043446Smrj n = ehdr.e_phentsize * ehdr.e_phnum; 6053446Smrj 6063446Smrj phdrbase = kobj_alloc(n, KM_WAIT | KM_TMP); 6073446Smrj 6083446Smrj if (kobj_read_file(file, phdrbase, n, ehdr.e_phoff) < 0) { 6093446Smrj _kobj_printf(ops, "krtld: %s: failed to read phdrs\n", 6103446Smrj filename); 6113446Smrj return; 6123446Smrj } 6133446Smrj bootaux[BA_PHDR].ba_ptr = phdrbase; 6143446Smrj kobj_close_file(file); 6153446Smrj KOBJ_MARK("closed file"); 6163446Smrj 6173446Smrj /* 6183446Smrj * Find the dynamic section address 6193446Smrj */ 6203446Smrj for (i = 0; i < ehdr.e_phnum; i++) { 6213446Smrj Phdr *phdr = (Phdr *)(phdrbase + ehdr.e_phentsize * i); 6223446Smrj 6233446Smrj if (phdr->p_type == PT_DYNAMIC) { 6243446Smrj bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr; 6253446Smrj break; 6263446Smrj } 6273446Smrj } 6283446Smrj KOBJ_MARK("synthetic_bootaux() done"); 6293446Smrj } 6303446Smrj #endif 6313446Smrj 6320Sstevel@tonic-gate /* 6330Sstevel@tonic-gate * Set up any global information derived 6340Sstevel@tonic-gate * from attribute/values in the boot or 6350Sstevel@tonic-gate * aux vector. 6360Sstevel@tonic-gate */ 6370Sstevel@tonic-gate static void 6380Sstevel@tonic-gate attr_val(val_t *bootaux) 6390Sstevel@tonic-gate { 6400Sstevel@tonic-gate Phdr *phdr; 6410Sstevel@tonic-gate int phnum, phsize; 6420Sstevel@tonic-gate int i; 6430Sstevel@tonic-gate 6443446Smrj KOBJ_MARK("attr_val()"); 6450Sstevel@tonic-gate kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val; 6460Sstevel@tonic-gate lg_pagesize = bootaux[BA_LPAGESZ].ba_val; 6470Sstevel@tonic-gate use_iflush = bootaux[BA_IFLUSH].ba_val; 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr; 6500Sstevel@tonic-gate phnum = bootaux[BA_PHNUM].ba_val; 6510Sstevel@tonic-gate phsize = bootaux[BA_PHENT].ba_val; 6520Sstevel@tonic-gate for (i = 0; i < phnum; i++) { 6530Sstevel@tonic-gate phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize); 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate if (phdr->p_type != PT_LOAD) 6560Sstevel@tonic-gate continue; 6570Sstevel@tonic-gate /* 6580Sstevel@tonic-gate * Bounds of the various segments. 6590Sstevel@tonic-gate */ 6600Sstevel@tonic-gate if (!(phdr->p_flags & PF_X)) { 6613446Smrj #if defined(_UNIX_KRTLD) 6620Sstevel@tonic-gate dynseg = phdr->p_vaddr; 6633446Smrj #else 6643446Smrj ASSERT(phdr->p_vaddr == 0); 6653446Smrj #endif 6660Sstevel@tonic-gate } else { 6670Sstevel@tonic-gate if (phdr->p_flags & PF_W) { 6683446Smrj _data = (caddr_t)phdr->p_vaddr; 6693446Smrj _edata = _data + phdr->p_memsz; 6700Sstevel@tonic-gate } else { 6710Sstevel@tonic-gate _text = (caddr_t)phdr->p_vaddr; 6720Sstevel@tonic-gate _etext = _text + phdr->p_memsz; 6730Sstevel@tonic-gate } 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* To do the kobj_alloc, _edata needs to be set. */ 6780Sstevel@tonic-gate for (i = 0; i < NLIBMACROS; i++) { 6790Sstevel@tonic-gate if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) { 6800Sstevel@tonic-gate libmacros[i].lmi_list = kobj_alloc( 6810Sstevel@tonic-gate strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) + 6820Sstevel@tonic-gate 1, KM_WAIT); 6830Sstevel@tonic-gate (void) strcpy(libmacros[i].lmi_list, 6843912Slling bootaux[libmacros[i].lmi_ba_index].ba_ptr); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname); 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate /* 6910Sstevel@tonic-gate * Set up the booted executable. 6920Sstevel@tonic-gate */ 6930Sstevel@tonic-gate static struct module * 6943446Smrj load_exec(val_t *bootaux, char *filename) 6950Sstevel@tonic-gate { 6960Sstevel@tonic-gate struct modctl *cp; 6970Sstevel@tonic-gate struct module *mp; 6980Sstevel@tonic-gate Dyn *dyn; 6990Sstevel@tonic-gate Sym *sp; 7000Sstevel@tonic-gate int i, lsize, osize, nsize, allocsize; 7010Sstevel@tonic-gate char *libname, *tmp; 7020Sstevel@tonic-gate 7033446Smrj /* 7043446Smrj * Set the module search path. 7053446Smrj */ 7063446Smrj kobj_module_path = getmodpath(filename); 7073446Smrj 7083446Smrj #ifdef KOBJ_DEBUG 7093446Smrj if (kobj_debug & D_DEBUG) 7103446Smrj _kobj_printf(ops, "module path '%s'\n", kobj_module_path); 7113446Smrj #endif 7123446Smrj 7133446Smrj KOBJ_MARK("add_primary"); 7140Sstevel@tonic-gate cp = add_primary(filename, KOBJ_LM_PRIMARY); 7150Sstevel@tonic-gate 7163446Smrj KOBJ_MARK("struct module"); 7170Sstevel@tonic-gate mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 7180Sstevel@tonic-gate cp->mod_mp = mp; 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate /* 7210Sstevel@tonic-gate * We don't have the following information 7220Sstevel@tonic-gate * since this module is an executable and not 7230Sstevel@tonic-gate * a relocatable .o. 7240Sstevel@tonic-gate */ 7250Sstevel@tonic-gate mp->symtbl_section = 0; 7260Sstevel@tonic-gate mp->shdrs = NULL; 7270Sstevel@tonic-gate mp->strhdr = NULL; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate /* 7300Sstevel@tonic-gate * Since this module is the only exception, 7310Sstevel@tonic-gate * we cons up some section headers. 7320Sstevel@tonic-gate */ 7333446Smrj KOBJ_MARK("symhdr"); 7340Sstevel@tonic-gate mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 7353446Smrj 7363446Smrj KOBJ_MARK("strhdr"); 7370Sstevel@tonic-gate mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate mp->symhdr->sh_type = SHT_SYMTAB; 7400Sstevel@tonic-gate mp->strhdr->sh_type = SHT_STRTAB; 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * Scan the dynamic structure. 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 7450Sstevel@tonic-gate dyn->d_tag != DT_NULL; dyn++) { 7460Sstevel@tonic-gate switch (dyn->d_tag) { 7470Sstevel@tonic-gate case DT_SYMTAB: 7480Sstevel@tonic-gate dyn->d_un.d_ptr += dynseg; 7490Sstevel@tonic-gate mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr; 7500Sstevel@tonic-gate mp->symhdr->sh_addr = dyn->d_un.d_ptr; 7510Sstevel@tonic-gate break; 7520Sstevel@tonic-gate case DT_HASH: 7530Sstevel@tonic-gate dyn->d_un.d_ptr += dynseg; 7540Sstevel@tonic-gate mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1); 7550Sstevel@tonic-gate mp->hashsize = *(uint_t *)dyn->d_un.d_ptr; 7560Sstevel@tonic-gate break; 7570Sstevel@tonic-gate case DT_STRTAB: 7580Sstevel@tonic-gate dyn->d_un.d_ptr += dynseg; 7590Sstevel@tonic-gate mp->strings = (char *)dyn->d_un.d_ptr; 7600Sstevel@tonic-gate mp->strhdr->sh_addr = dyn->d_un.d_ptr; 7610Sstevel@tonic-gate break; 7620Sstevel@tonic-gate case DT_STRSZ: 7630Sstevel@tonic-gate mp->strhdr->sh_size = dyn->d_un.d_val; 7640Sstevel@tonic-gate break; 7650Sstevel@tonic-gate case DT_SYMENT: 7660Sstevel@tonic-gate mp->symhdr->sh_entsize = dyn->d_un.d_val; 7670Sstevel@tonic-gate break; 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * Collapse any DT_NEEDED entries into one string. 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate nsize = osize = 0; 7750Sstevel@tonic-gate allocsize = MAXPATHLEN; 7760Sstevel@tonic-gate 7773446Smrj KOBJ_MARK("depends_on"); 7780Sstevel@tonic-gate mp->depends_on = kobj_alloc(allocsize, KM_WAIT); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 7810Sstevel@tonic-gate dyn->d_tag != DT_NULL; dyn++) 7820Sstevel@tonic-gate if (dyn->d_tag == DT_NEEDED) { 7830Sstevel@tonic-gate char *_lib; 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate libname = mp->strings + dyn->d_un.d_val; 7860Sstevel@tonic-gate if (strchr(libname, '$') != NULL) { 7870Sstevel@tonic-gate if ((_lib = expand_libmacro(libname, 7880Sstevel@tonic-gate filename, filename)) != NULL) 7890Sstevel@tonic-gate libname = _lib; 7900Sstevel@tonic-gate else 7910Sstevel@tonic-gate _kobj_printf(ops, "krtld: " 7920Sstevel@tonic-gate "load_exec: fail to " 7930Sstevel@tonic-gate "expand %s\n", libname); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate lsize = strlen(libname); 7960Sstevel@tonic-gate nsize += lsize; 7970Sstevel@tonic-gate if (nsize + 1 > allocsize) { 7983446Smrj KOBJ_MARK("grow depends_on"); 7990Sstevel@tonic-gate tmp = kobj_alloc(allocsize + MAXPATHLEN, 8000Sstevel@tonic-gate KM_WAIT); 8010Sstevel@tonic-gate bcopy(mp->depends_on, tmp, osize); 8020Sstevel@tonic-gate kobj_free(mp->depends_on, allocsize); 8030Sstevel@tonic-gate mp->depends_on = tmp; 8040Sstevel@tonic-gate allocsize += MAXPATHLEN; 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate bcopy(libname, mp->depends_on + osize, lsize); 8070Sstevel@tonic-gate *(mp->depends_on + nsize) = ' '; /* seperate */ 8080Sstevel@tonic-gate nsize++; 8090Sstevel@tonic-gate osize = nsize; 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate if (nsize) { 8120Sstevel@tonic-gate mp->depends_on[nsize - 1] = '\0'; /* terminate the string */ 8130Sstevel@tonic-gate /* 8140Sstevel@tonic-gate * alloc with exact size and copy whatever it got over 8150Sstevel@tonic-gate */ 8163446Smrj KOBJ_MARK("realloc depends_on"); 8170Sstevel@tonic-gate tmp = kobj_alloc(nsize, KM_WAIT); 8180Sstevel@tonic-gate bcopy(mp->depends_on, tmp, nsize); 8190Sstevel@tonic-gate kobj_free(mp->depends_on, allocsize); 8200Sstevel@tonic-gate mp->depends_on = tmp; 8210Sstevel@tonic-gate } else { 8220Sstevel@tonic-gate kobj_free(mp->depends_on, allocsize); 8230Sstevel@tonic-gate mp->depends_on = NULL; 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */ 8270Sstevel@tonic-gate mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize; 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * We allocate our own table since we don't 8300Sstevel@tonic-gate * hash undefined references. 8310Sstevel@tonic-gate */ 8323446Smrj KOBJ_MARK("chains"); 8330Sstevel@tonic-gate mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 8343446Smrj KOBJ_MARK("buckets"); 8350Sstevel@tonic-gate mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate mp->text = _text; 8380Sstevel@tonic-gate mp->data = _data; 8393446Smrj 8403446Smrj mp->text_size = _etext - _text; 8413446Smrj mp->data_size = _edata - _data; 8423446Smrj 8430Sstevel@tonic-gate cp->mod_text = mp->text; 8440Sstevel@tonic-gate cp->mod_text_size = mp->text_size; 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate mp->filename = cp->mod_filename; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate #ifdef KOBJ_DEBUG 8490Sstevel@tonic-gate if (kobj_debug & D_LOADING) { 8500Sstevel@tonic-gate _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 8510Sstevel@tonic-gate _kobj_printf(ops, "\ttext: 0x%p", mp->text); 8520Sstevel@tonic-gate _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 8530Sstevel@tonic-gate _kobj_printf(ops, "\tdata: 0x%p", mp->data); 8540Sstevel@tonic-gate _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* 8590Sstevel@tonic-gate * Insert symbols into the hash table. 8600Sstevel@tonic-gate */ 8610Sstevel@tonic-gate for (i = 0; i < mp->nsyms; i++) { 8620Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 8650Sstevel@tonic-gate continue; 8660Sstevel@tonic-gate #ifdef __sparc 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * Register symbols are ignored in the kernel 8690Sstevel@tonic-gate */ 8700Sstevel@tonic-gate if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) 8710Sstevel@tonic-gate continue; 8720Sstevel@tonic-gate #endif /* __sparc */ 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate sym_insert(mp, mp->strings + sp->st_name, i); 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate 8773446Smrj KOBJ_MARK("load_exec done"); 8780Sstevel@tonic-gate return (mp); 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate /* 8823446Smrj * Set up the linker module (if it's compiled in, LDNAME is NULL) 8830Sstevel@tonic-gate */ 8840Sstevel@tonic-gate static void 8850Sstevel@tonic-gate load_linker(val_t *bootaux) 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate struct module *kmp = (struct module *)kobj_modules->mod_mp; 8880Sstevel@tonic-gate struct module *mp; 8890Sstevel@tonic-gate struct modctl *cp; 8900Sstevel@tonic-gate int i; 8910Sstevel@tonic-gate Shdr *shp; 8920Sstevel@tonic-gate Sym *sp; 8930Sstevel@tonic-gate int shsize; 8940Sstevel@tonic-gate char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr; 8950Sstevel@tonic-gate 8963446Smrj /* 8973446Smrj * On some architectures, krtld is compiled into the kernel. 8983446Smrj */ 8993446Smrj if (dlname == NULL) 9003446Smrj return; 9013446Smrj 9020Sstevel@tonic-gate cp = add_primary(dlname, KOBJ_LM_PRIMARY); 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate cp->mod_mp = mp; 9070Sstevel@tonic-gate mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr; 9080Sstevel@tonic-gate shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum; 9090Sstevel@tonic-gate mp->shdrs = kobj_alloc(shsize, KM_WAIT); 9100Sstevel@tonic-gate bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize); 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate for (i = 1; i < (int)mp->hdr.e_shnum; i++) { 9130Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize)); 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (shp->sh_flags & SHF_ALLOC) { 9160Sstevel@tonic-gate if (shp->sh_flags & SHF_WRITE) { 9170Sstevel@tonic-gate if (mp->data == NULL) 9180Sstevel@tonic-gate mp->data = (char *)shp->sh_addr; 9190Sstevel@tonic-gate } else if (mp->text == NULL) { 9200Sstevel@tonic-gate mp->text = (char *)shp->sh_addr; 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate if (shp->sh_type == SHT_SYMTAB) { 9240Sstevel@tonic-gate mp->symtbl_section = i; 9250Sstevel@tonic-gate mp->symhdr = shp; 9260Sstevel@tonic-gate mp->symspace = mp->symtbl = (char *)shp->sh_addr; 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 9300Sstevel@tonic-gate mp->flags = KOBJ_INTERP|KOBJ_PRIM; 9310Sstevel@tonic-gate mp->strhdr = (Shdr *) 9323912Slling (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 9330Sstevel@tonic-gate mp->strings = (char *)mp->strhdr->sh_addr; 9340Sstevel@tonic-gate mp->hashsize = kobj_gethashsize(mp->nsyms); 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) + 9373912Slling (mp->hashsize + mp->nsyms) * sizeof (symid_t); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 9400Sstevel@tonic-gate mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate mp->bss = bootaux[BA_BSS].ba_val; 9430Sstevel@tonic-gate mp->bss_align = 0; /* pre-aligned during allocation */ 9440Sstevel@tonic-gate mp->bss_size = (uintptr_t)_edata - mp->bss; 9450Sstevel@tonic-gate mp->text_size = _etext - mp->text; 9460Sstevel@tonic-gate mp->data_size = _edata - mp->data; 9470Sstevel@tonic-gate mp->filename = cp->mod_filename; 9480Sstevel@tonic-gate cp->mod_text = mp->text; 9490Sstevel@tonic-gate cp->mod_text_size = mp->text_size; 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * Now that we've figured out where the linker is, 9530Sstevel@tonic-gate * set the limits for the booted object. 9540Sstevel@tonic-gate */ 9550Sstevel@tonic-gate kmp->text_size = (size_t)(mp->text - kmp->text); 9560Sstevel@tonic-gate kmp->data_size = (size_t)(mp->data - kmp->data); 9570Sstevel@tonic-gate kobj_modules->mod_text_size = kmp->text_size; 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate #ifdef KOBJ_DEBUG 9600Sstevel@tonic-gate if (kobj_debug & D_LOADING) { 9610Sstevel@tonic-gate _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 9620Sstevel@tonic-gate _kobj_printf(ops, "\ttext:0x%p", mp->text); 9630Sstevel@tonic-gate _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 9640Sstevel@tonic-gate _kobj_printf(ops, "\tdata:0x%p", mp->data); 9650Sstevel@tonic-gate _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate /* 9700Sstevel@tonic-gate * Insert the symbols into the hash table. 9710Sstevel@tonic-gate */ 9720Sstevel@tonic-gate for (i = 0; i < mp->nsyms; i++) { 9730Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 9760Sstevel@tonic-gate continue; 9770Sstevel@tonic-gate if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 9780Sstevel@tonic-gate if (sp->st_shndx == SHN_COMMON) 9790Sstevel@tonic-gate sp->st_shndx = SHN_ABS; 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate sym_insert(mp, mp->strings + sp->st_name, i); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate static kobj_notify_list_t ** 9870Sstevel@tonic-gate kobj_notify_lookup(uint_t type) 9880Sstevel@tonic-gate { 9890Sstevel@tonic-gate ASSERT(type != 0 && type < sizeof (kobj_notifiers) / 9900Sstevel@tonic-gate sizeof (kobj_notify_list_t *)); 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate return (&kobj_notifiers[type]); 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate int 9960Sstevel@tonic-gate kobj_notify_add(kobj_notify_list_t *knp) 9970Sstevel@tonic-gate { 9980Sstevel@tonic-gate kobj_notify_list_t **knl; 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate knl = kobj_notify_lookup(knp->kn_type); 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate knp->kn_next = NULL; 10030Sstevel@tonic-gate knp->kn_prev = NULL; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate mutex_enter(&kobj_lock); 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (*knl != NULL) { 10080Sstevel@tonic-gate (*knl)->kn_prev = knp; 10090Sstevel@tonic-gate knp->kn_next = *knl; 10100Sstevel@tonic-gate } 10110Sstevel@tonic-gate (*knl) = knp; 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate mutex_exit(&kobj_lock); 10140Sstevel@tonic-gate return (0); 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate int 10180Sstevel@tonic-gate kobj_notify_remove(kobj_notify_list_t *knp) 10190Sstevel@tonic-gate { 10200Sstevel@tonic-gate kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type); 10210Sstevel@tonic-gate kobj_notify_list_t *tknp; 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate mutex_enter(&kobj_lock); 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate /* LINTED */ 10260Sstevel@tonic-gate if (tknp = knp->kn_next) 10270Sstevel@tonic-gate tknp->kn_prev = knp->kn_prev; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate /* LINTED */ 10300Sstevel@tonic-gate if (tknp = knp->kn_prev) 10310Sstevel@tonic-gate tknp->kn_next = knp->kn_next; 10320Sstevel@tonic-gate else 10330Sstevel@tonic-gate *knl = knp->kn_next; 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate mutex_exit(&kobj_lock); 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate return (0); 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate /* 10410Sstevel@tonic-gate * Notify all interested callbacks of a specified change in module state. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate static void 10440Sstevel@tonic-gate kobj_notify(int type, struct modctl *modp) 10450Sstevel@tonic-gate { 10460Sstevel@tonic-gate kobj_notify_list_t *knp; 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate if (modp->mod_loadflags & MOD_NONOTIFY || standalone) 10490Sstevel@tonic-gate return; 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate mutex_enter(&kobj_lock); 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next) 10540Sstevel@tonic-gate knp->kn_func(type, modp); 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate /* 10570Sstevel@tonic-gate * KDI notification must be last (it has to allow for work done by the 10580Sstevel@tonic-gate * other notification callbacks), so we call it manually. 10590Sstevel@tonic-gate */ 10600Sstevel@tonic-gate kobj_kdi_mod_notify(type, modp); 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate mutex_exit(&kobj_lock); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * Ask boot for the module path. 10670Sstevel@tonic-gate */ 10683446Smrj /*ARGSUSED*/ 10690Sstevel@tonic-gate static char * 10703446Smrj getmodpath(const char *filename) 10710Sstevel@tonic-gate { 10720Sstevel@tonic-gate char *path; 10730Sstevel@tonic-gate int len; 10740Sstevel@tonic-gate 10753446Smrj #if defined(_UNIX_KRTLD) 10763446Smrj /* 10773446Smrj * The boot program provides the module name when it detects 10783446Smrj * that the executable has an interpreter, thus we can ask 10793446Smrj * it directly in this case. 10803446Smrj */ 10810Sstevel@tonic-gate if ((len = BOP_GETPROPLEN(ops, MODPATH_PROPNAME)) == -1) 10820Sstevel@tonic-gate return (MOD_DEFPATH); 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate path = kobj_zalloc(len, KM_WAIT); 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate (void) BOP_GETPROP(ops, MODPATH_PROPNAME, path); 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate return (*path ? path : MOD_DEFPATH); 10893446Smrj 10903446Smrj #else 10913446Smrj 10923446Smrj /* 10933446Smrj * Construct the directory path from the filename. 10943446Smrj */ 10953446Smrj 10963446Smrj char *p; 10973446Smrj const char isastr[] = "/amd64"; 10983446Smrj size_t isalen = strlen(isastr); 10993446Smrj 11003446Smrj if ((p = strrchr(filename, '/')) == NULL) 11013446Smrj return (MOD_DEFPATH); 11023446Smrj 11033446Smrj while (p > filename && *(p - 1) == '/') 11043446Smrj p--; /* remove trailing '/' characters */ 11053446Smrj if (p == filename) 11063446Smrj p++; /* so "/" -is- the modpath in this case */ 11073446Smrj 11083446Smrj /* 11093446Smrj * Remove optional isa-dependent directory name - the module 11103446Smrj * subsystem will put this back again (!) 11113446Smrj */ 11123446Smrj len = p - filename; 11133446Smrj if (len > isalen && 11143446Smrj strncmp(&filename[len - isalen], isastr, isalen) == 0) 11153446Smrj p -= isalen; 11163446Smrj 11173446Smrj /* 11183446Smrj * "/platform/mumblefrotz" + " " + MOD_DEFPATH 11193446Smrj */ 11203446Smrj len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1; 11213446Smrj 11223446Smrj path = kobj_zalloc(len, KM_WAIT); 11233446Smrj (void) strncpy(path, filename, p - filename); 11243446Smrj (void) strcat(path, " "); 11253446Smrj return (strcat(path, MOD_DEFPATH)); 11263446Smrj #endif 11270Sstevel@tonic-gate } 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate static struct modctl * 11303446Smrj add_primary(const char *filename, int lmid) 11310Sstevel@tonic-gate { 11320Sstevel@tonic-gate struct modctl *cp; 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT); 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate /* 11390Sstevel@tonic-gate * For symbol lookup, we assemble our own 11400Sstevel@tonic-gate * modctl list of the primary modules. 11410Sstevel@tonic-gate */ 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate (void) strcpy(cp->mod_filename, filename); 11440Sstevel@tonic-gate cp->mod_modname = basename(cp->mod_filename); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* set values for modinfo assuming that the load will work */ 11470Sstevel@tonic-gate cp->mod_prim = 1; 11480Sstevel@tonic-gate cp->mod_loaded = 1; 11490Sstevel@tonic-gate cp->mod_installed = 1; 11500Sstevel@tonic-gate cp->mod_loadcnt = 1; 11510Sstevel@tonic-gate cp->mod_loadflags = MOD_NOAUTOUNLOAD; 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate cp->mod_id = kobj_last_module_id++; 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate /* 11560Sstevel@tonic-gate * Link the module in. We'll pass this info on 11570Sstevel@tonic-gate * to the mod squad later. 11580Sstevel@tonic-gate */ 11590Sstevel@tonic-gate if (kobj_modules == NULL) { 11600Sstevel@tonic-gate kobj_modules = cp; 11610Sstevel@tonic-gate cp->mod_prev = cp->mod_next = cp; 11620Sstevel@tonic-gate } else { 11630Sstevel@tonic-gate cp->mod_prev = kobj_modules->mod_prev; 11640Sstevel@tonic-gate cp->mod_next = kobj_modules; 11650Sstevel@tonic-gate kobj_modules->mod_prev->mod_next = cp; 11660Sstevel@tonic-gate kobj_modules->mod_prev = cp; 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate kobj_lm_append(lmid, cp); 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate return (cp); 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate static int 11750Sstevel@tonic-gate bind_primary(val_t *bootaux, int lmid) 11760Sstevel@tonic-gate { 11770Sstevel@tonic-gate struct modctl_list *linkmap = kobj_lm_lookup(lmid); 11780Sstevel@tonic-gate struct modctl_list *lp; 11790Sstevel@tonic-gate struct module *mp; 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate /* 11820Sstevel@tonic-gate * Do common symbols. 11830Sstevel@tonic-gate */ 11840Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 11850Sstevel@tonic-gate mp = mod(lp); 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate /* 11880Sstevel@tonic-gate * Don't do common section relocations for modules that 11890Sstevel@tonic-gate * don't need it. 11900Sstevel@tonic-gate */ 11910Sstevel@tonic-gate if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP)) 11920Sstevel@tonic-gate continue; 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate if (do_common(mp) < 0) 11950Sstevel@tonic-gate return (-1); 11960Sstevel@tonic-gate } 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate /* 11990Sstevel@tonic-gate * Resolve symbols. 12000Sstevel@tonic-gate */ 12010Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 12020Sstevel@tonic-gate mp = mod(lp); 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate if (do_symbols(mp, 0) < 0) 12050Sstevel@tonic-gate return (-1); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * Do relocations. 12100Sstevel@tonic-gate */ 12110Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 12120Sstevel@tonic-gate mp = mod(lp); 12130Sstevel@tonic-gate 12140Sstevel@tonic-gate if (mp->flags & KOBJ_EXEC) { 12153446Smrj Dyn *dyn; 12163446Smrj Word relasz = 0, relaent = 0; 12173446Smrj Word shtype; 12183446Smrj char *rela = NULL; 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr; 12210Sstevel@tonic-gate dyn->d_tag != DT_NULL; dyn++) { 12220Sstevel@tonic-gate switch (dyn->d_tag) { 12230Sstevel@tonic-gate case DT_RELASZ: 12240Sstevel@tonic-gate case DT_RELSZ: 12250Sstevel@tonic-gate relasz = dyn->d_un.d_val; 12260Sstevel@tonic-gate break; 12270Sstevel@tonic-gate case DT_RELAENT: 12280Sstevel@tonic-gate case DT_RELENT: 12290Sstevel@tonic-gate relaent = dyn->d_un.d_val; 12300Sstevel@tonic-gate break; 12310Sstevel@tonic-gate case DT_RELA: 12320Sstevel@tonic-gate shtype = SHT_RELA; 12330Sstevel@tonic-gate rela = (char *)(dyn->d_un.d_ptr + 12343912Slling dynseg); 12350Sstevel@tonic-gate break; 12360Sstevel@tonic-gate case DT_REL: 12370Sstevel@tonic-gate shtype = SHT_REL; 12380Sstevel@tonic-gate rela = (char *)(dyn->d_un.d_ptr + 12393912Slling dynseg); 12400Sstevel@tonic-gate break; 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate if (relasz == 0 || 12440Sstevel@tonic-gate relaent == 0 || rela == NULL) { 12450Sstevel@tonic-gate _kobj_printf(ops, "krtld: bind_primary(): " 12460Sstevel@tonic-gate "no relocation information found for " 12470Sstevel@tonic-gate "module %s\n", mp->filename); 12480Sstevel@tonic-gate return (-1); 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate #ifdef KOBJ_DEBUG 12510Sstevel@tonic-gate if (kobj_debug & D_RELOCATIONS) 12520Sstevel@tonic-gate _kobj_printf(ops, "krtld: relocating: file=%s " 12530Sstevel@tonic-gate "KOBJ_EXEC\n", mp->filename); 12540Sstevel@tonic-gate #endif 12550Sstevel@tonic-gate if (do_relocate(mp, rela, shtype, relasz/relaent, 12560Sstevel@tonic-gate relaent, (Addr)mp->text) < 0) 12570Sstevel@tonic-gate return (-1); 12580Sstevel@tonic-gate } else { 12590Sstevel@tonic-gate if (do_relocations(mp) < 0) 12600Sstevel@tonic-gate return (-1); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate kobj_sync_instruction_memory(mp->text, mp->text_size); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 12670Sstevel@tonic-gate mp = mod(lp); 12680Sstevel@tonic-gate 12690Sstevel@tonic-gate /* 12700Sstevel@tonic-gate * We need to re-read the full symbol table for the boot file, 12710Sstevel@tonic-gate * since we couldn't use the full one before. We also need to 12720Sstevel@tonic-gate * load the CTF sections of both the boot file and the 12730Sstevel@tonic-gate * interpreter (us). 12740Sstevel@tonic-gate */ 12750Sstevel@tonic-gate if (mp->flags & KOBJ_EXEC) { 12760Sstevel@tonic-gate struct _buf *file; 12770Sstevel@tonic-gate int n; 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate file = kobj_open_file(mp->filename); 12800Sstevel@tonic-gate if (file == (struct _buf *)-1) 12810Sstevel@tonic-gate return (-1); 12820Sstevel@tonic-gate if (kobj_read_file(file, (char *)&mp->hdr, 12830Sstevel@tonic-gate sizeof (mp->hdr), 0) < 0) 12840Sstevel@tonic-gate return (-1); 12850Sstevel@tonic-gate n = mp->hdr.e_shentsize * mp->hdr.e_shnum; 12860Sstevel@tonic-gate mp->shdrs = kobj_alloc(n, KM_WAIT); 12870Sstevel@tonic-gate if (kobj_read_file(file, mp->shdrs, n, 12880Sstevel@tonic-gate mp->hdr.e_shoff) < 0) 12890Sstevel@tonic-gate return (-1); 12900Sstevel@tonic-gate if (get_syms(mp, file) < 0) 12910Sstevel@tonic-gate return (-1); 12920Sstevel@tonic-gate if (get_ctf(mp, file) < 0) 12930Sstevel@tonic-gate return (-1); 12940Sstevel@tonic-gate kobj_close_file(file); 12950Sstevel@tonic-gate mp->flags |= KOBJ_RELOCATED; 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate } else if (mp->flags & KOBJ_INTERP) { 12980Sstevel@tonic-gate struct _buf *file; 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate /* 13010Sstevel@tonic-gate * The interpreter path fragment in mp->filename 13020Sstevel@tonic-gate * will already have the module directory suffix 13030Sstevel@tonic-gate * in it (if appropriate). 13040Sstevel@tonic-gate */ 13050Sstevel@tonic-gate file = kobj_open_path(mp->filename, 1, 0); 13060Sstevel@tonic-gate if (file == (struct _buf *)-1) 13070Sstevel@tonic-gate return (-1); 13080Sstevel@tonic-gate if (get_ctf(mp, file) < 0) 13090Sstevel@tonic-gate return (-1); 13100Sstevel@tonic-gate kobj_close_file(file); 13110Sstevel@tonic-gate mp->flags |= KOBJ_RELOCATED; 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate } 13140Sstevel@tonic-gate 13150Sstevel@tonic-gate return (0); 13160Sstevel@tonic-gate } 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate static struct modctl * 13190Sstevel@tonic-gate mod_already_loaded(char *modname) 13200Sstevel@tonic-gate { 13210Sstevel@tonic-gate struct modctl *mctl = kobj_modules; 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate do { 13240Sstevel@tonic-gate if (strcmp(modname, mctl->mod_filename) == 0) 13250Sstevel@tonic-gate return (mctl); 13260Sstevel@tonic-gate mctl = mctl->mod_next; 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate } while (mctl != kobj_modules); 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate return (NULL); 13310Sstevel@tonic-gate } 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate /* 13340Sstevel@tonic-gate * Load all the primary dependent modules. 13350Sstevel@tonic-gate */ 13360Sstevel@tonic-gate static int 13370Sstevel@tonic-gate load_primary(struct module *mp, int lmid) 13380Sstevel@tonic-gate { 13390Sstevel@tonic-gate struct modctl *cp; 13400Sstevel@tonic-gate struct module *dmp; 13410Sstevel@tonic-gate char *p, *q; 13420Sstevel@tonic-gate char modname[MODMAXNAMELEN]; 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate if ((p = mp->depends_on) == NULL) 13450Sstevel@tonic-gate return (0); 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate /* CONSTANTCONDITION */ 13480Sstevel@tonic-gate while (1) { 13490Sstevel@tonic-gate /* 13500Sstevel@tonic-gate * Skip space. 13510Sstevel@tonic-gate */ 13520Sstevel@tonic-gate while (*p && (*p == ' ' || *p == '\t')) 13530Sstevel@tonic-gate p++; 13540Sstevel@tonic-gate /* 13550Sstevel@tonic-gate * Get module name. 13560Sstevel@tonic-gate */ 13570Sstevel@tonic-gate q = modname; 13580Sstevel@tonic-gate while (*p && *p != ' ' && *p != '\t') 13590Sstevel@tonic-gate *q++ = *p++; 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate if (q == modname) 13620Sstevel@tonic-gate break; 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate *q = '\0'; 13650Sstevel@tonic-gate /* 13660Sstevel@tonic-gate * Check for dup dependencies. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate if (strcmp(modname, "dtracestubs") == 0 || 13690Sstevel@tonic-gate mod_already_loaded(modname) != NULL) 13700Sstevel@tonic-gate continue; 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate cp = add_primary(modname, lmid); 13730Sstevel@tonic-gate cp->mod_busy = 1; 13740Sstevel@tonic-gate /* 13750Sstevel@tonic-gate * Load it. 13760Sstevel@tonic-gate */ 13770Sstevel@tonic-gate (void) kobj_load_module(cp, 1); 13780Sstevel@tonic-gate cp->mod_busy = 0; 13790Sstevel@tonic-gate 13800Sstevel@tonic-gate if ((dmp = cp->mod_mp) == NULL) { 13810Sstevel@tonic-gate cp->mod_loaded = 0; 13820Sstevel@tonic-gate cp->mod_installed = 0; 13830Sstevel@tonic-gate cp->mod_loadcnt = 0; 13840Sstevel@tonic-gate return (-1); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate add_dependent(mp, dmp); 13880Sstevel@tonic-gate dmp->flags |= KOBJ_PRIM; 13890Sstevel@tonic-gate 13900Sstevel@tonic-gate /* 13910Sstevel@tonic-gate * Recurse. 13920Sstevel@tonic-gate */ 13930Sstevel@tonic-gate if (load_primary(dmp, lmid) == -1) { 13940Sstevel@tonic-gate cp->mod_loaded = 0; 13950Sstevel@tonic-gate cp->mod_installed = 0; 13960Sstevel@tonic-gate cp->mod_loadcnt = 0; 13970Sstevel@tonic-gate return (-1); 13980Sstevel@tonic-gate } 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate return (0); 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate static int 14042191Sszhou console_is_usb_serial(void) 14052191Sszhou { 14062191Sszhou char *console; 14072191Sszhou int len, ret; 14082191Sszhou 14092191Sszhou if ((len = BOP_GETPROPLEN(ops, "console")) == -1) 14102191Sszhou return (0); 14112191Sszhou 14122191Sszhou console = kobj_zalloc(len, KM_WAIT|KM_TMP); 14132191Sszhou (void) BOP_GETPROP(ops, "console", console); 14142191Sszhou ret = (strcmp(console, "usb-serial") == 0); 14152191Sszhou kobj_free(console, len); 14162191Sszhou 14172191Sszhou return (ret); 14182191Sszhou } 14192191Sszhou 14202191Sszhou static int 14210Sstevel@tonic-gate load_kmdb(val_t *bootaux) 14220Sstevel@tonic-gate { 14230Sstevel@tonic-gate struct modctl *mctl; 14240Sstevel@tonic-gate struct module *mp; 14250Sstevel@tonic-gate Sym *sym; 14260Sstevel@tonic-gate 14272191Sszhou if (console_is_usb_serial()) { 14282191Sszhou _kobj_printf(ops, "kmdb not loaded " 14292191Sszhou "(unsupported on usb serial console)\n"); 14302191Sszhou return (0); 14312191Sszhou } 14322191Sszhou 14330Sstevel@tonic-gate _kobj_printf(ops, "Loading kmdb...\n"); 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL) 14360Sstevel@tonic-gate return (-1); 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate mctl->mod_busy = 1; 14390Sstevel@tonic-gate (void) kobj_load_module(mctl, 1); 14400Sstevel@tonic-gate mctl->mod_busy = 0; 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate if ((mp = mctl->mod_mp) == NULL) 14430Sstevel@tonic-gate return (-1); 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate mp->flags |= KOBJ_PRIM; 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0) 14480Sstevel@tonic-gate return (-1); 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate if (boothowto & RB_VERBOSE) 14510Sstevel@tonic-gate kobj_lm_dump(KOBJ_LM_DEBUGGER); 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0) 14540Sstevel@tonic-gate return (-1); 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL) 14570Sstevel@tonic-gate return (-1); 14580Sstevel@tonic-gate 14593446Smrj #ifdef KOBJ_DEBUG 14603446Smrj if (kobj_debug & D_DEBUG) { 14613446Smrj _kobj_printf(ops, "calling kctl_boot_activate() @ 0x%lx\n", 14623446Smrj sym->st_value); 14633446Smrj _kobj_printf(ops, "\tops 0x%p\n", ops); 14643446Smrj _kobj_printf(ops, "\tromp 0x%p\n", romp); 14653446Smrj } 14663446Smrj #endif 14673446Smrj 14680Sstevel@tonic-gate if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0, 14690Sstevel@tonic-gate (const char **)kobj_kmdb_argv) < 0) 14700Sstevel@tonic-gate return (-1); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate return (0); 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate /* 14760Sstevel@tonic-gate * Return a string listing module dependencies. 14770Sstevel@tonic-gate */ 14780Sstevel@tonic-gate static char * 14790Sstevel@tonic-gate depends_on(struct module *mp) 14800Sstevel@tonic-gate { 14810Sstevel@tonic-gate Sym *sp; 14820Sstevel@tonic-gate char *depstr, *q; 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate /* 14850Sstevel@tonic-gate * The module doesn't have a depends_on value, so let's try it the 14860Sstevel@tonic-gate * old-fashioned way - via "_depends_on" 14870Sstevel@tonic-gate */ 14880Sstevel@tonic-gate if ((sp = lookup_one(mp, "_depends_on")) == NULL) 14890Sstevel@tonic-gate return (NULL); 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate q = (char *)sp->st_value; 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate /* 14940Sstevel@tonic-gate * Idiot checks. Make sure it's 14950Sstevel@tonic-gate * in-bounds and NULL terminated. 14960Sstevel@tonic-gate */ 14970Sstevel@tonic-gate if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') { 14980Sstevel@tonic-gate _kobj_printf(ops, "Error processing dependency for %s\n", 14990Sstevel@tonic-gate mp->filename); 15000Sstevel@tonic-gate return (NULL); 15010Sstevel@tonic-gate } 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT); 15040Sstevel@tonic-gate (void) strcpy(depstr, q); 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate return (depstr); 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate void 15100Sstevel@tonic-gate kobj_getmodinfo(void *xmp, struct modinfo *modinfo) 15110Sstevel@tonic-gate { 15120Sstevel@tonic-gate struct module *mp; 15130Sstevel@tonic-gate mp = (struct module *)xmp; 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate modinfo->mi_base = mp->text; 15160Sstevel@tonic-gate modinfo->mi_size = mp->text_size + mp->data_size; 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate /* 15200Sstevel@tonic-gate * kobj_export_ksyms() performs the following services: 15210Sstevel@tonic-gate * 15220Sstevel@tonic-gate * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena. 15230Sstevel@tonic-gate * (2) Removes unneeded symbols to save space. 15240Sstevel@tonic-gate * (3) Reduces memory footprint by using VM_BESTFIT allocations. 15250Sstevel@tonic-gate * (4) Makes the symbol table visible to /dev/ksyms. 15260Sstevel@tonic-gate */ 15270Sstevel@tonic-gate static void 15280Sstevel@tonic-gate kobj_export_ksyms(struct module *mp) 15290Sstevel@tonic-gate { 15300Sstevel@tonic-gate Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size); 15310Sstevel@tonic-gate Sym *sp, *osp; 15320Sstevel@tonic-gate char *name; 15330Sstevel@tonic-gate size_t namelen; 15340Sstevel@tonic-gate struct module *omp; 15350Sstevel@tonic-gate uint_t nsyms; 15360Sstevel@tonic-gate size_t symsize = mp->symhdr->sh_entsize; 15370Sstevel@tonic-gate size_t locals = 1; 15380Sstevel@tonic-gate size_t strsize; 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate /* 15410Sstevel@tonic-gate * Make a copy of the original module structure. 15420Sstevel@tonic-gate */ 15430Sstevel@tonic-gate omp = kobj_alloc(sizeof (struct module), KM_WAIT); 15440Sstevel@tonic-gate bcopy(mp, omp, sizeof (struct module)); 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate /* 15470Sstevel@tonic-gate * Compute the sizes of the new symbol table sections. 15480Sstevel@tonic-gate */ 15490Sstevel@tonic-gate for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) { 15500Sstevel@tonic-gate if (osp->st_value == 0) 15510Sstevel@tonic-gate continue; 15520Sstevel@tonic-gate if (sym_lookup(omp, osp) == NULL) 15530Sstevel@tonic-gate continue; 15540Sstevel@tonic-gate name = omp->strings + osp->st_name; 15550Sstevel@tonic-gate namelen = strlen(name); 15560Sstevel@tonic-gate if (ELF_ST_BIND(osp->st_info) == STB_LOCAL) 15570Sstevel@tonic-gate locals++; 15580Sstevel@tonic-gate nsyms++; 15590Sstevel@tonic-gate strsize += namelen + 1; 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate mp->nsyms = nsyms; 15630Sstevel@tonic-gate mp->hashsize = kobj_gethashsize(mp->nsyms); 15640Sstevel@tonic-gate 15650Sstevel@tonic-gate /* 15660Sstevel@tonic-gate * ksyms_lock must be held as writer during any operation that 15670Sstevel@tonic-gate * modifies ksyms_arena, including allocation from same, and 15680Sstevel@tonic-gate * must not be dropped until the arena is vmem_walk()able. 15690Sstevel@tonic-gate */ 15700Sstevel@tonic-gate rw_enter(&ksyms_lock, RW_WRITER); 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate /* 15730Sstevel@tonic-gate * Allocate space for the new section headers (symtab and strtab), 15740Sstevel@tonic-gate * symbol table, buckets, chains, and strings. 15750Sstevel@tonic-gate */ 15760Sstevel@tonic-gate mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) + 15770Sstevel@tonic-gate (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize; 15780Sstevel@tonic-gate 15790Sstevel@tonic-gate if (mp->flags & KOBJ_NOKSYMS) { 15800Sstevel@tonic-gate mp->symspace = kobj_alloc(mp->symsize, KM_WAIT); 15810Sstevel@tonic-gate } else { 15820Sstevel@tonic-gate mp->symspace = vmem_alloc(ksyms_arena, mp->symsize, 15830Sstevel@tonic-gate VM_BESTFIT | VM_SLEEP); 15840Sstevel@tonic-gate } 15850Sstevel@tonic-gate bzero(mp->symspace, mp->symsize); 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate /* 15880Sstevel@tonic-gate * Divvy up symspace. 15890Sstevel@tonic-gate */ 15900Sstevel@tonic-gate mp->shdrs = mp->symspace; 15910Sstevel@tonic-gate mp->symhdr = (Shdr *)mp->shdrs; 15920Sstevel@tonic-gate mp->strhdr = (Shdr *)(mp->symhdr + 1); 15930Sstevel@tonic-gate mp->symtbl = (char *)(mp->strhdr + 1); 15940Sstevel@tonic-gate mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize)); 15950Sstevel@tonic-gate mp->chains = (symid_t *)(mp->buckets + mp->hashsize); 15960Sstevel@tonic-gate mp->strings = (char *)(mp->chains + nsyms); 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate /* 15990Sstevel@tonic-gate * Fill in the new section headers (symtab and strtab). 16000Sstevel@tonic-gate */ 16010Sstevel@tonic-gate mp->hdr.e_shnum = 2; 16020Sstevel@tonic-gate mp->symtbl_section = 0; 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate mp->symhdr->sh_type = SHT_SYMTAB; 16050Sstevel@tonic-gate mp->symhdr->sh_addr = (Addr)mp->symtbl; 16060Sstevel@tonic-gate mp->symhdr->sh_size = nsyms * symsize; 16070Sstevel@tonic-gate mp->symhdr->sh_link = 1; 16080Sstevel@tonic-gate mp->symhdr->sh_info = locals; 16090Sstevel@tonic-gate mp->symhdr->sh_addralign = sizeof (Addr); 16100Sstevel@tonic-gate mp->symhdr->sh_entsize = symsize; 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate mp->strhdr->sh_type = SHT_STRTAB; 16130Sstevel@tonic-gate mp->strhdr->sh_addr = (Addr)mp->strings; 16140Sstevel@tonic-gate mp->strhdr->sh_size = strsize; 16150Sstevel@tonic-gate mp->strhdr->sh_addralign = 1; 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate /* 16180Sstevel@tonic-gate * Construct the new symbol table. 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) { 16210Sstevel@tonic-gate if (osp->st_value == 0) 16220Sstevel@tonic-gate continue; 16230Sstevel@tonic-gate if (sym_lookup(omp, osp) == NULL) 16240Sstevel@tonic-gate continue; 16250Sstevel@tonic-gate name = omp->strings + osp->st_name; 16260Sstevel@tonic-gate namelen = strlen(name); 16270Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + symsize * nsyms); 16280Sstevel@tonic-gate bcopy(osp, sp, symsize); 16290Sstevel@tonic-gate bcopy(name, mp->strings + strsize, namelen); 16300Sstevel@tonic-gate sp->st_name = strsize; 16310Sstevel@tonic-gate sym_insert(mp, name, nsyms); 16320Sstevel@tonic-gate nsyms++; 16330Sstevel@tonic-gate strsize += namelen + 1; 16340Sstevel@tonic-gate } 16350Sstevel@tonic-gate 16360Sstevel@tonic-gate rw_exit(&ksyms_lock); 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate /* 16390Sstevel@tonic-gate * Free the old section headers -- we'll never need them again. 16400Sstevel@tonic-gate */ 16410Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 16420Sstevel@tonic-gate kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum); 16430Sstevel@tonic-gate /* 16440Sstevel@tonic-gate * Discard the old symbol table and our copy of the module strucure. 16450Sstevel@tonic-gate */ 16460Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 16470Sstevel@tonic-gate kobj_free(omp->symspace, omp->symsize); 16480Sstevel@tonic-gate kobj_free(omp, sizeof (struct module)); 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate static void 16520Sstevel@tonic-gate kobj_export_ctf(struct module *mp) 16530Sstevel@tonic-gate { 16540Sstevel@tonic-gate char *data = mp->ctfdata; 16550Sstevel@tonic-gate size_t size = mp->ctfsize; 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate if (data != NULL) { 16580Sstevel@tonic-gate if (_moddebug & MODDEBUG_NOCTF) { 16590Sstevel@tonic-gate mp->ctfdata = NULL; 16600Sstevel@tonic-gate mp->ctfsize = 0; 16610Sstevel@tonic-gate } else { 16620Sstevel@tonic-gate mp->ctfdata = vmem_alloc(ctf_arena, size, 16630Sstevel@tonic-gate VM_BESTFIT | VM_SLEEP); 16640Sstevel@tonic-gate bcopy(data, mp->ctfdata, size); 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate 16670Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 16680Sstevel@tonic-gate kobj_free(data, size); 16690Sstevel@tonic-gate } 16700Sstevel@tonic-gate } 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate void 16730Sstevel@tonic-gate kobj_export_module(struct module *mp) 16740Sstevel@tonic-gate { 16750Sstevel@tonic-gate kobj_export_ksyms(mp); 16760Sstevel@tonic-gate kobj_export_ctf(mp); 16770Sstevel@tonic-gate 16780Sstevel@tonic-gate mp->flags |= KOBJ_EXPORTED; 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate 16810Sstevel@tonic-gate static int 16820Sstevel@tonic-gate process_dynamic(struct module *mp, char *dyndata, char *strdata) 16830Sstevel@tonic-gate { 16840Sstevel@tonic-gate char *path = NULL, *depstr = NULL; 16850Sstevel@tonic-gate int allocsize = 0, osize = 0, nsize = 0; 16860Sstevel@tonic-gate char *libname, *tmp; 16870Sstevel@tonic-gate int lsize; 16880Sstevel@tonic-gate Dyn *dynp; 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) { 16910Sstevel@tonic-gate switch (dynp->d_tag) { 16920Sstevel@tonic-gate case DT_NEEDED: 16930Sstevel@tonic-gate /* 16940Sstevel@tonic-gate * Read the DT_NEEDED entries, expanding the macros they 16950Sstevel@tonic-gate * contain (if any), and concatenating them into a 16960Sstevel@tonic-gate * single space-separated dependency list. 16970Sstevel@tonic-gate */ 16980Sstevel@tonic-gate libname = (ulong_t)dynp->d_un.d_ptr + strdata; 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate if (strchr(libname, '$') != NULL) { 17010Sstevel@tonic-gate char *_lib; 17020Sstevel@tonic-gate 17030Sstevel@tonic-gate if (path == NULL) 17040Sstevel@tonic-gate path = kobj_alloc(MAXPATHLEN, KM_WAIT); 17050Sstevel@tonic-gate if ((_lib = expand_libmacro(libname, path, 17060Sstevel@tonic-gate path)) != NULL) 17070Sstevel@tonic-gate libname = _lib; 17080Sstevel@tonic-gate else { 17090Sstevel@tonic-gate _kobj_printf(ops, "krtld: " 17100Sstevel@tonic-gate "process_dynamic: failed to expand " 17110Sstevel@tonic-gate "%s\n", libname); 17120Sstevel@tonic-gate } 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate lsize = strlen(libname); 17160Sstevel@tonic-gate nsize += lsize; 17170Sstevel@tonic-gate if (nsize + 1 > allocsize) { 17180Sstevel@tonic-gate tmp = kobj_alloc(allocsize + MAXPATHLEN, 17190Sstevel@tonic-gate KM_WAIT); 17200Sstevel@tonic-gate if (depstr != NULL) { 17210Sstevel@tonic-gate bcopy(depstr, tmp, osize); 17220Sstevel@tonic-gate kobj_free(depstr, allocsize); 17230Sstevel@tonic-gate } 17240Sstevel@tonic-gate depstr = tmp; 17250Sstevel@tonic-gate allocsize += MAXPATHLEN; 17260Sstevel@tonic-gate } 17270Sstevel@tonic-gate bcopy(libname, depstr + osize, lsize); 17280Sstevel@tonic-gate *(depstr + nsize) = ' '; /* separator */ 17290Sstevel@tonic-gate nsize++; 17300Sstevel@tonic-gate osize = nsize; 17310Sstevel@tonic-gate break; 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate case DT_FLAGS_1: 17340Sstevel@tonic-gate if (dynp->d_un.d_val & DF_1_IGNMULDEF) 17350Sstevel@tonic-gate mp->flags |= KOBJ_IGNMULDEF; 17360Sstevel@tonic-gate if (dynp->d_un.d_val & DF_1_NOKSYMS) 17370Sstevel@tonic-gate mp->flags |= KOBJ_NOKSYMS; 17380Sstevel@tonic-gate 17390Sstevel@tonic-gate break; 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate } 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate /* 17440Sstevel@tonic-gate * finish up the depends string (if any) 17450Sstevel@tonic-gate */ 17460Sstevel@tonic-gate if (depstr != NULL) { 17470Sstevel@tonic-gate *(depstr + nsize - 1) = '\0'; /* overwrite seperator w/term */ 17480Sstevel@tonic-gate if (path != NULL) 17490Sstevel@tonic-gate kobj_free(path, MAXPATHLEN); 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate tmp = kobj_alloc(nsize, KM_WAIT); 17520Sstevel@tonic-gate bcopy(depstr, tmp, nsize); 17530Sstevel@tonic-gate kobj_free(depstr, allocsize); 17540Sstevel@tonic-gate depstr = tmp; 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate mp->depends_on = depstr; 17570Sstevel@tonic-gate } 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate return (0); 17600Sstevel@tonic-gate } 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate static int 17630Sstevel@tonic-gate do_dynamic(struct module *mp, struct _buf *file) 17640Sstevel@tonic-gate { 17650Sstevel@tonic-gate Shdr *dshp, *dstrp, *shp; 17660Sstevel@tonic-gate char *dyndata, *dstrdata; 17670Sstevel@tonic-gate int dshn, shn, rc; 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate /* find and validate the dynamic section (if any) */ 17700Sstevel@tonic-gate 17710Sstevel@tonic-gate for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) { 17720Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 17730Sstevel@tonic-gate switch (shp->sh_type) { 17740Sstevel@tonic-gate case SHT_DYNAMIC: 17750Sstevel@tonic-gate if (dshp != NULL) { 17760Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", 17770Sstevel@tonic-gate mp->filename); 17780Sstevel@tonic-gate _kobj_printf(ops, 17790Sstevel@tonic-gate "multiple dynamic sections\n"); 17800Sstevel@tonic-gate return (-1); 17810Sstevel@tonic-gate } else { 17820Sstevel@tonic-gate dshp = shp; 17830Sstevel@tonic-gate dshn = shn; 17840Sstevel@tonic-gate } 17850Sstevel@tonic-gate break; 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate } 17880Sstevel@tonic-gate 17890Sstevel@tonic-gate if (dshp == NULL) 17900Sstevel@tonic-gate return (0); 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate if (dshp->sh_link > mp->hdr.e_shnum) { 17930Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 17940Sstevel@tonic-gate _kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link); 17950Sstevel@tonic-gate return (-1); 17960Sstevel@tonic-gate } 17970Sstevel@tonic-gate dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize); 17980Sstevel@tonic-gate 17990Sstevel@tonic-gate if (dstrp->sh_type != SHT_STRTAB) { 18000Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 18010Sstevel@tonic-gate _kobj_printf(ops, "sh_link not a string table for section %d\n", 18020Sstevel@tonic-gate dshn); 18030Sstevel@tonic-gate return (-1); 18040Sstevel@tonic-gate } 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate /* read it from disk */ 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP); 18090Sstevel@tonic-gate if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) { 18100Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 18110Sstevel@tonic-gate _kobj_printf(ops, "error reading section %d\n", dshn); 18120Sstevel@tonic-gate 18130Sstevel@tonic-gate kobj_free(dyndata, dshp->sh_size); 18140Sstevel@tonic-gate return (-1); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP); 18180Sstevel@tonic-gate if (kobj_read_file(file, dstrdata, dstrp->sh_size, 18190Sstevel@tonic-gate dstrp->sh_offset) < 0) { 18200Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 18210Sstevel@tonic-gate _kobj_printf(ops, "error reading section %d\n", dshp->sh_link); 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate kobj_free(dyndata, dshp->sh_size); 18240Sstevel@tonic-gate kobj_free(dstrdata, dstrp->sh_size); 18250Sstevel@tonic-gate return (-1); 18260Sstevel@tonic-gate } 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate /* pull the interesting pieces out */ 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate rc = process_dynamic(mp, dyndata, dstrdata); 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate kobj_free(dyndata, dshp->sh_size); 18330Sstevel@tonic-gate kobj_free(dstrdata, dstrp->sh_size); 18340Sstevel@tonic-gate 18350Sstevel@tonic-gate return (rc); 18360Sstevel@tonic-gate } 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate void 18390Sstevel@tonic-gate kobj_set_ctf(struct module *mp, caddr_t data, size_t size) 18400Sstevel@tonic-gate { 18410Sstevel@tonic-gate if (!standalone) { 18420Sstevel@tonic-gate if (mp->ctfdata != NULL) { 18430Sstevel@tonic-gate if (vmem_contains(ctf_arena, mp->ctfdata, 18443912Slling mp->ctfsize)) { 18450Sstevel@tonic-gate vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize); 18460Sstevel@tonic-gate } else { 18470Sstevel@tonic-gate kobj_free(mp->ctfdata, mp->ctfsize); 18480Sstevel@tonic-gate } 18490Sstevel@tonic-gate } 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate /* 18530Sstevel@tonic-gate * The order is very important here. We need to make sure that 18540Sstevel@tonic-gate * consumers, at any given instant, see a consistent state. We'd 18550Sstevel@tonic-gate * rather they see no CTF data than the address of one buffer and the 18560Sstevel@tonic-gate * size of another. 18570Sstevel@tonic-gate */ 18580Sstevel@tonic-gate mp->ctfdata = NULL; 18590Sstevel@tonic-gate membar_producer(); 18600Sstevel@tonic-gate mp->ctfsize = size; 18610Sstevel@tonic-gate mp->ctfdata = data; 18620Sstevel@tonic-gate membar_producer(); 18630Sstevel@tonic-gate } 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate int 18660Sstevel@tonic-gate kobj_load_module(struct modctl *modp, int use_path) 18670Sstevel@tonic-gate { 18680Sstevel@tonic-gate char *filename = modp->mod_filename; 18690Sstevel@tonic-gate char *modname = modp->mod_modname; 18700Sstevel@tonic-gate int i; 18710Sstevel@tonic-gate int n; 18720Sstevel@tonic-gate struct _buf *file; 18730Sstevel@tonic-gate struct module *mp = NULL; 18740Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 18750Sstevel@tonic-gate int no_suffixdir_drv = 0; 18760Sstevel@tonic-gate #endif 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 18790Sstevel@tonic-gate 18800Sstevel@tonic-gate /* 18810Sstevel@tonic-gate * We need to prevent kmdb's symbols from leaking into /dev/ksyms. 18820Sstevel@tonic-gate * kmdb contains a bunch of symbols with well-known names, symbols 18830Sstevel@tonic-gate * which will mask the real versions, thus causing no end of trouble 18840Sstevel@tonic-gate * for mdb. 18850Sstevel@tonic-gate */ 18860Sstevel@tonic-gate if (strcmp(modp->mod_modname, "kmdbmod") == 0) 18870Sstevel@tonic-gate mp->flags |= KOBJ_NOKSYMS; 18880Sstevel@tonic-gate 18890Sstevel@tonic-gate file = kobj_open_path(filename, use_path, 1); 18900Sstevel@tonic-gate if (file == (struct _buf *)-1) { 18910Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 18920Sstevel@tonic-gate file = kobj_open_path(filename, use_path, 0); 18930Sstevel@tonic-gate #endif 18940Sstevel@tonic-gate if (file == (struct _buf *)-1) { 18950Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 18960Sstevel@tonic-gate goto bad; 18970Sstevel@tonic-gate } 18980Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 18990Sstevel@tonic-gate /* 19000Sstevel@tonic-gate * There is no driver module in the ISA specific (suffix) 19010Sstevel@tonic-gate * subdirectory but there is a module in the parent directory. 19020Sstevel@tonic-gate */ 19030Sstevel@tonic-gate if (strncmp(filename, "drv/", 4) == 0) { 19040Sstevel@tonic-gate no_suffixdir_drv = 1; 19050Sstevel@tonic-gate } 19060Sstevel@tonic-gate #endif 19070Sstevel@tonic-gate } 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT); 19100Sstevel@tonic-gate (void) strcpy(mp->filename, file->_name); 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) { 19130Sstevel@tonic-gate _kobj_printf(ops, "kobj_load_module: %s read header failed\n", 19140Sstevel@tonic-gate modname); 19150Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 19160Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 19170Sstevel@tonic-gate goto bad; 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate for (i = 0; i < SELFMAG; i++) { 19200Sstevel@tonic-gate if (mp->hdr.e_ident[i] != ELFMAG[i]) { 19210Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 19220Sstevel@tonic-gate _kobj_printf(ops, "%s not an elf module\n", 19230Sstevel@tonic-gate modname); 19240Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 19250Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 19260Sstevel@tonic-gate goto bad; 19270Sstevel@tonic-gate } 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate /* 19300Sstevel@tonic-gate * It's ELF, but is it our ISA? Interpreting the header 19310Sstevel@tonic-gate * from a file for a byte-swapped ISA could cause a huge 19320Sstevel@tonic-gate * and unsatisfiable value to be passed to kobj_alloc below 19330Sstevel@tonic-gate * and therefore hang booting. 19340Sstevel@tonic-gate */ 19350Sstevel@tonic-gate if (!elf_mach_ok(&mp->hdr)) { 19360Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 19370Sstevel@tonic-gate _kobj_printf(ops, "%s not an elf module for this ISA\n", 19380Sstevel@tonic-gate modname); 19390Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 19400Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 19410Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 19420Sstevel@tonic-gate /* 19430Sstevel@tonic-gate * The driver mod is not in the ISA specific subdirectory 19440Sstevel@tonic-gate * and the module in the parent directory is not our ISA. 19450Sstevel@tonic-gate * If it is our ISA, for now we will silently succeed. 19460Sstevel@tonic-gate */ 19470Sstevel@tonic-gate if (no_suffixdir_drv == 1) { 19480Sstevel@tonic-gate cmn_err(CE_CONT, "?NOTICE: %s: 64-bit driver module" 19490Sstevel@tonic-gate " not found\n", modname); 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate #endif 19520Sstevel@tonic-gate goto bad; 19530Sstevel@tonic-gate } 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate /* 19560Sstevel@tonic-gate * All modules, save for unix, should be relocatable (as opposed to 19570Sstevel@tonic-gate * dynamic). Dynamic modules come with PLTs and GOTs, which can't 19580Sstevel@tonic-gate * currently be processed by krtld. 19590Sstevel@tonic-gate */ 19600Sstevel@tonic-gate if (mp->hdr.e_type != ET_REL) { 19610Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 19620Sstevel@tonic-gate _kobj_printf(ops, "%s isn't a relocatable (ET_REL) " 19630Sstevel@tonic-gate "module\n", modname); 19640Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 19650Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 19660Sstevel@tonic-gate goto bad; 19670Sstevel@tonic-gate } 19680Sstevel@tonic-gate 19690Sstevel@tonic-gate n = mp->hdr.e_shentsize * mp->hdr.e_shnum; 19700Sstevel@tonic-gate mp->shdrs = kobj_alloc(n, KM_WAIT); 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) { 19730Sstevel@tonic-gate _kobj_printf(ops, "kobj_load_module: %s error reading " 19740Sstevel@tonic-gate "section headers\n", modname); 19750Sstevel@tonic-gate kobj_free(mp->shdrs, n); 19760Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 19770Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 19780Sstevel@tonic-gate goto bad; 19790Sstevel@tonic-gate } 19800Sstevel@tonic-gate 19810Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODLOADING, modp); 19820Sstevel@tonic-gate module_assign(modp, mp); 19830Sstevel@tonic-gate 19840Sstevel@tonic-gate /* read in sections */ 19850Sstevel@tonic-gate if (get_progbits(mp, file) < 0) { 19860Sstevel@tonic-gate _kobj_printf(ops, "%s error reading sections\n", modname); 19870Sstevel@tonic-gate goto bad; 19880Sstevel@tonic-gate } 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate if (do_dynamic(mp, file) < 0) { 19910Sstevel@tonic-gate _kobj_printf(ops, "%s error reading dynamic section\n", 19920Sstevel@tonic-gate modname); 19930Sstevel@tonic-gate goto bad; 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate modp->mod_text = mp->text; 19970Sstevel@tonic-gate modp->mod_text_size = mp->text_size; 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate /* read in symbols; adjust values for each section's real address */ 20000Sstevel@tonic-gate if (get_syms(mp, file) < 0) { 20010Sstevel@tonic-gate _kobj_printf(ops, "%s error reading symbols\n", 20020Sstevel@tonic-gate modname); 20030Sstevel@tonic-gate goto bad; 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate /* 20070Sstevel@tonic-gate * If we didn't dependency information from the dynamic section, look 20080Sstevel@tonic-gate * for it the old-fashioned way. 20090Sstevel@tonic-gate */ 20100Sstevel@tonic-gate if (mp->depends_on == NULL) 20110Sstevel@tonic-gate mp->depends_on = depends_on(mp); 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate if (get_ctf(mp, file) < 0) { 20140Sstevel@tonic-gate _kobj_printf(ops, "%s debug information will not " 20150Sstevel@tonic-gate "be available\n", modname); 20160Sstevel@tonic-gate } 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate /* primary kernel modules do not have a signature section */ 20190Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 20200Sstevel@tonic-gate get_signature(mp, file); 20210Sstevel@tonic-gate 20220Sstevel@tonic-gate #ifdef KOBJ_DEBUG 20230Sstevel@tonic-gate if (kobj_debug & D_LOADING) { 20240Sstevel@tonic-gate _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 20250Sstevel@tonic-gate _kobj_printf(ops, "\ttext:0x%p", mp->text); 20260Sstevel@tonic-gate _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 20270Sstevel@tonic-gate _kobj_printf(ops, "\tdata:0x%p", mp->data); 20280Sstevel@tonic-gate _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* 20330Sstevel@tonic-gate * For primary kernel modules, we defer 20340Sstevel@tonic-gate * symbol resolution and relocation until 20350Sstevel@tonic-gate * all primary objects have been loaded. 20360Sstevel@tonic-gate */ 20370Sstevel@tonic-gate if (!standalone) { 20380Sstevel@tonic-gate int ddrval, dcrval; 20390Sstevel@tonic-gate char *dependent_modname; 20400Sstevel@tonic-gate /* load all dependents */ 20410Sstevel@tonic-gate dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT); 20420Sstevel@tonic-gate ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN); 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate /* 20450Sstevel@tonic-gate * resolve undefined and common symbols, 20460Sstevel@tonic-gate * also allocates common space 20470Sstevel@tonic-gate */ 20480Sstevel@tonic-gate if ((dcrval = do_common(mp)) < 0) { 20490Sstevel@tonic-gate switch (dcrval) { 20500Sstevel@tonic-gate case DOSYM_UNSAFE: 20510Sstevel@tonic-gate _kobj_printf(ops, "WARNING: mod_load: " 20520Sstevel@tonic-gate "MT-unsafe module '%s' rejected\n", 20530Sstevel@tonic-gate modname); 20540Sstevel@tonic-gate break; 20550Sstevel@tonic-gate case DOSYM_UNDEF: 20560Sstevel@tonic-gate _kobj_printf(ops, "WARNING: mod_load: " 20570Sstevel@tonic-gate "cannot load module '%s'\n", 20580Sstevel@tonic-gate modname); 20590Sstevel@tonic-gate if (ddrval == -1) { 20600Sstevel@tonic-gate _kobj_printf(ops, "WARNING: %s: ", 20610Sstevel@tonic-gate modname); 20620Sstevel@tonic-gate _kobj_printf(ops, 20630Sstevel@tonic-gate "unable to resolve dependency, " 20640Sstevel@tonic-gate "module '%s' not found\n", 20650Sstevel@tonic-gate dependent_modname); 20660Sstevel@tonic-gate } 20670Sstevel@tonic-gate break; 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate } 20700Sstevel@tonic-gate kobj_free(dependent_modname, MODMAXNAMELEN); 20710Sstevel@tonic-gate if (dcrval < 0) 20720Sstevel@tonic-gate goto bad; 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate /* process relocation tables */ 20750Sstevel@tonic-gate if (do_relocations(mp) < 0) { 20760Sstevel@tonic-gate _kobj_printf(ops, "%s error doing relocations\n", 20770Sstevel@tonic-gate modname); 20780Sstevel@tonic-gate goto bad; 20790Sstevel@tonic-gate } 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate if (mp->destination) { 20820Sstevel@tonic-gate off_t off = (uintptr_t)mp->destination & PAGEOFFSET; 20830Sstevel@tonic-gate caddr_t base = (caddr_t)mp->destination - off; 20840Sstevel@tonic-gate size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE); 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK); 20870Sstevel@tonic-gate vmem_free(heap_arena, base, size); 20880Sstevel@tonic-gate } 20890Sstevel@tonic-gate 20900Sstevel@tonic-gate /* sync_instruction_memory */ 20910Sstevel@tonic-gate kobj_sync_instruction_memory(mp->text, mp->text_size); 20920Sstevel@tonic-gate #ifdef MPSAS 20930Sstevel@tonic-gate sas_syms(mp); 20940Sstevel@tonic-gate #endif 20950Sstevel@tonic-gate kobj_export_module(mp); 20960Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODLOADED, modp); 20970Sstevel@tonic-gate } 20980Sstevel@tonic-gate kobj_close_file(file); 20990Sstevel@tonic-gate return (0); 21000Sstevel@tonic-gate bad: 21010Sstevel@tonic-gate if (file != (struct _buf *)-1) 21020Sstevel@tonic-gate kobj_close_file(file); 21030Sstevel@tonic-gate if (modp->mod_mp != NULL) 21040Sstevel@tonic-gate free_module_data(modp->mod_mp); 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate module_assign(modp, NULL); 21070Sstevel@tonic-gate return ((file == (struct _buf *)-1) ? ENOENT : EINVAL); 21080Sstevel@tonic-gate } 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate int 21110Sstevel@tonic-gate kobj_load_primary_module(struct modctl *modp) 21120Sstevel@tonic-gate { 21130Sstevel@tonic-gate struct modctl *dep; 21140Sstevel@tonic-gate struct module *mp; 21150Sstevel@tonic-gate 21160Sstevel@tonic-gate if (kobj_load_module(modp, 0) != 0) 21170Sstevel@tonic-gate return (-1); 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate mp = modp->mod_mp; 21200Sstevel@tonic-gate mp->flags |= KOBJ_PRIM; 21210Sstevel@tonic-gate 21220Sstevel@tonic-gate /* Bind new module to its dependents */ 21230Sstevel@tonic-gate if (mp->depends_on != NULL && (dep = 21240Sstevel@tonic-gate mod_already_loaded(mp->depends_on)) == NULL) { 21250Sstevel@tonic-gate #ifdef KOBJ_DEBUG 21260Sstevel@tonic-gate if (kobj_debug & D_DEBUG) { 21270Sstevel@tonic-gate _kobj_printf(ops, "krtld: failed to resolve deps " 21280Sstevel@tonic-gate "for primary %s\n", modp->mod_modname); 21290Sstevel@tonic-gate } 21300Sstevel@tonic-gate #endif 21310Sstevel@tonic-gate return (-1); 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate add_dependent(mp, dep->mod_mp); 21350Sstevel@tonic-gate 21360Sstevel@tonic-gate /* 21370Sstevel@tonic-gate * Relocate it. This module may not be part of a link map, so we 21380Sstevel@tonic-gate * can't use bind_primary. 21390Sstevel@tonic-gate */ 21400Sstevel@tonic-gate if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 || 21410Sstevel@tonic-gate do_relocations(mp) < 0) { 21420Sstevel@tonic-gate #ifdef KOBJ_DEBUG 21430Sstevel@tonic-gate if (kobj_debug & D_DEBUG) { 21440Sstevel@tonic-gate _kobj_printf(ops, "krtld: failed to relocate " 21450Sstevel@tonic-gate "primary %s\n", modp->mod_modname); 21460Sstevel@tonic-gate } 21470Sstevel@tonic-gate #endif 21480Sstevel@tonic-gate return (-1); 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate 21510Sstevel@tonic-gate return (0); 21520Sstevel@tonic-gate } 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate static void 21550Sstevel@tonic-gate module_assign(struct modctl *cp, struct module *mp) 21560Sstevel@tonic-gate { 21570Sstevel@tonic-gate if (standalone) { 21580Sstevel@tonic-gate cp->mod_mp = mp; 21590Sstevel@tonic-gate return; 21600Sstevel@tonic-gate } 21610Sstevel@tonic-gate mutex_enter(&mod_lock); 21620Sstevel@tonic-gate cp->mod_mp = mp; 21630Sstevel@tonic-gate cp->mod_gencount++; 21640Sstevel@tonic-gate mutex_exit(&mod_lock); 21650Sstevel@tonic-gate } 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate void 21680Sstevel@tonic-gate kobj_unload_module(struct modctl *modp) 21690Sstevel@tonic-gate { 21700Sstevel@tonic-gate struct module *mp = modp->mod_mp; 21710Sstevel@tonic-gate 21720Sstevel@tonic-gate if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) { 21730Sstevel@tonic-gate _kobj_printf(ops, "text for %s ", mp->filename); 21740Sstevel@tonic-gate _kobj_printf(ops, "was at %p\n", mp->text); 21750Sstevel@tonic-gate mp->text = NULL; /* don't actually free it */ 21760Sstevel@tonic-gate } 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp); 21790Sstevel@tonic-gate 21800Sstevel@tonic-gate /* 21810Sstevel@tonic-gate * Null out mod_mp first, so consumers (debuggers) know not to look 21820Sstevel@tonic-gate * at the module structure any more. 21830Sstevel@tonic-gate */ 21840Sstevel@tonic-gate mutex_enter(&mod_lock); 21850Sstevel@tonic-gate modp->mod_mp = NULL; 21860Sstevel@tonic-gate mutex_exit(&mod_lock); 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp); 21890Sstevel@tonic-gate free_module_data(mp); 21900Sstevel@tonic-gate } 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate static void 21930Sstevel@tonic-gate free_module_data(struct module *mp) 21940Sstevel@tonic-gate { 21950Sstevel@tonic-gate struct module_list *lp, *tmp; 21960Sstevel@tonic-gate int ksyms_exported = 0; 21970Sstevel@tonic-gate 21980Sstevel@tonic-gate lp = mp->head; 21990Sstevel@tonic-gate while (lp) { 22000Sstevel@tonic-gate tmp = lp; 22010Sstevel@tonic-gate lp = lp->next; 22020Sstevel@tonic-gate kobj_free((char *)tmp, sizeof (*tmp)); 22030Sstevel@tonic-gate } 22040Sstevel@tonic-gate 22050Sstevel@tonic-gate rw_enter(&ksyms_lock, RW_WRITER); 22060Sstevel@tonic-gate if (mp->symspace) { 22070Sstevel@tonic-gate if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) { 22080Sstevel@tonic-gate vmem_free(ksyms_arena, mp->symspace, mp->symsize); 22090Sstevel@tonic-gate ksyms_exported = 1; 22100Sstevel@tonic-gate } else { 22110Sstevel@tonic-gate if (mp->flags & KOBJ_NOKSYMS) 22120Sstevel@tonic-gate ksyms_exported = 1; 22130Sstevel@tonic-gate kobj_free(mp->symspace, mp->symsize); 22140Sstevel@tonic-gate } 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate rw_exit(&ksyms_lock); 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate if (mp->ctfdata) { 22190Sstevel@tonic-gate if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize)) 22200Sstevel@tonic-gate vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize); 22210Sstevel@tonic-gate else 22220Sstevel@tonic-gate kobj_free(mp->ctfdata, mp->ctfsize); 22230Sstevel@tonic-gate } 22240Sstevel@tonic-gate 22250Sstevel@tonic-gate if (mp->sigdata) 22260Sstevel@tonic-gate kobj_free(mp->sigdata, mp->sigsize); 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate /* 22290Sstevel@tonic-gate * We did not get far enough into kobj_export_ksyms() to free allocated 22300Sstevel@tonic-gate * buffers because we encounted error conditions. Free the buffers. 22310Sstevel@tonic-gate */ 22320Sstevel@tonic-gate if ((ksyms_exported == 0) && (mp->shdrs != NULL)) { 22330Sstevel@tonic-gate uint_t shn; 22340Sstevel@tonic-gate Shdr *shp; 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 22370Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 22380Sstevel@tonic-gate switch (shp->sh_type) { 22390Sstevel@tonic-gate case SHT_RELA: 22400Sstevel@tonic-gate case SHT_REL: 22410Sstevel@tonic-gate if (shp->sh_addr != 0) 22420Sstevel@tonic-gate kobj_free((void *)shp->sh_addr, 22430Sstevel@tonic-gate shp->sh_size); 22440Sstevel@tonic-gate break; 22450Sstevel@tonic-gate } 22460Sstevel@tonic-gate } 22470Sstevel@tonic-gate err_free_done: 22480Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) { 22490Sstevel@tonic-gate kobj_free(mp->shdrs, 22500Sstevel@tonic-gate mp->hdr.e_shentsize * mp->hdr.e_shnum); 22510Sstevel@tonic-gate } 22520Sstevel@tonic-gate } 22530Sstevel@tonic-gate 22540Sstevel@tonic-gate if (mp->bss) 22550Sstevel@tonic-gate vmem_free(data_arena, (void *)mp->bss, mp->bss_size); 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate if (mp->fbt_tab) 22580Sstevel@tonic-gate kobj_texthole_free(mp->fbt_tab, mp->fbt_size); 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate if (mp->textwin_base) 22610Sstevel@tonic-gate kobj_textwin_free(mp); 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate if (mp->sdt_probes != NULL) { 22640Sstevel@tonic-gate sdt_probedesc_t *sdp = mp->sdt_probes, *next; 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate while (sdp != NULL) { 22670Sstevel@tonic-gate next = sdp->sdpd_next; 22680Sstevel@tonic-gate kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1); 22690Sstevel@tonic-gate kobj_free(sdp, sizeof (sdt_probedesc_t)); 22700Sstevel@tonic-gate sdp = next; 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate if (mp->sdt_tab) 22750Sstevel@tonic-gate kobj_texthole_free(mp->sdt_tab, mp->sdt_size); 22760Sstevel@tonic-gate if (mp->text) 22770Sstevel@tonic-gate vmem_free(text_arena, mp->text, mp->text_size); 22780Sstevel@tonic-gate if (mp->data) 22790Sstevel@tonic-gate vmem_free(data_arena, mp->data, mp->data_size); 22800Sstevel@tonic-gate if (mp->depends_on) 22810Sstevel@tonic-gate kobj_free(mp->depends_on, strlen(mp->depends_on)+1); 22820Sstevel@tonic-gate if (mp->filename) 22830Sstevel@tonic-gate kobj_free(mp->filename, strlen(mp->filename)+1); 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate kobj_free((char *)mp, sizeof (*mp)); 22860Sstevel@tonic-gate } 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate static int 22890Sstevel@tonic-gate get_progbits(struct module *mp, struct _buf *file) 22900Sstevel@tonic-gate { 22910Sstevel@tonic-gate struct proginfo *tp, *dp, *sdp; 22920Sstevel@tonic-gate Shdr *shp; 22930Sstevel@tonic-gate reloc_dest_t dest = NULL; 22940Sstevel@tonic-gate uintptr_t bits_ptr; 22950Sstevel@tonic-gate uintptr_t text = 0, data, sdata = 0, textptr; 22960Sstevel@tonic-gate uint_t shn; 22970Sstevel@tonic-gate int err = -1; 22980Sstevel@tonic-gate 22990Sstevel@tonic-gate tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 23000Sstevel@tonic-gate dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 23010Sstevel@tonic-gate sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 23020Sstevel@tonic-gate /* 23030Sstevel@tonic-gate * loop through sections to find out how much space we need 23040Sstevel@tonic-gate * for text, data, (also bss that is already assigned) 23050Sstevel@tonic-gate */ 23060Sstevel@tonic-gate if (get_progbits_size(mp, tp, dp, sdp) < 0) 23070Sstevel@tonic-gate goto done; 23080Sstevel@tonic-gate 23090Sstevel@tonic-gate mp->text_size = tp->size; 23100Sstevel@tonic-gate mp->data_size = dp->size; 23110Sstevel@tonic-gate 23120Sstevel@tonic-gate if (standalone) { 23133446Smrj caddr_t limit = _data; 23143446Smrj 23153446Smrj if (lg_pagesize && _text + lg_pagesize < limit) 23163446Smrj limit = _text + lg_pagesize; 23173446Smrj 23180Sstevel@tonic-gate mp->text = kobj_segbrk(&_etext, mp->text_size, 23193912Slling tp->align, limit); 23200Sstevel@tonic-gate /* 23210Sstevel@tonic-gate * If we can't grow the text segment, try the 23220Sstevel@tonic-gate * data segment before failing. 23230Sstevel@tonic-gate */ 23240Sstevel@tonic-gate if (mp->text == NULL) { 23250Sstevel@tonic-gate mp->text = kobj_segbrk(&_edata, mp->text_size, 23263912Slling tp->align, 0); 23270Sstevel@tonic-gate } 23280Sstevel@tonic-gate 23290Sstevel@tonic-gate mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0); 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate if (mp->text == NULL || mp->data == NULL) 23320Sstevel@tonic-gate goto done; 23330Sstevel@tonic-gate 23340Sstevel@tonic-gate } else { 23350Sstevel@tonic-gate if (text_arena == NULL) 23360Sstevel@tonic-gate kobj_vmem_init(&text_arena, &data_arena); 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate /* 23390Sstevel@tonic-gate * some architectures may want to load the module on a 23400Sstevel@tonic-gate * page that is currently read only. It may not be 23410Sstevel@tonic-gate * possible for those architectures to remap their page 23420Sstevel@tonic-gate * on the fly. So we provide a facility for them to hang 23430Sstevel@tonic-gate * a private hook where the memory they assign the module 23440Sstevel@tonic-gate * is not the actual place where the module loads. 23450Sstevel@tonic-gate * 23460Sstevel@tonic-gate * In this case there are two addresses that deal with the 23470Sstevel@tonic-gate * modload. 23480Sstevel@tonic-gate * 1) the final destination of the module 23490Sstevel@tonic-gate * 2) the address that is used to view the newly 23500Sstevel@tonic-gate * loaded module until all the relocations relative to 1 23510Sstevel@tonic-gate * above are completed. 23520Sstevel@tonic-gate * 23530Sstevel@tonic-gate * That is what dest is used for below. 23540Sstevel@tonic-gate */ 23550Sstevel@tonic-gate mp->text_size += tp->align; 23560Sstevel@tonic-gate mp->data_size += dp->align; 23570Sstevel@tonic-gate 23580Sstevel@tonic-gate mp->text = kobj_text_alloc(text_arena, mp->text_size); 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate /* 23610Sstevel@tonic-gate * a remap is taking place. Align the text ptr relative 23620Sstevel@tonic-gate * to the secondary mapping. That is where the bits will 23630Sstevel@tonic-gate * be read in. 23640Sstevel@tonic-gate */ 23650Sstevel@tonic-gate if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena, 23660Sstevel@tonic-gate mp->text, mp->text_size)) { 23670Sstevel@tonic-gate off_t off = (uintptr_t)mp->text & PAGEOFFSET; 23680Sstevel@tonic-gate size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE); 23690Sstevel@tonic-gate caddr_t map = vmem_alloc(heap_arena, size, VM_SLEEP); 23700Sstevel@tonic-gate caddr_t orig = mp->text - off; 23710Sstevel@tonic-gate pgcnt_t pages = size / PAGESIZE; 23720Sstevel@tonic-gate 23730Sstevel@tonic-gate dest = (reloc_dest_t)(map + off); 23740Sstevel@tonic-gate text = ALIGN((uintptr_t)dest, tp->align); 23750Sstevel@tonic-gate 23760Sstevel@tonic-gate while (pages--) { 23770Sstevel@tonic-gate hat_devload(kas.a_hat, map, PAGESIZE, 23780Sstevel@tonic-gate hat_getpfnum(kas.a_hat, orig), 23790Sstevel@tonic-gate PROT_READ | PROT_WRITE | PROT_EXEC, 23800Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 23810Sstevel@tonic-gate map += PAGESIZE; 23820Sstevel@tonic-gate orig += PAGESIZE; 23830Sstevel@tonic-gate } 23840Sstevel@tonic-gate /* 23850Sstevel@tonic-gate * Since we set up a non-cacheable mapping, we need 23860Sstevel@tonic-gate * to flush any old entries in the cache that might 23870Sstevel@tonic-gate * be left around from the read-only mapping. 23880Sstevel@tonic-gate */ 23890Sstevel@tonic-gate dcache_flushall(); 23900Sstevel@tonic-gate } 23910Sstevel@tonic-gate if (mp->data_size) 23920Sstevel@tonic-gate mp->data = vmem_alloc(data_arena, mp->data_size, 23930Sstevel@tonic-gate VM_SLEEP | VM_BESTFIT); 23940Sstevel@tonic-gate } 23950Sstevel@tonic-gate textptr = (uintptr_t)mp->text; 23960Sstevel@tonic-gate textptr = ALIGN(textptr, tp->align); 23970Sstevel@tonic-gate mp->destination = dest; 23980Sstevel@tonic-gate 23990Sstevel@tonic-gate /* 24000Sstevel@tonic-gate * This is the case where a remap is not being done. 24010Sstevel@tonic-gate */ 24020Sstevel@tonic-gate if (text == 0) 24030Sstevel@tonic-gate text = ALIGN((uintptr_t)mp->text, tp->align); 24040Sstevel@tonic-gate data = ALIGN((uintptr_t)mp->data, dp->align); 24050Sstevel@tonic-gate 24060Sstevel@tonic-gate /* now loop though sections assigning addresses and loading the data */ 24070Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 24080Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 24090Sstevel@tonic-gate if (!(shp->sh_flags & SHF_ALLOC)) 24100Sstevel@tonic-gate continue; 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate if ((shp->sh_flags & SHF_WRITE) == 0) 24130Sstevel@tonic-gate bits_ptr = text; 24140Sstevel@tonic-gate else if (shp->sh_flags & SHF_NEUT_SHORT) 24150Sstevel@tonic-gate bits_ptr = sdata; 24160Sstevel@tonic-gate else 24170Sstevel@tonic-gate bits_ptr = data; 24180Sstevel@tonic-gate 24190Sstevel@tonic-gate bits_ptr = ALIGN(bits_ptr, shp->sh_addralign); 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate if (shp->sh_type == SHT_NOBITS) { 24220Sstevel@tonic-gate /* 24230Sstevel@tonic-gate * Zero bss. 24240Sstevel@tonic-gate */ 24250Sstevel@tonic-gate bzero((caddr_t)bits_ptr, shp->sh_size); 24260Sstevel@tonic-gate shp->sh_type = SHT_PROGBITS; 24270Sstevel@tonic-gate } else { 24280Sstevel@tonic-gate if (kobj_read_file(file, (char *)bits_ptr, 24290Sstevel@tonic-gate shp->sh_size, shp->sh_offset) < 0) 24300Sstevel@tonic-gate goto done; 24310Sstevel@tonic-gate } 24320Sstevel@tonic-gate 24330Sstevel@tonic-gate if (shp->sh_flags & SHF_WRITE) { 24340Sstevel@tonic-gate shp->sh_addr = bits_ptr; 24350Sstevel@tonic-gate } else { 24360Sstevel@tonic-gate textptr = ALIGN(textptr, shp->sh_addralign); 24370Sstevel@tonic-gate shp->sh_addr = textptr; 24380Sstevel@tonic-gate textptr += shp->sh_size; 24390Sstevel@tonic-gate } 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate bits_ptr += shp->sh_size; 24420Sstevel@tonic-gate if ((shp->sh_flags & SHF_WRITE) == 0) 24430Sstevel@tonic-gate text = bits_ptr; 24440Sstevel@tonic-gate else if (shp->sh_flags & SHF_NEUT_SHORT) 24450Sstevel@tonic-gate sdata = bits_ptr; 24460Sstevel@tonic-gate else 24470Sstevel@tonic-gate data = bits_ptr; 24480Sstevel@tonic-gate } 24490Sstevel@tonic-gate 24500Sstevel@tonic-gate err = 0; 24510Sstevel@tonic-gate done: 24520Sstevel@tonic-gate /* 24530Sstevel@tonic-gate * Free and mark as freed the section headers here so that 24540Sstevel@tonic-gate * free_module_data() does not have to worry about this buffer. 24550Sstevel@tonic-gate * 24560Sstevel@tonic-gate * This buffer is freed here because one of the possible reasons 24570Sstevel@tonic-gate * for error is a section with non-zero sh_addr and in that case 24580Sstevel@tonic-gate * free_module_data() would have no way of recognizing that this 24590Sstevel@tonic-gate * buffer was unallocated. 24600Sstevel@tonic-gate */ 24610Sstevel@tonic-gate if (err != 0) { 24620Sstevel@tonic-gate kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum); 24630Sstevel@tonic-gate mp->shdrs = NULL; 24640Sstevel@tonic-gate } 24650Sstevel@tonic-gate 24660Sstevel@tonic-gate (void) kobj_free(tp, sizeof (struct proginfo)); 24670Sstevel@tonic-gate (void) kobj_free(dp, sizeof (struct proginfo)); 24680Sstevel@tonic-gate (void) kobj_free(sdp, sizeof (struct proginfo)); 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate return (err); 24710Sstevel@tonic-gate } 24720Sstevel@tonic-gate 24730Sstevel@tonic-gate /* 24740Sstevel@tonic-gate * Go through suppress_sym_list to see if "multiply defined" 24750Sstevel@tonic-gate * warning of this symbol should be suppressed. Return 1 if 24760Sstevel@tonic-gate * warning should be suppressed, 0 otherwise. 24770Sstevel@tonic-gate */ 24780Sstevel@tonic-gate static int 24790Sstevel@tonic-gate kobj_suppress_warning(char *symname) 24800Sstevel@tonic-gate { 24810Sstevel@tonic-gate int i; 24820Sstevel@tonic-gate 24830Sstevel@tonic-gate for (i = 0; suppress_sym_list[i] != NULL; i++) { 24840Sstevel@tonic-gate if (strcmp(suppress_sym_list[i], symname) == 0) 24850Sstevel@tonic-gate return (1); 24860Sstevel@tonic-gate } 24870Sstevel@tonic-gate 24880Sstevel@tonic-gate return (0); 24890Sstevel@tonic-gate } 24900Sstevel@tonic-gate 24910Sstevel@tonic-gate static int 24920Sstevel@tonic-gate get_syms(struct module *mp, struct _buf *file) 24930Sstevel@tonic-gate { 24940Sstevel@tonic-gate uint_t shn; 24950Sstevel@tonic-gate Shdr *shp; 24960Sstevel@tonic-gate uint_t i; 24970Sstevel@tonic-gate Sym *sp, *ksp; 24980Sstevel@tonic-gate char *symname; 24990Sstevel@tonic-gate int dosymtab = 0; 25000Sstevel@tonic-gate extern char stubs_base[], stubs_end[]; 25010Sstevel@tonic-gate 25020Sstevel@tonic-gate /* 25030Sstevel@tonic-gate * Find the interesting sections. 25040Sstevel@tonic-gate */ 25050Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 25060Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 25070Sstevel@tonic-gate switch (shp->sh_type) { 25080Sstevel@tonic-gate case SHT_SYMTAB: 25090Sstevel@tonic-gate mp->symtbl_section = shn; 25100Sstevel@tonic-gate mp->symhdr = shp; 25110Sstevel@tonic-gate dosymtab++; 25120Sstevel@tonic-gate break; 25130Sstevel@tonic-gate 25140Sstevel@tonic-gate case SHT_RELA: 25150Sstevel@tonic-gate case SHT_REL: 25160Sstevel@tonic-gate /* 25170Sstevel@tonic-gate * Already loaded. 25180Sstevel@tonic-gate */ 25190Sstevel@tonic-gate if (shp->sh_addr) 25200Sstevel@tonic-gate continue; 25210Sstevel@tonic-gate shp->sh_addr = (Addr) 25220Sstevel@tonic-gate kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP); 25230Sstevel@tonic-gate 25240Sstevel@tonic-gate if (kobj_read_file(file, (char *)shp->sh_addr, 25250Sstevel@tonic-gate shp->sh_size, shp->sh_offset) < 0) { 25260Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_syms: %s, ", 25270Sstevel@tonic-gate mp->filename); 25280Sstevel@tonic-gate _kobj_printf(ops, "error reading section %d\n", 25290Sstevel@tonic-gate shn); 25300Sstevel@tonic-gate return (-1); 25310Sstevel@tonic-gate } 25320Sstevel@tonic-gate break; 25330Sstevel@tonic-gate } 25340Sstevel@tonic-gate } 25350Sstevel@tonic-gate 25360Sstevel@tonic-gate /* 25370Sstevel@tonic-gate * This is true for a stripped executable. In the case of 25380Sstevel@tonic-gate * 'unix' it can be stripped but it still contains the SHT_DYNSYM, 25390Sstevel@tonic-gate * and since that symbol information is still present everything 25400Sstevel@tonic-gate * is just fine. 25410Sstevel@tonic-gate */ 25420Sstevel@tonic-gate if (!dosymtab) { 25430Sstevel@tonic-gate if (mp->flags & KOBJ_EXEC) 25440Sstevel@tonic-gate return (0); 25450Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_syms: %s ", 25463912Slling mp->filename); 25470Sstevel@tonic-gate _kobj_printf(ops, "no SHT_SYMTAB symbol table found\n"); 25480Sstevel@tonic-gate return (-1); 25490Sstevel@tonic-gate } 25500Sstevel@tonic-gate 25510Sstevel@tonic-gate /* 25520Sstevel@tonic-gate * get the associated string table header 25530Sstevel@tonic-gate */ 25540Sstevel@tonic-gate if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum)) 25550Sstevel@tonic-gate return (-1); 25560Sstevel@tonic-gate mp->strhdr = (Shdr *) 25573912Slling (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 25580Sstevel@tonic-gate 25590Sstevel@tonic-gate mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 25600Sstevel@tonic-gate mp->hashsize = kobj_gethashsize(mp->nsyms); 25610Sstevel@tonic-gate 25620Sstevel@tonic-gate /* 25630Sstevel@tonic-gate * Allocate space for the symbol table, buckets, chains, and strings. 25640Sstevel@tonic-gate */ 25650Sstevel@tonic-gate mp->symsize = mp->symhdr->sh_size + 25660Sstevel@tonic-gate (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size; 25670Sstevel@tonic-gate mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH); 25680Sstevel@tonic-gate 25690Sstevel@tonic-gate mp->symtbl = mp->symspace; 25700Sstevel@tonic-gate mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size); 25710Sstevel@tonic-gate mp->chains = mp->buckets + mp->hashsize; 25720Sstevel@tonic-gate mp->strings = (char *)(mp->chains + mp->nsyms); 25730Sstevel@tonic-gate 25740Sstevel@tonic-gate if (kobj_read_file(file, mp->symtbl, 25750Sstevel@tonic-gate mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 || 25760Sstevel@tonic-gate kobj_read_file(file, mp->strings, 25770Sstevel@tonic-gate mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0) 25780Sstevel@tonic-gate return (-1); 25790Sstevel@tonic-gate 25800Sstevel@tonic-gate /* 25810Sstevel@tonic-gate * loop through the symbol table adjusting values to account 25820Sstevel@tonic-gate * for where each section got loaded into memory. Also 25830Sstevel@tonic-gate * fill in the hash table. 25840Sstevel@tonic-gate */ 25850Sstevel@tonic-gate for (i = 1; i < mp->nsyms; i++) { 25860Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 25870Sstevel@tonic-gate if (sp->st_shndx < SHN_LORESERVE) { 25880Sstevel@tonic-gate if (sp->st_shndx >= mp->hdr.e_shnum) { 25890Sstevel@tonic-gate _kobj_printf(ops, "%s bad shndx ", 25900Sstevel@tonic-gate file->_name); 25910Sstevel@tonic-gate _kobj_printf(ops, "in symbol %d\n", i); 25920Sstevel@tonic-gate return (-1); 25930Sstevel@tonic-gate } 25940Sstevel@tonic-gate shp = (Shdr *) 25950Sstevel@tonic-gate (mp->shdrs + 25960Sstevel@tonic-gate sp->st_shndx * mp->hdr.e_shentsize); 25970Sstevel@tonic-gate if (!(mp->flags & KOBJ_EXEC)) 25980Sstevel@tonic-gate sp->st_value += shp->sh_addr; 25990Sstevel@tonic-gate } 26000Sstevel@tonic-gate 26010Sstevel@tonic-gate if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 26020Sstevel@tonic-gate continue; 26030Sstevel@tonic-gate if (sp->st_name >= mp->strhdr->sh_size) 26040Sstevel@tonic-gate return (-1); 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate symname = mp->strings + sp->st_name; 26070Sstevel@tonic-gate 26080Sstevel@tonic-gate if (!(mp->flags & KOBJ_EXEC) && 26090Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 26100Sstevel@tonic-gate ksp = kobj_lookup_all(mp, symname, 0); 26110Sstevel@tonic-gate 26120Sstevel@tonic-gate if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL && 26130Sstevel@tonic-gate !kobj_suppress_warning(symname) && 26140Sstevel@tonic-gate sp->st_shndx != SHN_UNDEF && 26150Sstevel@tonic-gate sp->st_shndx != SHN_COMMON && 26160Sstevel@tonic-gate ksp->st_shndx != SHN_UNDEF && 26170Sstevel@tonic-gate ksp->st_shndx != SHN_COMMON) { 26180Sstevel@tonic-gate /* 26190Sstevel@tonic-gate * Unless this symbol is a stub, it's multiply 26200Sstevel@tonic-gate * defined. Multiply-defined symbols are 26210Sstevel@tonic-gate * usually bad, but some objects (kmdb) have 26220Sstevel@tonic-gate * a legitimate need to have their own 26230Sstevel@tonic-gate * copies of common functions. 26240Sstevel@tonic-gate */ 26250Sstevel@tonic-gate if ((standalone || 26260Sstevel@tonic-gate ksp->st_value < (uintptr_t)stubs_base || 26270Sstevel@tonic-gate ksp->st_value >= (uintptr_t)stubs_end) && 26280Sstevel@tonic-gate !(mp->flags & KOBJ_IGNMULDEF)) { 26290Sstevel@tonic-gate _kobj_printf(ops, 26300Sstevel@tonic-gate "%s symbol ", file->_name); 26310Sstevel@tonic-gate _kobj_printf(ops, 26320Sstevel@tonic-gate "%s multiply defined\n", symname); 26330Sstevel@tonic-gate } 26340Sstevel@tonic-gate } 26350Sstevel@tonic-gate } 26363446Smrj 26370Sstevel@tonic-gate sym_insert(mp, symname, i); 26380Sstevel@tonic-gate } 26390Sstevel@tonic-gate 26400Sstevel@tonic-gate return (0); 26410Sstevel@tonic-gate } 26420Sstevel@tonic-gate 26430Sstevel@tonic-gate static int 26440Sstevel@tonic-gate get_ctf(struct module *mp, struct _buf *file) 26450Sstevel@tonic-gate { 26460Sstevel@tonic-gate char *shstrtab, *ctfdata; 26470Sstevel@tonic-gate size_t shstrlen; 26480Sstevel@tonic-gate Shdr *shp; 26490Sstevel@tonic-gate uint_t i; 26500Sstevel@tonic-gate 26510Sstevel@tonic-gate if (_moddebug & MODDEBUG_NOCTF) 26520Sstevel@tonic-gate return (0); /* do not attempt to even load CTF data */ 26530Sstevel@tonic-gate 26540Sstevel@tonic-gate if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) { 26550Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_ctf: %s, ", 26560Sstevel@tonic-gate mp->filename); 26570Sstevel@tonic-gate _kobj_printf(ops, "corrupt e_shstrndx %u\n", 26580Sstevel@tonic-gate mp->hdr.e_shstrndx); 26590Sstevel@tonic-gate return (-1); 26600Sstevel@tonic-gate } 26610Sstevel@tonic-gate 26620Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize); 26630Sstevel@tonic-gate shstrlen = shp->sh_size; 26640Sstevel@tonic-gate shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP); 26650Sstevel@tonic-gate 26660Sstevel@tonic-gate if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) { 26670Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_ctf: %s, ", 26680Sstevel@tonic-gate mp->filename); 26690Sstevel@tonic-gate _kobj_printf(ops, "error reading section %u\n", 26700Sstevel@tonic-gate mp->hdr.e_shstrndx); 26710Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 26720Sstevel@tonic-gate return (-1); 26730Sstevel@tonic-gate } 26740Sstevel@tonic-gate 26750Sstevel@tonic-gate for (i = 0; i < mp->hdr.e_shnum; i++) { 26760Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize); 26770Sstevel@tonic-gate 26780Sstevel@tonic-gate if (shp->sh_size != 0 && shp->sh_name < shstrlen && 26790Sstevel@tonic-gate strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) { 26800Sstevel@tonic-gate ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH); 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate if (kobj_read_file(file, ctfdata, shp->sh_size, 26830Sstevel@tonic-gate shp->sh_offset) < 0) { 26840Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_ctf: %s, error " 26850Sstevel@tonic-gate "reading .SUNW_ctf data\n", mp->filename); 26860Sstevel@tonic-gate kobj_free(ctfdata, shp->sh_size); 26870Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 26880Sstevel@tonic-gate return (-1); 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate 26910Sstevel@tonic-gate mp->ctfdata = ctfdata; 26920Sstevel@tonic-gate mp->ctfsize = shp->sh_size; 26930Sstevel@tonic-gate break; 26940Sstevel@tonic-gate } 26950Sstevel@tonic-gate } 26960Sstevel@tonic-gate 26970Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 26980Sstevel@tonic-gate return (0); 26990Sstevel@tonic-gate } 27000Sstevel@tonic-gate 27010Sstevel@tonic-gate #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */ 27020Sstevel@tonic-gate 27030Sstevel@tonic-gate /* 27040Sstevel@tonic-gate * Return the hash of the ELF sections that are memory resident. 27050Sstevel@tonic-gate * i.e. text and data. We skip a SHT_NOBITS section since it occupies 27060Sstevel@tonic-gate * no space in the file. We use SHA1 here since libelfsign uses 27070Sstevel@tonic-gate * it and both places need to use the same algorithm. 27080Sstevel@tonic-gate */ 27090Sstevel@tonic-gate static void 27100Sstevel@tonic-gate crypto_es_hash(struct module *mp, char *hash, char *shstrtab) 27110Sstevel@tonic-gate { 27120Sstevel@tonic-gate uint_t shn; 27130Sstevel@tonic-gate Shdr *shp; 27140Sstevel@tonic-gate SHA1_CTX ctx; 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate SHA1Init(&ctx); 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 27190Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 27200Sstevel@tonic-gate if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0) 27210Sstevel@tonic-gate continue; 27220Sstevel@tonic-gate 27230Sstevel@tonic-gate /* 27240Sstevel@tonic-gate * The check should ideally be shp->sh_type == SHT_NOBITS. 27250Sstevel@tonic-gate * However, we can't do that check here as get_progbits() 27260Sstevel@tonic-gate * resets the type. 27270Sstevel@tonic-gate */ 27280Sstevel@tonic-gate if (strcmp(shstrtab + shp->sh_name, ".bss") == 0) 27290Sstevel@tonic-gate continue; 27300Sstevel@tonic-gate #ifdef KOBJ_DEBUG 27310Sstevel@tonic-gate if (kobj_debug & D_DEBUG) 27320Sstevel@tonic-gate _kobj_printf(ops, 27330Sstevel@tonic-gate "krtld: crypto_es_hash: updating hash with" 27340Sstevel@tonic-gate " %s data size=%d\n", shstrtab + shp->sh_name, 27353912Slling shp->sh_size); 27360Sstevel@tonic-gate #endif 27370Sstevel@tonic-gate ASSERT(shp->sh_addr != NULL); 27380Sstevel@tonic-gate SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size); 27390Sstevel@tonic-gate } 27400Sstevel@tonic-gate 27410Sstevel@tonic-gate SHA1Final((uchar_t *)hash, &ctx); 27420Sstevel@tonic-gate } 27430Sstevel@tonic-gate 27440Sstevel@tonic-gate /* 27450Sstevel@tonic-gate * Get the .SUNW_signature section for the module, it it exists. 27460Sstevel@tonic-gate * 27470Sstevel@tonic-gate * This section exists only for crypto modules. None of the 27480Sstevel@tonic-gate * primary modules have this section currently. 27490Sstevel@tonic-gate */ 27500Sstevel@tonic-gate static void 27510Sstevel@tonic-gate get_signature(struct module *mp, struct _buf *file) 27520Sstevel@tonic-gate { 27530Sstevel@tonic-gate char *shstrtab, *sigdata = NULL; 27540Sstevel@tonic-gate size_t shstrlen; 27550Sstevel@tonic-gate Shdr *shp; 27560Sstevel@tonic-gate uint_t i; 27570Sstevel@tonic-gate 27580Sstevel@tonic-gate if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) { 27590Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_signature: %s, ", 27600Sstevel@tonic-gate mp->filename); 27610Sstevel@tonic-gate _kobj_printf(ops, "corrupt e_shstrndx %u\n", 27620Sstevel@tonic-gate mp->hdr.e_shstrndx); 27630Sstevel@tonic-gate return; 27640Sstevel@tonic-gate } 27650Sstevel@tonic-gate 27660Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize); 27670Sstevel@tonic-gate shstrlen = shp->sh_size; 27680Sstevel@tonic-gate shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP); 27690Sstevel@tonic-gate 27700Sstevel@tonic-gate if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) { 27710Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_signature: %s, ", 27720Sstevel@tonic-gate mp->filename); 27730Sstevel@tonic-gate _kobj_printf(ops, "error reading section %u\n", 27740Sstevel@tonic-gate mp->hdr.e_shstrndx); 27750Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 27760Sstevel@tonic-gate return; 27770Sstevel@tonic-gate } 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate for (i = 0; i < mp->hdr.e_shnum; i++) { 27800Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize); 27810Sstevel@tonic-gate if (shp->sh_size != 0 && shp->sh_name < shstrlen && 27820Sstevel@tonic-gate strcmp(shstrtab + shp->sh_name, 27830Sstevel@tonic-gate ELF_SIGNATURE_SECTION) == 0) { 27840Sstevel@tonic-gate filesig_vers_t filesig_version; 27850Sstevel@tonic-gate size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH; 27860Sstevel@tonic-gate sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH); 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate if (kobj_read_file(file, sigdata, shp->sh_size, 27890Sstevel@tonic-gate shp->sh_offset) < 0) { 27900Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_signature: %s," 27910Sstevel@tonic-gate " error reading .SUNW_signature data\n", 27920Sstevel@tonic-gate mp->filename); 27930Sstevel@tonic-gate kobj_free(sigdata, sigsize); 27940Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 27950Sstevel@tonic-gate return; 27960Sstevel@tonic-gate } 27970Sstevel@tonic-gate filesig_version = ((struct filesignatures *)sigdata)-> 27980Sstevel@tonic-gate filesig_sig.filesig_version; 27990Sstevel@tonic-gate if (!(filesig_version == FILESIG_VERSION1 || 28000Sstevel@tonic-gate filesig_version == FILESIG_VERSION3)) { 28010Sstevel@tonic-gate /* skip versions we don't understand */ 28020Sstevel@tonic-gate kobj_free(sigdata, sigsize); 28030Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 28040Sstevel@tonic-gate return; 28050Sstevel@tonic-gate } 28060Sstevel@tonic-gate 28070Sstevel@tonic-gate mp->sigdata = sigdata; 28080Sstevel@tonic-gate mp->sigsize = sigsize; 28090Sstevel@tonic-gate break; 28100Sstevel@tonic-gate } 28110Sstevel@tonic-gate } 28120Sstevel@tonic-gate 28130Sstevel@tonic-gate if (sigdata != NULL) { 28140Sstevel@tonic-gate crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab); 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 28180Sstevel@tonic-gate } 28190Sstevel@tonic-gate 28200Sstevel@tonic-gate static void 28210Sstevel@tonic-gate add_dependent(struct module *mp, struct module *dep) 28220Sstevel@tonic-gate { 28230Sstevel@tonic-gate struct module_list *lp; 28240Sstevel@tonic-gate 28250Sstevel@tonic-gate for (lp = mp->head; lp; lp = lp->next) { 28260Sstevel@tonic-gate if (lp->mp == dep) 28270Sstevel@tonic-gate return; /* already on the list */ 28280Sstevel@tonic-gate } 28290Sstevel@tonic-gate 28300Sstevel@tonic-gate if (lp == NULL) { 28310Sstevel@tonic-gate lp = kobj_zalloc(sizeof (*lp), KM_WAIT); 28320Sstevel@tonic-gate 28330Sstevel@tonic-gate lp->mp = dep; 28340Sstevel@tonic-gate lp->next = NULL; 28350Sstevel@tonic-gate if (mp->tail) 28360Sstevel@tonic-gate mp->tail->next = lp; 28370Sstevel@tonic-gate else 28380Sstevel@tonic-gate mp->head = lp; 28390Sstevel@tonic-gate mp->tail = lp; 28400Sstevel@tonic-gate } 28410Sstevel@tonic-gate } 28420Sstevel@tonic-gate 28430Sstevel@tonic-gate static int 28440Sstevel@tonic-gate do_dependents(struct modctl *modp, char *modname, size_t modnamelen) 28450Sstevel@tonic-gate { 28460Sstevel@tonic-gate struct module *mp; 28470Sstevel@tonic-gate struct modctl *req; 28480Sstevel@tonic-gate char *d, *p, *q; 28490Sstevel@tonic-gate int c; 28500Sstevel@tonic-gate char *err_modname = NULL; 28510Sstevel@tonic-gate 28520Sstevel@tonic-gate mp = modp->mod_mp; 28530Sstevel@tonic-gate 28540Sstevel@tonic-gate if ((p = mp->depends_on) == NULL) 28550Sstevel@tonic-gate return (0); 28560Sstevel@tonic-gate 28570Sstevel@tonic-gate for (;;) { 28580Sstevel@tonic-gate /* 28590Sstevel@tonic-gate * Skip space. 28600Sstevel@tonic-gate */ 28610Sstevel@tonic-gate while (*p && (*p == ' ' || *p == '\t')) 28620Sstevel@tonic-gate p++; 28630Sstevel@tonic-gate /* 28640Sstevel@tonic-gate * Get module name. 28650Sstevel@tonic-gate */ 28660Sstevel@tonic-gate d = p; 28670Sstevel@tonic-gate q = modname; 28680Sstevel@tonic-gate c = 0; 28690Sstevel@tonic-gate while (*p && *p != ' ' && *p != '\t') { 28700Sstevel@tonic-gate if (c < modnamelen - 1) { 28710Sstevel@tonic-gate *q++ = *p; 28720Sstevel@tonic-gate c++; 28730Sstevel@tonic-gate } 28740Sstevel@tonic-gate p++; 28750Sstevel@tonic-gate } 28760Sstevel@tonic-gate 28770Sstevel@tonic-gate if (q == modname) 28780Sstevel@tonic-gate break; 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate if (c == modnamelen - 1) { 28810Sstevel@tonic-gate char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP); 28820Sstevel@tonic-gate 28830Sstevel@tonic-gate (void) strncpy(dep, d, p - d + 1); 28840Sstevel@tonic-gate dep[p - d] = '\0'; 28850Sstevel@tonic-gate 28860Sstevel@tonic-gate _kobj_printf(ops, "%s: dependency ", modp->mod_modname); 28870Sstevel@tonic-gate _kobj_printf(ops, "'%s' too long ", dep); 28880Sstevel@tonic-gate _kobj_printf(ops, "(max %d chars)\n", modnamelen); 28890Sstevel@tonic-gate 28900Sstevel@tonic-gate kobj_free(dep, p - d + 1); 28910Sstevel@tonic-gate 28920Sstevel@tonic-gate return (-1); 28930Sstevel@tonic-gate } 28940Sstevel@tonic-gate 28950Sstevel@tonic-gate *q = '\0'; 28960Sstevel@tonic-gate if ((req = mod_load_requisite(modp, modname)) == NULL) { 28970Sstevel@tonic-gate #ifndef KOBJ_DEBUG 28980Sstevel@tonic-gate if (_moddebug & MODDEBUG_LOADMSG) { 28990Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 29000Sstevel@tonic-gate _kobj_printf(ops, 29010Sstevel@tonic-gate "%s: unable to resolve dependency, ", 29020Sstevel@tonic-gate modp->mod_modname); 29030Sstevel@tonic-gate _kobj_printf(ops, "cannot load module '%s'\n", 29040Sstevel@tonic-gate modname); 29050Sstevel@tonic-gate #ifndef KOBJ_DEBUG 29060Sstevel@tonic-gate } 29070Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 29080Sstevel@tonic-gate if (err_modname == NULL) { 29090Sstevel@tonic-gate /* 29100Sstevel@tonic-gate * This must be the same size as the modname 29110Sstevel@tonic-gate * one. 29120Sstevel@tonic-gate */ 29130Sstevel@tonic-gate err_modname = kobj_zalloc(MODMAXNAMELEN, 29140Sstevel@tonic-gate KM_WAIT); 29150Sstevel@tonic-gate 29160Sstevel@tonic-gate /* 29170Sstevel@tonic-gate * We can use strcpy() here without fearing 29180Sstevel@tonic-gate * the NULL terminator because the size of 29190Sstevel@tonic-gate * err_modname is the same as one of modname, 29200Sstevel@tonic-gate * and it's filled with zeros. 29210Sstevel@tonic-gate */ 29220Sstevel@tonic-gate (void) strcpy(err_modname, modname); 29230Sstevel@tonic-gate } 29240Sstevel@tonic-gate continue; 29250Sstevel@tonic-gate } 29260Sstevel@tonic-gate 29270Sstevel@tonic-gate add_dependent(mp, req->mod_mp); 29280Sstevel@tonic-gate mod_release_mod(req); 29290Sstevel@tonic-gate 29300Sstevel@tonic-gate } 29310Sstevel@tonic-gate 29320Sstevel@tonic-gate if (err_modname != NULL) { 29330Sstevel@tonic-gate /* 29340Sstevel@tonic-gate * Copy the first module name where you detect an error to keep 29350Sstevel@tonic-gate * its behavior the same as before. 29360Sstevel@tonic-gate * This way keeps minimizing the memory use for error 29370Sstevel@tonic-gate * modules, and this might be important at boot time because 29380Sstevel@tonic-gate * the memory usage is a crucial factor for booting in most 29390Sstevel@tonic-gate * cases. You can expect more verbose messages when using 29400Sstevel@tonic-gate * a debug kernel or setting a bit in moddebug. 29410Sstevel@tonic-gate */ 29420Sstevel@tonic-gate bzero(modname, MODMAXNAMELEN); 29430Sstevel@tonic-gate (void) strcpy(modname, err_modname); 29440Sstevel@tonic-gate kobj_free(err_modname, MODMAXNAMELEN); 29450Sstevel@tonic-gate return (-1); 29460Sstevel@tonic-gate } 29470Sstevel@tonic-gate 29480Sstevel@tonic-gate return (0); 29490Sstevel@tonic-gate } 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate static int 29520Sstevel@tonic-gate do_common(struct module *mp) 29530Sstevel@tonic-gate { 29540Sstevel@tonic-gate int err; 29550Sstevel@tonic-gate 29560Sstevel@tonic-gate /* 29570Sstevel@tonic-gate * first time through, assign all symbols defined in other 29580Sstevel@tonic-gate * modules, and count up how much common space will be needed 29590Sstevel@tonic-gate * (bss_size and bss_align) 29600Sstevel@tonic-gate */ 29610Sstevel@tonic-gate if ((err = do_symbols(mp, 0)) < 0) 29620Sstevel@tonic-gate return (err); 29630Sstevel@tonic-gate /* 29640Sstevel@tonic-gate * increase bss_size by the maximum delta that could be 29650Sstevel@tonic-gate * computed by the ALIGN below 29660Sstevel@tonic-gate */ 29670Sstevel@tonic-gate mp->bss_size += mp->bss_align; 29680Sstevel@tonic-gate if (mp->bss_size) { 29690Sstevel@tonic-gate if (standalone) 29700Sstevel@tonic-gate mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size, 29710Sstevel@tonic-gate MINALIGN, 0); 29720Sstevel@tonic-gate else 29730Sstevel@tonic-gate mp->bss = (uintptr_t)vmem_alloc(data_arena, 29740Sstevel@tonic-gate mp->bss_size, VM_SLEEP | VM_BESTFIT); 29750Sstevel@tonic-gate bzero((void *)mp->bss, mp->bss_size); 29760Sstevel@tonic-gate /* now assign addresses to all common symbols */ 29770Sstevel@tonic-gate if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0) 29780Sstevel@tonic-gate return (err); 29790Sstevel@tonic-gate } 29800Sstevel@tonic-gate return (0); 29810Sstevel@tonic-gate } 29820Sstevel@tonic-gate 29830Sstevel@tonic-gate static int 29840Sstevel@tonic-gate do_symbols(struct module *mp, Elf64_Addr bss_base) 29850Sstevel@tonic-gate { 29860Sstevel@tonic-gate int bss_align; 29870Sstevel@tonic-gate uintptr_t bss_ptr; 29880Sstevel@tonic-gate int err; 29890Sstevel@tonic-gate int i; 29900Sstevel@tonic-gate Sym *sp, *sp1; 29910Sstevel@tonic-gate char *name; 29920Sstevel@tonic-gate int assign; 29930Sstevel@tonic-gate int resolved = 1; 29940Sstevel@tonic-gate 29950Sstevel@tonic-gate /* 29960Sstevel@tonic-gate * Nothing left to do (optimization). 29970Sstevel@tonic-gate */ 29980Sstevel@tonic-gate if (mp->flags & KOBJ_RESOLVED) 29990Sstevel@tonic-gate return (0); 30000Sstevel@tonic-gate 30010Sstevel@tonic-gate assign = (bss_base) ? 1 : 0; 30020Sstevel@tonic-gate bss_ptr = bss_base; 30030Sstevel@tonic-gate bss_align = 0; 30040Sstevel@tonic-gate err = 0; 30050Sstevel@tonic-gate 30060Sstevel@tonic-gate for (i = 1; i < mp->nsyms; i++) { 30070Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i); 30080Sstevel@tonic-gate /* 30090Sstevel@tonic-gate * we know that st_name is in bounds, since get_sections 30100Sstevel@tonic-gate * has already checked all of the symbols 30110Sstevel@tonic-gate */ 30120Sstevel@tonic-gate name = mp->strings + sp->st_name; 30130Sstevel@tonic-gate if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON) 30140Sstevel@tonic-gate continue; 30150Sstevel@tonic-gate #ifdef __sparc 30160Sstevel@tonic-gate /* 30170Sstevel@tonic-gate * Register symbols are ignored in the kernel 30180Sstevel@tonic-gate */ 30190Sstevel@tonic-gate if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) { 30200Sstevel@tonic-gate if (*name != '\0') { 30210Sstevel@tonic-gate _kobj_printf(ops, "%s: named REGISTER symbol ", 30223912Slling mp->filename); 30230Sstevel@tonic-gate _kobj_printf(ops, "not supported '%s'\n", 30243912Slling name); 30250Sstevel@tonic-gate err = DOSYM_UNDEF; 30260Sstevel@tonic-gate } 30270Sstevel@tonic-gate continue; 30280Sstevel@tonic-gate } 30290Sstevel@tonic-gate #endif /* __sparc */ 30300Sstevel@tonic-gate /* 30310Sstevel@tonic-gate * TLS symbols are ignored in the kernel 30320Sstevel@tonic-gate */ 30330Sstevel@tonic-gate if (ELF_ST_TYPE(sp->st_info) == STT_TLS) { 30340Sstevel@tonic-gate _kobj_printf(ops, "%s: TLS symbol ", 30353912Slling mp->filename); 30360Sstevel@tonic-gate _kobj_printf(ops, "not supported '%s'\n", 30373912Slling name); 30380Sstevel@tonic-gate err = DOSYM_UNDEF; 30390Sstevel@tonic-gate continue; 30400Sstevel@tonic-gate } 30410Sstevel@tonic-gate 30420Sstevel@tonic-gate if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) { 30430Sstevel@tonic-gate if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) { 30440Sstevel@tonic-gate sp->st_shndx = SHN_ABS; 30450Sstevel@tonic-gate sp->st_value = sp1->st_value; 30460Sstevel@tonic-gate continue; 30470Sstevel@tonic-gate } 30480Sstevel@tonic-gate } 30490Sstevel@tonic-gate 30500Sstevel@tonic-gate if (sp->st_shndx == SHN_UNDEF) { 30510Sstevel@tonic-gate resolved = 0; 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0) 30540Sstevel@tonic-gate continue; 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate /* 30570Sstevel@tonic-gate * If it's not a weak reference and it's 30580Sstevel@tonic-gate * not a primary object, it's an error. 30590Sstevel@tonic-gate * (Primary objects may take more than 30600Sstevel@tonic-gate * one pass to resolve) 30610Sstevel@tonic-gate */ 30620Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM) && 30630Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) != STB_WEAK) { 30640Sstevel@tonic-gate _kobj_printf(ops, "%s: undefined symbol", 30650Sstevel@tonic-gate mp->filename); 30660Sstevel@tonic-gate _kobj_printf(ops, " '%s'\n", name); 30670Sstevel@tonic-gate /* 30680Sstevel@tonic-gate * Try to determine whether this symbol 30690Sstevel@tonic-gate * represents a dependency on obsolete 30700Sstevel@tonic-gate * unsafe driver support. This is just 30710Sstevel@tonic-gate * to make the warning more informative. 30720Sstevel@tonic-gate */ 30730Sstevel@tonic-gate if (strcmp(name, "sleep") == 0 || 30740Sstevel@tonic-gate strcmp(name, "unsleep") == 0 || 30750Sstevel@tonic-gate strcmp(name, "wakeup") == 0 || 30760Sstevel@tonic-gate strcmp(name, "bsd_compat_ioctl") == 0 || 30770Sstevel@tonic-gate strcmp(name, "unsafe_driver") == 0 || 30780Sstevel@tonic-gate strncmp(name, "spl", 3) == 0 || 30790Sstevel@tonic-gate strncmp(name, "i_ddi_spl", 9) == 0) 30800Sstevel@tonic-gate err = DOSYM_UNSAFE; 30810Sstevel@tonic-gate if (err == 0) 30820Sstevel@tonic-gate err = DOSYM_UNDEF; 30830Sstevel@tonic-gate } 30840Sstevel@tonic-gate continue; 30850Sstevel@tonic-gate } 30860Sstevel@tonic-gate /* 30870Sstevel@tonic-gate * It's a common symbol - st_value is the 30880Sstevel@tonic-gate * required alignment. 30890Sstevel@tonic-gate */ 30900Sstevel@tonic-gate if (sp->st_value > bss_align) 30910Sstevel@tonic-gate bss_align = sp->st_value; 30920Sstevel@tonic-gate bss_ptr = ALIGN(bss_ptr, sp->st_value); 30930Sstevel@tonic-gate if (assign) { 30940Sstevel@tonic-gate sp->st_shndx = SHN_ABS; 30950Sstevel@tonic-gate sp->st_value = bss_ptr; 30960Sstevel@tonic-gate } 30970Sstevel@tonic-gate bss_ptr += sp->st_size; 30980Sstevel@tonic-gate } 30990Sstevel@tonic-gate if (err) 31000Sstevel@tonic-gate return (err); 31010Sstevel@tonic-gate if (assign == 0 && mp->bss == NULL) { 31020Sstevel@tonic-gate mp->bss_align = bss_align; 31030Sstevel@tonic-gate mp->bss_size = bss_ptr; 31040Sstevel@tonic-gate } else if (resolved) { 31050Sstevel@tonic-gate mp->flags |= KOBJ_RESOLVED; 31060Sstevel@tonic-gate } 31070Sstevel@tonic-gate 31080Sstevel@tonic-gate return (0); 31090Sstevel@tonic-gate } 31100Sstevel@tonic-gate 31110Sstevel@tonic-gate uint_t 31120Sstevel@tonic-gate kobj_hash_name(const char *p) 31130Sstevel@tonic-gate { 31140Sstevel@tonic-gate unsigned int g; 31150Sstevel@tonic-gate uint_t hval; 31160Sstevel@tonic-gate 31170Sstevel@tonic-gate hval = 0; 31180Sstevel@tonic-gate while (*p) { 31190Sstevel@tonic-gate hval = (hval << 4) + *p++; 31200Sstevel@tonic-gate if ((g = (hval & 0xf0000000)) != 0) 31210Sstevel@tonic-gate hval ^= g >> 24; 31220Sstevel@tonic-gate hval &= ~g; 31230Sstevel@tonic-gate } 31240Sstevel@tonic-gate return (hval); 31250Sstevel@tonic-gate } 31260Sstevel@tonic-gate 31270Sstevel@tonic-gate /* look for name in all modules */ 31280Sstevel@tonic-gate uintptr_t 31290Sstevel@tonic-gate kobj_getsymvalue(char *name, int kernelonly) 31300Sstevel@tonic-gate { 31310Sstevel@tonic-gate Sym *sp; 31320Sstevel@tonic-gate struct modctl *modp; 31330Sstevel@tonic-gate struct module *mp; 31340Sstevel@tonic-gate uintptr_t value = 0; 31350Sstevel@tonic-gate 31360Sstevel@tonic-gate if ((sp = kobj_lookup_kernel(name)) != NULL) 31370Sstevel@tonic-gate return ((uintptr_t)sp->st_value); 31380Sstevel@tonic-gate 31390Sstevel@tonic-gate if (kernelonly) 31400Sstevel@tonic-gate return (0); /* didn't find it in the kernel so give up */ 31410Sstevel@tonic-gate 31420Sstevel@tonic-gate mutex_enter(&mod_lock); 31430Sstevel@tonic-gate modp = &modules; 31440Sstevel@tonic-gate do { 31450Sstevel@tonic-gate mp = (struct module *)modp->mod_mp; 31460Sstevel@tonic-gate if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded && 31470Sstevel@tonic-gate (sp = lookup_one(mp, name))) { 31480Sstevel@tonic-gate value = (uintptr_t)sp->st_value; 31490Sstevel@tonic-gate break; 31500Sstevel@tonic-gate } 31510Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 31520Sstevel@tonic-gate mutex_exit(&mod_lock); 31530Sstevel@tonic-gate return (value); 31540Sstevel@tonic-gate } 31550Sstevel@tonic-gate 31560Sstevel@tonic-gate /* look for a symbol near value. */ 31570Sstevel@tonic-gate char * 31580Sstevel@tonic-gate kobj_getsymname(uintptr_t value, ulong_t *offset) 31590Sstevel@tonic-gate { 31600Sstevel@tonic-gate char *name = NULL; 31610Sstevel@tonic-gate struct modctl *modp; 31620Sstevel@tonic-gate 31630Sstevel@tonic-gate struct modctl_list *lp; 31640Sstevel@tonic-gate struct module *mp; 31650Sstevel@tonic-gate 31660Sstevel@tonic-gate /* 31670Sstevel@tonic-gate * Loop through the primary kernel modules. 31680Sstevel@tonic-gate */ 31690Sstevel@tonic-gate for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) { 31700Sstevel@tonic-gate mp = mod(lp); 31710Sstevel@tonic-gate 31720Sstevel@tonic-gate if ((name = kobj_searchsym(mp, value, offset)) != NULL) 31730Sstevel@tonic-gate return (name); 31740Sstevel@tonic-gate } 31750Sstevel@tonic-gate 31760Sstevel@tonic-gate mutex_enter(&mod_lock); 31770Sstevel@tonic-gate modp = &modules; 31780Sstevel@tonic-gate do { 31790Sstevel@tonic-gate mp = (struct module *)modp->mod_mp; 31800Sstevel@tonic-gate if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded && 31810Sstevel@tonic-gate (name = kobj_searchsym(mp, value, offset))) 31820Sstevel@tonic-gate break; 31830Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 31840Sstevel@tonic-gate mutex_exit(&mod_lock); 31850Sstevel@tonic-gate return (name); 31860Sstevel@tonic-gate } 31870Sstevel@tonic-gate 31880Sstevel@tonic-gate /* return address of symbol and size */ 31890Sstevel@tonic-gate 31900Sstevel@tonic-gate uintptr_t 31910Sstevel@tonic-gate kobj_getelfsym(char *name, void *mp, int *size) 31920Sstevel@tonic-gate { 31930Sstevel@tonic-gate Sym *sp; 31940Sstevel@tonic-gate 31950Sstevel@tonic-gate if (mp == NULL) 31960Sstevel@tonic-gate sp = kobj_lookup_kernel(name); 31970Sstevel@tonic-gate else 31980Sstevel@tonic-gate sp = lookup_one(mp, name); 31990Sstevel@tonic-gate 32000Sstevel@tonic-gate if (sp == NULL) 32010Sstevel@tonic-gate return (0); 32020Sstevel@tonic-gate 32030Sstevel@tonic-gate *size = (int)sp->st_size; 32040Sstevel@tonic-gate return ((uintptr_t)sp->st_value); 32050Sstevel@tonic-gate } 32060Sstevel@tonic-gate 32070Sstevel@tonic-gate uintptr_t 32081414Scindi kobj_lookup(struct module *mod, const char *name) 32090Sstevel@tonic-gate { 32100Sstevel@tonic-gate Sym *sp; 32110Sstevel@tonic-gate 32120Sstevel@tonic-gate sp = lookup_one(mod, name); 32130Sstevel@tonic-gate 32140Sstevel@tonic-gate if (sp == NULL) 32150Sstevel@tonic-gate return (0); 32160Sstevel@tonic-gate 32170Sstevel@tonic-gate return ((uintptr_t)sp->st_value); 32180Sstevel@tonic-gate } 32190Sstevel@tonic-gate 32200Sstevel@tonic-gate char * 32210Sstevel@tonic-gate kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset) 32220Sstevel@tonic-gate { 32230Sstevel@tonic-gate Sym *symtabptr; 32240Sstevel@tonic-gate char *strtabptr; 32250Sstevel@tonic-gate int symnum; 32260Sstevel@tonic-gate Sym *sym; 32270Sstevel@tonic-gate Sym *cursym; 32280Sstevel@tonic-gate uintptr_t curval; 32290Sstevel@tonic-gate 32300Sstevel@tonic-gate *offset = (ulong_t)-1l; /* assume not found */ 32310Sstevel@tonic-gate cursym = NULL; 32320Sstevel@tonic-gate 32330Sstevel@tonic-gate if (kobj_addrcheck(mp, (void *)value) != 0) 32340Sstevel@tonic-gate return (NULL); /* not in this module */ 32350Sstevel@tonic-gate 32360Sstevel@tonic-gate strtabptr = mp->strings; 32370Sstevel@tonic-gate symtabptr = (Sym *)mp->symtbl; 32380Sstevel@tonic-gate 32390Sstevel@tonic-gate /* 32400Sstevel@tonic-gate * Scan the module's symbol table for a symbol <= value 32410Sstevel@tonic-gate */ 32420Sstevel@tonic-gate for (symnum = 1, sym = symtabptr + 1; 32430Sstevel@tonic-gate symnum < mp->nsyms; symnum++, sym = (Sym *) 32440Sstevel@tonic-gate ((uintptr_t)sym + mp->symhdr->sh_entsize)) { 32450Sstevel@tonic-gate if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) { 32460Sstevel@tonic-gate if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 32470Sstevel@tonic-gate continue; 32480Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 32490Sstevel@tonic-gate ELF_ST_TYPE(sym->st_info) != STT_FUNC) 32500Sstevel@tonic-gate continue; 32510Sstevel@tonic-gate } 32520Sstevel@tonic-gate 32530Sstevel@tonic-gate curval = (uintptr_t)sym->st_value; 32540Sstevel@tonic-gate 32550Sstevel@tonic-gate if (curval > value) 32560Sstevel@tonic-gate continue; 32570Sstevel@tonic-gate 32580Sstevel@tonic-gate /* 32590Sstevel@tonic-gate * If one or both are functions... 32600Sstevel@tonic-gate */ 32610Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL && 32620Sstevel@tonic-gate ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) { 32630Sstevel@tonic-gate /* Ignore if the address is out of the bounds */ 32640Sstevel@tonic-gate if (value - sym->st_value >= sym->st_size) 32650Sstevel@tonic-gate continue; 32660Sstevel@tonic-gate 32670Sstevel@tonic-gate if (cursym != NULL && 32680Sstevel@tonic-gate ELF_ST_TYPE(cursym->st_info) == STT_FUNC) { 32690Sstevel@tonic-gate /* Prefer the function to the non-function */ 32700Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) 32710Sstevel@tonic-gate continue; 32720Sstevel@tonic-gate 32730Sstevel@tonic-gate /* Prefer the larger of the two functions */ 32740Sstevel@tonic-gate if (sym->st_size <= cursym->st_size) 32750Sstevel@tonic-gate continue; 32760Sstevel@tonic-gate } 32770Sstevel@tonic-gate } else if (value - curval >= *offset) { 32780Sstevel@tonic-gate continue; 32790Sstevel@tonic-gate } 32800Sstevel@tonic-gate 32810Sstevel@tonic-gate *offset = (ulong_t)(value - curval); 32820Sstevel@tonic-gate cursym = sym; 32830Sstevel@tonic-gate } 32840Sstevel@tonic-gate if (cursym == NULL) 32850Sstevel@tonic-gate return (NULL); 32860Sstevel@tonic-gate 32870Sstevel@tonic-gate return (strtabptr + cursym->st_name); 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate 32900Sstevel@tonic-gate Sym * 32910Sstevel@tonic-gate kobj_lookup_all(struct module *mp, char *name, int include_self) 32920Sstevel@tonic-gate { 32930Sstevel@tonic-gate Sym *sp; 32940Sstevel@tonic-gate struct module_list *mlp; 32950Sstevel@tonic-gate struct modctl_list *clp; 32960Sstevel@tonic-gate struct module *mmp; 32970Sstevel@tonic-gate 32980Sstevel@tonic-gate if (include_self && (sp = lookup_one(mp, name)) != NULL) 32990Sstevel@tonic-gate return (sp); 33000Sstevel@tonic-gate 33010Sstevel@tonic-gate for (mlp = mp->head; mlp; mlp = mlp->next) { 33020Sstevel@tonic-gate if ((sp = lookup_one(mlp->mp, name)) != NULL && 33030Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) != STB_LOCAL) 33040Sstevel@tonic-gate return (sp); 33050Sstevel@tonic-gate } 33060Sstevel@tonic-gate 33070Sstevel@tonic-gate /* 33080Sstevel@tonic-gate * Loop through the primary kernel modules. 33090Sstevel@tonic-gate */ 33100Sstevel@tonic-gate for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) { 33110Sstevel@tonic-gate mmp = mod(clp); 33120Sstevel@tonic-gate 33130Sstevel@tonic-gate if (mmp == NULL || mp == mmp) 33140Sstevel@tonic-gate continue; 33150Sstevel@tonic-gate 33160Sstevel@tonic-gate if ((sp = lookup_one(mmp, name)) != NULL && 33170Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) != STB_LOCAL) 33180Sstevel@tonic-gate return (sp); 33190Sstevel@tonic-gate } 33200Sstevel@tonic-gate return (NULL); 33210Sstevel@tonic-gate } 33220Sstevel@tonic-gate 33230Sstevel@tonic-gate Sym * 33240Sstevel@tonic-gate kobj_lookup_kernel(const char *name) 33250Sstevel@tonic-gate { 33260Sstevel@tonic-gate struct modctl_list *lp; 33270Sstevel@tonic-gate struct module *mp; 33280Sstevel@tonic-gate Sym *sp; 33290Sstevel@tonic-gate 33300Sstevel@tonic-gate /* 33310Sstevel@tonic-gate * Loop through the primary kernel modules. 33320Sstevel@tonic-gate */ 33330Sstevel@tonic-gate for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) { 33340Sstevel@tonic-gate mp = mod(lp); 33350Sstevel@tonic-gate 33360Sstevel@tonic-gate if (mp == NULL) 33370Sstevel@tonic-gate continue; 33380Sstevel@tonic-gate 33390Sstevel@tonic-gate if ((sp = lookup_one(mp, name)) != NULL) 33400Sstevel@tonic-gate return (sp); 33410Sstevel@tonic-gate } 33420Sstevel@tonic-gate return (NULL); 33430Sstevel@tonic-gate } 33440Sstevel@tonic-gate 33450Sstevel@tonic-gate static Sym * 33460Sstevel@tonic-gate lookup_one(struct module *mp, const char *name) 33470Sstevel@tonic-gate { 33480Sstevel@tonic-gate symid_t *ip; 33490Sstevel@tonic-gate char *name1; 33500Sstevel@tonic-gate Sym *sp; 33510Sstevel@tonic-gate 33520Sstevel@tonic-gate for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 33530Sstevel@tonic-gate ip = &mp->chains[*ip]) { 33540Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + 33550Sstevel@tonic-gate mp->symhdr->sh_entsize * *ip); 33560Sstevel@tonic-gate name1 = mp->strings + sp->st_name; 33570Sstevel@tonic-gate if (strcmp(name, name1) == 0 && 33580Sstevel@tonic-gate ELF_ST_TYPE(sp->st_info) != STT_FILE && 33590Sstevel@tonic-gate sp->st_shndx != SHN_UNDEF && 33600Sstevel@tonic-gate sp->st_shndx != SHN_COMMON) 33610Sstevel@tonic-gate return (sp); 33620Sstevel@tonic-gate } 33630Sstevel@tonic-gate return (NULL); 33640Sstevel@tonic-gate } 33650Sstevel@tonic-gate 33660Sstevel@tonic-gate /* 33670Sstevel@tonic-gate * Lookup a given symbol pointer in the module's symbol hash. If the symbol 33680Sstevel@tonic-gate * is hashed, return the symbol pointer; otherwise return NULL. 33690Sstevel@tonic-gate */ 33700Sstevel@tonic-gate static Sym * 33710Sstevel@tonic-gate sym_lookup(struct module *mp, Sym *ksp) 33720Sstevel@tonic-gate { 33730Sstevel@tonic-gate char *name = mp->strings + ksp->st_name; 33740Sstevel@tonic-gate symid_t *ip; 33750Sstevel@tonic-gate Sym *sp; 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 33780Sstevel@tonic-gate ip = &mp->chains[*ip]) { 33790Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip); 33800Sstevel@tonic-gate if (sp == ksp) 33810Sstevel@tonic-gate return (ksp); 33820Sstevel@tonic-gate } 33830Sstevel@tonic-gate return (NULL); 33840Sstevel@tonic-gate } 33850Sstevel@tonic-gate 33860Sstevel@tonic-gate static void 33870Sstevel@tonic-gate sym_insert(struct module *mp, char *name, symid_t index) 33880Sstevel@tonic-gate { 33890Sstevel@tonic-gate symid_t *ip; 33900Sstevel@tonic-gate 33910Sstevel@tonic-gate #ifdef KOBJ_DEBUG 33920Sstevel@tonic-gate if (kobj_debug & D_SYMBOLS) { 33930Sstevel@tonic-gate static struct module *lastmp = NULL; 33940Sstevel@tonic-gate Sym *sp; 33950Sstevel@tonic-gate if (lastmp != mp) { 33960Sstevel@tonic-gate _kobj_printf(ops, 33970Sstevel@tonic-gate "krtld: symbol entry: file=%s\n", 33980Sstevel@tonic-gate mp->filename); 33990Sstevel@tonic-gate _kobj_printf(ops, 34000Sstevel@tonic-gate "krtld:\tsymndx\tvalue\t\t" 34010Sstevel@tonic-gate "symbol name\n"); 34020Sstevel@tonic-gate lastmp = mp; 34030Sstevel@tonic-gate } 34040Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + 34053912Slling index * mp->symhdr->sh_entsize); 34060Sstevel@tonic-gate _kobj_printf(ops, "krtld:\t[%3d]", index); 34070Sstevel@tonic-gate _kobj_printf(ops, "\t0x%lx", sp->st_value); 34080Sstevel@tonic-gate _kobj_printf(ops, "\t%s\n", name); 34090Sstevel@tonic-gate } 34100Sstevel@tonic-gate 34110Sstevel@tonic-gate #endif 34120Sstevel@tonic-gate for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 34130Sstevel@tonic-gate ip = &mp->chains[*ip]) { 34140Sstevel@tonic-gate ; 34150Sstevel@tonic-gate } 34160Sstevel@tonic-gate *ip = index; 34170Sstevel@tonic-gate } 34180Sstevel@tonic-gate 34190Sstevel@tonic-gate struct modctl * 34200Sstevel@tonic-gate kobj_boot_mod_lookup(const char *modname) 34210Sstevel@tonic-gate { 34220Sstevel@tonic-gate struct modctl *mctl = kobj_modules; 34230Sstevel@tonic-gate 34240Sstevel@tonic-gate do { 34250Sstevel@tonic-gate if (strcmp(modname, mctl->mod_modname) == 0) 34260Sstevel@tonic-gate return (mctl); 34270Sstevel@tonic-gate } while ((mctl = mctl->mod_next) != kobj_modules); 34280Sstevel@tonic-gate 34290Sstevel@tonic-gate return (NULL); 34300Sstevel@tonic-gate } 34310Sstevel@tonic-gate 34320Sstevel@tonic-gate /* 3433309Scth * Determine if the module exists. 3434309Scth */ 3435309Scth int 3436309Scth kobj_path_exists(char *name, int use_path) 3437309Scth { 3438309Scth struct _buf *file; 3439309Scth 3440309Scth file = kobj_open_path(name, use_path, 1); 3441309Scth #ifdef MODDIR_SUFFIX 3442309Scth if (file == (struct _buf *)-1) 3443309Scth file = kobj_open_path(name, use_path, 0); 3444309Scth #endif /* MODDIR_SUFFIX */ 3445309Scth if (file == (struct _buf *)-1) 3446309Scth return (0); 3447309Scth kobj_close_file(file); 3448309Scth return (1); 3449309Scth } 3450309Scth 3451309Scth /* 34520Sstevel@tonic-gate * fullname is dynamically allocated to be able to hold the 34530Sstevel@tonic-gate * maximum size string that can be constructed from name. 34540Sstevel@tonic-gate * path is exactly like the shell PATH variable. 34550Sstevel@tonic-gate */ 34560Sstevel@tonic-gate struct _buf * 34570Sstevel@tonic-gate kobj_open_path(char *name, int use_path, int use_moddir_suffix) 34580Sstevel@tonic-gate { 34590Sstevel@tonic-gate char *p, *q; 34600Sstevel@tonic-gate char *pathp; 34610Sstevel@tonic-gate char *pathpsave; 34620Sstevel@tonic-gate char *fullname; 34630Sstevel@tonic-gate int maxpathlen; 34640Sstevel@tonic-gate struct _buf *file; 34650Sstevel@tonic-gate 34660Sstevel@tonic-gate #if !defined(MODDIR_SUFFIX) 34670Sstevel@tonic-gate use_moddir_suffix = B_FALSE; 34680Sstevel@tonic-gate #endif 34690Sstevel@tonic-gate 34700Sstevel@tonic-gate if (!use_path) 34710Sstevel@tonic-gate pathp = ""; /* use name as specified */ 34720Sstevel@tonic-gate else 34733446Smrj pathp = kobj_module_path; 34743446Smrj /* use configured default path */ 34750Sstevel@tonic-gate 34760Sstevel@tonic-gate pathpsave = pathp; /* keep this for error reporting */ 34770Sstevel@tonic-gate 34780Sstevel@tonic-gate /* 34790Sstevel@tonic-gate * Allocate enough space for the largest possible fullname. 34800Sstevel@tonic-gate * since path is of the form <directory> : <directory> : ... 34810Sstevel@tonic-gate * we're potentially allocating a little more than we need to 34820Sstevel@tonic-gate * but we'll allocate the exact amount when we find the right directory. 34830Sstevel@tonic-gate * (The + 3 below is one for NULL terminator and one for the '/' 34840Sstevel@tonic-gate * we might have to add at the beginning of path and one for 34850Sstevel@tonic-gate * the '/' between path and name.) 34860Sstevel@tonic-gate */ 34870Sstevel@tonic-gate maxpathlen = strlen(pathp) + strlen(name) + 3; 34880Sstevel@tonic-gate /* sizeof includes null */ 34890Sstevel@tonic-gate maxpathlen += sizeof (slash_moddir_suffix_slash) - 1; 34900Sstevel@tonic-gate fullname = kobj_zalloc(maxpathlen, KM_WAIT); 34910Sstevel@tonic-gate 34920Sstevel@tonic-gate for (;;) { 34930Sstevel@tonic-gate p = fullname; 34940Sstevel@tonic-gate if (*pathp != '\0' && *pathp != '/') 34950Sstevel@tonic-gate *p++ = '/'; /* path must start with '/' */ 34960Sstevel@tonic-gate while (*pathp && *pathp != ':' && *pathp != ' ') 34970Sstevel@tonic-gate *p++ = *pathp++; 34980Sstevel@tonic-gate if (p != fullname && p[-1] != '/') 34990Sstevel@tonic-gate *p++ = '/'; 35000Sstevel@tonic-gate if (use_moddir_suffix) { 35010Sstevel@tonic-gate char *b = basename(name); 35020Sstevel@tonic-gate char *s; 35030Sstevel@tonic-gate 35040Sstevel@tonic-gate /* copy everything up to the base name */ 35050Sstevel@tonic-gate q = name; 35060Sstevel@tonic-gate while (q != b && *q) 35070Sstevel@tonic-gate *p++ = *q++; 35080Sstevel@tonic-gate s = slash_moddir_suffix_slash; 35090Sstevel@tonic-gate while (*s) 35100Sstevel@tonic-gate *p++ = *s++; 35110Sstevel@tonic-gate /* copy the rest */ 35120Sstevel@tonic-gate while (*b) 35130Sstevel@tonic-gate *p++ = *b++; 35140Sstevel@tonic-gate } else { 35150Sstevel@tonic-gate q = name; 35160Sstevel@tonic-gate while (*q) 35170Sstevel@tonic-gate *p++ = *q++; 35180Sstevel@tonic-gate } 35190Sstevel@tonic-gate *p = 0; 35200Sstevel@tonic-gate if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) { 35210Sstevel@tonic-gate kobj_free(fullname, maxpathlen); 35220Sstevel@tonic-gate return (file); 35230Sstevel@tonic-gate } 35240Sstevel@tonic-gate if (*pathp == 0) 35250Sstevel@tonic-gate break; 35260Sstevel@tonic-gate pathp++; 35270Sstevel@tonic-gate } 35280Sstevel@tonic-gate kobj_free(fullname, maxpathlen); 35290Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 35300Sstevel@tonic-gate _kobj_printf(ops, "can't open %s,", name); 35310Sstevel@tonic-gate _kobj_printf(ops, " path is %s\n", pathpsave); 35320Sstevel@tonic-gate } 35330Sstevel@tonic-gate return ((struct _buf *)-1); 35340Sstevel@tonic-gate } 35350Sstevel@tonic-gate 35360Sstevel@tonic-gate intptr_t 35370Sstevel@tonic-gate kobj_open(char *filename) 35380Sstevel@tonic-gate { 35390Sstevel@tonic-gate struct vnode *vp; 35400Sstevel@tonic-gate int fd; 35410Sstevel@tonic-gate 35420Sstevel@tonic-gate if (_modrootloaded) { 35430Sstevel@tonic-gate struct kobjopen_tctl *ltp = kobjopen_alloc(filename); 35440Sstevel@tonic-gate int Errno; 35450Sstevel@tonic-gate 35460Sstevel@tonic-gate /* 35470Sstevel@tonic-gate * Hand off the open to a thread who has a 35480Sstevel@tonic-gate * stack size capable handling the request. 35490Sstevel@tonic-gate */ 35500Sstevel@tonic-gate if (curthread != &t0) { 35510Sstevel@tonic-gate (void) thread_create(NULL, DEFAULTSTKSZ * 2, 35520Sstevel@tonic-gate kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri); 35530Sstevel@tonic-gate sema_p(<p->sema); 35540Sstevel@tonic-gate Errno = ltp->Errno; 35550Sstevel@tonic-gate vp = ltp->vp; 35560Sstevel@tonic-gate } else { 35570Sstevel@tonic-gate /* 35580Sstevel@tonic-gate * 1098067: module creds should not be those of the 35590Sstevel@tonic-gate * caller 35600Sstevel@tonic-gate */ 35610Sstevel@tonic-gate cred_t *saved_cred = curthread->t_cred; 35620Sstevel@tonic-gate curthread->t_cred = kcred; 35631544Seschrock Errno = vn_openat(filename, UIO_SYSSPACE, FREAD, 0, &vp, 35641544Seschrock 0, 0, rootdir); 35650Sstevel@tonic-gate curthread->t_cred = saved_cred; 35660Sstevel@tonic-gate } 35670Sstevel@tonic-gate kobjopen_free(ltp); 35680Sstevel@tonic-gate 35690Sstevel@tonic-gate if (Errno) { 35700Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 35710Sstevel@tonic-gate _kobj_printf(ops, 35720Sstevel@tonic-gate "kobj_open: vn_open of %s fails, ", 35730Sstevel@tonic-gate filename); 35740Sstevel@tonic-gate _kobj_printf(ops, "Errno = %d\n", Errno); 35750Sstevel@tonic-gate } 35760Sstevel@tonic-gate return (-1); 35770Sstevel@tonic-gate } else { 35780Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 35790Sstevel@tonic-gate _kobj_printf(ops, "kobj_open: '%s'", filename); 35800Sstevel@tonic-gate _kobj_printf(ops, " vp = %p\n", vp); 35810Sstevel@tonic-gate } 35820Sstevel@tonic-gate return ((intptr_t)vp); 35830Sstevel@tonic-gate } 35840Sstevel@tonic-gate } else { 35850Sstevel@tonic-gate fd = kobj_boot_open(filename, 0); 35860Sstevel@tonic-gate 35870Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 35880Sstevel@tonic-gate if (fd < 0) 35890Sstevel@tonic-gate _kobj_printf(ops, 35900Sstevel@tonic-gate "kobj_open: can't open %s\n", filename); 35910Sstevel@tonic-gate else { 35920Sstevel@tonic-gate _kobj_printf(ops, "kobj_open: '%s'", filename); 35930Sstevel@tonic-gate _kobj_printf(ops, " descr = 0x%x\n", fd); 35940Sstevel@tonic-gate } 35950Sstevel@tonic-gate } 35960Sstevel@tonic-gate return ((intptr_t)fd); 35970Sstevel@tonic-gate } 35980Sstevel@tonic-gate } 35990Sstevel@tonic-gate 36000Sstevel@tonic-gate /* 36010Sstevel@tonic-gate * Calls to kobj_open() are handled off to this routine as a separate thread. 36020Sstevel@tonic-gate */ 36030Sstevel@tonic-gate static void 36040Sstevel@tonic-gate kobjopen_thread(struct kobjopen_tctl *ltp) 36050Sstevel@tonic-gate { 36060Sstevel@tonic-gate kmutex_t cpr_lk; 36070Sstevel@tonic-gate callb_cpr_t cpr_i; 36080Sstevel@tonic-gate 36090Sstevel@tonic-gate mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL); 36100Sstevel@tonic-gate CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen"); 36110Sstevel@tonic-gate ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp), 36123912Slling 0, 0); 36130Sstevel@tonic-gate sema_v(<p->sema); 36140Sstevel@tonic-gate mutex_enter(&cpr_lk); 36150Sstevel@tonic-gate CALLB_CPR_EXIT(&cpr_i); 36160Sstevel@tonic-gate mutex_destroy(&cpr_lk); 36170Sstevel@tonic-gate thread_exit(); 36180Sstevel@tonic-gate } 36190Sstevel@tonic-gate 36200Sstevel@tonic-gate /* 36210Sstevel@tonic-gate * allocate and initialize a kobjopen thread structure 36220Sstevel@tonic-gate */ 36230Sstevel@tonic-gate static struct kobjopen_tctl * 36240Sstevel@tonic-gate kobjopen_alloc(char *filename) 36250Sstevel@tonic-gate { 36260Sstevel@tonic-gate struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP); 36270Sstevel@tonic-gate 36280Sstevel@tonic-gate ASSERT(filename != NULL); 36290Sstevel@tonic-gate 36300Sstevel@tonic-gate ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP); 36310Sstevel@tonic-gate bcopy(filename, ltp->name, strlen(filename) + 1); 36320Sstevel@tonic-gate sema_init(<p->sema, 0, NULL, SEMA_DEFAULT, NULL); 36330Sstevel@tonic-gate return (ltp); 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate 36360Sstevel@tonic-gate /* 36370Sstevel@tonic-gate * free a kobjopen thread control structure 36380Sstevel@tonic-gate */ 36390Sstevel@tonic-gate static void 36400Sstevel@tonic-gate kobjopen_free(struct kobjopen_tctl *ltp) 36410Sstevel@tonic-gate { 36420Sstevel@tonic-gate sema_destroy(<p->sema); 36430Sstevel@tonic-gate kmem_free(ltp->name, strlen(ltp->name) + 1); 36440Sstevel@tonic-gate kmem_free(ltp, sizeof (*ltp)); 36450Sstevel@tonic-gate } 36460Sstevel@tonic-gate 36470Sstevel@tonic-gate int 36480Sstevel@tonic-gate kobj_read(intptr_t descr, char *buf, unsigned size, unsigned offset) 36490Sstevel@tonic-gate { 36500Sstevel@tonic-gate int stat; 36510Sstevel@tonic-gate ssize_t resid; 36520Sstevel@tonic-gate 36530Sstevel@tonic-gate if (_modrootloaded) { 36540Sstevel@tonic-gate if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size, 36550Sstevel@tonic-gate (offset_t)offset, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), 36560Sstevel@tonic-gate &resid)) != 0) { 36570Sstevel@tonic-gate _kobj_printf(ops, 36580Sstevel@tonic-gate "vn_rdwr failed with error 0x%x\n", stat); 36590Sstevel@tonic-gate return (-1); 36600Sstevel@tonic-gate } 36610Sstevel@tonic-gate return (size - resid); 36620Sstevel@tonic-gate } else { 36630Sstevel@tonic-gate int count = 0; 36640Sstevel@tonic-gate 36650Sstevel@tonic-gate if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) { 36660Sstevel@tonic-gate _kobj_printf(ops, 36670Sstevel@tonic-gate "kobj_read: seek 0x%x failed\n", offset); 36680Sstevel@tonic-gate return (-1); 36690Sstevel@tonic-gate } 36700Sstevel@tonic-gate 36710Sstevel@tonic-gate count = kobj_boot_read((int)descr, buf, size); 36720Sstevel@tonic-gate if (count < size) { 36730Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 36740Sstevel@tonic-gate _kobj_printf(ops, 36750Sstevel@tonic-gate "kobj_read: req %d bytes, ", size); 36760Sstevel@tonic-gate _kobj_printf(ops, "got %d\n", count); 36770Sstevel@tonic-gate } 36780Sstevel@tonic-gate } 36790Sstevel@tonic-gate return (count); 36800Sstevel@tonic-gate } 36810Sstevel@tonic-gate } 36820Sstevel@tonic-gate 36830Sstevel@tonic-gate void 36840Sstevel@tonic-gate kobj_close(intptr_t descr) 36850Sstevel@tonic-gate { 36860Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 36870Sstevel@tonic-gate _kobj_printf(ops, "kobj_close: 0x%lx\n", descr); 36880Sstevel@tonic-gate 36890Sstevel@tonic-gate if (_modrootloaded) { 36900Sstevel@tonic-gate struct vnode *vp = (struct vnode *)descr; 36910Sstevel@tonic-gate (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED()); 36920Sstevel@tonic-gate VN_RELE(vp); 36930Sstevel@tonic-gate } else 36940Sstevel@tonic-gate (void) kobj_boot_close((int)descr); 36950Sstevel@tonic-gate } 36960Sstevel@tonic-gate 36971544Seschrock int 36981544Seschrock kobj_fstat(intptr_t descr, struct bootstat *buf) 36991544Seschrock { 37001544Seschrock if (buf == NULL) 37011544Seschrock return (-1); 37021544Seschrock 37031544Seschrock if (_modrootloaded) { 37041544Seschrock vattr_t vattr; 37051544Seschrock struct vnode *vp = (struct vnode *)descr; 37061544Seschrock if (VOP_GETATTR(vp, &vattr, 0, kcred) != 0) 37071544Seschrock return (-1); 37081544Seschrock 37091544Seschrock /* 37101544Seschrock * The vattr and bootstat structures are similar, but not 37111544Seschrock * identical. We do our best to fill in the bootstat structure 37121544Seschrock * from the contents of vattr (transfering only the ones that 37131544Seschrock * are obvious. 37141544Seschrock */ 37151544Seschrock 37161544Seschrock buf->st_mode = (uint32_t)vattr.va_mode; 37171544Seschrock buf->st_nlink = (uint32_t)vattr.va_nlink; 37181544Seschrock buf->st_uid = (int32_t)vattr.va_uid; 37191544Seschrock buf->st_gid = (int32_t)vattr.va_gid; 37201544Seschrock buf->st_rdev = (uint64_t)vattr.va_rdev; 37211544Seschrock buf->st_size = (uint64_t)vattr.va_size; 37221544Seschrock buf->st_atim.tv_sec = (int64_t)vattr.va_atime.tv_sec; 37231544Seschrock buf->st_atim.tv_nsec = (int64_t)vattr.va_atime.tv_nsec; 37241544Seschrock buf->st_mtim.tv_sec = (int64_t)vattr.va_mtime.tv_sec; 37251544Seschrock buf->st_mtim.tv_nsec = (int64_t)vattr.va_mtime.tv_nsec; 37261544Seschrock buf->st_ctim.tv_sec = (int64_t)vattr.va_ctime.tv_sec; 37271544Seschrock buf->st_ctim.tv_nsec = (int64_t)vattr.va_ctime.tv_nsec; 37281544Seschrock buf->st_blksize = (int32_t)vattr.va_blksize; 37291544Seschrock buf->st_blocks = (int64_t)vattr.va_nblocks; 37301544Seschrock 37311544Seschrock return (0); 37321544Seschrock } 37331544Seschrock 37341544Seschrock return (kobj_boot_fstat((int)descr, buf)); 37351544Seschrock } 37361544Seschrock 37371544Seschrock 37380Sstevel@tonic-gate struct _buf * 37390Sstevel@tonic-gate kobj_open_file(char *name) 37400Sstevel@tonic-gate { 37410Sstevel@tonic-gate struct _buf *file; 37420Sstevel@tonic-gate intptr_t fd; 37430Sstevel@tonic-gate 37440Sstevel@tonic-gate if ((fd = kobj_open(name)) == -1) { 37450Sstevel@tonic-gate return ((struct _buf *)-1); 37460Sstevel@tonic-gate } 37470Sstevel@tonic-gate 37480Sstevel@tonic-gate file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP); 37490Sstevel@tonic-gate file->_fd = fd; 37500Sstevel@tonic-gate file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP); 37510Sstevel@tonic-gate file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT|KM_TMP); 37520Sstevel@tonic-gate file->_cnt = file->_size = file->_off = 0; 37530Sstevel@tonic-gate file->_ln = 1; 37540Sstevel@tonic-gate file->_ptr = file->_base; 37550Sstevel@tonic-gate (void) strcpy(file->_name, name); 37560Sstevel@tonic-gate return (file); 37570Sstevel@tonic-gate } 37580Sstevel@tonic-gate 37590Sstevel@tonic-gate void 37600Sstevel@tonic-gate kobj_close_file(struct _buf *file) 37610Sstevel@tonic-gate { 37620Sstevel@tonic-gate kobj_close(file->_fd); 37630Sstevel@tonic-gate kobj_free(file->_base, MAXBSIZE); 37640Sstevel@tonic-gate kobj_free(file->_name, strlen(file->_name)+1); 37650Sstevel@tonic-gate kobj_free(file, sizeof (struct _buf)); 37660Sstevel@tonic-gate } 37670Sstevel@tonic-gate 37680Sstevel@tonic-gate int 37690Sstevel@tonic-gate kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) 37700Sstevel@tonic-gate { 37710Sstevel@tonic-gate int b_size, c_size; 37720Sstevel@tonic-gate int b_off; /* Offset into buffer for start of bcopy */ 37730Sstevel@tonic-gate int count = 0; 37740Sstevel@tonic-gate int page_addr; 37750Sstevel@tonic-gate 37760Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 37770Sstevel@tonic-gate _kobj_printf(ops, "kobj_read_file: size=%x,", size); 37780Sstevel@tonic-gate _kobj_printf(ops, " offset=%x at", off); 37790Sstevel@tonic-gate _kobj_printf(ops, " buf=%x\n", buf); 37800Sstevel@tonic-gate } 37810Sstevel@tonic-gate 37820Sstevel@tonic-gate while (size) { 37830Sstevel@tonic-gate page_addr = F_PAGE(off); 37840Sstevel@tonic-gate b_size = file->_size; 37850Sstevel@tonic-gate /* 37860Sstevel@tonic-gate * If we have the filesystem page the caller's referring to 37870Sstevel@tonic-gate * and we have something in the buffer, 37880Sstevel@tonic-gate * satisfy as much of the request from the buffer as we can. 37890Sstevel@tonic-gate */ 37900Sstevel@tonic-gate if (page_addr == file->_off && b_size > 0) { 37910Sstevel@tonic-gate b_off = B_OFFSET(off); 37920Sstevel@tonic-gate c_size = b_size - b_off; 37930Sstevel@tonic-gate /* 37940Sstevel@tonic-gate * If there's nothing to copy, we're at EOF. 37950Sstevel@tonic-gate */ 37960Sstevel@tonic-gate if (c_size <= 0) 37970Sstevel@tonic-gate break; 37980Sstevel@tonic-gate if (c_size > size) 37990Sstevel@tonic-gate c_size = size; 38000Sstevel@tonic-gate if (buf) { 38010Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 38020Sstevel@tonic-gate _kobj_printf(ops, "copying %x bytes\n", 38030Sstevel@tonic-gate c_size); 38040Sstevel@tonic-gate bcopy(file->_base+b_off, buf, c_size); 38050Sstevel@tonic-gate size -= c_size; 38060Sstevel@tonic-gate off += c_size; 38070Sstevel@tonic-gate buf += c_size; 38080Sstevel@tonic-gate count += c_size; 38090Sstevel@tonic-gate } else { 38100Sstevel@tonic-gate _kobj_printf(ops, "kobj_read: system error"); 38110Sstevel@tonic-gate count = -1; 38120Sstevel@tonic-gate break; 38130Sstevel@tonic-gate } 38140Sstevel@tonic-gate } else { 38150Sstevel@tonic-gate /* 38160Sstevel@tonic-gate * If the caller's offset is page aligned and 38170Sstevel@tonic-gate * the caller want's at least a filesystem page and 38180Sstevel@tonic-gate * the caller provided a buffer, 38190Sstevel@tonic-gate * read directly into the caller's buffer. 38200Sstevel@tonic-gate */ 38210Sstevel@tonic-gate if (page_addr == off && 38220Sstevel@tonic-gate (c_size = F_PAGE(size)) && buf) { 38230Sstevel@tonic-gate c_size = kobj_read(file->_fd, buf, c_size, 38243912Slling page_addr); 38250Sstevel@tonic-gate if (c_size < 0) { 38260Sstevel@tonic-gate count = -1; 38270Sstevel@tonic-gate break; 38280Sstevel@tonic-gate } 38290Sstevel@tonic-gate count += c_size; 38300Sstevel@tonic-gate if (c_size != F_PAGE(size)) 38310Sstevel@tonic-gate break; 38320Sstevel@tonic-gate size -= c_size; 38330Sstevel@tonic-gate off += c_size; 38340Sstevel@tonic-gate buf += c_size; 38350Sstevel@tonic-gate /* 38360Sstevel@tonic-gate * Otherwise, read into our buffer and copy next time 38370Sstevel@tonic-gate * around the loop. 38380Sstevel@tonic-gate */ 38390Sstevel@tonic-gate } else { 38400Sstevel@tonic-gate file->_off = page_addr; 38410Sstevel@tonic-gate c_size = kobj_read(file->_fd, file->_base, 38423912Slling MAXBSIZE, page_addr); 38430Sstevel@tonic-gate file->_ptr = file->_base; 38440Sstevel@tonic-gate file->_cnt = c_size; 38450Sstevel@tonic-gate file->_size = c_size; 38460Sstevel@tonic-gate /* 38470Sstevel@tonic-gate * If a _filbuf call or nothing read, break. 38480Sstevel@tonic-gate */ 38490Sstevel@tonic-gate if (buf == NULL || c_size <= 0) { 38500Sstevel@tonic-gate count = c_size; 38510Sstevel@tonic-gate break; 38520Sstevel@tonic-gate } 38530Sstevel@tonic-gate } 38540Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 38550Sstevel@tonic-gate _kobj_printf(ops, "read %x bytes\n", c_size); 38560Sstevel@tonic-gate } 38570Sstevel@tonic-gate } 38580Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 38590Sstevel@tonic-gate _kobj_printf(ops, "count = %x\n", count); 38600Sstevel@tonic-gate 38610Sstevel@tonic-gate return (count); 38620Sstevel@tonic-gate } 38630Sstevel@tonic-gate 38640Sstevel@tonic-gate int 38650Sstevel@tonic-gate kobj_filbuf(struct _buf *f) 38660Sstevel@tonic-gate { 38670Sstevel@tonic-gate if (kobj_read_file(f, NULL, MAXBSIZE, f->_off + f->_size) > 0) 38680Sstevel@tonic-gate return (kobj_getc(f)); 38690Sstevel@tonic-gate return (-1); 38700Sstevel@tonic-gate } 38710Sstevel@tonic-gate 38720Sstevel@tonic-gate void 38730Sstevel@tonic-gate kobj_free(void *address, size_t size) 38740Sstevel@tonic-gate { 38750Sstevel@tonic-gate if (standalone) 38760Sstevel@tonic-gate return; 38770Sstevel@tonic-gate 38780Sstevel@tonic-gate kmem_free(address, size); 38790Sstevel@tonic-gate kobj_stat.nfree_calls++; 38800Sstevel@tonic-gate kobj_stat.nfree += size; 38810Sstevel@tonic-gate } 38820Sstevel@tonic-gate 38830Sstevel@tonic-gate void * 38840Sstevel@tonic-gate kobj_zalloc(size_t size, int flag) 38850Sstevel@tonic-gate { 38860Sstevel@tonic-gate void *v; 38870Sstevel@tonic-gate 38880Sstevel@tonic-gate if ((v = kobj_alloc(size, flag)) != 0) { 38890Sstevel@tonic-gate bzero(v, size); 38900Sstevel@tonic-gate } 38910Sstevel@tonic-gate 38920Sstevel@tonic-gate return (v); 38930Sstevel@tonic-gate } 38940Sstevel@tonic-gate 38950Sstevel@tonic-gate void * 38960Sstevel@tonic-gate kobj_alloc(size_t size, int flag) 38970Sstevel@tonic-gate { 38980Sstevel@tonic-gate /* 38990Sstevel@tonic-gate * If we are running standalone in the 39000Sstevel@tonic-gate * linker, we ask boot for memory. 39010Sstevel@tonic-gate * Either it's temporary memory that we lose 39020Sstevel@tonic-gate * once boot is mapped out or we allocate it 39030Sstevel@tonic-gate * permanently using the dynamic data segment. 39040Sstevel@tonic-gate */ 39050Sstevel@tonic-gate if (standalone) { 39060Sstevel@tonic-gate #ifdef __sparc 39070Sstevel@tonic-gate if (flag & KM_TMP) { 39080Sstevel@tonic-gate return (kobj_tmp_alloc(size)); 39090Sstevel@tonic-gate } else if (flag & KM_SCRATCH) { 39100Sstevel@tonic-gate void *buf = kobj_bs_alloc(size); 39110Sstevel@tonic-gate 39120Sstevel@tonic-gate if (buf != NULL) 39130Sstevel@tonic-gate return (buf); 39140Sstevel@tonic-gate #ifdef KOBJ_DEBUG 39150Sstevel@tonic-gate if (kobj_debug & D_DEBUG) { 39160Sstevel@tonic-gate _kobj_printf(ops, "krtld: failed scratch alloc " 39173446Smrj "of %lu bytes -- falling back\n", size); 39180Sstevel@tonic-gate } 39190Sstevel@tonic-gate #endif 39200Sstevel@tonic-gate } 39210Sstevel@tonic-gate 39220Sstevel@tonic-gate #else /* x86 */ 39230Sstevel@tonic-gate if (flag & (KM_TMP | KM_SCRATCH)) 39240Sstevel@tonic-gate return (BOP_ALLOC(ops, 0, size, MINALIGN)); 39250Sstevel@tonic-gate #endif 39260Sstevel@tonic-gate return (kobj_segbrk(&_edata, size, MINALIGN, 0)); 39270Sstevel@tonic-gate } 39280Sstevel@tonic-gate 39290Sstevel@tonic-gate kobj_stat.nalloc_calls++; 39300Sstevel@tonic-gate kobj_stat.nalloc += size; 39310Sstevel@tonic-gate 39320Sstevel@tonic-gate return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)); 39330Sstevel@tonic-gate } 39340Sstevel@tonic-gate 39350Sstevel@tonic-gate /* 39360Sstevel@tonic-gate * Allow the "mod" system to sync up with the work 39370Sstevel@tonic-gate * already done by kobj during the initial loading 39380Sstevel@tonic-gate * of the kernel. This also gives us a chance 39390Sstevel@tonic-gate * to reallocate memory that belongs to boot. 39400Sstevel@tonic-gate */ 39410Sstevel@tonic-gate void 39420Sstevel@tonic-gate kobj_sync(void) 39430Sstevel@tonic-gate { 39440Sstevel@tonic-gate struct modctl_list *lp, **lpp; 39450Sstevel@tonic-gate 39460Sstevel@tonic-gate /* 39473446Smrj * The module path can be set in /etc/system via 'moddir' commands 39480Sstevel@tonic-gate */ 39490Sstevel@tonic-gate if (default_path != NULL) 39503446Smrj kobj_module_path = default_path; 39510Sstevel@tonic-gate else 39523446Smrj default_path = kobj_module_path; 39530Sstevel@tonic-gate 39540Sstevel@tonic-gate ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t), 39550Sstevel@tonic-gate segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 39560Sstevel@tonic-gate 39570Sstevel@tonic-gate ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t), 39580Sstevel@tonic-gate segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 39590Sstevel@tonic-gate 39600Sstevel@tonic-gate /* 39610Sstevel@tonic-gate * Move symbol tables from boot memory to ksyms_arena. 39620Sstevel@tonic-gate */ 39630Sstevel@tonic-gate for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) { 39640Sstevel@tonic-gate for (lp = *lpp; lp != NULL; lp = lp->modl_next) 39650Sstevel@tonic-gate kobj_export_module(mod(lp)); 39660Sstevel@tonic-gate } 39670Sstevel@tonic-gate } 39680Sstevel@tonic-gate 39690Sstevel@tonic-gate caddr_t 39700Sstevel@tonic-gate kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit) 39710Sstevel@tonic-gate { 39720Sstevel@tonic-gate uintptr_t va, pva; 39730Sstevel@tonic-gate size_t alloc_pgsz = kobj_mmu_pagesize; 39740Sstevel@tonic-gate size_t alloc_align = BO_NO_ALIGN; 39750Sstevel@tonic-gate size_t alloc_size; 39760Sstevel@tonic-gate 39770Sstevel@tonic-gate /* 39780Sstevel@tonic-gate * If we are using "large" mappings for the kernel, 39790Sstevel@tonic-gate * request aligned memory from boot using the 39800Sstevel@tonic-gate * "large" pagesize. 39810Sstevel@tonic-gate */ 39820Sstevel@tonic-gate if (lg_pagesize) { 39830Sstevel@tonic-gate alloc_align = lg_pagesize; 39840Sstevel@tonic-gate alloc_pgsz = lg_pagesize; 39850Sstevel@tonic-gate } 39860Sstevel@tonic-gate va = ALIGN((uintptr_t)*spp, align); 39870Sstevel@tonic-gate pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz); 39880Sstevel@tonic-gate /* 39890Sstevel@tonic-gate * Need more pages? 39900Sstevel@tonic-gate */ 39910Sstevel@tonic-gate if (va + size > pva) { 39923446Smrj uintptr_t npva; 39933446Smrj 39940Sstevel@tonic-gate alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz); 39950Sstevel@tonic-gate /* 39960Sstevel@tonic-gate * Check for overlapping segments. 39970Sstevel@tonic-gate */ 39983446Smrj if (limit && limit <= *spp + alloc_size) { 39990Sstevel@tonic-gate return ((caddr_t)0); 40003446Smrj } 40013446Smrj 40023446Smrj npva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva, 40033912Slling alloc_size, alloc_align); 40043446Smrj 40053446Smrj if (npva == NULL) { 40063446Smrj _kobj_printf(ops, "BOP_ALLOC failed, 0x%lx bytes", 40070Sstevel@tonic-gate alloc_size); 40083446Smrj _kobj_printf(ops, " aligned %lx", alloc_align); 40090Sstevel@tonic-gate _kobj_printf(ops, " at 0x%lx\n", pva); 40103446Smrj return (NULL); 40110Sstevel@tonic-gate } 40120Sstevel@tonic-gate } 40130Sstevel@tonic-gate *spp = (caddr_t)(va + size); 40140Sstevel@tonic-gate 40150Sstevel@tonic-gate return ((caddr_t)va); 40160Sstevel@tonic-gate } 40170Sstevel@tonic-gate 40180Sstevel@tonic-gate /* 40190Sstevel@tonic-gate * Calculate the number of output hash buckets. 40200Sstevel@tonic-gate * We use the next prime larger than n / 4, 40210Sstevel@tonic-gate * so the average hash chain is about 4 entries. 40220Sstevel@tonic-gate * More buckets would just be a waste of memory. 40230Sstevel@tonic-gate */ 40240Sstevel@tonic-gate uint_t 40250Sstevel@tonic-gate kobj_gethashsize(uint_t n) 40260Sstevel@tonic-gate { 40270Sstevel@tonic-gate int f; 40280Sstevel@tonic-gate int hsize = MAX(n / 4, 2); 40290Sstevel@tonic-gate 40300Sstevel@tonic-gate for (f = 2; f * f <= hsize; f++) 40310Sstevel@tonic-gate if (hsize % f == 0) 40320Sstevel@tonic-gate hsize += f = 1; 40330Sstevel@tonic-gate 40340Sstevel@tonic-gate return (hsize); 40350Sstevel@tonic-gate } 40360Sstevel@tonic-gate 40373912Slling /* 40383912Slling * Get the file size. 40393912Slling * 40403912Slling * Before root is mounted, files are compressed in the boot_archive ramdisk 40413912Slling * (in the memory). kobj_fstat would return the compressed file size. 40423912Slling * In order to get the uncompressed file size, read the file to the end and 40433912Slling * count its size. 40443912Slling */ 40453912Slling int 40463912Slling kobj_get_filesize(struct _buf *file, uint64_t *size) 40473912Slling { 40483912Slling if (_modrootloaded) { 40493912Slling struct bootstat bst; 40503912Slling 40513912Slling if (kobj_fstat(file->_fd, &bst) != 0) 40523912Slling return (EIO); 40533912Slling *size = bst.st_size; 40543912Slling } else { 40553912Slling char *buf; 40563912Slling int count; 40573912Slling uint64_t offset = 0; 40583912Slling 40593912Slling buf = kmem_alloc(MAXBSIZE, KM_SLEEP); 40603912Slling do { 40613912Slling count = kobj_read_file(file, buf, MAXBSIZE, offset); 40623912Slling if (count < 0) { 40633912Slling kmem_free(buf, MAXBSIZE); 40643912Slling return (EIO); 40653912Slling } 40663912Slling offset += count; 40673912Slling } while (count == MAXBSIZE); 40683912Slling kmem_free(buf, MAXBSIZE); 40693912Slling 40703912Slling *size = offset; 40713912Slling } 40723912Slling 40733912Slling return (0); 40743912Slling } 40753912Slling 40760Sstevel@tonic-gate static char * 40770Sstevel@tonic-gate basename(char *s) 40780Sstevel@tonic-gate { 40790Sstevel@tonic-gate char *p, *q; 40800Sstevel@tonic-gate 40810Sstevel@tonic-gate q = NULL; 40820Sstevel@tonic-gate p = s; 40830Sstevel@tonic-gate do { 40840Sstevel@tonic-gate if (*p == '/') 40850Sstevel@tonic-gate q = p; 40860Sstevel@tonic-gate } while (*p++); 40870Sstevel@tonic-gate return (q ? q + 1 : s); 40880Sstevel@tonic-gate } 40890Sstevel@tonic-gate 40900Sstevel@tonic-gate /*ARGSUSED*/ 40910Sstevel@tonic-gate static void 40920Sstevel@tonic-gate kprintf(void *op, const char *fmt, ...) 40930Sstevel@tonic-gate { 40940Sstevel@tonic-gate va_list adx; 40950Sstevel@tonic-gate 40960Sstevel@tonic-gate va_start(adx, fmt); 40970Sstevel@tonic-gate vprintf(fmt, adx); 40980Sstevel@tonic-gate va_end(adx); 40990Sstevel@tonic-gate } 41000Sstevel@tonic-gate 41010Sstevel@tonic-gate void 41020Sstevel@tonic-gate kobj_stat_get(kobj_stat_t *kp) 41030Sstevel@tonic-gate { 41040Sstevel@tonic-gate *kp = kobj_stat; 41050Sstevel@tonic-gate } 41060Sstevel@tonic-gate 41070Sstevel@tonic-gate int 41080Sstevel@tonic-gate kobj_getpagesize() 41090Sstevel@tonic-gate { 41100Sstevel@tonic-gate return (lg_pagesize); 41110Sstevel@tonic-gate } 41120Sstevel@tonic-gate 41130Sstevel@tonic-gate void 41140Sstevel@tonic-gate kobj_textwin_alloc(struct module *mp) 41150Sstevel@tonic-gate { 41160Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mod_lock)); 41170Sstevel@tonic-gate 41180Sstevel@tonic-gate if (mp->textwin != NULL) 41190Sstevel@tonic-gate return; 41200Sstevel@tonic-gate 41210Sstevel@tonic-gate /* 41220Sstevel@tonic-gate * If the text is not contained in the heap, then it is not contained 41230Sstevel@tonic-gate * by a writable mapping. (Specifically, it's on the nucleus page.) 41240Sstevel@tonic-gate * We allocate a read/write mapping for this module's text to allow 41250Sstevel@tonic-gate * the text to be patched without calling hot_patch_kernel_text() 41260Sstevel@tonic-gate * (which is quite slow). 41270Sstevel@tonic-gate */ 41280Sstevel@tonic-gate if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) { 41290Sstevel@tonic-gate uintptr_t text = (uintptr_t)mp->text; 41300Sstevel@tonic-gate uintptr_t size = (uintptr_t)mp->text_size; 41310Sstevel@tonic-gate uintptr_t i; 41320Sstevel@tonic-gate caddr_t va; 41330Sstevel@tonic-gate size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) - 41340Sstevel@tonic-gate (text & PAGEMASK); 41350Sstevel@tonic-gate 41360Sstevel@tonic-gate va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP); 41370Sstevel@tonic-gate 41380Sstevel@tonic-gate for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) { 41390Sstevel@tonic-gate hat_devload(kas.a_hat, va, PAGESIZE, 41400Sstevel@tonic-gate hat_getpfnum(kas.a_hat, (caddr_t)i), 41410Sstevel@tonic-gate PROT_READ | PROT_WRITE, 41420Sstevel@tonic-gate HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST); 41430Sstevel@tonic-gate va += PAGESIZE; 41440Sstevel@tonic-gate } 41450Sstevel@tonic-gate 41460Sstevel@tonic-gate mp->textwin = mp->textwin_base + (text & PAGEOFFSET); 41470Sstevel@tonic-gate } else { 41480Sstevel@tonic-gate mp->textwin = mp->text; 41490Sstevel@tonic-gate } 41500Sstevel@tonic-gate } 41510Sstevel@tonic-gate 41520Sstevel@tonic-gate void 41530Sstevel@tonic-gate kobj_textwin_free(struct module *mp) 41540Sstevel@tonic-gate { 41550Sstevel@tonic-gate uintptr_t text = (uintptr_t)mp->text; 41560Sstevel@tonic-gate uintptr_t tsize = (uintptr_t)mp->text_size; 41570Sstevel@tonic-gate size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) - 41580Sstevel@tonic-gate (text & PAGEMASK)); 41590Sstevel@tonic-gate 41600Sstevel@tonic-gate mp->textwin = NULL; 41610Sstevel@tonic-gate 41620Sstevel@tonic-gate if (mp->textwin_base == NULL) 41630Sstevel@tonic-gate return; 41640Sstevel@tonic-gate 41650Sstevel@tonic-gate hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK); 41660Sstevel@tonic-gate vmem_free(heap_arena, mp->textwin_base, size); 41670Sstevel@tonic-gate mp->textwin_base = NULL; 41680Sstevel@tonic-gate } 41690Sstevel@tonic-gate 41700Sstevel@tonic-gate static char * 41710Sstevel@tonic-gate find_libmacro(char *name) 41720Sstevel@tonic-gate { 41730Sstevel@tonic-gate int lmi; 41740Sstevel@tonic-gate 41750Sstevel@tonic-gate for (lmi = 0; lmi < NLIBMACROS; lmi++) { 41760Sstevel@tonic-gate if (strcmp(name, libmacros[lmi].lmi_macroname) == 0) 41770Sstevel@tonic-gate return (libmacros[lmi].lmi_list); 41780Sstevel@tonic-gate } 41790Sstevel@tonic-gate return (NULL); 41800Sstevel@tonic-gate } 41810Sstevel@tonic-gate 41820Sstevel@tonic-gate /* 41830Sstevel@tonic-gate * Check for $MACRO in tail (string to expand) and expand it in path at pathend 41840Sstevel@tonic-gate * returns path if successful, else NULL 41850Sstevel@tonic-gate * Support multiple $MACROs expansion and the first valid path will be returned 41860Sstevel@tonic-gate * Caller's responsibility to provide enough space in path to expand 41870Sstevel@tonic-gate */ 41880Sstevel@tonic-gate char * 41890Sstevel@tonic-gate expand_libmacro(char *tail, char *path, char *pathend) 41900Sstevel@tonic-gate { 41910Sstevel@tonic-gate char c, *p, *p1, *p2, *path2, *endp; 41920Sstevel@tonic-gate int diff, lmi, macrolen, valid_macro, more_macro; 41930Sstevel@tonic-gate struct _buf *file; 41940Sstevel@tonic-gate 41950Sstevel@tonic-gate /* 41960Sstevel@tonic-gate * check for $MACROS between nulls or slashes 41970Sstevel@tonic-gate */ 41980Sstevel@tonic-gate p = strchr(tail, '$'); 41990Sstevel@tonic-gate if (p == NULL) 42000Sstevel@tonic-gate return (NULL); 42010Sstevel@tonic-gate for (lmi = 0; lmi < NLIBMACROS; lmi++) { 42020Sstevel@tonic-gate macrolen = libmacros[lmi].lmi_macrolen; 42030Sstevel@tonic-gate if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0) 42040Sstevel@tonic-gate break; 42050Sstevel@tonic-gate } 42060Sstevel@tonic-gate 42070Sstevel@tonic-gate valid_macro = 0; 42080Sstevel@tonic-gate if (lmi < NLIBMACROS) { 42090Sstevel@tonic-gate /* 42100Sstevel@tonic-gate * The following checks are used to restrict expansion of 42110Sstevel@tonic-gate * macros to those that form a full directory/file name 42120Sstevel@tonic-gate * and to keep the behavior same as before. If this 42130Sstevel@tonic-gate * restriction is removed or no longer valid in the future, 42140Sstevel@tonic-gate * the checks below can be deleted. 42150Sstevel@tonic-gate */ 42160Sstevel@tonic-gate if ((p == tail) || (*(p - 1) == '/')) { 42170Sstevel@tonic-gate c = *(p + macrolen + 1); 42180Sstevel@tonic-gate if (c == '/' || c == '\0') 42190Sstevel@tonic-gate valid_macro = 1; 42200Sstevel@tonic-gate } 42210Sstevel@tonic-gate } 42220Sstevel@tonic-gate 42230Sstevel@tonic-gate if (!valid_macro) { 42240Sstevel@tonic-gate p2 = strchr(p, '/'); 42250Sstevel@tonic-gate /* 42260Sstevel@tonic-gate * if no more macro to expand, then just copy whatever left 42270Sstevel@tonic-gate * and check whether it exists 42280Sstevel@tonic-gate */ 42290Sstevel@tonic-gate if (p2 == NULL || strchr(p2, '$') == NULL) { 42300Sstevel@tonic-gate (void) strcpy(pathend, tail); 42310Sstevel@tonic-gate if ((file = kobj_open_path(path, 1, 1)) != 42320Sstevel@tonic-gate (struct _buf *)-1) { 42330Sstevel@tonic-gate kobj_close_file(file); 42340Sstevel@tonic-gate return (path); 42350Sstevel@tonic-gate } else 42360Sstevel@tonic-gate return (NULL); 42370Sstevel@tonic-gate } else { 42380Sstevel@tonic-gate /* 42390Sstevel@tonic-gate * copy all chars before '/' and call expand_libmacro() 42400Sstevel@tonic-gate * again 42410Sstevel@tonic-gate */ 42420Sstevel@tonic-gate diff = p2 - tail; 42430Sstevel@tonic-gate bcopy(tail, pathend, diff); 42440Sstevel@tonic-gate pathend += diff; 42450Sstevel@tonic-gate *(pathend) = '\0'; 42460Sstevel@tonic-gate return (expand_libmacro(p2, path, pathend)); 42470Sstevel@tonic-gate } 42480Sstevel@tonic-gate } 42490Sstevel@tonic-gate 42500Sstevel@tonic-gate more_macro = 0; 42510Sstevel@tonic-gate if (c != '\0') { 42520Sstevel@tonic-gate endp = p + macrolen + 1; 42530Sstevel@tonic-gate if (strchr(endp, '$') != NULL) 42540Sstevel@tonic-gate more_macro = 1; 42550Sstevel@tonic-gate } else 42560Sstevel@tonic-gate endp = NULL; 42570Sstevel@tonic-gate 42580Sstevel@tonic-gate /* 42590Sstevel@tonic-gate * copy lmi_list and split it into components. 42600Sstevel@tonic-gate * then put the part of tail before $MACRO into path 42610Sstevel@tonic-gate * at pathend 42620Sstevel@tonic-gate */ 42630Sstevel@tonic-gate diff = p - tail; 42640Sstevel@tonic-gate if (diff > 0) 42650Sstevel@tonic-gate bcopy(tail, pathend, diff); 42660Sstevel@tonic-gate path2 = pathend + diff; 42670Sstevel@tonic-gate p1 = libmacros[lmi].lmi_list; 42680Sstevel@tonic-gate while (p1 && (*p1 != '\0')) { 42690Sstevel@tonic-gate p2 = strchr(p1, ':'); 42700Sstevel@tonic-gate if (p2) { 42710Sstevel@tonic-gate diff = p2 - p1; 42720Sstevel@tonic-gate bcopy(p1, path2, diff); 42730Sstevel@tonic-gate *(path2 + diff) = '\0'; 42740Sstevel@tonic-gate } else { 42750Sstevel@tonic-gate diff = strlen(p1); 42760Sstevel@tonic-gate bcopy(p1, path2, diff + 1); 42770Sstevel@tonic-gate } 42780Sstevel@tonic-gate /* copy endp only if there isn't any more macro to expand */ 42790Sstevel@tonic-gate if (!more_macro && (endp != NULL)) 42800Sstevel@tonic-gate (void) strcat(path2, endp); 42810Sstevel@tonic-gate file = kobj_open_path(path, 1, 1); 42820Sstevel@tonic-gate if (file != (struct _buf *)-1) { 42830Sstevel@tonic-gate kobj_close_file(file); 42840Sstevel@tonic-gate /* 42850Sstevel@tonic-gate * if more macros to expand then call expand_libmacro(), 42860Sstevel@tonic-gate * else return path which has the whole path 42870Sstevel@tonic-gate */ 42880Sstevel@tonic-gate if (!more_macro || (expand_libmacro(endp, path, 42890Sstevel@tonic-gate path2 + diff) != NULL)) { 42900Sstevel@tonic-gate return (path); 42910Sstevel@tonic-gate } 42920Sstevel@tonic-gate } 42930Sstevel@tonic-gate if (p2) 42940Sstevel@tonic-gate p1 = ++p2; 42950Sstevel@tonic-gate else 42960Sstevel@tonic-gate return (NULL); 42970Sstevel@tonic-gate } 42980Sstevel@tonic-gate return (NULL); 42990Sstevel@tonic-gate } 43000Sstevel@tonic-gate 43010Sstevel@tonic-gate static void 43020Sstevel@tonic-gate tnf_add_notifyunload(kobj_notify_f *fp) 43030Sstevel@tonic-gate { 43040Sstevel@tonic-gate kobj_notify_list_t *entry; 43050Sstevel@tonic-gate 43060Sstevel@tonic-gate entry = kobj_alloc(sizeof (kobj_notify_list_t), KM_WAIT); 43070Sstevel@tonic-gate entry->kn_type = KOBJ_NOTIFY_MODUNLOADING; 43080Sstevel@tonic-gate entry->kn_func = fp; 43090Sstevel@tonic-gate (void) kobj_notify_add(entry); 43100Sstevel@tonic-gate } 43110Sstevel@tonic-gate 43120Sstevel@tonic-gate /* ARGSUSED */ 43130Sstevel@tonic-gate static void 43140Sstevel@tonic-gate tnf_unsplice_probes(unsigned int what, struct modctl *mod) 43150Sstevel@tonic-gate { 43160Sstevel@tonic-gate extern tnf_probe_control_t *__tnf_probe_list_head; 43170Sstevel@tonic-gate extern tnf_tag_data_t *__tnf_tag_list_head; 43180Sstevel@tonic-gate tnf_probe_control_t **p; 43190Sstevel@tonic-gate tnf_tag_data_t **q; 43200Sstevel@tonic-gate struct module *mp = mod->mod_mp; 43210Sstevel@tonic-gate 43220Sstevel@tonic-gate if (!(mp->flags & KOBJ_TNF_PROBE)) 43230Sstevel@tonic-gate return; 43240Sstevel@tonic-gate 43250Sstevel@tonic-gate for (p = &__tnf_probe_list_head; *p; ) 43260Sstevel@tonic-gate if (kobj_addrcheck(mp, (char *)*p) == 0) 43270Sstevel@tonic-gate *p = (*p)->next; 43280Sstevel@tonic-gate else 43290Sstevel@tonic-gate p = &(*p)->next; 43300Sstevel@tonic-gate 43310Sstevel@tonic-gate for (q = &__tnf_tag_list_head; *q; ) 43320Sstevel@tonic-gate if (kobj_addrcheck(mp, (char *)*q) == 0) 43330Sstevel@tonic-gate *q = (tnf_tag_data_t *)(*q)->tag_version; 43340Sstevel@tonic-gate else 43350Sstevel@tonic-gate q = (tnf_tag_data_t **)&(*q)->tag_version; 43360Sstevel@tonic-gate 43370Sstevel@tonic-gate tnf_changed_probe_list = 1; 43380Sstevel@tonic-gate } 43390Sstevel@tonic-gate 43400Sstevel@tonic-gate int 43410Sstevel@tonic-gate tnf_splice_probes(int boot_load, tnf_probe_control_t *plist, 43420Sstevel@tonic-gate tnf_tag_data_t *tlist) 43430Sstevel@tonic-gate { 43440Sstevel@tonic-gate int result = 0; 43450Sstevel@tonic-gate static int add_notify = 1; 43460Sstevel@tonic-gate 43470Sstevel@tonic-gate if (plist) { 43480Sstevel@tonic-gate tnf_probe_control_t *pl; 43490Sstevel@tonic-gate 43500Sstevel@tonic-gate for (pl = plist; pl->next; ) 43510Sstevel@tonic-gate pl = pl->next; 43520Sstevel@tonic-gate 43530Sstevel@tonic-gate if (!boot_load) 43540Sstevel@tonic-gate mutex_enter(&mod_lock); 43550Sstevel@tonic-gate tnf_changed_probe_list = 1; 43560Sstevel@tonic-gate pl->next = __tnf_probe_list_head; 43570Sstevel@tonic-gate __tnf_probe_list_head = plist; 43580Sstevel@tonic-gate if (!boot_load) 43590Sstevel@tonic-gate mutex_exit(&mod_lock); 43600Sstevel@tonic-gate result = 1; 43610Sstevel@tonic-gate } 43620Sstevel@tonic-gate 43630Sstevel@tonic-gate if (tlist) { 43640Sstevel@tonic-gate tnf_tag_data_t *tl; 43650Sstevel@tonic-gate 43660Sstevel@tonic-gate for (tl = tlist; tl->tag_version; ) 43670Sstevel@tonic-gate tl = (tnf_tag_data_t *)tl->tag_version; 43680Sstevel@tonic-gate 43690Sstevel@tonic-gate if (!boot_load) 43700Sstevel@tonic-gate mutex_enter(&mod_lock); 43710Sstevel@tonic-gate tl->tag_version = (tnf_tag_version_t *)__tnf_tag_list_head; 43720Sstevel@tonic-gate __tnf_tag_list_head = tlist; 43730Sstevel@tonic-gate if (!boot_load) 43740Sstevel@tonic-gate mutex_exit(&mod_lock); 43750Sstevel@tonic-gate result = 1; 43760Sstevel@tonic-gate } 43770Sstevel@tonic-gate if (!boot_load && result && add_notify) { 43780Sstevel@tonic-gate tnf_add_notifyunload(tnf_unsplice_probes); 43790Sstevel@tonic-gate add_notify = 0; 43800Sstevel@tonic-gate } 43810Sstevel@tonic-gate return (result); 43820Sstevel@tonic-gate } 43830Sstevel@tonic-gate 43840Sstevel@tonic-gate #if defined(__x86) 43850Sstevel@tonic-gate /* 43860Sstevel@tonic-gate * This code is for the purpose of manually recording which files 43870Sstevel@tonic-gate * needs to go into the boot archive on any given system. 43880Sstevel@tonic-gate * 43890Sstevel@tonic-gate * To enable the code, set kobj_file_bufsize in /etc/system 43900Sstevel@tonic-gate * and reboot the system, then use mdb to look at kobj_file_buf. 43910Sstevel@tonic-gate */ 43920Sstevel@tonic-gate static void 43930Sstevel@tonic-gate kobj_record_file(char *filename) 43940Sstevel@tonic-gate { 43950Sstevel@tonic-gate extern char *kobj_file_buf; 43960Sstevel@tonic-gate extern int kobj_file_bufsize; 43970Sstevel@tonic-gate static char *buf; 43980Sstevel@tonic-gate static int size = 0; 43990Sstevel@tonic-gate int n; 44000Sstevel@tonic-gate 44010Sstevel@tonic-gate if (standalone) /* kernel symbol not available */ 44020Sstevel@tonic-gate return; 44030Sstevel@tonic-gate 44040Sstevel@tonic-gate if (kobj_file_bufsize == 0) /* don't bother */ 44050Sstevel@tonic-gate return; 44060Sstevel@tonic-gate 44070Sstevel@tonic-gate if (kobj_file_buf == NULL) { /* allocate buffer */ 44080Sstevel@tonic-gate size = kobj_file_bufsize; 44090Sstevel@tonic-gate buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP); 44100Sstevel@tonic-gate } 44110Sstevel@tonic-gate 44120Sstevel@tonic-gate n = snprintf(buf, size, "%s\n", filename); 44130Sstevel@tonic-gate if (n > size) 44140Sstevel@tonic-gate n = size; 44150Sstevel@tonic-gate size -= n; 44160Sstevel@tonic-gate buf += n; 44170Sstevel@tonic-gate } 44180Sstevel@tonic-gate #endif /* __x86 */ 44190Sstevel@tonic-gate 44201544Seschrock static int 44211544Seschrock kobj_boot_fstat(int fd, struct bootstat *stp) 44221544Seschrock { 44231544Seschrock #if defined(__sparc) 44241544Seschrock if (!standalone && _ioquiesced) 44251544Seschrock return (-1); 44261544Seschrock return (BOP_FSTAT(ops, fd, stp)); 44271544Seschrock #else 44281544Seschrock return (BRD_FSTAT(bfs_ops, fd, stp)); 44291544Seschrock #endif 44301544Seschrock } 44311544Seschrock 44320Sstevel@tonic-gate /* 44330Sstevel@tonic-gate * XXX these wrappers should go away when sparc is converted 44340Sstevel@tonic-gate * boot from ramdisk 44350Sstevel@tonic-gate */ 44360Sstevel@tonic-gate static int 44370Sstevel@tonic-gate kobj_boot_open(char *filename, int flags) 44380Sstevel@tonic-gate { 44390Sstevel@tonic-gate #if defined(__sparc) 44400Sstevel@tonic-gate /* 44410Sstevel@tonic-gate * If io via bootops is quiesced, it means boot is no longer 44420Sstevel@tonic-gate * available to us. We make it look as if we can't open the 44430Sstevel@tonic-gate * named file - which is reasonably accurate. 44440Sstevel@tonic-gate */ 44450Sstevel@tonic-gate if (!standalone && _ioquiesced) 44460Sstevel@tonic-gate return (-1); 44470Sstevel@tonic-gate 44480Sstevel@tonic-gate return (BOP_OPEN(ops, filename, flags)); 44490Sstevel@tonic-gate #else /* x86 */ 44500Sstevel@tonic-gate kobj_record_file(filename); 44510Sstevel@tonic-gate return (BRD_OPEN(bfs_ops, filename, flags)); 44520Sstevel@tonic-gate #endif 44530Sstevel@tonic-gate } 44540Sstevel@tonic-gate 44550Sstevel@tonic-gate static int 44560Sstevel@tonic-gate kobj_boot_close(int fd) 44570Sstevel@tonic-gate { 44580Sstevel@tonic-gate #if defined(__sparc) 44590Sstevel@tonic-gate if (!standalone && _ioquiesced) 44600Sstevel@tonic-gate return (-1); 44610Sstevel@tonic-gate 44620Sstevel@tonic-gate return (BOP_CLOSE(ops, fd)); 44630Sstevel@tonic-gate #else /* x86 */ 44640Sstevel@tonic-gate return (BRD_CLOSE(bfs_ops, fd)); 44650Sstevel@tonic-gate #endif 44660Sstevel@tonic-gate } 44670Sstevel@tonic-gate 44680Sstevel@tonic-gate /*ARGSUSED*/ 44690Sstevel@tonic-gate static int 44700Sstevel@tonic-gate kobj_boot_seek(int fd, off_t hi, off_t lo) 44710Sstevel@tonic-gate { 44720Sstevel@tonic-gate #if defined(__sparc) 44730Sstevel@tonic-gate return (BOP_SEEK(ops, fd, hi, lo)); 44740Sstevel@tonic-gate #else 44750Sstevel@tonic-gate return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET)); 44760Sstevel@tonic-gate #endif 44770Sstevel@tonic-gate } 44780Sstevel@tonic-gate 44790Sstevel@tonic-gate static int 44800Sstevel@tonic-gate kobj_boot_read(int fd, caddr_t buf, size_t size) 44810Sstevel@tonic-gate { 44820Sstevel@tonic-gate #if defined(__sparc) 44830Sstevel@tonic-gate return (BOP_READ(ops, fd, buf, size)); 44840Sstevel@tonic-gate #else 44850Sstevel@tonic-gate return (BRD_READ(bfs_ops, fd, buf, size)); 44860Sstevel@tonic-gate #endif 44870Sstevel@tonic-gate } 4488