17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 227aec1d6eScindi * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Kernel's linker/loader 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/param.h> 347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 357c478bd9Sstevel@tonic-gate #include <sys/systm.h> 367c478bd9Sstevel@tonic-gate #include <sys/user.h> 377c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 387c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 397c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 407c478bd9Sstevel@tonic-gate #include <sys/debug.h> 417c478bd9Sstevel@tonic-gate #include <sys/uio.h> 427c478bd9Sstevel@tonic-gate #include <sys/file.h> 437c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 447c478bd9Sstevel@tonic-gate #include <sys/user.h> 457c478bd9Sstevel@tonic-gate #include <sys/mman.h> 467c478bd9Sstevel@tonic-gate #include <vm/as.h> 477c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h> 487c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 497c478bd9Sstevel@tonic-gate #include <sys/elf.h> 507c478bd9Sstevel@tonic-gate #include <sys/elf_notes.h> 517c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h> 527c478bd9Sstevel@tonic-gate #include <sys/kdi.h> 537c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 547c478bd9Sstevel@tonic-gate #include <sys/kmdb.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #include <sys/link.h> 577c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 587c478bd9Sstevel@tonic-gate #include <sys/ksyms.h> 597c478bd9Sstevel@tonic-gate #include <sys/disp.h> 607c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 617c478bd9Sstevel@tonic-gate #include <sys/varargs.h> 627c478bd9Sstevel@tonic-gate #include <sys/kstat.h> 637c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h> 647c478bd9Sstevel@tonic-gate #include <sys/callb.h> 657c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 667c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h> 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #include <reloc.h> 697c478bd9Sstevel@tonic-gate #include <kobj_kdi.h> 707c478bd9Sstevel@tonic-gate #include <sys/sha1.h> 717c478bd9Sstevel@tonic-gate #include <sys/crypto/elfsign.h> 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate #if !defined(__sparc) 747c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h> 757c478bd9Sstevel@tonic-gate #endif 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * do_symbols() error codes 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate #define DOSYM_UNDEF -1 /* undefined symbol */ 817c478bd9Sstevel@tonic-gate #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static struct module *load_exec(val_t *); 847c478bd9Sstevel@tonic-gate static void load_linker(val_t *); 857c478bd9Sstevel@tonic-gate static struct modctl *add_primary(char *filename, int); 867c478bd9Sstevel@tonic-gate static int bind_primary(val_t *, int); 877c478bd9Sstevel@tonic-gate static int load_primary(struct module *, int); 887c478bd9Sstevel@tonic-gate static int load_kmdb(val_t *); 897c478bd9Sstevel@tonic-gate static int get_progbits(struct module *, struct _buf *); 907c478bd9Sstevel@tonic-gate static int get_syms(struct module *, struct _buf *); 917c478bd9Sstevel@tonic-gate static int get_ctf(struct module *, struct _buf *); 927c478bd9Sstevel@tonic-gate static void get_signature(struct module *, struct _buf *); 937c478bd9Sstevel@tonic-gate static int do_common(struct module *); 947c478bd9Sstevel@tonic-gate static void add_dependent(struct module *, struct module *); 957c478bd9Sstevel@tonic-gate static int do_dependents(struct modctl *, char *, size_t); 967c478bd9Sstevel@tonic-gate static int do_symbols(struct module *, Elf64_Addr); 977c478bd9Sstevel@tonic-gate static void module_assign(struct modctl *, struct module *); 987c478bd9Sstevel@tonic-gate static void free_module_data(struct module *); 997c478bd9Sstevel@tonic-gate static char *depends_on(struct module *); 1007c478bd9Sstevel@tonic-gate static char *getmodpath(void); 1017c478bd9Sstevel@tonic-gate static char *basename(char *); 1027c478bd9Sstevel@tonic-gate static void attr_val(val_t *); 1037c478bd9Sstevel@tonic-gate static char *find_libmacro(char *); 1047c478bd9Sstevel@tonic-gate static char *expand_libmacro(char *, char *, char *); 1057c478bd9Sstevel@tonic-gate static int read_bootflags(void); 1067c478bd9Sstevel@tonic-gate static int kobj_boot_open(char *, int); 1077c478bd9Sstevel@tonic-gate static int kobj_boot_close(int); 1087c478bd9Sstevel@tonic-gate static int kobj_boot_seek(int, off_t, off_t); 1097c478bd9Sstevel@tonic-gate static int kobj_boot_read(int, caddr_t, size_t); 110ea8dc4b6Seschrock static int kobj_boot_fstat(int, struct bootstat *); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate static Sym *lookup_one(struct module *, const char *); 1137c478bd9Sstevel@tonic-gate static void sym_insert(struct module *, char *, symid_t); 1147c478bd9Sstevel@tonic-gate static Sym *sym_lookup(struct module *, Sym *); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1177c478bd9Sstevel@tonic-gate static void kprintf(void *, const char *, ...) __KPRINTFLIKE(2); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate static struct kobjopen_tctl *kobjopen_alloc(char *filename); 1207c478bd9Sstevel@tonic-gate static void kobjopen_free(struct kobjopen_tctl *ltp); 1217c478bd9Sstevel@tonic-gate static void kobjopen_thread(struct kobjopen_tctl *ltp); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate extern int kcopy(const void *, void *, size_t); 1247c478bd9Sstevel@tonic-gate extern int elf_mach_ok(Ehdr *); 1257c478bd9Sstevel@tonic-gate extern int alloc_gottable(struct module *, caddr_t *, caddr_t *); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate static void tnf_unsplice_probes(unsigned int, struct modctl *); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate extern int modrootloaded; 1307c478bd9Sstevel@tonic-gate extern int swaploaded; 1317c478bd9Sstevel@tonic-gate extern int bop_io_quiesced; 1327c478bd9Sstevel@tonic-gate extern int last_module_id; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * Values that can be or'd in to kobj_debug and their effects: 1377c478bd9Sstevel@tonic-gate * 1387c478bd9Sstevel@tonic-gate * D_DEBUG - misc. debugging information. 1397c478bd9Sstevel@tonic-gate * D_SYMBOLS - list symbols and their values as they are entered 1407c478bd9Sstevel@tonic-gate * into the hash table 1417c478bd9Sstevel@tonic-gate * D_RELOCATIONS - display relocation processing information 1427c478bd9Sstevel@tonic-gate * D_LOADING - display information about each module as it 1437c478bd9Sstevel@tonic-gate * is loaded. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate int kobj_debug = 0; 1467c478bd9Sstevel@tonic-gate #endif 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate #define MODPATH_PROPNAME "module-path" 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 1517c478bd9Sstevel@tonic-gate static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/"; 1527c478bd9Sstevel@tonic-gate #else 1537c478bd9Sstevel@tonic-gate #define slash_moddir_suffix_slash "" 1547c478bd9Sstevel@tonic-gate #endif 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate #define _moddebug get_weakish_int(&moddebug) 1577c478bd9Sstevel@tonic-gate #define _modrootloaded get_weakish_int(&modrootloaded) 1587c478bd9Sstevel@tonic-gate #define _swaploaded get_weakish_int(&swaploaded) 1597c478bd9Sstevel@tonic-gate #define _ioquiesced get_weakish_int(&bop_io_quiesced) 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate #define mod(X) (struct module *)((X)->modl_modp->mod_mp) 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate void *romp; /* rom vector (opaque to us) */ 1647c478bd9Sstevel@tonic-gate struct bootops *ops; /* bootops vector */ 1657c478bd9Sstevel@tonic-gate void *dbvec; /* debug vector */ 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * kobjopen thread control structure 1697c478bd9Sstevel@tonic-gate */ 1707c478bd9Sstevel@tonic-gate struct kobjopen_tctl { 1717c478bd9Sstevel@tonic-gate ksema_t sema; 1727c478bd9Sstevel@tonic-gate char *name; /* name of file */ 1737c478bd9Sstevel@tonic-gate struct vnode *vp; /* vnode return from vn_open() */ 1747c478bd9Sstevel@tonic-gate int Errno; /* error return from vnopen */ 1757c478bd9Sstevel@tonic-gate }; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * Structure for defining dynamically expandable library macros 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate struct lib_macro_info { 1827c478bd9Sstevel@tonic-gate char *lmi_list; /* ptr to list of possible choices */ 1837c478bd9Sstevel@tonic-gate char *lmi_macroname; /* pointer to macro name */ 1847c478bd9Sstevel@tonic-gate ushort_t lmi_ba_index; /* index into bootaux vector */ 1857c478bd9Sstevel@tonic-gate ushort_t lmi_macrolen; /* macro length */ 1867c478bd9Sstevel@tonic-gate } libmacros[] = { 1877c478bd9Sstevel@tonic-gate { NULL, "CPU", BA_CPU, 0 }, 1887c478bd9Sstevel@tonic-gate { NULL, "MMU", BA_MMU, 0 } 1897c478bd9Sstevel@tonic-gate }; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info) 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate char *boot_cpu_compatible_list; /* make $CPU available */ 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate #ifdef MPSAS 1967c478bd9Sstevel@tonic-gate void sas_prisyms(struct modctl_list *); 1977c478bd9Sstevel@tonic-gate void sas_syms(struct module *); 1987c478bd9Sstevel@tonic-gate #endif 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate vmem_t *text_arena; /* module text arena */ 2017c478bd9Sstevel@tonic-gate static vmem_t *data_arena; /* module data & bss arena */ 2027c478bd9Sstevel@tonic-gate static vmem_t *ctf_arena; /* CTF debug data arena */ 2037c478bd9Sstevel@tonic-gate static struct modctl *kobj_modules = NULL; /* modules loaded */ 2047c478bd9Sstevel@tonic-gate static char *module_path; /* module search path */ 2057c478bd9Sstevel@tonic-gate int kobj_mmu_pagesize; /* system pagesize */ 2067c478bd9Sstevel@tonic-gate static int lg_pagesize; /* "large" pagesize */ 2077c478bd9Sstevel@tonic-gate static int kobj_last_module_id = 0; /* id assignment */ 2087c478bd9Sstevel@tonic-gate static kmutex_t kobj_lock; /* protects mach memory list */ 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* 2117c478bd9Sstevel@tonic-gate * The following functions have been implemented by the kernel. 2127c478bd9Sstevel@tonic-gate * However, many 3rd party drivers provide their own implementations 2137c478bd9Sstevel@tonic-gate * of these functions. When such drivers are loaded, messages 2147c478bd9Sstevel@tonic-gate * indicateing that these symbols have been mulply defined will be 2157c478bd9Sstevel@tonic-gate * emitted to the console. To avoid alarming customers for no good 2167c478bd9Sstevel@tonic-gate * reason, we simply suppress such warnings for the following set of 2177c478bd9Sstevel@tonic-gate * functions. 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate static char *suppress_sym_list[] = 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate "strstr", 2227c478bd9Sstevel@tonic-gate "strncat", 2237c478bd9Sstevel@tonic-gate "strlcat", 2247c478bd9Sstevel@tonic-gate "strlcpy", 2257c478bd9Sstevel@tonic-gate "strspn", 2267c478bd9Sstevel@tonic-gate "memcpy", 2277c478bd9Sstevel@tonic-gate "memset", 2287c478bd9Sstevel@tonic-gate "memmove", 2297c478bd9Sstevel@tonic-gate "memcmp", 2307c478bd9Sstevel@tonic-gate "memchr", 2317c478bd9Sstevel@tonic-gate "__udivdi3", 2327c478bd9Sstevel@tonic-gate "__divdi3", 2337c478bd9Sstevel@tonic-gate "__umoddi3", 2347c478bd9Sstevel@tonic-gate "__moddi3", 2357c478bd9Sstevel@tonic-gate NULL /* This entry must exist */ 2367c478bd9Sstevel@tonic-gate }; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* indexed by KOBJ_NOTIFY_* */ 2397c478bd9Sstevel@tonic-gate static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1]; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * TNF probe management globals 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate tnf_probe_control_t *__tnf_probe_list_head = NULL; 2457c478bd9Sstevel@tonic-gate tnf_tag_data_t *__tnf_tag_list_head = NULL; 2467c478bd9Sstevel@tonic-gate int tnf_changed_probe_list = 0; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * Prefix for statically defined tracing (SDT) DTrace probes. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate const char *sdt_prefix = "__dtrace_probe_"; 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate #if defined(__sparc) 2547c478bd9Sstevel@tonic-gate /* 2557c478bd9Sstevel@tonic-gate * Some PROMs return SUNW,UltraSPARC when they actually have 2567c478bd9Sstevel@tonic-gate * SUNW,UltraSPARC-II cpus. SInce we're now filtering out all 2577c478bd9Sstevel@tonic-gate * SUNW,UltraSPARC systems during the boot phase, we can safely 2587c478bd9Sstevel@tonic-gate * point the auxv CPU value at SUNW,UltraSPARC-II. This is what 2597c478bd9Sstevel@tonic-gate * we point it at. 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate const char *ultra_2 = "SUNW,UltraSPARC-II"; 2627c478bd9Sstevel@tonic-gate #endif 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate /* 2657c478bd9Sstevel@tonic-gate * Beginning and end of the kernel's 2667c478bd9Sstevel@tonic-gate * dynamic text/data segments. 2677c478bd9Sstevel@tonic-gate */ 2687c478bd9Sstevel@tonic-gate static caddr_t _text; 2697c478bd9Sstevel@tonic-gate static caddr_t _etext; 2707c478bd9Sstevel@tonic-gate static caddr_t _data; 2717c478bd9Sstevel@tonic-gate caddr_t _edata; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate static Addr dynseg = 0; /* load address of "dynamic" segment */ 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate int standalone = 1; /* an unwholey kernel? */ 2767c478bd9Sstevel@tonic-gate int use_iflush; /* iflush after relocations */ 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate * _kobj_printf() 2807c478bd9Sstevel@tonic-gate * 2817c478bd9Sstevel@tonic-gate * Common printf function pointer. Can handle only one conversion 2827c478bd9Sstevel@tonic-gate * specification in the format string. Some of the functions invoked 2837c478bd9Sstevel@tonic-gate * through this function pointer cannot handle more that one conversion 2847c478bd9Sstevel@tonic-gate * specification in the format string. 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate void (*_kobj_printf)(void *, const char *, ...); /* printf routine */ 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate static kobj_stat_t kobj_stat; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate #define MINALIGN 8 /* at least a double-word */ 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate int 2937c478bd9Sstevel@tonic-gate get_weakish_int(int *ip) 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate if (standalone) 2967c478bd9Sstevel@tonic-gate return (0); 2977c478bd9Sstevel@tonic-gate return (ip == NULL ? 0 : *ip); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate static void * 3017c478bd9Sstevel@tonic-gate get_weakish_pointer(void **ptrp) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate if (standalone) 3047c478bd9Sstevel@tonic-gate return (0); 3057c478bd9Sstevel@tonic-gate return (ptrp == NULL ? 0 : *ptrp); 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate /* 3097c478bd9Sstevel@tonic-gate * XXX fix dependencies on "kernel"; this should work 3107c478bd9Sstevel@tonic-gate * for other standalone binaries as well. 3117c478bd9Sstevel@tonic-gate * 3127c478bd9Sstevel@tonic-gate * XXX Fix hashing code to use one pointer to 3137c478bd9Sstevel@tonic-gate * hash entries. 3147c478bd9Sstevel@tonic-gate * |----------| 3157c478bd9Sstevel@tonic-gate * | nbuckets | 3167c478bd9Sstevel@tonic-gate * |----------| 3177c478bd9Sstevel@tonic-gate * | nchains | 3187c478bd9Sstevel@tonic-gate * |----------| 3197c478bd9Sstevel@tonic-gate * | bucket[] | 3207c478bd9Sstevel@tonic-gate * |----------| 3217c478bd9Sstevel@tonic-gate * | chain[] | 3227c478bd9Sstevel@tonic-gate * |----------| 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * Load, bind and relocate all modules that 3277c478bd9Sstevel@tonic-gate * form the primary kernel. At this point, our 3287c478bd9Sstevel@tonic-gate * externals have not been relocated. 3297c478bd9Sstevel@tonic-gate */ 3307c478bd9Sstevel@tonic-gate void 3317c478bd9Sstevel@tonic-gate kobj_init( 3327c478bd9Sstevel@tonic-gate void *romvec, 3337c478bd9Sstevel@tonic-gate void *dvec, 3347c478bd9Sstevel@tonic-gate struct bootops *bootvec, 3357c478bd9Sstevel@tonic-gate val_t *bootaux) 3367c478bd9Sstevel@tonic-gate { 3377c478bd9Sstevel@tonic-gate struct module *mp; 3387c478bd9Sstevel@tonic-gate struct modctl *modp; 3397c478bd9Sstevel@tonic-gate Addr entry; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate /* 3427c478bd9Sstevel@tonic-gate * Save these to pass on to 3437c478bd9Sstevel@tonic-gate * the booted standalone. 3447c478bd9Sstevel@tonic-gate */ 3457c478bd9Sstevel@tonic-gate romp = romvec; 3467c478bd9Sstevel@tonic-gate dbvec = dvec; 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate ops = bootvec; 3497c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 3507c478bd9Sstevel@tonic-gate _kobj_printf = (void (*)(void *, const char *, ...))ops->bsys_printf; 3517c478bd9Sstevel@tonic-gate #else 3527c478bd9Sstevel@tonic-gate _kobj_printf = (void (*)(void *, const char *, ...))bop_putsarg; 3537c478bd9Sstevel@tonic-gate #endif 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate #if defined(__sparc) 3567c478bd9Sstevel@tonic-gate /* XXXQ should suppress this test on sun4v */ 3577c478bd9Sstevel@tonic-gate if (bootaux[BA_CPU].ba_ptr) { 3587c478bd9Sstevel@tonic-gate if (strcmp("SUNW,UltraSPARC", bootaux[BA_CPU].ba_ptr) == 0) { 3597c478bd9Sstevel@tonic-gate bootaux[BA_CPU].ba_ptr = (void *) ultra_2; 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate #endif 3637c478bd9Sstevel@tonic-gate /* 3647c478bd9Sstevel@tonic-gate * Save the interesting attribute-values 3657c478bd9Sstevel@tonic-gate * (scanned by kobj_boot). 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate attr_val(bootaux); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * Check bootops version. 3717c478bd9Sstevel@tonic-gate */ 3727c478bd9Sstevel@tonic-gate if (BOP_GETVERSION(ops) != BO_VERSION) { 3737c478bd9Sstevel@tonic-gate _kobj_printf(ops, "Warning: Using boot version %d, ", 3747c478bd9Sstevel@tonic-gate BOP_GETVERSION(ops)); 3757c478bd9Sstevel@tonic-gate _kobj_printf(ops, "expected %d\n", BO_VERSION); 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate /* 3797c478bd9Sstevel@tonic-gate * We don't support standalone debuggers anymore. The use of kadb 3807c478bd9Sstevel@tonic-gate * will interfere with the later use of kmdb. Let the user mend 3817c478bd9Sstevel@tonic-gate * their ways now. Users will reach this message if they still 3827c478bd9Sstevel@tonic-gate * have the kadb binary on their system (perhaps they used an old 3837c478bd9Sstevel@tonic-gate * bfu, or maybe they intentionally copied it there) and have 3847c478bd9Sstevel@tonic-gate * specified its use in a way that eluded our checking in the boot 3857c478bd9Sstevel@tonic-gate * program. 3867c478bd9Sstevel@tonic-gate */ 3877c478bd9Sstevel@tonic-gate if (dvec != NULL) { 3887c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\nWARNING: Standalone debuggers such as " 3897c478bd9Sstevel@tonic-gate "kadb are no longer supported\n\n"); 3907c478bd9Sstevel@tonic-gate goto fail; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate #ifndef __sparc 3947c478bd9Sstevel@tonic-gate { 3957c478bd9Sstevel@tonic-gate /* on x86, we always boot with a ramdisk */ 3967c478bd9Sstevel@tonic-gate extern int kobj_boot_mountroot(void); 3977c478bd9Sstevel@tonic-gate (void) kobj_boot_mountroot(); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate #endif 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate /* 4027c478bd9Sstevel@tonic-gate * Set the module search path. 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate module_path = getmodpath(); 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate boot_cpu_compatible_list = find_libmacro("CPU"); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* 4097c478bd9Sstevel@tonic-gate * These two modules have actually been 4107c478bd9Sstevel@tonic-gate * loaded by boot, but we finish the job 4117c478bd9Sstevel@tonic-gate * by introducing them into the world of 4127c478bd9Sstevel@tonic-gate * loadable modules. 4137c478bd9Sstevel@tonic-gate */ 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate mp = load_exec(bootaux); 4167c478bd9Sstevel@tonic-gate load_linker(bootaux); 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * Load all the primary dependent modules. 4207c478bd9Sstevel@tonic-gate */ 4217c478bd9Sstevel@tonic-gate if (load_primary(mp, KOBJ_LM_PRIMARY) == -1) 4227c478bd9Sstevel@tonic-gate goto fail; 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* 4257c478bd9Sstevel@tonic-gate * Glue it together. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1) 4287c478bd9Sstevel@tonic-gate goto fail; 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate entry = bootaux[BA_ENTRY].ba_val; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate #ifdef __sparc 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * On sparcv9, boot scratch memory is running out. 4357c478bd9Sstevel@tonic-gate * Free the temporary allocations here to allow boot 4367c478bd9Sstevel@tonic-gate * to continue. 4377c478bd9Sstevel@tonic-gate */ 4387c478bd9Sstevel@tonic-gate kobj_tmp_free(); 4397c478bd9Sstevel@tonic-gate #endif 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * Get the boot flags 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate bootflags(ops); 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate if (boothowto & RB_VERBOSE) 4477c478bd9Sstevel@tonic-gate kobj_lm_dump(KOBJ_LM_PRIMARY); 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate kobj_kdi_init(); 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate if (boothowto & RB_KMDB) { 4527c478bd9Sstevel@tonic-gate if (load_kmdb(bootaux) < 0) 4537c478bd9Sstevel@tonic-gate goto fail; 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * Post setup. 4587c478bd9Sstevel@tonic-gate */ 4597c478bd9Sstevel@tonic-gate #ifdef MPSAS 4607c478bd9Sstevel@tonic-gate sas_prisyms(kobj_lm_lookup(KOBJ_LM_PRIMARY)); 4617c478bd9Sstevel@tonic-gate #endif 4627c478bd9Sstevel@tonic-gate s_text = _text; 4637c478bd9Sstevel@tonic-gate e_text = _etext; 4647c478bd9Sstevel@tonic-gate s_data = _data; 4657c478bd9Sstevel@tonic-gate e_data = _edata; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate kobj_sync_instruction_memory(s_text, e_text - s_text); 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 4707c478bd9Sstevel@tonic-gate if (kobj_debug & D_DEBUG) 4717c478bd9Sstevel@tonic-gate _kobj_printf(ops, 4727c478bd9Sstevel@tonic-gate "krtld: transferring control to: 0x%p\n", entry); 4737c478bd9Sstevel@tonic-gate #endif 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * Make sure the mod system knows about the modules already loaded. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate last_module_id = kobj_last_module_id; 4797c478bd9Sstevel@tonic-gate bcopy(kobj_modules, &modules, sizeof (modules)); 4807c478bd9Sstevel@tonic-gate modp = &modules; 4817c478bd9Sstevel@tonic-gate do { 4827c478bd9Sstevel@tonic-gate if (modp->mod_next == kobj_modules) 4837c478bd9Sstevel@tonic-gate modp->mod_next = &modules; 4847c478bd9Sstevel@tonic-gate if (modp->mod_prev == kobj_modules) 4857c478bd9Sstevel@tonic-gate modp->mod_prev = &modules; 4867c478bd9Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate standalone = 0; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate #ifdef __sparc 4917c478bd9Sstevel@tonic-gate /* 4927c478bd9Sstevel@tonic-gate * On sparcv9, boot scratch memory is running out. 4937c478bd9Sstevel@tonic-gate * Free the temporary allocations here to allow boot 4947c478bd9Sstevel@tonic-gate * to continue. 4957c478bd9Sstevel@tonic-gate */ 4967c478bd9Sstevel@tonic-gate kobj_tmp_free(); 4977c478bd9Sstevel@tonic-gate #endif 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate _kobj_printf = kprintf; 5007c478bd9Sstevel@tonic-gate exitto((caddr_t)entry); 5017c478bd9Sstevel@tonic-gate fail: 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: error during initial load/link phase\n"); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate /* 5077c478bd9Sstevel@tonic-gate * Set up any global information derived 5087c478bd9Sstevel@tonic-gate * from attribute/values in the boot or 5097c478bd9Sstevel@tonic-gate * aux vector. 5107c478bd9Sstevel@tonic-gate */ 5117c478bd9Sstevel@tonic-gate static void 5127c478bd9Sstevel@tonic-gate attr_val(val_t *bootaux) 5137c478bd9Sstevel@tonic-gate { 5147c478bd9Sstevel@tonic-gate Phdr *phdr; 5157c478bd9Sstevel@tonic-gate int phnum, phsize; 5167c478bd9Sstevel@tonic-gate int i; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val; 5197c478bd9Sstevel@tonic-gate lg_pagesize = bootaux[BA_LPAGESZ].ba_val; 5207c478bd9Sstevel@tonic-gate use_iflush = bootaux[BA_IFLUSH].ba_val; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr; 5237c478bd9Sstevel@tonic-gate phnum = bootaux[BA_PHNUM].ba_val; 5247c478bd9Sstevel@tonic-gate phsize = bootaux[BA_PHENT].ba_val; 5257c478bd9Sstevel@tonic-gate for (i = 0; i < phnum; i++) { 5267c478bd9Sstevel@tonic-gate phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize); 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate if (phdr->p_type != PT_LOAD) 5297c478bd9Sstevel@tonic-gate continue; 5307c478bd9Sstevel@tonic-gate /* 5317c478bd9Sstevel@tonic-gate * Bounds of the various segments. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate if (!(phdr->p_flags & PF_X)) { 5347c478bd9Sstevel@tonic-gate dynseg = phdr->p_vaddr; 5357c478bd9Sstevel@tonic-gate } else { 5367c478bd9Sstevel@tonic-gate if (phdr->p_flags & PF_W) { 5377c478bd9Sstevel@tonic-gate _data = (caddr_t)phdr->p_vaddr; 5387c478bd9Sstevel@tonic-gate _edata = _data + phdr->p_memsz; 5397c478bd9Sstevel@tonic-gate } else { 5407c478bd9Sstevel@tonic-gate _text = (caddr_t)phdr->p_vaddr; 5417c478bd9Sstevel@tonic-gate _etext = _text + phdr->p_memsz; 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* To do the kobj_alloc, _edata needs to be set. */ 5477c478bd9Sstevel@tonic-gate for (i = 0; i < NLIBMACROS; i++) { 5487c478bd9Sstevel@tonic-gate if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) { 5497c478bd9Sstevel@tonic-gate libmacros[i].lmi_list = kobj_alloc( 5507c478bd9Sstevel@tonic-gate strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) + 5517c478bd9Sstevel@tonic-gate 1, KM_WAIT); 5527c478bd9Sstevel@tonic-gate (void) strcpy(libmacros[i].lmi_list, 5537c478bd9Sstevel@tonic-gate bootaux[libmacros[i].lmi_ba_index].ba_ptr); 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * Set up the booted executable. 5617c478bd9Sstevel@tonic-gate */ 5627c478bd9Sstevel@tonic-gate static struct module * 5637c478bd9Sstevel@tonic-gate load_exec(val_t *bootaux) 5647c478bd9Sstevel@tonic-gate { 5657c478bd9Sstevel@tonic-gate char filename[MAXPATHLEN]; 5667c478bd9Sstevel@tonic-gate struct modctl *cp; 5677c478bd9Sstevel@tonic-gate struct module *mp; 5687c478bd9Sstevel@tonic-gate Dyn *dyn; 5697c478bd9Sstevel@tonic-gate Sym *sp; 5707c478bd9Sstevel@tonic-gate int i, lsize, osize, nsize, allocsize; 5717c478bd9Sstevel@tonic-gate char *libname, *tmp; 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate (void) BOP_GETPROP(ops, "whoami", filename); 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate cp = add_primary(filename, KOBJ_LM_PRIMARY); 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 5787c478bd9Sstevel@tonic-gate cp->mod_mp = mp; 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate /* 5817c478bd9Sstevel@tonic-gate * We don't have the following information 5827c478bd9Sstevel@tonic-gate * since this module is an executable and not 5837c478bd9Sstevel@tonic-gate * a relocatable .o. 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate mp->symtbl_section = 0; 5867c478bd9Sstevel@tonic-gate mp->shdrs = NULL; 5877c478bd9Sstevel@tonic-gate mp->strhdr = NULL; 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate /* 5907c478bd9Sstevel@tonic-gate * Since this module is the only exception, 5917c478bd9Sstevel@tonic-gate * we cons up some section headers. 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 5947c478bd9Sstevel@tonic-gate mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT); 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate mp->symhdr->sh_type = SHT_SYMTAB; 5977c478bd9Sstevel@tonic-gate mp->strhdr->sh_type = SHT_STRTAB; 5987c478bd9Sstevel@tonic-gate /* 5997c478bd9Sstevel@tonic-gate * Scan the dynamic structure. 6007c478bd9Sstevel@tonic-gate */ 6017c478bd9Sstevel@tonic-gate for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 6027c478bd9Sstevel@tonic-gate dyn->d_tag != DT_NULL; dyn++) { 6037c478bd9Sstevel@tonic-gate switch (dyn->d_tag) { 6047c478bd9Sstevel@tonic-gate case DT_SYMTAB: 6057c478bd9Sstevel@tonic-gate dyn->d_un.d_ptr += dynseg; 6067c478bd9Sstevel@tonic-gate mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr; 6077c478bd9Sstevel@tonic-gate mp->symhdr->sh_addr = dyn->d_un.d_ptr; 6087c478bd9Sstevel@tonic-gate break; 6097c478bd9Sstevel@tonic-gate case DT_HASH: 6107c478bd9Sstevel@tonic-gate dyn->d_un.d_ptr += dynseg; 6117c478bd9Sstevel@tonic-gate mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1); 6127c478bd9Sstevel@tonic-gate mp->hashsize = *(uint_t *)dyn->d_un.d_ptr; 6137c478bd9Sstevel@tonic-gate break; 6147c478bd9Sstevel@tonic-gate case DT_STRTAB: 6157c478bd9Sstevel@tonic-gate dyn->d_un.d_ptr += dynseg; 6167c478bd9Sstevel@tonic-gate mp->strings = (char *)dyn->d_un.d_ptr; 6177c478bd9Sstevel@tonic-gate mp->strhdr->sh_addr = dyn->d_un.d_ptr; 6187c478bd9Sstevel@tonic-gate break; 6197c478bd9Sstevel@tonic-gate case DT_STRSZ: 6207c478bd9Sstevel@tonic-gate mp->strhdr->sh_size = dyn->d_un.d_val; 6217c478bd9Sstevel@tonic-gate break; 6227c478bd9Sstevel@tonic-gate case DT_SYMENT: 6237c478bd9Sstevel@tonic-gate mp->symhdr->sh_entsize = dyn->d_un.d_val; 6247c478bd9Sstevel@tonic-gate break; 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate /* 6297c478bd9Sstevel@tonic-gate * Collapse any DT_NEEDED entries into one string. 6307c478bd9Sstevel@tonic-gate */ 6317c478bd9Sstevel@tonic-gate nsize = osize = 0; 6327c478bd9Sstevel@tonic-gate allocsize = MAXPATHLEN; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate mp->depends_on = kobj_alloc(allocsize, KM_WAIT); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr; 6377c478bd9Sstevel@tonic-gate dyn->d_tag != DT_NULL; dyn++) 6387c478bd9Sstevel@tonic-gate if (dyn->d_tag == DT_NEEDED) { 6397c478bd9Sstevel@tonic-gate char *_lib; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate libname = mp->strings + dyn->d_un.d_val; 6427c478bd9Sstevel@tonic-gate if (strchr(libname, '$') != NULL) { 6437c478bd9Sstevel@tonic-gate if ((_lib = expand_libmacro(libname, 6447c478bd9Sstevel@tonic-gate filename, filename)) != NULL) 6457c478bd9Sstevel@tonic-gate libname = _lib; 6467c478bd9Sstevel@tonic-gate else 6477c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: " 6487c478bd9Sstevel@tonic-gate "load_exec: fail to " 6497c478bd9Sstevel@tonic-gate "expand %s\n", libname); 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate lsize = strlen(libname); 6527c478bd9Sstevel@tonic-gate nsize += lsize; 6537c478bd9Sstevel@tonic-gate if (nsize + 1 > allocsize) { 6547c478bd9Sstevel@tonic-gate tmp = kobj_alloc(allocsize + MAXPATHLEN, 6557c478bd9Sstevel@tonic-gate KM_WAIT); 6567c478bd9Sstevel@tonic-gate bcopy(mp->depends_on, tmp, osize); 6577c478bd9Sstevel@tonic-gate kobj_free(mp->depends_on, allocsize); 6587c478bd9Sstevel@tonic-gate mp->depends_on = tmp; 6597c478bd9Sstevel@tonic-gate allocsize += MAXPATHLEN; 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate bcopy(libname, mp->depends_on + osize, lsize); 6627c478bd9Sstevel@tonic-gate *(mp->depends_on + nsize) = ' '; /* seperate */ 6637c478bd9Sstevel@tonic-gate nsize++; 6647c478bd9Sstevel@tonic-gate osize = nsize; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate if (nsize) { 6677c478bd9Sstevel@tonic-gate mp->depends_on[nsize - 1] = '\0'; /* terminate the string */ 6687c478bd9Sstevel@tonic-gate /* 6697c478bd9Sstevel@tonic-gate * alloc with exact size and copy whatever it got over 6707c478bd9Sstevel@tonic-gate */ 6717c478bd9Sstevel@tonic-gate tmp = kobj_alloc(nsize, KM_WAIT); 6727c478bd9Sstevel@tonic-gate bcopy(mp->depends_on, tmp, nsize); 6737c478bd9Sstevel@tonic-gate kobj_free(mp->depends_on, allocsize); 6747c478bd9Sstevel@tonic-gate mp->depends_on = tmp; 6757c478bd9Sstevel@tonic-gate } else { 6767c478bd9Sstevel@tonic-gate kobj_free(mp->depends_on, allocsize); 6777c478bd9Sstevel@tonic-gate mp->depends_on = NULL; 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */ 6817c478bd9Sstevel@tonic-gate mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize; 6827c478bd9Sstevel@tonic-gate /* 6837c478bd9Sstevel@tonic-gate * We allocate our own table since we don't 6847c478bd9Sstevel@tonic-gate * hash undefined references. 6857c478bd9Sstevel@tonic-gate */ 6867c478bd9Sstevel@tonic-gate mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 6877c478bd9Sstevel@tonic-gate mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate mp->text = _text; 6907c478bd9Sstevel@tonic-gate mp->data = _data; 6917c478bd9Sstevel@tonic-gate cp->mod_text = mp->text; 6927c478bd9Sstevel@tonic-gate cp->mod_text_size = mp->text_size; 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate mp->filename = cp->mod_filename; 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 6977c478bd9Sstevel@tonic-gate if (kobj_debug & D_LOADING) { 6987c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 6997c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\ttext: 0x%p", mp->text); 7007c478bd9Sstevel@tonic-gate _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 7017c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\tdata: 0x%p", mp->data); 7027c478bd9Sstevel@tonic-gate _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate /* 7077c478bd9Sstevel@tonic-gate * Insert symbols into the hash table. 7087c478bd9Sstevel@tonic-gate */ 7097c478bd9Sstevel@tonic-gate for (i = 0; i < mp->nsyms; i++) { 7107c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 7137c478bd9Sstevel@tonic-gate continue; 7147c478bd9Sstevel@tonic-gate #ifdef __sparc 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Register symbols are ignored in the kernel 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) 7197c478bd9Sstevel@tonic-gate continue; 7207c478bd9Sstevel@tonic-gate #endif /* __sparc */ 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate sym_insert(mp, mp->strings + sp->st_name, i); 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate return (mp); 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate /* 7297c478bd9Sstevel@tonic-gate * Set up the linker module. 7307c478bd9Sstevel@tonic-gate */ 7317c478bd9Sstevel@tonic-gate static void 7327c478bd9Sstevel@tonic-gate load_linker(val_t *bootaux) 7337c478bd9Sstevel@tonic-gate { 7347c478bd9Sstevel@tonic-gate struct module *kmp = (struct module *)kobj_modules->mod_mp; 7357c478bd9Sstevel@tonic-gate struct module *mp; 7367c478bd9Sstevel@tonic-gate struct modctl *cp; 7377c478bd9Sstevel@tonic-gate int i; 7387c478bd9Sstevel@tonic-gate Shdr *shp; 7397c478bd9Sstevel@tonic-gate Sym *sp; 7407c478bd9Sstevel@tonic-gate int shsize; 7417c478bd9Sstevel@tonic-gate char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr; 7427c478bd9Sstevel@tonic-gate 7437c478bd9Sstevel@tonic-gate cp = add_primary(dlname, KOBJ_LM_PRIMARY); 7447c478bd9Sstevel@tonic-gate 7457c478bd9Sstevel@tonic-gate mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate cp->mod_mp = mp; 7487c478bd9Sstevel@tonic-gate mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr; 7497c478bd9Sstevel@tonic-gate shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum; 7507c478bd9Sstevel@tonic-gate mp->shdrs = kobj_alloc(shsize, KM_WAIT); 7517c478bd9Sstevel@tonic-gate bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize); 7527c478bd9Sstevel@tonic-gate 7537c478bd9Sstevel@tonic-gate for (i = 1; i < (int)mp->hdr.e_shnum; i++) { 7547c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize)); 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate if (shp->sh_flags & SHF_ALLOC) { 7577c478bd9Sstevel@tonic-gate if (shp->sh_flags & SHF_WRITE) { 7587c478bd9Sstevel@tonic-gate if (mp->data == NULL) 7597c478bd9Sstevel@tonic-gate mp->data = (char *)shp->sh_addr; 7607c478bd9Sstevel@tonic-gate } else if (mp->text == NULL) { 7617c478bd9Sstevel@tonic-gate mp->text = (char *)shp->sh_addr; 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate if (shp->sh_type == SHT_SYMTAB) { 7657c478bd9Sstevel@tonic-gate mp->symtbl_section = i; 7667c478bd9Sstevel@tonic-gate mp->symhdr = shp; 7677c478bd9Sstevel@tonic-gate mp->symspace = mp->symtbl = (char *)shp->sh_addr; 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 7717c478bd9Sstevel@tonic-gate mp->flags = KOBJ_INTERP|KOBJ_PRIM; 7727c478bd9Sstevel@tonic-gate mp->strhdr = (Shdr *) 7737c478bd9Sstevel@tonic-gate (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 7747c478bd9Sstevel@tonic-gate mp->strings = (char *)mp->strhdr->sh_addr; 7757c478bd9Sstevel@tonic-gate mp->hashsize = kobj_gethashsize(mp->nsyms); 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) + 7787c478bd9Sstevel@tonic-gate (mp->hashsize + mp->nsyms) * sizeof (symid_t); 7797c478bd9Sstevel@tonic-gate 7807c478bd9Sstevel@tonic-gate mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT); 7817c478bd9Sstevel@tonic-gate mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT); 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate mp->bss = bootaux[BA_BSS].ba_val; 7847c478bd9Sstevel@tonic-gate mp->bss_align = 0; /* pre-aligned during allocation */ 7857c478bd9Sstevel@tonic-gate mp->bss_size = (uintptr_t)_edata - mp->bss; 7867c478bd9Sstevel@tonic-gate mp->text_size = _etext - mp->text; 7877c478bd9Sstevel@tonic-gate mp->data_size = _edata - mp->data; 7887c478bd9Sstevel@tonic-gate mp->filename = cp->mod_filename; 7897c478bd9Sstevel@tonic-gate cp->mod_text = mp->text; 7907c478bd9Sstevel@tonic-gate cp->mod_text_size = mp->text_size; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate /* 7937c478bd9Sstevel@tonic-gate * Now that we've figured out where the linker is, 7947c478bd9Sstevel@tonic-gate * set the limits for the booted object. 7957c478bd9Sstevel@tonic-gate */ 7967c478bd9Sstevel@tonic-gate kmp->text_size = (size_t)(mp->text - kmp->text); 7977c478bd9Sstevel@tonic-gate kmp->data_size = (size_t)(mp->data - kmp->data); 7987c478bd9Sstevel@tonic-gate kobj_modules->mod_text_size = kmp->text_size; 7997c478bd9Sstevel@tonic-gate 8007c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 8017c478bd9Sstevel@tonic-gate if (kobj_debug & D_LOADING) { 8027c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 8037c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\ttext:0x%p", mp->text); 8047c478bd9Sstevel@tonic-gate _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 8057c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\tdata:0x%p", mp->data); 8067c478bd9Sstevel@tonic-gate _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * Insert the symbols into the hash table. 8127c478bd9Sstevel@tonic-gate */ 8137c478bd9Sstevel@tonic-gate for (i = 0; i < mp->nsyms; i++) { 8147c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 8177c478bd9Sstevel@tonic-gate continue; 8187c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 8197c478bd9Sstevel@tonic-gate if (sp->st_shndx == SHN_COMMON) 8207c478bd9Sstevel@tonic-gate sp->st_shndx = SHN_ABS; 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate sym_insert(mp, mp->strings + sp->st_name, i); 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate static kobj_notify_list_t ** 8287c478bd9Sstevel@tonic-gate kobj_notify_lookup(uint_t type) 8297c478bd9Sstevel@tonic-gate { 8307c478bd9Sstevel@tonic-gate ASSERT(type != 0 && type < sizeof (kobj_notifiers) / 8317c478bd9Sstevel@tonic-gate sizeof (kobj_notify_list_t *)); 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate return (&kobj_notifiers[type]); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate int 8377c478bd9Sstevel@tonic-gate kobj_notify_add(kobj_notify_list_t *knp) 8387c478bd9Sstevel@tonic-gate { 8397c478bd9Sstevel@tonic-gate kobj_notify_list_t **knl; 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate knl = kobj_notify_lookup(knp->kn_type); 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate knp->kn_next = NULL; 8447c478bd9Sstevel@tonic-gate knp->kn_prev = NULL; 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate mutex_enter(&kobj_lock); 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate if (*knl != NULL) { 8497c478bd9Sstevel@tonic-gate (*knl)->kn_prev = knp; 8507c478bd9Sstevel@tonic-gate knp->kn_next = *knl; 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate (*knl) = knp; 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate mutex_exit(&kobj_lock); 8557c478bd9Sstevel@tonic-gate return (0); 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate 8587c478bd9Sstevel@tonic-gate int 8597c478bd9Sstevel@tonic-gate kobj_notify_remove(kobj_notify_list_t *knp) 8607c478bd9Sstevel@tonic-gate { 8617c478bd9Sstevel@tonic-gate kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type); 8627c478bd9Sstevel@tonic-gate kobj_notify_list_t *tknp; 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate mutex_enter(&kobj_lock); 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate /* LINTED */ 8677c478bd9Sstevel@tonic-gate if (tknp = knp->kn_next) 8687c478bd9Sstevel@tonic-gate tknp->kn_prev = knp->kn_prev; 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* LINTED */ 8717c478bd9Sstevel@tonic-gate if (tknp = knp->kn_prev) 8727c478bd9Sstevel@tonic-gate tknp->kn_next = knp->kn_next; 8737c478bd9Sstevel@tonic-gate else 8747c478bd9Sstevel@tonic-gate *knl = knp->kn_next; 8757c478bd9Sstevel@tonic-gate 8767c478bd9Sstevel@tonic-gate mutex_exit(&kobj_lock); 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate return (0); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate /* 8827c478bd9Sstevel@tonic-gate * Notify all interested callbacks of a specified change in module state. 8837c478bd9Sstevel@tonic-gate */ 8847c478bd9Sstevel@tonic-gate static void 8857c478bd9Sstevel@tonic-gate kobj_notify(int type, struct modctl *modp) 8867c478bd9Sstevel@tonic-gate { 8877c478bd9Sstevel@tonic-gate kobj_notify_list_t *knp; 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate if (modp->mod_loadflags & MOD_NONOTIFY || standalone) 8907c478bd9Sstevel@tonic-gate return; 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate mutex_enter(&kobj_lock); 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next) 8957c478bd9Sstevel@tonic-gate knp->kn_func(type, modp); 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate /* 8987c478bd9Sstevel@tonic-gate * KDI notification must be last (it has to allow for work done by the 8997c478bd9Sstevel@tonic-gate * other notification callbacks), so we call it manually. 9007c478bd9Sstevel@tonic-gate */ 9017c478bd9Sstevel@tonic-gate kobj_kdi_mod_notify(type, modp); 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate mutex_exit(&kobj_lock); 9047c478bd9Sstevel@tonic-gate } 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate /* 9077c478bd9Sstevel@tonic-gate * Ask boot for the module path. 9087c478bd9Sstevel@tonic-gate */ 9097c478bd9Sstevel@tonic-gate static char * 9107c478bd9Sstevel@tonic-gate getmodpath(void) 9117c478bd9Sstevel@tonic-gate { 9127c478bd9Sstevel@tonic-gate char *path; 9137c478bd9Sstevel@tonic-gate int len; 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate if ((len = BOP_GETPROPLEN(ops, MODPATH_PROPNAME)) == -1) 9167c478bd9Sstevel@tonic-gate return (MOD_DEFPATH); 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate path = kobj_zalloc(len, KM_WAIT); 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate (void) BOP_GETPROP(ops, MODPATH_PROPNAME, path); 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate return (*path ? path : MOD_DEFPATH); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate static struct modctl * 9267c478bd9Sstevel@tonic-gate add_primary(char *filename, int lmid) 9277c478bd9Sstevel@tonic-gate { 9287c478bd9Sstevel@tonic-gate struct modctl *cp; 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT); 9317c478bd9Sstevel@tonic-gate 9327c478bd9Sstevel@tonic-gate cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT); 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate /* 9357c478bd9Sstevel@tonic-gate * For symbol lookup, we assemble our own 9367c478bd9Sstevel@tonic-gate * modctl list of the primary modules. 9377c478bd9Sstevel@tonic-gate */ 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate (void) strcpy(cp->mod_filename, filename); 9407c478bd9Sstevel@tonic-gate cp->mod_modname = basename(cp->mod_filename); 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate /* set values for modinfo assuming that the load will work */ 9437c478bd9Sstevel@tonic-gate cp->mod_prim = 1; 9447c478bd9Sstevel@tonic-gate cp->mod_loaded = 1; 9457c478bd9Sstevel@tonic-gate cp->mod_installed = 1; 9467c478bd9Sstevel@tonic-gate cp->mod_loadcnt = 1; 9477c478bd9Sstevel@tonic-gate cp->mod_loadflags = MOD_NOAUTOUNLOAD; 9487c478bd9Sstevel@tonic-gate 9497c478bd9Sstevel@tonic-gate cp->mod_id = kobj_last_module_id++; 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate /* 9527c478bd9Sstevel@tonic-gate * Link the module in. We'll pass this info on 9537c478bd9Sstevel@tonic-gate * to the mod squad later. 9547c478bd9Sstevel@tonic-gate */ 9557c478bd9Sstevel@tonic-gate if (kobj_modules == NULL) { 9567c478bd9Sstevel@tonic-gate kobj_modules = cp; 9577c478bd9Sstevel@tonic-gate cp->mod_prev = cp->mod_next = cp; 9587c478bd9Sstevel@tonic-gate } else { 9597c478bd9Sstevel@tonic-gate cp->mod_prev = kobj_modules->mod_prev; 9607c478bd9Sstevel@tonic-gate cp->mod_next = kobj_modules; 9617c478bd9Sstevel@tonic-gate kobj_modules->mod_prev->mod_next = cp; 9627c478bd9Sstevel@tonic-gate kobj_modules->mod_prev = cp; 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate kobj_lm_append(lmid, cp); 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate return (cp); 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate static int 9717c478bd9Sstevel@tonic-gate bind_primary(val_t *bootaux, int lmid) 9727c478bd9Sstevel@tonic-gate { 9737c478bd9Sstevel@tonic-gate struct modctl_list *linkmap = kobj_lm_lookup(lmid); 9747c478bd9Sstevel@tonic-gate struct modctl_list *lp; 9757c478bd9Sstevel@tonic-gate struct module *mp; 9767c478bd9Sstevel@tonic-gate Dyn *dyn; 9777c478bd9Sstevel@tonic-gate Word relasz; 9787c478bd9Sstevel@tonic-gate Word relaent; 9797c478bd9Sstevel@tonic-gate char *rela; 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate /* 9827c478bd9Sstevel@tonic-gate * Do common symbols. 9837c478bd9Sstevel@tonic-gate */ 9847c478bd9Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 9857c478bd9Sstevel@tonic-gate mp = mod(lp); 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate /* 9887c478bd9Sstevel@tonic-gate * Don't do common section relocations for modules that 9897c478bd9Sstevel@tonic-gate * don't need it. 9907c478bd9Sstevel@tonic-gate */ 9917c478bd9Sstevel@tonic-gate if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP)) 9927c478bd9Sstevel@tonic-gate continue; 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate if (do_common(mp) < 0) 9957c478bd9Sstevel@tonic-gate return (-1); 9967c478bd9Sstevel@tonic-gate } 9977c478bd9Sstevel@tonic-gate 9987c478bd9Sstevel@tonic-gate /* 9997c478bd9Sstevel@tonic-gate * Resolve symbols. 10007c478bd9Sstevel@tonic-gate */ 10017c478bd9Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 10027c478bd9Sstevel@tonic-gate mp = mod(lp); 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate if (do_symbols(mp, 0) < 0) 10057c478bd9Sstevel@tonic-gate return (-1); 10067c478bd9Sstevel@tonic-gate } 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate /* 10097c478bd9Sstevel@tonic-gate * Do relocations. 10107c478bd9Sstevel@tonic-gate */ 10117c478bd9Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 10127c478bd9Sstevel@tonic-gate mp = mod(lp); 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate if (mp->flags & KOBJ_EXEC) { 10157c478bd9Sstevel@tonic-gate Word shtype; 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate relasz = 0; 10187c478bd9Sstevel@tonic-gate relaent = 0; 10197c478bd9Sstevel@tonic-gate rela = NULL; 10207c478bd9Sstevel@tonic-gate 10217c478bd9Sstevel@tonic-gate for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr; 10227c478bd9Sstevel@tonic-gate dyn->d_tag != DT_NULL; dyn++) { 10237c478bd9Sstevel@tonic-gate switch (dyn->d_tag) { 10247c478bd9Sstevel@tonic-gate case DT_RELASZ: 10257c478bd9Sstevel@tonic-gate case DT_RELSZ: 10267c478bd9Sstevel@tonic-gate relasz = dyn->d_un.d_val; 10277c478bd9Sstevel@tonic-gate break; 10287c478bd9Sstevel@tonic-gate case DT_RELAENT: 10297c478bd9Sstevel@tonic-gate case DT_RELENT: 10307c478bd9Sstevel@tonic-gate relaent = dyn->d_un.d_val; 10317c478bd9Sstevel@tonic-gate break; 10327c478bd9Sstevel@tonic-gate case DT_RELA: 10337c478bd9Sstevel@tonic-gate shtype = SHT_RELA; 10347c478bd9Sstevel@tonic-gate rela = (char *)(dyn->d_un.d_ptr + 10357c478bd9Sstevel@tonic-gate dynseg); 10367c478bd9Sstevel@tonic-gate break; 10377c478bd9Sstevel@tonic-gate case DT_REL: 10387c478bd9Sstevel@tonic-gate shtype = SHT_REL; 10397c478bd9Sstevel@tonic-gate rela = (char *)(dyn->d_un.d_ptr + 10407c478bd9Sstevel@tonic-gate dynseg); 10417c478bd9Sstevel@tonic-gate break; 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate } 10447c478bd9Sstevel@tonic-gate if (relasz == 0 || 10457c478bd9Sstevel@tonic-gate relaent == 0 || rela == NULL) { 10467c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: bind_primary(): " 10477c478bd9Sstevel@tonic-gate "no relocation information found for " 10487c478bd9Sstevel@tonic-gate "module %s\n", mp->filename); 10497c478bd9Sstevel@tonic-gate return (-1); 10507c478bd9Sstevel@tonic-gate } 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 10537c478bd9Sstevel@tonic-gate if (kobj_debug & D_RELOCATIONS) 10547c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: relocating: file=%s " 10557c478bd9Sstevel@tonic-gate "KOBJ_EXEC\n", mp->filename); 10567c478bd9Sstevel@tonic-gate #endif 10577c478bd9Sstevel@tonic-gate if (do_relocate(mp, rela, shtype, relasz/relaent, 10587c478bd9Sstevel@tonic-gate relaent, (Addr)mp->text) < 0) 10597c478bd9Sstevel@tonic-gate return (-1); 10607c478bd9Sstevel@tonic-gate } else { 10617c478bd9Sstevel@tonic-gate if (do_relocations(mp) < 0) 10627c478bd9Sstevel@tonic-gate return (-1); 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate /* sync_instruction_memory */ 10667c478bd9Sstevel@tonic-gate kobj_sync_instruction_memory(mp->text, mp->text_size); 10677c478bd9Sstevel@tonic-gate } 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate for (lp = linkmap; lp; lp = lp->modl_next) { 10707c478bd9Sstevel@tonic-gate mp = mod(lp); 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate /* 10737c478bd9Sstevel@tonic-gate * We need to re-read the full symbol table for the boot file, 10747c478bd9Sstevel@tonic-gate * since we couldn't use the full one before. We also need to 10757c478bd9Sstevel@tonic-gate * load the CTF sections of both the boot file and the 10767c478bd9Sstevel@tonic-gate * interpreter (us). 10777c478bd9Sstevel@tonic-gate */ 10787c478bd9Sstevel@tonic-gate if (mp->flags & KOBJ_EXEC) { 10797c478bd9Sstevel@tonic-gate struct _buf *file; 10807c478bd9Sstevel@tonic-gate int n; 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate file = kobj_open_file(mp->filename); 10837c478bd9Sstevel@tonic-gate if (file == (struct _buf *)-1) 10847c478bd9Sstevel@tonic-gate return (-1); 10857c478bd9Sstevel@tonic-gate if (kobj_read_file(file, (char *)&mp->hdr, 10867c478bd9Sstevel@tonic-gate sizeof (mp->hdr), 0) < 0) 10877c478bd9Sstevel@tonic-gate return (-1); 10887c478bd9Sstevel@tonic-gate n = mp->hdr.e_shentsize * mp->hdr.e_shnum; 10897c478bd9Sstevel@tonic-gate mp->shdrs = kobj_alloc(n, KM_WAIT); 10907c478bd9Sstevel@tonic-gate if (kobj_read_file(file, mp->shdrs, n, 10917c478bd9Sstevel@tonic-gate mp->hdr.e_shoff) < 0) 10927c478bd9Sstevel@tonic-gate return (-1); 10937c478bd9Sstevel@tonic-gate if (get_syms(mp, file) < 0) 10947c478bd9Sstevel@tonic-gate return (-1); 10957c478bd9Sstevel@tonic-gate if (get_ctf(mp, file) < 0) 10967c478bd9Sstevel@tonic-gate return (-1); 10977c478bd9Sstevel@tonic-gate kobj_close_file(file); 10987c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_RELOCATED; 10997c478bd9Sstevel@tonic-gate 11007c478bd9Sstevel@tonic-gate } else if (mp->flags & KOBJ_INTERP) { 11017c478bd9Sstevel@tonic-gate struct _buf *file; 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate /* 11047c478bd9Sstevel@tonic-gate * The interpreter path fragment in mp->filename 11057c478bd9Sstevel@tonic-gate * will already have the module directory suffix 11067c478bd9Sstevel@tonic-gate * in it (if appropriate). 11077c478bd9Sstevel@tonic-gate */ 11087c478bd9Sstevel@tonic-gate file = kobj_open_path(mp->filename, 1, 0); 11097c478bd9Sstevel@tonic-gate if (file == (struct _buf *)-1) 11107c478bd9Sstevel@tonic-gate return (-1); 11117c478bd9Sstevel@tonic-gate if (get_ctf(mp, file) < 0) 11127c478bd9Sstevel@tonic-gate return (-1); 11137c478bd9Sstevel@tonic-gate kobj_close_file(file); 11147c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_RELOCATED; 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate return (0); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate static struct modctl * 11227c478bd9Sstevel@tonic-gate mod_already_loaded(char *modname) 11237c478bd9Sstevel@tonic-gate { 11247c478bd9Sstevel@tonic-gate struct modctl *mctl = kobj_modules; 11257c478bd9Sstevel@tonic-gate 11267c478bd9Sstevel@tonic-gate do { 11277c478bd9Sstevel@tonic-gate if (strcmp(modname, mctl->mod_filename) == 0) 11287c478bd9Sstevel@tonic-gate return (mctl); 11297c478bd9Sstevel@tonic-gate mctl = mctl->mod_next; 11307c478bd9Sstevel@tonic-gate 11317c478bd9Sstevel@tonic-gate } while (mctl != kobj_modules); 11327c478bd9Sstevel@tonic-gate 11337c478bd9Sstevel@tonic-gate return (NULL); 11347c478bd9Sstevel@tonic-gate } 11357c478bd9Sstevel@tonic-gate 11367c478bd9Sstevel@tonic-gate /* 11377c478bd9Sstevel@tonic-gate * Load all the primary dependent modules. 11387c478bd9Sstevel@tonic-gate */ 11397c478bd9Sstevel@tonic-gate static int 11407c478bd9Sstevel@tonic-gate load_primary(struct module *mp, int lmid) 11417c478bd9Sstevel@tonic-gate { 11427c478bd9Sstevel@tonic-gate struct modctl *cp; 11437c478bd9Sstevel@tonic-gate struct module *dmp; 11447c478bd9Sstevel@tonic-gate char *p, *q; 11457c478bd9Sstevel@tonic-gate char modname[MODMAXNAMELEN]; 11467c478bd9Sstevel@tonic-gate 11477c478bd9Sstevel@tonic-gate if ((p = mp->depends_on) == NULL) 11487c478bd9Sstevel@tonic-gate return (0); 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate /* CONSTANTCONDITION */ 11517c478bd9Sstevel@tonic-gate while (1) { 11527c478bd9Sstevel@tonic-gate /* 11537c478bd9Sstevel@tonic-gate * Skip space. 11547c478bd9Sstevel@tonic-gate */ 11557c478bd9Sstevel@tonic-gate while (*p && (*p == ' ' || *p == '\t')) 11567c478bd9Sstevel@tonic-gate p++; 11577c478bd9Sstevel@tonic-gate /* 11587c478bd9Sstevel@tonic-gate * Get module name. 11597c478bd9Sstevel@tonic-gate */ 11607c478bd9Sstevel@tonic-gate q = modname; 11617c478bd9Sstevel@tonic-gate while (*p && *p != ' ' && *p != '\t') 11627c478bd9Sstevel@tonic-gate *q++ = *p++; 11637c478bd9Sstevel@tonic-gate 11647c478bd9Sstevel@tonic-gate if (q == modname) 11657c478bd9Sstevel@tonic-gate break; 11667c478bd9Sstevel@tonic-gate 11677c478bd9Sstevel@tonic-gate *q = '\0'; 11687c478bd9Sstevel@tonic-gate /* 11697c478bd9Sstevel@tonic-gate * Check for dup dependencies. 11707c478bd9Sstevel@tonic-gate */ 11717c478bd9Sstevel@tonic-gate if (strcmp(modname, "dtracestubs") == 0 || 11727c478bd9Sstevel@tonic-gate mod_already_loaded(modname) != NULL) 11737c478bd9Sstevel@tonic-gate continue; 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate cp = add_primary(modname, lmid); 11767c478bd9Sstevel@tonic-gate cp->mod_busy = 1; 11777c478bd9Sstevel@tonic-gate /* 11787c478bd9Sstevel@tonic-gate * Load it. 11797c478bd9Sstevel@tonic-gate */ 11807c478bd9Sstevel@tonic-gate (void) kobj_load_module(cp, 1); 11817c478bd9Sstevel@tonic-gate cp->mod_busy = 0; 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate if ((dmp = cp->mod_mp) == NULL) { 11847c478bd9Sstevel@tonic-gate cp->mod_loaded = 0; 11857c478bd9Sstevel@tonic-gate cp->mod_installed = 0; 11867c478bd9Sstevel@tonic-gate cp->mod_loadcnt = 0; 11877c478bd9Sstevel@tonic-gate return (-1); 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate add_dependent(mp, dmp); 11917c478bd9Sstevel@tonic-gate dmp->flags |= KOBJ_PRIM; 11927c478bd9Sstevel@tonic-gate 11937c478bd9Sstevel@tonic-gate /* 11947c478bd9Sstevel@tonic-gate * Recurse. 11957c478bd9Sstevel@tonic-gate */ 11967c478bd9Sstevel@tonic-gate if (load_primary(dmp, lmid) == -1) { 11977c478bd9Sstevel@tonic-gate cp->mod_loaded = 0; 11987c478bd9Sstevel@tonic-gate cp->mod_installed = 0; 11997c478bd9Sstevel@tonic-gate cp->mod_loadcnt = 0; 12007c478bd9Sstevel@tonic-gate return (-1); 12017c478bd9Sstevel@tonic-gate } 12027c478bd9Sstevel@tonic-gate } 12037c478bd9Sstevel@tonic-gate return (0); 12047c478bd9Sstevel@tonic-gate } 12057c478bd9Sstevel@tonic-gate 12067c478bd9Sstevel@tonic-gate static int 1207*28cdc3d7Sszhou console_is_usb_serial(void) 1208*28cdc3d7Sszhou { 1209*28cdc3d7Sszhou char *console; 1210*28cdc3d7Sszhou int len, ret; 1211*28cdc3d7Sszhou 1212*28cdc3d7Sszhou if ((len = BOP_GETPROPLEN(ops, "console")) == -1) 1213*28cdc3d7Sszhou return (0); 1214*28cdc3d7Sszhou 1215*28cdc3d7Sszhou console = kobj_zalloc(len, KM_WAIT|KM_TMP); 1216*28cdc3d7Sszhou (void) BOP_GETPROP(ops, "console", console); 1217*28cdc3d7Sszhou ret = (strcmp(console, "usb-serial") == 0); 1218*28cdc3d7Sszhou kobj_free(console, len); 1219*28cdc3d7Sszhou 1220*28cdc3d7Sszhou return (ret); 1221*28cdc3d7Sszhou } 1222*28cdc3d7Sszhou 1223*28cdc3d7Sszhou static int 12247c478bd9Sstevel@tonic-gate load_kmdb(val_t *bootaux) 12257c478bd9Sstevel@tonic-gate { 12267c478bd9Sstevel@tonic-gate struct modctl *mctl; 12277c478bd9Sstevel@tonic-gate struct module *mp; 12287c478bd9Sstevel@tonic-gate Sym *sym; 12297c478bd9Sstevel@tonic-gate 1230*28cdc3d7Sszhou if (console_is_usb_serial()) { 1231*28cdc3d7Sszhou _kobj_printf(ops, "kmdb not loaded " 1232*28cdc3d7Sszhou "(unsupported on usb serial console)\n"); 1233*28cdc3d7Sszhou return (0); 1234*28cdc3d7Sszhou } 1235*28cdc3d7Sszhou 12367c478bd9Sstevel@tonic-gate _kobj_printf(ops, "Loading kmdb...\n"); 12377c478bd9Sstevel@tonic-gate 12387c478bd9Sstevel@tonic-gate if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL) 12397c478bd9Sstevel@tonic-gate return (-1); 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate mctl->mod_busy = 1; 12427c478bd9Sstevel@tonic-gate (void) kobj_load_module(mctl, 1); 12437c478bd9Sstevel@tonic-gate mctl->mod_busy = 0; 12447c478bd9Sstevel@tonic-gate 12457c478bd9Sstevel@tonic-gate if ((mp = mctl->mod_mp) == NULL) 12467c478bd9Sstevel@tonic-gate return (-1); 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_PRIM; 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0) 12517c478bd9Sstevel@tonic-gate return (-1); 12527c478bd9Sstevel@tonic-gate 12537c478bd9Sstevel@tonic-gate if (boothowto & RB_VERBOSE) 12547c478bd9Sstevel@tonic-gate kobj_lm_dump(KOBJ_LM_DEBUGGER); 12557c478bd9Sstevel@tonic-gate 12567c478bd9Sstevel@tonic-gate if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0) 12577c478bd9Sstevel@tonic-gate return (-1); 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL) 12607c478bd9Sstevel@tonic-gate return (-1); 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0, 12637c478bd9Sstevel@tonic-gate (const char **)kobj_kmdb_argv) < 0) 12647c478bd9Sstevel@tonic-gate return (-1); 12657c478bd9Sstevel@tonic-gate 12667c478bd9Sstevel@tonic-gate return (0); 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate /* 12707c478bd9Sstevel@tonic-gate * Return a string listing module dependencies. 12717c478bd9Sstevel@tonic-gate */ 12727c478bd9Sstevel@tonic-gate static char * 12737c478bd9Sstevel@tonic-gate depends_on(struct module *mp) 12747c478bd9Sstevel@tonic-gate { 12757c478bd9Sstevel@tonic-gate Sym *sp; 12767c478bd9Sstevel@tonic-gate char *depstr, *q; 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * The module doesn't have a depends_on value, so let's try it the 12807c478bd9Sstevel@tonic-gate * old-fashioned way - via "_depends_on" 12817c478bd9Sstevel@tonic-gate */ 12827c478bd9Sstevel@tonic-gate if ((sp = lookup_one(mp, "_depends_on")) == NULL) 12837c478bd9Sstevel@tonic-gate return (NULL); 12847c478bd9Sstevel@tonic-gate 12857c478bd9Sstevel@tonic-gate q = (char *)sp->st_value; 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate /* 12887c478bd9Sstevel@tonic-gate * Idiot checks. Make sure it's 12897c478bd9Sstevel@tonic-gate * in-bounds and NULL terminated. 12907c478bd9Sstevel@tonic-gate */ 12917c478bd9Sstevel@tonic-gate if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') { 12927c478bd9Sstevel@tonic-gate _kobj_printf(ops, "Error processing dependency for %s\n", 12937c478bd9Sstevel@tonic-gate mp->filename); 12947c478bd9Sstevel@tonic-gate return (NULL); 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT); 12987c478bd9Sstevel@tonic-gate (void) strcpy(depstr, q); 12997c478bd9Sstevel@tonic-gate 13007c478bd9Sstevel@tonic-gate return (depstr); 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate void 13047c478bd9Sstevel@tonic-gate kobj_getmodinfo(void *xmp, struct modinfo *modinfo) 13057c478bd9Sstevel@tonic-gate { 13067c478bd9Sstevel@tonic-gate struct module *mp; 13077c478bd9Sstevel@tonic-gate mp = (struct module *)xmp; 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate modinfo->mi_base = mp->text; 13107c478bd9Sstevel@tonic-gate modinfo->mi_size = mp->text_size + mp->data_size; 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate 13137c478bd9Sstevel@tonic-gate /* 13147c478bd9Sstevel@tonic-gate * kobj_export_ksyms() performs the following services: 13157c478bd9Sstevel@tonic-gate * 13167c478bd9Sstevel@tonic-gate * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena. 13177c478bd9Sstevel@tonic-gate * (2) Removes unneeded symbols to save space. 13187c478bd9Sstevel@tonic-gate * (3) Reduces memory footprint by using VM_BESTFIT allocations. 13197c478bd9Sstevel@tonic-gate * (4) Makes the symbol table visible to /dev/ksyms. 13207c478bd9Sstevel@tonic-gate */ 13217c478bd9Sstevel@tonic-gate static void 13227c478bd9Sstevel@tonic-gate kobj_export_ksyms(struct module *mp) 13237c478bd9Sstevel@tonic-gate { 13247c478bd9Sstevel@tonic-gate Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size); 13257c478bd9Sstevel@tonic-gate Sym *sp, *osp; 13267c478bd9Sstevel@tonic-gate char *name; 13277c478bd9Sstevel@tonic-gate size_t namelen; 13287c478bd9Sstevel@tonic-gate struct module *omp; 13297c478bd9Sstevel@tonic-gate uint_t nsyms; 13307c478bd9Sstevel@tonic-gate size_t symsize = mp->symhdr->sh_entsize; 13317c478bd9Sstevel@tonic-gate size_t locals = 1; 13327c478bd9Sstevel@tonic-gate size_t strsize; 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate /* 13357c478bd9Sstevel@tonic-gate * Make a copy of the original module structure. 13367c478bd9Sstevel@tonic-gate */ 13377c478bd9Sstevel@tonic-gate omp = kobj_alloc(sizeof (struct module), KM_WAIT); 13387c478bd9Sstevel@tonic-gate bcopy(mp, omp, sizeof (struct module)); 13397c478bd9Sstevel@tonic-gate 13407c478bd9Sstevel@tonic-gate /* 13417c478bd9Sstevel@tonic-gate * Compute the sizes of the new symbol table sections. 13427c478bd9Sstevel@tonic-gate */ 13437c478bd9Sstevel@tonic-gate for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) { 13447c478bd9Sstevel@tonic-gate if (osp->st_value == 0) 13457c478bd9Sstevel@tonic-gate continue; 13467c478bd9Sstevel@tonic-gate if (sym_lookup(omp, osp) == NULL) 13477c478bd9Sstevel@tonic-gate continue; 13487c478bd9Sstevel@tonic-gate name = omp->strings + osp->st_name; 13497c478bd9Sstevel@tonic-gate namelen = strlen(name); 13507c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(osp->st_info) == STB_LOCAL) 13517c478bd9Sstevel@tonic-gate locals++; 13527c478bd9Sstevel@tonic-gate nsyms++; 13537c478bd9Sstevel@tonic-gate strsize += namelen + 1; 13547c478bd9Sstevel@tonic-gate } 13557c478bd9Sstevel@tonic-gate 13567c478bd9Sstevel@tonic-gate mp->nsyms = nsyms; 13577c478bd9Sstevel@tonic-gate mp->hashsize = kobj_gethashsize(mp->nsyms); 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate /* 13607c478bd9Sstevel@tonic-gate * ksyms_lock must be held as writer during any operation that 13617c478bd9Sstevel@tonic-gate * modifies ksyms_arena, including allocation from same, and 13627c478bd9Sstevel@tonic-gate * must not be dropped until the arena is vmem_walk()able. 13637c478bd9Sstevel@tonic-gate */ 13647c478bd9Sstevel@tonic-gate rw_enter(&ksyms_lock, RW_WRITER); 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate /* 13677c478bd9Sstevel@tonic-gate * Allocate space for the new section headers (symtab and strtab), 13687c478bd9Sstevel@tonic-gate * symbol table, buckets, chains, and strings. 13697c478bd9Sstevel@tonic-gate */ 13707c478bd9Sstevel@tonic-gate mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) + 13717c478bd9Sstevel@tonic-gate (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize; 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate if (mp->flags & KOBJ_NOKSYMS) { 13747c478bd9Sstevel@tonic-gate mp->symspace = kobj_alloc(mp->symsize, KM_WAIT); 13757c478bd9Sstevel@tonic-gate } else { 13767c478bd9Sstevel@tonic-gate mp->symspace = vmem_alloc(ksyms_arena, mp->symsize, 13777c478bd9Sstevel@tonic-gate VM_BESTFIT | VM_SLEEP); 13787c478bd9Sstevel@tonic-gate } 13797c478bd9Sstevel@tonic-gate bzero(mp->symspace, mp->symsize); 13807c478bd9Sstevel@tonic-gate 13817c478bd9Sstevel@tonic-gate /* 13827c478bd9Sstevel@tonic-gate * Divvy up symspace. 13837c478bd9Sstevel@tonic-gate */ 13847c478bd9Sstevel@tonic-gate mp->shdrs = mp->symspace; 13857c478bd9Sstevel@tonic-gate mp->symhdr = (Shdr *)mp->shdrs; 13867c478bd9Sstevel@tonic-gate mp->strhdr = (Shdr *)(mp->symhdr + 1); 13877c478bd9Sstevel@tonic-gate mp->symtbl = (char *)(mp->strhdr + 1); 13887c478bd9Sstevel@tonic-gate mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize)); 13897c478bd9Sstevel@tonic-gate mp->chains = (symid_t *)(mp->buckets + mp->hashsize); 13907c478bd9Sstevel@tonic-gate mp->strings = (char *)(mp->chains + nsyms); 13917c478bd9Sstevel@tonic-gate 13927c478bd9Sstevel@tonic-gate /* 13937c478bd9Sstevel@tonic-gate * Fill in the new section headers (symtab and strtab). 13947c478bd9Sstevel@tonic-gate */ 13957c478bd9Sstevel@tonic-gate mp->hdr.e_shnum = 2; 13967c478bd9Sstevel@tonic-gate mp->symtbl_section = 0; 13977c478bd9Sstevel@tonic-gate 13987c478bd9Sstevel@tonic-gate mp->symhdr->sh_type = SHT_SYMTAB; 13997c478bd9Sstevel@tonic-gate mp->symhdr->sh_addr = (Addr)mp->symtbl; 14007c478bd9Sstevel@tonic-gate mp->symhdr->sh_size = nsyms * symsize; 14017c478bd9Sstevel@tonic-gate mp->symhdr->sh_link = 1; 14027c478bd9Sstevel@tonic-gate mp->symhdr->sh_info = locals; 14037c478bd9Sstevel@tonic-gate mp->symhdr->sh_addralign = sizeof (Addr); 14047c478bd9Sstevel@tonic-gate mp->symhdr->sh_entsize = symsize; 14057c478bd9Sstevel@tonic-gate 14067c478bd9Sstevel@tonic-gate mp->strhdr->sh_type = SHT_STRTAB; 14077c478bd9Sstevel@tonic-gate mp->strhdr->sh_addr = (Addr)mp->strings; 14087c478bd9Sstevel@tonic-gate mp->strhdr->sh_size = strsize; 14097c478bd9Sstevel@tonic-gate mp->strhdr->sh_addralign = 1; 14107c478bd9Sstevel@tonic-gate 14117c478bd9Sstevel@tonic-gate /* 14127c478bd9Sstevel@tonic-gate * Construct the new symbol table. 14137c478bd9Sstevel@tonic-gate */ 14147c478bd9Sstevel@tonic-gate for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) { 14157c478bd9Sstevel@tonic-gate if (osp->st_value == 0) 14167c478bd9Sstevel@tonic-gate continue; 14177c478bd9Sstevel@tonic-gate if (sym_lookup(omp, osp) == NULL) 14187c478bd9Sstevel@tonic-gate continue; 14197c478bd9Sstevel@tonic-gate name = omp->strings + osp->st_name; 14207c478bd9Sstevel@tonic-gate namelen = strlen(name); 14217c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + symsize * nsyms); 14227c478bd9Sstevel@tonic-gate bcopy(osp, sp, symsize); 14237c478bd9Sstevel@tonic-gate bcopy(name, mp->strings + strsize, namelen); 14247c478bd9Sstevel@tonic-gate sp->st_name = strsize; 14257c478bd9Sstevel@tonic-gate sym_insert(mp, name, nsyms); 14267c478bd9Sstevel@tonic-gate nsyms++; 14277c478bd9Sstevel@tonic-gate strsize += namelen + 1; 14287c478bd9Sstevel@tonic-gate } 14297c478bd9Sstevel@tonic-gate 14307c478bd9Sstevel@tonic-gate rw_exit(&ksyms_lock); 14317c478bd9Sstevel@tonic-gate 14327c478bd9Sstevel@tonic-gate /* 14337c478bd9Sstevel@tonic-gate * Free the old section headers -- we'll never need them again. 14347c478bd9Sstevel@tonic-gate */ 14357c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 14367c478bd9Sstevel@tonic-gate kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum); 14377c478bd9Sstevel@tonic-gate /* 14387c478bd9Sstevel@tonic-gate * Discard the old symbol table and our copy of the module strucure. 14397c478bd9Sstevel@tonic-gate */ 14407c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 14417c478bd9Sstevel@tonic-gate kobj_free(omp->symspace, omp->symsize); 14427c478bd9Sstevel@tonic-gate kobj_free(omp, sizeof (struct module)); 14437c478bd9Sstevel@tonic-gate } 14447c478bd9Sstevel@tonic-gate 14457c478bd9Sstevel@tonic-gate static void 14467c478bd9Sstevel@tonic-gate kobj_export_ctf(struct module *mp) 14477c478bd9Sstevel@tonic-gate { 14487c478bd9Sstevel@tonic-gate char *data = mp->ctfdata; 14497c478bd9Sstevel@tonic-gate size_t size = mp->ctfsize; 14507c478bd9Sstevel@tonic-gate 14517c478bd9Sstevel@tonic-gate if (data != NULL) { 14527c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_NOCTF) { 14537c478bd9Sstevel@tonic-gate mp->ctfdata = NULL; 14547c478bd9Sstevel@tonic-gate mp->ctfsize = 0; 14557c478bd9Sstevel@tonic-gate } else { 14567c478bd9Sstevel@tonic-gate mp->ctfdata = vmem_alloc(ctf_arena, size, 14577c478bd9Sstevel@tonic-gate VM_BESTFIT | VM_SLEEP); 14587c478bd9Sstevel@tonic-gate bcopy(data, mp->ctfdata, size); 14597c478bd9Sstevel@tonic-gate } 14607c478bd9Sstevel@tonic-gate 14617c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 14627c478bd9Sstevel@tonic-gate kobj_free(data, size); 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate void 14677c478bd9Sstevel@tonic-gate kobj_export_module(struct module *mp) 14687c478bd9Sstevel@tonic-gate { 14697c478bd9Sstevel@tonic-gate kobj_export_ksyms(mp); 14707c478bd9Sstevel@tonic-gate kobj_export_ctf(mp); 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_EXPORTED; 14737c478bd9Sstevel@tonic-gate } 14747c478bd9Sstevel@tonic-gate 14757c478bd9Sstevel@tonic-gate static int 14767c478bd9Sstevel@tonic-gate process_dynamic(struct module *mp, char *dyndata, char *strdata) 14777c478bd9Sstevel@tonic-gate { 14787c478bd9Sstevel@tonic-gate char *path = NULL, *depstr = NULL; 14797c478bd9Sstevel@tonic-gate int allocsize = 0, osize = 0, nsize = 0; 14807c478bd9Sstevel@tonic-gate char *libname, *tmp; 14817c478bd9Sstevel@tonic-gate int lsize; 14827c478bd9Sstevel@tonic-gate Dyn *dynp; 14837c478bd9Sstevel@tonic-gate 14847c478bd9Sstevel@tonic-gate for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) { 14857c478bd9Sstevel@tonic-gate switch (dynp->d_tag) { 14867c478bd9Sstevel@tonic-gate case DT_NEEDED: 14877c478bd9Sstevel@tonic-gate /* 14887c478bd9Sstevel@tonic-gate * Read the DT_NEEDED entries, expanding the macros they 14897c478bd9Sstevel@tonic-gate * contain (if any), and concatenating them into a 14907c478bd9Sstevel@tonic-gate * single space-separated dependency list. 14917c478bd9Sstevel@tonic-gate */ 14927c478bd9Sstevel@tonic-gate libname = (ulong_t)dynp->d_un.d_ptr + strdata; 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate if (strchr(libname, '$') != NULL) { 14957c478bd9Sstevel@tonic-gate char *_lib; 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate if (path == NULL) 14987c478bd9Sstevel@tonic-gate path = kobj_alloc(MAXPATHLEN, KM_WAIT); 14997c478bd9Sstevel@tonic-gate if ((_lib = expand_libmacro(libname, path, 15007c478bd9Sstevel@tonic-gate path)) != NULL) 15017c478bd9Sstevel@tonic-gate libname = _lib; 15027c478bd9Sstevel@tonic-gate else { 15037c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: " 15047c478bd9Sstevel@tonic-gate "process_dynamic: failed to expand " 15057c478bd9Sstevel@tonic-gate "%s\n", libname); 15067c478bd9Sstevel@tonic-gate } 15077c478bd9Sstevel@tonic-gate } 15087c478bd9Sstevel@tonic-gate 15097c478bd9Sstevel@tonic-gate lsize = strlen(libname); 15107c478bd9Sstevel@tonic-gate nsize += lsize; 15117c478bd9Sstevel@tonic-gate if (nsize + 1 > allocsize) { 15127c478bd9Sstevel@tonic-gate tmp = kobj_alloc(allocsize + MAXPATHLEN, 15137c478bd9Sstevel@tonic-gate KM_WAIT); 15147c478bd9Sstevel@tonic-gate if (depstr != NULL) { 15157c478bd9Sstevel@tonic-gate bcopy(depstr, tmp, osize); 15167c478bd9Sstevel@tonic-gate kobj_free(depstr, allocsize); 15177c478bd9Sstevel@tonic-gate } 15187c478bd9Sstevel@tonic-gate depstr = tmp; 15197c478bd9Sstevel@tonic-gate allocsize += MAXPATHLEN; 15207c478bd9Sstevel@tonic-gate } 15217c478bd9Sstevel@tonic-gate bcopy(libname, depstr + osize, lsize); 15227c478bd9Sstevel@tonic-gate *(depstr + nsize) = ' '; /* separator */ 15237c478bd9Sstevel@tonic-gate nsize++; 15247c478bd9Sstevel@tonic-gate osize = nsize; 15257c478bd9Sstevel@tonic-gate break; 15267c478bd9Sstevel@tonic-gate 15277c478bd9Sstevel@tonic-gate case DT_FLAGS_1: 15287c478bd9Sstevel@tonic-gate if (dynp->d_un.d_val & DF_1_IGNMULDEF) 15297c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_IGNMULDEF; 15307c478bd9Sstevel@tonic-gate if (dynp->d_un.d_val & DF_1_NOKSYMS) 15317c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_NOKSYMS; 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate break; 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate } 15367c478bd9Sstevel@tonic-gate 15377c478bd9Sstevel@tonic-gate /* 15387c478bd9Sstevel@tonic-gate * finish up the depends string (if any) 15397c478bd9Sstevel@tonic-gate */ 15407c478bd9Sstevel@tonic-gate if (depstr != NULL) { 15417c478bd9Sstevel@tonic-gate *(depstr + nsize - 1) = '\0'; /* overwrite seperator w/term */ 15427c478bd9Sstevel@tonic-gate if (path != NULL) 15437c478bd9Sstevel@tonic-gate kobj_free(path, MAXPATHLEN); 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate tmp = kobj_alloc(nsize, KM_WAIT); 15467c478bd9Sstevel@tonic-gate bcopy(depstr, tmp, nsize); 15477c478bd9Sstevel@tonic-gate kobj_free(depstr, allocsize); 15487c478bd9Sstevel@tonic-gate depstr = tmp; 15497c478bd9Sstevel@tonic-gate 15507c478bd9Sstevel@tonic-gate mp->depends_on = depstr; 15517c478bd9Sstevel@tonic-gate } 15527c478bd9Sstevel@tonic-gate 15537c478bd9Sstevel@tonic-gate return (0); 15547c478bd9Sstevel@tonic-gate } 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate static int 15577c478bd9Sstevel@tonic-gate do_dynamic(struct module *mp, struct _buf *file) 15587c478bd9Sstevel@tonic-gate { 15597c478bd9Sstevel@tonic-gate Shdr *dshp, *dstrp, *shp; 15607c478bd9Sstevel@tonic-gate char *dyndata, *dstrdata; 15617c478bd9Sstevel@tonic-gate int dshn, shn, rc; 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate /* find and validate the dynamic section (if any) */ 15647c478bd9Sstevel@tonic-gate 15657c478bd9Sstevel@tonic-gate for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) { 15667c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 15677c478bd9Sstevel@tonic-gate switch (shp->sh_type) { 15687c478bd9Sstevel@tonic-gate case SHT_DYNAMIC: 15697c478bd9Sstevel@tonic-gate if (dshp != NULL) { 15707c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", 15717c478bd9Sstevel@tonic-gate mp->filename); 15727c478bd9Sstevel@tonic-gate _kobj_printf(ops, 15737c478bd9Sstevel@tonic-gate "multiple dynamic sections\n"); 15747c478bd9Sstevel@tonic-gate return (-1); 15757c478bd9Sstevel@tonic-gate } else { 15767c478bd9Sstevel@tonic-gate dshp = shp; 15777c478bd9Sstevel@tonic-gate dshn = shn; 15787c478bd9Sstevel@tonic-gate } 15797c478bd9Sstevel@tonic-gate break; 15807c478bd9Sstevel@tonic-gate } 15817c478bd9Sstevel@tonic-gate } 15827c478bd9Sstevel@tonic-gate 15837c478bd9Sstevel@tonic-gate if (dshp == NULL) 15847c478bd9Sstevel@tonic-gate return (0); 15857c478bd9Sstevel@tonic-gate 15867c478bd9Sstevel@tonic-gate if (dshp->sh_link > mp->hdr.e_shnum) { 15877c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 15887c478bd9Sstevel@tonic-gate _kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link); 15897c478bd9Sstevel@tonic-gate return (-1); 15907c478bd9Sstevel@tonic-gate } 15917c478bd9Sstevel@tonic-gate dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize); 15927c478bd9Sstevel@tonic-gate 15937c478bd9Sstevel@tonic-gate if (dstrp->sh_type != SHT_STRTAB) { 15947c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 15957c478bd9Sstevel@tonic-gate _kobj_printf(ops, "sh_link not a string table for section %d\n", 15967c478bd9Sstevel@tonic-gate dshn); 15977c478bd9Sstevel@tonic-gate return (-1); 15987c478bd9Sstevel@tonic-gate } 15997c478bd9Sstevel@tonic-gate 16007c478bd9Sstevel@tonic-gate /* read it from disk */ 16017c478bd9Sstevel@tonic-gate 16027c478bd9Sstevel@tonic-gate dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP); 16037c478bd9Sstevel@tonic-gate if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) { 16047c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 16057c478bd9Sstevel@tonic-gate _kobj_printf(ops, "error reading section %d\n", dshn); 16067c478bd9Sstevel@tonic-gate 16077c478bd9Sstevel@tonic-gate kobj_free(dyndata, dshp->sh_size); 16087c478bd9Sstevel@tonic-gate return (-1); 16097c478bd9Sstevel@tonic-gate } 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP); 16127c478bd9Sstevel@tonic-gate if (kobj_read_file(file, dstrdata, dstrp->sh_size, 16137c478bd9Sstevel@tonic-gate dstrp->sh_offset) < 0) { 16147c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename); 16157c478bd9Sstevel@tonic-gate _kobj_printf(ops, "error reading section %d\n", dshp->sh_link); 16167c478bd9Sstevel@tonic-gate 16177c478bd9Sstevel@tonic-gate kobj_free(dyndata, dshp->sh_size); 16187c478bd9Sstevel@tonic-gate kobj_free(dstrdata, dstrp->sh_size); 16197c478bd9Sstevel@tonic-gate return (-1); 16207c478bd9Sstevel@tonic-gate } 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate /* pull the interesting pieces out */ 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate rc = process_dynamic(mp, dyndata, dstrdata); 16257c478bd9Sstevel@tonic-gate 16267c478bd9Sstevel@tonic-gate kobj_free(dyndata, dshp->sh_size); 16277c478bd9Sstevel@tonic-gate kobj_free(dstrdata, dstrp->sh_size); 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate return (rc); 16307c478bd9Sstevel@tonic-gate } 16317c478bd9Sstevel@tonic-gate 16327c478bd9Sstevel@tonic-gate void 16337c478bd9Sstevel@tonic-gate kobj_set_ctf(struct module *mp, caddr_t data, size_t size) 16347c478bd9Sstevel@tonic-gate { 16357c478bd9Sstevel@tonic-gate if (!standalone) { 16367c478bd9Sstevel@tonic-gate if (mp->ctfdata != NULL) { 16377c478bd9Sstevel@tonic-gate if (vmem_contains(ctf_arena, mp->ctfdata, 16387c478bd9Sstevel@tonic-gate mp->ctfsize)) { 16397c478bd9Sstevel@tonic-gate vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize); 16407c478bd9Sstevel@tonic-gate } else { 16417c478bd9Sstevel@tonic-gate kobj_free(mp->ctfdata, mp->ctfsize); 16427c478bd9Sstevel@tonic-gate } 16437c478bd9Sstevel@tonic-gate } 16447c478bd9Sstevel@tonic-gate } 16457c478bd9Sstevel@tonic-gate 16467c478bd9Sstevel@tonic-gate /* 16477c478bd9Sstevel@tonic-gate * The order is very important here. We need to make sure that 16487c478bd9Sstevel@tonic-gate * consumers, at any given instant, see a consistent state. We'd 16497c478bd9Sstevel@tonic-gate * rather they see no CTF data than the address of one buffer and the 16507c478bd9Sstevel@tonic-gate * size of another. 16517c478bd9Sstevel@tonic-gate */ 16527c478bd9Sstevel@tonic-gate mp->ctfdata = NULL; 16537c478bd9Sstevel@tonic-gate membar_producer(); 16547c478bd9Sstevel@tonic-gate mp->ctfsize = size; 16557c478bd9Sstevel@tonic-gate mp->ctfdata = data; 16567c478bd9Sstevel@tonic-gate membar_producer(); 16577c478bd9Sstevel@tonic-gate } 16587c478bd9Sstevel@tonic-gate 16597c478bd9Sstevel@tonic-gate int 16607c478bd9Sstevel@tonic-gate kobj_load_module(struct modctl *modp, int use_path) 16617c478bd9Sstevel@tonic-gate { 16627c478bd9Sstevel@tonic-gate char *filename = modp->mod_filename; 16637c478bd9Sstevel@tonic-gate char *modname = modp->mod_modname; 16647c478bd9Sstevel@tonic-gate int i; 16657c478bd9Sstevel@tonic-gate int n; 16667c478bd9Sstevel@tonic-gate struct _buf *file; 16677c478bd9Sstevel@tonic-gate struct module *mp = NULL; 16687c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 16697c478bd9Sstevel@tonic-gate int no_suffixdir_drv = 0; 16707c478bd9Sstevel@tonic-gate #endif 16717c478bd9Sstevel@tonic-gate 16727c478bd9Sstevel@tonic-gate mp = kobj_zalloc(sizeof (struct module), KM_WAIT); 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate /* 16757c478bd9Sstevel@tonic-gate * We need to prevent kmdb's symbols from leaking into /dev/ksyms. 16767c478bd9Sstevel@tonic-gate * kmdb contains a bunch of symbols with well-known names, symbols 16777c478bd9Sstevel@tonic-gate * which will mask the real versions, thus causing no end of trouble 16787c478bd9Sstevel@tonic-gate * for mdb. 16797c478bd9Sstevel@tonic-gate */ 16807c478bd9Sstevel@tonic-gate if (strcmp(modp->mod_modname, "kmdbmod") == 0) 16817c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_NOKSYMS; 16827c478bd9Sstevel@tonic-gate 16837c478bd9Sstevel@tonic-gate file = kobj_open_path(filename, use_path, 1); 16847c478bd9Sstevel@tonic-gate if (file == (struct _buf *)-1) { 16857c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 16867c478bd9Sstevel@tonic-gate file = kobj_open_path(filename, use_path, 0); 16877c478bd9Sstevel@tonic-gate #endif 16887c478bd9Sstevel@tonic-gate if (file == (struct _buf *)-1) { 16897c478bd9Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 16907c478bd9Sstevel@tonic-gate goto bad; 16917c478bd9Sstevel@tonic-gate } 16927c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 16937c478bd9Sstevel@tonic-gate /* 16947c478bd9Sstevel@tonic-gate * There is no driver module in the ISA specific (suffix) 16957c478bd9Sstevel@tonic-gate * subdirectory but there is a module in the parent directory. 16967c478bd9Sstevel@tonic-gate */ 16977c478bd9Sstevel@tonic-gate if (strncmp(filename, "drv/", 4) == 0) { 16987c478bd9Sstevel@tonic-gate no_suffixdir_drv = 1; 16997c478bd9Sstevel@tonic-gate } 17007c478bd9Sstevel@tonic-gate #endif 17017c478bd9Sstevel@tonic-gate } 17027c478bd9Sstevel@tonic-gate 17037c478bd9Sstevel@tonic-gate mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT); 17047c478bd9Sstevel@tonic-gate (void) strcpy(mp->filename, file->_name); 17057c478bd9Sstevel@tonic-gate 17067c478bd9Sstevel@tonic-gate if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) { 17077c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_load_module: %s read header failed\n", 17087c478bd9Sstevel@tonic-gate modname); 17097c478bd9Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 17107c478bd9Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 17117c478bd9Sstevel@tonic-gate goto bad; 17127c478bd9Sstevel@tonic-gate } 17137c478bd9Sstevel@tonic-gate for (i = 0; i < SELFMAG; i++) { 17147c478bd9Sstevel@tonic-gate if (mp->hdr.e_ident[i] != ELFMAG[i]) { 17157c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 17167c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s not an elf module\n", 17177c478bd9Sstevel@tonic-gate modname); 17187c478bd9Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 17197c478bd9Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 17207c478bd9Sstevel@tonic-gate goto bad; 17217c478bd9Sstevel@tonic-gate } 17227c478bd9Sstevel@tonic-gate } 17237c478bd9Sstevel@tonic-gate /* 17247c478bd9Sstevel@tonic-gate * It's ELF, but is it our ISA? Interpreting the header 17257c478bd9Sstevel@tonic-gate * from a file for a byte-swapped ISA could cause a huge 17267c478bd9Sstevel@tonic-gate * and unsatisfiable value to be passed to kobj_alloc below 17277c478bd9Sstevel@tonic-gate * and therefore hang booting. 17287c478bd9Sstevel@tonic-gate */ 17297c478bd9Sstevel@tonic-gate if (!elf_mach_ok(&mp->hdr)) { 17307c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 17317c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s not an elf module for this ISA\n", 17327c478bd9Sstevel@tonic-gate modname); 17337c478bd9Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 17347c478bd9Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 17357c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX 17367c478bd9Sstevel@tonic-gate /* 17377c478bd9Sstevel@tonic-gate * The driver mod is not in the ISA specific subdirectory 17387c478bd9Sstevel@tonic-gate * and the module in the parent directory is not our ISA. 17397c478bd9Sstevel@tonic-gate * If it is our ISA, for now we will silently succeed. 17407c478bd9Sstevel@tonic-gate */ 17417c478bd9Sstevel@tonic-gate if (no_suffixdir_drv == 1) { 17427c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?NOTICE: %s: 64-bit driver module" 17437c478bd9Sstevel@tonic-gate " not found\n", modname); 17447c478bd9Sstevel@tonic-gate } 17457c478bd9Sstevel@tonic-gate #endif 17467c478bd9Sstevel@tonic-gate goto bad; 17477c478bd9Sstevel@tonic-gate } 17487c478bd9Sstevel@tonic-gate 17497c478bd9Sstevel@tonic-gate /* 17507c478bd9Sstevel@tonic-gate * All modules, save for unix, should be relocatable (as opposed to 17517c478bd9Sstevel@tonic-gate * dynamic). Dynamic modules come with PLTs and GOTs, which can't 17527c478bd9Sstevel@tonic-gate * currently be processed by krtld. 17537c478bd9Sstevel@tonic-gate */ 17547c478bd9Sstevel@tonic-gate if (mp->hdr.e_type != ET_REL) { 17557c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 17567c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s isn't a relocatable (ET_REL) " 17577c478bd9Sstevel@tonic-gate "module\n", modname); 17587c478bd9Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 17597c478bd9Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 17607c478bd9Sstevel@tonic-gate goto bad; 17617c478bd9Sstevel@tonic-gate } 17627c478bd9Sstevel@tonic-gate 17637c478bd9Sstevel@tonic-gate n = mp->hdr.e_shentsize * mp->hdr.e_shnum; 17647c478bd9Sstevel@tonic-gate mp->shdrs = kobj_alloc(n, KM_WAIT); 17657c478bd9Sstevel@tonic-gate 17667c478bd9Sstevel@tonic-gate if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) { 17677c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_load_module: %s error reading " 17687c478bd9Sstevel@tonic-gate "section headers\n", modname); 17697c478bd9Sstevel@tonic-gate kobj_free(mp->shdrs, n); 17707c478bd9Sstevel@tonic-gate kobj_free(mp->filename, strlen(file->_name) + 1); 17717c478bd9Sstevel@tonic-gate kobj_free(mp, sizeof (*mp)); 17727c478bd9Sstevel@tonic-gate goto bad; 17737c478bd9Sstevel@tonic-gate } 17747c478bd9Sstevel@tonic-gate 17757c478bd9Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODLOADING, modp); 17767c478bd9Sstevel@tonic-gate module_assign(modp, mp); 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate /* read in sections */ 17797c478bd9Sstevel@tonic-gate if (get_progbits(mp, file) < 0) { 17807c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s error reading sections\n", modname); 17817c478bd9Sstevel@tonic-gate goto bad; 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate if (do_dynamic(mp, file) < 0) { 17857c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s error reading dynamic section\n", 17867c478bd9Sstevel@tonic-gate modname); 17877c478bd9Sstevel@tonic-gate goto bad; 17887c478bd9Sstevel@tonic-gate } 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate modp->mod_text = mp->text; 17917c478bd9Sstevel@tonic-gate modp->mod_text_size = mp->text_size; 17927c478bd9Sstevel@tonic-gate 17937c478bd9Sstevel@tonic-gate /* read in symbols; adjust values for each section's real address */ 17947c478bd9Sstevel@tonic-gate if (get_syms(mp, file) < 0) { 17957c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s error reading symbols\n", 17967c478bd9Sstevel@tonic-gate modname); 17977c478bd9Sstevel@tonic-gate goto bad; 17987c478bd9Sstevel@tonic-gate } 17997c478bd9Sstevel@tonic-gate 18007c478bd9Sstevel@tonic-gate /* 18017c478bd9Sstevel@tonic-gate * If we didn't dependency information from the dynamic section, look 18027c478bd9Sstevel@tonic-gate * for it the old-fashioned way. 18037c478bd9Sstevel@tonic-gate */ 18047c478bd9Sstevel@tonic-gate if (mp->depends_on == NULL) 18057c478bd9Sstevel@tonic-gate mp->depends_on = depends_on(mp); 18067c478bd9Sstevel@tonic-gate 18077c478bd9Sstevel@tonic-gate if (get_ctf(mp, file) < 0) { 18087c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s debug information will not " 18097c478bd9Sstevel@tonic-gate "be available\n", modname); 18107c478bd9Sstevel@tonic-gate } 18117c478bd9Sstevel@tonic-gate 18127c478bd9Sstevel@tonic-gate /* primary kernel modules do not have a signature section */ 18137c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) 18147c478bd9Sstevel@tonic-gate get_signature(mp, file); 18157c478bd9Sstevel@tonic-gate 18167c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 18177c478bd9Sstevel@tonic-gate if (kobj_debug & D_LOADING) { 18187c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: file=%s\n", mp->filename); 18197c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\ttext:0x%p", mp->text); 18207c478bd9Sstevel@tonic-gate _kobj_printf(ops, " size: 0x%x\n", mp->text_size); 18217c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\tdata:0x%p", mp->data); 18227c478bd9Sstevel@tonic-gate _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size); 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate /* 18277c478bd9Sstevel@tonic-gate * For primary kernel modules, we defer 18287c478bd9Sstevel@tonic-gate * symbol resolution and relocation until 18297c478bd9Sstevel@tonic-gate * all primary objects have been loaded. 18307c478bd9Sstevel@tonic-gate */ 18317c478bd9Sstevel@tonic-gate if (!standalone) { 18327c478bd9Sstevel@tonic-gate int ddrval, dcrval; 18337c478bd9Sstevel@tonic-gate char *dependent_modname; 18347c478bd9Sstevel@tonic-gate /* load all dependents */ 18357c478bd9Sstevel@tonic-gate dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT); 18367c478bd9Sstevel@tonic-gate ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN); 18377c478bd9Sstevel@tonic-gate 18387c478bd9Sstevel@tonic-gate /* 18397c478bd9Sstevel@tonic-gate * resolve undefined and common symbols, 18407c478bd9Sstevel@tonic-gate * also allocates common space 18417c478bd9Sstevel@tonic-gate */ 18427c478bd9Sstevel@tonic-gate if ((dcrval = do_common(mp)) < 0) { 18437c478bd9Sstevel@tonic-gate switch (dcrval) { 18447c478bd9Sstevel@tonic-gate case DOSYM_UNSAFE: 18457c478bd9Sstevel@tonic-gate _kobj_printf(ops, "WARNING: mod_load: " 18467c478bd9Sstevel@tonic-gate "MT-unsafe module '%s' rejected\n", 18477c478bd9Sstevel@tonic-gate modname); 18487c478bd9Sstevel@tonic-gate break; 18497c478bd9Sstevel@tonic-gate case DOSYM_UNDEF: 18507c478bd9Sstevel@tonic-gate _kobj_printf(ops, "WARNING: mod_load: " 18517c478bd9Sstevel@tonic-gate "cannot load module '%s'\n", 18527c478bd9Sstevel@tonic-gate modname); 18537c478bd9Sstevel@tonic-gate if (ddrval == -1) { 18547c478bd9Sstevel@tonic-gate _kobj_printf(ops, "WARNING: %s: ", 18557c478bd9Sstevel@tonic-gate modname); 18567c478bd9Sstevel@tonic-gate _kobj_printf(ops, 18577c478bd9Sstevel@tonic-gate "unable to resolve dependency, " 18587c478bd9Sstevel@tonic-gate "module '%s' not found\n", 18597c478bd9Sstevel@tonic-gate dependent_modname); 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate break; 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate } 18647c478bd9Sstevel@tonic-gate kobj_free(dependent_modname, MODMAXNAMELEN); 18657c478bd9Sstevel@tonic-gate if (dcrval < 0) 18667c478bd9Sstevel@tonic-gate goto bad; 18677c478bd9Sstevel@tonic-gate 18687c478bd9Sstevel@tonic-gate /* process relocation tables */ 18697c478bd9Sstevel@tonic-gate if (do_relocations(mp) < 0) { 18707c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s error doing relocations\n", 18717c478bd9Sstevel@tonic-gate modname); 18727c478bd9Sstevel@tonic-gate goto bad; 18737c478bd9Sstevel@tonic-gate } 18747c478bd9Sstevel@tonic-gate 18757c478bd9Sstevel@tonic-gate if (mp->destination) { 18767c478bd9Sstevel@tonic-gate off_t off = (uintptr_t)mp->destination & PAGEOFFSET; 18777c478bd9Sstevel@tonic-gate caddr_t base = (caddr_t)mp->destination - off; 18787c478bd9Sstevel@tonic-gate size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE); 18797c478bd9Sstevel@tonic-gate 18807c478bd9Sstevel@tonic-gate hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK); 18817c478bd9Sstevel@tonic-gate vmem_free(heap_arena, base, size); 18827c478bd9Sstevel@tonic-gate } 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate /* sync_instruction_memory */ 18857c478bd9Sstevel@tonic-gate kobj_sync_instruction_memory(mp->text, mp->text_size); 18867c478bd9Sstevel@tonic-gate #ifdef MPSAS 18877c478bd9Sstevel@tonic-gate sas_syms(mp); 18887c478bd9Sstevel@tonic-gate #endif 18897c478bd9Sstevel@tonic-gate kobj_export_module(mp); 18907c478bd9Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODLOADED, modp); 18917c478bd9Sstevel@tonic-gate } 18927c478bd9Sstevel@tonic-gate kobj_close_file(file); 18937c478bd9Sstevel@tonic-gate return (0); 18947c478bd9Sstevel@tonic-gate bad: 18957c478bd9Sstevel@tonic-gate if (file != (struct _buf *)-1) 18967c478bd9Sstevel@tonic-gate kobj_close_file(file); 18977c478bd9Sstevel@tonic-gate if (modp->mod_mp != NULL) 18987c478bd9Sstevel@tonic-gate free_module_data(modp->mod_mp); 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate module_assign(modp, NULL); 19017c478bd9Sstevel@tonic-gate return ((file == (struct _buf *)-1) ? ENOENT : EINVAL); 19027c478bd9Sstevel@tonic-gate } 19037c478bd9Sstevel@tonic-gate 19047c478bd9Sstevel@tonic-gate int 19057c478bd9Sstevel@tonic-gate kobj_load_primary_module(struct modctl *modp) 19067c478bd9Sstevel@tonic-gate { 19077c478bd9Sstevel@tonic-gate struct modctl *dep; 19087c478bd9Sstevel@tonic-gate struct module *mp; 19097c478bd9Sstevel@tonic-gate 19107c478bd9Sstevel@tonic-gate if (kobj_load_module(modp, 0) != 0) 19117c478bd9Sstevel@tonic-gate return (-1); 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate mp = modp->mod_mp; 19147c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_PRIM; 19157c478bd9Sstevel@tonic-gate 19167c478bd9Sstevel@tonic-gate /* Bind new module to its dependents */ 19177c478bd9Sstevel@tonic-gate if (mp->depends_on != NULL && (dep = 19187c478bd9Sstevel@tonic-gate mod_already_loaded(mp->depends_on)) == NULL) { 19197c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 19207c478bd9Sstevel@tonic-gate if (kobj_debug & D_DEBUG) { 19217c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: failed to resolve deps " 19227c478bd9Sstevel@tonic-gate "for primary %s\n", modp->mod_modname); 19237c478bd9Sstevel@tonic-gate } 19247c478bd9Sstevel@tonic-gate #endif 19257c478bd9Sstevel@tonic-gate return (-1); 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate 19287c478bd9Sstevel@tonic-gate add_dependent(mp, dep->mod_mp); 19297c478bd9Sstevel@tonic-gate 19307c478bd9Sstevel@tonic-gate /* 19317c478bd9Sstevel@tonic-gate * Relocate it. This module may not be part of a link map, so we 19327c478bd9Sstevel@tonic-gate * can't use bind_primary. 19337c478bd9Sstevel@tonic-gate */ 19347c478bd9Sstevel@tonic-gate if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 || 19357c478bd9Sstevel@tonic-gate do_relocations(mp) < 0) { 19367c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 19377c478bd9Sstevel@tonic-gate if (kobj_debug & D_DEBUG) { 19387c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: failed to relocate " 19397c478bd9Sstevel@tonic-gate "primary %s\n", modp->mod_modname); 19407c478bd9Sstevel@tonic-gate } 19417c478bd9Sstevel@tonic-gate #endif 19427c478bd9Sstevel@tonic-gate return (-1); 19437c478bd9Sstevel@tonic-gate } 19447c478bd9Sstevel@tonic-gate 19457c478bd9Sstevel@tonic-gate return (0); 19467c478bd9Sstevel@tonic-gate } 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate static void 19497c478bd9Sstevel@tonic-gate module_assign(struct modctl *cp, struct module *mp) 19507c478bd9Sstevel@tonic-gate { 19517c478bd9Sstevel@tonic-gate if (standalone) { 19527c478bd9Sstevel@tonic-gate cp->mod_mp = mp; 19537c478bd9Sstevel@tonic-gate return; 19547c478bd9Sstevel@tonic-gate } 19557c478bd9Sstevel@tonic-gate mutex_enter(&mod_lock); 19567c478bd9Sstevel@tonic-gate cp->mod_mp = mp; 19577c478bd9Sstevel@tonic-gate cp->mod_gencount++; 19587c478bd9Sstevel@tonic-gate mutex_exit(&mod_lock); 19597c478bd9Sstevel@tonic-gate } 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate void 19627c478bd9Sstevel@tonic-gate kobj_unload_module(struct modctl *modp) 19637c478bd9Sstevel@tonic-gate { 19647c478bd9Sstevel@tonic-gate struct module *mp = modp->mod_mp; 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) { 19677c478bd9Sstevel@tonic-gate _kobj_printf(ops, "text for %s ", mp->filename); 19687c478bd9Sstevel@tonic-gate _kobj_printf(ops, "was at %p\n", mp->text); 19697c478bd9Sstevel@tonic-gate mp->text = NULL; /* don't actually free it */ 19707c478bd9Sstevel@tonic-gate } 19717c478bd9Sstevel@tonic-gate 19727c478bd9Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp); 19737c478bd9Sstevel@tonic-gate 19747c478bd9Sstevel@tonic-gate /* 19757c478bd9Sstevel@tonic-gate * Null out mod_mp first, so consumers (debuggers) know not to look 19767c478bd9Sstevel@tonic-gate * at the module structure any more. 19777c478bd9Sstevel@tonic-gate */ 19787c478bd9Sstevel@tonic-gate mutex_enter(&mod_lock); 19797c478bd9Sstevel@tonic-gate modp->mod_mp = NULL; 19807c478bd9Sstevel@tonic-gate mutex_exit(&mod_lock); 19817c478bd9Sstevel@tonic-gate 19827c478bd9Sstevel@tonic-gate kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp); 19837c478bd9Sstevel@tonic-gate free_module_data(mp); 19847c478bd9Sstevel@tonic-gate } 19857c478bd9Sstevel@tonic-gate 19867c478bd9Sstevel@tonic-gate static void 19877c478bd9Sstevel@tonic-gate free_module_data(struct module *mp) 19887c478bd9Sstevel@tonic-gate { 19897c478bd9Sstevel@tonic-gate struct module_list *lp, *tmp; 19907c478bd9Sstevel@tonic-gate int ksyms_exported = 0; 19917c478bd9Sstevel@tonic-gate 19927c478bd9Sstevel@tonic-gate lp = mp->head; 19937c478bd9Sstevel@tonic-gate while (lp) { 19947c478bd9Sstevel@tonic-gate tmp = lp; 19957c478bd9Sstevel@tonic-gate lp = lp->next; 19967c478bd9Sstevel@tonic-gate kobj_free((char *)tmp, sizeof (*tmp)); 19977c478bd9Sstevel@tonic-gate } 19987c478bd9Sstevel@tonic-gate 19997c478bd9Sstevel@tonic-gate rw_enter(&ksyms_lock, RW_WRITER); 20007c478bd9Sstevel@tonic-gate if (mp->symspace) { 20017c478bd9Sstevel@tonic-gate if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) { 20027c478bd9Sstevel@tonic-gate vmem_free(ksyms_arena, mp->symspace, mp->symsize); 20037c478bd9Sstevel@tonic-gate ksyms_exported = 1; 20047c478bd9Sstevel@tonic-gate } else { 20057c478bd9Sstevel@tonic-gate if (mp->flags & KOBJ_NOKSYMS) 20067c478bd9Sstevel@tonic-gate ksyms_exported = 1; 20077c478bd9Sstevel@tonic-gate kobj_free(mp->symspace, mp->symsize); 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate } 20107c478bd9Sstevel@tonic-gate rw_exit(&ksyms_lock); 20117c478bd9Sstevel@tonic-gate 20127c478bd9Sstevel@tonic-gate if (mp->ctfdata) { 20137c478bd9Sstevel@tonic-gate if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize)) 20147c478bd9Sstevel@tonic-gate vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize); 20157c478bd9Sstevel@tonic-gate else 20167c478bd9Sstevel@tonic-gate kobj_free(mp->ctfdata, mp->ctfsize); 20177c478bd9Sstevel@tonic-gate } 20187c478bd9Sstevel@tonic-gate 20197c478bd9Sstevel@tonic-gate if (mp->sigdata) 20207c478bd9Sstevel@tonic-gate kobj_free(mp->sigdata, mp->sigsize); 20217c478bd9Sstevel@tonic-gate 20227c478bd9Sstevel@tonic-gate /* 20237c478bd9Sstevel@tonic-gate * We did not get far enough into kobj_export_ksyms() to free allocated 20247c478bd9Sstevel@tonic-gate * buffers because we encounted error conditions. Free the buffers. 20257c478bd9Sstevel@tonic-gate */ 20267c478bd9Sstevel@tonic-gate if ((ksyms_exported == 0) && (mp->shdrs != NULL)) { 20277c478bd9Sstevel@tonic-gate uint_t shn; 20287c478bd9Sstevel@tonic-gate Shdr *shp; 20297c478bd9Sstevel@tonic-gate 20307c478bd9Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 20317c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 20327c478bd9Sstevel@tonic-gate switch (shp->sh_type) { 20337c478bd9Sstevel@tonic-gate case SHT_RELA: 20347c478bd9Sstevel@tonic-gate case SHT_REL: 20357c478bd9Sstevel@tonic-gate if (shp->sh_addr != 0) 20367c478bd9Sstevel@tonic-gate kobj_free((void *)shp->sh_addr, 20377c478bd9Sstevel@tonic-gate shp->sh_size); 20387c478bd9Sstevel@tonic-gate break; 20397c478bd9Sstevel@tonic-gate } 20407c478bd9Sstevel@tonic-gate } 20417c478bd9Sstevel@tonic-gate err_free_done: 20427c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM)) { 20437c478bd9Sstevel@tonic-gate kobj_free(mp->shdrs, 20447c478bd9Sstevel@tonic-gate mp->hdr.e_shentsize * mp->hdr.e_shnum); 20457c478bd9Sstevel@tonic-gate } 20467c478bd9Sstevel@tonic-gate } 20477c478bd9Sstevel@tonic-gate 20487c478bd9Sstevel@tonic-gate if (mp->bss) 20497c478bd9Sstevel@tonic-gate vmem_free(data_arena, (void *)mp->bss, mp->bss_size); 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate if (mp->fbt_tab) 20527c478bd9Sstevel@tonic-gate kobj_texthole_free(mp->fbt_tab, mp->fbt_size); 20537c478bd9Sstevel@tonic-gate 20547c478bd9Sstevel@tonic-gate if (mp->textwin_base) 20557c478bd9Sstevel@tonic-gate kobj_textwin_free(mp); 20567c478bd9Sstevel@tonic-gate 20577c478bd9Sstevel@tonic-gate if (mp->sdt_probes != NULL) { 20587c478bd9Sstevel@tonic-gate sdt_probedesc_t *sdp = mp->sdt_probes, *next; 20597c478bd9Sstevel@tonic-gate 20607c478bd9Sstevel@tonic-gate while (sdp != NULL) { 20617c478bd9Sstevel@tonic-gate next = sdp->sdpd_next; 20627c478bd9Sstevel@tonic-gate kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1); 20637c478bd9Sstevel@tonic-gate kobj_free(sdp, sizeof (sdt_probedesc_t)); 20647c478bd9Sstevel@tonic-gate sdp = next; 20657c478bd9Sstevel@tonic-gate } 20667c478bd9Sstevel@tonic-gate } 20677c478bd9Sstevel@tonic-gate 20687c478bd9Sstevel@tonic-gate if (mp->sdt_tab) 20697c478bd9Sstevel@tonic-gate kobj_texthole_free(mp->sdt_tab, mp->sdt_size); 20707c478bd9Sstevel@tonic-gate if (mp->text) 20717c478bd9Sstevel@tonic-gate vmem_free(text_arena, mp->text, mp->text_size); 20727c478bd9Sstevel@tonic-gate if (mp->data) 20737c478bd9Sstevel@tonic-gate vmem_free(data_arena, mp->data, mp->data_size); 20747c478bd9Sstevel@tonic-gate if (mp->depends_on) 20757c478bd9Sstevel@tonic-gate kobj_free(mp->depends_on, strlen(mp->depends_on)+1); 20767c478bd9Sstevel@tonic-gate if (mp->filename) 20777c478bd9Sstevel@tonic-gate kobj_free(mp->filename, strlen(mp->filename)+1); 20787c478bd9Sstevel@tonic-gate 20797c478bd9Sstevel@tonic-gate kobj_free((char *)mp, sizeof (*mp)); 20807c478bd9Sstevel@tonic-gate } 20817c478bd9Sstevel@tonic-gate 20827c478bd9Sstevel@tonic-gate static int 20837c478bd9Sstevel@tonic-gate get_progbits(struct module *mp, struct _buf *file) 20847c478bd9Sstevel@tonic-gate { 20857c478bd9Sstevel@tonic-gate struct proginfo *tp, *dp, *sdp; 20867c478bd9Sstevel@tonic-gate Shdr *shp; 20877c478bd9Sstevel@tonic-gate reloc_dest_t dest = NULL; 20887c478bd9Sstevel@tonic-gate uintptr_t bits_ptr; 20897c478bd9Sstevel@tonic-gate uintptr_t text = 0, data, sdata = 0, textptr; 20907c478bd9Sstevel@tonic-gate uint_t shn; 20917c478bd9Sstevel@tonic-gate int err = -1; 20927c478bd9Sstevel@tonic-gate 20937c478bd9Sstevel@tonic-gate tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 20947c478bd9Sstevel@tonic-gate dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 20957c478bd9Sstevel@tonic-gate sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT); 20967c478bd9Sstevel@tonic-gate /* 20977c478bd9Sstevel@tonic-gate * loop through sections to find out how much space we need 20987c478bd9Sstevel@tonic-gate * for text, data, (also bss that is already assigned) 20997c478bd9Sstevel@tonic-gate */ 21007c478bd9Sstevel@tonic-gate if (get_progbits_size(mp, tp, dp, sdp) < 0) 21017c478bd9Sstevel@tonic-gate goto done; 21027c478bd9Sstevel@tonic-gate 21037c478bd9Sstevel@tonic-gate mp->text_size = tp->size; 21047c478bd9Sstevel@tonic-gate mp->data_size = dp->size; 21057c478bd9Sstevel@tonic-gate 21067c478bd9Sstevel@tonic-gate if (standalone) { 21077c478bd9Sstevel@tonic-gate mp->text = kobj_segbrk(&_etext, mp->text_size, 21087c478bd9Sstevel@tonic-gate tp->align, _data); 21097c478bd9Sstevel@tonic-gate /* 21107c478bd9Sstevel@tonic-gate * If we can't grow the text segment, try the 21117c478bd9Sstevel@tonic-gate * data segment before failing. 21127c478bd9Sstevel@tonic-gate */ 21137c478bd9Sstevel@tonic-gate if (mp->text == NULL) { 21147c478bd9Sstevel@tonic-gate mp->text = kobj_segbrk(&_edata, mp->text_size, 21157c478bd9Sstevel@tonic-gate tp->align, 0); 21167c478bd9Sstevel@tonic-gate } 21177c478bd9Sstevel@tonic-gate 21187c478bd9Sstevel@tonic-gate mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0); 21197c478bd9Sstevel@tonic-gate 21207c478bd9Sstevel@tonic-gate if (mp->text == NULL || mp->data == NULL) 21217c478bd9Sstevel@tonic-gate goto done; 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate } else { 21247c478bd9Sstevel@tonic-gate if (text_arena == NULL) 21257c478bd9Sstevel@tonic-gate kobj_vmem_init(&text_arena, &data_arena); 21267c478bd9Sstevel@tonic-gate 21277c478bd9Sstevel@tonic-gate /* 21287c478bd9Sstevel@tonic-gate * some architectures may want to load the module on a 21297c478bd9Sstevel@tonic-gate * page that is currently read only. It may not be 21307c478bd9Sstevel@tonic-gate * possible for those architectures to remap their page 21317c478bd9Sstevel@tonic-gate * on the fly. So we provide a facility for them to hang 21327c478bd9Sstevel@tonic-gate * a private hook where the memory they assign the module 21337c478bd9Sstevel@tonic-gate * is not the actual place where the module loads. 21347c478bd9Sstevel@tonic-gate * 21357c478bd9Sstevel@tonic-gate * In this case there are two addresses that deal with the 21367c478bd9Sstevel@tonic-gate * modload. 21377c478bd9Sstevel@tonic-gate * 1) the final destination of the module 21387c478bd9Sstevel@tonic-gate * 2) the address that is used to view the newly 21397c478bd9Sstevel@tonic-gate * loaded module until all the relocations relative to 1 21407c478bd9Sstevel@tonic-gate * above are completed. 21417c478bd9Sstevel@tonic-gate * 21427c478bd9Sstevel@tonic-gate * That is what dest is used for below. 21437c478bd9Sstevel@tonic-gate */ 21447c478bd9Sstevel@tonic-gate mp->text_size += tp->align; 21457c478bd9Sstevel@tonic-gate mp->data_size += dp->align; 21467c478bd9Sstevel@tonic-gate 21477c478bd9Sstevel@tonic-gate mp->text = kobj_text_alloc(text_arena, mp->text_size); 21487c478bd9Sstevel@tonic-gate 21497c478bd9Sstevel@tonic-gate /* 21507c478bd9Sstevel@tonic-gate * a remap is taking place. Align the text ptr relative 21517c478bd9Sstevel@tonic-gate * to the secondary mapping. That is where the bits will 21527c478bd9Sstevel@tonic-gate * be read in. 21537c478bd9Sstevel@tonic-gate */ 21547c478bd9Sstevel@tonic-gate if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena, 21557c478bd9Sstevel@tonic-gate mp->text, mp->text_size)) { 21567c478bd9Sstevel@tonic-gate off_t off = (uintptr_t)mp->text & PAGEOFFSET; 21577c478bd9Sstevel@tonic-gate size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE); 21587c478bd9Sstevel@tonic-gate caddr_t map = vmem_alloc(heap_arena, size, VM_SLEEP); 21597c478bd9Sstevel@tonic-gate caddr_t orig = mp->text - off; 21607c478bd9Sstevel@tonic-gate pgcnt_t pages = size / PAGESIZE; 21617c478bd9Sstevel@tonic-gate 21627c478bd9Sstevel@tonic-gate dest = (reloc_dest_t)(map + off); 21637c478bd9Sstevel@tonic-gate text = ALIGN((uintptr_t)dest, tp->align); 21647c478bd9Sstevel@tonic-gate 21657c478bd9Sstevel@tonic-gate while (pages--) { 21667c478bd9Sstevel@tonic-gate hat_devload(kas.a_hat, map, PAGESIZE, 21677c478bd9Sstevel@tonic-gate hat_getpfnum(kas.a_hat, orig), 21687c478bd9Sstevel@tonic-gate PROT_READ | PROT_WRITE | PROT_EXEC, 21697c478bd9Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK); 21707c478bd9Sstevel@tonic-gate map += PAGESIZE; 21717c478bd9Sstevel@tonic-gate orig += PAGESIZE; 21727c478bd9Sstevel@tonic-gate } 21737c478bd9Sstevel@tonic-gate /* 21747c478bd9Sstevel@tonic-gate * Since we set up a non-cacheable mapping, we need 21757c478bd9Sstevel@tonic-gate * to flush any old entries in the cache that might 21767c478bd9Sstevel@tonic-gate * be left around from the read-only mapping. 21777c478bd9Sstevel@tonic-gate */ 21787c478bd9Sstevel@tonic-gate dcache_flushall(); 21797c478bd9Sstevel@tonic-gate } 21807c478bd9Sstevel@tonic-gate if (mp->data_size) 21817c478bd9Sstevel@tonic-gate mp->data = vmem_alloc(data_arena, mp->data_size, 21827c478bd9Sstevel@tonic-gate VM_SLEEP | VM_BESTFIT); 21837c478bd9Sstevel@tonic-gate } 21847c478bd9Sstevel@tonic-gate textptr = (uintptr_t)mp->text; 21857c478bd9Sstevel@tonic-gate textptr = ALIGN(textptr, tp->align); 21867c478bd9Sstevel@tonic-gate mp->destination = dest; 21877c478bd9Sstevel@tonic-gate 21887c478bd9Sstevel@tonic-gate /* 21897c478bd9Sstevel@tonic-gate * This is the case where a remap is not being done. 21907c478bd9Sstevel@tonic-gate */ 21917c478bd9Sstevel@tonic-gate if (text == 0) 21927c478bd9Sstevel@tonic-gate text = ALIGN((uintptr_t)mp->text, tp->align); 21937c478bd9Sstevel@tonic-gate data = ALIGN((uintptr_t)mp->data, dp->align); 21947c478bd9Sstevel@tonic-gate 21957c478bd9Sstevel@tonic-gate /* now loop though sections assigning addresses and loading the data */ 21967c478bd9Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 21977c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 21987c478bd9Sstevel@tonic-gate if (!(shp->sh_flags & SHF_ALLOC)) 21997c478bd9Sstevel@tonic-gate continue; 22007c478bd9Sstevel@tonic-gate 22017c478bd9Sstevel@tonic-gate if ((shp->sh_flags & SHF_WRITE) == 0) 22027c478bd9Sstevel@tonic-gate bits_ptr = text; 22037c478bd9Sstevel@tonic-gate else if (shp->sh_flags & SHF_NEUT_SHORT) 22047c478bd9Sstevel@tonic-gate bits_ptr = sdata; 22057c478bd9Sstevel@tonic-gate else 22067c478bd9Sstevel@tonic-gate bits_ptr = data; 22077c478bd9Sstevel@tonic-gate 22087c478bd9Sstevel@tonic-gate bits_ptr = ALIGN(bits_ptr, shp->sh_addralign); 22097c478bd9Sstevel@tonic-gate 22107c478bd9Sstevel@tonic-gate if (shp->sh_type == SHT_NOBITS) { 22117c478bd9Sstevel@tonic-gate /* 22127c478bd9Sstevel@tonic-gate * Zero bss. 22137c478bd9Sstevel@tonic-gate */ 22147c478bd9Sstevel@tonic-gate bzero((caddr_t)bits_ptr, shp->sh_size); 22157c478bd9Sstevel@tonic-gate shp->sh_type = SHT_PROGBITS; 22167c478bd9Sstevel@tonic-gate } else { 22177c478bd9Sstevel@tonic-gate if (kobj_read_file(file, (char *)bits_ptr, 22187c478bd9Sstevel@tonic-gate shp->sh_size, shp->sh_offset) < 0) 22197c478bd9Sstevel@tonic-gate goto done; 22207c478bd9Sstevel@tonic-gate } 22217c478bd9Sstevel@tonic-gate 22227c478bd9Sstevel@tonic-gate if (shp->sh_flags & SHF_WRITE) { 22237c478bd9Sstevel@tonic-gate shp->sh_addr = bits_ptr; 22247c478bd9Sstevel@tonic-gate } else { 22257c478bd9Sstevel@tonic-gate textptr = ALIGN(textptr, shp->sh_addralign); 22267c478bd9Sstevel@tonic-gate shp->sh_addr = textptr; 22277c478bd9Sstevel@tonic-gate textptr += shp->sh_size; 22287c478bd9Sstevel@tonic-gate } 22297c478bd9Sstevel@tonic-gate 22307c478bd9Sstevel@tonic-gate bits_ptr += shp->sh_size; 22317c478bd9Sstevel@tonic-gate if ((shp->sh_flags & SHF_WRITE) == 0) 22327c478bd9Sstevel@tonic-gate text = bits_ptr; 22337c478bd9Sstevel@tonic-gate else if (shp->sh_flags & SHF_NEUT_SHORT) 22347c478bd9Sstevel@tonic-gate sdata = bits_ptr; 22357c478bd9Sstevel@tonic-gate else 22367c478bd9Sstevel@tonic-gate data = bits_ptr; 22377c478bd9Sstevel@tonic-gate } 22387c478bd9Sstevel@tonic-gate 22397c478bd9Sstevel@tonic-gate err = 0; 22407c478bd9Sstevel@tonic-gate done: 22417c478bd9Sstevel@tonic-gate /* 22427c478bd9Sstevel@tonic-gate * Free and mark as freed the section headers here so that 22437c478bd9Sstevel@tonic-gate * free_module_data() does not have to worry about this buffer. 22447c478bd9Sstevel@tonic-gate * 22457c478bd9Sstevel@tonic-gate * This buffer is freed here because one of the possible reasons 22467c478bd9Sstevel@tonic-gate * for error is a section with non-zero sh_addr and in that case 22477c478bd9Sstevel@tonic-gate * free_module_data() would have no way of recognizing that this 22487c478bd9Sstevel@tonic-gate * buffer was unallocated. 22497c478bd9Sstevel@tonic-gate */ 22507c478bd9Sstevel@tonic-gate if (err != 0) { 22517c478bd9Sstevel@tonic-gate kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum); 22527c478bd9Sstevel@tonic-gate mp->shdrs = NULL; 22537c478bd9Sstevel@tonic-gate } 22547c478bd9Sstevel@tonic-gate 22557c478bd9Sstevel@tonic-gate (void) kobj_free(tp, sizeof (struct proginfo)); 22567c478bd9Sstevel@tonic-gate (void) kobj_free(dp, sizeof (struct proginfo)); 22577c478bd9Sstevel@tonic-gate (void) kobj_free(sdp, sizeof (struct proginfo)); 22587c478bd9Sstevel@tonic-gate 22597c478bd9Sstevel@tonic-gate return (err); 22607c478bd9Sstevel@tonic-gate } 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate /* 22637c478bd9Sstevel@tonic-gate * Go through suppress_sym_list to see if "multiply defined" 22647c478bd9Sstevel@tonic-gate * warning of this symbol should be suppressed. Return 1 if 22657c478bd9Sstevel@tonic-gate * warning should be suppressed, 0 otherwise. 22667c478bd9Sstevel@tonic-gate */ 22677c478bd9Sstevel@tonic-gate static int 22687c478bd9Sstevel@tonic-gate kobj_suppress_warning(char *symname) 22697c478bd9Sstevel@tonic-gate { 22707c478bd9Sstevel@tonic-gate int i; 22717c478bd9Sstevel@tonic-gate 22727c478bd9Sstevel@tonic-gate for (i = 0; suppress_sym_list[i] != NULL; i++) { 22737c478bd9Sstevel@tonic-gate if (strcmp(suppress_sym_list[i], symname) == 0) 22747c478bd9Sstevel@tonic-gate return (1); 22757c478bd9Sstevel@tonic-gate } 22767c478bd9Sstevel@tonic-gate 22777c478bd9Sstevel@tonic-gate return (0); 22787c478bd9Sstevel@tonic-gate } 22797c478bd9Sstevel@tonic-gate 22807c478bd9Sstevel@tonic-gate static int 22817c478bd9Sstevel@tonic-gate get_syms(struct module *mp, struct _buf *file) 22827c478bd9Sstevel@tonic-gate { 22837c478bd9Sstevel@tonic-gate uint_t shn; 22847c478bd9Sstevel@tonic-gate Shdr *shp; 22857c478bd9Sstevel@tonic-gate uint_t i; 22867c478bd9Sstevel@tonic-gate Sym *sp, *ksp; 22877c478bd9Sstevel@tonic-gate char *symname; 22887c478bd9Sstevel@tonic-gate int dosymtab = 0; 22897c478bd9Sstevel@tonic-gate extern char stubs_base[], stubs_end[]; 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate /* 22927c478bd9Sstevel@tonic-gate * Find the interesting sections. 22937c478bd9Sstevel@tonic-gate */ 22947c478bd9Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 22957c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 22967c478bd9Sstevel@tonic-gate switch (shp->sh_type) { 22977c478bd9Sstevel@tonic-gate case SHT_SYMTAB: 22987c478bd9Sstevel@tonic-gate mp->symtbl_section = shn; 22997c478bd9Sstevel@tonic-gate mp->symhdr = shp; 23007c478bd9Sstevel@tonic-gate dosymtab++; 23017c478bd9Sstevel@tonic-gate break; 23027c478bd9Sstevel@tonic-gate 23037c478bd9Sstevel@tonic-gate case SHT_RELA: 23047c478bd9Sstevel@tonic-gate case SHT_REL: 23057c478bd9Sstevel@tonic-gate /* 23067c478bd9Sstevel@tonic-gate * Already loaded. 23077c478bd9Sstevel@tonic-gate */ 23087c478bd9Sstevel@tonic-gate if (shp->sh_addr) 23097c478bd9Sstevel@tonic-gate continue; 23107c478bd9Sstevel@tonic-gate shp->sh_addr = (Addr) 23117c478bd9Sstevel@tonic-gate kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP); 23127c478bd9Sstevel@tonic-gate 23137c478bd9Sstevel@tonic-gate if (kobj_read_file(file, (char *)shp->sh_addr, 23147c478bd9Sstevel@tonic-gate shp->sh_size, shp->sh_offset) < 0) { 23157c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_syms: %s, ", 23167c478bd9Sstevel@tonic-gate mp->filename); 23177c478bd9Sstevel@tonic-gate _kobj_printf(ops, "error reading section %d\n", 23187c478bd9Sstevel@tonic-gate shn); 23197c478bd9Sstevel@tonic-gate return (-1); 23207c478bd9Sstevel@tonic-gate } 23217c478bd9Sstevel@tonic-gate break; 23227c478bd9Sstevel@tonic-gate } 23237c478bd9Sstevel@tonic-gate } 23247c478bd9Sstevel@tonic-gate 23257c478bd9Sstevel@tonic-gate /* 23267c478bd9Sstevel@tonic-gate * This is true for a stripped executable. In the case of 23277c478bd9Sstevel@tonic-gate * 'unix' it can be stripped but it still contains the SHT_DYNSYM, 23287c478bd9Sstevel@tonic-gate * and since that symbol information is still present everything 23297c478bd9Sstevel@tonic-gate * is just fine. 23307c478bd9Sstevel@tonic-gate */ 23317c478bd9Sstevel@tonic-gate if (!dosymtab) { 23327c478bd9Sstevel@tonic-gate if (mp->flags & KOBJ_EXEC) 23337c478bd9Sstevel@tonic-gate return (0); 23347c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_syms: %s ", 23357c478bd9Sstevel@tonic-gate mp->filename); 23367c478bd9Sstevel@tonic-gate _kobj_printf(ops, "no SHT_SYMTAB symbol table found\n"); 23377c478bd9Sstevel@tonic-gate return (-1); 23387c478bd9Sstevel@tonic-gate } 23397c478bd9Sstevel@tonic-gate 23407c478bd9Sstevel@tonic-gate /* 23417c478bd9Sstevel@tonic-gate * get the associated string table header 23427c478bd9Sstevel@tonic-gate */ 23437c478bd9Sstevel@tonic-gate if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum)) 23447c478bd9Sstevel@tonic-gate return (-1); 23457c478bd9Sstevel@tonic-gate mp->strhdr = (Shdr *) 23467c478bd9Sstevel@tonic-gate (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize); 23477c478bd9Sstevel@tonic-gate 23487c478bd9Sstevel@tonic-gate mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize; 23497c478bd9Sstevel@tonic-gate mp->hashsize = kobj_gethashsize(mp->nsyms); 23507c478bd9Sstevel@tonic-gate 23517c478bd9Sstevel@tonic-gate /* 23527c478bd9Sstevel@tonic-gate * Allocate space for the symbol table, buckets, chains, and strings. 23537c478bd9Sstevel@tonic-gate */ 23547c478bd9Sstevel@tonic-gate mp->symsize = mp->symhdr->sh_size + 23557c478bd9Sstevel@tonic-gate (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size; 23567c478bd9Sstevel@tonic-gate mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH); 23577c478bd9Sstevel@tonic-gate 23587c478bd9Sstevel@tonic-gate mp->symtbl = mp->symspace; 23597c478bd9Sstevel@tonic-gate mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size); 23607c478bd9Sstevel@tonic-gate mp->chains = mp->buckets + mp->hashsize; 23617c478bd9Sstevel@tonic-gate mp->strings = (char *)(mp->chains + mp->nsyms); 23627c478bd9Sstevel@tonic-gate 23637c478bd9Sstevel@tonic-gate if (kobj_read_file(file, mp->symtbl, 23647c478bd9Sstevel@tonic-gate mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 || 23657c478bd9Sstevel@tonic-gate kobj_read_file(file, mp->strings, 23667c478bd9Sstevel@tonic-gate mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0) 23677c478bd9Sstevel@tonic-gate return (-1); 23687c478bd9Sstevel@tonic-gate 23697c478bd9Sstevel@tonic-gate /* 23707c478bd9Sstevel@tonic-gate * loop through the symbol table adjusting values to account 23717c478bd9Sstevel@tonic-gate * for where each section got loaded into memory. Also 23727c478bd9Sstevel@tonic-gate * fill in the hash table. 23737c478bd9Sstevel@tonic-gate */ 23747c478bd9Sstevel@tonic-gate for (i = 1; i < mp->nsyms; i++) { 23757c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize); 23767c478bd9Sstevel@tonic-gate if (sp->st_shndx < SHN_LORESERVE) { 23777c478bd9Sstevel@tonic-gate if (sp->st_shndx >= mp->hdr.e_shnum) { 23787c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s bad shndx ", 23797c478bd9Sstevel@tonic-gate file->_name); 23807c478bd9Sstevel@tonic-gate _kobj_printf(ops, "in symbol %d\n", i); 23817c478bd9Sstevel@tonic-gate return (-1); 23827c478bd9Sstevel@tonic-gate } 23837c478bd9Sstevel@tonic-gate shp = (Shdr *) 23847c478bd9Sstevel@tonic-gate (mp->shdrs + 23857c478bd9Sstevel@tonic-gate sp->st_shndx * mp->hdr.e_shentsize); 23867c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_EXEC)) 23877c478bd9Sstevel@tonic-gate sp->st_value += shp->sh_addr; 23887c478bd9Sstevel@tonic-gate } 23897c478bd9Sstevel@tonic-gate 23907c478bd9Sstevel@tonic-gate if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF) 23917c478bd9Sstevel@tonic-gate continue; 23927c478bd9Sstevel@tonic-gate if (sp->st_name >= mp->strhdr->sh_size) 23937c478bd9Sstevel@tonic-gate return (-1); 23947c478bd9Sstevel@tonic-gate 23957c478bd9Sstevel@tonic-gate symname = mp->strings + sp->st_name; 23967c478bd9Sstevel@tonic-gate 23977c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_EXEC) && 23987c478bd9Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) == STB_GLOBAL) { 23997c478bd9Sstevel@tonic-gate ksp = kobj_lookup_all(mp, symname, 0); 24007c478bd9Sstevel@tonic-gate 24017c478bd9Sstevel@tonic-gate if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL && 24027c478bd9Sstevel@tonic-gate !kobj_suppress_warning(symname) && 24037c478bd9Sstevel@tonic-gate sp->st_shndx != SHN_UNDEF && 24047c478bd9Sstevel@tonic-gate sp->st_shndx != SHN_COMMON && 24057c478bd9Sstevel@tonic-gate ksp->st_shndx != SHN_UNDEF && 24067c478bd9Sstevel@tonic-gate ksp->st_shndx != SHN_COMMON) { 24077c478bd9Sstevel@tonic-gate /* 24087c478bd9Sstevel@tonic-gate * Unless this symbol is a stub, it's multiply 24097c478bd9Sstevel@tonic-gate * defined. Multiply-defined symbols are 24107c478bd9Sstevel@tonic-gate * usually bad, but some objects (kmdb) have 24117c478bd9Sstevel@tonic-gate * a legitimate need to have their own 24127c478bd9Sstevel@tonic-gate * copies of common functions. 24137c478bd9Sstevel@tonic-gate */ 24147c478bd9Sstevel@tonic-gate if ((standalone || 24157c478bd9Sstevel@tonic-gate ksp->st_value < (uintptr_t)stubs_base || 24167c478bd9Sstevel@tonic-gate ksp->st_value >= (uintptr_t)stubs_end) && 24177c478bd9Sstevel@tonic-gate !(mp->flags & KOBJ_IGNMULDEF)) { 24187c478bd9Sstevel@tonic-gate _kobj_printf(ops, 24197c478bd9Sstevel@tonic-gate "%s symbol ", file->_name); 24207c478bd9Sstevel@tonic-gate _kobj_printf(ops, 24217c478bd9Sstevel@tonic-gate "%s multiply defined\n", symname); 24227c478bd9Sstevel@tonic-gate } 24237c478bd9Sstevel@tonic-gate } 24247c478bd9Sstevel@tonic-gate } 24257c478bd9Sstevel@tonic-gate sym_insert(mp, symname, i); 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate return (0); 24297c478bd9Sstevel@tonic-gate } 24307c478bd9Sstevel@tonic-gate 24317c478bd9Sstevel@tonic-gate static int 24327c478bd9Sstevel@tonic-gate get_ctf(struct module *mp, struct _buf *file) 24337c478bd9Sstevel@tonic-gate { 24347c478bd9Sstevel@tonic-gate char *shstrtab, *ctfdata; 24357c478bd9Sstevel@tonic-gate size_t shstrlen; 24367c478bd9Sstevel@tonic-gate Shdr *shp; 24377c478bd9Sstevel@tonic-gate uint_t i; 24387c478bd9Sstevel@tonic-gate 24397c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_NOCTF) 24407c478bd9Sstevel@tonic-gate return (0); /* do not attempt to even load CTF data */ 24417c478bd9Sstevel@tonic-gate 24427c478bd9Sstevel@tonic-gate if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) { 24437c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_ctf: %s, ", 24447c478bd9Sstevel@tonic-gate mp->filename); 24457c478bd9Sstevel@tonic-gate _kobj_printf(ops, "corrupt e_shstrndx %u\n", 24467c478bd9Sstevel@tonic-gate mp->hdr.e_shstrndx); 24477c478bd9Sstevel@tonic-gate return (-1); 24487c478bd9Sstevel@tonic-gate } 24497c478bd9Sstevel@tonic-gate 24507c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize); 24517c478bd9Sstevel@tonic-gate shstrlen = shp->sh_size; 24527c478bd9Sstevel@tonic-gate shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP); 24537c478bd9Sstevel@tonic-gate 24547c478bd9Sstevel@tonic-gate if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) { 24557c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_ctf: %s, ", 24567c478bd9Sstevel@tonic-gate mp->filename); 24577c478bd9Sstevel@tonic-gate _kobj_printf(ops, "error reading section %u\n", 24587c478bd9Sstevel@tonic-gate mp->hdr.e_shstrndx); 24597c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 24607c478bd9Sstevel@tonic-gate return (-1); 24617c478bd9Sstevel@tonic-gate } 24627c478bd9Sstevel@tonic-gate 24637c478bd9Sstevel@tonic-gate for (i = 0; i < mp->hdr.e_shnum; i++) { 24647c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize); 24657c478bd9Sstevel@tonic-gate 24667c478bd9Sstevel@tonic-gate if (shp->sh_size != 0 && shp->sh_name < shstrlen && 24677c478bd9Sstevel@tonic-gate strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) { 24687c478bd9Sstevel@tonic-gate ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH); 24697c478bd9Sstevel@tonic-gate 24707c478bd9Sstevel@tonic-gate if (kobj_read_file(file, ctfdata, shp->sh_size, 24717c478bd9Sstevel@tonic-gate shp->sh_offset) < 0) { 24727c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_ctf: %s, error " 24737c478bd9Sstevel@tonic-gate "reading .SUNW_ctf data\n", mp->filename); 24747c478bd9Sstevel@tonic-gate kobj_free(ctfdata, shp->sh_size); 24757c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 24767c478bd9Sstevel@tonic-gate return (-1); 24777c478bd9Sstevel@tonic-gate } 24787c478bd9Sstevel@tonic-gate 24797c478bd9Sstevel@tonic-gate mp->ctfdata = ctfdata; 24807c478bd9Sstevel@tonic-gate mp->ctfsize = shp->sh_size; 24817c478bd9Sstevel@tonic-gate break; 24827c478bd9Sstevel@tonic-gate } 24837c478bd9Sstevel@tonic-gate } 24847c478bd9Sstevel@tonic-gate 24857c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 24867c478bd9Sstevel@tonic-gate return (0); 24877c478bd9Sstevel@tonic-gate } 24887c478bd9Sstevel@tonic-gate 24897c478bd9Sstevel@tonic-gate #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */ 24907c478bd9Sstevel@tonic-gate 24917c478bd9Sstevel@tonic-gate /* 24927c478bd9Sstevel@tonic-gate * Return the hash of the ELF sections that are memory resident. 24937c478bd9Sstevel@tonic-gate * i.e. text and data. We skip a SHT_NOBITS section since it occupies 24947c478bd9Sstevel@tonic-gate * no space in the file. We use SHA1 here since libelfsign uses 24957c478bd9Sstevel@tonic-gate * it and both places need to use the same algorithm. 24967c478bd9Sstevel@tonic-gate */ 24977c478bd9Sstevel@tonic-gate static void 24987c478bd9Sstevel@tonic-gate crypto_es_hash(struct module *mp, char *hash, char *shstrtab) 24997c478bd9Sstevel@tonic-gate { 25007c478bd9Sstevel@tonic-gate uint_t shn; 25017c478bd9Sstevel@tonic-gate Shdr *shp; 25027c478bd9Sstevel@tonic-gate SHA1_CTX ctx; 25037c478bd9Sstevel@tonic-gate 25047c478bd9Sstevel@tonic-gate SHA1Init(&ctx); 25057c478bd9Sstevel@tonic-gate 25067c478bd9Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 25077c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize); 25087c478bd9Sstevel@tonic-gate if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0) 25097c478bd9Sstevel@tonic-gate continue; 25107c478bd9Sstevel@tonic-gate 25117c478bd9Sstevel@tonic-gate /* 25127c478bd9Sstevel@tonic-gate * The check should ideally be shp->sh_type == SHT_NOBITS. 25137c478bd9Sstevel@tonic-gate * However, we can't do that check here as get_progbits() 25147c478bd9Sstevel@tonic-gate * resets the type. 25157c478bd9Sstevel@tonic-gate */ 25167c478bd9Sstevel@tonic-gate if (strcmp(shstrtab + shp->sh_name, ".bss") == 0) 25177c478bd9Sstevel@tonic-gate continue; 25187c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 25197c478bd9Sstevel@tonic-gate if (kobj_debug & D_DEBUG) 25207c478bd9Sstevel@tonic-gate _kobj_printf(ops, 25217c478bd9Sstevel@tonic-gate "krtld: crypto_es_hash: updating hash with" 25227c478bd9Sstevel@tonic-gate " %s data size=%d\n", shstrtab + shp->sh_name, 25237c478bd9Sstevel@tonic-gate shp->sh_size); 25247c478bd9Sstevel@tonic-gate #endif 25257c478bd9Sstevel@tonic-gate ASSERT(shp->sh_addr != NULL); 25267c478bd9Sstevel@tonic-gate SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size); 25277c478bd9Sstevel@tonic-gate } 25287c478bd9Sstevel@tonic-gate 25297c478bd9Sstevel@tonic-gate SHA1Final((uchar_t *)hash, &ctx); 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate 25327c478bd9Sstevel@tonic-gate /* 25337c478bd9Sstevel@tonic-gate * Get the .SUNW_signature section for the module, it it exists. 25347c478bd9Sstevel@tonic-gate * 25357c478bd9Sstevel@tonic-gate * This section exists only for crypto modules. None of the 25367c478bd9Sstevel@tonic-gate * primary modules have this section currently. 25377c478bd9Sstevel@tonic-gate */ 25387c478bd9Sstevel@tonic-gate static void 25397c478bd9Sstevel@tonic-gate get_signature(struct module *mp, struct _buf *file) 25407c478bd9Sstevel@tonic-gate { 25417c478bd9Sstevel@tonic-gate char *shstrtab, *sigdata = NULL; 25427c478bd9Sstevel@tonic-gate size_t shstrlen; 25437c478bd9Sstevel@tonic-gate Shdr *shp; 25447c478bd9Sstevel@tonic-gate uint_t i; 25457c478bd9Sstevel@tonic-gate 25467c478bd9Sstevel@tonic-gate if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) { 25477c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_signature: %s, ", 25487c478bd9Sstevel@tonic-gate mp->filename); 25497c478bd9Sstevel@tonic-gate _kobj_printf(ops, "corrupt e_shstrndx %u\n", 25507c478bd9Sstevel@tonic-gate mp->hdr.e_shstrndx); 25517c478bd9Sstevel@tonic-gate return; 25527c478bd9Sstevel@tonic-gate } 25537c478bd9Sstevel@tonic-gate 25547c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize); 25557c478bd9Sstevel@tonic-gate shstrlen = shp->sh_size; 25567c478bd9Sstevel@tonic-gate shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP); 25577c478bd9Sstevel@tonic-gate 25587c478bd9Sstevel@tonic-gate if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) { 25597c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_signature: %s, ", 25607c478bd9Sstevel@tonic-gate mp->filename); 25617c478bd9Sstevel@tonic-gate _kobj_printf(ops, "error reading section %u\n", 25627c478bd9Sstevel@tonic-gate mp->hdr.e_shstrndx); 25637c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 25647c478bd9Sstevel@tonic-gate return; 25657c478bd9Sstevel@tonic-gate } 25667c478bd9Sstevel@tonic-gate 25677c478bd9Sstevel@tonic-gate for (i = 0; i < mp->hdr.e_shnum; i++) { 25687c478bd9Sstevel@tonic-gate shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize); 25697c478bd9Sstevel@tonic-gate if (shp->sh_size != 0 && shp->sh_name < shstrlen && 25707c478bd9Sstevel@tonic-gate strcmp(shstrtab + shp->sh_name, 25717c478bd9Sstevel@tonic-gate ELF_SIGNATURE_SECTION) == 0) { 25727c478bd9Sstevel@tonic-gate filesig_vers_t filesig_version; 25737c478bd9Sstevel@tonic-gate size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH; 25747c478bd9Sstevel@tonic-gate sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH); 25757c478bd9Sstevel@tonic-gate 25767c478bd9Sstevel@tonic-gate if (kobj_read_file(file, sigdata, shp->sh_size, 25777c478bd9Sstevel@tonic-gate shp->sh_offset) < 0) { 25787c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: get_signature: %s," 25797c478bd9Sstevel@tonic-gate " error reading .SUNW_signature data\n", 25807c478bd9Sstevel@tonic-gate mp->filename); 25817c478bd9Sstevel@tonic-gate kobj_free(sigdata, sigsize); 25827c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 25837c478bd9Sstevel@tonic-gate return; 25847c478bd9Sstevel@tonic-gate } 25857c478bd9Sstevel@tonic-gate filesig_version = ((struct filesignatures *)sigdata)-> 25867c478bd9Sstevel@tonic-gate filesig_sig.filesig_version; 25877c478bd9Sstevel@tonic-gate if (!(filesig_version == FILESIG_VERSION1 || 25887c478bd9Sstevel@tonic-gate filesig_version == FILESIG_VERSION3)) { 25897c478bd9Sstevel@tonic-gate /* skip versions we don't understand */ 25907c478bd9Sstevel@tonic-gate kobj_free(sigdata, sigsize); 25917c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 25927c478bd9Sstevel@tonic-gate return; 25937c478bd9Sstevel@tonic-gate } 25947c478bd9Sstevel@tonic-gate 25957c478bd9Sstevel@tonic-gate mp->sigdata = sigdata; 25967c478bd9Sstevel@tonic-gate mp->sigsize = sigsize; 25977c478bd9Sstevel@tonic-gate break; 25987c478bd9Sstevel@tonic-gate } 25997c478bd9Sstevel@tonic-gate } 26007c478bd9Sstevel@tonic-gate 26017c478bd9Sstevel@tonic-gate if (sigdata != NULL) { 26027c478bd9Sstevel@tonic-gate crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab); 26037c478bd9Sstevel@tonic-gate } 26047c478bd9Sstevel@tonic-gate 26057c478bd9Sstevel@tonic-gate kobj_free(shstrtab, shstrlen); 26067c478bd9Sstevel@tonic-gate } 26077c478bd9Sstevel@tonic-gate 26087c478bd9Sstevel@tonic-gate static void 26097c478bd9Sstevel@tonic-gate add_dependent(struct module *mp, struct module *dep) 26107c478bd9Sstevel@tonic-gate { 26117c478bd9Sstevel@tonic-gate struct module_list *lp; 26127c478bd9Sstevel@tonic-gate 26137c478bd9Sstevel@tonic-gate for (lp = mp->head; lp; lp = lp->next) { 26147c478bd9Sstevel@tonic-gate if (lp->mp == dep) 26157c478bd9Sstevel@tonic-gate return; /* already on the list */ 26167c478bd9Sstevel@tonic-gate } 26177c478bd9Sstevel@tonic-gate 26187c478bd9Sstevel@tonic-gate if (lp == NULL) { 26197c478bd9Sstevel@tonic-gate lp = kobj_zalloc(sizeof (*lp), KM_WAIT); 26207c478bd9Sstevel@tonic-gate 26217c478bd9Sstevel@tonic-gate lp->mp = dep; 26227c478bd9Sstevel@tonic-gate lp->next = NULL; 26237c478bd9Sstevel@tonic-gate if (mp->tail) 26247c478bd9Sstevel@tonic-gate mp->tail->next = lp; 26257c478bd9Sstevel@tonic-gate else 26267c478bd9Sstevel@tonic-gate mp->head = lp; 26277c478bd9Sstevel@tonic-gate mp->tail = lp; 26287c478bd9Sstevel@tonic-gate } 26297c478bd9Sstevel@tonic-gate } 26307c478bd9Sstevel@tonic-gate 26317c478bd9Sstevel@tonic-gate static int 26327c478bd9Sstevel@tonic-gate do_dependents(struct modctl *modp, char *modname, size_t modnamelen) 26337c478bd9Sstevel@tonic-gate { 26347c478bd9Sstevel@tonic-gate struct module *mp; 26357c478bd9Sstevel@tonic-gate struct modctl *req; 26367c478bd9Sstevel@tonic-gate char *d, *p, *q; 26377c478bd9Sstevel@tonic-gate int c; 26387c478bd9Sstevel@tonic-gate char *err_modname = NULL; 26397c478bd9Sstevel@tonic-gate 26407c478bd9Sstevel@tonic-gate mp = modp->mod_mp; 26417c478bd9Sstevel@tonic-gate 26427c478bd9Sstevel@tonic-gate if ((p = mp->depends_on) == NULL) 26437c478bd9Sstevel@tonic-gate return (0); 26447c478bd9Sstevel@tonic-gate 26457c478bd9Sstevel@tonic-gate for (;;) { 26467c478bd9Sstevel@tonic-gate /* 26477c478bd9Sstevel@tonic-gate * Skip space. 26487c478bd9Sstevel@tonic-gate */ 26497c478bd9Sstevel@tonic-gate while (*p && (*p == ' ' || *p == '\t')) 26507c478bd9Sstevel@tonic-gate p++; 26517c478bd9Sstevel@tonic-gate /* 26527c478bd9Sstevel@tonic-gate * Get module name. 26537c478bd9Sstevel@tonic-gate */ 26547c478bd9Sstevel@tonic-gate d = p; 26557c478bd9Sstevel@tonic-gate q = modname; 26567c478bd9Sstevel@tonic-gate c = 0; 26577c478bd9Sstevel@tonic-gate while (*p && *p != ' ' && *p != '\t') { 26587c478bd9Sstevel@tonic-gate if (c < modnamelen - 1) { 26597c478bd9Sstevel@tonic-gate *q++ = *p; 26607c478bd9Sstevel@tonic-gate c++; 26617c478bd9Sstevel@tonic-gate } 26627c478bd9Sstevel@tonic-gate p++; 26637c478bd9Sstevel@tonic-gate } 26647c478bd9Sstevel@tonic-gate 26657c478bd9Sstevel@tonic-gate if (q == modname) 26667c478bd9Sstevel@tonic-gate break; 26677c478bd9Sstevel@tonic-gate 26687c478bd9Sstevel@tonic-gate if (c == modnamelen - 1) { 26697c478bd9Sstevel@tonic-gate char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP); 26707c478bd9Sstevel@tonic-gate 26717c478bd9Sstevel@tonic-gate (void) strncpy(dep, d, p - d + 1); 26727c478bd9Sstevel@tonic-gate dep[p - d] = '\0'; 26737c478bd9Sstevel@tonic-gate 26747c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s: dependency ", modp->mod_modname); 26757c478bd9Sstevel@tonic-gate _kobj_printf(ops, "'%s' too long ", dep); 26767c478bd9Sstevel@tonic-gate _kobj_printf(ops, "(max %d chars)\n", modnamelen); 26777c478bd9Sstevel@tonic-gate 26787c478bd9Sstevel@tonic-gate kobj_free(dep, p - d + 1); 26797c478bd9Sstevel@tonic-gate 26807c478bd9Sstevel@tonic-gate return (-1); 26817c478bd9Sstevel@tonic-gate } 26827c478bd9Sstevel@tonic-gate 26837c478bd9Sstevel@tonic-gate *q = '\0'; 26847c478bd9Sstevel@tonic-gate if ((req = mod_load_requisite(modp, modname)) == NULL) { 26857c478bd9Sstevel@tonic-gate #ifndef KOBJ_DEBUG 26867c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_LOADMSG) { 26877c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 26887c478bd9Sstevel@tonic-gate _kobj_printf(ops, 26897c478bd9Sstevel@tonic-gate "%s: unable to resolve dependency, ", 26907c478bd9Sstevel@tonic-gate modp->mod_modname); 26917c478bd9Sstevel@tonic-gate _kobj_printf(ops, "cannot load module '%s'\n", 26927c478bd9Sstevel@tonic-gate modname); 26937c478bd9Sstevel@tonic-gate #ifndef KOBJ_DEBUG 26947c478bd9Sstevel@tonic-gate } 26957c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */ 26967c478bd9Sstevel@tonic-gate if (err_modname == NULL) { 26977c478bd9Sstevel@tonic-gate /* 26987c478bd9Sstevel@tonic-gate * This must be the same size as the modname 26997c478bd9Sstevel@tonic-gate * one. 27007c478bd9Sstevel@tonic-gate */ 27017c478bd9Sstevel@tonic-gate err_modname = kobj_zalloc(MODMAXNAMELEN, 27027c478bd9Sstevel@tonic-gate KM_WAIT); 27037c478bd9Sstevel@tonic-gate 27047c478bd9Sstevel@tonic-gate /* 27057c478bd9Sstevel@tonic-gate * We can use strcpy() here without fearing 27067c478bd9Sstevel@tonic-gate * the NULL terminator because the size of 27077c478bd9Sstevel@tonic-gate * err_modname is the same as one of modname, 27087c478bd9Sstevel@tonic-gate * and it's filled with zeros. 27097c478bd9Sstevel@tonic-gate */ 27107c478bd9Sstevel@tonic-gate (void) strcpy(err_modname, modname); 27117c478bd9Sstevel@tonic-gate } 27127c478bd9Sstevel@tonic-gate continue; 27137c478bd9Sstevel@tonic-gate } 27147c478bd9Sstevel@tonic-gate 27157c478bd9Sstevel@tonic-gate add_dependent(mp, req->mod_mp); 27167c478bd9Sstevel@tonic-gate mod_release_mod(req); 27177c478bd9Sstevel@tonic-gate 27187c478bd9Sstevel@tonic-gate } 27197c478bd9Sstevel@tonic-gate 27207c478bd9Sstevel@tonic-gate if (err_modname != NULL) { 27217c478bd9Sstevel@tonic-gate /* 27227c478bd9Sstevel@tonic-gate * Copy the first module name where you detect an error to keep 27237c478bd9Sstevel@tonic-gate * its behavior the same as before. 27247c478bd9Sstevel@tonic-gate * This way keeps minimizing the memory use for error 27257c478bd9Sstevel@tonic-gate * modules, and this might be important at boot time because 27267c478bd9Sstevel@tonic-gate * the memory usage is a crucial factor for booting in most 27277c478bd9Sstevel@tonic-gate * cases. You can expect more verbose messages when using 27287c478bd9Sstevel@tonic-gate * a debug kernel or setting a bit in moddebug. 27297c478bd9Sstevel@tonic-gate */ 27307c478bd9Sstevel@tonic-gate bzero(modname, MODMAXNAMELEN); 27317c478bd9Sstevel@tonic-gate (void) strcpy(modname, err_modname); 27327c478bd9Sstevel@tonic-gate kobj_free(err_modname, MODMAXNAMELEN); 27337c478bd9Sstevel@tonic-gate return (-1); 27347c478bd9Sstevel@tonic-gate } 27357c478bd9Sstevel@tonic-gate 27367c478bd9Sstevel@tonic-gate return (0); 27377c478bd9Sstevel@tonic-gate } 27387c478bd9Sstevel@tonic-gate 27397c478bd9Sstevel@tonic-gate static int 27407c478bd9Sstevel@tonic-gate do_common(struct module *mp) 27417c478bd9Sstevel@tonic-gate { 27427c478bd9Sstevel@tonic-gate int err; 27437c478bd9Sstevel@tonic-gate 27447c478bd9Sstevel@tonic-gate /* 27457c478bd9Sstevel@tonic-gate * first time through, assign all symbols defined in other 27467c478bd9Sstevel@tonic-gate * modules, and count up how much common space will be needed 27477c478bd9Sstevel@tonic-gate * (bss_size and bss_align) 27487c478bd9Sstevel@tonic-gate */ 27497c478bd9Sstevel@tonic-gate if ((err = do_symbols(mp, 0)) < 0) 27507c478bd9Sstevel@tonic-gate return (err); 27517c478bd9Sstevel@tonic-gate /* 27527c478bd9Sstevel@tonic-gate * increase bss_size by the maximum delta that could be 27537c478bd9Sstevel@tonic-gate * computed by the ALIGN below 27547c478bd9Sstevel@tonic-gate */ 27557c478bd9Sstevel@tonic-gate mp->bss_size += mp->bss_align; 27567c478bd9Sstevel@tonic-gate if (mp->bss_size) { 27577c478bd9Sstevel@tonic-gate if (standalone) 27587c478bd9Sstevel@tonic-gate mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size, 27597c478bd9Sstevel@tonic-gate MINALIGN, 0); 27607c478bd9Sstevel@tonic-gate else 27617c478bd9Sstevel@tonic-gate mp->bss = (uintptr_t)vmem_alloc(data_arena, 27627c478bd9Sstevel@tonic-gate mp->bss_size, VM_SLEEP | VM_BESTFIT); 27637c478bd9Sstevel@tonic-gate bzero((void *)mp->bss, mp->bss_size); 27647c478bd9Sstevel@tonic-gate /* now assign addresses to all common symbols */ 27657c478bd9Sstevel@tonic-gate if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0) 27667c478bd9Sstevel@tonic-gate return (err); 27677c478bd9Sstevel@tonic-gate } 27687c478bd9Sstevel@tonic-gate return (0); 27697c478bd9Sstevel@tonic-gate } 27707c478bd9Sstevel@tonic-gate 27717c478bd9Sstevel@tonic-gate static int 27727c478bd9Sstevel@tonic-gate do_symbols(struct module *mp, Elf64_Addr bss_base) 27737c478bd9Sstevel@tonic-gate { 27747c478bd9Sstevel@tonic-gate int bss_align; 27757c478bd9Sstevel@tonic-gate uintptr_t bss_ptr; 27767c478bd9Sstevel@tonic-gate int err; 27777c478bd9Sstevel@tonic-gate int i; 27787c478bd9Sstevel@tonic-gate Sym *sp, *sp1; 27797c478bd9Sstevel@tonic-gate char *name; 27807c478bd9Sstevel@tonic-gate int assign; 27817c478bd9Sstevel@tonic-gate int resolved = 1; 27827c478bd9Sstevel@tonic-gate 27837c478bd9Sstevel@tonic-gate /* 27847c478bd9Sstevel@tonic-gate * Nothing left to do (optimization). 27857c478bd9Sstevel@tonic-gate */ 27867c478bd9Sstevel@tonic-gate if (mp->flags & KOBJ_RESOLVED) 27877c478bd9Sstevel@tonic-gate return (0); 27887c478bd9Sstevel@tonic-gate 27897c478bd9Sstevel@tonic-gate assign = (bss_base) ? 1 : 0; 27907c478bd9Sstevel@tonic-gate bss_ptr = bss_base; 27917c478bd9Sstevel@tonic-gate bss_align = 0; 27927c478bd9Sstevel@tonic-gate err = 0; 27937c478bd9Sstevel@tonic-gate 27947c478bd9Sstevel@tonic-gate for (i = 1; i < mp->nsyms; i++) { 27957c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i); 27967c478bd9Sstevel@tonic-gate /* 27977c478bd9Sstevel@tonic-gate * we know that st_name is in bounds, since get_sections 27987c478bd9Sstevel@tonic-gate * has already checked all of the symbols 27997c478bd9Sstevel@tonic-gate */ 28007c478bd9Sstevel@tonic-gate name = mp->strings + sp->st_name; 28017c478bd9Sstevel@tonic-gate if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON) 28027c478bd9Sstevel@tonic-gate continue; 28037c478bd9Sstevel@tonic-gate #ifdef __sparc 28047c478bd9Sstevel@tonic-gate /* 28057c478bd9Sstevel@tonic-gate * Register symbols are ignored in the kernel 28067c478bd9Sstevel@tonic-gate */ 28077c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) { 28087c478bd9Sstevel@tonic-gate if (*name != '\0') { 28097c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s: named REGISTER symbol ", 28107c478bd9Sstevel@tonic-gate mp->filename); 28117c478bd9Sstevel@tonic-gate _kobj_printf(ops, "not supported '%s'\n", 28127c478bd9Sstevel@tonic-gate name); 28137c478bd9Sstevel@tonic-gate err = DOSYM_UNDEF; 28147c478bd9Sstevel@tonic-gate } 28157c478bd9Sstevel@tonic-gate continue; 28167c478bd9Sstevel@tonic-gate } 28177c478bd9Sstevel@tonic-gate #endif /* __sparc */ 28187c478bd9Sstevel@tonic-gate /* 28197c478bd9Sstevel@tonic-gate * TLS symbols are ignored in the kernel 28207c478bd9Sstevel@tonic-gate */ 28217c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sp->st_info) == STT_TLS) { 28227c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s: TLS symbol ", 28237c478bd9Sstevel@tonic-gate mp->filename); 28247c478bd9Sstevel@tonic-gate _kobj_printf(ops, "not supported '%s'\n", 28257c478bd9Sstevel@tonic-gate name); 28267c478bd9Sstevel@tonic-gate err = DOSYM_UNDEF; 28277c478bd9Sstevel@tonic-gate continue; 28287c478bd9Sstevel@tonic-gate } 28297c478bd9Sstevel@tonic-gate 28307c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) { 28317c478bd9Sstevel@tonic-gate if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) { 28327c478bd9Sstevel@tonic-gate sp->st_shndx = SHN_ABS; 28337c478bd9Sstevel@tonic-gate sp->st_value = sp1->st_value; 28347c478bd9Sstevel@tonic-gate continue; 28357c478bd9Sstevel@tonic-gate } 28367c478bd9Sstevel@tonic-gate } 28377c478bd9Sstevel@tonic-gate 28387c478bd9Sstevel@tonic-gate if (sp->st_shndx == SHN_UNDEF) { 28397c478bd9Sstevel@tonic-gate resolved = 0; 28407c478bd9Sstevel@tonic-gate 28417c478bd9Sstevel@tonic-gate if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0) 28427c478bd9Sstevel@tonic-gate continue; 28437c478bd9Sstevel@tonic-gate 28447c478bd9Sstevel@tonic-gate /* 28457c478bd9Sstevel@tonic-gate * If it's not a weak reference and it's 28467c478bd9Sstevel@tonic-gate * not a primary object, it's an error. 28477c478bd9Sstevel@tonic-gate * (Primary objects may take more than 28487c478bd9Sstevel@tonic-gate * one pass to resolve) 28497c478bd9Sstevel@tonic-gate */ 28507c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_PRIM) && 28517c478bd9Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) != STB_WEAK) { 28527c478bd9Sstevel@tonic-gate _kobj_printf(ops, "%s: undefined symbol", 28537c478bd9Sstevel@tonic-gate mp->filename); 28547c478bd9Sstevel@tonic-gate _kobj_printf(ops, " '%s'\n", name); 28557c478bd9Sstevel@tonic-gate /* 28567c478bd9Sstevel@tonic-gate * Try to determine whether this symbol 28577c478bd9Sstevel@tonic-gate * represents a dependency on obsolete 28587c478bd9Sstevel@tonic-gate * unsafe driver support. This is just 28597c478bd9Sstevel@tonic-gate * to make the warning more informative. 28607c478bd9Sstevel@tonic-gate */ 28617c478bd9Sstevel@tonic-gate if (strcmp(name, "sleep") == 0 || 28627c478bd9Sstevel@tonic-gate strcmp(name, "unsleep") == 0 || 28637c478bd9Sstevel@tonic-gate strcmp(name, "wakeup") == 0 || 28647c478bd9Sstevel@tonic-gate strcmp(name, "bsd_compat_ioctl") == 0 || 28657c478bd9Sstevel@tonic-gate strcmp(name, "unsafe_driver") == 0 || 28667c478bd9Sstevel@tonic-gate strncmp(name, "spl", 3) == 0 || 28677c478bd9Sstevel@tonic-gate strncmp(name, "i_ddi_spl", 9) == 0) 28687c478bd9Sstevel@tonic-gate err = DOSYM_UNSAFE; 28697c478bd9Sstevel@tonic-gate if (err == 0) 28707c478bd9Sstevel@tonic-gate err = DOSYM_UNDEF; 28717c478bd9Sstevel@tonic-gate } 28727c478bd9Sstevel@tonic-gate continue; 28737c478bd9Sstevel@tonic-gate } 28747c478bd9Sstevel@tonic-gate /* 28757c478bd9Sstevel@tonic-gate * It's a common symbol - st_value is the 28767c478bd9Sstevel@tonic-gate * required alignment. 28777c478bd9Sstevel@tonic-gate */ 28787c478bd9Sstevel@tonic-gate if (sp->st_value > bss_align) 28797c478bd9Sstevel@tonic-gate bss_align = sp->st_value; 28807c478bd9Sstevel@tonic-gate bss_ptr = ALIGN(bss_ptr, sp->st_value); 28817c478bd9Sstevel@tonic-gate if (assign) { 28827c478bd9Sstevel@tonic-gate sp->st_shndx = SHN_ABS; 28837c478bd9Sstevel@tonic-gate sp->st_value = bss_ptr; 28847c478bd9Sstevel@tonic-gate } 28857c478bd9Sstevel@tonic-gate bss_ptr += sp->st_size; 28867c478bd9Sstevel@tonic-gate } 28877c478bd9Sstevel@tonic-gate if (err) 28887c478bd9Sstevel@tonic-gate return (err); 28897c478bd9Sstevel@tonic-gate if (assign == 0 && mp->bss == NULL) { 28907c478bd9Sstevel@tonic-gate mp->bss_align = bss_align; 28917c478bd9Sstevel@tonic-gate mp->bss_size = bss_ptr; 28927c478bd9Sstevel@tonic-gate } else if (resolved) { 28937c478bd9Sstevel@tonic-gate mp->flags |= KOBJ_RESOLVED; 28947c478bd9Sstevel@tonic-gate } 28957c478bd9Sstevel@tonic-gate 28967c478bd9Sstevel@tonic-gate return (0); 28977c478bd9Sstevel@tonic-gate } 28987c478bd9Sstevel@tonic-gate 28997c478bd9Sstevel@tonic-gate uint_t 29007c478bd9Sstevel@tonic-gate kobj_hash_name(const char *p) 29017c478bd9Sstevel@tonic-gate { 29027c478bd9Sstevel@tonic-gate unsigned int g; 29037c478bd9Sstevel@tonic-gate uint_t hval; 29047c478bd9Sstevel@tonic-gate 29057c478bd9Sstevel@tonic-gate hval = 0; 29067c478bd9Sstevel@tonic-gate while (*p) { 29077c478bd9Sstevel@tonic-gate hval = (hval << 4) + *p++; 29087c478bd9Sstevel@tonic-gate if ((g = (hval & 0xf0000000)) != 0) 29097c478bd9Sstevel@tonic-gate hval ^= g >> 24; 29107c478bd9Sstevel@tonic-gate hval &= ~g; 29117c478bd9Sstevel@tonic-gate } 29127c478bd9Sstevel@tonic-gate return (hval); 29137c478bd9Sstevel@tonic-gate } 29147c478bd9Sstevel@tonic-gate 29157c478bd9Sstevel@tonic-gate /* look for name in all modules */ 29167c478bd9Sstevel@tonic-gate uintptr_t 29177c478bd9Sstevel@tonic-gate kobj_getsymvalue(char *name, int kernelonly) 29187c478bd9Sstevel@tonic-gate { 29197c478bd9Sstevel@tonic-gate Sym *sp; 29207c478bd9Sstevel@tonic-gate struct modctl *modp; 29217c478bd9Sstevel@tonic-gate struct module *mp; 29227c478bd9Sstevel@tonic-gate uintptr_t value = 0; 29237c478bd9Sstevel@tonic-gate 29247c478bd9Sstevel@tonic-gate if ((sp = kobj_lookup_kernel(name)) != NULL) 29257c478bd9Sstevel@tonic-gate return ((uintptr_t)sp->st_value); 29267c478bd9Sstevel@tonic-gate 29277c478bd9Sstevel@tonic-gate if (kernelonly) 29287c478bd9Sstevel@tonic-gate return (0); /* didn't find it in the kernel so give up */ 29297c478bd9Sstevel@tonic-gate 29307c478bd9Sstevel@tonic-gate mutex_enter(&mod_lock); 29317c478bd9Sstevel@tonic-gate modp = &modules; 29327c478bd9Sstevel@tonic-gate do { 29337c478bd9Sstevel@tonic-gate mp = (struct module *)modp->mod_mp; 29347c478bd9Sstevel@tonic-gate if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded && 29357c478bd9Sstevel@tonic-gate (sp = lookup_one(mp, name))) { 29367c478bd9Sstevel@tonic-gate value = (uintptr_t)sp->st_value; 29377c478bd9Sstevel@tonic-gate break; 29387c478bd9Sstevel@tonic-gate } 29397c478bd9Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 29407c478bd9Sstevel@tonic-gate mutex_exit(&mod_lock); 29417c478bd9Sstevel@tonic-gate return (value); 29427c478bd9Sstevel@tonic-gate } 29437c478bd9Sstevel@tonic-gate 29447c478bd9Sstevel@tonic-gate /* look for a symbol near value. */ 29457c478bd9Sstevel@tonic-gate char * 29467c478bd9Sstevel@tonic-gate kobj_getsymname(uintptr_t value, ulong_t *offset) 29477c478bd9Sstevel@tonic-gate { 29487c478bd9Sstevel@tonic-gate char *name = NULL; 29497c478bd9Sstevel@tonic-gate struct modctl *modp; 29507c478bd9Sstevel@tonic-gate 29517c478bd9Sstevel@tonic-gate struct modctl_list *lp; 29527c478bd9Sstevel@tonic-gate struct module *mp; 29537c478bd9Sstevel@tonic-gate 29547c478bd9Sstevel@tonic-gate /* 29557c478bd9Sstevel@tonic-gate * Loop through the primary kernel modules. 29567c478bd9Sstevel@tonic-gate */ 29577c478bd9Sstevel@tonic-gate for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) { 29587c478bd9Sstevel@tonic-gate mp = mod(lp); 29597c478bd9Sstevel@tonic-gate 29607c478bd9Sstevel@tonic-gate if ((name = kobj_searchsym(mp, value, offset)) != NULL) 29617c478bd9Sstevel@tonic-gate return (name); 29627c478bd9Sstevel@tonic-gate } 29637c478bd9Sstevel@tonic-gate 29647c478bd9Sstevel@tonic-gate mutex_enter(&mod_lock); 29657c478bd9Sstevel@tonic-gate modp = &modules; 29667c478bd9Sstevel@tonic-gate do { 29677c478bd9Sstevel@tonic-gate mp = (struct module *)modp->mod_mp; 29687c478bd9Sstevel@tonic-gate if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded && 29697c478bd9Sstevel@tonic-gate (name = kobj_searchsym(mp, value, offset))) 29707c478bd9Sstevel@tonic-gate break; 29717c478bd9Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 29727c478bd9Sstevel@tonic-gate mutex_exit(&mod_lock); 29737c478bd9Sstevel@tonic-gate return (name); 29747c478bd9Sstevel@tonic-gate } 29757c478bd9Sstevel@tonic-gate 29767c478bd9Sstevel@tonic-gate /* return address of symbol and size */ 29777c478bd9Sstevel@tonic-gate 29787c478bd9Sstevel@tonic-gate uintptr_t 29797c478bd9Sstevel@tonic-gate kobj_getelfsym(char *name, void *mp, int *size) 29807c478bd9Sstevel@tonic-gate { 29817c478bd9Sstevel@tonic-gate Sym *sp; 29827c478bd9Sstevel@tonic-gate 29837c478bd9Sstevel@tonic-gate if (mp == NULL) 29847c478bd9Sstevel@tonic-gate sp = kobj_lookup_kernel(name); 29857c478bd9Sstevel@tonic-gate else 29867c478bd9Sstevel@tonic-gate sp = lookup_one(mp, name); 29877c478bd9Sstevel@tonic-gate 29887c478bd9Sstevel@tonic-gate if (sp == NULL) 29897c478bd9Sstevel@tonic-gate return (0); 29907c478bd9Sstevel@tonic-gate 29917c478bd9Sstevel@tonic-gate *size = (int)sp->st_size; 29927c478bd9Sstevel@tonic-gate return ((uintptr_t)sp->st_value); 29937c478bd9Sstevel@tonic-gate } 29947c478bd9Sstevel@tonic-gate 29957c478bd9Sstevel@tonic-gate uintptr_t 29967aec1d6eScindi kobj_lookup(struct module *mod, const char *name) 29977c478bd9Sstevel@tonic-gate { 29987c478bd9Sstevel@tonic-gate Sym *sp; 29997c478bd9Sstevel@tonic-gate 30007c478bd9Sstevel@tonic-gate sp = lookup_one(mod, name); 30017c478bd9Sstevel@tonic-gate 30027c478bd9Sstevel@tonic-gate if (sp == NULL) 30037c478bd9Sstevel@tonic-gate return (0); 30047c478bd9Sstevel@tonic-gate 30057c478bd9Sstevel@tonic-gate return ((uintptr_t)sp->st_value); 30067c478bd9Sstevel@tonic-gate } 30077c478bd9Sstevel@tonic-gate 30087c478bd9Sstevel@tonic-gate char * 30097c478bd9Sstevel@tonic-gate kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset) 30107c478bd9Sstevel@tonic-gate { 30117c478bd9Sstevel@tonic-gate Sym *symtabptr; 30127c478bd9Sstevel@tonic-gate char *strtabptr; 30137c478bd9Sstevel@tonic-gate int symnum; 30147c478bd9Sstevel@tonic-gate Sym *sym; 30157c478bd9Sstevel@tonic-gate Sym *cursym; 30167c478bd9Sstevel@tonic-gate uintptr_t curval; 30177c478bd9Sstevel@tonic-gate 30187c478bd9Sstevel@tonic-gate *offset = (ulong_t)-1l; /* assume not found */ 30197c478bd9Sstevel@tonic-gate cursym = NULL; 30207c478bd9Sstevel@tonic-gate 30217c478bd9Sstevel@tonic-gate if (kobj_addrcheck(mp, (void *)value) != 0) 30227c478bd9Sstevel@tonic-gate return (NULL); /* not in this module */ 30237c478bd9Sstevel@tonic-gate 30247c478bd9Sstevel@tonic-gate strtabptr = mp->strings; 30257c478bd9Sstevel@tonic-gate symtabptr = (Sym *)mp->symtbl; 30267c478bd9Sstevel@tonic-gate 30277c478bd9Sstevel@tonic-gate /* 30287c478bd9Sstevel@tonic-gate * Scan the module's symbol table for a symbol <= value 30297c478bd9Sstevel@tonic-gate */ 30307c478bd9Sstevel@tonic-gate for (symnum = 1, sym = symtabptr + 1; 30317c478bd9Sstevel@tonic-gate symnum < mp->nsyms; symnum++, sym = (Sym *) 30327c478bd9Sstevel@tonic-gate ((uintptr_t)sym + mp->symhdr->sh_entsize)) { 30337c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) { 30347c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(sym->st_info) != STB_LOCAL) 30357c478bd9Sstevel@tonic-gate continue; 30367c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 30377c478bd9Sstevel@tonic-gate ELF_ST_TYPE(sym->st_info) != STT_FUNC) 30387c478bd9Sstevel@tonic-gate continue; 30397c478bd9Sstevel@tonic-gate } 30407c478bd9Sstevel@tonic-gate 30417c478bd9Sstevel@tonic-gate curval = (uintptr_t)sym->st_value; 30427c478bd9Sstevel@tonic-gate 30437c478bd9Sstevel@tonic-gate if (curval > value) 30447c478bd9Sstevel@tonic-gate continue; 30457c478bd9Sstevel@tonic-gate 30467c478bd9Sstevel@tonic-gate /* 30477c478bd9Sstevel@tonic-gate * If one or both are functions... 30487c478bd9Sstevel@tonic-gate */ 30497c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL && 30507c478bd9Sstevel@tonic-gate ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) { 30517c478bd9Sstevel@tonic-gate /* Ignore if the address is out of the bounds */ 30527c478bd9Sstevel@tonic-gate if (value - sym->st_value >= sym->st_size) 30537c478bd9Sstevel@tonic-gate continue; 30547c478bd9Sstevel@tonic-gate 30557c478bd9Sstevel@tonic-gate if (cursym != NULL && 30567c478bd9Sstevel@tonic-gate ELF_ST_TYPE(cursym->st_info) == STT_FUNC) { 30577c478bd9Sstevel@tonic-gate /* Prefer the function to the non-function */ 30587c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) 30597c478bd9Sstevel@tonic-gate continue; 30607c478bd9Sstevel@tonic-gate 30617c478bd9Sstevel@tonic-gate /* Prefer the larger of the two functions */ 30627c478bd9Sstevel@tonic-gate if (sym->st_size <= cursym->st_size) 30637c478bd9Sstevel@tonic-gate continue; 30647c478bd9Sstevel@tonic-gate } 30657c478bd9Sstevel@tonic-gate } else if (value - curval >= *offset) { 30667c478bd9Sstevel@tonic-gate continue; 30677c478bd9Sstevel@tonic-gate } 30687c478bd9Sstevel@tonic-gate 30697c478bd9Sstevel@tonic-gate *offset = (ulong_t)(value - curval); 30707c478bd9Sstevel@tonic-gate cursym = sym; 30717c478bd9Sstevel@tonic-gate } 30727c478bd9Sstevel@tonic-gate if (cursym == NULL) 30737c478bd9Sstevel@tonic-gate return (NULL); 30747c478bd9Sstevel@tonic-gate 30757c478bd9Sstevel@tonic-gate return (strtabptr + cursym->st_name); 30767c478bd9Sstevel@tonic-gate } 30777c478bd9Sstevel@tonic-gate 30787c478bd9Sstevel@tonic-gate Sym * 30797c478bd9Sstevel@tonic-gate kobj_lookup_all(struct module *mp, char *name, int include_self) 30807c478bd9Sstevel@tonic-gate { 30817c478bd9Sstevel@tonic-gate Sym *sp; 30827c478bd9Sstevel@tonic-gate struct module_list *mlp; 30837c478bd9Sstevel@tonic-gate struct modctl_list *clp; 30847c478bd9Sstevel@tonic-gate struct module *mmp; 30857c478bd9Sstevel@tonic-gate 30867c478bd9Sstevel@tonic-gate if (include_self && (sp = lookup_one(mp, name)) != NULL) 30877c478bd9Sstevel@tonic-gate return (sp); 30887c478bd9Sstevel@tonic-gate 30897c478bd9Sstevel@tonic-gate for (mlp = mp->head; mlp; mlp = mlp->next) { 30907c478bd9Sstevel@tonic-gate if ((sp = lookup_one(mlp->mp, name)) != NULL && 30917c478bd9Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) != STB_LOCAL) 30927c478bd9Sstevel@tonic-gate return (sp); 30937c478bd9Sstevel@tonic-gate } 30947c478bd9Sstevel@tonic-gate 30957c478bd9Sstevel@tonic-gate /* 30967c478bd9Sstevel@tonic-gate * Loop through the primary kernel modules. 30977c478bd9Sstevel@tonic-gate */ 30987c478bd9Sstevel@tonic-gate for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) { 30997c478bd9Sstevel@tonic-gate mmp = mod(clp); 31007c478bd9Sstevel@tonic-gate 31017c478bd9Sstevel@tonic-gate if (mmp == NULL || mp == mmp) 31027c478bd9Sstevel@tonic-gate continue; 31037c478bd9Sstevel@tonic-gate 31047c478bd9Sstevel@tonic-gate if ((sp = lookup_one(mmp, name)) != NULL && 31057c478bd9Sstevel@tonic-gate ELF_ST_BIND(sp->st_info) != STB_LOCAL) 31067c478bd9Sstevel@tonic-gate return (sp); 31077c478bd9Sstevel@tonic-gate } 31087c478bd9Sstevel@tonic-gate return (NULL); 31097c478bd9Sstevel@tonic-gate } 31107c478bd9Sstevel@tonic-gate 31117c478bd9Sstevel@tonic-gate Sym * 31127c478bd9Sstevel@tonic-gate kobj_lookup_kernel(const char *name) 31137c478bd9Sstevel@tonic-gate { 31147c478bd9Sstevel@tonic-gate struct modctl_list *lp; 31157c478bd9Sstevel@tonic-gate struct module *mp; 31167c478bd9Sstevel@tonic-gate Sym *sp; 31177c478bd9Sstevel@tonic-gate 31187c478bd9Sstevel@tonic-gate /* 31197c478bd9Sstevel@tonic-gate * Loop through the primary kernel modules. 31207c478bd9Sstevel@tonic-gate */ 31217c478bd9Sstevel@tonic-gate for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) { 31227c478bd9Sstevel@tonic-gate mp = mod(lp); 31237c478bd9Sstevel@tonic-gate 31247c478bd9Sstevel@tonic-gate if (mp == NULL) 31257c478bd9Sstevel@tonic-gate continue; 31267c478bd9Sstevel@tonic-gate 31277c478bd9Sstevel@tonic-gate if ((sp = lookup_one(mp, name)) != NULL) 31287c478bd9Sstevel@tonic-gate return (sp); 31297c478bd9Sstevel@tonic-gate } 31307c478bd9Sstevel@tonic-gate return (NULL); 31317c478bd9Sstevel@tonic-gate } 31327c478bd9Sstevel@tonic-gate 31337c478bd9Sstevel@tonic-gate static Sym * 31347c478bd9Sstevel@tonic-gate lookup_one(struct module *mp, const char *name) 31357c478bd9Sstevel@tonic-gate { 31367c478bd9Sstevel@tonic-gate symid_t *ip; 31377c478bd9Sstevel@tonic-gate char *name1; 31387c478bd9Sstevel@tonic-gate Sym *sp; 31397c478bd9Sstevel@tonic-gate 31407c478bd9Sstevel@tonic-gate for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 31417c478bd9Sstevel@tonic-gate ip = &mp->chains[*ip]) { 31427c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + 31437c478bd9Sstevel@tonic-gate mp->symhdr->sh_entsize * *ip); 31447c478bd9Sstevel@tonic-gate name1 = mp->strings + sp->st_name; 31457c478bd9Sstevel@tonic-gate if (strcmp(name, name1) == 0 && 31467c478bd9Sstevel@tonic-gate ELF_ST_TYPE(sp->st_info) != STT_FILE && 31477c478bd9Sstevel@tonic-gate sp->st_shndx != SHN_UNDEF && 31487c478bd9Sstevel@tonic-gate sp->st_shndx != SHN_COMMON) 31497c478bd9Sstevel@tonic-gate return (sp); 31507c478bd9Sstevel@tonic-gate } 31517c478bd9Sstevel@tonic-gate return (NULL); 31527c478bd9Sstevel@tonic-gate } 31537c478bd9Sstevel@tonic-gate 31547c478bd9Sstevel@tonic-gate /* 31557c478bd9Sstevel@tonic-gate * Lookup a given symbol pointer in the module's symbol hash. If the symbol 31567c478bd9Sstevel@tonic-gate * is hashed, return the symbol pointer; otherwise return NULL. 31577c478bd9Sstevel@tonic-gate */ 31587c478bd9Sstevel@tonic-gate static Sym * 31597c478bd9Sstevel@tonic-gate sym_lookup(struct module *mp, Sym *ksp) 31607c478bd9Sstevel@tonic-gate { 31617c478bd9Sstevel@tonic-gate char *name = mp->strings + ksp->st_name; 31627c478bd9Sstevel@tonic-gate symid_t *ip; 31637c478bd9Sstevel@tonic-gate Sym *sp; 31647c478bd9Sstevel@tonic-gate 31657c478bd9Sstevel@tonic-gate for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 31667c478bd9Sstevel@tonic-gate ip = &mp->chains[*ip]) { 31677c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip); 31687c478bd9Sstevel@tonic-gate if (sp == ksp) 31697c478bd9Sstevel@tonic-gate return (ksp); 31707c478bd9Sstevel@tonic-gate } 31717c478bd9Sstevel@tonic-gate return (NULL); 31727c478bd9Sstevel@tonic-gate } 31737c478bd9Sstevel@tonic-gate 31747c478bd9Sstevel@tonic-gate static void 31757c478bd9Sstevel@tonic-gate sym_insert(struct module *mp, char *name, symid_t index) 31767c478bd9Sstevel@tonic-gate { 31777c478bd9Sstevel@tonic-gate symid_t *ip; 31787c478bd9Sstevel@tonic-gate 31797c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 31807c478bd9Sstevel@tonic-gate if (kobj_debug & D_SYMBOLS) { 31817c478bd9Sstevel@tonic-gate static struct module *lastmp = NULL; 31827c478bd9Sstevel@tonic-gate Sym *sp; 31837c478bd9Sstevel@tonic-gate if (lastmp != mp) { 31847c478bd9Sstevel@tonic-gate _kobj_printf(ops, 31857c478bd9Sstevel@tonic-gate "krtld: symbol entry: file=%s\n", 31867c478bd9Sstevel@tonic-gate mp->filename); 31877c478bd9Sstevel@tonic-gate _kobj_printf(ops, 31887c478bd9Sstevel@tonic-gate "krtld:\tsymndx\tvalue\t\t" 31897c478bd9Sstevel@tonic-gate "symbol name\n"); 31907c478bd9Sstevel@tonic-gate lastmp = mp; 31917c478bd9Sstevel@tonic-gate } 31927c478bd9Sstevel@tonic-gate sp = (Sym *)(mp->symtbl + 31937c478bd9Sstevel@tonic-gate index * mp->symhdr->sh_entsize); 31947c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld:\t[%3d]", index); 31957c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\t0x%lx", sp->st_value); 31967c478bd9Sstevel@tonic-gate _kobj_printf(ops, "\t%s\n", name); 31977c478bd9Sstevel@tonic-gate } 31987c478bd9Sstevel@tonic-gate 31997c478bd9Sstevel@tonic-gate #endif 32007c478bd9Sstevel@tonic-gate for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip; 32017c478bd9Sstevel@tonic-gate ip = &mp->chains[*ip]) { 32027c478bd9Sstevel@tonic-gate ; 32037c478bd9Sstevel@tonic-gate } 32047c478bd9Sstevel@tonic-gate *ip = index; 32057c478bd9Sstevel@tonic-gate } 32067c478bd9Sstevel@tonic-gate 32077c478bd9Sstevel@tonic-gate struct modctl * 32087c478bd9Sstevel@tonic-gate kobj_boot_mod_lookup(const char *modname) 32097c478bd9Sstevel@tonic-gate { 32107c478bd9Sstevel@tonic-gate struct modctl *mctl = kobj_modules; 32117c478bd9Sstevel@tonic-gate 32127c478bd9Sstevel@tonic-gate do { 32137c478bd9Sstevel@tonic-gate if (strcmp(modname, mctl->mod_modname) == 0) 32147c478bd9Sstevel@tonic-gate return (mctl); 32157c478bd9Sstevel@tonic-gate } while ((mctl = mctl->mod_next) != kobj_modules); 32167c478bd9Sstevel@tonic-gate 32177c478bd9Sstevel@tonic-gate return (NULL); 32187c478bd9Sstevel@tonic-gate } 32197c478bd9Sstevel@tonic-gate 32207c478bd9Sstevel@tonic-gate /* 32215c311300Scth * Determine if the module exists. 32225c311300Scth */ 32235c311300Scth int 32245c311300Scth kobj_path_exists(char *name, int use_path) 32255c311300Scth { 32265c311300Scth struct _buf *file; 32275c311300Scth 32285c311300Scth file = kobj_open_path(name, use_path, 1); 32295c311300Scth #ifdef MODDIR_SUFFIX 32305c311300Scth if (file == (struct _buf *)-1) 32315c311300Scth file = kobj_open_path(name, use_path, 0); 32325c311300Scth #endif /* MODDIR_SUFFIX */ 32335c311300Scth if (file == (struct _buf *)-1) 32345c311300Scth return (0); 32355c311300Scth kobj_close_file(file); 32365c311300Scth return (1); 32375c311300Scth } 32385c311300Scth 32395c311300Scth /* 32407c478bd9Sstevel@tonic-gate * fullname is dynamically allocated to be able to hold the 32417c478bd9Sstevel@tonic-gate * maximum size string that can be constructed from name. 32427c478bd9Sstevel@tonic-gate * path is exactly like the shell PATH variable. 32437c478bd9Sstevel@tonic-gate */ 32447c478bd9Sstevel@tonic-gate struct _buf * 32457c478bd9Sstevel@tonic-gate kobj_open_path(char *name, int use_path, int use_moddir_suffix) 32467c478bd9Sstevel@tonic-gate { 32477c478bd9Sstevel@tonic-gate char *p, *q; 32487c478bd9Sstevel@tonic-gate char *pathp; 32497c478bd9Sstevel@tonic-gate char *pathpsave; 32507c478bd9Sstevel@tonic-gate char *fullname; 32517c478bd9Sstevel@tonic-gate int maxpathlen; 32527c478bd9Sstevel@tonic-gate struct _buf *file; 32537c478bd9Sstevel@tonic-gate 32547c478bd9Sstevel@tonic-gate #if !defined(MODDIR_SUFFIX) 32557c478bd9Sstevel@tonic-gate use_moddir_suffix = B_FALSE; 32567c478bd9Sstevel@tonic-gate #endif 32577c478bd9Sstevel@tonic-gate 32587c478bd9Sstevel@tonic-gate if (!use_path) 32597c478bd9Sstevel@tonic-gate pathp = ""; /* use name as specified */ 32607c478bd9Sstevel@tonic-gate else 32617c478bd9Sstevel@tonic-gate pathp = module_path; /* use configured default path */ 32627c478bd9Sstevel@tonic-gate 32637c478bd9Sstevel@tonic-gate pathpsave = pathp; /* keep this for error reporting */ 32647c478bd9Sstevel@tonic-gate 32657c478bd9Sstevel@tonic-gate /* 32667c478bd9Sstevel@tonic-gate * Allocate enough space for the largest possible fullname. 32677c478bd9Sstevel@tonic-gate * since path is of the form <directory> : <directory> : ... 32687c478bd9Sstevel@tonic-gate * we're potentially allocating a little more than we need to 32697c478bd9Sstevel@tonic-gate * but we'll allocate the exact amount when we find the right directory. 32707c478bd9Sstevel@tonic-gate * (The + 3 below is one for NULL terminator and one for the '/' 32717c478bd9Sstevel@tonic-gate * we might have to add at the beginning of path and one for 32727c478bd9Sstevel@tonic-gate * the '/' between path and name.) 32737c478bd9Sstevel@tonic-gate */ 32747c478bd9Sstevel@tonic-gate maxpathlen = strlen(pathp) + strlen(name) + 3; 32757c478bd9Sstevel@tonic-gate /* sizeof includes null */ 32767c478bd9Sstevel@tonic-gate maxpathlen += sizeof (slash_moddir_suffix_slash) - 1; 32777c478bd9Sstevel@tonic-gate fullname = kobj_zalloc(maxpathlen, KM_WAIT); 32787c478bd9Sstevel@tonic-gate 32797c478bd9Sstevel@tonic-gate for (;;) { 32807c478bd9Sstevel@tonic-gate p = fullname; 32817c478bd9Sstevel@tonic-gate if (*pathp != '\0' && *pathp != '/') 32827c478bd9Sstevel@tonic-gate *p++ = '/'; /* path must start with '/' */ 32837c478bd9Sstevel@tonic-gate while (*pathp && *pathp != ':' && *pathp != ' ') 32847c478bd9Sstevel@tonic-gate *p++ = *pathp++; 32857c478bd9Sstevel@tonic-gate if (p != fullname && p[-1] != '/') 32867c478bd9Sstevel@tonic-gate *p++ = '/'; 32877c478bd9Sstevel@tonic-gate if (use_moddir_suffix) { 32887c478bd9Sstevel@tonic-gate char *b = basename(name); 32897c478bd9Sstevel@tonic-gate char *s; 32907c478bd9Sstevel@tonic-gate 32917c478bd9Sstevel@tonic-gate /* copy everything up to the base name */ 32927c478bd9Sstevel@tonic-gate q = name; 32937c478bd9Sstevel@tonic-gate while (q != b && *q) 32947c478bd9Sstevel@tonic-gate *p++ = *q++; 32957c478bd9Sstevel@tonic-gate s = slash_moddir_suffix_slash; 32967c478bd9Sstevel@tonic-gate while (*s) 32977c478bd9Sstevel@tonic-gate *p++ = *s++; 32987c478bd9Sstevel@tonic-gate /* copy the rest */ 32997c478bd9Sstevel@tonic-gate while (*b) 33007c478bd9Sstevel@tonic-gate *p++ = *b++; 33017c478bd9Sstevel@tonic-gate } else { 33027c478bd9Sstevel@tonic-gate q = name; 33037c478bd9Sstevel@tonic-gate while (*q) 33047c478bd9Sstevel@tonic-gate *p++ = *q++; 33057c478bd9Sstevel@tonic-gate } 33067c478bd9Sstevel@tonic-gate *p = 0; 33077c478bd9Sstevel@tonic-gate if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) { 33087c478bd9Sstevel@tonic-gate kobj_free(fullname, maxpathlen); 33097c478bd9Sstevel@tonic-gate return (file); 33107c478bd9Sstevel@tonic-gate } 33117c478bd9Sstevel@tonic-gate if (*pathp == 0) 33127c478bd9Sstevel@tonic-gate break; 33137c478bd9Sstevel@tonic-gate pathp++; 33147c478bd9Sstevel@tonic-gate } 33157c478bd9Sstevel@tonic-gate kobj_free(fullname, maxpathlen); 33167c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 33177c478bd9Sstevel@tonic-gate _kobj_printf(ops, "can't open %s,", name); 33187c478bd9Sstevel@tonic-gate _kobj_printf(ops, " path is %s\n", pathpsave); 33197c478bd9Sstevel@tonic-gate } 33207c478bd9Sstevel@tonic-gate return ((struct _buf *)-1); 33217c478bd9Sstevel@tonic-gate } 33227c478bd9Sstevel@tonic-gate 33237c478bd9Sstevel@tonic-gate intptr_t 33247c478bd9Sstevel@tonic-gate kobj_open(char *filename) 33257c478bd9Sstevel@tonic-gate { 33267c478bd9Sstevel@tonic-gate struct vnode *vp; 33277c478bd9Sstevel@tonic-gate int fd; 33287c478bd9Sstevel@tonic-gate 33297c478bd9Sstevel@tonic-gate if (_modrootloaded) { 33307c478bd9Sstevel@tonic-gate struct kobjopen_tctl *ltp = kobjopen_alloc(filename); 33317c478bd9Sstevel@tonic-gate int Errno; 33327c478bd9Sstevel@tonic-gate 33337c478bd9Sstevel@tonic-gate /* 33347c478bd9Sstevel@tonic-gate * Hand off the open to a thread who has a 33357c478bd9Sstevel@tonic-gate * stack size capable handling the request. 33367c478bd9Sstevel@tonic-gate */ 33377c478bd9Sstevel@tonic-gate if (curthread != &t0) { 33387c478bd9Sstevel@tonic-gate (void) thread_create(NULL, DEFAULTSTKSZ * 2, 33397c478bd9Sstevel@tonic-gate kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri); 33407c478bd9Sstevel@tonic-gate sema_p(<p->sema); 33417c478bd9Sstevel@tonic-gate Errno = ltp->Errno; 33427c478bd9Sstevel@tonic-gate vp = ltp->vp; 33437c478bd9Sstevel@tonic-gate } else { 33447c478bd9Sstevel@tonic-gate /* 33457c478bd9Sstevel@tonic-gate * 1098067: module creds should not be those of the 33467c478bd9Sstevel@tonic-gate * caller 33477c478bd9Sstevel@tonic-gate */ 33487c478bd9Sstevel@tonic-gate cred_t *saved_cred = curthread->t_cred; 33497c478bd9Sstevel@tonic-gate curthread->t_cred = kcred; 3350ea8dc4b6Seschrock Errno = vn_openat(filename, UIO_SYSSPACE, FREAD, 0, &vp, 3351ea8dc4b6Seschrock 0, 0, rootdir); 33527c478bd9Sstevel@tonic-gate curthread->t_cred = saved_cred; 33537c478bd9Sstevel@tonic-gate } 33547c478bd9Sstevel@tonic-gate kobjopen_free(ltp); 33557c478bd9Sstevel@tonic-gate 33567c478bd9Sstevel@tonic-gate if (Errno) { 33577c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 33587c478bd9Sstevel@tonic-gate _kobj_printf(ops, 33597c478bd9Sstevel@tonic-gate "kobj_open: vn_open of %s fails, ", 33607c478bd9Sstevel@tonic-gate filename); 33617c478bd9Sstevel@tonic-gate _kobj_printf(ops, "Errno = %d\n", Errno); 33627c478bd9Sstevel@tonic-gate } 33637c478bd9Sstevel@tonic-gate return (-1); 33647c478bd9Sstevel@tonic-gate } else { 33657c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 33667c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_open: '%s'", filename); 33677c478bd9Sstevel@tonic-gate _kobj_printf(ops, " vp = %p\n", vp); 33687c478bd9Sstevel@tonic-gate } 33697c478bd9Sstevel@tonic-gate return ((intptr_t)vp); 33707c478bd9Sstevel@tonic-gate } 33717c478bd9Sstevel@tonic-gate } else { 33727c478bd9Sstevel@tonic-gate fd = kobj_boot_open(filename, 0); 33737c478bd9Sstevel@tonic-gate 33747c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 33757c478bd9Sstevel@tonic-gate if (fd < 0) 33767c478bd9Sstevel@tonic-gate _kobj_printf(ops, 33777c478bd9Sstevel@tonic-gate "kobj_open: can't open %s\n", filename); 33787c478bd9Sstevel@tonic-gate else { 33797c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_open: '%s'", filename); 33807c478bd9Sstevel@tonic-gate _kobj_printf(ops, " descr = 0x%x\n", fd); 33817c478bd9Sstevel@tonic-gate } 33827c478bd9Sstevel@tonic-gate } 33837c478bd9Sstevel@tonic-gate return ((intptr_t)fd); 33847c478bd9Sstevel@tonic-gate } 33857c478bd9Sstevel@tonic-gate } 33867c478bd9Sstevel@tonic-gate 33877c478bd9Sstevel@tonic-gate /* 33887c478bd9Sstevel@tonic-gate * Calls to kobj_open() are handled off to this routine as a separate thread. 33897c478bd9Sstevel@tonic-gate */ 33907c478bd9Sstevel@tonic-gate static void 33917c478bd9Sstevel@tonic-gate kobjopen_thread(struct kobjopen_tctl *ltp) 33927c478bd9Sstevel@tonic-gate { 33937c478bd9Sstevel@tonic-gate kmutex_t cpr_lk; 33947c478bd9Sstevel@tonic-gate callb_cpr_t cpr_i; 33957c478bd9Sstevel@tonic-gate 33967c478bd9Sstevel@tonic-gate mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL); 33977c478bd9Sstevel@tonic-gate CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen"); 33987c478bd9Sstevel@tonic-gate ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp), 33997c478bd9Sstevel@tonic-gate 0, 0); 34007c478bd9Sstevel@tonic-gate sema_v(<p->sema); 34017c478bd9Sstevel@tonic-gate mutex_enter(&cpr_lk); 34027c478bd9Sstevel@tonic-gate CALLB_CPR_EXIT(&cpr_i); 34037c478bd9Sstevel@tonic-gate mutex_destroy(&cpr_lk); 34047c478bd9Sstevel@tonic-gate thread_exit(); 34057c478bd9Sstevel@tonic-gate } 34067c478bd9Sstevel@tonic-gate 34077c478bd9Sstevel@tonic-gate /* 34087c478bd9Sstevel@tonic-gate * allocate and initialize a kobjopen thread structure 34097c478bd9Sstevel@tonic-gate */ 34107c478bd9Sstevel@tonic-gate static struct kobjopen_tctl * 34117c478bd9Sstevel@tonic-gate kobjopen_alloc(char *filename) 34127c478bd9Sstevel@tonic-gate { 34137c478bd9Sstevel@tonic-gate struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP); 34147c478bd9Sstevel@tonic-gate 34157c478bd9Sstevel@tonic-gate ASSERT(filename != NULL); 34167c478bd9Sstevel@tonic-gate 34177c478bd9Sstevel@tonic-gate ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP); 34187c478bd9Sstevel@tonic-gate bcopy(filename, ltp->name, strlen(filename) + 1); 34197c478bd9Sstevel@tonic-gate sema_init(<p->sema, 0, NULL, SEMA_DEFAULT, NULL); 34207c478bd9Sstevel@tonic-gate return (ltp); 34217c478bd9Sstevel@tonic-gate } 34227c478bd9Sstevel@tonic-gate 34237c478bd9Sstevel@tonic-gate /* 34247c478bd9Sstevel@tonic-gate * free a kobjopen thread control structure 34257c478bd9Sstevel@tonic-gate */ 34267c478bd9Sstevel@tonic-gate static void 34277c478bd9Sstevel@tonic-gate kobjopen_free(struct kobjopen_tctl *ltp) 34287c478bd9Sstevel@tonic-gate { 34297c478bd9Sstevel@tonic-gate sema_destroy(<p->sema); 34307c478bd9Sstevel@tonic-gate kmem_free(ltp->name, strlen(ltp->name) + 1); 34317c478bd9Sstevel@tonic-gate kmem_free(ltp, sizeof (*ltp)); 34327c478bd9Sstevel@tonic-gate } 34337c478bd9Sstevel@tonic-gate 34347c478bd9Sstevel@tonic-gate int 34357c478bd9Sstevel@tonic-gate kobj_read(intptr_t descr, char *buf, unsigned size, unsigned offset) 34367c478bd9Sstevel@tonic-gate { 34377c478bd9Sstevel@tonic-gate int stat; 34387c478bd9Sstevel@tonic-gate ssize_t resid; 34397c478bd9Sstevel@tonic-gate 34407c478bd9Sstevel@tonic-gate if (_modrootloaded) { 34417c478bd9Sstevel@tonic-gate if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size, 34427c478bd9Sstevel@tonic-gate (offset_t)offset, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), 34437c478bd9Sstevel@tonic-gate &resid)) != 0) { 34447c478bd9Sstevel@tonic-gate _kobj_printf(ops, 34457c478bd9Sstevel@tonic-gate "vn_rdwr failed with error 0x%x\n", stat); 34467c478bd9Sstevel@tonic-gate return (-1); 34477c478bd9Sstevel@tonic-gate } 34487c478bd9Sstevel@tonic-gate return (size - resid); 34497c478bd9Sstevel@tonic-gate } else { 34507c478bd9Sstevel@tonic-gate int count = 0; 34517c478bd9Sstevel@tonic-gate 34527c478bd9Sstevel@tonic-gate if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) { 34537c478bd9Sstevel@tonic-gate _kobj_printf(ops, 34547c478bd9Sstevel@tonic-gate "kobj_read: seek 0x%x failed\n", offset); 34557c478bd9Sstevel@tonic-gate return (-1); 34567c478bd9Sstevel@tonic-gate } 34577c478bd9Sstevel@tonic-gate 34587c478bd9Sstevel@tonic-gate count = kobj_boot_read((int)descr, buf, size); 34597c478bd9Sstevel@tonic-gate if (count < size) { 34607c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 34617c478bd9Sstevel@tonic-gate _kobj_printf(ops, 34627c478bd9Sstevel@tonic-gate "kobj_read: req %d bytes, ", size); 34637c478bd9Sstevel@tonic-gate _kobj_printf(ops, "got %d\n", count); 34647c478bd9Sstevel@tonic-gate } 34657c478bd9Sstevel@tonic-gate } 34667c478bd9Sstevel@tonic-gate return (count); 34677c478bd9Sstevel@tonic-gate } 34687c478bd9Sstevel@tonic-gate } 34697c478bd9Sstevel@tonic-gate 34707c478bd9Sstevel@tonic-gate void 34717c478bd9Sstevel@tonic-gate kobj_close(intptr_t descr) 34727c478bd9Sstevel@tonic-gate { 34737c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 34747c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_close: 0x%lx\n", descr); 34757c478bd9Sstevel@tonic-gate 34767c478bd9Sstevel@tonic-gate if (_modrootloaded) { 34777c478bd9Sstevel@tonic-gate struct vnode *vp = (struct vnode *)descr; 34787c478bd9Sstevel@tonic-gate (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED()); 34797c478bd9Sstevel@tonic-gate VN_RELE(vp); 34807c478bd9Sstevel@tonic-gate } else 34817c478bd9Sstevel@tonic-gate (void) kobj_boot_close((int)descr); 34827c478bd9Sstevel@tonic-gate } 34837c478bd9Sstevel@tonic-gate 3484ea8dc4b6Seschrock int 3485ea8dc4b6Seschrock kobj_fstat(intptr_t descr, struct bootstat *buf) 3486ea8dc4b6Seschrock { 3487ea8dc4b6Seschrock if (buf == NULL) 3488ea8dc4b6Seschrock return (-1); 3489ea8dc4b6Seschrock 3490ea8dc4b6Seschrock if (_modrootloaded) { 3491ea8dc4b6Seschrock vattr_t vattr; 3492ea8dc4b6Seschrock struct vnode *vp = (struct vnode *)descr; 3493ea8dc4b6Seschrock if (VOP_GETATTR(vp, &vattr, 0, kcred) != 0) 3494ea8dc4b6Seschrock return (-1); 3495ea8dc4b6Seschrock 3496ea8dc4b6Seschrock /* 3497ea8dc4b6Seschrock * The vattr and bootstat structures are similar, but not 3498ea8dc4b6Seschrock * identical. We do our best to fill in the bootstat structure 3499ea8dc4b6Seschrock * from the contents of vattr (transfering only the ones that 3500ea8dc4b6Seschrock * are obvious. 3501ea8dc4b6Seschrock */ 3502ea8dc4b6Seschrock 3503ea8dc4b6Seschrock buf->st_mode = (uint32_t)vattr.va_mode; 3504ea8dc4b6Seschrock buf->st_nlink = (uint32_t)vattr.va_nlink; 3505ea8dc4b6Seschrock buf->st_uid = (int32_t)vattr.va_uid; 3506ea8dc4b6Seschrock buf->st_gid = (int32_t)vattr.va_gid; 3507ea8dc4b6Seschrock buf->st_rdev = (uint64_t)vattr.va_rdev; 3508ea8dc4b6Seschrock buf->st_size = (uint64_t)vattr.va_size; 3509ea8dc4b6Seschrock buf->st_atim.tv_sec = (int64_t)vattr.va_atime.tv_sec; 3510ea8dc4b6Seschrock buf->st_atim.tv_nsec = (int64_t)vattr.va_atime.tv_nsec; 3511ea8dc4b6Seschrock buf->st_mtim.tv_sec = (int64_t)vattr.va_mtime.tv_sec; 3512ea8dc4b6Seschrock buf->st_mtim.tv_nsec = (int64_t)vattr.va_mtime.tv_nsec; 3513ea8dc4b6Seschrock buf->st_ctim.tv_sec = (int64_t)vattr.va_ctime.tv_sec; 3514ea8dc4b6Seschrock buf->st_ctim.tv_nsec = (int64_t)vattr.va_ctime.tv_nsec; 3515ea8dc4b6Seschrock buf->st_blksize = (int32_t)vattr.va_blksize; 3516ea8dc4b6Seschrock buf->st_blocks = (int64_t)vattr.va_nblocks; 3517ea8dc4b6Seschrock 3518ea8dc4b6Seschrock return (0); 3519ea8dc4b6Seschrock } 3520ea8dc4b6Seschrock 3521ea8dc4b6Seschrock return (kobj_boot_fstat((int)descr, buf)); 3522ea8dc4b6Seschrock } 3523ea8dc4b6Seschrock 3524ea8dc4b6Seschrock 35257c478bd9Sstevel@tonic-gate struct _buf * 35267c478bd9Sstevel@tonic-gate kobj_open_file(char *name) 35277c478bd9Sstevel@tonic-gate { 35287c478bd9Sstevel@tonic-gate struct _buf *file; 35297c478bd9Sstevel@tonic-gate intptr_t fd; 35307c478bd9Sstevel@tonic-gate 35317c478bd9Sstevel@tonic-gate if ((fd = kobj_open(name)) == -1) { 35327c478bd9Sstevel@tonic-gate return ((struct _buf *)-1); 35337c478bd9Sstevel@tonic-gate } 35347c478bd9Sstevel@tonic-gate 35357c478bd9Sstevel@tonic-gate file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP); 35367c478bd9Sstevel@tonic-gate file->_fd = fd; 35377c478bd9Sstevel@tonic-gate file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP); 35387c478bd9Sstevel@tonic-gate file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT|KM_TMP); 35397c478bd9Sstevel@tonic-gate file->_cnt = file->_size = file->_off = 0; 35407c478bd9Sstevel@tonic-gate file->_ln = 1; 35417c478bd9Sstevel@tonic-gate file->_ptr = file->_base; 35427c478bd9Sstevel@tonic-gate (void) strcpy(file->_name, name); 35437c478bd9Sstevel@tonic-gate return (file); 35447c478bd9Sstevel@tonic-gate } 35457c478bd9Sstevel@tonic-gate 35467c478bd9Sstevel@tonic-gate void 35477c478bd9Sstevel@tonic-gate kobj_close_file(struct _buf *file) 35487c478bd9Sstevel@tonic-gate { 35497c478bd9Sstevel@tonic-gate kobj_close(file->_fd); 35507c478bd9Sstevel@tonic-gate kobj_free(file->_base, MAXBSIZE); 35517c478bd9Sstevel@tonic-gate kobj_free(file->_name, strlen(file->_name)+1); 35527c478bd9Sstevel@tonic-gate kobj_free(file, sizeof (struct _buf)); 35537c478bd9Sstevel@tonic-gate } 35547c478bd9Sstevel@tonic-gate 35557c478bd9Sstevel@tonic-gate int 35567c478bd9Sstevel@tonic-gate kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) 35577c478bd9Sstevel@tonic-gate { 35587c478bd9Sstevel@tonic-gate int b_size, c_size; 35597c478bd9Sstevel@tonic-gate int b_off; /* Offset into buffer for start of bcopy */ 35607c478bd9Sstevel@tonic-gate int count = 0; 35617c478bd9Sstevel@tonic-gate int page_addr; 35627c478bd9Sstevel@tonic-gate 35637c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) { 35647c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_read_file: size=%x,", size); 35657c478bd9Sstevel@tonic-gate _kobj_printf(ops, " offset=%x at", off); 35667c478bd9Sstevel@tonic-gate _kobj_printf(ops, " buf=%x\n", buf); 35677c478bd9Sstevel@tonic-gate } 35687c478bd9Sstevel@tonic-gate 35697c478bd9Sstevel@tonic-gate while (size) { 35707c478bd9Sstevel@tonic-gate page_addr = F_PAGE(off); 35717c478bd9Sstevel@tonic-gate b_size = file->_size; 35727c478bd9Sstevel@tonic-gate /* 35737c478bd9Sstevel@tonic-gate * If we have the filesystem page the caller's referring to 35747c478bd9Sstevel@tonic-gate * and we have something in the buffer, 35757c478bd9Sstevel@tonic-gate * satisfy as much of the request from the buffer as we can. 35767c478bd9Sstevel@tonic-gate */ 35777c478bd9Sstevel@tonic-gate if (page_addr == file->_off && b_size > 0) { 35787c478bd9Sstevel@tonic-gate b_off = B_OFFSET(off); 35797c478bd9Sstevel@tonic-gate c_size = b_size - b_off; 35807c478bd9Sstevel@tonic-gate /* 35817c478bd9Sstevel@tonic-gate * If there's nothing to copy, we're at EOF. 35827c478bd9Sstevel@tonic-gate */ 35837c478bd9Sstevel@tonic-gate if (c_size <= 0) 35847c478bd9Sstevel@tonic-gate break; 35857c478bd9Sstevel@tonic-gate if (c_size > size) 35867c478bd9Sstevel@tonic-gate c_size = size; 35877c478bd9Sstevel@tonic-gate if (buf) { 35887c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 35897c478bd9Sstevel@tonic-gate _kobj_printf(ops, "copying %x bytes\n", 35907c478bd9Sstevel@tonic-gate c_size); 35917c478bd9Sstevel@tonic-gate bcopy(file->_base+b_off, buf, c_size); 35927c478bd9Sstevel@tonic-gate size -= c_size; 35937c478bd9Sstevel@tonic-gate off += c_size; 35947c478bd9Sstevel@tonic-gate buf += c_size; 35957c478bd9Sstevel@tonic-gate count += c_size; 35967c478bd9Sstevel@tonic-gate } else { 35977c478bd9Sstevel@tonic-gate _kobj_printf(ops, "kobj_read: system error"); 35987c478bd9Sstevel@tonic-gate count = -1; 35997c478bd9Sstevel@tonic-gate break; 36007c478bd9Sstevel@tonic-gate } 36017c478bd9Sstevel@tonic-gate } else { 36027c478bd9Sstevel@tonic-gate /* 36037c478bd9Sstevel@tonic-gate * If the caller's offset is page aligned and 36047c478bd9Sstevel@tonic-gate * the caller want's at least a filesystem page and 36057c478bd9Sstevel@tonic-gate * the caller provided a buffer, 36067c478bd9Sstevel@tonic-gate * read directly into the caller's buffer. 36077c478bd9Sstevel@tonic-gate */ 36087c478bd9Sstevel@tonic-gate if (page_addr == off && 36097c478bd9Sstevel@tonic-gate (c_size = F_PAGE(size)) && buf) { 36107c478bd9Sstevel@tonic-gate c_size = kobj_read(file->_fd, buf, c_size, 36117c478bd9Sstevel@tonic-gate page_addr); 36127c478bd9Sstevel@tonic-gate if (c_size < 0) { 36137c478bd9Sstevel@tonic-gate count = -1; 36147c478bd9Sstevel@tonic-gate break; 36157c478bd9Sstevel@tonic-gate } 36167c478bd9Sstevel@tonic-gate count += c_size; 36177c478bd9Sstevel@tonic-gate if (c_size != F_PAGE(size)) 36187c478bd9Sstevel@tonic-gate break; 36197c478bd9Sstevel@tonic-gate size -= c_size; 36207c478bd9Sstevel@tonic-gate off += c_size; 36217c478bd9Sstevel@tonic-gate buf += c_size; 36227c478bd9Sstevel@tonic-gate /* 36237c478bd9Sstevel@tonic-gate * Otherwise, read into our buffer and copy next time 36247c478bd9Sstevel@tonic-gate * around the loop. 36257c478bd9Sstevel@tonic-gate */ 36267c478bd9Sstevel@tonic-gate } else { 36277c478bd9Sstevel@tonic-gate file->_off = page_addr; 36287c478bd9Sstevel@tonic-gate c_size = kobj_read(file->_fd, file->_base, 36297c478bd9Sstevel@tonic-gate MAXBSIZE, page_addr); 36307c478bd9Sstevel@tonic-gate file->_ptr = file->_base; 36317c478bd9Sstevel@tonic-gate file->_cnt = c_size; 36327c478bd9Sstevel@tonic-gate file->_size = c_size; 36337c478bd9Sstevel@tonic-gate /* 36347c478bd9Sstevel@tonic-gate * If a _filbuf call or nothing read, break. 36357c478bd9Sstevel@tonic-gate */ 36367c478bd9Sstevel@tonic-gate if (buf == NULL || c_size <= 0) { 36377c478bd9Sstevel@tonic-gate count = c_size; 36387c478bd9Sstevel@tonic-gate break; 36397c478bd9Sstevel@tonic-gate } 36407c478bd9Sstevel@tonic-gate } 36417c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 36427c478bd9Sstevel@tonic-gate _kobj_printf(ops, "read %x bytes\n", c_size); 36437c478bd9Sstevel@tonic-gate } 36447c478bd9Sstevel@tonic-gate } 36457c478bd9Sstevel@tonic-gate if (_moddebug & MODDEBUG_ERRMSG) 36467c478bd9Sstevel@tonic-gate _kobj_printf(ops, "count = %x\n", count); 36477c478bd9Sstevel@tonic-gate 36487c478bd9Sstevel@tonic-gate return (count); 36497c478bd9Sstevel@tonic-gate } 36507c478bd9Sstevel@tonic-gate 36517c478bd9Sstevel@tonic-gate int 36527c478bd9Sstevel@tonic-gate kobj_filbuf(struct _buf *f) 36537c478bd9Sstevel@tonic-gate { 36547c478bd9Sstevel@tonic-gate if (kobj_read_file(f, NULL, MAXBSIZE, f->_off + f->_size) > 0) 36557c478bd9Sstevel@tonic-gate return (kobj_getc(f)); 36567c478bd9Sstevel@tonic-gate return (-1); 36577c478bd9Sstevel@tonic-gate } 36587c478bd9Sstevel@tonic-gate 36597c478bd9Sstevel@tonic-gate void 36607c478bd9Sstevel@tonic-gate kobj_free(void *address, size_t size) 36617c478bd9Sstevel@tonic-gate { 36627c478bd9Sstevel@tonic-gate if (standalone) 36637c478bd9Sstevel@tonic-gate return; 36647c478bd9Sstevel@tonic-gate 36657c478bd9Sstevel@tonic-gate kmem_free(address, size); 36667c478bd9Sstevel@tonic-gate kobj_stat.nfree_calls++; 36677c478bd9Sstevel@tonic-gate kobj_stat.nfree += size; 36687c478bd9Sstevel@tonic-gate } 36697c478bd9Sstevel@tonic-gate 36707c478bd9Sstevel@tonic-gate void * 36717c478bd9Sstevel@tonic-gate kobj_zalloc(size_t size, int flag) 36727c478bd9Sstevel@tonic-gate { 36737c478bd9Sstevel@tonic-gate void *v; 36747c478bd9Sstevel@tonic-gate 36757c478bd9Sstevel@tonic-gate if ((v = kobj_alloc(size, flag)) != 0) { 36767c478bd9Sstevel@tonic-gate bzero(v, size); 36777c478bd9Sstevel@tonic-gate } 36787c478bd9Sstevel@tonic-gate 36797c478bd9Sstevel@tonic-gate return (v); 36807c478bd9Sstevel@tonic-gate } 36817c478bd9Sstevel@tonic-gate 36827c478bd9Sstevel@tonic-gate void * 36837c478bd9Sstevel@tonic-gate kobj_alloc(size_t size, int flag) 36847c478bd9Sstevel@tonic-gate { 36857c478bd9Sstevel@tonic-gate /* 36867c478bd9Sstevel@tonic-gate * If we are running standalone in the 36877c478bd9Sstevel@tonic-gate * linker, we ask boot for memory. 36887c478bd9Sstevel@tonic-gate * Either it's temporary memory that we lose 36897c478bd9Sstevel@tonic-gate * once boot is mapped out or we allocate it 36907c478bd9Sstevel@tonic-gate * permanently using the dynamic data segment. 36917c478bd9Sstevel@tonic-gate */ 36927c478bd9Sstevel@tonic-gate if (standalone) { 36937c478bd9Sstevel@tonic-gate #ifdef __sparc 36947c478bd9Sstevel@tonic-gate if (flag & KM_TMP) { 36957c478bd9Sstevel@tonic-gate return (kobj_tmp_alloc(size)); 36967c478bd9Sstevel@tonic-gate } else if (flag & KM_SCRATCH) { 36977c478bd9Sstevel@tonic-gate void *buf = kobj_bs_alloc(size); 36987c478bd9Sstevel@tonic-gate 36997c478bd9Sstevel@tonic-gate if (buf != NULL) 37007c478bd9Sstevel@tonic-gate return (buf); 37017c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG 37027c478bd9Sstevel@tonic-gate if (kobj_debug & D_DEBUG) { 37037c478bd9Sstevel@tonic-gate _kobj_printf(ops, "krtld: failed scratch alloc " 37047c478bd9Sstevel@tonic-gate "of %u bytes -- falling back\n", size); 37057c478bd9Sstevel@tonic-gate } 37067c478bd9Sstevel@tonic-gate #endif 37077c478bd9Sstevel@tonic-gate } 37087c478bd9Sstevel@tonic-gate 37097c478bd9Sstevel@tonic-gate #else /* x86 */ 37107c478bd9Sstevel@tonic-gate if (flag & (KM_TMP | KM_SCRATCH)) 37117c478bd9Sstevel@tonic-gate return (BOP_ALLOC(ops, 0, size, MINALIGN)); 37127c478bd9Sstevel@tonic-gate #endif 37137c478bd9Sstevel@tonic-gate return (kobj_segbrk(&_edata, size, MINALIGN, 0)); 37147c478bd9Sstevel@tonic-gate } 37157c478bd9Sstevel@tonic-gate 37167c478bd9Sstevel@tonic-gate kobj_stat.nalloc_calls++; 37177c478bd9Sstevel@tonic-gate kobj_stat.nalloc += size; 37187c478bd9Sstevel@tonic-gate 37197c478bd9Sstevel@tonic-gate return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)); 37207c478bd9Sstevel@tonic-gate } 37217c478bd9Sstevel@tonic-gate 37227c478bd9Sstevel@tonic-gate /* 37237c478bd9Sstevel@tonic-gate * Allow the "mod" system to sync up with the work 37247c478bd9Sstevel@tonic-gate * already done by kobj during the initial loading 37257c478bd9Sstevel@tonic-gate * of the kernel. This also gives us a chance 37267c478bd9Sstevel@tonic-gate * to reallocate memory that belongs to boot. 37277c478bd9Sstevel@tonic-gate */ 37287c478bd9Sstevel@tonic-gate void 37297c478bd9Sstevel@tonic-gate kobj_sync(void) 37307c478bd9Sstevel@tonic-gate { 37317c478bd9Sstevel@tonic-gate struct modctl_list *lp, **lpp; 37327c478bd9Sstevel@tonic-gate 37337c478bd9Sstevel@tonic-gate extern char *default_path; 37347c478bd9Sstevel@tonic-gate 37357c478bd9Sstevel@tonic-gate /* 37367c478bd9Sstevel@tonic-gate * module_path can be set in /etc/system 37377c478bd9Sstevel@tonic-gate */ 37387c478bd9Sstevel@tonic-gate if (default_path != NULL) 37397c478bd9Sstevel@tonic-gate module_path = default_path; 37407c478bd9Sstevel@tonic-gate else 37417c478bd9Sstevel@tonic-gate default_path = module_path; 37427c478bd9Sstevel@tonic-gate 37437c478bd9Sstevel@tonic-gate ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t), 37447c478bd9Sstevel@tonic-gate segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 37457c478bd9Sstevel@tonic-gate 37467c478bd9Sstevel@tonic-gate ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t), 37477c478bd9Sstevel@tonic-gate segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 37487c478bd9Sstevel@tonic-gate 37497c478bd9Sstevel@tonic-gate /* 37507c478bd9Sstevel@tonic-gate * Move symbol tables from boot memory to ksyms_arena. 37517c478bd9Sstevel@tonic-gate */ 37527c478bd9Sstevel@tonic-gate for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) { 37537c478bd9Sstevel@tonic-gate for (lp = *lpp; lp != NULL; lp = lp->modl_next) 37547c478bd9Sstevel@tonic-gate kobj_export_module(mod(lp)); 37557c478bd9Sstevel@tonic-gate } 37567c478bd9Sstevel@tonic-gate } 37577c478bd9Sstevel@tonic-gate 37587c478bd9Sstevel@tonic-gate caddr_t 37597c478bd9Sstevel@tonic-gate kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit) 37607c478bd9Sstevel@tonic-gate { 37617c478bd9Sstevel@tonic-gate uintptr_t va, pva; 37627c478bd9Sstevel@tonic-gate size_t alloc_pgsz = kobj_mmu_pagesize; 37637c478bd9Sstevel@tonic-gate size_t alloc_align = BO_NO_ALIGN; 37647c478bd9Sstevel@tonic-gate size_t alloc_size; 37657c478bd9Sstevel@tonic-gate 37667c478bd9Sstevel@tonic-gate /* 37677c478bd9Sstevel@tonic-gate * If we are using "large" mappings for the kernel, 37687c478bd9Sstevel@tonic-gate * request aligned memory from boot using the 37697c478bd9Sstevel@tonic-gate * "large" pagesize. 37707c478bd9Sstevel@tonic-gate */ 37717c478bd9Sstevel@tonic-gate if (lg_pagesize) { 37727c478bd9Sstevel@tonic-gate alloc_align = lg_pagesize; 37737c478bd9Sstevel@tonic-gate alloc_pgsz = lg_pagesize; 37747c478bd9Sstevel@tonic-gate } 37757c478bd9Sstevel@tonic-gate va = ALIGN((uintptr_t)*spp, align); 37767c478bd9Sstevel@tonic-gate pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz); 37777c478bd9Sstevel@tonic-gate /* 37787c478bd9Sstevel@tonic-gate * Need more pages? 37797c478bd9Sstevel@tonic-gate */ 37807c478bd9Sstevel@tonic-gate if (va + size > pva) { 37817c478bd9Sstevel@tonic-gate alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz); 37827c478bd9Sstevel@tonic-gate /* 37837c478bd9Sstevel@tonic-gate * Check for overlapping segments. 37847c478bd9Sstevel@tonic-gate */ 37857c478bd9Sstevel@tonic-gate if (limit && limit <= *spp + alloc_size) 37867c478bd9Sstevel@tonic-gate return ((caddr_t)0); 37877c478bd9Sstevel@tonic-gate 37887c478bd9Sstevel@tonic-gate pva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva, 37897c478bd9Sstevel@tonic-gate alloc_size, alloc_align); 37907c478bd9Sstevel@tonic-gate if (pva == NULL) { 37917c478bd9Sstevel@tonic-gate _kobj_printf(ops, "BOP_ALLOC refused, 0x%x bytes ", 37927c478bd9Sstevel@tonic-gate alloc_size); 37937c478bd9Sstevel@tonic-gate _kobj_printf(ops, " at 0x%lx\n", pva); 37947c478bd9Sstevel@tonic-gate } 37957c478bd9Sstevel@tonic-gate } 37967c478bd9Sstevel@tonic-gate *spp = (caddr_t)(va + size); 37977c478bd9Sstevel@tonic-gate 37987c478bd9Sstevel@tonic-gate return ((caddr_t)va); 37997c478bd9Sstevel@tonic-gate } 38007c478bd9Sstevel@tonic-gate 38017c478bd9Sstevel@tonic-gate /* 38027c478bd9Sstevel@tonic-gate * Calculate the number of output hash buckets. 38037c478bd9Sstevel@tonic-gate * We use the next prime larger than n / 4, 38047c478bd9Sstevel@tonic-gate * so the average hash chain is about 4 entries. 38057c478bd9Sstevel@tonic-gate * More buckets would just be a waste of memory. 38067c478bd9Sstevel@tonic-gate */ 38077c478bd9Sstevel@tonic-gate uint_t 38087c478bd9Sstevel@tonic-gate kobj_gethashsize(uint_t n) 38097c478bd9Sstevel@tonic-gate { 38107c478bd9Sstevel@tonic-gate int f; 38117c478bd9Sstevel@tonic-gate int hsize = MAX(n / 4, 2); 38127c478bd9Sstevel@tonic-gate 38137c478bd9Sstevel@tonic-gate for (f = 2; f * f <= hsize; f++) 38147c478bd9Sstevel@tonic-gate if (hsize % f == 0) 38157c478bd9Sstevel@tonic-gate hsize += f = 1; 38167c478bd9Sstevel@tonic-gate 38177c478bd9Sstevel@tonic-gate return (hsize); 38187c478bd9Sstevel@tonic-gate } 38197c478bd9Sstevel@tonic-gate 38207c478bd9Sstevel@tonic-gate static char * 38217c478bd9Sstevel@tonic-gate basename(char *s) 38227c478bd9Sstevel@tonic-gate { 38237c478bd9Sstevel@tonic-gate char *p, *q; 38247c478bd9Sstevel@tonic-gate 38257c478bd9Sstevel@tonic-gate q = NULL; 38267c478bd9Sstevel@tonic-gate p = s; 38277c478bd9Sstevel@tonic-gate do { 38287c478bd9Sstevel@tonic-gate if (*p == '/') 38297c478bd9Sstevel@tonic-gate q = p; 38307c478bd9Sstevel@tonic-gate } while (*p++); 38317c478bd9Sstevel@tonic-gate return (q ? q + 1 : s); 38327c478bd9Sstevel@tonic-gate } 38337c478bd9Sstevel@tonic-gate 38347c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 38357c478bd9Sstevel@tonic-gate static void 38367c478bd9Sstevel@tonic-gate kprintf(void *op, const char *fmt, ...) 38377c478bd9Sstevel@tonic-gate { 38387c478bd9Sstevel@tonic-gate va_list adx; 38397c478bd9Sstevel@tonic-gate 38407c478bd9Sstevel@tonic-gate va_start(adx, fmt); 38417c478bd9Sstevel@tonic-gate vprintf(fmt, adx); 38427c478bd9Sstevel@tonic-gate va_end(adx); 38437c478bd9Sstevel@tonic-gate } 38447c478bd9Sstevel@tonic-gate 38457c478bd9Sstevel@tonic-gate void 38467c478bd9Sstevel@tonic-gate kobj_stat_get(kobj_stat_t *kp) 38477c478bd9Sstevel@tonic-gate { 38487c478bd9Sstevel@tonic-gate *kp = kobj_stat; 38497c478bd9Sstevel@tonic-gate } 38507c478bd9Sstevel@tonic-gate 38517c478bd9Sstevel@tonic-gate int 38527c478bd9Sstevel@tonic-gate kobj_getpagesize() 38537c478bd9Sstevel@tonic-gate { 38547c478bd9Sstevel@tonic-gate return (lg_pagesize); 38557c478bd9Sstevel@tonic-gate } 38567c478bd9Sstevel@tonic-gate 38577c478bd9Sstevel@tonic-gate void 38587c478bd9Sstevel@tonic-gate kobj_textwin_alloc(struct module *mp) 38597c478bd9Sstevel@tonic-gate { 38607c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mod_lock)); 38617c478bd9Sstevel@tonic-gate 38627c478bd9Sstevel@tonic-gate if (mp->textwin != NULL) 38637c478bd9Sstevel@tonic-gate return; 38647c478bd9Sstevel@tonic-gate 38657c478bd9Sstevel@tonic-gate /* 38667c478bd9Sstevel@tonic-gate * If the text is not contained in the heap, then it is not contained 38677c478bd9Sstevel@tonic-gate * by a writable mapping. (Specifically, it's on the nucleus page.) 38687c478bd9Sstevel@tonic-gate * We allocate a read/write mapping for this module's text to allow 38697c478bd9Sstevel@tonic-gate * the text to be patched without calling hot_patch_kernel_text() 38707c478bd9Sstevel@tonic-gate * (which is quite slow). 38717c478bd9Sstevel@tonic-gate */ 38727c478bd9Sstevel@tonic-gate if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) { 38737c478bd9Sstevel@tonic-gate uintptr_t text = (uintptr_t)mp->text; 38747c478bd9Sstevel@tonic-gate uintptr_t size = (uintptr_t)mp->text_size; 38757c478bd9Sstevel@tonic-gate uintptr_t i; 38767c478bd9Sstevel@tonic-gate caddr_t va; 38777c478bd9Sstevel@tonic-gate size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) - 38787c478bd9Sstevel@tonic-gate (text & PAGEMASK); 38797c478bd9Sstevel@tonic-gate 38807c478bd9Sstevel@tonic-gate va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP); 38817c478bd9Sstevel@tonic-gate 38827c478bd9Sstevel@tonic-gate for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) { 38837c478bd9Sstevel@tonic-gate hat_devload(kas.a_hat, va, PAGESIZE, 38847c478bd9Sstevel@tonic-gate hat_getpfnum(kas.a_hat, (caddr_t)i), 38857c478bd9Sstevel@tonic-gate PROT_READ | PROT_WRITE, 38867c478bd9Sstevel@tonic-gate HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST); 38877c478bd9Sstevel@tonic-gate va += PAGESIZE; 38887c478bd9Sstevel@tonic-gate } 38897c478bd9Sstevel@tonic-gate 38907c478bd9Sstevel@tonic-gate mp->textwin = mp->textwin_base + (text & PAGEOFFSET); 38917c478bd9Sstevel@tonic-gate } else { 38927c478bd9Sstevel@tonic-gate mp->textwin = mp->text; 38937c478bd9Sstevel@tonic-gate } 38947c478bd9Sstevel@tonic-gate } 38957c478bd9Sstevel@tonic-gate 38967c478bd9Sstevel@tonic-gate void 38977c478bd9Sstevel@tonic-gate kobj_textwin_free(struct module *mp) 38987c478bd9Sstevel@tonic-gate { 38997c478bd9Sstevel@tonic-gate uintptr_t text = (uintptr_t)mp->text; 39007c478bd9Sstevel@tonic-gate uintptr_t tsize = (uintptr_t)mp->text_size; 39017c478bd9Sstevel@tonic-gate size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) - 39027c478bd9Sstevel@tonic-gate (text & PAGEMASK)); 39037c478bd9Sstevel@tonic-gate 39047c478bd9Sstevel@tonic-gate mp->textwin = NULL; 39057c478bd9Sstevel@tonic-gate 39067c478bd9Sstevel@tonic-gate if (mp->textwin_base == NULL) 39077c478bd9Sstevel@tonic-gate return; 39087c478bd9Sstevel@tonic-gate 39097c478bd9Sstevel@tonic-gate hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK); 39107c478bd9Sstevel@tonic-gate vmem_free(heap_arena, mp->textwin_base, size); 39117c478bd9Sstevel@tonic-gate mp->textwin_base = NULL; 39127c478bd9Sstevel@tonic-gate } 39137c478bd9Sstevel@tonic-gate 39147c478bd9Sstevel@tonic-gate static char * 39157c478bd9Sstevel@tonic-gate find_libmacro(char *name) 39167c478bd9Sstevel@tonic-gate { 39177c478bd9Sstevel@tonic-gate int lmi; 39187c478bd9Sstevel@tonic-gate 39197c478bd9Sstevel@tonic-gate for (lmi = 0; lmi < NLIBMACROS; lmi++) { 39207c478bd9Sstevel@tonic-gate if (strcmp(name, libmacros[lmi].lmi_macroname) == 0) 39217c478bd9Sstevel@tonic-gate return (libmacros[lmi].lmi_list); 39227c478bd9Sstevel@tonic-gate } 39237c478bd9Sstevel@tonic-gate return (NULL); 39247c478bd9Sstevel@tonic-gate } 39257c478bd9Sstevel@tonic-gate 39267c478bd9Sstevel@tonic-gate /* 39277c478bd9Sstevel@tonic-gate * Check for $MACRO in tail (string to expand) and expand it in path at pathend 39287c478bd9Sstevel@tonic-gate * returns path if successful, else NULL 39297c478bd9Sstevel@tonic-gate * Support multiple $MACROs expansion and the first valid path will be returned 39307c478bd9Sstevel@tonic-gate * Caller's responsibility to provide enough space in path to expand 39317c478bd9Sstevel@tonic-gate */ 39327c478bd9Sstevel@tonic-gate char * 39337c478bd9Sstevel@tonic-gate expand_libmacro(char *tail, char *path, char *pathend) 39347c478bd9Sstevel@tonic-gate { 39357c478bd9Sstevel@tonic-gate char c, *p, *p1, *p2, *path2, *endp; 39367c478bd9Sstevel@tonic-gate int diff, lmi, macrolen, valid_macro, more_macro; 39377c478bd9Sstevel@tonic-gate struct _buf *file; 39387c478bd9Sstevel@tonic-gate 39397c478bd9Sstevel@tonic-gate /* 39407c478bd9Sstevel@tonic-gate * check for $MACROS between nulls or slashes 39417c478bd9Sstevel@tonic-gate */ 39427c478bd9Sstevel@tonic-gate p = strchr(tail, '$'); 39437c478bd9Sstevel@tonic-gate if (p == NULL) 39447c478bd9Sstevel@tonic-gate return (NULL); 39457c478bd9Sstevel@tonic-gate for (lmi = 0; lmi < NLIBMACROS; lmi++) { 39467c478bd9Sstevel@tonic-gate macrolen = libmacros[lmi].lmi_macrolen; 39477c478bd9Sstevel@tonic-gate if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0) 39487c478bd9Sstevel@tonic-gate break; 39497c478bd9Sstevel@tonic-gate } 39507c478bd9Sstevel@tonic-gate 39517c478bd9Sstevel@tonic-gate valid_macro = 0; 39527c478bd9Sstevel@tonic-gate if (lmi < NLIBMACROS) { 39537c478bd9Sstevel@tonic-gate /* 39547c478bd9Sstevel@tonic-gate * The following checks are used to restrict expansion of 39557c478bd9Sstevel@tonic-gate * macros to those that form a full directory/file name 39567c478bd9Sstevel@tonic-gate * and to keep the behavior same as before. If this 39577c478bd9Sstevel@tonic-gate * restriction is removed or no longer valid in the future, 39587c478bd9Sstevel@tonic-gate * the checks below can be deleted. 39597c478bd9Sstevel@tonic-gate */ 39607c478bd9Sstevel@tonic-gate if ((p == tail) || (*(p - 1) == '/')) { 39617c478bd9Sstevel@tonic-gate c = *(p + macrolen + 1); 39627c478bd9Sstevel@tonic-gate if (c == '/' || c == '\0') 39637c478bd9Sstevel@tonic-gate valid_macro = 1; 39647c478bd9Sstevel@tonic-gate } 39657c478bd9Sstevel@tonic-gate } 39667c478bd9Sstevel@tonic-gate 39677c478bd9Sstevel@tonic-gate if (!valid_macro) { 39687c478bd9Sstevel@tonic-gate p2 = strchr(p, '/'); 39697c478bd9Sstevel@tonic-gate /* 39707c478bd9Sstevel@tonic-gate * if no more macro to expand, then just copy whatever left 39717c478bd9Sstevel@tonic-gate * and check whether it exists 39727c478bd9Sstevel@tonic-gate */ 39737c478bd9Sstevel@tonic-gate if (p2 == NULL || strchr(p2, '$') == NULL) { 39747c478bd9Sstevel@tonic-gate (void) strcpy(pathend, tail); 39757c478bd9Sstevel@tonic-gate if ((file = kobj_open_path(path, 1, 1)) != 39767c478bd9Sstevel@tonic-gate (struct _buf *)-1) { 39777c478bd9Sstevel@tonic-gate kobj_close_file(file); 39787c478bd9Sstevel@tonic-gate return (path); 39797c478bd9Sstevel@tonic-gate } else 39807c478bd9Sstevel@tonic-gate return (NULL); 39817c478bd9Sstevel@tonic-gate } else { 39827c478bd9Sstevel@tonic-gate /* 39837c478bd9Sstevel@tonic-gate * copy all chars before '/' and call expand_libmacro() 39847c478bd9Sstevel@tonic-gate * again 39857c478bd9Sstevel@tonic-gate */ 39867c478bd9Sstevel@tonic-gate diff = p2 - tail; 39877c478bd9Sstevel@tonic-gate bcopy(tail, pathend, diff); 39887c478bd9Sstevel@tonic-gate pathend += diff; 39897c478bd9Sstevel@tonic-gate *(pathend) = '\0'; 39907c478bd9Sstevel@tonic-gate return (expand_libmacro(p2, path, pathend)); 39917c478bd9Sstevel@tonic-gate } 39927c478bd9Sstevel@tonic-gate } 39937c478bd9Sstevel@tonic-gate 39947c478bd9Sstevel@tonic-gate more_macro = 0; 39957c478bd9Sstevel@tonic-gate if (c != '\0') { 39967c478bd9Sstevel@tonic-gate endp = p + macrolen + 1; 39977c478bd9Sstevel@tonic-gate if (strchr(endp, '$') != NULL) 39987c478bd9Sstevel@tonic-gate more_macro = 1; 39997c478bd9Sstevel@tonic-gate } else 40007c478bd9Sstevel@tonic-gate endp = NULL; 40017c478bd9Sstevel@tonic-gate 40027c478bd9Sstevel@tonic-gate /* 40037c478bd9Sstevel@tonic-gate * copy lmi_list and split it into components. 40047c478bd9Sstevel@tonic-gate * then put the part of tail before $MACRO into path 40057c478bd9Sstevel@tonic-gate * at pathend 40067c478bd9Sstevel@tonic-gate */ 40077c478bd9Sstevel@tonic-gate diff = p - tail; 40087c478bd9Sstevel@tonic-gate if (diff > 0) 40097c478bd9Sstevel@tonic-gate bcopy(tail, pathend, diff); 40107c478bd9Sstevel@tonic-gate path2 = pathend + diff; 40117c478bd9Sstevel@tonic-gate p1 = libmacros[lmi].lmi_list; 40127c478bd9Sstevel@tonic-gate while (p1 && (*p1 != '\0')) { 40137c478bd9Sstevel@tonic-gate p2 = strchr(p1, ':'); 40147c478bd9Sstevel@tonic-gate if (p2) { 40157c478bd9Sstevel@tonic-gate diff = p2 - p1; 40167c478bd9Sstevel@tonic-gate bcopy(p1, path2, diff); 40177c478bd9Sstevel@tonic-gate *(path2 + diff) = '\0'; 40187c478bd9Sstevel@tonic-gate } else { 40197c478bd9Sstevel@tonic-gate diff = strlen(p1); 40207c478bd9Sstevel@tonic-gate bcopy(p1, path2, diff + 1); 40217c478bd9Sstevel@tonic-gate } 40227c478bd9Sstevel@tonic-gate /* copy endp only if there isn't any more macro to expand */ 40237c478bd9Sstevel@tonic-gate if (!more_macro && (endp != NULL)) 40247c478bd9Sstevel@tonic-gate (void) strcat(path2, endp); 40257c478bd9Sstevel@tonic-gate file = kobj_open_path(path, 1, 1); 40267c478bd9Sstevel@tonic-gate if (file != (struct _buf *)-1) { 40277c478bd9Sstevel@tonic-gate kobj_close_file(file); 40287c478bd9Sstevel@tonic-gate /* 40297c478bd9Sstevel@tonic-gate * if more macros to expand then call expand_libmacro(), 40307c478bd9Sstevel@tonic-gate * else return path which has the whole path 40317c478bd9Sstevel@tonic-gate */ 40327c478bd9Sstevel@tonic-gate if (!more_macro || (expand_libmacro(endp, path, 40337c478bd9Sstevel@tonic-gate path2 + diff) != NULL)) { 40347c478bd9Sstevel@tonic-gate return (path); 40357c478bd9Sstevel@tonic-gate } 40367c478bd9Sstevel@tonic-gate } 40377c478bd9Sstevel@tonic-gate if (p2) 40387c478bd9Sstevel@tonic-gate p1 = ++p2; 40397c478bd9Sstevel@tonic-gate else 40407c478bd9Sstevel@tonic-gate return (NULL); 40417c478bd9Sstevel@tonic-gate } 40427c478bd9Sstevel@tonic-gate return (NULL); 40437c478bd9Sstevel@tonic-gate } 40447c478bd9Sstevel@tonic-gate 40457c478bd9Sstevel@tonic-gate static void 40467c478bd9Sstevel@tonic-gate tnf_add_notifyunload(kobj_notify_f *fp) 40477c478bd9Sstevel@tonic-gate { 40487c478bd9Sstevel@tonic-gate kobj_notify_list_t *entry; 40497c478bd9Sstevel@tonic-gate 40507c478bd9Sstevel@tonic-gate entry = kobj_alloc(sizeof (kobj_notify_list_t), KM_WAIT); 40517c478bd9Sstevel@tonic-gate entry->kn_type = KOBJ_NOTIFY_MODUNLOADING; 40527c478bd9Sstevel@tonic-gate entry->kn_func = fp; 40537c478bd9Sstevel@tonic-gate (void) kobj_notify_add(entry); 40547c478bd9Sstevel@tonic-gate } 40557c478bd9Sstevel@tonic-gate 40567c478bd9Sstevel@tonic-gate /* ARGSUSED */ 40577c478bd9Sstevel@tonic-gate static void 40587c478bd9Sstevel@tonic-gate tnf_unsplice_probes(unsigned int what, struct modctl *mod) 40597c478bd9Sstevel@tonic-gate { 40607c478bd9Sstevel@tonic-gate extern tnf_probe_control_t *__tnf_probe_list_head; 40617c478bd9Sstevel@tonic-gate extern tnf_tag_data_t *__tnf_tag_list_head; 40627c478bd9Sstevel@tonic-gate tnf_probe_control_t **p; 40637c478bd9Sstevel@tonic-gate tnf_tag_data_t **q; 40647c478bd9Sstevel@tonic-gate struct module *mp = mod->mod_mp; 40657c478bd9Sstevel@tonic-gate 40667c478bd9Sstevel@tonic-gate if (!(mp->flags & KOBJ_TNF_PROBE)) 40677c478bd9Sstevel@tonic-gate return; 40687c478bd9Sstevel@tonic-gate 40697c478bd9Sstevel@tonic-gate for (p = &__tnf_probe_list_head; *p; ) 40707c478bd9Sstevel@tonic-gate if (kobj_addrcheck(mp, (char *)*p) == 0) 40717c478bd9Sstevel@tonic-gate *p = (*p)->next; 40727c478bd9Sstevel@tonic-gate else 40737c478bd9Sstevel@tonic-gate p = &(*p)->next; 40747c478bd9Sstevel@tonic-gate 40757c478bd9Sstevel@tonic-gate for (q = &__tnf_tag_list_head; *q; ) 40767c478bd9Sstevel@tonic-gate if (kobj_addrcheck(mp, (char *)*q) == 0) 40777c478bd9Sstevel@tonic-gate *q = (tnf_tag_data_t *)(*q)->tag_version; 40787c478bd9Sstevel@tonic-gate else 40797c478bd9Sstevel@tonic-gate q = (tnf_tag_data_t **)&(*q)->tag_version; 40807c478bd9Sstevel@tonic-gate 40817c478bd9Sstevel@tonic-gate tnf_changed_probe_list = 1; 40827c478bd9Sstevel@tonic-gate } 40837c478bd9Sstevel@tonic-gate 40847c478bd9Sstevel@tonic-gate int 40857c478bd9Sstevel@tonic-gate tnf_splice_probes(int boot_load, tnf_probe_control_t *plist, 40867c478bd9Sstevel@tonic-gate tnf_tag_data_t *tlist) 40877c478bd9Sstevel@tonic-gate { 40887c478bd9Sstevel@tonic-gate int result = 0; 40897c478bd9Sstevel@tonic-gate static int add_notify = 1; 40907c478bd9Sstevel@tonic-gate 40917c478bd9Sstevel@tonic-gate if (plist) { 40927c478bd9Sstevel@tonic-gate tnf_probe_control_t *pl; 40937c478bd9Sstevel@tonic-gate 40947c478bd9Sstevel@tonic-gate for (pl = plist; pl->next; ) 40957c478bd9Sstevel@tonic-gate pl = pl->next; 40967c478bd9Sstevel@tonic-gate 40977c478bd9Sstevel@tonic-gate if (!boot_load) 40987c478bd9Sstevel@tonic-gate mutex_enter(&mod_lock); 40997c478bd9Sstevel@tonic-gate tnf_changed_probe_list = 1; 41007c478bd9Sstevel@tonic-gate pl->next = __tnf_probe_list_head; 41017c478bd9Sstevel@tonic-gate __tnf_probe_list_head = plist; 41027c478bd9Sstevel@tonic-gate if (!boot_load) 41037c478bd9Sstevel@tonic-gate mutex_exit(&mod_lock); 41047c478bd9Sstevel@tonic-gate result = 1; 41057c478bd9Sstevel@tonic-gate } 41067c478bd9Sstevel@tonic-gate 41077c478bd9Sstevel@tonic-gate if (tlist) { 41087c478bd9Sstevel@tonic-gate tnf_tag_data_t *tl; 41097c478bd9Sstevel@tonic-gate 41107c478bd9Sstevel@tonic-gate for (tl = tlist; tl->tag_version; ) 41117c478bd9Sstevel@tonic-gate tl = (tnf_tag_data_t *)tl->tag_version; 41127c478bd9Sstevel@tonic-gate 41137c478bd9Sstevel@tonic-gate if (!boot_load) 41147c478bd9Sstevel@tonic-gate mutex_enter(&mod_lock); 41157c478bd9Sstevel@tonic-gate tl->tag_version = (tnf_tag_version_t *)__tnf_tag_list_head; 41167c478bd9Sstevel@tonic-gate __tnf_tag_list_head = tlist; 41177c478bd9Sstevel@tonic-gate if (!boot_load) 41187c478bd9Sstevel@tonic-gate mutex_exit(&mod_lock); 41197c478bd9Sstevel@tonic-gate result = 1; 41207c478bd9Sstevel@tonic-gate } 41217c478bd9Sstevel@tonic-gate if (!boot_load && result && add_notify) { 41227c478bd9Sstevel@tonic-gate tnf_add_notifyunload(tnf_unsplice_probes); 41237c478bd9Sstevel@tonic-gate add_notify = 0; 41247c478bd9Sstevel@tonic-gate } 41257c478bd9Sstevel@tonic-gate return (result); 41267c478bd9Sstevel@tonic-gate } 41277c478bd9Sstevel@tonic-gate 41287c478bd9Sstevel@tonic-gate #if defined(__x86) 41297c478bd9Sstevel@tonic-gate /* 41307c478bd9Sstevel@tonic-gate * This code is for the purpose of manually recording which files 41317c478bd9Sstevel@tonic-gate * needs to go into the boot archive on any given system. 41327c478bd9Sstevel@tonic-gate * 41337c478bd9Sstevel@tonic-gate * To enable the code, set kobj_file_bufsize in /etc/system 41347c478bd9Sstevel@tonic-gate * and reboot the system, then use mdb to look at kobj_file_buf. 41357c478bd9Sstevel@tonic-gate */ 41367c478bd9Sstevel@tonic-gate static void 41377c478bd9Sstevel@tonic-gate kobj_record_file(char *filename) 41387c478bd9Sstevel@tonic-gate { 41397c478bd9Sstevel@tonic-gate extern char *kobj_file_buf; 41407c478bd9Sstevel@tonic-gate extern int kobj_file_bufsize; 41417c478bd9Sstevel@tonic-gate static char *buf; 41427c478bd9Sstevel@tonic-gate static int size = 0; 41437c478bd9Sstevel@tonic-gate int n; 41447c478bd9Sstevel@tonic-gate 41457c478bd9Sstevel@tonic-gate if (standalone) /* kernel symbol not available */ 41467c478bd9Sstevel@tonic-gate return; 41477c478bd9Sstevel@tonic-gate 41487c478bd9Sstevel@tonic-gate if (kobj_file_bufsize == 0) /* don't bother */ 41497c478bd9Sstevel@tonic-gate return; 41507c478bd9Sstevel@tonic-gate 41517c478bd9Sstevel@tonic-gate if (kobj_file_buf == NULL) { /* allocate buffer */ 41527c478bd9Sstevel@tonic-gate size = kobj_file_bufsize; 41537c478bd9Sstevel@tonic-gate buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP); 41547c478bd9Sstevel@tonic-gate } 41557c478bd9Sstevel@tonic-gate 41567c478bd9Sstevel@tonic-gate n = snprintf(buf, size, "%s\n", filename); 41577c478bd9Sstevel@tonic-gate if (n > size) 41587c478bd9Sstevel@tonic-gate n = size; 41597c478bd9Sstevel@tonic-gate size -= n; 41607c478bd9Sstevel@tonic-gate buf += n; 41617c478bd9Sstevel@tonic-gate } 41627c478bd9Sstevel@tonic-gate #endif /* __x86 */ 41637c478bd9Sstevel@tonic-gate 4164ea8dc4b6Seschrock static int 4165ea8dc4b6Seschrock kobj_boot_fstat(int fd, struct bootstat *stp) 4166ea8dc4b6Seschrock { 4167ea8dc4b6Seschrock #if defined(__sparc) 4168ea8dc4b6Seschrock if (!standalone && _ioquiesced) 4169ea8dc4b6Seschrock return (-1); 4170ea8dc4b6Seschrock return (BOP_FSTAT(ops, fd, stp)); 4171ea8dc4b6Seschrock #else 4172ea8dc4b6Seschrock return (BRD_FSTAT(bfs_ops, fd, stp)); 4173ea8dc4b6Seschrock #endif 4174ea8dc4b6Seschrock } 4175ea8dc4b6Seschrock 41767c478bd9Sstevel@tonic-gate /* 41777c478bd9Sstevel@tonic-gate * XXX these wrappers should go away when sparc is converted 41787c478bd9Sstevel@tonic-gate * boot from ramdisk 41797c478bd9Sstevel@tonic-gate */ 41807c478bd9Sstevel@tonic-gate static int 41817c478bd9Sstevel@tonic-gate kobj_boot_open(char *filename, int flags) 41827c478bd9Sstevel@tonic-gate { 41837c478bd9Sstevel@tonic-gate #if defined(__sparc) 41847c478bd9Sstevel@tonic-gate /* 41857c478bd9Sstevel@tonic-gate * If io via bootops is quiesced, it means boot is no longer 41867c478bd9Sstevel@tonic-gate * available to us. We make it look as if we can't open the 41877c478bd9Sstevel@tonic-gate * named file - which is reasonably accurate. 41887c478bd9Sstevel@tonic-gate */ 41897c478bd9Sstevel@tonic-gate if (!standalone && _ioquiesced) 41907c478bd9Sstevel@tonic-gate return (-1); 41917c478bd9Sstevel@tonic-gate 41927c478bd9Sstevel@tonic-gate return (BOP_OPEN(ops, filename, flags)); 41937c478bd9Sstevel@tonic-gate #else /* x86 */ 41947c478bd9Sstevel@tonic-gate kobj_record_file(filename); 41957c478bd9Sstevel@tonic-gate return (BRD_OPEN(bfs_ops, filename, flags)); 41967c478bd9Sstevel@tonic-gate #endif 41977c478bd9Sstevel@tonic-gate } 41987c478bd9Sstevel@tonic-gate 41997c478bd9Sstevel@tonic-gate static int 42007c478bd9Sstevel@tonic-gate kobj_boot_close(int fd) 42017c478bd9Sstevel@tonic-gate { 42027c478bd9Sstevel@tonic-gate #if defined(__sparc) 42037c478bd9Sstevel@tonic-gate if (!standalone && _ioquiesced) 42047c478bd9Sstevel@tonic-gate return (-1); 42057c478bd9Sstevel@tonic-gate 42067c478bd9Sstevel@tonic-gate return (BOP_CLOSE(ops, fd)); 42077c478bd9Sstevel@tonic-gate #else /* x86 */ 42087c478bd9Sstevel@tonic-gate return (BRD_CLOSE(bfs_ops, fd)); 42097c478bd9Sstevel@tonic-gate #endif 42107c478bd9Sstevel@tonic-gate } 42117c478bd9Sstevel@tonic-gate 42127c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 42137c478bd9Sstevel@tonic-gate static int 42147c478bd9Sstevel@tonic-gate kobj_boot_seek(int fd, off_t hi, off_t lo) 42157c478bd9Sstevel@tonic-gate { 42167c478bd9Sstevel@tonic-gate #if defined(__sparc) 42177c478bd9Sstevel@tonic-gate return (BOP_SEEK(ops, fd, hi, lo)); 42187c478bd9Sstevel@tonic-gate #else 42197c478bd9Sstevel@tonic-gate return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET)); 42207c478bd9Sstevel@tonic-gate #endif 42217c478bd9Sstevel@tonic-gate } 42227c478bd9Sstevel@tonic-gate 42237c478bd9Sstevel@tonic-gate static int 42247c478bd9Sstevel@tonic-gate kobj_boot_read(int fd, caddr_t buf, size_t size) 42257c478bd9Sstevel@tonic-gate { 42267c478bd9Sstevel@tonic-gate #if defined(__sparc) 42277c478bd9Sstevel@tonic-gate return (BOP_READ(ops, fd, buf, size)); 42287c478bd9Sstevel@tonic-gate #else 42297c478bd9Sstevel@tonic-gate return (BRD_READ(bfs_ops, fd, buf, size)); 42307c478bd9Sstevel@tonic-gate #endif 42317c478bd9Sstevel@tonic-gate } 4232