xref: /illumos-gate/usr/src/uts/common/krtld/kobj.c (revision 2570281c)
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 /*
22da346f3bSPramod Batni  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
2583eb21d9SBayard Bell /*
2683eb21d9SBayard Bell  * Copyright 2011 Bayard G. Bell <buffer.g.overflow@gmail.com>.
2783eb21d9SBayard Bell  * All rights reserved. Use is subject to license terms.
282e6e9b6bSJohn Levon  * Copyright 2020 Joyent, Inc.
2983eb21d9SBayard Bell  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Kernel's linker/loader
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/systm.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
427c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/uio.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/mman.h>
497c478bd9Sstevel@tonic-gate #include <vm/as.h>
507c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
517c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
527c478bd9Sstevel@tonic-gate #include <sys/elf.h>
537c478bd9Sstevel@tonic-gate #include <sys/elf_notes.h>
547c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
567c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
577c478bd9Sstevel@tonic-gate #include <sys/kmdb.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/link.h>
607c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
617c478bd9Sstevel@tonic-gate #include <sys/ksyms.h>
627c478bd9Sstevel@tonic-gate #include <sys/disp.h>
637c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
647c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
657c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
667c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
67986fd29aSsetje #include <sys/fs/decomp.h>
687c478bd9Sstevel@tonic-gate #include <sys/callb.h>
697c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
70986fd29aSsetje #include <sys/zmod.h>
717c478bd9Sstevel@tonic-gate 
72986fd29aSsetje #include <krtld/reloc.h>
73986fd29aSsetje #include <krtld/kobj_kdi.h>
747c478bd9Sstevel@tonic-gate #include <sys/sha1.h>
757c478bd9Sstevel@tonic-gate #include <sys/crypto/elfsign.h>
767c478bd9Sstevel@tonic-gate 
77986fd29aSsetje #if !defined(_OBP)
787c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * do_symbols() error codes
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate #define	DOSYM_UNDEF		-1	/* undefined symbol */
857c478bd9Sstevel@tonic-gate #define	DOSYM_UNSAFE		-2	/* MT-unsafe driver symbol */
867c478bd9Sstevel@tonic-gate 
87986fd29aSsetje #if !defined(_OBP)
88ae115bc7Smrj static void synthetic_bootaux(char *, val_t *);
89986fd29aSsetje #endif
90986fd29aSsetje 
91ae115bc7Smrj static struct module *load_exec(val_t *, char *);
927c478bd9Sstevel@tonic-gate static void load_linker(val_t *);
93ae115bc7Smrj static struct modctl *add_primary(const char *filename, int);
947c478bd9Sstevel@tonic-gate static int bind_primary(val_t *, int);
957c478bd9Sstevel@tonic-gate static int load_primary(struct module *, int);
967c478bd9Sstevel@tonic-gate static int load_kmdb(val_t *);
977c478bd9Sstevel@tonic-gate static int get_progbits(struct module *, struct _buf *);
987c478bd9Sstevel@tonic-gate static int get_syms(struct module *, struct _buf *);
997c478bd9Sstevel@tonic-gate static int get_ctf(struct module *, struct _buf *);
1007c478bd9Sstevel@tonic-gate static void get_signature(struct module *, struct _buf *);
1017c478bd9Sstevel@tonic-gate static int do_common(struct module *);
1027c478bd9Sstevel@tonic-gate static void add_dependent(struct module *, struct module *);
1037c478bd9Sstevel@tonic-gate static int do_dependents(struct modctl *, char *, size_t);
1047c478bd9Sstevel@tonic-gate static int do_symbols(struct module *, Elf64_Addr);
1057c478bd9Sstevel@tonic-gate static void module_assign(struct modctl *, struct module *);
1067c478bd9Sstevel@tonic-gate static void free_module_data(struct module *);
1077c478bd9Sstevel@tonic-gate static char *depends_on(struct module *);
108ae115bc7Smrj static char *getmodpath(const char *);
1097c478bd9Sstevel@tonic-gate static char *basename(char *);
1107c478bd9Sstevel@tonic-gate static void attr_val(val_t *);
1117c478bd9Sstevel@tonic-gate static char *find_libmacro(char *);
1127c478bd9Sstevel@tonic-gate static char *expand_libmacro(char *, char *, char *);
1137c478bd9Sstevel@tonic-gate static int read_bootflags(void);
114986fd29aSsetje static int kobj_comp_setup(struct _buf *, struct compinfo *);
115986fd29aSsetje static int kobj_uncomp_blk(struct _buf *, caddr_t, uint_t);
116986fd29aSsetje static int kobj_read_blks(struct _buf *, caddr_t, uint_t, uint_t);
1177c478bd9Sstevel@tonic-gate static int kobj_boot_open(char *, int);
1187c478bd9Sstevel@tonic-gate static int kobj_boot_close(int);
1197c478bd9Sstevel@tonic-gate static int kobj_boot_seek(int, off_t, off_t);
1207c478bd9Sstevel@tonic-gate static int kobj_boot_read(int, caddr_t, size_t);
121ea8dc4b6Seschrock static int kobj_boot_fstat(int, struct bootstat *);
122986fd29aSsetje static int kobj_boot_compinfo(int, struct compinfo *);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static Sym *lookup_one(struct module *, const char *);
1257c478bd9Sstevel@tonic-gate static void sym_insert(struct module *, char *, symid_t);
1267c478bd9Sstevel@tonic-gate static Sym *sym_lookup(struct module *, Sym *);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static struct kobjopen_tctl *kobjopen_alloc(char *filename);
1297c478bd9Sstevel@tonic-gate static void kobjopen_free(struct kobjopen_tctl *ltp);
1307c478bd9Sstevel@tonic-gate static void kobjopen_thread(struct kobjopen_tctl *ltp);
1311d7f3fadSKrishnendu Sadhukhan - Sun Microsystems static int kobj_is_compressed(intptr_t);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate extern int kcopy(const void *, void *, size_t);
1347c478bd9Sstevel@tonic-gate extern int elf_mach_ok(Ehdr *);
1357c478bd9Sstevel@tonic-gate extern int alloc_gottable(struct module *, caddr_t *, caddr_t *);
1367c478bd9Sstevel@tonic-gate 
137986fd29aSsetje #if !defined(_OBP)
138986fd29aSsetje extern int kobj_boot_mountroot(void);
139986fd29aSsetje #endif
140986fd29aSsetje 
1417c478bd9Sstevel@tonic-gate extern int modrootloaded;
1427c478bd9Sstevel@tonic-gate extern int swaploaded;
1437c478bd9Sstevel@tonic-gate extern int bop_io_quiesced;
1447c478bd9Sstevel@tonic-gate extern int last_module_id;
1457c478bd9Sstevel@tonic-gate 
146986fd29aSsetje extern char stubs_base[];
147986fd29aSsetje extern char stubs_end[];
148986fd29aSsetje 
1497c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Values that can be or'd in to kobj_debug and their effects:
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  *	D_DEBUG		- misc. debugging information.
1547c478bd9Sstevel@tonic-gate  *	D_SYMBOLS	- list symbols and their values as they are entered
1557c478bd9Sstevel@tonic-gate  *			  into the hash table
1567c478bd9Sstevel@tonic-gate  *	D_RELOCATIONS	- display relocation processing information
1577c478bd9Sstevel@tonic-gate  *	D_LOADING	- display information about each module as it
1587c478bd9Sstevel@tonic-gate  *			  is loaded.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate int kobj_debug = 0;
161ae115bc7Smrj 
162ae115bc7Smrj #define	KOBJ_MARK(s)	if (kobj_debug & D_DEBUG)	\
163ae115bc7Smrj 	(_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
164ae115bc7Smrj #else
165ae115bc7Smrj #define	KOBJ_MARK(s)	/* discard */
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #define	MODPATH_PROPNAME	"module-path"
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX
1717c478bd9Sstevel@tonic-gate static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/";
1727c478bd9Sstevel@tonic-gate #else
1737c478bd9Sstevel@tonic-gate #define	slash_moddir_suffix_slash	""
1747c478bd9Sstevel@tonic-gate #endif
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	_moddebug	get_weakish_int(&moddebug)
1777c478bd9Sstevel@tonic-gate #define	_modrootloaded	get_weakish_int(&modrootloaded)
1787c478bd9Sstevel@tonic-gate #define	_swaploaded	get_weakish_int(&swaploaded)
1797c478bd9Sstevel@tonic-gate #define	_ioquiesced	get_weakish_int(&bop_io_quiesced)
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #define	mod(X)		(struct module *)((X)->modl_modp->mod_mp)
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate void	*romp;		/* rom vector (opaque to us) */
1847c478bd9Sstevel@tonic-gate struct bootops *ops;	/* bootops vector */
1857c478bd9Sstevel@tonic-gate void *dbvec;		/* debug vector */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * kobjopen thread control structure
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate struct kobjopen_tctl {
1917c478bd9Sstevel@tonic-gate 	ksema_t		sema;
1927c478bd9Sstevel@tonic-gate 	char		*name;		/* name of file */
1937c478bd9Sstevel@tonic-gate 	struct vnode	*vp;		/* vnode return from vn_open() */
1947c478bd9Sstevel@tonic-gate 	int		Errno;		/* error return from vnopen    */
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Structure for defining dynamically expandable library macros
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate struct lib_macro_info {
2027c478bd9Sstevel@tonic-gate 	char	*lmi_list;		/* ptr to list of possible choices */
2037c478bd9Sstevel@tonic-gate 	char	*lmi_macroname;		/* pointer to macro name */
2047c478bd9Sstevel@tonic-gate 	ushort_t lmi_ba_index;		/* index into bootaux vector */
2057c478bd9Sstevel@tonic-gate 	ushort_t lmi_macrolen;		/* macro length */
2067c478bd9Sstevel@tonic-gate } libmacros[] = {
2077c478bd9Sstevel@tonic-gate 	{ NULL, "CPU", BA_CPU, 0 },
2087c478bd9Sstevel@tonic-gate 	{ NULL, "MMU", BA_MMU, 0 }
2097c478bd9Sstevel@tonic-gate };
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #define	NLIBMACROS	sizeof (libmacros) / sizeof (struct lib_macro_info)
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate char *boot_cpu_compatible_list;			/* make $CPU available */
2147c478bd9Sstevel@tonic-gate 
215ae115bc7Smrj char *kobj_module_path;				/* module search path */
2167c478bd9Sstevel@tonic-gate vmem_t	*text_arena;				/* module text arena */
2177c478bd9Sstevel@tonic-gate static vmem_t *data_arena;			/* module data & bss arena */
2187c478bd9Sstevel@tonic-gate static vmem_t *ctf_arena;			/* CTF debug data arena */
2197c478bd9Sstevel@tonic-gate static struct modctl *kobj_modules = NULL;	/* modules loaded */
2207c478bd9Sstevel@tonic-gate int kobj_mmu_pagesize;				/* system pagesize */
2217c478bd9Sstevel@tonic-gate static int lg_pagesize;				/* "large" pagesize */
2227c478bd9Sstevel@tonic-gate static int kobj_last_module_id = 0;		/* id assignment */
2237c478bd9Sstevel@tonic-gate static kmutex_t kobj_lock;			/* protects mach memory list */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * The following functions have been implemented by the kernel.
2277c478bd9Sstevel@tonic-gate  * However, many 3rd party drivers provide their own implementations
2287c478bd9Sstevel@tonic-gate  * of these functions.  When such drivers are loaded, messages
229da6c28aaSamw  * indicating that these symbols have been multiply defined will be
2307c478bd9Sstevel@tonic-gate  * emitted to the console.  To avoid alarming customers for no good
2317c478bd9Sstevel@tonic-gate  * reason, we simply suppress such warnings for the following set of
2327c478bd9Sstevel@tonic-gate  * functions.
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate static char *suppress_sym_list[] =
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	"strstr",
2377c478bd9Sstevel@tonic-gate 	"strncat",
2387c478bd9Sstevel@tonic-gate 	"strlcat",
2397c478bd9Sstevel@tonic-gate 	"strlcpy",
2407c478bd9Sstevel@tonic-gate 	"strspn",
2417c478bd9Sstevel@tonic-gate 	"memcpy",
2427c478bd9Sstevel@tonic-gate 	"memset",
2437c478bd9Sstevel@tonic-gate 	"memmove",
2447c478bd9Sstevel@tonic-gate 	"memcmp",
2457c478bd9Sstevel@tonic-gate 	"memchr",
2467c478bd9Sstevel@tonic-gate 	"__udivdi3",
2477c478bd9Sstevel@tonic-gate 	"__divdi3",
2487c478bd9Sstevel@tonic-gate 	"__umoddi3",
2497c478bd9Sstevel@tonic-gate 	"__moddi3",
2507c478bd9Sstevel@tonic-gate 	NULL		/* This entry must exist */
2517c478bd9Sstevel@tonic-gate };
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /* indexed by KOBJ_NOTIFY_* */
2547c478bd9Sstevel@tonic-gate static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1];
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * Prefix for statically defined tracing (SDT) DTrace probes.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate const char		*sdt_prefix = "__dtrace_probe_";
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
262ae115bc7Smrj  * Beginning and end of the kernel's dynamic text/data segments.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate static caddr_t _text;
2657c478bd9Sstevel@tonic-gate static caddr_t _etext;
266ae115bc7Smrj static caddr_t _data;
267ae115bc7Smrj 
268ae115bc7Smrj /*
269986fd29aSsetje  * The sparc linker doesn't create a memory location
270986fd29aSsetje  * for a variable named _edata, so _edata can only be
271986fd29aSsetje  * referred to, not modified.  krtld needs a static
272986fd29aSsetje  * variable to modify it - within krtld, of course -
273986fd29aSsetje  * outside of krtld, e_data is used in all kernels.
274ae115bc7Smrj  */
275986fd29aSsetje #if defined(__sparc)
276986fd29aSsetje static caddr_t _edata;
277986fd29aSsetje #else
278986fd29aSsetje extern caddr_t _edata;
279ae115bc7Smrj #endif
2807c478bd9Sstevel@tonic-gate 
281986fd29aSsetje Addr dynseg = 0;	/* load address of "dynamic" segment */
282986fd29aSsetje size_t dynsize;		/* "dynamic" segment size */
283986fd29aSsetje 
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate int standalone = 1;			/* an unwholey kernel? */
2867c478bd9Sstevel@tonic-gate int use_iflush;				/* iflush after relocations */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
289508de9f3SToomas Soome  * _kobj_printf() and _vkobj_printf()
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * Common printf function pointer. Can handle only one conversion
2927c478bd9Sstevel@tonic-gate  * specification in the format string. Some of the functions invoked
2937c478bd9Sstevel@tonic-gate  * through this function pointer cannot handle more that one conversion
2947c478bd9Sstevel@tonic-gate  * specification in the format string.
2957c478bd9Sstevel@tonic-gate  */
296c61137dfSJohn Levon void (*_kobj_printf)(void *, const char *, ...) __KPRINTFLIKE(2);
297c61137dfSJohn Levon void (*_vkobj_printf)(void *, const char *, va_list) __KVPRINTFLIKE(2);
2987c478bd9Sstevel@tonic-gate 
299986fd29aSsetje /*
300986fd29aSsetje  * Standalone function pointers for use within krtld.
301986fd29aSsetje  * Many platforms implement optimized platmod versions of
302986fd29aSsetje  * utilities such as bcopy and any such are not yet available
303986fd29aSsetje  * until the kernel is more completely stitched together.
304986fd29aSsetje  * See kobj_impl.h
305986fd29aSsetje  */
306986fd29aSsetje void (*kobj_bcopy)(const void *, void *, size_t);
307986fd29aSsetje void (*kobj_bzero)(void *, size_t);
308986fd29aSsetje size_t (*kobj_strlcat)(char *, const char *, size_t);
309986fd29aSsetje 
3107c478bd9Sstevel@tonic-gate static kobj_stat_t kobj_stat;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate #define	MINALIGN	8	/* at least a double-word */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate int
get_weakish_int(int * ip)3157c478bd9Sstevel@tonic-gate get_weakish_int(int *ip)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	if (standalone)
3187c478bd9Sstevel@tonic-gate 		return (0);
3197c478bd9Sstevel@tonic-gate 	return (ip == NULL ? 0 : *ip);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate static void *
get_weakish_pointer(void ** ptrp)3237c478bd9Sstevel@tonic-gate get_weakish_pointer(void **ptrp)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	if (standalone)
3267c478bd9Sstevel@tonic-gate 		return (0);
3277c478bd9Sstevel@tonic-gate 	return (ptrp == NULL ? 0 : *ptrp);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * XXX fix dependencies on "kernel"; this should work
3327c478bd9Sstevel@tonic-gate  * for other standalone binaries as well.
3337c478bd9Sstevel@tonic-gate  *
3347c478bd9Sstevel@tonic-gate  * XXX Fix hashing code to use one pointer to
3357c478bd9Sstevel@tonic-gate  * hash entries.
3367c478bd9Sstevel@tonic-gate  *	|----------|
3377c478bd9Sstevel@tonic-gate  *	| nbuckets |
3387c478bd9Sstevel@tonic-gate  *	|----------|
3397c478bd9Sstevel@tonic-gate  *	| nchains  |
3407c478bd9Sstevel@tonic-gate  *	|----------|
3417c478bd9Sstevel@tonic-gate  *	| bucket[] |
3427c478bd9Sstevel@tonic-gate  *	|----------|
3437c478bd9Sstevel@tonic-gate  *	| chain[]  |
3447c478bd9Sstevel@tonic-gate  *	|----------|
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Load, bind and relocate all modules that
3497c478bd9Sstevel@tonic-gate  * form the primary kernel. At this point, our
3507c478bd9Sstevel@tonic-gate  * externals have not been relocated.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate void
kobj_init(void * romvec,void * dvec,struct bootops * bootvec,val_t * bootaux)3537c478bd9Sstevel@tonic-gate kobj_init(
3547c478bd9Sstevel@tonic-gate 	void *romvec,
3557c478bd9Sstevel@tonic-gate 	void *dvec,
3567c478bd9Sstevel@tonic-gate 	struct bootops *bootvec,
3577c478bd9Sstevel@tonic-gate 	val_t *bootaux)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	struct module *mp;
3607c478bd9Sstevel@tonic-gate 	struct modctl *modp;
3617c478bd9Sstevel@tonic-gate 	Addr entry;
362ae115bc7Smrj 	char filename[MAXPATHLEN];
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * Save these to pass on to
3667c478bd9Sstevel@tonic-gate 	 * the booted standalone.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	romp = romvec;
3697c478bd9Sstevel@tonic-gate 	dbvec = dvec;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	ops = bootvec;
372986fd29aSsetje 	kobj_setup_standalone_vectors();
3737c478bd9Sstevel@tonic-gate 
374986fd29aSsetje 	KOBJ_MARK("Entered kobj_init()");
375ae115bc7Smrj 
376ae115bc7Smrj 	(void) BOP_GETPROP(ops, "whoami", filename);
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 
393986fd29aSsetje #if defined(_OBP)
394986fd29aSsetje 	/*
395986fd29aSsetje 	 * OBP allows us to read both the ramdisk and
396986fd29aSsetje 	 * the underlying root fs when root is a disk.
397986fd29aSsetje 	 * This can lower incidences of unbootable systems
398986fd29aSsetje 	 * when the archive is out-of-date with the /etc
399986fd29aSsetje 	 * state files.
400986fd29aSsetje 	 */
401986fd29aSsetje 	if (BOP_MOUNTROOT() != BOOT_SVC_OK) {
402986fd29aSsetje 		_kobj_printf(ops, "can't mount boot fs\n");
403986fd29aSsetje 		goto fail;
404986fd29aSsetje 	}
405986fd29aSsetje #else
4067c478bd9Sstevel@tonic-gate 	{
4077c478bd9Sstevel@tonic-gate 		/* on x86, we always boot with a ramdisk */
4087c478bd9Sstevel@tonic-gate 		(void) kobj_boot_mountroot();
409ae115bc7Smrj 
410ae115bc7Smrj 		/*
411ae115bc7Smrj 		 * Now that the ramdisk is mounted, finish boot property
412ae115bc7Smrj 		 * initialization.
413ae115bc7Smrj 		 */
4142e6e9b6bSJohn Levon 		read_bootenvrc();
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
417ae115bc7Smrj #if !defined(_UNIX_KRTLD)
418ae115bc7Smrj 	/*
419986fd29aSsetje 	 * 'unix' is linked together with 'krtld' into one executable and
420ae115bc7Smrj 	 * the early boot code does -not- hand us any of the dynamic metadata
421ae115bc7Smrj 	 * about the executable. In particular, it does not read in, map or
422ae115bc7Smrj 	 * otherwise look at the program headers. We fake all that up now.
423ae115bc7Smrj 	 *
424*2570281cSToomas Soome 	 * We do this early as DTrace static probes call undefined references.
425*2570281cSToomas Soome 	 * We have to process those relocations before calling any of them.
426986fd29aSsetje 	 *
427986fd29aSsetje 	 * OBP tells kobj_start() where the ELF image is in memory, so it
428986fd29aSsetje 	 * synthesized bootaux before kobj_init() was called
429ae115bc7Smrj 	 */
430ae115bc7Smrj 	if (bootaux[BA_PHDR].ba_ptr == NULL)
431ae115bc7Smrj 		synthetic_bootaux(filename, bootaux);
432986fd29aSsetje 
433986fd29aSsetje #endif	/* !_UNIX_KRTLD */
434986fd29aSsetje #endif	/* _OBP */
435ae115bc7Smrj 
436ae115bc7Smrj 	/*
437ae115bc7Smrj 	 * Save the interesting attribute-values
438ae115bc7Smrj 	 * (scanned by kobj_boot).
439ae115bc7Smrj 	 */
440ae115bc7Smrj 	attr_val(bootaux);
441ae115bc7Smrj 
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Set the module search path.
4447c478bd9Sstevel@tonic-gate 	 */
445ae115bc7Smrj 	kobj_module_path = getmodpath(filename);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	boot_cpu_compatible_list = find_libmacro("CPU");
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * These two modules have actually been
4517c478bd9Sstevel@tonic-gate 	 * loaded by boot, but we finish the job
4527c478bd9Sstevel@tonic-gate 	 * by introducing them into the world of
4537c478bd9Sstevel@tonic-gate 	 * loadable modules.
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 
456ae115bc7Smrj 	mp = load_exec(bootaux, filename);
4577c478bd9Sstevel@tonic-gate 	load_linker(bootaux);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Load all the primary dependent modules.
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 	if (load_primary(mp, KOBJ_LM_PRIMARY) == -1)
4637c478bd9Sstevel@tonic-gate 		goto fail;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Glue it together.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1)
4697c478bd9Sstevel@tonic-gate 		goto fail;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	entry = bootaux[BA_ENTRY].ba_val;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * Get the boot flags
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	bootflags(ops);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (boothowto & RB_VERBOSE)
4797c478bd9Sstevel@tonic-gate 		kobj_lm_dump(KOBJ_LM_PRIMARY);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	kobj_kdi_init();
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (boothowto & RB_KMDB) {
4847c478bd9Sstevel@tonic-gate 		if (load_kmdb(bootaux) < 0)
4857c478bd9Sstevel@tonic-gate 			goto fail;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Post setup.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	s_text = _text;
4927c478bd9Sstevel@tonic-gate 	e_text = _etext;
4937c478bd9Sstevel@tonic-gate 	s_data = _data;
4947c478bd9Sstevel@tonic-gate 	e_data = _edata;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	kobj_sync_instruction_memory(s_text, e_text - s_text);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
4997c478bd9Sstevel@tonic-gate 	if (kobj_debug & D_DEBUG)
5007c478bd9Sstevel@tonic-gate 		_kobj_printf(ops,
501c61137dfSJohn Levon 		    "krtld: transferring control to: 0x%lx\n", entry);
5027c478bd9Sstevel@tonic-gate #endif
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Make sure the mod system knows about the modules already loaded.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	last_module_id = kobj_last_module_id;
5087c478bd9Sstevel@tonic-gate 	bcopy(kobj_modules, &modules, sizeof (modules));
5097c478bd9Sstevel@tonic-gate 	modp = &modules;
5107c478bd9Sstevel@tonic-gate 	do {
5117c478bd9Sstevel@tonic-gate 		if (modp->mod_next == kobj_modules)
5127c478bd9Sstevel@tonic-gate 			modp->mod_next = &modules;
5137c478bd9Sstevel@tonic-gate 		if (modp->mod_prev == kobj_modules)
5147c478bd9Sstevel@tonic-gate 			modp->mod_prev = &modules;
5157c478bd9Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	standalone = 0;
5187c478bd9Sstevel@tonic-gate 
519986fd29aSsetje #ifdef	KOBJ_DEBUG
520986fd29aSsetje 	if (kobj_debug & D_DEBUG)
521986fd29aSsetje 		_kobj_printf(ops,
522c61137dfSJohn Levon 		    "krtld: really transferring control to: 0x%lx\n", entry);
523986fd29aSsetje #endif
524986fd29aSsetje 
525986fd29aSsetje 	/* restore printf/bcopy/bzero vectors before returning */
526986fd29aSsetje 	kobj_restore_vectors();
527986fd29aSsetje 
528986fd29aSsetje #if defined(_DBOOT)
5297c478bd9Sstevel@tonic-gate 	/*
530986fd29aSsetje 	 * krtld was called from a dboot ELF section, the embedded
531986fd29aSsetje 	 * dboot code contains the real entry via bootaux
5327c478bd9Sstevel@tonic-gate 	 */
533986fd29aSsetje 	exitto((caddr_t)entry);
534986fd29aSsetje #else
535986fd29aSsetje 	/*
536986fd29aSsetje 	 * krtld was directly called from startup
537986fd29aSsetje 	 */
538986fd29aSsetje 	return;
5397c478bd9Sstevel@tonic-gate #endif
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate fail:
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	_kobj_printf(ops, "krtld: error during initial load/link phase\n");
544c8cefa24Sjosephb 
545c8cefa24Sjosephb #if !defined(_UNIX_KRTLD)
546c8cefa24Sjosephb 	_kobj_printf(ops, "\n");
547c8cefa24Sjosephb 	_kobj_printf(ops, "krtld could neither locate nor resolve symbols"
548c8cefa24Sjosephb 	    " for:\n");
549c8cefa24Sjosephb 	_kobj_printf(ops, "    %s\n", filename);
550c8cefa24Sjosephb 	_kobj_printf(ops, "in the boot archive. Please verify that this"
551c8cefa24Sjosephb 	    " file\n");
552c8cefa24Sjosephb 	_kobj_printf(ops, "matches what is found in the boot archive.\n");
553c8cefa24Sjosephb 	_kobj_printf(ops, "You may need to boot using the Solaris failsafe to"
554c8cefa24Sjosephb 	    " fix this.\n");
555c8cefa24Sjosephb 	bop_panic("Unable to boot");
556c8cefa24Sjosephb #endif
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
559986fd29aSsetje #if !defined(_UNIX_KRTLD) && !defined(_OBP)
560ae115bc7Smrj /*
561986fd29aSsetje  * Synthesize additional metadata that describes the executable if
562986fd29aSsetje  * krtld's caller didn't do it.
563ae115bc7Smrj  *
564ae115bc7Smrj  * (When the dynamic executable has an interpreter, the boot program
565ae115bc7Smrj  * does all this for us.  Where we don't have an interpreter, (or a
566ae115bc7Smrj  * even a boot program, perhaps) we have to do this for ourselves.)
567ae115bc7Smrj  */
568ae115bc7Smrj static void
synthetic_bootaux(char * filename,val_t * bootaux)569ae115bc7Smrj synthetic_bootaux(char *filename, val_t *bootaux)
570ae115bc7Smrj {
571ae115bc7Smrj 	Ehdr ehdr;
572ae115bc7Smrj 	caddr_t phdrbase;
573ae115bc7Smrj 	struct _buf *file;
574ae115bc7Smrj 	int i, n;
575ae115bc7Smrj 
576ae115bc7Smrj 	/*
577ae115bc7Smrj 	 * Elf header
578ae115bc7Smrj 	 */
579ae115bc7Smrj 	KOBJ_MARK("synthetic_bootaux()");
580ae115bc7Smrj 	KOBJ_MARK(filename);
581ae115bc7Smrj 	file = kobj_open_file(filename);
582ae115bc7Smrj 	if (file == (struct _buf *)-1) {
583ae115bc7Smrj 		_kobj_printf(ops, "krtld: failed to open '%s'\n", filename);
584ae115bc7Smrj 		return;
585ae115bc7Smrj 	}
586ae115bc7Smrj 	KOBJ_MARK("reading program headers");
587ae115bc7Smrj 	if (kobj_read_file(file, (char *)&ehdr, sizeof (ehdr), 0) < 0) {
588ae115bc7Smrj 		_kobj_printf(ops, "krtld: %s: failed to read ehder\n",
589ae115bc7Smrj 		    filename);
590ae115bc7Smrj 		return;
591ae115bc7Smrj 	}
592ae115bc7Smrj 
593ae115bc7Smrj 	/*
594ae115bc7Smrj 	 * Program headers
595ae115bc7Smrj 	 */
596ae115bc7Smrj 	bootaux[BA_PHNUM].ba_val = ehdr.e_phnum;
597ae115bc7Smrj 	bootaux[BA_PHENT].ba_val = ehdr.e_phentsize;
598ae115bc7Smrj 	n = ehdr.e_phentsize * ehdr.e_phnum;
599ae115bc7Smrj 
600ae115bc7Smrj 	phdrbase = kobj_alloc(n, KM_WAIT | KM_TMP);
601ae115bc7Smrj 
602ae115bc7Smrj 	if (kobj_read_file(file, phdrbase, n, ehdr.e_phoff) < 0) {
603ae115bc7Smrj 		_kobj_printf(ops, "krtld: %s: failed to read phdrs\n",
604ae115bc7Smrj 		    filename);
605ae115bc7Smrj 		return;
606ae115bc7Smrj 	}
607ae115bc7Smrj 	bootaux[BA_PHDR].ba_ptr = phdrbase;
608ae115bc7Smrj 	kobj_close_file(file);
609ae115bc7Smrj 	KOBJ_MARK("closed file");
610ae115bc7Smrj 
611ae115bc7Smrj 	/*
612ae115bc7Smrj 	 * Find the dynamic section address
613ae115bc7Smrj 	 */
614ae115bc7Smrj 	for (i = 0; i < ehdr.e_phnum; i++) {
615ae115bc7Smrj 		Phdr *phdr = (Phdr *)(phdrbase + ehdr.e_phentsize * i);
616ae115bc7Smrj 
617ae115bc7Smrj 		if (phdr->p_type == PT_DYNAMIC) {
618ae115bc7Smrj 			bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr;
619ae115bc7Smrj 			break;
620ae115bc7Smrj 		}
621ae115bc7Smrj 	}
622ae115bc7Smrj 	KOBJ_MARK("synthetic_bootaux() done");
623ae115bc7Smrj }
624986fd29aSsetje #endif	/* !_UNIX_KRTLD && !_OBP */
625ae115bc7Smrj 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * Set up any global information derived
6287c478bd9Sstevel@tonic-gate  * from attribute/values in the boot or
6297c478bd9Sstevel@tonic-gate  * aux vector.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate static void
attr_val(val_t * bootaux)6327c478bd9Sstevel@tonic-gate attr_val(val_t *bootaux)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	Phdr *phdr;
6357c478bd9Sstevel@tonic-gate 	int phnum, phsize;
6367c478bd9Sstevel@tonic-gate 	int i;
6377c478bd9Sstevel@tonic-gate 
638ae115bc7Smrj 	KOBJ_MARK("attr_val()");
6397c478bd9Sstevel@tonic-gate 	kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val;
6407c478bd9Sstevel@tonic-gate 	lg_pagesize = bootaux[BA_LPAGESZ].ba_val;
6417c478bd9Sstevel@tonic-gate 	use_iflush = bootaux[BA_IFLUSH].ba_val;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr;
6447c478bd9Sstevel@tonic-gate 	phnum = bootaux[BA_PHNUM].ba_val;
6457c478bd9Sstevel@tonic-gate 	phsize = bootaux[BA_PHENT].ba_val;
6467c478bd9Sstevel@tonic-gate 	for (i = 0; i < phnum; i++) {
6477c478bd9Sstevel@tonic-gate 		phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize);
6487c478bd9Sstevel@tonic-gate 
649986fd29aSsetje 		if (phdr->p_type != PT_LOAD) {
6507c478bd9Sstevel@tonic-gate 			continue;
651986fd29aSsetje 		}
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * Bounds of the various segments.
6547c478bd9Sstevel@tonic-gate 		 */
6557c478bd9Sstevel@tonic-gate 		if (!(phdr->p_flags & PF_X)) {
656986fd29aSsetje #if defined(_RELSEG)
657986fd29aSsetje 			/*
658986fd29aSsetje 			 * sparc kernel puts the dynamic info
659986fd29aSsetje 			 * into a separate segment, which is
660986fd29aSsetje 			 * free'd in bop_fini()
661986fd29aSsetje 			 */
662986fd29aSsetje 			ASSERT(phdr->p_vaddr != 0);
6637c478bd9Sstevel@tonic-gate 			dynseg = phdr->p_vaddr;
664986fd29aSsetje 			dynsize = phdr->p_memsz;
665ae115bc7Smrj #else
666ae115bc7Smrj 			ASSERT(phdr->p_vaddr == 0);
667ae115bc7Smrj #endif
6687c478bd9Sstevel@tonic-gate 		} else {
6697c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_W) {
670ae115bc7Smrj 				_data = (caddr_t)phdr->p_vaddr;
671ae115bc7Smrj 				_edata = _data + phdr->p_memsz;
6727c478bd9Sstevel@tonic-gate 			} else {
6737c478bd9Sstevel@tonic-gate 				_text = (caddr_t)phdr->p_vaddr;
6747c478bd9Sstevel@tonic-gate 				_etext = _text + phdr->p_memsz;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* To do the kobj_alloc, _edata needs to be set. */
6807c478bd9Sstevel@tonic-gate 	for (i = 0; i < NLIBMACROS; i++) {
6817c478bd9Sstevel@tonic-gate 		if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) {
6827c478bd9Sstevel@tonic-gate 			libmacros[i].lmi_list = kobj_alloc(
6837c478bd9Sstevel@tonic-gate 			    strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) +
6847c478bd9Sstevel@tonic-gate 			    1, KM_WAIT);
6857c478bd9Sstevel@tonic-gate 			(void) strcpy(libmacros[i].lmi_list,
686b1b8ab34Slling 			    bootaux[libmacros[i].lmi_ba_index].ba_ptr);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 		libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Set up the booted executable.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate static struct module *
load_exec(val_t * bootaux,char * filename)696ae115bc7Smrj load_exec(val_t *bootaux, char *filename)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	struct modctl *cp;
6997c478bd9Sstevel@tonic-gate 	struct module *mp;
7007c478bd9Sstevel@tonic-gate 	Dyn *dyn;
7017c478bd9Sstevel@tonic-gate 	Sym *sp;
7027c478bd9Sstevel@tonic-gate 	int i, lsize, osize, nsize, allocsize;
7037c478bd9Sstevel@tonic-gate 	char *libname, *tmp;
704986fd29aSsetje 	char path[MAXPATHLEN];
705ae115bc7Smrj 
706ae115bc7Smrj #ifdef KOBJ_DEBUG
707ae115bc7Smrj 	if (kobj_debug & D_DEBUG)
708ae115bc7Smrj 		_kobj_printf(ops, "module path '%s'\n", kobj_module_path);
709ae115bc7Smrj #endif
7107c478bd9Sstevel@tonic-gate 
711ae115bc7Smrj 	KOBJ_MARK("add_primary");
7127c478bd9Sstevel@tonic-gate 	cp = add_primary(filename, KOBJ_LM_PRIMARY);
7137c478bd9Sstevel@tonic-gate 
714ae115bc7Smrj 	KOBJ_MARK("struct module");
7157c478bd9Sstevel@tonic-gate 	mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
7167c478bd9Sstevel@tonic-gate 	cp->mod_mp = mp;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * We don't have the following information
7207c478bd9Sstevel@tonic-gate 	 * since this module is an executable and not
7217c478bd9Sstevel@tonic-gate 	 * a relocatable .o.
7227c478bd9Sstevel@tonic-gate 	 */
7237c478bd9Sstevel@tonic-gate 	mp->symtbl_section = 0;
7247c478bd9Sstevel@tonic-gate 	mp->shdrs = NULL;
7257c478bd9Sstevel@tonic-gate 	mp->strhdr = NULL;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	/*
7287c478bd9Sstevel@tonic-gate 	 * Since this module is the only exception,
7297c478bd9Sstevel@tonic-gate 	 * we cons up some section headers.
7307c478bd9Sstevel@tonic-gate 	 */
731ae115bc7Smrj 	KOBJ_MARK("symhdr");
7327c478bd9Sstevel@tonic-gate 	mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
733ae115bc7Smrj 
734ae115bc7Smrj 	KOBJ_MARK("strhdr");
7357c478bd9Sstevel@tonic-gate 	mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_type = SHT_SYMTAB;
7387c478bd9Sstevel@tonic-gate 	mp->strhdr->sh_type = SHT_STRTAB;
7397c478bd9Sstevel@tonic-gate 	/*
7407c478bd9Sstevel@tonic-gate 	 * Scan the dynamic structure.
7417c478bd9Sstevel@tonic-gate 	 */
7427c478bd9Sstevel@tonic-gate 	for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
7437c478bd9Sstevel@tonic-gate 	    dyn->d_tag != DT_NULL; dyn++) {
7447c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
7457c478bd9Sstevel@tonic-gate 		case DT_SYMTAB:
7467c478bd9Sstevel@tonic-gate 			mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr;
7477c478bd9Sstevel@tonic-gate 			mp->symhdr->sh_addr = dyn->d_un.d_ptr;
7487c478bd9Sstevel@tonic-gate 			break;
7497c478bd9Sstevel@tonic-gate 		case DT_HASH:
7507c478bd9Sstevel@tonic-gate 			mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1);
7517c478bd9Sstevel@tonic-gate 			mp->hashsize = *(uint_t *)dyn->d_un.d_ptr;
7527c478bd9Sstevel@tonic-gate 			break;
7537c478bd9Sstevel@tonic-gate 		case DT_STRTAB:
7547c478bd9Sstevel@tonic-gate 			mp->strings = (char *)dyn->d_un.d_ptr;
7557c478bd9Sstevel@tonic-gate 			mp->strhdr->sh_addr = dyn->d_un.d_ptr;
7567c478bd9Sstevel@tonic-gate 			break;
7577c478bd9Sstevel@tonic-gate 		case DT_STRSZ:
7587c478bd9Sstevel@tonic-gate 			mp->strhdr->sh_size = dyn->d_un.d_val;
7597c478bd9Sstevel@tonic-gate 			break;
7607c478bd9Sstevel@tonic-gate 		case DT_SYMENT:
7617c478bd9Sstevel@tonic-gate 			mp->symhdr->sh_entsize = dyn->d_un.d_val;
7627c478bd9Sstevel@tonic-gate 			break;
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/*
7677c478bd9Sstevel@tonic-gate 	 * Collapse any DT_NEEDED entries into one string.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	nsize = osize = 0;
7707c478bd9Sstevel@tonic-gate 	allocsize = MAXPATHLEN;
7717c478bd9Sstevel@tonic-gate 
772ae115bc7Smrj 	KOBJ_MARK("depends_on");
7737c478bd9Sstevel@tonic-gate 	mp->depends_on = kobj_alloc(allocsize, KM_WAIT);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
7767c478bd9Sstevel@tonic-gate 	    dyn->d_tag != DT_NULL; dyn++)
7777c478bd9Sstevel@tonic-gate 		if (dyn->d_tag == DT_NEEDED) {
7787c478bd9Sstevel@tonic-gate 			char *_lib;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 			libname = mp->strings + dyn->d_un.d_val;
7817c478bd9Sstevel@tonic-gate 			if (strchr(libname, '$') != NULL) {
7827c478bd9Sstevel@tonic-gate 				if ((_lib = expand_libmacro(libname,
783986fd29aSsetje 				    path, path)) != NULL)
7847c478bd9Sstevel@tonic-gate 					libname = _lib;
7857c478bd9Sstevel@tonic-gate 				else
7867c478bd9Sstevel@tonic-gate 					_kobj_printf(ops, "krtld: "
7877c478bd9Sstevel@tonic-gate 					    "load_exec: fail to "
7887c478bd9Sstevel@tonic-gate 					    "expand %s\n", libname);
7897c478bd9Sstevel@tonic-gate 			}
7907c478bd9Sstevel@tonic-gate 			lsize = strlen(libname);
7917c478bd9Sstevel@tonic-gate 			nsize += lsize;
7927c478bd9Sstevel@tonic-gate 			if (nsize + 1 > allocsize) {
793ae115bc7Smrj 				KOBJ_MARK("grow depends_on");
7947c478bd9Sstevel@tonic-gate 				tmp = kobj_alloc(allocsize + MAXPATHLEN,
7957c478bd9Sstevel@tonic-gate 				    KM_WAIT);
7967c478bd9Sstevel@tonic-gate 				bcopy(mp->depends_on, tmp, osize);
7977c478bd9Sstevel@tonic-gate 				kobj_free(mp->depends_on, allocsize);
7987c478bd9Sstevel@tonic-gate 				mp->depends_on = tmp;
7997c478bd9Sstevel@tonic-gate 				allocsize += MAXPATHLEN;
8007c478bd9Sstevel@tonic-gate 			}
8017c478bd9Sstevel@tonic-gate 			bcopy(libname, mp->depends_on + osize, lsize);
802da6c28aaSamw 			*(mp->depends_on + nsize) = ' '; /* separate */
8037c478bd9Sstevel@tonic-gate 			nsize++;
8047c478bd9Sstevel@tonic-gate 			osize = nsize;
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 	if (nsize) {
8077c478bd9Sstevel@tonic-gate 		mp->depends_on[nsize - 1] = '\0'; /* terminate the string */
8087c478bd9Sstevel@tonic-gate 		/*
8097c478bd9Sstevel@tonic-gate 		 * alloc with exact size and copy whatever it got over
8107c478bd9Sstevel@tonic-gate 		 */
811ae115bc7Smrj 		KOBJ_MARK("realloc depends_on");
8127c478bd9Sstevel@tonic-gate 		tmp = kobj_alloc(nsize, KM_WAIT);
8137c478bd9Sstevel@tonic-gate 		bcopy(mp->depends_on, tmp, nsize);
8147c478bd9Sstevel@tonic-gate 		kobj_free(mp->depends_on, allocsize);
8157c478bd9Sstevel@tonic-gate 		mp->depends_on = tmp;
8167c478bd9Sstevel@tonic-gate 	} else {
8177c478bd9Sstevel@tonic-gate 		kobj_free(mp->depends_on, allocsize);
8187c478bd9Sstevel@tonic-gate 		mp->depends_on = NULL;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	mp->flags = KOBJ_EXEC|KOBJ_PRIM;	/* NOT a relocatable .o */
8227c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize;
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * We allocate our own table since we don't
8257c478bd9Sstevel@tonic-gate 	 * hash undefined references.
8267c478bd9Sstevel@tonic-gate 	 */
827ae115bc7Smrj 	KOBJ_MARK("chains");
8287c478bd9Sstevel@tonic-gate 	mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
829ae115bc7Smrj 	KOBJ_MARK("buckets");
8307c478bd9Sstevel@tonic-gate 	mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	mp->text = _text;
8337c478bd9Sstevel@tonic-gate 	mp->data = _data;
834ae115bc7Smrj 
835ae115bc7Smrj 	mp->text_size = _etext - _text;
836ae115bc7Smrj 	mp->data_size = _edata - _data;
837ae115bc7Smrj 
8387c478bd9Sstevel@tonic-gate 	cp->mod_text = mp->text;
8397c478bd9Sstevel@tonic-gate 	cp->mod_text_size = mp->text_size;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	mp->filename = cp->mod_filename;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
8447c478bd9Sstevel@tonic-gate 	if (kobj_debug & D_LOADING) {
8457c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: file=%s\n", mp->filename);
8467c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "\ttext: 0x%p", mp->text);
847c61137dfSJohn Levon 		_kobj_printf(ops, " size: 0x%lx\n", mp->text_size);
8487c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "\tdata: 0x%p", mp->data);
849c61137dfSJohn Levon 		_kobj_printf(ops, " dsize: 0x%lx\n", mp->data_size);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/*
8547c478bd9Sstevel@tonic-gate 	 * Insert symbols into the hash table.
8557c478bd9Sstevel@tonic-gate 	 */
8567c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->nsyms; i++) {
8577c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
8607c478bd9Sstevel@tonic-gate 			continue;
861986fd29aSsetje #if defined(__sparc)
8627c478bd9Sstevel@tonic-gate 		/*
8637c478bd9Sstevel@tonic-gate 		 * Register symbols are ignored in the kernel
8647c478bd9Sstevel@tonic-gate 		 */
8657c478bd9Sstevel@tonic-gate 		if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER)
8667c478bd9Sstevel@tonic-gate 			continue;
8677c478bd9Sstevel@tonic-gate #endif	/* __sparc */
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		sym_insert(mp, mp->strings + sp->st_name, i);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
872ae115bc7Smrj 	KOBJ_MARK("load_exec done");
8737c478bd9Sstevel@tonic-gate 	return (mp);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
877ae115bc7Smrj  * Set up the linker module (if it's compiled in, LDNAME is NULL)
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate static void
load_linker(val_t * bootaux)8807c478bd9Sstevel@tonic-gate load_linker(val_t *bootaux)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	struct module *kmp = (struct module *)kobj_modules->mod_mp;
8837c478bd9Sstevel@tonic-gate 	struct module *mp;
8847c478bd9Sstevel@tonic-gate 	struct modctl *cp;
8857c478bd9Sstevel@tonic-gate 	int i;
8867c478bd9Sstevel@tonic-gate 	Shdr *shp;
8877c478bd9Sstevel@tonic-gate 	Sym *sp;
8887c478bd9Sstevel@tonic-gate 	int shsize;
8897c478bd9Sstevel@tonic-gate 	char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr;
8907c478bd9Sstevel@tonic-gate 
891ae115bc7Smrj 	/*
892ae115bc7Smrj 	 * On some architectures, krtld is compiled into the kernel.
893ae115bc7Smrj 	 */
894ae115bc7Smrj 	if (dlname == NULL)
895ae115bc7Smrj 		return;
896ae115bc7Smrj 
8977c478bd9Sstevel@tonic-gate 	cp = add_primary(dlname, KOBJ_LM_PRIMARY);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	cp->mod_mp = mp;
9027c478bd9Sstevel@tonic-gate 	mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr;
9037c478bd9Sstevel@tonic-gate 	shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum;
9047c478bd9Sstevel@tonic-gate 	mp->shdrs = kobj_alloc(shsize, KM_WAIT);
9057c478bd9Sstevel@tonic-gate 	bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	for (i = 1; i < (int)mp->hdr.e_shnum; i++) {
9087c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize));
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		if (shp->sh_flags & SHF_ALLOC) {
9117c478bd9Sstevel@tonic-gate 			if (shp->sh_flags & SHF_WRITE) {
9127c478bd9Sstevel@tonic-gate 				if (mp->data == NULL)
9137c478bd9Sstevel@tonic-gate 					mp->data = (char *)shp->sh_addr;
9147c478bd9Sstevel@tonic-gate 			} else if (mp->text == NULL) {
9157c478bd9Sstevel@tonic-gate 				mp->text = (char *)shp->sh_addr;
9167c478bd9Sstevel@tonic-gate 			}
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 		if (shp->sh_type == SHT_SYMTAB) {
9197c478bd9Sstevel@tonic-gate 			mp->symtbl_section = i;
9207c478bd9Sstevel@tonic-gate 			mp->symhdr = shp;
9217c478bd9Sstevel@tonic-gate 			mp->symspace = mp->symtbl = (char *)shp->sh_addr;
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
9257c478bd9Sstevel@tonic-gate 	mp->flags = KOBJ_INTERP|KOBJ_PRIM;
9267c478bd9Sstevel@tonic-gate 	mp->strhdr = (Shdr *)
927b1b8ab34Slling 	    (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
9287c478bd9Sstevel@tonic-gate 	mp->strings = (char *)mp->strhdr->sh_addr;
9297c478bd9Sstevel@tonic-gate 	mp->hashsize = kobj_gethashsize(mp->nsyms);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) +
932b1b8ab34Slling 	    (mp->hashsize + mp->nsyms) * sizeof (symid_t);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
9357c478bd9Sstevel@tonic-gate 	mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	mp->bss = bootaux[BA_BSS].ba_val;
9387c478bd9Sstevel@tonic-gate 	mp->bss_align = 0;	/* pre-aligned during allocation */
9397c478bd9Sstevel@tonic-gate 	mp->bss_size = (uintptr_t)_edata - mp->bss;
9407c478bd9Sstevel@tonic-gate 	mp->text_size = _etext - mp->text;
9417c478bd9Sstevel@tonic-gate 	mp->data_size = _edata - mp->data;
9427c478bd9Sstevel@tonic-gate 	mp->filename = cp->mod_filename;
9437c478bd9Sstevel@tonic-gate 	cp->mod_text = mp->text;
9447c478bd9Sstevel@tonic-gate 	cp->mod_text_size = mp->text_size;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * Now that we've figured out where the linker is,
9487c478bd9Sstevel@tonic-gate 	 * set the limits for the booted object.
9497c478bd9Sstevel@tonic-gate 	 */
9507c478bd9Sstevel@tonic-gate 	kmp->text_size = (size_t)(mp->text - kmp->text);
9517c478bd9Sstevel@tonic-gate 	kmp->data_size = (size_t)(mp->data - kmp->data);
9527c478bd9Sstevel@tonic-gate 	kobj_modules->mod_text_size = kmp->text_size;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
9557c478bd9Sstevel@tonic-gate 	if (kobj_debug & D_LOADING) {
9567c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: file=%s\n", mp->filename);
9577c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "\ttext:0x%p", mp->text);
958c61137dfSJohn Levon 		_kobj_printf(ops, " size: 0x%lx\n", mp->text_size);
9597c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "\tdata:0x%p", mp->data);
960c61137dfSJohn Levon 		_kobj_printf(ops, " dsize: 0x%lx\n", mp->data_size);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * Insert the symbols into the hash table.
9667c478bd9Sstevel@tonic-gate 	 */
9677c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->nsyms; i++) {
9687c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
9717c478bd9Sstevel@tonic-gate 			continue;
9727c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
9737c478bd9Sstevel@tonic-gate 			if (sp->st_shndx == SHN_COMMON)
9747c478bd9Sstevel@tonic-gate 				sp->st_shndx = SHN_ABS;
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 		sym_insert(mp, mp->strings + sp->st_name, i);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate static kobj_notify_list_t **
kobj_notify_lookup(uint_t type)9827c478bd9Sstevel@tonic-gate kobj_notify_lookup(uint_t type)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	ASSERT(type != 0 && type < sizeof (kobj_notifiers) /
9857c478bd9Sstevel@tonic-gate 	    sizeof (kobj_notify_list_t *));
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	return (&kobj_notifiers[type]);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate int
kobj_notify_add(kobj_notify_list_t * knp)9917c478bd9Sstevel@tonic-gate kobj_notify_add(kobj_notify_list_t *knp)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	kobj_notify_list_t **knl;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	knl = kobj_notify_lookup(knp->kn_type);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	knp->kn_next = NULL;
9987c478bd9Sstevel@tonic-gate 	knp->kn_prev = NULL;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	mutex_enter(&kobj_lock);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (*knl != NULL) {
10037c478bd9Sstevel@tonic-gate 		(*knl)->kn_prev = knp;
10047c478bd9Sstevel@tonic-gate 		knp->kn_next = *knl;
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 	(*knl) = knp;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	mutex_exit(&kobj_lock);
10097c478bd9Sstevel@tonic-gate 	return (0);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate int
kobj_notify_remove(kobj_notify_list_t * knp)10137c478bd9Sstevel@tonic-gate kobj_notify_remove(kobj_notify_list_t *knp)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type);
10167c478bd9Sstevel@tonic-gate 	kobj_notify_list_t *tknp;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	mutex_enter(&kobj_lock);
10197c478bd9Sstevel@tonic-gate 
10203df2e8b2SRobert Mustacchi 	if ((tknp = knp->kn_next) != NULL)
10217c478bd9Sstevel@tonic-gate 		tknp->kn_prev = knp->kn_prev;
10227c478bd9Sstevel@tonic-gate 
10233df2e8b2SRobert Mustacchi 	if ((tknp = knp->kn_prev) != NULL)
10247c478bd9Sstevel@tonic-gate 		tknp->kn_next = knp->kn_next;
10257c478bd9Sstevel@tonic-gate 	else
10267c478bd9Sstevel@tonic-gate 		*knl = knp->kn_next;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	mutex_exit(&kobj_lock);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	return (0);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * Notify all interested callbacks of a specified change in module state.
10357c478bd9Sstevel@tonic-gate  */
10367c478bd9Sstevel@tonic-gate static void
kobj_notify(int type,struct modctl * modp)10377c478bd9Sstevel@tonic-gate kobj_notify(int type, struct modctl *modp)
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate 	kobj_notify_list_t *knp;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	if (modp->mod_loadflags & MOD_NONOTIFY || standalone)
10427c478bd9Sstevel@tonic-gate 		return;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	mutex_enter(&kobj_lock);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next)
10477c478bd9Sstevel@tonic-gate 		knp->kn_func(type, modp);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	/*
10507c478bd9Sstevel@tonic-gate 	 * KDI notification must be last (it has to allow for work done by the
10517c478bd9Sstevel@tonic-gate 	 * other notification callbacks), so we call it manually.
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	kobj_kdi_mod_notify(type, modp);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	mutex_exit(&kobj_lock);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate /*
1059986fd29aSsetje  * Create the module path.
10607c478bd9Sstevel@tonic-gate  */
10617c478bd9Sstevel@tonic-gate static char *
getmodpath(const char * filename)1062ae115bc7Smrj getmodpath(const char *filename)
10637c478bd9Sstevel@tonic-gate {
1064986fd29aSsetje 	char *path = kobj_zalloc(MAXPATHLEN, KM_WAIT);
1065ae115bc7Smrj 
1066ae115bc7Smrj 	/*
1067986fd29aSsetje 	 * Platform code gets first crack, then add
1068986fd29aSsetje 	 * the default components
1069ae115bc7Smrj 	 */
1070986fd29aSsetje 	mach_modpath(path, filename);
1071986fd29aSsetje 	if (*path != '\0')
1072986fd29aSsetje 		(void) strcat(path, " ");
1073ae115bc7Smrj 	return (strcat(path, MOD_DEFPATH));
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate static struct modctl *
add_primary(const char * filename,int lmid)1077ae115bc7Smrj add_primary(const char *filename, int lmid)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	struct modctl *cp;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/*
10867c478bd9Sstevel@tonic-gate 	 * For symbol lookup, we assemble our own
10877c478bd9Sstevel@tonic-gate 	 * modctl list of the primary modules.
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	(void) strcpy(cp->mod_filename, filename);
10917c478bd9Sstevel@tonic-gate 	cp->mod_modname = basename(cp->mod_filename);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* set values for modinfo assuming that the load will work */
10947c478bd9Sstevel@tonic-gate 	cp->mod_prim = 1;
10957c478bd9Sstevel@tonic-gate 	cp->mod_loaded = 1;
10967c478bd9Sstevel@tonic-gate 	cp->mod_installed = 1;
10977c478bd9Sstevel@tonic-gate 	cp->mod_loadcnt = 1;
10987c478bd9Sstevel@tonic-gate 	cp->mod_loadflags = MOD_NOAUTOUNLOAD;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	cp->mod_id = kobj_last_module_id++;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/*
11037c478bd9Sstevel@tonic-gate 	 * Link the module in. We'll pass this info on
11047c478bd9Sstevel@tonic-gate 	 * to the mod squad later.
11057c478bd9Sstevel@tonic-gate 	 */
11067c478bd9Sstevel@tonic-gate 	if (kobj_modules == NULL) {
11077c478bd9Sstevel@tonic-gate 		kobj_modules = cp;
11087c478bd9Sstevel@tonic-gate 		cp->mod_prev = cp->mod_next = cp;
11097c478bd9Sstevel@tonic-gate 	} else {
11107c478bd9Sstevel@tonic-gate 		cp->mod_prev = kobj_modules->mod_prev;
11117c478bd9Sstevel@tonic-gate 		cp->mod_next = kobj_modules;
11127c478bd9Sstevel@tonic-gate 		kobj_modules->mod_prev->mod_next = cp;
11137c478bd9Sstevel@tonic-gate 		kobj_modules->mod_prev = cp;
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	kobj_lm_append(lmid, cp);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	return (cp);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate static int
bind_primary(val_t * bootaux,int lmid)11227c478bd9Sstevel@tonic-gate bind_primary(val_t *bootaux, int lmid)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	struct modctl_list *linkmap = kobj_lm_lookup(lmid);
11257c478bd9Sstevel@tonic-gate 	struct modctl_list *lp;
11267c478bd9Sstevel@tonic-gate 	struct module *mp;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * Do common symbols.
11307c478bd9Sstevel@tonic-gate 	 */
11317c478bd9Sstevel@tonic-gate 	for (lp = linkmap; lp; lp = lp->modl_next) {
11327c478bd9Sstevel@tonic-gate 		mp = mod(lp);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		/*
11357c478bd9Sstevel@tonic-gate 		 * Don't do common section relocations for modules that
11367c478bd9Sstevel@tonic-gate 		 * don't need it.
11377c478bd9Sstevel@tonic-gate 		 */
11387c478bd9Sstevel@tonic-gate 		if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP))
11397c478bd9Sstevel@tonic-gate 			continue;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		if (do_common(mp) < 0)
11427c478bd9Sstevel@tonic-gate 			return (-1);
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/*
11467c478bd9Sstevel@tonic-gate 	 * Resolve symbols.
11477c478bd9Sstevel@tonic-gate 	 */
11487c478bd9Sstevel@tonic-gate 	for (lp = linkmap; lp; lp = lp->modl_next) {
11497c478bd9Sstevel@tonic-gate 		mp = mod(lp);
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 		if (do_symbols(mp, 0) < 0)
11527c478bd9Sstevel@tonic-gate 			return (-1);
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/*
11567c478bd9Sstevel@tonic-gate 	 * Do relocations.
11577c478bd9Sstevel@tonic-gate 	 */
11587c478bd9Sstevel@tonic-gate 	for (lp = linkmap; lp; lp = lp->modl_next) {
11597c478bd9Sstevel@tonic-gate 		mp = mod(lp);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		if (mp->flags & KOBJ_EXEC) {
1162ae115bc7Smrj 			Dyn *dyn;
1163ae115bc7Smrj 			Word relasz = 0, relaent = 0;
1164ae115bc7Smrj 			char *rela = NULL;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 			for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr;
11677c478bd9Sstevel@tonic-gate 			    dyn->d_tag != DT_NULL; dyn++) {
11687c478bd9Sstevel@tonic-gate 				switch (dyn->d_tag) {
11697c478bd9Sstevel@tonic-gate 				case DT_RELASZ:
11707c478bd9Sstevel@tonic-gate 				case DT_RELSZ:
11717c478bd9Sstevel@tonic-gate 					relasz = dyn->d_un.d_val;
11727c478bd9Sstevel@tonic-gate 					break;
11737c478bd9Sstevel@tonic-gate 				case DT_RELAENT:
11747c478bd9Sstevel@tonic-gate 				case DT_RELENT:
11757c478bd9Sstevel@tonic-gate 					relaent = dyn->d_un.d_val;
11767c478bd9Sstevel@tonic-gate 					break;
11777c478bd9Sstevel@tonic-gate 				case DT_RELA:
1178986fd29aSsetje 					rela = (char *)dyn->d_un.d_ptr;
11797c478bd9Sstevel@tonic-gate 					break;
11807c478bd9Sstevel@tonic-gate 				case DT_REL:
1181986fd29aSsetje 					rela = (char *)dyn->d_un.d_ptr;
11827c478bd9Sstevel@tonic-gate 					break;
11837c478bd9Sstevel@tonic-gate 				}
11847c478bd9Sstevel@tonic-gate 			}
11857c478bd9Sstevel@tonic-gate 			if (relasz == 0 ||
11867c478bd9Sstevel@tonic-gate 			    relaent == 0 || rela == NULL) {
11877c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "krtld: bind_primary(): "
11887c478bd9Sstevel@tonic-gate 				    "no relocation information found for "
11897c478bd9Sstevel@tonic-gate 				    "module %s\n", mp->filename);
11907c478bd9Sstevel@tonic-gate 				return (-1);
11917c478bd9Sstevel@tonic-gate 			}
11927c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
11937c478bd9Sstevel@tonic-gate 			if (kobj_debug & D_RELOCATIONS)
11947c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "krtld: relocating: file=%s "
11957c478bd9Sstevel@tonic-gate 				    "KOBJ_EXEC\n", mp->filename);
11967c478bd9Sstevel@tonic-gate #endif
1197584b574aSToomas Soome 			if (do_relocate(mp, rela, relasz/relaent, relaent,
1198584b574aSToomas Soome 			    (Addr)mp->text) < 0)
11997c478bd9Sstevel@tonic-gate 				return (-1);
12007c478bd9Sstevel@tonic-gate 		} else {
12017c478bd9Sstevel@tonic-gate 			if (do_relocations(mp) < 0)
12027c478bd9Sstevel@tonic-gate 				return (-1);
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		kobj_sync_instruction_memory(mp->text, mp->text_size);
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	for (lp = linkmap; lp; lp = lp->modl_next) {
12097c478bd9Sstevel@tonic-gate 		mp = mod(lp);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 		/*
12127c478bd9Sstevel@tonic-gate 		 * We need to re-read the full symbol table for the boot file,
12137c478bd9Sstevel@tonic-gate 		 * since we couldn't use the full one before.  We also need to
12147c478bd9Sstevel@tonic-gate 		 * load the CTF sections of both the boot file and the
12157c478bd9Sstevel@tonic-gate 		 * interpreter (us).
12167c478bd9Sstevel@tonic-gate 		 */
12177c478bd9Sstevel@tonic-gate 		if (mp->flags & KOBJ_EXEC) {
12187c478bd9Sstevel@tonic-gate 			struct _buf *file;
12197c478bd9Sstevel@tonic-gate 			int n;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 			file = kobj_open_file(mp->filename);
12227c478bd9Sstevel@tonic-gate 			if (file == (struct _buf *)-1)
12237c478bd9Sstevel@tonic-gate 				return (-1);
12247c478bd9Sstevel@tonic-gate 			if (kobj_read_file(file, (char *)&mp->hdr,
12257c478bd9Sstevel@tonic-gate 			    sizeof (mp->hdr), 0) < 0)
12267c478bd9Sstevel@tonic-gate 				return (-1);
12277c478bd9Sstevel@tonic-gate 			n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
12287c478bd9Sstevel@tonic-gate 			mp->shdrs = kobj_alloc(n, KM_WAIT);
12297c478bd9Sstevel@tonic-gate 			if (kobj_read_file(file, mp->shdrs, n,
12307c478bd9Sstevel@tonic-gate 			    mp->hdr.e_shoff) < 0)
12317c478bd9Sstevel@tonic-gate 				return (-1);
12327c478bd9Sstevel@tonic-gate 			if (get_syms(mp, file) < 0)
12337c478bd9Sstevel@tonic-gate 				return (-1);
12347c478bd9Sstevel@tonic-gate 			if (get_ctf(mp, file) < 0)
12357c478bd9Sstevel@tonic-gate 				return (-1);
12367c478bd9Sstevel@tonic-gate 			kobj_close_file(file);
12377c478bd9Sstevel@tonic-gate 			mp->flags |= KOBJ_RELOCATED;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 		} else if (mp->flags & KOBJ_INTERP) {
12407c478bd9Sstevel@tonic-gate 			struct _buf *file;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 			/*
12437c478bd9Sstevel@tonic-gate 			 * The interpreter path fragment in mp->filename
12447c478bd9Sstevel@tonic-gate 			 * will already have the module directory suffix
12457c478bd9Sstevel@tonic-gate 			 * in it (if appropriate).
12467c478bd9Sstevel@tonic-gate 			 */
12477c478bd9Sstevel@tonic-gate 			file = kobj_open_path(mp->filename, 1, 0);
12487c478bd9Sstevel@tonic-gate 			if (file == (struct _buf *)-1)
12497c478bd9Sstevel@tonic-gate 				return (-1);
12507c478bd9Sstevel@tonic-gate 			if (get_ctf(mp, file) < 0)
12517c478bd9Sstevel@tonic-gate 				return (-1);
12527c478bd9Sstevel@tonic-gate 			kobj_close_file(file);
12537c478bd9Sstevel@tonic-gate 			mp->flags |= KOBJ_RELOCATED;
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	return (0);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate static struct modctl *
mod_already_loaded(char * modname)12617c478bd9Sstevel@tonic-gate mod_already_loaded(char *modname)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	struct modctl *mctl = kobj_modules;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	do {
12667c478bd9Sstevel@tonic-gate 		if (strcmp(modname, mctl->mod_filename) == 0)
12677c478bd9Sstevel@tonic-gate 			return (mctl);
12687c478bd9Sstevel@tonic-gate 		mctl = mctl->mod_next;
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	} while (mctl != kobj_modules);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	return (NULL);
12737c478bd9Sstevel@tonic-gate }
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate /*
12767c478bd9Sstevel@tonic-gate  * Load all the primary dependent modules.
12777c478bd9Sstevel@tonic-gate  */
12787c478bd9Sstevel@tonic-gate static int
load_primary(struct module * mp,int lmid)12797c478bd9Sstevel@tonic-gate load_primary(struct module *mp, int lmid)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	struct modctl *cp;
12827c478bd9Sstevel@tonic-gate 	struct module *dmp;
12837c478bd9Sstevel@tonic-gate 	char *p, *q;
12847c478bd9Sstevel@tonic-gate 	char modname[MODMAXNAMELEN];
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	if ((p = mp->depends_on) == NULL)
12877c478bd9Sstevel@tonic-gate 		return (0);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/* CONSTANTCONDITION */
12907c478bd9Sstevel@tonic-gate 	while (1) {
12917c478bd9Sstevel@tonic-gate 		/*
12927c478bd9Sstevel@tonic-gate 		 * Skip space.
12937c478bd9Sstevel@tonic-gate 		 */
12947c478bd9Sstevel@tonic-gate 		while (*p && (*p == ' ' || *p == '\t'))
12957c478bd9Sstevel@tonic-gate 			p++;
12967c478bd9Sstevel@tonic-gate 		/*
12977c478bd9Sstevel@tonic-gate 		 * Get module name.
12987c478bd9Sstevel@tonic-gate 		 */
12997c478bd9Sstevel@tonic-gate 		q = modname;
13007c478bd9Sstevel@tonic-gate 		while (*p && *p != ' ' && *p != '\t')
13017c478bd9Sstevel@tonic-gate 			*q++ = *p++;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 		if (q == modname)
13047c478bd9Sstevel@tonic-gate 			break;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 		*q = '\0';
13077c478bd9Sstevel@tonic-gate 		/*
13087c478bd9Sstevel@tonic-gate 		 * Check for dup dependencies.
13097c478bd9Sstevel@tonic-gate 		 */
13107c478bd9Sstevel@tonic-gate 		if (strcmp(modname, "dtracestubs") == 0 ||
13117c478bd9Sstevel@tonic-gate 		    mod_already_loaded(modname) != NULL)
13127c478bd9Sstevel@tonic-gate 			continue;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		cp = add_primary(modname, lmid);
13157c478bd9Sstevel@tonic-gate 		cp->mod_busy = 1;
13167c478bd9Sstevel@tonic-gate 		/*
13177c478bd9Sstevel@tonic-gate 		 * Load it.
13187c478bd9Sstevel@tonic-gate 		 */
13197c478bd9Sstevel@tonic-gate 		(void) kobj_load_module(cp, 1);
13207c478bd9Sstevel@tonic-gate 		cp->mod_busy = 0;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 		if ((dmp = cp->mod_mp) == NULL) {
13237c478bd9Sstevel@tonic-gate 			cp->mod_loaded = 0;
13247c478bd9Sstevel@tonic-gate 			cp->mod_installed = 0;
13257c478bd9Sstevel@tonic-gate 			cp->mod_loadcnt = 0;
13267c478bd9Sstevel@tonic-gate 			return (-1);
13277c478bd9Sstevel@tonic-gate 		}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 		add_dependent(mp, dmp);
13307c478bd9Sstevel@tonic-gate 		dmp->flags |= KOBJ_PRIM;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 		/*
13337c478bd9Sstevel@tonic-gate 		 * Recurse.
13347c478bd9Sstevel@tonic-gate 		 */
13357c478bd9Sstevel@tonic-gate 		if (load_primary(dmp, lmid) == -1) {
13367c478bd9Sstevel@tonic-gate 			cp->mod_loaded = 0;
13377c478bd9Sstevel@tonic-gate 			cp->mod_installed = 0;
13387c478bd9Sstevel@tonic-gate 			cp->mod_loadcnt = 0;
13397c478bd9Sstevel@tonic-gate 			return (-1);
13407c478bd9Sstevel@tonic-gate 		}
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	return (0);
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
134528cdc3d7Sszhou static int
console_is_usb_serial(void)134628cdc3d7Sszhou console_is_usb_serial(void)
134728cdc3d7Sszhou {
134828cdc3d7Sszhou 	char *console;
134928cdc3d7Sszhou 	int len, ret;
135028cdc3d7Sszhou 
135128cdc3d7Sszhou 	if ((len = BOP_GETPROPLEN(ops, "console")) == -1)
135228cdc3d7Sszhou 		return (0);
135328cdc3d7Sszhou 
135428cdc3d7Sszhou 	console = kobj_zalloc(len, KM_WAIT|KM_TMP);
135528cdc3d7Sszhou 	(void) BOP_GETPROP(ops, "console", console);
135628cdc3d7Sszhou 	ret = (strcmp(console, "usb-serial") == 0);
135728cdc3d7Sszhou 	kobj_free(console, len);
135828cdc3d7Sszhou 
135928cdc3d7Sszhou 	return (ret);
136028cdc3d7Sszhou }
136128cdc3d7Sszhou 
13627c478bd9Sstevel@tonic-gate static int
load_kmdb(val_t * bootaux)13637c478bd9Sstevel@tonic-gate load_kmdb(val_t *bootaux)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 	struct modctl *mctl;
13667c478bd9Sstevel@tonic-gate 	struct module *mp;
13677c478bd9Sstevel@tonic-gate 	Sym *sym;
13687c478bd9Sstevel@tonic-gate 
136928cdc3d7Sszhou 	if (console_is_usb_serial()) {
137028cdc3d7Sszhou 		_kobj_printf(ops, "kmdb not loaded "
137128cdc3d7Sszhou 		    "(unsupported on usb serial console)\n");
137228cdc3d7Sszhou 		return (0);
137328cdc3d7Sszhou 	}
137428cdc3d7Sszhou 
13757c478bd9Sstevel@tonic-gate 	_kobj_printf(ops, "Loading kmdb...\n");
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL)
13787c478bd9Sstevel@tonic-gate 		return (-1);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	mctl->mod_busy = 1;
13817c478bd9Sstevel@tonic-gate 	(void) kobj_load_module(mctl, 1);
13827c478bd9Sstevel@tonic-gate 	mctl->mod_busy = 0;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	if ((mp = mctl->mod_mp) == NULL)
13857c478bd9Sstevel@tonic-gate 		return (-1);
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	mp->flags |= KOBJ_PRIM;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0)
13907c478bd9Sstevel@tonic-gate 		return (-1);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	if (boothowto & RB_VERBOSE)
13937c478bd9Sstevel@tonic-gate 		kobj_lm_dump(KOBJ_LM_DEBUGGER);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0)
13967c478bd9Sstevel@tonic-gate 		return (-1);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL)
13997c478bd9Sstevel@tonic-gate 		return (-1);
14007c478bd9Sstevel@tonic-gate 
1401ae115bc7Smrj #ifdef	KOBJ_DEBUG
1402ae115bc7Smrj 	if (kobj_debug & D_DEBUG) {
1403ae115bc7Smrj 		_kobj_printf(ops, "calling kctl_boot_activate() @ 0x%lx\n",
1404ae115bc7Smrj 		    sym->st_value);
1405ae115bc7Smrj 		_kobj_printf(ops, "\tops 0x%p\n", ops);
1406ae115bc7Smrj 		_kobj_printf(ops, "\tromp 0x%p\n", romp);
1407ae115bc7Smrj 	}
1408ae115bc7Smrj #endif
1409ae115bc7Smrj 
14107c478bd9Sstevel@tonic-gate 	if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0,
14117c478bd9Sstevel@tonic-gate 	    (const char **)kobj_kmdb_argv) < 0)
14127c478bd9Sstevel@tonic-gate 		return (-1);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	return (0);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate /*
14187c478bd9Sstevel@tonic-gate  * Return a string listing module dependencies.
14197c478bd9Sstevel@tonic-gate  */
14207c478bd9Sstevel@tonic-gate static char *
depends_on(struct module * mp)14217c478bd9Sstevel@tonic-gate depends_on(struct module *mp)
14227c478bd9Sstevel@tonic-gate {
14237c478bd9Sstevel@tonic-gate 	Sym *sp;
14247c478bd9Sstevel@tonic-gate 	char *depstr, *q;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	/*
14277c478bd9Sstevel@tonic-gate 	 * The module doesn't have a depends_on value, so let's try it the
14287c478bd9Sstevel@tonic-gate 	 * old-fashioned way - via "_depends_on"
14297c478bd9Sstevel@tonic-gate 	 */
14307c478bd9Sstevel@tonic-gate 	if ((sp = lookup_one(mp, "_depends_on")) == NULL)
14317c478bd9Sstevel@tonic-gate 		return (NULL);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	q = (char *)sp->st_value;
14347c478bd9Sstevel@tonic-gate 
143583eb21d9SBayard Bell #ifdef KOBJ_DEBUG
143683eb21d9SBayard Bell 	/*
143783eb21d9SBayard Bell 	 * _depends_on is a deprecated interface, so we warn about its use
143883eb21d9SBayard Bell 	 * irrespective of subsequent processing errors. How else are we going
143983eb21d9SBayard Bell 	 * to be able to deco this interface completely?
144083eb21d9SBayard Bell 	 * Changes initially limited to DEBUG because third-party modules
144183eb21d9SBayard Bell 	 * should be flagged to developers before general use base.
144283eb21d9SBayard Bell 	 */
144383eb21d9SBayard Bell 	_kobj_printf(ops,
144483eb21d9SBayard Bell 	    "Warning: %s uses deprecated _depends_on interface.\n",
144583eb21d9SBayard Bell 	    mp->filename);
144683eb21d9SBayard Bell 	_kobj_printf(ops, "Please notify module developer or vendor.\n");
144783eb21d9SBayard Bell #endif
144883eb21d9SBayard Bell 
14497c478bd9Sstevel@tonic-gate 	/*
14507c478bd9Sstevel@tonic-gate 	 * Idiot checks. Make sure it's
14517c478bd9Sstevel@tonic-gate 	 * in-bounds and NULL terminated.
14527c478bd9Sstevel@tonic-gate 	 */
14537c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') {
14547c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "Error processing dependency for %s\n",
14557c478bd9Sstevel@tonic-gate 		    mp->filename);
14567c478bd9Sstevel@tonic-gate 		return (NULL);
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT);
14607c478bd9Sstevel@tonic-gate 	(void) strcpy(depstr, q);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	return (depstr);
14637c478bd9Sstevel@tonic-gate }
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate void
kobj_getmodinfo(void * xmp,struct modinfo * modinfo)14667c478bd9Sstevel@tonic-gate kobj_getmodinfo(void *xmp, struct modinfo *modinfo)
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	struct module *mp;
14697c478bd9Sstevel@tonic-gate 	mp = (struct module *)xmp;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	modinfo->mi_base = mp->text;
14727c478bd9Sstevel@tonic-gate 	modinfo->mi_size = mp->text_size + mp->data_size;
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate /*
14767c478bd9Sstevel@tonic-gate  * kobj_export_ksyms() performs the following services:
14777c478bd9Sstevel@tonic-gate  *
14787c478bd9Sstevel@tonic-gate  * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
14797c478bd9Sstevel@tonic-gate  * (2) Removes unneeded symbols to save space.
14807c478bd9Sstevel@tonic-gate  * (3) Reduces memory footprint by using VM_BESTFIT allocations.
14817c478bd9Sstevel@tonic-gate  * (4) Makes the symbol table visible to /dev/ksyms.
14827c478bd9Sstevel@tonic-gate  */
14837c478bd9Sstevel@tonic-gate static void
kobj_export_ksyms(struct module * mp)14847c478bd9Sstevel@tonic-gate kobj_export_ksyms(struct module *mp)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate 	Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size);
14877c478bd9Sstevel@tonic-gate 	Sym *sp, *osp;
14887c478bd9Sstevel@tonic-gate 	char *name;
14897c478bd9Sstevel@tonic-gate 	size_t namelen;
14907c478bd9Sstevel@tonic-gate 	struct module *omp;
14917c478bd9Sstevel@tonic-gate 	uint_t nsyms;
14927c478bd9Sstevel@tonic-gate 	size_t symsize = mp->symhdr->sh_entsize;
14937c478bd9Sstevel@tonic-gate 	size_t locals = 1;
14947c478bd9Sstevel@tonic-gate 	size_t strsize;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	/*
14977c478bd9Sstevel@tonic-gate 	 * Make a copy of the original module structure.
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	omp = kobj_alloc(sizeof (struct module), KM_WAIT);
15007c478bd9Sstevel@tonic-gate 	bcopy(mp, omp, sizeof (struct module));
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * Compute the sizes of the new symbol table sections.
15047c478bd9Sstevel@tonic-gate 	 */
15057c478bd9Sstevel@tonic-gate 	for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
15067c478bd9Sstevel@tonic-gate 		if (osp->st_value == 0)
15077c478bd9Sstevel@tonic-gate 			continue;
15087c478bd9Sstevel@tonic-gate 		if (sym_lookup(omp, osp) == NULL)
15097c478bd9Sstevel@tonic-gate 			continue;
15107c478bd9Sstevel@tonic-gate 		name = omp->strings + osp->st_name;
15117c478bd9Sstevel@tonic-gate 		namelen = strlen(name);
15127c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(osp->st_info) == STB_LOCAL)
15137c478bd9Sstevel@tonic-gate 			locals++;
15147c478bd9Sstevel@tonic-gate 		nsyms++;
15157c478bd9Sstevel@tonic-gate 		strsize += namelen + 1;
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	mp->nsyms = nsyms;
15197c478bd9Sstevel@tonic-gate 	mp->hashsize = kobj_gethashsize(mp->nsyms);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	/*
15227c478bd9Sstevel@tonic-gate 	 * ksyms_lock must be held as writer during any operation that
15237c478bd9Sstevel@tonic-gate 	 * modifies ksyms_arena, including allocation from same, and
15247c478bd9Sstevel@tonic-gate 	 * must not be dropped until the arena is vmem_walk()able.
15257c478bd9Sstevel@tonic-gate 	 */
15267c478bd9Sstevel@tonic-gate 	rw_enter(&ksyms_lock, RW_WRITER);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	/*
15297c478bd9Sstevel@tonic-gate 	 * Allocate space for the new section headers (symtab and strtab),
15307c478bd9Sstevel@tonic-gate 	 * symbol table, buckets, chains, and strings.
15317c478bd9Sstevel@tonic-gate 	 */
15327c478bd9Sstevel@tonic-gate 	mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) +
15337c478bd9Sstevel@tonic-gate 	    (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	if (mp->flags & KOBJ_NOKSYMS) {
15367c478bd9Sstevel@tonic-gate 		mp->symspace = kobj_alloc(mp->symsize, KM_WAIT);
15377c478bd9Sstevel@tonic-gate 	} else {
15387c478bd9Sstevel@tonic-gate 		mp->symspace = vmem_alloc(ksyms_arena, mp->symsize,
15397c478bd9Sstevel@tonic-gate 		    VM_BESTFIT | VM_SLEEP);
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 	bzero(mp->symspace, mp->symsize);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	/*
15447c478bd9Sstevel@tonic-gate 	 * Divvy up symspace.
15457c478bd9Sstevel@tonic-gate 	 */
15467c478bd9Sstevel@tonic-gate 	mp->shdrs = mp->symspace;
15477c478bd9Sstevel@tonic-gate 	mp->symhdr = (Shdr *)mp->shdrs;
15487c478bd9Sstevel@tonic-gate 	mp->strhdr = (Shdr *)(mp->symhdr + 1);
15497c478bd9Sstevel@tonic-gate 	mp->symtbl = (char *)(mp->strhdr + 1);
15507c478bd9Sstevel@tonic-gate 	mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize));
15517c478bd9Sstevel@tonic-gate 	mp->chains = (symid_t *)(mp->buckets + mp->hashsize);
15527c478bd9Sstevel@tonic-gate 	mp->strings = (char *)(mp->chains + nsyms);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * Fill in the new section headers (symtab and strtab).
15567c478bd9Sstevel@tonic-gate 	 */
15577c478bd9Sstevel@tonic-gate 	mp->hdr.e_shnum = 2;
15587c478bd9Sstevel@tonic-gate 	mp->symtbl_section = 0;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_type = SHT_SYMTAB;
15617c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_addr = (Addr)mp->symtbl;
15627c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_size = nsyms * symsize;
15637c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_link = 1;
15647c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_info = locals;
15657c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_addralign = sizeof (Addr);
15667c478bd9Sstevel@tonic-gate 	mp->symhdr->sh_entsize = symsize;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	mp->strhdr->sh_type = SHT_STRTAB;
15697c478bd9Sstevel@tonic-gate 	mp->strhdr->sh_addr = (Addr)mp->strings;
15707c478bd9Sstevel@tonic-gate 	mp->strhdr->sh_size = strsize;
15717c478bd9Sstevel@tonic-gate 	mp->strhdr->sh_addralign = 1;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	/*
15747c478bd9Sstevel@tonic-gate 	 * Construct the new symbol table.
15757c478bd9Sstevel@tonic-gate 	 */
15767c478bd9Sstevel@tonic-gate 	for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
15777c478bd9Sstevel@tonic-gate 		if (osp->st_value == 0)
15787c478bd9Sstevel@tonic-gate 			continue;
15797c478bd9Sstevel@tonic-gate 		if (sym_lookup(omp, osp) == NULL)
15807c478bd9Sstevel@tonic-gate 			continue;
15817c478bd9Sstevel@tonic-gate 		name = omp->strings + osp->st_name;
15827c478bd9Sstevel@tonic-gate 		namelen = strlen(name);
15837c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl + symsize * nsyms);
15847c478bd9Sstevel@tonic-gate 		bcopy(osp, sp, symsize);
15857c478bd9Sstevel@tonic-gate 		bcopy(name, mp->strings + strsize, namelen);
15867c478bd9Sstevel@tonic-gate 		sp->st_name = strsize;
15877c478bd9Sstevel@tonic-gate 		sym_insert(mp, name, nsyms);
15887c478bd9Sstevel@tonic-gate 		nsyms++;
15897c478bd9Sstevel@tonic-gate 		strsize += namelen + 1;
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	rw_exit(&ksyms_lock);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/*
15957c478bd9Sstevel@tonic-gate 	 * Free the old section headers -- we'll never need them again.
15967c478bd9Sstevel@tonic-gate 	 */
159766f0458fSkchow 	if (!(mp->flags & KOBJ_PRIM)) {
159866f0458fSkchow 		uint_t	shn;
159966f0458fSkchow 		Shdr	*shp;
160066f0458fSkchow 
160166f0458fSkchow 		for (shn = 1; shn < omp->hdr.e_shnum; shn++) {
160266f0458fSkchow 			shp = (Shdr *)(omp->shdrs + shn * omp->hdr.e_shentsize);
160366f0458fSkchow 			switch (shp->sh_type) {
160466f0458fSkchow 			case SHT_RELA:
160566f0458fSkchow 			case SHT_REL:
160666f0458fSkchow 				if (shp->sh_addr != 0) {
160766f0458fSkchow 					kobj_free((void *)shp->sh_addr,
160866f0458fSkchow 					    shp->sh_size);
160966f0458fSkchow 				}
161066f0458fSkchow 				break;
161166f0458fSkchow 			}
161266f0458fSkchow 		}
16137c478bd9Sstevel@tonic-gate 		kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum);
161466f0458fSkchow 	}
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * Discard the old symbol table and our copy of the module strucure.
16177c478bd9Sstevel@tonic-gate 	 */
16187c478bd9Sstevel@tonic-gate 	if (!(mp->flags & KOBJ_PRIM))
16197c478bd9Sstevel@tonic-gate 		kobj_free(omp->symspace, omp->symsize);
16207c478bd9Sstevel@tonic-gate 	kobj_free(omp, sizeof (struct module));
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate static void
kobj_export_ctf(struct module * mp)16247c478bd9Sstevel@tonic-gate kobj_export_ctf(struct module *mp)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	char *data = mp->ctfdata;
16277c478bd9Sstevel@tonic-gate 	size_t size = mp->ctfsize;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	if (data != NULL) {
16307c478bd9Sstevel@tonic-gate 		if (_moddebug & MODDEBUG_NOCTF) {
16317c478bd9Sstevel@tonic-gate 			mp->ctfdata = NULL;
16327c478bd9Sstevel@tonic-gate 			mp->ctfsize = 0;
16337c478bd9Sstevel@tonic-gate 		} else {
16347c478bd9Sstevel@tonic-gate 			mp->ctfdata = vmem_alloc(ctf_arena, size,
16357c478bd9Sstevel@tonic-gate 			    VM_BESTFIT | VM_SLEEP);
16367c478bd9Sstevel@tonic-gate 			bcopy(data, mp->ctfdata, size);
16377c478bd9Sstevel@tonic-gate 		}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_PRIM))
16407c478bd9Sstevel@tonic-gate 			kobj_free(data, size);
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate void
kobj_export_module(struct module * mp)16457c478bd9Sstevel@tonic-gate kobj_export_module(struct module *mp)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	kobj_export_ksyms(mp);
16487c478bd9Sstevel@tonic-gate 	kobj_export_ctf(mp);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	mp->flags |= KOBJ_EXPORTED;
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate static int
process_dynamic(struct module * mp,char * dyndata,char * strdata)16547c478bd9Sstevel@tonic-gate process_dynamic(struct module *mp, char *dyndata, char *strdata)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	char *path = NULL, *depstr = NULL;
16577c478bd9Sstevel@tonic-gate 	int allocsize = 0, osize = 0, nsize = 0;
16587c478bd9Sstevel@tonic-gate 	char *libname, *tmp;
16597c478bd9Sstevel@tonic-gate 	int lsize;
16607c478bd9Sstevel@tonic-gate 	Dyn *dynp;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) {
16637c478bd9Sstevel@tonic-gate 		switch (dynp->d_tag) {
16647c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
16657c478bd9Sstevel@tonic-gate 			/*
16667c478bd9Sstevel@tonic-gate 			 * Read the DT_NEEDED entries, expanding the macros they
16677c478bd9Sstevel@tonic-gate 			 * contain (if any), and concatenating them into a
16687c478bd9Sstevel@tonic-gate 			 * single space-separated dependency list.
16697c478bd9Sstevel@tonic-gate 			 */
16707c478bd9Sstevel@tonic-gate 			libname = (ulong_t)dynp->d_un.d_ptr + strdata;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 			if (strchr(libname, '$') != NULL) {
16737c478bd9Sstevel@tonic-gate 				char *_lib;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 				if (path == NULL)
16767c478bd9Sstevel@tonic-gate 					path = kobj_alloc(MAXPATHLEN, KM_WAIT);
16777c478bd9Sstevel@tonic-gate 				if ((_lib = expand_libmacro(libname, path,
16787c478bd9Sstevel@tonic-gate 				    path)) != NULL)
16797c478bd9Sstevel@tonic-gate 					libname = _lib;
16807c478bd9Sstevel@tonic-gate 				else {
16817c478bd9Sstevel@tonic-gate 					_kobj_printf(ops, "krtld: "
16827c478bd9Sstevel@tonic-gate 					    "process_dynamic: failed to expand "
16837c478bd9Sstevel@tonic-gate 					    "%s\n", libname);
16847c478bd9Sstevel@tonic-gate 				}
16857c478bd9Sstevel@tonic-gate 			}
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 			lsize = strlen(libname);
16887c478bd9Sstevel@tonic-gate 			nsize += lsize;
16897c478bd9Sstevel@tonic-gate 			if (nsize + 1 > allocsize) {
16907c478bd9Sstevel@tonic-gate 				tmp = kobj_alloc(allocsize + MAXPATHLEN,
16917c478bd9Sstevel@tonic-gate 				    KM_WAIT);
16927c478bd9Sstevel@tonic-gate 				if (depstr != NULL) {
16937c478bd9Sstevel@tonic-gate 					bcopy(depstr, tmp, osize);
16947c478bd9Sstevel@tonic-gate 					kobj_free(depstr, allocsize);
16957c478bd9Sstevel@tonic-gate 				}
16967c478bd9Sstevel@tonic-gate 				depstr = tmp;
16977c478bd9Sstevel@tonic-gate 				allocsize += MAXPATHLEN;
16987c478bd9Sstevel@tonic-gate 			}
16997c478bd9Sstevel@tonic-gate 			bcopy(libname, depstr + osize, lsize);
17007c478bd9Sstevel@tonic-gate 			*(depstr + nsize) = ' '; /* separator */
17017c478bd9Sstevel@tonic-gate 			nsize++;
17027c478bd9Sstevel@tonic-gate 			osize = nsize;
17037c478bd9Sstevel@tonic-gate 			break;
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 		case DT_FLAGS_1:
17067c478bd9Sstevel@tonic-gate 			if (dynp->d_un.d_val & DF_1_IGNMULDEF)
17077c478bd9Sstevel@tonic-gate 				mp->flags |= KOBJ_IGNMULDEF;
17087c478bd9Sstevel@tonic-gate 			if (dynp->d_un.d_val & DF_1_NOKSYMS)
17097c478bd9Sstevel@tonic-gate 				mp->flags |= KOBJ_NOKSYMS;
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 			break;
17127c478bd9Sstevel@tonic-gate 		}
17137c478bd9Sstevel@tonic-gate 	}
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	/*
17167c478bd9Sstevel@tonic-gate 	 * finish up the depends string (if any)
17177c478bd9Sstevel@tonic-gate 	 */
17187c478bd9Sstevel@tonic-gate 	if (depstr != NULL) {
1719da6c28aaSamw 		*(depstr + nsize - 1) = '\0'; /* overwrite separator w/term */
17207c478bd9Sstevel@tonic-gate 		if (path != NULL)
17217c478bd9Sstevel@tonic-gate 			kobj_free(path, MAXPATHLEN);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		tmp = kobj_alloc(nsize, KM_WAIT);
17247c478bd9Sstevel@tonic-gate 		bcopy(depstr, tmp, nsize);
17257c478bd9Sstevel@tonic-gate 		kobj_free(depstr, allocsize);
17267c478bd9Sstevel@tonic-gate 		depstr = tmp;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 		mp->depends_on = depstr;
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	return (0);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate static int
do_dynamic(struct module * mp,struct _buf * file)17357c478bd9Sstevel@tonic-gate do_dynamic(struct module *mp, struct _buf *file)
17367c478bd9Sstevel@tonic-gate {
17377c478bd9Sstevel@tonic-gate 	Shdr *dshp, *dstrp, *shp;
17387c478bd9Sstevel@tonic-gate 	char *dyndata, *dstrdata;
17397c478bd9Sstevel@tonic-gate 	int dshn, shn, rc;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	/* find and validate the dynamic section (if any) */
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) {
17447c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
17457c478bd9Sstevel@tonic-gate 		switch (shp->sh_type) {
17467c478bd9Sstevel@tonic-gate 		case SHT_DYNAMIC:
17477c478bd9Sstevel@tonic-gate 			if (dshp != NULL) {
17487c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "krtld: get_dynamic: %s, ",
17497c478bd9Sstevel@tonic-gate 				    mp->filename);
17507c478bd9Sstevel@tonic-gate 				_kobj_printf(ops,
17517c478bd9Sstevel@tonic-gate 				    "multiple dynamic sections\n");
17527c478bd9Sstevel@tonic-gate 				return (-1);
17537c478bd9Sstevel@tonic-gate 			} else {
17547c478bd9Sstevel@tonic-gate 				dshp = shp;
17557c478bd9Sstevel@tonic-gate 				dshn = shn;
17567c478bd9Sstevel@tonic-gate 			}
17577c478bd9Sstevel@tonic-gate 			break;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	if (dshp == NULL)
17627c478bd9Sstevel@tonic-gate 		return (0);
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	if (dshp->sh_link > mp->hdr.e_shnum) {
17657c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
17667c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link);
17677c478bd9Sstevel@tonic-gate 		return (-1);
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 	dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	if (dstrp->sh_type != SHT_STRTAB) {
17727c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
17737c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "sh_link not a string table for section %d\n",
17747c478bd9Sstevel@tonic-gate 		    dshn);
17757c478bd9Sstevel@tonic-gate 		return (-1);
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/* read it from disk */
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP);
17817c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) {
17827c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
17837c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "error reading section %d\n", dshn);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		kobj_free(dyndata, dshp->sh_size);
17867c478bd9Sstevel@tonic-gate 		return (-1);
17877c478bd9Sstevel@tonic-gate 	}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP);
17907c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, dstrdata, dstrp->sh_size,
17917c478bd9Sstevel@tonic-gate 	    dstrp->sh_offset) < 0) {
17927c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
17937c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "error reading section %d\n", dshp->sh_link);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 		kobj_free(dyndata, dshp->sh_size);
17967c478bd9Sstevel@tonic-gate 		kobj_free(dstrdata, dstrp->sh_size);
17977c478bd9Sstevel@tonic-gate 		return (-1);
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	/* pull the interesting pieces out */
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	rc = process_dynamic(mp, dyndata, dstrdata);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	kobj_free(dyndata, dshp->sh_size);
18057c478bd9Sstevel@tonic-gate 	kobj_free(dstrdata, dstrp->sh_size);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	return (rc);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate void
kobj_set_ctf(struct module * mp,caddr_t data,size_t size)18117c478bd9Sstevel@tonic-gate kobj_set_ctf(struct module *mp, caddr_t data, size_t size)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	if (!standalone) {
18147c478bd9Sstevel@tonic-gate 		if (mp->ctfdata != NULL) {
18157c478bd9Sstevel@tonic-gate 			if (vmem_contains(ctf_arena, mp->ctfdata,
1816b1b8ab34Slling 			    mp->ctfsize)) {
18177c478bd9Sstevel@tonic-gate 				vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
18187c478bd9Sstevel@tonic-gate 			} else {
18197c478bd9Sstevel@tonic-gate 				kobj_free(mp->ctfdata, mp->ctfsize);
18207c478bd9Sstevel@tonic-gate 			}
18217c478bd9Sstevel@tonic-gate 		}
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * The order is very important here.  We need to make sure that
18267c478bd9Sstevel@tonic-gate 	 * consumers, at any given instant, see a consistent state.  We'd
18277c478bd9Sstevel@tonic-gate 	 * rather they see no CTF data than the address of one buffer and the
18287c478bd9Sstevel@tonic-gate 	 * size of another.
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	mp->ctfdata = NULL;
18317c478bd9Sstevel@tonic-gate 	membar_producer();
18327c478bd9Sstevel@tonic-gate 	mp->ctfsize = size;
18337c478bd9Sstevel@tonic-gate 	mp->ctfdata = data;
18347c478bd9Sstevel@tonic-gate 	membar_producer();
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate int
kobj_load_module(struct modctl * modp,int use_path)18387c478bd9Sstevel@tonic-gate kobj_load_module(struct modctl *modp, int use_path)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	char *filename = modp->mod_filename;
18417c478bd9Sstevel@tonic-gate 	char *modname = modp->mod_modname;
18427c478bd9Sstevel@tonic-gate 	int i;
18437c478bd9Sstevel@tonic-gate 	int n;
18447c478bd9Sstevel@tonic-gate 	struct _buf *file;
18457c478bd9Sstevel@tonic-gate 	struct module *mp = NULL;
18467c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX
18477c478bd9Sstevel@tonic-gate 	int no_suffixdir_drv = 0;
18487c478bd9Sstevel@tonic-gate #endif
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	/*
18537c478bd9Sstevel@tonic-gate 	 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
18547c478bd9Sstevel@tonic-gate 	 * kmdb contains a bunch of symbols with well-known names, symbols
18557c478bd9Sstevel@tonic-gate 	 * which will mask the real versions, thus causing no end of trouble
18567c478bd9Sstevel@tonic-gate 	 * for mdb.
18577c478bd9Sstevel@tonic-gate 	 */
18587c478bd9Sstevel@tonic-gate 	if (strcmp(modp->mod_modname, "kmdbmod") == 0)
18597c478bd9Sstevel@tonic-gate 		mp->flags |= KOBJ_NOKSYMS;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	file = kobj_open_path(filename, use_path, 1);
18627c478bd9Sstevel@tonic-gate 	if (file == (struct _buf *)-1) {
18637c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX
18647c478bd9Sstevel@tonic-gate 		file = kobj_open_path(filename, use_path, 0);
18657c478bd9Sstevel@tonic-gate #endif
18667c478bd9Sstevel@tonic-gate 		if (file == (struct _buf *)-1) {
18677c478bd9Sstevel@tonic-gate 			kobj_free(mp, sizeof (*mp));
18687c478bd9Sstevel@tonic-gate 			goto bad;
18697c478bd9Sstevel@tonic-gate 		}
18707c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX
18717c478bd9Sstevel@tonic-gate 		/*
18727c478bd9Sstevel@tonic-gate 		 * There is no driver module in the ISA specific (suffix)
18737c478bd9Sstevel@tonic-gate 		 * subdirectory but there is a module in the parent directory.
18747c478bd9Sstevel@tonic-gate 		 */
18757c478bd9Sstevel@tonic-gate 		if (strncmp(filename, "drv/", 4) == 0) {
18767c478bd9Sstevel@tonic-gate 			no_suffixdir_drv = 1;
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate #endif
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT);
18827c478bd9Sstevel@tonic-gate 	(void) strcpy(mp->filename, file->_name);
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) {
18857c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "kobj_load_module: %s read header failed\n",
18867c478bd9Sstevel@tonic-gate 		    modname);
18877c478bd9Sstevel@tonic-gate 		kobj_free(mp->filename, strlen(file->_name) + 1);
18887c478bd9Sstevel@tonic-gate 		kobj_free(mp, sizeof (*mp));
18897c478bd9Sstevel@tonic-gate 		goto bad;
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate 	for (i = 0; i < SELFMAG; i++) {
18927c478bd9Sstevel@tonic-gate 		if (mp->hdr.e_ident[i] != ELFMAG[i]) {
18937c478bd9Sstevel@tonic-gate 			if (_moddebug & MODDEBUG_ERRMSG)
18947c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "%s not an elf module\n",
18957c478bd9Sstevel@tonic-gate 				    modname);
18967c478bd9Sstevel@tonic-gate 			kobj_free(mp->filename, strlen(file->_name) + 1);
18977c478bd9Sstevel@tonic-gate 			kobj_free(mp, sizeof (*mp));
18987c478bd9Sstevel@tonic-gate 			goto bad;
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 	/*
19027c478bd9Sstevel@tonic-gate 	 * It's ELF, but is it our ISA?  Interpreting the header
19037c478bd9Sstevel@tonic-gate 	 * from a file for a byte-swapped ISA could cause a huge
19047c478bd9Sstevel@tonic-gate 	 * and unsatisfiable value to be passed to kobj_alloc below
19057c478bd9Sstevel@tonic-gate 	 * and therefore hang booting.
19067c478bd9Sstevel@tonic-gate 	 */
19077c478bd9Sstevel@tonic-gate 	if (!elf_mach_ok(&mp->hdr)) {
19087c478bd9Sstevel@tonic-gate 		if (_moddebug & MODDEBUG_ERRMSG)
19097c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s not an elf module for this ISA\n",
19107c478bd9Sstevel@tonic-gate 			    modname);
19117c478bd9Sstevel@tonic-gate 		kobj_free(mp->filename, strlen(file->_name) + 1);
19127c478bd9Sstevel@tonic-gate 		kobj_free(mp, sizeof (*mp));
19137c478bd9Sstevel@tonic-gate #ifdef MODDIR_SUFFIX
19147c478bd9Sstevel@tonic-gate 		/*
19157c478bd9Sstevel@tonic-gate 		 * The driver mod is not in the ISA specific subdirectory
19167c478bd9Sstevel@tonic-gate 		 * and the module in the parent directory is not our ISA.
19177c478bd9Sstevel@tonic-gate 		 * If it is our ISA, for now we will silently succeed.
19187c478bd9Sstevel@tonic-gate 		 */
19197c478bd9Sstevel@tonic-gate 		if (no_suffixdir_drv == 1) {
19207c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "?NOTICE: %s: 64-bit driver module"
19217c478bd9Sstevel@tonic-gate 			    " not found\n", modname);
19227c478bd9Sstevel@tonic-gate 		}
19237c478bd9Sstevel@tonic-gate #endif
19247c478bd9Sstevel@tonic-gate 		goto bad;
19257c478bd9Sstevel@tonic-gate 	}
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	/*
19287c478bd9Sstevel@tonic-gate 	 * All modules, save for unix, should be relocatable (as opposed to
19297c478bd9Sstevel@tonic-gate 	 * dynamic).  Dynamic modules come with PLTs and GOTs, which can't
19307c478bd9Sstevel@tonic-gate 	 * currently be processed by krtld.
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	if (mp->hdr.e_type != ET_REL) {
19337c478bd9Sstevel@tonic-gate 		if (_moddebug & MODDEBUG_ERRMSG)
19347c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s isn't a relocatable (ET_REL) "
19357c478bd9Sstevel@tonic-gate 			    "module\n", modname);
19367c478bd9Sstevel@tonic-gate 		kobj_free(mp->filename, strlen(file->_name) + 1);
19377c478bd9Sstevel@tonic-gate 		kobj_free(mp, sizeof (*mp));
19387c478bd9Sstevel@tonic-gate 		goto bad;
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
19427c478bd9Sstevel@tonic-gate 	mp->shdrs = kobj_alloc(n, KM_WAIT);
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) {
19457c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "kobj_load_module: %s error reading "
19467c478bd9Sstevel@tonic-gate 		    "section headers\n", modname);
19477c478bd9Sstevel@tonic-gate 		kobj_free(mp->shdrs, n);
19487c478bd9Sstevel@tonic-gate 		kobj_free(mp->filename, strlen(file->_name) + 1);
19497c478bd9Sstevel@tonic-gate 		kobj_free(mp, sizeof (*mp));
19507c478bd9Sstevel@tonic-gate 		goto bad;
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	kobj_notify(KOBJ_NOTIFY_MODLOADING, modp);
19547c478bd9Sstevel@tonic-gate 	module_assign(modp, mp);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/* read in sections */
19577c478bd9Sstevel@tonic-gate 	if (get_progbits(mp, file) < 0) {
19587c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "%s error reading sections\n", modname);
19597c478bd9Sstevel@tonic-gate 		goto bad;
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	if (do_dynamic(mp, file) < 0) {
19637c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "%s error reading dynamic section\n",
19647c478bd9Sstevel@tonic-gate 		    modname);
19657c478bd9Sstevel@tonic-gate 		goto bad;
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	modp->mod_text = mp->text;
19697c478bd9Sstevel@tonic-gate 	modp->mod_text_size = mp->text_size;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	/* read in symbols; adjust values for each section's real address */
19727c478bd9Sstevel@tonic-gate 	if (get_syms(mp, file) < 0) {
19737c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "%s error reading symbols\n",
19747c478bd9Sstevel@tonic-gate 		    modname);
19757c478bd9Sstevel@tonic-gate 		goto bad;
19767c478bd9Sstevel@tonic-gate 	}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	/*
19797c478bd9Sstevel@tonic-gate 	 * If we didn't dependency information from the dynamic section, look
19807c478bd9Sstevel@tonic-gate 	 * for it the old-fashioned way.
19817c478bd9Sstevel@tonic-gate 	 */
19827c478bd9Sstevel@tonic-gate 	if (mp->depends_on == NULL)
19837c478bd9Sstevel@tonic-gate 		mp->depends_on = depends_on(mp);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	if (get_ctf(mp, file) < 0) {
19867c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "%s debug information will not "
19877c478bd9Sstevel@tonic-gate 		    "be available\n", modname);
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	/* primary kernel modules do not have a signature section */
19917c478bd9Sstevel@tonic-gate 	if (!(mp->flags & KOBJ_PRIM))
19927c478bd9Sstevel@tonic-gate 		get_signature(mp, file);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
19957c478bd9Sstevel@tonic-gate 	if (kobj_debug & D_LOADING) {
19967c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: file=%s\n", mp->filename);
19977c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "\ttext:0x%p", mp->text);
1998c61137dfSJohn Levon 		_kobj_printf(ops, " size: 0x%lx\n", mp->text_size);
19997c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "\tdata:0x%p", mp->data);
2000c61137dfSJohn Levon 		_kobj_printf(ops, " dsize: 0x%lx\n", mp->data_size);
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate #endif /* KOBJ_DEBUG */
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	/*
20057c478bd9Sstevel@tonic-gate 	 * For primary kernel modules, we defer
20067c478bd9Sstevel@tonic-gate 	 * symbol resolution and relocation until
20077c478bd9Sstevel@tonic-gate 	 * all primary objects have been loaded.
20087c478bd9Sstevel@tonic-gate 	 */
20097c478bd9Sstevel@tonic-gate 	if (!standalone) {
20107c478bd9Sstevel@tonic-gate 		int ddrval, dcrval;
20117c478bd9Sstevel@tonic-gate 		char *dependent_modname;
20127c478bd9Sstevel@tonic-gate 		/* load all dependents */
20137c478bd9Sstevel@tonic-gate 		dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT);
20147c478bd9Sstevel@tonic-gate 		ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 		/*
20177c478bd9Sstevel@tonic-gate 		 * resolve undefined and common symbols,
20187c478bd9Sstevel@tonic-gate 		 * also allocates common space
20197c478bd9Sstevel@tonic-gate 		 */
20207c478bd9Sstevel@tonic-gate 		if ((dcrval = do_common(mp)) < 0) {
20217c478bd9Sstevel@tonic-gate 			switch (dcrval) {
20227c478bd9Sstevel@tonic-gate 			case DOSYM_UNSAFE:
20237c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "WARNING: mod_load: "
20247c478bd9Sstevel@tonic-gate 				    "MT-unsafe module '%s' rejected\n",
20257c478bd9Sstevel@tonic-gate 				    modname);
20267c478bd9Sstevel@tonic-gate 				break;
20277c478bd9Sstevel@tonic-gate 			case DOSYM_UNDEF:
20287c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "WARNING: mod_load: "
20297c478bd9Sstevel@tonic-gate 				    "cannot load module '%s'\n",
20307c478bd9Sstevel@tonic-gate 				    modname);
20317c478bd9Sstevel@tonic-gate 				if (ddrval == -1) {
20327c478bd9Sstevel@tonic-gate 					_kobj_printf(ops, "WARNING: %s: ",
20337c478bd9Sstevel@tonic-gate 					    modname);
20347c478bd9Sstevel@tonic-gate 					_kobj_printf(ops,
20357c478bd9Sstevel@tonic-gate 					    "unable to resolve dependency, "
20367c478bd9Sstevel@tonic-gate 					    "module '%s' not found\n",
20377c478bd9Sstevel@tonic-gate 					    dependent_modname);
20387c478bd9Sstevel@tonic-gate 				}
20397c478bd9Sstevel@tonic-gate 				break;
20407c478bd9Sstevel@tonic-gate 			}
20417c478bd9Sstevel@tonic-gate 		}
20427c478bd9Sstevel@tonic-gate 		kobj_free(dependent_modname, MODMAXNAMELEN);
20437c478bd9Sstevel@tonic-gate 		if (dcrval < 0)
20447c478bd9Sstevel@tonic-gate 			goto bad;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 		/* process relocation tables */
20477c478bd9Sstevel@tonic-gate 		if (do_relocations(mp) < 0) {
20487c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s error doing relocations\n",
20497c478bd9Sstevel@tonic-gate 			    modname);
20507c478bd9Sstevel@tonic-gate 			goto bad;
20517c478bd9Sstevel@tonic-gate 		}
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 		if (mp->destination) {
20547c478bd9Sstevel@tonic-gate 			off_t	off = (uintptr_t)mp->destination & PAGEOFFSET;
20557c478bd9Sstevel@tonic-gate 			caddr_t	base = (caddr_t)mp->destination - off;
20567c478bd9Sstevel@tonic-gate 			size_t	size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 			hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK);
20597c478bd9Sstevel@tonic-gate 			vmem_free(heap_arena, base, size);
20607c478bd9Sstevel@tonic-gate 		}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 		/* sync_instruction_memory */
20637c478bd9Sstevel@tonic-gate 		kobj_sync_instruction_memory(mp->text, mp->text_size);
20647c478bd9Sstevel@tonic-gate 		kobj_export_module(mp);
20657c478bd9Sstevel@tonic-gate 		kobj_notify(KOBJ_NOTIFY_MODLOADED, modp);
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
20687c478bd9Sstevel@tonic-gate 	return (0);
20697c478bd9Sstevel@tonic-gate bad:
20707c478bd9Sstevel@tonic-gate 	if (file != (struct _buf *)-1)
20717c478bd9Sstevel@tonic-gate 		kobj_close_file(file);
20727c478bd9Sstevel@tonic-gate 	if (modp->mod_mp != NULL)
20737c478bd9Sstevel@tonic-gate 		free_module_data(modp->mod_mp);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	module_assign(modp, NULL);
20767c478bd9Sstevel@tonic-gate 	return ((file == (struct _buf *)-1) ? ENOENT : EINVAL);
20777c478bd9Sstevel@tonic-gate }
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate int
kobj_load_primary_module(struct modctl * modp)20807c478bd9Sstevel@tonic-gate kobj_load_primary_module(struct modctl *modp)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	struct modctl *dep;
20837c478bd9Sstevel@tonic-gate 	struct module *mp;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	if (kobj_load_module(modp, 0) != 0)
20867c478bd9Sstevel@tonic-gate 		return (-1);
20877c478bd9Sstevel@tonic-gate 
2088584b574aSToomas Soome 	dep = NULL;
20897c478bd9Sstevel@tonic-gate 	mp = modp->mod_mp;
20907c478bd9Sstevel@tonic-gate 	mp->flags |= KOBJ_PRIM;
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	/* Bind new module to its dependents */
20937c478bd9Sstevel@tonic-gate 	if (mp->depends_on != NULL && (dep =
20947c478bd9Sstevel@tonic-gate 	    mod_already_loaded(mp->depends_on)) == NULL) {
20957c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
20967c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_DEBUG) {
20977c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: failed to resolve deps "
20987c478bd9Sstevel@tonic-gate 			    "for primary %s\n", modp->mod_modname);
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate #endif
21017c478bd9Sstevel@tonic-gate 		return (-1);
21027c478bd9Sstevel@tonic-gate 	}
21037c478bd9Sstevel@tonic-gate 
2104584b574aSToomas Soome 	if (dep != NULL)
2105584b574aSToomas Soome 		add_dependent(mp, dep->mod_mp);
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	/*
21087c478bd9Sstevel@tonic-gate 	 * Relocate it.  This module may not be part of a link map, so we
21097c478bd9Sstevel@tonic-gate 	 * can't use bind_primary.
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 ||
21127c478bd9Sstevel@tonic-gate 	    do_relocations(mp) < 0) {
21137c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
21147c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_DEBUG) {
21157c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: failed to relocate "
21167c478bd9Sstevel@tonic-gate 			    "primary %s\n", modp->mod_modname);
21177c478bd9Sstevel@tonic-gate 		}
21187c478bd9Sstevel@tonic-gate #endif
21197c478bd9Sstevel@tonic-gate 		return (-1);
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	return (0);
21237c478bd9Sstevel@tonic-gate }
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate static void
module_assign(struct modctl * cp,struct module * mp)21267c478bd9Sstevel@tonic-gate module_assign(struct modctl *cp, struct module *mp)
21277c478bd9Sstevel@tonic-gate {
21287c478bd9Sstevel@tonic-gate 	if (standalone) {
21297c478bd9Sstevel@tonic-gate 		cp->mod_mp = mp;
21307c478bd9Sstevel@tonic-gate 		return;
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
21337c478bd9Sstevel@tonic-gate 	cp->mod_mp = mp;
21347c478bd9Sstevel@tonic-gate 	cp->mod_gencount++;
21357c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
21367c478bd9Sstevel@tonic-gate }
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate void
kobj_unload_module(struct modctl * modp)21397c478bd9Sstevel@tonic-gate kobj_unload_module(struct modctl *modp)
21407c478bd9Sstevel@tonic-gate {
21417c478bd9Sstevel@tonic-gate 	struct module *mp = modp->mod_mp;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) {
21447c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "text for %s ", mp->filename);
21457c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "was at %p\n", mp->text);
21467c478bd9Sstevel@tonic-gate 		mp->text = NULL;	/* don't actually free it */
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp);
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	/*
21527c478bd9Sstevel@tonic-gate 	 * Null out mod_mp first, so consumers (debuggers) know not to look
21537c478bd9Sstevel@tonic-gate 	 * at the module structure any more.
21547c478bd9Sstevel@tonic-gate 	 */
21557c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
21567c478bd9Sstevel@tonic-gate 	modp->mod_mp = NULL;
21577c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp);
21607c478bd9Sstevel@tonic-gate 	free_module_data(mp);
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate static void
free_module_data(struct module * mp)21647c478bd9Sstevel@tonic-gate free_module_data(struct module *mp)
21657c478bd9Sstevel@tonic-gate {
21667c478bd9Sstevel@tonic-gate 	struct module_list *lp, *tmp;
2167425251fdSSam Gwydir 	hotinline_desc_t *hid, *next;
21687c478bd9Sstevel@tonic-gate 	int ksyms_exported = 0;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	lp = mp->head;
21717c478bd9Sstevel@tonic-gate 	while (lp) {
21727c478bd9Sstevel@tonic-gate 		tmp = lp;
21737c478bd9Sstevel@tonic-gate 		lp = lp->next;
21747c478bd9Sstevel@tonic-gate 		kobj_free((char *)tmp, sizeof (*tmp));
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 
2177425251fdSSam Gwydir 	/* release hotinlines */
2178425251fdSSam Gwydir 	hid = mp->hi_calls;
2179425251fdSSam Gwydir 	while (hid != NULL) {
2180425251fdSSam Gwydir 		next = hid->hid_next;
2181425251fdSSam Gwydir 		kobj_free(hid->hid_symname, strlen(hid->hid_symname) + 1);
2182425251fdSSam Gwydir 		kobj_free(hid, sizeof (hotinline_desc_t));
2183425251fdSSam Gwydir 		hid = next;
2184425251fdSSam Gwydir 	}
2185425251fdSSam Gwydir 
21867c478bd9Sstevel@tonic-gate 	rw_enter(&ksyms_lock, RW_WRITER);
21877c478bd9Sstevel@tonic-gate 	if (mp->symspace) {
21887c478bd9Sstevel@tonic-gate 		if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) {
21897c478bd9Sstevel@tonic-gate 			vmem_free(ksyms_arena, mp->symspace, mp->symsize);
21907c478bd9Sstevel@tonic-gate 			ksyms_exported = 1;
21917c478bd9Sstevel@tonic-gate 		} else {
21927c478bd9Sstevel@tonic-gate 			if (mp->flags & KOBJ_NOKSYMS)
21937c478bd9Sstevel@tonic-gate 				ksyms_exported = 1;
21947c478bd9Sstevel@tonic-gate 			kobj_free(mp->symspace, mp->symsize);
21957c478bd9Sstevel@tonic-gate 		}
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 	rw_exit(&ksyms_lock);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	if (mp->ctfdata) {
22007c478bd9Sstevel@tonic-gate 		if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize))
22017c478bd9Sstevel@tonic-gate 			vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
22027c478bd9Sstevel@tonic-gate 		else
22037c478bd9Sstevel@tonic-gate 			kobj_free(mp->ctfdata, mp->ctfsize);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	if (mp->sigdata)
22077c478bd9Sstevel@tonic-gate 		kobj_free(mp->sigdata, mp->sigsize);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	/*
22107c478bd9Sstevel@tonic-gate 	 * We did not get far enough into kobj_export_ksyms() to free allocated
22117c478bd9Sstevel@tonic-gate 	 * buffers because we encounted error conditions. Free the buffers.
22127c478bd9Sstevel@tonic-gate 	 */
22137c478bd9Sstevel@tonic-gate 	if ((ksyms_exported == 0) && (mp->shdrs != NULL)) {
22147c478bd9Sstevel@tonic-gate 		uint_t shn;
22157c478bd9Sstevel@tonic-gate 		Shdr *shp;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
22187c478bd9Sstevel@tonic-gate 			shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
22197c478bd9Sstevel@tonic-gate 			switch (shp->sh_type) {
22207c478bd9Sstevel@tonic-gate 			case SHT_RELA:
22217c478bd9Sstevel@tonic-gate 			case SHT_REL:
22227c478bd9Sstevel@tonic-gate 				if (shp->sh_addr != 0)
22237c478bd9Sstevel@tonic-gate 					kobj_free((void *)shp->sh_addr,
22247c478bd9Sstevel@tonic-gate 					    shp->sh_size);
22257c478bd9Sstevel@tonic-gate 				break;
22267c478bd9Sstevel@tonic-gate 			}
22277c478bd9Sstevel@tonic-gate 		}
22283df2e8b2SRobert Mustacchi 
22297c478bd9Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_PRIM)) {
22307c478bd9Sstevel@tonic-gate 			kobj_free(mp->shdrs,
22317c478bd9Sstevel@tonic-gate 			    mp->hdr.e_shentsize * mp->hdr.e_shnum);
22327c478bd9Sstevel@tonic-gate 		}
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	if (mp->bss)
22367c478bd9Sstevel@tonic-gate 		vmem_free(data_arena, (void *)mp->bss, mp->bss_size);
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	if (mp->fbt_tab)
22397c478bd9Sstevel@tonic-gate 		kobj_texthole_free(mp->fbt_tab, mp->fbt_size);
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	if (mp->textwin_base)
22427c478bd9Sstevel@tonic-gate 		kobj_textwin_free(mp);
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	if (mp->sdt_probes != NULL) {
22457c478bd9Sstevel@tonic-gate 		sdt_probedesc_t *sdp = mp->sdt_probes, *next;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 		while (sdp != NULL) {
22487c478bd9Sstevel@tonic-gate 			next = sdp->sdpd_next;
22497c478bd9Sstevel@tonic-gate 			kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1);
22507c478bd9Sstevel@tonic-gate 			kobj_free(sdp, sizeof (sdt_probedesc_t));
22517c478bd9Sstevel@tonic-gate 			sdp = next;
22527c478bd9Sstevel@tonic-gate 		}
22537c478bd9Sstevel@tonic-gate 	}
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	if (mp->sdt_tab)
22567c478bd9Sstevel@tonic-gate 		kobj_texthole_free(mp->sdt_tab, mp->sdt_size);
22577c478bd9Sstevel@tonic-gate 	if (mp->text)
22587c478bd9Sstevel@tonic-gate 		vmem_free(text_arena, mp->text, mp->text_size);
22597c478bd9Sstevel@tonic-gate 	if (mp->data)
22607c478bd9Sstevel@tonic-gate 		vmem_free(data_arena, mp->data, mp->data_size);
22617c478bd9Sstevel@tonic-gate 	if (mp->depends_on)
22627c478bd9Sstevel@tonic-gate 		kobj_free(mp->depends_on, strlen(mp->depends_on)+1);
22637c478bd9Sstevel@tonic-gate 	if (mp->filename)
22647c478bd9Sstevel@tonic-gate 		kobj_free(mp->filename, strlen(mp->filename)+1);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	kobj_free((char *)mp, sizeof (*mp));
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate static int
get_progbits(struct module * mp,struct _buf * file)22707c478bd9Sstevel@tonic-gate get_progbits(struct module *mp, struct _buf *file)
22717c478bd9Sstevel@tonic-gate {
22727c478bd9Sstevel@tonic-gate 	struct proginfo *tp, *dp, *sdp;
22737c478bd9Sstevel@tonic-gate 	Shdr *shp;
22747c478bd9Sstevel@tonic-gate 	reloc_dest_t dest = NULL;
22757c478bd9Sstevel@tonic-gate 	uintptr_t bits_ptr;
2276ba2be530Sab 	uintptr_t text = 0, data, textptr;
22777c478bd9Sstevel@tonic-gate 	uint_t shn;
22787c478bd9Sstevel@tonic-gate 	int err = -1;
22797c478bd9Sstevel@tonic-gate 
2280986fd29aSsetje 	tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2281986fd29aSsetje 	dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2282986fd29aSsetje 	sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
22837c478bd9Sstevel@tonic-gate 	/*
22847c478bd9Sstevel@tonic-gate 	 * loop through sections to find out how much space we need
22857c478bd9Sstevel@tonic-gate 	 * for text, data, (also bss that is already assigned)
22867c478bd9Sstevel@tonic-gate 	 */
22877c478bd9Sstevel@tonic-gate 	if (get_progbits_size(mp, tp, dp, sdp) < 0)
22887c478bd9Sstevel@tonic-gate 		goto done;
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	mp->text_size = tp->size;
22917c478bd9Sstevel@tonic-gate 	mp->data_size = dp->size;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	if (standalone) {
2294ae115bc7Smrj 		caddr_t limit = _data;
2295ae115bc7Smrj 
2296ae115bc7Smrj 		if (lg_pagesize && _text + lg_pagesize < limit)
2297ae115bc7Smrj 			limit = _text + lg_pagesize;
2298ae115bc7Smrj 
22997c478bd9Sstevel@tonic-gate 		mp->text = kobj_segbrk(&_etext, mp->text_size,
2300b1b8ab34Slling 		    tp->align, limit);
23017c478bd9Sstevel@tonic-gate 		/*
23027c478bd9Sstevel@tonic-gate 		 * If we can't grow the text segment, try the
23037c478bd9Sstevel@tonic-gate 		 * data segment before failing.
23047c478bd9Sstevel@tonic-gate 		 */
23057c478bd9Sstevel@tonic-gate 		if (mp->text == NULL) {
23067c478bd9Sstevel@tonic-gate 			mp->text = kobj_segbrk(&_edata, mp->text_size,
2307b1b8ab34Slling 			    tp->align, 0);
23087c478bd9Sstevel@tonic-gate 		}
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 		mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0);
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		if (mp->text == NULL || mp->data == NULL)
23137c478bd9Sstevel@tonic-gate 			goto done;
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	} else {
23167c478bd9Sstevel@tonic-gate 		if (text_arena == NULL)
23177c478bd9Sstevel@tonic-gate 			kobj_vmem_init(&text_arena, &data_arena);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 		/*
23207c478bd9Sstevel@tonic-gate 		 * some architectures may want to load the module on a
23217c478bd9Sstevel@tonic-gate 		 * page that is currently read only. It may not be
23227c478bd9Sstevel@tonic-gate 		 * possible for those architectures to remap their page
23237c478bd9Sstevel@tonic-gate 		 * on the fly. So we provide a facility for them to hang
23247c478bd9Sstevel@tonic-gate 		 * a private hook where the memory they assign the module
23257c478bd9Sstevel@tonic-gate 		 * is not the actual place where the module loads.
23267c478bd9Sstevel@tonic-gate 		 *
23277c478bd9Sstevel@tonic-gate 		 * In this case there are two addresses that deal with the
23287c478bd9Sstevel@tonic-gate 		 * modload.
23297c478bd9Sstevel@tonic-gate 		 * 1) the final destination of the module
23307c478bd9Sstevel@tonic-gate 		 * 2) the address that is used to view the newly
23317c478bd9Sstevel@tonic-gate 		 * loaded module until all the relocations relative to 1
23327c478bd9Sstevel@tonic-gate 		 * above are completed.
23337c478bd9Sstevel@tonic-gate 		 *
23347c478bd9Sstevel@tonic-gate 		 * That is what dest is used for below.
23357c478bd9Sstevel@tonic-gate 		 */
23367c478bd9Sstevel@tonic-gate 		mp->text_size += tp->align;
23377c478bd9Sstevel@tonic-gate 		mp->data_size += dp->align;
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		mp->text = kobj_text_alloc(text_arena, mp->text_size);
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 		/*
23427c478bd9Sstevel@tonic-gate 		 * a remap is taking place. Align the text ptr relative
23437c478bd9Sstevel@tonic-gate 		 * to the secondary mapping. That is where the bits will
23447c478bd9Sstevel@tonic-gate 		 * be read in.
23457c478bd9Sstevel@tonic-gate 		 */
23467c478bd9Sstevel@tonic-gate 		if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena,
23477c478bd9Sstevel@tonic-gate 		    mp->text, mp->text_size)) {
23487c478bd9Sstevel@tonic-gate 			off_t	off = (uintptr_t)mp->text & PAGEOFFSET;
23497c478bd9Sstevel@tonic-gate 			size_t	size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
23507c478bd9Sstevel@tonic-gate 			caddr_t	map = vmem_alloc(heap_arena, size, VM_SLEEP);
23517c478bd9Sstevel@tonic-gate 			caddr_t orig = mp->text - off;
23527c478bd9Sstevel@tonic-gate 			pgcnt_t pages = size / PAGESIZE;
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 			dest = (reloc_dest_t)(map + off);
23557c478bd9Sstevel@tonic-gate 			text = ALIGN((uintptr_t)dest, tp->align);
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 			while (pages--) {
23587c478bd9Sstevel@tonic-gate 				hat_devload(kas.a_hat, map, PAGESIZE,
23597c478bd9Sstevel@tonic-gate 				    hat_getpfnum(kas.a_hat, orig),
23607c478bd9Sstevel@tonic-gate 				    PROT_READ | PROT_WRITE | PROT_EXEC,
23617c478bd9Sstevel@tonic-gate 				    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
23627c478bd9Sstevel@tonic-gate 				map += PAGESIZE;
23637c478bd9Sstevel@tonic-gate 				orig += PAGESIZE;
23647c478bd9Sstevel@tonic-gate 			}
23657c478bd9Sstevel@tonic-gate 			/*
23667c478bd9Sstevel@tonic-gate 			 * Since we set up a non-cacheable mapping, we need
23677c478bd9Sstevel@tonic-gate 			 * to flush any old entries in the cache that might
23687c478bd9Sstevel@tonic-gate 			 * be left around from the read-only mapping.
23697c478bd9Sstevel@tonic-gate 			 */
23707c478bd9Sstevel@tonic-gate 			dcache_flushall();
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 		if (mp->data_size)
23737c478bd9Sstevel@tonic-gate 			mp->data = vmem_alloc(data_arena, mp->data_size,
23747c478bd9Sstevel@tonic-gate 			    VM_SLEEP | VM_BESTFIT);
23757c478bd9Sstevel@tonic-gate 	}
23767c478bd9Sstevel@tonic-gate 	textptr = (uintptr_t)mp->text;
23777c478bd9Sstevel@tonic-gate 	textptr = ALIGN(textptr, tp->align);
23787c478bd9Sstevel@tonic-gate 	mp->destination = dest;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	/*
23817c478bd9Sstevel@tonic-gate 	 * This is the case where a remap is not being done.
23827c478bd9Sstevel@tonic-gate 	 */
23837c478bd9Sstevel@tonic-gate 	if (text == 0)
23847c478bd9Sstevel@tonic-gate 		text = ALIGN((uintptr_t)mp->text, tp->align);
23857c478bd9Sstevel@tonic-gate 	data = ALIGN((uintptr_t)mp->data, dp->align);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	/* now loop though sections assigning addresses and loading the data */
23887c478bd9Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
23897c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
23907c478bd9Sstevel@tonic-gate 		if (!(shp->sh_flags & SHF_ALLOC))
23917c478bd9Sstevel@tonic-gate 			continue;
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 		if ((shp->sh_flags & SHF_WRITE) == 0)
23947c478bd9Sstevel@tonic-gate 			bits_ptr = text;
23957c478bd9Sstevel@tonic-gate 		else
23967c478bd9Sstevel@tonic-gate 			bits_ptr = data;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 		bits_ptr = ALIGN(bits_ptr, shp->sh_addralign);
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		if (shp->sh_type == SHT_NOBITS) {
24017c478bd9Sstevel@tonic-gate 			/*
24027c478bd9Sstevel@tonic-gate 			 * Zero bss.
24037c478bd9Sstevel@tonic-gate 			 */
24047c478bd9Sstevel@tonic-gate 			bzero((caddr_t)bits_ptr, shp->sh_size);
24057c478bd9Sstevel@tonic-gate 			shp->sh_type = SHT_PROGBITS;
24067c478bd9Sstevel@tonic-gate 		} else {
24077c478bd9Sstevel@tonic-gate 			if (kobj_read_file(file, (char *)bits_ptr,
24087c478bd9Sstevel@tonic-gate 			    shp->sh_size, shp->sh_offset) < 0)
24097c478bd9Sstevel@tonic-gate 				goto done;
24107c478bd9Sstevel@tonic-gate 		}
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 		if (shp->sh_flags & SHF_WRITE) {
24137c478bd9Sstevel@tonic-gate 			shp->sh_addr = bits_ptr;
24147c478bd9Sstevel@tonic-gate 		} else {
24157c478bd9Sstevel@tonic-gate 			textptr = ALIGN(textptr, shp->sh_addralign);
24167c478bd9Sstevel@tonic-gate 			shp->sh_addr = textptr;
24177c478bd9Sstevel@tonic-gate 			textptr += shp->sh_size;
24187c478bd9Sstevel@tonic-gate 		}
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 		bits_ptr += shp->sh_size;
24217c478bd9Sstevel@tonic-gate 		if ((shp->sh_flags & SHF_WRITE) == 0)
24227c478bd9Sstevel@tonic-gate 			text = bits_ptr;
24237c478bd9Sstevel@tonic-gate 		else
24247c478bd9Sstevel@tonic-gate 			data = bits_ptr;
24257c478bd9Sstevel@tonic-gate 	}
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	err = 0;
24287c478bd9Sstevel@tonic-gate done:
24297c478bd9Sstevel@tonic-gate 	/*
24307c478bd9Sstevel@tonic-gate 	 * Free and mark as freed the section headers here so that
24317c478bd9Sstevel@tonic-gate 	 * free_module_data() does not have to worry about this buffer.
24327c478bd9Sstevel@tonic-gate 	 *
24337c478bd9Sstevel@tonic-gate 	 * This buffer is freed here because one of the possible reasons
24347c478bd9Sstevel@tonic-gate 	 * for error is a section with non-zero sh_addr and in that case
24357c478bd9Sstevel@tonic-gate 	 * free_module_data() would have no way of recognizing that this
24367c478bd9Sstevel@tonic-gate 	 * buffer was unallocated.
24377c478bd9Sstevel@tonic-gate 	 */
24387c478bd9Sstevel@tonic-gate 	if (err != 0) {
24397c478bd9Sstevel@tonic-gate 		kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum);
24407c478bd9Sstevel@tonic-gate 		mp->shdrs = NULL;
24417c478bd9Sstevel@tonic-gate 	}
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	(void) kobj_free(tp, sizeof (struct proginfo));
24447c478bd9Sstevel@tonic-gate 	(void) kobj_free(dp, sizeof (struct proginfo));
24457c478bd9Sstevel@tonic-gate 	(void) kobj_free(sdp, sizeof (struct proginfo));
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	return (err);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Go through suppress_sym_list to see if "multiply defined"
24527c478bd9Sstevel@tonic-gate  * warning of this symbol should be suppressed.  Return 1 if
24537c478bd9Sstevel@tonic-gate  * warning should be suppressed, 0 otherwise.
24547c478bd9Sstevel@tonic-gate  */
24557c478bd9Sstevel@tonic-gate static int
kobj_suppress_warning(char * symname)24567c478bd9Sstevel@tonic-gate kobj_suppress_warning(char *symname)
24577c478bd9Sstevel@tonic-gate {
24587c478bd9Sstevel@tonic-gate 	int	i;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	for (i = 0; suppress_sym_list[i] != NULL; i++) {
24617c478bd9Sstevel@tonic-gate 		if (strcmp(suppress_sym_list[i], symname) == 0)
24627c478bd9Sstevel@tonic-gate 			return (1);
24637c478bd9Sstevel@tonic-gate 	}
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	return (0);
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate static int
get_syms(struct module * mp,struct _buf * file)24697c478bd9Sstevel@tonic-gate get_syms(struct module *mp, struct _buf *file)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	uint_t		shn;
24727c478bd9Sstevel@tonic-gate 	Shdr	*shp;
24737c478bd9Sstevel@tonic-gate 	uint_t		i;
24747c478bd9Sstevel@tonic-gate 	Sym	*sp, *ksp;
24757c478bd9Sstevel@tonic-gate 	char		*symname;
24767c478bd9Sstevel@tonic-gate 	int		dosymtab = 0;
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	/*
24797c478bd9Sstevel@tonic-gate 	 * Find the interesting sections.
24807c478bd9Sstevel@tonic-gate 	 */
24817c478bd9Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
24827c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
24837c478bd9Sstevel@tonic-gate 		switch (shp->sh_type) {
24847c478bd9Sstevel@tonic-gate 		case SHT_SYMTAB:
24857c478bd9Sstevel@tonic-gate 			mp->symtbl_section = shn;
24867c478bd9Sstevel@tonic-gate 			mp->symhdr = shp;
24877c478bd9Sstevel@tonic-gate 			dosymtab++;
24887c478bd9Sstevel@tonic-gate 			break;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 		case SHT_RELA:
24917c478bd9Sstevel@tonic-gate 		case SHT_REL:
24927c478bd9Sstevel@tonic-gate 			/*
24937c478bd9Sstevel@tonic-gate 			 * Already loaded.
24947c478bd9Sstevel@tonic-gate 			 */
24957c478bd9Sstevel@tonic-gate 			if (shp->sh_addr)
24967c478bd9Sstevel@tonic-gate 				continue;
2497986fd29aSsetje 
2498986fd29aSsetje 			/* KM_TMP since kobj_free'd in do_relocations */
24997c478bd9Sstevel@tonic-gate 			shp->sh_addr = (Addr)
25007c478bd9Sstevel@tonic-gate 			    kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP);
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 			if (kobj_read_file(file, (char *)shp->sh_addr,
25037c478bd9Sstevel@tonic-gate 			    shp->sh_size, shp->sh_offset) < 0) {
25047c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "krtld: get_syms: %s, ",
25057c478bd9Sstevel@tonic-gate 				    mp->filename);
25067c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "error reading section %d\n",
25077c478bd9Sstevel@tonic-gate 				    shn);
25087c478bd9Sstevel@tonic-gate 				return (-1);
25097c478bd9Sstevel@tonic-gate 			}
25107c478bd9Sstevel@tonic-gate 			break;
25117c478bd9Sstevel@tonic-gate 		}
25127c478bd9Sstevel@tonic-gate 	}
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	/*
25157c478bd9Sstevel@tonic-gate 	 * This is true for a stripped executable.  In the case of
25167c478bd9Sstevel@tonic-gate 	 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
25177c478bd9Sstevel@tonic-gate 	 * and since that symbol information is still present everything
25187c478bd9Sstevel@tonic-gate 	 * is just fine.
25197c478bd9Sstevel@tonic-gate 	 */
25207c478bd9Sstevel@tonic-gate 	if (!dosymtab) {
25217c478bd9Sstevel@tonic-gate 		if (mp->flags & KOBJ_EXEC)
25227c478bd9Sstevel@tonic-gate 			return (0);
25237c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_syms: %s ",
2524b1b8ab34Slling 		    mp->filename);
25257c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "no SHT_SYMTAB symbol table found\n");
25267c478bd9Sstevel@tonic-gate 		return (-1);
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	/*
25307c478bd9Sstevel@tonic-gate 	 * get the associated string table header
25317c478bd9Sstevel@tonic-gate 	 */
25327c478bd9Sstevel@tonic-gate 	if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum))
25337c478bd9Sstevel@tonic-gate 		return (-1);
25347c478bd9Sstevel@tonic-gate 	mp->strhdr = (Shdr *)
2535b1b8ab34Slling 	    (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
25387c478bd9Sstevel@tonic-gate 	mp->hashsize = kobj_gethashsize(mp->nsyms);
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 	/*
25417c478bd9Sstevel@tonic-gate 	 * Allocate space for the symbol table, buckets, chains, and strings.
25427c478bd9Sstevel@tonic-gate 	 */
25437c478bd9Sstevel@tonic-gate 	mp->symsize = mp->symhdr->sh_size +
25447c478bd9Sstevel@tonic-gate 	    (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size;
25457c478bd9Sstevel@tonic-gate 	mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH);
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	mp->symtbl = mp->symspace;
25487c478bd9Sstevel@tonic-gate 	mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size);
25497c478bd9Sstevel@tonic-gate 	mp->chains = mp->buckets + mp->hashsize;
25507c478bd9Sstevel@tonic-gate 	mp->strings = (char *)(mp->chains + mp->nsyms);
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, mp->symtbl,
25537c478bd9Sstevel@tonic-gate 	    mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 ||
25547c478bd9Sstevel@tonic-gate 	    kobj_read_file(file, mp->strings,
25557c478bd9Sstevel@tonic-gate 	    mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0)
25567c478bd9Sstevel@tonic-gate 		return (-1);
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	/*
25597c478bd9Sstevel@tonic-gate 	 * loop through the symbol table adjusting values to account
25607c478bd9Sstevel@tonic-gate 	 * for where each section got loaded into memory.  Also
25617c478bd9Sstevel@tonic-gate 	 * fill in the hash table.
25627c478bd9Sstevel@tonic-gate 	 */
25637c478bd9Sstevel@tonic-gate 	for (i = 1; i < mp->nsyms; i++) {
25647c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
25657c478bd9Sstevel@tonic-gate 		if (sp->st_shndx < SHN_LORESERVE) {
25667c478bd9Sstevel@tonic-gate 			if (sp->st_shndx >= mp->hdr.e_shnum) {
25677c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "%s bad shndx ",
25687c478bd9Sstevel@tonic-gate 				    file->_name);
25697c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "in symbol %d\n", i);
25707c478bd9Sstevel@tonic-gate 				return (-1);
25717c478bd9Sstevel@tonic-gate 			}
25727c478bd9Sstevel@tonic-gate 			shp = (Shdr *)
25737c478bd9Sstevel@tonic-gate 			    (mp->shdrs +
25747c478bd9Sstevel@tonic-gate 			    sp->st_shndx * mp->hdr.e_shentsize);
25757c478bd9Sstevel@tonic-gate 			if (!(mp->flags & KOBJ_EXEC))
25767c478bd9Sstevel@tonic-gate 				sp->st_value += shp->sh_addr;
25777c478bd9Sstevel@tonic-gate 		}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 		if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
25807c478bd9Sstevel@tonic-gate 			continue;
25817c478bd9Sstevel@tonic-gate 		if (sp->st_name >= mp->strhdr->sh_size)
25827c478bd9Sstevel@tonic-gate 			return (-1);
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 		symname = mp->strings + sp->st_name;
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_EXEC) &&
25877c478bd9Sstevel@tonic-gate 		    ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
25887c478bd9Sstevel@tonic-gate 			ksp = kobj_lookup_all(mp, symname, 0);
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 			if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL &&
25917c478bd9Sstevel@tonic-gate 			    !kobj_suppress_warning(symname) &&
25927c478bd9Sstevel@tonic-gate 			    sp->st_shndx != SHN_UNDEF &&
25937c478bd9Sstevel@tonic-gate 			    sp->st_shndx != SHN_COMMON &&
25947c478bd9Sstevel@tonic-gate 			    ksp->st_shndx != SHN_UNDEF &&
25957c478bd9Sstevel@tonic-gate 			    ksp->st_shndx != SHN_COMMON) {
25967c478bd9Sstevel@tonic-gate 				/*
25977c478bd9Sstevel@tonic-gate 				 * Unless this symbol is a stub, it's multiply
25987c478bd9Sstevel@tonic-gate 				 * defined.  Multiply-defined symbols are
25997c478bd9Sstevel@tonic-gate 				 * usually bad, but some objects (kmdb) have
26007c478bd9Sstevel@tonic-gate 				 * a legitimate need to have their own
26017c478bd9Sstevel@tonic-gate 				 * copies of common functions.
26027c478bd9Sstevel@tonic-gate 				 */
26037c478bd9Sstevel@tonic-gate 				if ((standalone ||
26047c478bd9Sstevel@tonic-gate 				    ksp->st_value < (uintptr_t)stubs_base ||
26057c478bd9Sstevel@tonic-gate 				    ksp->st_value >= (uintptr_t)stubs_end) &&
26067c478bd9Sstevel@tonic-gate 				    !(mp->flags & KOBJ_IGNMULDEF)) {
26077c478bd9Sstevel@tonic-gate 					_kobj_printf(ops,
26087c478bd9Sstevel@tonic-gate 					    "%s symbol ", file->_name);
26097c478bd9Sstevel@tonic-gate 					_kobj_printf(ops,
26107c478bd9Sstevel@tonic-gate 					    "%s multiply defined\n", symname);
26117c478bd9Sstevel@tonic-gate 				}
26127c478bd9Sstevel@tonic-gate 			}
26137c478bd9Sstevel@tonic-gate 		}
2614ae115bc7Smrj 
26157c478bd9Sstevel@tonic-gate 		sym_insert(mp, symname, i);
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	return (0);
26197c478bd9Sstevel@tonic-gate }
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate static int
get_ctf(struct module * mp,struct _buf * file)26227c478bd9Sstevel@tonic-gate get_ctf(struct module *mp, struct _buf *file)
26237c478bd9Sstevel@tonic-gate {
26247c478bd9Sstevel@tonic-gate 	char *shstrtab, *ctfdata;
26257c478bd9Sstevel@tonic-gate 	size_t shstrlen;
26267c478bd9Sstevel@tonic-gate 	Shdr *shp;
26277c478bd9Sstevel@tonic-gate 	uint_t i;
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	if (_moddebug & MODDEBUG_NOCTF)
26307c478bd9Sstevel@tonic-gate 		return (0); /* do not attempt to even load CTF data */
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
26337c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_ctf: %s, ",
26347c478bd9Sstevel@tonic-gate 		    mp->filename);
26357c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "corrupt e_shstrndx %u\n",
26367c478bd9Sstevel@tonic-gate 		    mp->hdr.e_shstrndx);
26377c478bd9Sstevel@tonic-gate 		return (-1);
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
26417c478bd9Sstevel@tonic-gate 	shstrlen = shp->sh_size;
26427c478bd9Sstevel@tonic-gate 	shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
26457c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_ctf: %s, ",
26467c478bd9Sstevel@tonic-gate 		    mp->filename);
26477c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "error reading section %u\n",
26487c478bd9Sstevel@tonic-gate 		    mp->hdr.e_shstrndx);
26497c478bd9Sstevel@tonic-gate 		kobj_free(shstrtab, shstrlen);
26507c478bd9Sstevel@tonic-gate 		return (-1);
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->hdr.e_shnum; i++) {
26547c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 		if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
26577c478bd9Sstevel@tonic-gate 		    strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) {
26587c478bd9Sstevel@tonic-gate 			ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH);
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 			if (kobj_read_file(file, ctfdata, shp->sh_size,
26617c478bd9Sstevel@tonic-gate 			    shp->sh_offset) < 0) {
26627c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "krtld: get_ctf: %s, error "
26637c478bd9Sstevel@tonic-gate 				    "reading .SUNW_ctf data\n", mp->filename);
26647c478bd9Sstevel@tonic-gate 				kobj_free(ctfdata, shp->sh_size);
26657c478bd9Sstevel@tonic-gate 				kobj_free(shstrtab, shstrlen);
26667c478bd9Sstevel@tonic-gate 				return (-1);
26677c478bd9Sstevel@tonic-gate 			}
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 			mp->ctfdata = ctfdata;
26707c478bd9Sstevel@tonic-gate 			mp->ctfsize = shp->sh_size;
26717c478bd9Sstevel@tonic-gate 			break;
26727c478bd9Sstevel@tonic-gate 		}
26737c478bd9Sstevel@tonic-gate 	}
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	kobj_free(shstrtab, shstrlen);
26767c478bd9Sstevel@tonic-gate 	return (0);
26777c478bd9Sstevel@tonic-gate }
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate #define	SHA1_DIGEST_LENGTH	20	/* SHA1 digest length in bytes */
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate /*
26827c478bd9Sstevel@tonic-gate  * Return the hash of the ELF sections that are memory resident.
26837c478bd9Sstevel@tonic-gate  * i.e. text and data.  We skip a SHT_NOBITS section since it occupies
26847c478bd9Sstevel@tonic-gate  * no space in the file. We use SHA1 here since libelfsign uses
26857c478bd9Sstevel@tonic-gate  * it and both places need to use the same algorithm.
26867c478bd9Sstevel@tonic-gate  */
26877c478bd9Sstevel@tonic-gate static void
crypto_es_hash(struct module * mp,char * hash,char * shstrtab)26887c478bd9Sstevel@tonic-gate crypto_es_hash(struct module *mp, char *hash, char *shstrtab)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	uint_t shn;
26917c478bd9Sstevel@tonic-gate 	Shdr *shp;
26927c478bd9Sstevel@tonic-gate 	SHA1_CTX ctx;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	SHA1Init(&ctx);
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
26977c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
26987c478bd9Sstevel@tonic-gate 		if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0)
26997c478bd9Sstevel@tonic-gate 			continue;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 		/*
27027c478bd9Sstevel@tonic-gate 		 * The check should ideally be shp->sh_type == SHT_NOBITS.
27037c478bd9Sstevel@tonic-gate 		 * However, we can't do that check here as get_progbits()
27047c478bd9Sstevel@tonic-gate 		 * resets the type.
27057c478bd9Sstevel@tonic-gate 		 */
27067c478bd9Sstevel@tonic-gate 		if (strcmp(shstrtab + shp->sh_name, ".bss") == 0)
27077c478bd9Sstevel@tonic-gate 			continue;
27087c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
27097c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_DEBUG)
27107c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
27117c478bd9Sstevel@tonic-gate 			    "krtld: crypto_es_hash: updating hash with"
2712c61137dfSJohn Levon 			    " %s data size=%lx\n", shstrtab + shp->sh_name,
2713c61137dfSJohn Levon 			    (size_t)shp->sh_size);
27147c478bd9Sstevel@tonic-gate #endif
2715fb46ffcaSToomas Soome 		ASSERT(shp->sh_addr != 0);
27167c478bd9Sstevel@tonic-gate 		SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size);
27177c478bd9Sstevel@tonic-gate 	}
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	SHA1Final((uchar_t *)hash, &ctx);
27207c478bd9Sstevel@tonic-gate }
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate /*
27237c478bd9Sstevel@tonic-gate  * Get the .SUNW_signature section for the module, it it exists.
27247c478bd9Sstevel@tonic-gate  *
27257c478bd9Sstevel@tonic-gate  * This section exists only for crypto modules. None of the
27267c478bd9Sstevel@tonic-gate  * primary modules have this section currently.
27277c478bd9Sstevel@tonic-gate  */
27287c478bd9Sstevel@tonic-gate static void
get_signature(struct module * mp,struct _buf * file)27297c478bd9Sstevel@tonic-gate get_signature(struct module *mp, struct _buf *file)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	char *shstrtab, *sigdata = NULL;
27327c478bd9Sstevel@tonic-gate 	size_t shstrlen;
27337c478bd9Sstevel@tonic-gate 	Shdr *shp;
27347c478bd9Sstevel@tonic-gate 	uint_t i;
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
27377c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_signature: %s, ",
27387c478bd9Sstevel@tonic-gate 		    mp->filename);
27397c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "corrupt e_shstrndx %u\n",
27407c478bd9Sstevel@tonic-gate 		    mp->hdr.e_shstrndx);
27417c478bd9Sstevel@tonic-gate 		return;
27427c478bd9Sstevel@tonic-gate 	}
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
27457c478bd9Sstevel@tonic-gate 	shstrlen = shp->sh_size;
27467c478bd9Sstevel@tonic-gate 	shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
27497c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld: get_signature: %s, ",
27507c478bd9Sstevel@tonic-gate 		    mp->filename);
27517c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "error reading section %u\n",
27527c478bd9Sstevel@tonic-gate 		    mp->hdr.e_shstrndx);
27537c478bd9Sstevel@tonic-gate 		kobj_free(shstrtab, shstrlen);
27547c478bd9Sstevel@tonic-gate 		return;
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->hdr.e_shnum; i++) {
27587c478bd9Sstevel@tonic-gate 		shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
27597c478bd9Sstevel@tonic-gate 		if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
27607c478bd9Sstevel@tonic-gate 		    strcmp(shstrtab + shp->sh_name,
27617c478bd9Sstevel@tonic-gate 		    ELF_SIGNATURE_SECTION) == 0) {
27627c478bd9Sstevel@tonic-gate 			filesig_vers_t filesig_version;
27637c478bd9Sstevel@tonic-gate 			size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH;
27647c478bd9Sstevel@tonic-gate 			sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH);
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 			if (kobj_read_file(file, sigdata, shp->sh_size,
27677c478bd9Sstevel@tonic-gate 			    shp->sh_offset) < 0) {
27687c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "krtld: get_signature: %s,"
27697c478bd9Sstevel@tonic-gate 				    " error reading .SUNW_signature data\n",
27707c478bd9Sstevel@tonic-gate 				    mp->filename);
27717c478bd9Sstevel@tonic-gate 				kobj_free(sigdata, sigsize);
27727c478bd9Sstevel@tonic-gate 				kobj_free(shstrtab, shstrlen);
27737c478bd9Sstevel@tonic-gate 				return;
27747c478bd9Sstevel@tonic-gate 			}
27757c478bd9Sstevel@tonic-gate 			filesig_version = ((struct filesignatures *)sigdata)->
27767c478bd9Sstevel@tonic-gate 			    filesig_sig.filesig_version;
27777c478bd9Sstevel@tonic-gate 			if (!(filesig_version == FILESIG_VERSION1 ||
27787c478bd9Sstevel@tonic-gate 			    filesig_version == FILESIG_VERSION3)) {
27797c478bd9Sstevel@tonic-gate 				/* skip versions we don't understand */
27807c478bd9Sstevel@tonic-gate 				kobj_free(sigdata, sigsize);
27817c478bd9Sstevel@tonic-gate 				kobj_free(shstrtab, shstrlen);
27827c478bd9Sstevel@tonic-gate 				return;
27837c478bd9Sstevel@tonic-gate 			}
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 			mp->sigdata = sigdata;
27867c478bd9Sstevel@tonic-gate 			mp->sigsize = sigsize;
27877c478bd9Sstevel@tonic-gate 			break;
27887c478bd9Sstevel@tonic-gate 		}
27897c478bd9Sstevel@tonic-gate 	}
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	if (sigdata != NULL) {
27927c478bd9Sstevel@tonic-gate 		crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab);
27937c478bd9Sstevel@tonic-gate 	}
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	kobj_free(shstrtab, shstrlen);
27967c478bd9Sstevel@tonic-gate }
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate static void
add_dependent(struct module * mp,struct module * dep)27997c478bd9Sstevel@tonic-gate add_dependent(struct module *mp, struct module *dep)
28007c478bd9Sstevel@tonic-gate {
28017c478bd9Sstevel@tonic-gate 	struct module_list *lp;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	for (lp = mp->head; lp; lp = lp->next) {
28047c478bd9Sstevel@tonic-gate 		if (lp->mp == dep)
28057c478bd9Sstevel@tonic-gate 			return;	/* already on the list */
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	if (lp == NULL) {
28097c478bd9Sstevel@tonic-gate 		lp = kobj_zalloc(sizeof (*lp), KM_WAIT);
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 		lp->mp = dep;
28127c478bd9Sstevel@tonic-gate 		lp->next = NULL;
28137c478bd9Sstevel@tonic-gate 		if (mp->tail)
28147c478bd9Sstevel@tonic-gate 			mp->tail->next = lp;
28157c478bd9Sstevel@tonic-gate 		else
28167c478bd9Sstevel@tonic-gate 			mp->head = lp;
28177c478bd9Sstevel@tonic-gate 		mp->tail = lp;
28187c478bd9Sstevel@tonic-gate 	}
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate static int
do_dependents(struct modctl * modp,char * modname,size_t modnamelen)28227c478bd9Sstevel@tonic-gate do_dependents(struct modctl *modp, char *modname, size_t modnamelen)
28237c478bd9Sstevel@tonic-gate {
28247c478bd9Sstevel@tonic-gate 	struct module *mp;
28257c478bd9Sstevel@tonic-gate 	struct modctl *req;
28267c478bd9Sstevel@tonic-gate 	char *d, *p, *q;
28277c478bd9Sstevel@tonic-gate 	int c;
28287c478bd9Sstevel@tonic-gate 	char *err_modname = NULL;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	mp = modp->mod_mp;
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	if ((p = mp->depends_on) == NULL)
28337c478bd9Sstevel@tonic-gate 		return (0);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	for (;;) {
28367c478bd9Sstevel@tonic-gate 		/*
28377c478bd9Sstevel@tonic-gate 		 * Skip space.
28387c478bd9Sstevel@tonic-gate 		 */
28397c478bd9Sstevel@tonic-gate 		while (*p && (*p == ' ' || *p == '\t'))
28407c478bd9Sstevel@tonic-gate 			p++;
28417c478bd9Sstevel@tonic-gate 		/*
28427c478bd9Sstevel@tonic-gate 		 * Get module name.
28437c478bd9Sstevel@tonic-gate 		 */
28447c478bd9Sstevel@tonic-gate 		d = p;
28457c478bd9Sstevel@tonic-gate 		q = modname;
28467c478bd9Sstevel@tonic-gate 		c = 0;
28477c478bd9Sstevel@tonic-gate 		while (*p && *p != ' ' && *p != '\t') {
28487c478bd9Sstevel@tonic-gate 			if (c < modnamelen - 1) {
28497c478bd9Sstevel@tonic-gate 				*q++ = *p;
28507c478bd9Sstevel@tonic-gate 				c++;
28517c478bd9Sstevel@tonic-gate 			}
28527c478bd9Sstevel@tonic-gate 			p++;
28537c478bd9Sstevel@tonic-gate 		}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 		if (q == modname)
28567c478bd9Sstevel@tonic-gate 			break;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 		if (c == modnamelen - 1) {
28597c478bd9Sstevel@tonic-gate 			char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 			(void) strncpy(dep, d,  p - d + 1);
28627c478bd9Sstevel@tonic-gate 			dep[p - d] = '\0';
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s: dependency ", modp->mod_modname);
28657c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "'%s' too long ", dep);
2866c61137dfSJohn Levon 			_kobj_printf(ops, "(max %d chars)\n", (int)modnamelen);
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 			kobj_free(dep, p - d + 1);
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate 			return (-1);
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 		*q = '\0';
28747c478bd9Sstevel@tonic-gate 		if ((req = mod_load_requisite(modp, modname)) == NULL) {
28757c478bd9Sstevel@tonic-gate #ifndef	KOBJ_DEBUG
28767c478bd9Sstevel@tonic-gate 			if (_moddebug & MODDEBUG_LOADMSG) {
28777c478bd9Sstevel@tonic-gate #endif	/* KOBJ_DEBUG */
28787c478bd9Sstevel@tonic-gate 				_kobj_printf(ops,
28797c478bd9Sstevel@tonic-gate 				    "%s: unable to resolve dependency, ",
28807c478bd9Sstevel@tonic-gate 				    modp->mod_modname);
28817c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "cannot load module '%s'\n",
28827c478bd9Sstevel@tonic-gate 				    modname);
28837c478bd9Sstevel@tonic-gate #ifndef	KOBJ_DEBUG
28847c478bd9Sstevel@tonic-gate 			}
28857c478bd9Sstevel@tonic-gate #endif	/* KOBJ_DEBUG */
28867c478bd9Sstevel@tonic-gate 			if (err_modname == NULL) {
28877c478bd9Sstevel@tonic-gate 				/*
28887c478bd9Sstevel@tonic-gate 				 * This must be the same size as the modname
28897c478bd9Sstevel@tonic-gate 				 * one.
28907c478bd9Sstevel@tonic-gate 				 */
28917c478bd9Sstevel@tonic-gate 				err_modname = kobj_zalloc(MODMAXNAMELEN,
28927c478bd9Sstevel@tonic-gate 				    KM_WAIT);
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 				/*
28957c478bd9Sstevel@tonic-gate 				 * We can use strcpy() here without fearing
28967c478bd9Sstevel@tonic-gate 				 * the NULL terminator because the size of
28977c478bd9Sstevel@tonic-gate 				 * err_modname is the same as one of modname,
28987c478bd9Sstevel@tonic-gate 				 * and it's filled with zeros.
28997c478bd9Sstevel@tonic-gate 				 */
29007c478bd9Sstevel@tonic-gate 				(void) strcpy(err_modname, modname);
29017c478bd9Sstevel@tonic-gate 			}
29027c478bd9Sstevel@tonic-gate 			continue;
29037c478bd9Sstevel@tonic-gate 		}
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 		add_dependent(mp, req->mod_mp);
29067c478bd9Sstevel@tonic-gate 		mod_release_mod(req);
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	}
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	if (err_modname != NULL) {
29117c478bd9Sstevel@tonic-gate 		/*
29127c478bd9Sstevel@tonic-gate 		 * Copy the first module name where you detect an error to keep
29137c478bd9Sstevel@tonic-gate 		 * its behavior the same as before.
29147c478bd9Sstevel@tonic-gate 		 * This way keeps minimizing the memory use for error
29157c478bd9Sstevel@tonic-gate 		 * modules, and this might be important at boot time because
29167c478bd9Sstevel@tonic-gate 		 * the memory usage is a crucial factor for booting in most
29177c478bd9Sstevel@tonic-gate 		 * cases. You can expect more verbose messages when using
29187c478bd9Sstevel@tonic-gate 		 * a debug kernel or setting a bit in moddebug.
29197c478bd9Sstevel@tonic-gate 		 */
29207c478bd9Sstevel@tonic-gate 		bzero(modname, MODMAXNAMELEN);
29217c478bd9Sstevel@tonic-gate 		(void) strcpy(modname, err_modname);
29227c478bd9Sstevel@tonic-gate 		kobj_free(err_modname, MODMAXNAMELEN);
29237c478bd9Sstevel@tonic-gate 		return (-1);
29247c478bd9Sstevel@tonic-gate 	}
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	return (0);
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate static int
do_common(struct module * mp)29307c478bd9Sstevel@tonic-gate do_common(struct module *mp)
29317c478bd9Sstevel@tonic-gate {
29327c478bd9Sstevel@tonic-gate 	int err;
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	/*
29357c478bd9Sstevel@tonic-gate 	 * first time through, assign all symbols defined in other
29367c478bd9Sstevel@tonic-gate 	 * modules, and count up how much common space will be needed
29377c478bd9Sstevel@tonic-gate 	 * (bss_size and bss_align)
29387c478bd9Sstevel@tonic-gate 	 */
29397c478bd9Sstevel@tonic-gate 	if ((err = do_symbols(mp, 0)) < 0)
29407c478bd9Sstevel@tonic-gate 		return (err);
29417c478bd9Sstevel@tonic-gate 	/*
29427c478bd9Sstevel@tonic-gate 	 * increase bss_size by the maximum delta that could be
29437c478bd9Sstevel@tonic-gate 	 * computed by the ALIGN below
29447c478bd9Sstevel@tonic-gate 	 */
29457c478bd9Sstevel@tonic-gate 	mp->bss_size += mp->bss_align;
29467c478bd9Sstevel@tonic-gate 	if (mp->bss_size) {
29477c478bd9Sstevel@tonic-gate 		if (standalone)
29487c478bd9Sstevel@tonic-gate 			mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size,
29497c478bd9Sstevel@tonic-gate 			    MINALIGN, 0);
29507c478bd9Sstevel@tonic-gate 		else
29517c478bd9Sstevel@tonic-gate 			mp->bss = (uintptr_t)vmem_alloc(data_arena,
29527c478bd9Sstevel@tonic-gate 			    mp->bss_size, VM_SLEEP | VM_BESTFIT);
29537c478bd9Sstevel@tonic-gate 		bzero((void *)mp->bss, mp->bss_size);
29547c478bd9Sstevel@tonic-gate 		/* now assign addresses to all common symbols */
29557c478bd9Sstevel@tonic-gate 		if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0)
29567c478bd9Sstevel@tonic-gate 			return (err);
29577c478bd9Sstevel@tonic-gate 	}
29587c478bd9Sstevel@tonic-gate 	return (0);
29597c478bd9Sstevel@tonic-gate }
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate static int
do_symbols(struct module * mp,Elf64_Addr bss_base)29627c478bd9Sstevel@tonic-gate do_symbols(struct module *mp, Elf64_Addr bss_base)
29637c478bd9Sstevel@tonic-gate {
29647c478bd9Sstevel@tonic-gate 	int bss_align;
29657c478bd9Sstevel@tonic-gate 	uintptr_t bss_ptr;
29667c478bd9Sstevel@tonic-gate 	int err;
29677c478bd9Sstevel@tonic-gate 	int i;
29687c478bd9Sstevel@tonic-gate 	Sym *sp, *sp1;
29697c478bd9Sstevel@tonic-gate 	char *name;
29707c478bd9Sstevel@tonic-gate 	int assign;
29717c478bd9Sstevel@tonic-gate 	int resolved = 1;
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	/*
29747c478bd9Sstevel@tonic-gate 	 * Nothing left to do (optimization).
29757c478bd9Sstevel@tonic-gate 	 */
29767c478bd9Sstevel@tonic-gate 	if (mp->flags & KOBJ_RESOLVED)
29777c478bd9Sstevel@tonic-gate 		return (0);
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 	assign = (bss_base) ? 1 : 0;
29807c478bd9Sstevel@tonic-gate 	bss_ptr = bss_base;
29817c478bd9Sstevel@tonic-gate 	bss_align = 0;
29827c478bd9Sstevel@tonic-gate 	err = 0;
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	for (i = 1; i < mp->nsyms; i++) {
29857c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i);
29867c478bd9Sstevel@tonic-gate 		/*
29877c478bd9Sstevel@tonic-gate 		 * we know that st_name is in bounds, since get_sections
29887c478bd9Sstevel@tonic-gate 		 * has already checked all of the symbols
29897c478bd9Sstevel@tonic-gate 		 */
29907c478bd9Sstevel@tonic-gate 		name = mp->strings + sp->st_name;
29917c478bd9Sstevel@tonic-gate 		if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON)
29927c478bd9Sstevel@tonic-gate 			continue;
2993986fd29aSsetje #if defined(__sparc)
29947c478bd9Sstevel@tonic-gate 		/*
29957c478bd9Sstevel@tonic-gate 		 * Register symbols are ignored in the kernel
29967c478bd9Sstevel@tonic-gate 		 */
29977c478bd9Sstevel@tonic-gate 		if (ELF_ST_TYPE(sp->st_info) == STT_SPARC_REGISTER) {
29987c478bd9Sstevel@tonic-gate 			if (*name != '\0') {
29997c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "%s: named REGISTER symbol ",
3000b1b8ab34Slling 				    mp->filename);
30017c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "not supported '%s'\n",
3002b1b8ab34Slling 				    name);
30037c478bd9Sstevel@tonic-gate 				err = DOSYM_UNDEF;
30047c478bd9Sstevel@tonic-gate 			}
30057c478bd9Sstevel@tonic-gate 			continue;
30067c478bd9Sstevel@tonic-gate 		}
30077c478bd9Sstevel@tonic-gate #endif	/* __sparc */
30087c478bd9Sstevel@tonic-gate 		/*
30097c478bd9Sstevel@tonic-gate 		 * TLS symbols are ignored in the kernel
30107c478bd9Sstevel@tonic-gate 		 */
30117c478bd9Sstevel@tonic-gate 		if (ELF_ST_TYPE(sp->st_info) == STT_TLS) {
30127c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s: TLS symbol ",
3013b1b8ab34Slling 			    mp->filename);
30147c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "not supported '%s'\n",
3015b1b8ab34Slling 			    name);
30167c478bd9Sstevel@tonic-gate 			err = DOSYM_UNDEF;
30177c478bd9Sstevel@tonic-gate 			continue;
30187c478bd9Sstevel@tonic-gate 		}
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) {
30217c478bd9Sstevel@tonic-gate 			if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) {
30227c478bd9Sstevel@tonic-gate 				sp->st_shndx = SHN_ABS;
30237c478bd9Sstevel@tonic-gate 				sp->st_value = sp1->st_value;
30247c478bd9Sstevel@tonic-gate 				continue;
30257c478bd9Sstevel@tonic-gate 			}
30267c478bd9Sstevel@tonic-gate 		}
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 		if (sp->st_shndx == SHN_UNDEF) {
30297c478bd9Sstevel@tonic-gate 			resolved = 0;
30307c478bd9Sstevel@tonic-gate 
3031425251fdSSam Gwydir 			/*
3032425251fdSSam Gwydir 			 * Skip over sdt probes and smap calls,
3033425251fdSSam Gwydir 			 * they're relocated later.
3034425251fdSSam Gwydir 			 */
30357c478bd9Sstevel@tonic-gate 			if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0)
30367c478bd9Sstevel@tonic-gate 				continue;
3037425251fdSSam Gwydir #if defined(__x86)
3038425251fdSSam Gwydir 			if (strcmp(name, "smap_enable") == 0 ||
3039425251fdSSam Gwydir 			    strcmp(name, "smap_disable") == 0)
3040425251fdSSam Gwydir 				continue;
3041425251fdSSam Gwydir #endif /* defined(__x86) */
3042425251fdSSam Gwydir 
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 			/*
30457c478bd9Sstevel@tonic-gate 			 * If it's not a weak reference and it's
30467c478bd9Sstevel@tonic-gate 			 * not a primary object, it's an error.
30477c478bd9Sstevel@tonic-gate 			 * (Primary objects may take more than
30487c478bd9Sstevel@tonic-gate 			 * one pass to resolve)
30497c478bd9Sstevel@tonic-gate 			 */
30507c478bd9Sstevel@tonic-gate 			if (!(mp->flags & KOBJ_PRIM) &&
30517c478bd9Sstevel@tonic-gate 			    ELF_ST_BIND(sp->st_info) != STB_WEAK) {
30527c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "%s: undefined symbol",
30537c478bd9Sstevel@tonic-gate 				    mp->filename);
30547c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, " '%s'\n", name);
30557c478bd9Sstevel@tonic-gate 				/*
30567c478bd9Sstevel@tonic-gate 				 * Try to determine whether this symbol
30577c478bd9Sstevel@tonic-gate 				 * represents a dependency on obsolete
30587c478bd9Sstevel@tonic-gate 				 * unsafe driver support.  This is just
30597c478bd9Sstevel@tonic-gate 				 * to make the warning more informative.
30607c478bd9Sstevel@tonic-gate 				 */
30617c478bd9Sstevel@tonic-gate 				if (strcmp(name, "sleep") == 0 ||
30627c478bd9Sstevel@tonic-gate 				    strcmp(name, "unsleep") == 0 ||
30637c478bd9Sstevel@tonic-gate 				    strcmp(name, "wakeup") == 0 ||
30647c478bd9Sstevel@tonic-gate 				    strcmp(name, "bsd_compat_ioctl") == 0 ||
30657c478bd9Sstevel@tonic-gate 				    strcmp(name, "unsafe_driver") == 0 ||
30667c478bd9Sstevel@tonic-gate 				    strncmp(name, "spl", 3) == 0 ||
30677c478bd9Sstevel@tonic-gate 				    strncmp(name, "i_ddi_spl", 9) == 0)
30687c478bd9Sstevel@tonic-gate 					err = DOSYM_UNSAFE;
30697c478bd9Sstevel@tonic-gate 				if (err == 0)
30707c478bd9Sstevel@tonic-gate 					err = DOSYM_UNDEF;
30717c478bd9Sstevel@tonic-gate 			}
30727c478bd9Sstevel@tonic-gate 			continue;
30737c478bd9Sstevel@tonic-gate 		}
30747c478bd9Sstevel@tonic-gate 		/*
30757c478bd9Sstevel@tonic-gate 		 * It's a common symbol - st_value is the
30767c478bd9Sstevel@tonic-gate 		 * required alignment.
30777c478bd9Sstevel@tonic-gate 		 */
30787c478bd9Sstevel@tonic-gate 		if (sp->st_value > bss_align)
30797c478bd9Sstevel@tonic-gate 			bss_align = sp->st_value;
30807c478bd9Sstevel@tonic-gate 		bss_ptr = ALIGN(bss_ptr, sp->st_value);
30817c478bd9Sstevel@tonic-gate 		if (assign) {
30827c478bd9Sstevel@tonic-gate 			sp->st_shndx = SHN_ABS;
30837c478bd9Sstevel@tonic-gate 			sp->st_value = bss_ptr;
30847c478bd9Sstevel@tonic-gate 		}
30857c478bd9Sstevel@tonic-gate 		bss_ptr += sp->st_size;
30867c478bd9Sstevel@tonic-gate 	}
30877c478bd9Sstevel@tonic-gate 	if (err)
30887c478bd9Sstevel@tonic-gate 		return (err);
30894da99751SToomas Soome 	if (assign == 0 && mp->bss == 0) {
30907c478bd9Sstevel@tonic-gate 		mp->bss_align = bss_align;
30917c478bd9Sstevel@tonic-gate 		mp->bss_size = bss_ptr;
30927c478bd9Sstevel@tonic-gate 	} else if (resolved) {
30937c478bd9Sstevel@tonic-gate 		mp->flags |= KOBJ_RESOLVED;
30947c478bd9Sstevel@tonic-gate 	}
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	return (0);
30977c478bd9Sstevel@tonic-gate }
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate uint_t
kobj_hash_name(const char * p)31007c478bd9Sstevel@tonic-gate kobj_hash_name(const char *p)
31017c478bd9Sstevel@tonic-gate {
3102986fd29aSsetje 	uint_t g;
31037c478bd9Sstevel@tonic-gate 	uint_t hval;
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 	hval = 0;
31067c478bd9Sstevel@tonic-gate 	while (*p) {
31077c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *p++;
31087c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
31097c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
31107c478bd9Sstevel@tonic-gate 		hval &= ~g;
31117c478bd9Sstevel@tonic-gate 	}
31127c478bd9Sstevel@tonic-gate 	return (hval);
31137c478bd9Sstevel@tonic-gate }
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate /* look for name in all modules */
31167c478bd9Sstevel@tonic-gate uintptr_t
kobj_getsymvalue(char * name,int kernelonly)31177c478bd9Sstevel@tonic-gate kobj_getsymvalue(char *name, int kernelonly)
31187c478bd9Sstevel@tonic-gate {
31197c478bd9Sstevel@tonic-gate 	Sym		*sp;
31207c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
31217c478bd9Sstevel@tonic-gate 	struct module	*mp;
31227c478bd9Sstevel@tonic-gate 	uintptr_t	value = 0;
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	if ((sp = kobj_lookup_kernel(name)) != NULL)
31257c478bd9Sstevel@tonic-gate 		return ((uintptr_t)sp->st_value);
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	if (kernelonly)
31287c478bd9Sstevel@tonic-gate 		return (0);	/* didn't find it in the kernel so give up */
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
31317c478bd9Sstevel@tonic-gate 	modp = &modules;
31327c478bd9Sstevel@tonic-gate 	do {
31337c478bd9Sstevel@tonic-gate 		mp = (struct module *)modp->mod_mp;
31347c478bd9Sstevel@tonic-gate 		if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
31357c478bd9Sstevel@tonic-gate 		    (sp = lookup_one(mp, name))) {
31367c478bd9Sstevel@tonic-gate 			value = (uintptr_t)sp->st_value;
31377c478bd9Sstevel@tonic-gate 			break;
31387c478bd9Sstevel@tonic-gate 		}
31397c478bd9Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
31407c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
31417c478bd9Sstevel@tonic-gate 	return (value);
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate /* look for a symbol near value. */
31457c478bd9Sstevel@tonic-gate char *
kobj_getsymname(uintptr_t value,ulong_t * offset)31467c478bd9Sstevel@tonic-gate kobj_getsymname(uintptr_t value, ulong_t *offset)
31477c478bd9Sstevel@tonic-gate {
31487c478bd9Sstevel@tonic-gate 	char *name = NULL;
31497c478bd9Sstevel@tonic-gate 	struct modctl *modp;
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	struct modctl_list *lp;
31527c478bd9Sstevel@tonic-gate 	struct module *mp;
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	/*
31557c478bd9Sstevel@tonic-gate 	 * Loop through the primary kernel modules.
31567c478bd9Sstevel@tonic-gate 	 */
31577c478bd9Sstevel@tonic-gate 	for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
31587c478bd9Sstevel@tonic-gate 		mp = mod(lp);
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 		if ((name = kobj_searchsym(mp, value, offset)) != NULL)
31617c478bd9Sstevel@tonic-gate 			return (name);
31627c478bd9Sstevel@tonic-gate 	}
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
31657c478bd9Sstevel@tonic-gate 	modp = &modules;
31667c478bd9Sstevel@tonic-gate 	do {
31677c478bd9Sstevel@tonic-gate 		mp = (struct module *)modp->mod_mp;
31687c478bd9Sstevel@tonic-gate 		if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
31697c478bd9Sstevel@tonic-gate 		    (name = kobj_searchsym(mp, value, offset)))
31707c478bd9Sstevel@tonic-gate 			break;
31717c478bd9Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
31727c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
31737c478bd9Sstevel@tonic-gate 	return (name);
31747c478bd9Sstevel@tonic-gate }
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate /* return address of symbol and size */
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate uintptr_t
kobj_getelfsym(char * name,void * mp,int * size)31797c478bd9Sstevel@tonic-gate kobj_getelfsym(char *name, void *mp, int *size)
31807c478bd9Sstevel@tonic-gate {
31817c478bd9Sstevel@tonic-gate 	Sym *sp;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	if (mp == NULL)
31847c478bd9Sstevel@tonic-gate 		sp = kobj_lookup_kernel(name);
31857c478bd9Sstevel@tonic-gate 	else
31867c478bd9Sstevel@tonic-gate 		sp = lookup_one(mp, name);
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	if (sp == NULL)
31897c478bd9Sstevel@tonic-gate 		return (0);
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	*size = (int)sp->st_size;
31927c478bd9Sstevel@tonic-gate 	return ((uintptr_t)sp->st_value);
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate uintptr_t
kobj_lookup(struct module * mod,const char * name)31967aec1d6eScindi kobj_lookup(struct module *mod, const char *name)
31977c478bd9Sstevel@tonic-gate {
31987c478bd9Sstevel@tonic-gate 	Sym *sp;
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 	sp = lookup_one(mod, name);
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 	if (sp == NULL)
32037c478bd9Sstevel@tonic-gate 		return (0);
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	return ((uintptr_t)sp->st_value);
32067c478bd9Sstevel@tonic-gate }
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate char *
kobj_searchsym(struct module * mp,uintptr_t value,ulong_t * offset)32097c478bd9Sstevel@tonic-gate kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset)
32107c478bd9Sstevel@tonic-gate {
32117c478bd9Sstevel@tonic-gate 	Sym *symtabptr;
32127c478bd9Sstevel@tonic-gate 	char *strtabptr;
32137c478bd9Sstevel@tonic-gate 	int symnum;
32147c478bd9Sstevel@tonic-gate 	Sym *sym;
32157c478bd9Sstevel@tonic-gate 	Sym *cursym;
32167c478bd9Sstevel@tonic-gate 	uintptr_t curval;
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	*offset = (ulong_t)-1l;		/* assume not found */
32197c478bd9Sstevel@tonic-gate 	cursym  = NULL;
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(mp, (void *)value) != 0)
32227c478bd9Sstevel@tonic-gate 		return (NULL);		/* not in this module */
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 	strtabptr  = mp->strings;
32257c478bd9Sstevel@tonic-gate 	symtabptr  = (Sym *)mp->symtbl;
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	/*
32287c478bd9Sstevel@tonic-gate 	 * Scan the module's symbol table for a symbol <= value
32297c478bd9Sstevel@tonic-gate 	 */
32307c478bd9Sstevel@tonic-gate 	for (symnum = 1, sym = symtabptr + 1;
32317c478bd9Sstevel@tonic-gate 	    symnum < mp->nsyms; symnum++, sym = (Sym *)
32327c478bd9Sstevel@tonic-gate 	    ((uintptr_t)sym + mp->symhdr->sh_entsize)) {
32337c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
32347c478bd9Sstevel@tonic-gate 			if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
32357c478bd9Sstevel@tonic-gate 				continue;
32367c478bd9Sstevel@tonic-gate 			if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
32377c478bd9Sstevel@tonic-gate 			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
32387c478bd9Sstevel@tonic-gate 				continue;
32397c478bd9Sstevel@tonic-gate 		}
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate 		curval = (uintptr_t)sym->st_value;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 		if (curval > value)
32447c478bd9Sstevel@tonic-gate 			continue;
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 		/*
32477c478bd9Sstevel@tonic-gate 		 * If one or both are functions...
32487c478bd9Sstevel@tonic-gate 		 */
32497c478bd9Sstevel@tonic-gate 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL &&
32507c478bd9Sstevel@tonic-gate 		    ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) {
32517c478bd9Sstevel@tonic-gate 			/* Ignore if the address is out of the bounds */
32527c478bd9Sstevel@tonic-gate 			if (value - sym->st_value >= sym->st_size)
32537c478bd9Sstevel@tonic-gate 				continue;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 			if (cursym != NULL &&
32567c478bd9Sstevel@tonic-gate 			    ELF_ST_TYPE(cursym->st_info) == STT_FUNC) {
32577c478bd9Sstevel@tonic-gate 				/* Prefer the function to the non-function */
32587c478bd9Sstevel@tonic-gate 				if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
32597c478bd9Sstevel@tonic-gate 					continue;
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 				/* Prefer the larger of the two functions */
32627c478bd9Sstevel@tonic-gate 				if (sym->st_size <= cursym->st_size)
32637c478bd9Sstevel@tonic-gate 					continue;
32647c478bd9Sstevel@tonic-gate 			}
32657c478bd9Sstevel@tonic-gate 		} else if (value - curval >= *offset) {
32667c478bd9Sstevel@tonic-gate 			continue;
32677c478bd9Sstevel@tonic-gate 		}
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 		*offset = (ulong_t)(value - curval);
32707c478bd9Sstevel@tonic-gate 		cursym = sym;
32717c478bd9Sstevel@tonic-gate 	}
32727c478bd9Sstevel@tonic-gate 	if (cursym == NULL)
32737c478bd9Sstevel@tonic-gate 		return (NULL);
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	return (strtabptr + cursym->st_name);
32767c478bd9Sstevel@tonic-gate }
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate Sym *
kobj_lookup_all(struct module * mp,char * name,int include_self)32797c478bd9Sstevel@tonic-gate kobj_lookup_all(struct module *mp, char *name, int include_self)
32807c478bd9Sstevel@tonic-gate {
32817c478bd9Sstevel@tonic-gate 	Sym *sp;
32827c478bd9Sstevel@tonic-gate 	struct module_list *mlp;
32837c478bd9Sstevel@tonic-gate 	struct modctl_list *clp;
32847c478bd9Sstevel@tonic-gate 	struct module *mmp;
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	if (include_self && (sp = lookup_one(mp, name)) != NULL)
32877c478bd9Sstevel@tonic-gate 		return (sp);
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	for (mlp = mp->head; mlp; mlp = mlp->next) {
32907c478bd9Sstevel@tonic-gate 		if ((sp = lookup_one(mlp->mp, name)) != NULL &&
32917c478bd9Sstevel@tonic-gate 		    ELF_ST_BIND(sp->st_info) != STB_LOCAL)
32927c478bd9Sstevel@tonic-gate 			return (sp);
32937c478bd9Sstevel@tonic-gate 	}
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	/*
32967c478bd9Sstevel@tonic-gate 	 * Loop through the primary kernel modules.
32977c478bd9Sstevel@tonic-gate 	 */
32987c478bd9Sstevel@tonic-gate 	for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) {
32997c478bd9Sstevel@tonic-gate 		mmp = mod(clp);
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 		if (mmp == NULL || mp == mmp)
33027c478bd9Sstevel@tonic-gate 			continue;
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 		if ((sp = lookup_one(mmp, name)) != NULL &&
33057c478bd9Sstevel@tonic-gate 		    ELF_ST_BIND(sp->st_info) != STB_LOCAL)
33067c478bd9Sstevel@tonic-gate 			return (sp);
33077c478bd9Sstevel@tonic-gate 	}
33087c478bd9Sstevel@tonic-gate 	return (NULL);
33097c478bd9Sstevel@tonic-gate }
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate Sym *
kobj_lookup_kernel(const char * name)33127c478bd9Sstevel@tonic-gate kobj_lookup_kernel(const char *name)
33137c478bd9Sstevel@tonic-gate {
33147c478bd9Sstevel@tonic-gate 	struct modctl_list *lp;
33157c478bd9Sstevel@tonic-gate 	struct module *mp;
33167c478bd9Sstevel@tonic-gate 	Sym *sp;
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	/*
33197c478bd9Sstevel@tonic-gate 	 * Loop through the primary kernel modules.
33207c478bd9Sstevel@tonic-gate 	 */
33217c478bd9Sstevel@tonic-gate 	for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
33227c478bd9Sstevel@tonic-gate 		mp = mod(lp);
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 		if (mp == NULL)
33257c478bd9Sstevel@tonic-gate 			continue;
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 		if ((sp = lookup_one(mp, name)) != NULL)
33287c478bd9Sstevel@tonic-gate 			return (sp);
33297c478bd9Sstevel@tonic-gate 	}
33307c478bd9Sstevel@tonic-gate 	return (NULL);
33317c478bd9Sstevel@tonic-gate }
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate static Sym *
lookup_one(struct module * mp,const char * name)33347c478bd9Sstevel@tonic-gate lookup_one(struct module *mp, const char *name)
33357c478bd9Sstevel@tonic-gate {
33367c478bd9Sstevel@tonic-gate 	symid_t *ip;
33377c478bd9Sstevel@tonic-gate 	char *name1;
33387c478bd9Sstevel@tonic-gate 	Sym *sp;
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate 	for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
33417c478bd9Sstevel@tonic-gate 	    ip = &mp->chains[*ip]) {
33427c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl +
33437c478bd9Sstevel@tonic-gate 		    mp->symhdr->sh_entsize * *ip);
33447c478bd9Sstevel@tonic-gate 		name1 = mp->strings + sp->st_name;
33457c478bd9Sstevel@tonic-gate 		if (strcmp(name, name1) == 0 &&
33467c478bd9Sstevel@tonic-gate 		    ELF_ST_TYPE(sp->st_info) != STT_FILE &&
33477c478bd9Sstevel@tonic-gate 		    sp->st_shndx != SHN_UNDEF &&
33487c478bd9Sstevel@tonic-gate 		    sp->st_shndx != SHN_COMMON)
33497c478bd9Sstevel@tonic-gate 			return (sp);
33507c478bd9Sstevel@tonic-gate 	}
33517c478bd9Sstevel@tonic-gate 	return (NULL);
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate /*
33557c478bd9Sstevel@tonic-gate  * Lookup a given symbol pointer in the module's symbol hash.  If the symbol
33567c478bd9Sstevel@tonic-gate  * is hashed, return the symbol pointer; otherwise return NULL.
33577c478bd9Sstevel@tonic-gate  */
33587c478bd9Sstevel@tonic-gate static Sym *
sym_lookup(struct module * mp,Sym * ksp)33597c478bd9Sstevel@tonic-gate sym_lookup(struct module *mp, Sym *ksp)
33607c478bd9Sstevel@tonic-gate {
33617c478bd9Sstevel@tonic-gate 	char *name = mp->strings + ksp->st_name;
33627c478bd9Sstevel@tonic-gate 	symid_t *ip;
33637c478bd9Sstevel@tonic-gate 	Sym *sp;
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 	for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
33667c478bd9Sstevel@tonic-gate 	    ip = &mp->chains[*ip]) {
33677c478bd9Sstevel@tonic-gate 		sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip);
33687c478bd9Sstevel@tonic-gate 		if (sp == ksp)
33697c478bd9Sstevel@tonic-gate 			return (ksp);
33707c478bd9Sstevel@tonic-gate 	}
33717c478bd9Sstevel@tonic-gate 	return (NULL);
33727c478bd9Sstevel@tonic-gate }
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate static void
sym_insert(struct module * mp,char * name,symid_t index)33757c478bd9Sstevel@tonic-gate sym_insert(struct module *mp, char *name, symid_t index)
33767c478bd9Sstevel@tonic-gate {
33777c478bd9Sstevel@tonic-gate 	symid_t *ip;
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate #ifdef KOBJ_DEBUG
338015c07adcSJohn Levon 	if (kobj_debug & D_SYMBOLS) {
338115c07adcSJohn Levon 		static struct module *lastmp = NULL;
338215c07adcSJohn Levon 		Sym *sp;
338315c07adcSJohn Levon 		if (lastmp != mp) {
338415c07adcSJohn Levon 			_kobj_printf(ops,
338515c07adcSJohn Levon 			    "krtld: symbol entry: file=%s\n",
338615c07adcSJohn Levon 			    mp->filename);
338715c07adcSJohn Levon 			_kobj_printf(ops,
338815c07adcSJohn Levon 			    "krtld:\tsymndx\tvalue\t\t"
338915c07adcSJohn Levon 			    "symbol name\n");
339015c07adcSJohn Levon 			lastmp = mp;
33917c478bd9Sstevel@tonic-gate 		}
339215c07adcSJohn Levon 		sp = (Sym *)(mp->symtbl +
339315c07adcSJohn Levon 		    index * mp->symhdr->sh_entsize);
339415c07adcSJohn Levon 		_kobj_printf(ops, "krtld:\t[%3d]", index);
339515c07adcSJohn Levon 		_kobj_printf(ops, "\t0x%lx", sp->st_value);
339615c07adcSJohn Levon 		_kobj_printf(ops, "\t%s\n", name);
339715c07adcSJohn Levon 	}
33987c478bd9Sstevel@tonic-gate #endif
339915c07adcSJohn Levon 
34007c478bd9Sstevel@tonic-gate 	for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
34017c478bd9Sstevel@tonic-gate 	    ip = &mp->chains[*ip]) {
34027c478bd9Sstevel@tonic-gate 		;
34037c478bd9Sstevel@tonic-gate 	}
34047c478bd9Sstevel@tonic-gate 	*ip = index;
34057c478bd9Sstevel@tonic-gate }
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate struct modctl *
kobj_boot_mod_lookup(const char * modname)34087c478bd9Sstevel@tonic-gate kobj_boot_mod_lookup(const char *modname)
34097c478bd9Sstevel@tonic-gate {
34107c478bd9Sstevel@tonic-gate 	struct modctl *mctl = kobj_modules;
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	do {
34137c478bd9Sstevel@tonic-gate 		if (strcmp(modname, mctl->mod_modname) == 0)
34147c478bd9Sstevel@tonic-gate 			return (mctl);
34157c478bd9Sstevel@tonic-gate 	} while ((mctl = mctl->mod_next) != kobj_modules);
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	return (NULL);
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34205c311300Scth /*
34215c311300Scth  * Determine if the module exists.
34225c311300Scth  */
34235c311300Scth int
kobj_path_exists(char * name,int use_path)34245c311300Scth kobj_path_exists(char *name, int use_path)
34255c311300Scth {
34265c311300Scth 	struct _buf *file;
34275c311300Scth 
34285c311300Scth 	file = kobj_open_path(name, use_path, 1);
34295c311300Scth #ifdef	MODDIR_SUFFIX
34305c311300Scth 	if (file == (struct _buf *)-1)
34315c311300Scth 		file = kobj_open_path(name, use_path, 0);
34325c311300Scth #endif	/* MODDIR_SUFFIX */
34335c311300Scth 	if (file == (struct _buf *)-1)
34345c311300Scth 		return (0);
34355c311300Scth 	kobj_close_file(file);
34365c311300Scth 	return (1);
34375c311300Scth }
34385c311300Scth 
34397c478bd9Sstevel@tonic-gate /*
34407c478bd9Sstevel@tonic-gate  * fullname is dynamically allocated to be able to hold the
34417c478bd9Sstevel@tonic-gate  * maximum size string that can be constructed from name.
34427c478bd9Sstevel@tonic-gate  * path is exactly like the shell PATH variable.
34437c478bd9Sstevel@tonic-gate  */
34447c478bd9Sstevel@tonic-gate struct _buf *
kobj_open_path(char * name,int use_path,int use_moddir_suffix)34457c478bd9Sstevel@tonic-gate kobj_open_path(char *name, int use_path, int use_moddir_suffix)
34467c478bd9Sstevel@tonic-gate {
34477c478bd9Sstevel@tonic-gate 	char *p, *q;
34487c478bd9Sstevel@tonic-gate 	char *pathp;
34497c478bd9Sstevel@tonic-gate 	char *pathpsave;
34507c478bd9Sstevel@tonic-gate 	char *fullname;
34517c478bd9Sstevel@tonic-gate 	int maxpathlen;
34527c478bd9Sstevel@tonic-gate 	struct _buf *file;
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate #if !defined(MODDIR_SUFFIX)
34557c478bd9Sstevel@tonic-gate 	use_moddir_suffix = B_FALSE;
34567c478bd9Sstevel@tonic-gate #endif
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	if (!use_path)
34597c478bd9Sstevel@tonic-gate 		pathp = "";		/* use name as specified */
34607c478bd9Sstevel@tonic-gate 	else
3461ae115bc7Smrj 		pathp = kobj_module_path;
3462ae115bc7Smrj 					/* use configured default path */
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	pathpsave = pathp;		/* keep this for error reporting */
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 	/*
34677c478bd9Sstevel@tonic-gate 	 * Allocate enough space for the largest possible fullname.
34687c478bd9Sstevel@tonic-gate 	 * since path is of the form <directory> : <directory> : ...
34697c478bd9Sstevel@tonic-gate 	 * we're potentially allocating a little more than we need to
34707c478bd9Sstevel@tonic-gate 	 * but we'll allocate the exact amount when we find the right directory.
34717c478bd9Sstevel@tonic-gate 	 * (The + 3 below is one for NULL terminator and one for the '/'
34727c478bd9Sstevel@tonic-gate 	 * we might have to add at the beginning of path and one for
34737c478bd9Sstevel@tonic-gate 	 * the '/' between path and name.)
34747c478bd9Sstevel@tonic-gate 	 */
34757c478bd9Sstevel@tonic-gate 	maxpathlen = strlen(pathp) + strlen(name) + 3;
34767c478bd9Sstevel@tonic-gate 	/* sizeof includes null */
34777c478bd9Sstevel@tonic-gate 	maxpathlen += sizeof (slash_moddir_suffix_slash) - 1;
34787c478bd9Sstevel@tonic-gate 	fullname = kobj_zalloc(maxpathlen, KM_WAIT);
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	for (;;) {
34817c478bd9Sstevel@tonic-gate 		p = fullname;
34827c478bd9Sstevel@tonic-gate 		if (*pathp != '\0' && *pathp != '/')
34837c478bd9Sstevel@tonic-gate 			*p++ = '/';	/* path must start with '/' */
34847c478bd9Sstevel@tonic-gate 		while (*pathp && *pathp != ':' && *pathp != ' ')
34857c478bd9Sstevel@tonic-gate 			*p++ = *pathp++;
34867c478bd9Sstevel@tonic-gate 		if (p != fullname && p[-1] != '/')
34877c478bd9Sstevel@tonic-gate 			*p++ = '/';
34887c478bd9Sstevel@tonic-gate 		if (use_moddir_suffix) {
34897c478bd9Sstevel@tonic-gate 			char *b = basename(name);
34907c478bd9Sstevel@tonic-gate 			char *s;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 			/* copy everything up to the base name */
34937c478bd9Sstevel@tonic-gate 			q = name;
34947c478bd9Sstevel@tonic-gate 			while (q != b && *q)
34957c478bd9Sstevel@tonic-gate 				*p++ = *q++;
34967c478bd9Sstevel@tonic-gate 			s = slash_moddir_suffix_slash;
34977c478bd9Sstevel@tonic-gate 			while (*s)
34987c478bd9Sstevel@tonic-gate 				*p++ = *s++;
34997c478bd9Sstevel@tonic-gate 			/* copy the rest */
35007c478bd9Sstevel@tonic-gate 			while (*b)
35017c478bd9Sstevel@tonic-gate 				*p++ = *b++;
35027c478bd9Sstevel@tonic-gate 		} else {
35037c478bd9Sstevel@tonic-gate 			q = name;
35047c478bd9Sstevel@tonic-gate 			while (*q)
35057c478bd9Sstevel@tonic-gate 				*p++ = *q++;
35067c478bd9Sstevel@tonic-gate 		}
35077c478bd9Sstevel@tonic-gate 		*p = 0;
35087c478bd9Sstevel@tonic-gate 		if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) {
35097c478bd9Sstevel@tonic-gate 			kobj_free(fullname, maxpathlen);
35107c478bd9Sstevel@tonic-gate 			return (file);
35117c478bd9Sstevel@tonic-gate 		}
3512b2940a7cSRichard Lowe 		while (*pathp == ' ' || *pathp == ':')
3513da346f3bSPramod Batni 			pathp++;
35147c478bd9Sstevel@tonic-gate 		if (*pathp == 0)
35157c478bd9Sstevel@tonic-gate 			break;
3516da346f3bSPramod Batni 
35177c478bd9Sstevel@tonic-gate 	}
35187c478bd9Sstevel@tonic-gate 	kobj_free(fullname, maxpathlen);
35197c478bd9Sstevel@tonic-gate 	if (_moddebug & MODDEBUG_ERRMSG) {
35207c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "can't open %s,", name);
35217c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, " path is %s\n", pathpsave);
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 	return ((struct _buf *)-1);
35247c478bd9Sstevel@tonic-gate }
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate intptr_t
kobj_open(char * filename)35277c478bd9Sstevel@tonic-gate kobj_open(char *filename)
35287c478bd9Sstevel@tonic-gate {
35297c478bd9Sstevel@tonic-gate 	struct vnode *vp;
35307c478bd9Sstevel@tonic-gate 	int fd;
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	if (_modrootloaded) {
35337c478bd9Sstevel@tonic-gate 		struct kobjopen_tctl *ltp = kobjopen_alloc(filename);
35347c478bd9Sstevel@tonic-gate 		int Errno;
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 		/*
35377c478bd9Sstevel@tonic-gate 		 * Hand off the open to a thread who has a
35387c478bd9Sstevel@tonic-gate 		 * stack size capable handling the request.
35397c478bd9Sstevel@tonic-gate 		 */
35407c478bd9Sstevel@tonic-gate 		if (curthread != &t0) {
35417c478bd9Sstevel@tonic-gate 			(void) thread_create(NULL, DEFAULTSTKSZ * 2,
35427c478bd9Sstevel@tonic-gate 			    kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri);
35437c478bd9Sstevel@tonic-gate 			sema_p(&ltp->sema);
35447c478bd9Sstevel@tonic-gate 			Errno = ltp->Errno;
35457c478bd9Sstevel@tonic-gate 			vp = ltp->vp;
35467c478bd9Sstevel@tonic-gate 		} else {
35477c478bd9Sstevel@tonic-gate 			/*
35487c478bd9Sstevel@tonic-gate 			 * 1098067: module creds should not be those of the
35497c478bd9Sstevel@tonic-gate 			 * caller
35507c478bd9Sstevel@tonic-gate 			 */
35517c478bd9Sstevel@tonic-gate 			cred_t *saved_cred = curthread->t_cred;
35527c478bd9Sstevel@tonic-gate 			curthread->t_cred = kcred;
3553ea8dc4b6Seschrock 			Errno = vn_openat(filename, UIO_SYSSPACE, FREAD, 0, &vp,
3554da6c28aaSamw 			    0, 0, rootdir, -1);
35557c478bd9Sstevel@tonic-gate 			curthread->t_cred = saved_cred;
35567c478bd9Sstevel@tonic-gate 		}
35577c478bd9Sstevel@tonic-gate 		kobjopen_free(ltp);
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 		if (Errno) {
35607c478bd9Sstevel@tonic-gate 			if (_moddebug & MODDEBUG_ERRMSG) {
35617c478bd9Sstevel@tonic-gate 				_kobj_printf(ops,
35627c478bd9Sstevel@tonic-gate 				    "kobj_open: vn_open of %s fails, ",
35637c478bd9Sstevel@tonic-gate 				    filename);
35647c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "Errno = %d\n", Errno);
35657c478bd9Sstevel@tonic-gate 			}
35667c478bd9Sstevel@tonic-gate 			return (-1);
35677c478bd9Sstevel@tonic-gate 		} else {
35687c478bd9Sstevel@tonic-gate 			if (_moddebug & MODDEBUG_ERRMSG) {
35697c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "kobj_open: '%s'", filename);
35707c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, " vp = %p\n", vp);
35717c478bd9Sstevel@tonic-gate 			}
35727c478bd9Sstevel@tonic-gate 			return ((intptr_t)vp);
35737c478bd9Sstevel@tonic-gate 		}
35747c478bd9Sstevel@tonic-gate 	} else {
35757c478bd9Sstevel@tonic-gate 		fd = kobj_boot_open(filename, 0);
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 		if (_moddebug & MODDEBUG_ERRMSG) {
35787c478bd9Sstevel@tonic-gate 			if (fd < 0)
35797c478bd9Sstevel@tonic-gate 				_kobj_printf(ops,
35807c478bd9Sstevel@tonic-gate 				    "kobj_open: can't open %s\n", filename);
35817c478bd9Sstevel@tonic-gate 			else {
35827c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "kobj_open: '%s'", filename);
35837c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, " descr = 0x%x\n", fd);
35847c478bd9Sstevel@tonic-gate 			}
35857c478bd9Sstevel@tonic-gate 		}
35867c478bd9Sstevel@tonic-gate 		return ((intptr_t)fd);
35877c478bd9Sstevel@tonic-gate 	}
35887c478bd9Sstevel@tonic-gate }
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate /*
35917c478bd9Sstevel@tonic-gate  * Calls to kobj_open() are handled off to this routine as a separate thread.
35927c478bd9Sstevel@tonic-gate  */
35937c478bd9Sstevel@tonic-gate static void
kobjopen_thread(struct kobjopen_tctl * ltp)35947c478bd9Sstevel@tonic-gate kobjopen_thread(struct kobjopen_tctl *ltp)
35957c478bd9Sstevel@tonic-gate {
35967c478bd9Sstevel@tonic-gate 	kmutex_t	cpr_lk;
35977c478bd9Sstevel@tonic-gate 	callb_cpr_t	cpr_i;
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
36007c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen");
36017c478bd9Sstevel@tonic-gate 	ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp),
3602b1b8ab34Slling 	    0, 0);
36037c478bd9Sstevel@tonic-gate 	sema_v(&ltp->sema);
36047c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lk);
36057c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_i);
36067c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lk);
36077c478bd9Sstevel@tonic-gate 	thread_exit();
36087c478bd9Sstevel@tonic-gate }
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate /*
36117c478bd9Sstevel@tonic-gate  * allocate and initialize a kobjopen thread structure
36127c478bd9Sstevel@tonic-gate  */
36137c478bd9Sstevel@tonic-gate static struct kobjopen_tctl *
kobjopen_alloc(char * filename)36147c478bd9Sstevel@tonic-gate kobjopen_alloc(char *filename)
36157c478bd9Sstevel@tonic-gate {
36167c478bd9Sstevel@tonic-gate 	struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP);
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate 	ASSERT(filename != NULL);
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP);
36217c478bd9Sstevel@tonic-gate 	bcopy(filename, ltp->name, strlen(filename) + 1);
36227c478bd9Sstevel@tonic-gate 	sema_init(&ltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
36237c478bd9Sstevel@tonic-gate 	return (ltp);
36247c478bd9Sstevel@tonic-gate }
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate /*
36277c478bd9Sstevel@tonic-gate  * free a kobjopen thread control structure
36287c478bd9Sstevel@tonic-gate  */
36297c478bd9Sstevel@tonic-gate static void
kobjopen_free(struct kobjopen_tctl * ltp)36307c478bd9Sstevel@tonic-gate kobjopen_free(struct kobjopen_tctl *ltp)
36317c478bd9Sstevel@tonic-gate {
36327c478bd9Sstevel@tonic-gate 	sema_destroy(&ltp->sema);
36337c478bd9Sstevel@tonic-gate 	kmem_free(ltp->name, strlen(ltp->name) + 1);
36347c478bd9Sstevel@tonic-gate 	kmem_free(ltp, sizeof (*ltp));
36357c478bd9Sstevel@tonic-gate }
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate int
kobj_read(intptr_t descr,char * buf,uint_t size,uint_t offset)3638986fd29aSsetje kobj_read(intptr_t descr, char *buf, uint_t size, uint_t offset)
36397c478bd9Sstevel@tonic-gate {
36407c478bd9Sstevel@tonic-gate 	int stat;
36417c478bd9Sstevel@tonic-gate 	ssize_t resid;
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate 	if (_modrootloaded) {
36447c478bd9Sstevel@tonic-gate 		if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size,
36457c478bd9Sstevel@tonic-gate 		    (offset_t)offset, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(),
36467c478bd9Sstevel@tonic-gate 		    &resid)) != 0) {
36477c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
36487c478bd9Sstevel@tonic-gate 			    "vn_rdwr failed with error 0x%x\n", stat);
36497c478bd9Sstevel@tonic-gate 			return (-1);
36507c478bd9Sstevel@tonic-gate 		}
36517c478bd9Sstevel@tonic-gate 		return (size - resid);
36527c478bd9Sstevel@tonic-gate 	} else {
36537c478bd9Sstevel@tonic-gate 		int count = 0;
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 		if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) {
36567c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
36577c478bd9Sstevel@tonic-gate 			    "kobj_read: seek 0x%x failed\n", offset);
36587c478bd9Sstevel@tonic-gate 			return (-1);
36597c478bd9Sstevel@tonic-gate 		}
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 		count = kobj_boot_read((int)descr, buf, size);
36627c478bd9Sstevel@tonic-gate 		if (count < size) {
36637c478bd9Sstevel@tonic-gate 			if (_moddebug & MODDEBUG_ERRMSG) {
36647c478bd9Sstevel@tonic-gate 				_kobj_printf(ops,
36657c478bd9Sstevel@tonic-gate 				    "kobj_read: req %d bytes, ", size);
36667c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "got %d\n", count);
36677c478bd9Sstevel@tonic-gate 			}
36687c478bd9Sstevel@tonic-gate 		}
36697c478bd9Sstevel@tonic-gate 		return (count);
36707c478bd9Sstevel@tonic-gate 	}
36717c478bd9Sstevel@tonic-gate }
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate void
kobj_close(intptr_t descr)36747c478bd9Sstevel@tonic-gate kobj_close(intptr_t descr)
36757c478bd9Sstevel@tonic-gate {
36767c478bd9Sstevel@tonic-gate 	if (_moddebug & MODDEBUG_ERRMSG)
36777c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "kobj_close: 0x%lx\n", descr);
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 	if (_modrootloaded) {
36807c478bd9Sstevel@tonic-gate 		struct vnode *vp = (struct vnode *)descr;
3681da6c28aaSamw 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
36827c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
36837c478bd9Sstevel@tonic-gate 	} else
36847c478bd9Sstevel@tonic-gate 		(void) kobj_boot_close((int)descr);
36857c478bd9Sstevel@tonic-gate }
36867c478bd9Sstevel@tonic-gate 
3687ea8dc4b6Seschrock int
kobj_fstat(intptr_t descr,struct bootstat * buf)3688ea8dc4b6Seschrock kobj_fstat(intptr_t descr, struct bootstat *buf)
3689ea8dc4b6Seschrock {
3690ea8dc4b6Seschrock 	if (buf == NULL)
3691ea8dc4b6Seschrock 		return (-1);
3692ea8dc4b6Seschrock 
3693ea8dc4b6Seschrock 	if (_modrootloaded) {
3694ea8dc4b6Seschrock 		vattr_t vattr;
3695ea8dc4b6Seschrock 		struct vnode *vp = (struct vnode *)descr;
3696da6c28aaSamw 		if (VOP_GETATTR(vp, &vattr, 0, kcred, NULL) != 0)
3697ea8dc4b6Seschrock 			return (-1);
3698ea8dc4b6Seschrock 
3699ea8dc4b6Seschrock 		/*
3700ea8dc4b6Seschrock 		 * The vattr and bootstat structures are similar, but not
3701ea8dc4b6Seschrock 		 * identical.  We do our best to fill in the bootstat structure
3702ea8dc4b6Seschrock 		 * from the contents of vattr (transfering only the ones that
3703ea8dc4b6Seschrock 		 * are obvious.
3704ea8dc4b6Seschrock 		 */
3705ea8dc4b6Seschrock 
3706ea8dc4b6Seschrock 		buf->st_mode = (uint32_t)vattr.va_mode;
3707ea8dc4b6Seschrock 		buf->st_nlink = (uint32_t)vattr.va_nlink;
3708ea8dc4b6Seschrock 		buf->st_uid = (int32_t)vattr.va_uid;
3709ea8dc4b6Seschrock 		buf->st_gid = (int32_t)vattr.va_gid;
3710ea8dc4b6Seschrock 		buf->st_rdev = (uint64_t)vattr.va_rdev;
3711ea8dc4b6Seschrock 		buf->st_size = (uint64_t)vattr.va_size;
3712ea8dc4b6Seschrock 		buf->st_atim.tv_sec = (int64_t)vattr.va_atime.tv_sec;
3713ea8dc4b6Seschrock 		buf->st_atim.tv_nsec = (int64_t)vattr.va_atime.tv_nsec;
3714ea8dc4b6Seschrock 		buf->st_mtim.tv_sec = (int64_t)vattr.va_mtime.tv_sec;
3715ea8dc4b6Seschrock 		buf->st_mtim.tv_nsec = (int64_t)vattr.va_mtime.tv_nsec;
3716ea8dc4b6Seschrock 		buf->st_ctim.tv_sec = (int64_t)vattr.va_ctime.tv_sec;
3717ea8dc4b6Seschrock 		buf->st_ctim.tv_nsec = (int64_t)vattr.va_ctime.tv_nsec;
3718ea8dc4b6Seschrock 		buf->st_blksize = (int32_t)vattr.va_blksize;
3719ea8dc4b6Seschrock 		buf->st_blocks = (int64_t)vattr.va_nblocks;
3720ea8dc4b6Seschrock 
3721ea8dc4b6Seschrock 		return (0);
3722ea8dc4b6Seschrock 	}
3723ea8dc4b6Seschrock 
3724ea8dc4b6Seschrock 	return (kobj_boot_fstat((int)descr, buf));
3725ea8dc4b6Seschrock }
3726ea8dc4b6Seschrock 
3727ea8dc4b6Seschrock 
37287c478bd9Sstevel@tonic-gate struct _buf *
kobj_open_file(char * name)37297c478bd9Sstevel@tonic-gate kobj_open_file(char *name)
37307c478bd9Sstevel@tonic-gate {
37317c478bd9Sstevel@tonic-gate 	struct _buf *file;
3732986fd29aSsetje 	struct compinfo cbuf;
37337c478bd9Sstevel@tonic-gate 	intptr_t fd;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	if ((fd = kobj_open(name)) == -1) {
37367c478bd9Sstevel@tonic-gate 		return ((struct _buf *)-1);
37377c478bd9Sstevel@tonic-gate 	}
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 	file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP);
37407c478bd9Sstevel@tonic-gate 	file->_fd = fd;
37417c478bd9Sstevel@tonic-gate 	file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP);
37427c478bd9Sstevel@tonic-gate 	file->_cnt = file->_size = file->_off = 0;
37437c478bd9Sstevel@tonic-gate 	file->_ln = 1;
37447c478bd9Sstevel@tonic-gate 	file->_ptr = file->_base;
37457c478bd9Sstevel@tonic-gate 	(void) strcpy(file->_name, name);
3746986fd29aSsetje 
3747986fd29aSsetje 	/*
3748986fd29aSsetje 	 * Before root is mounted, we must check
3749986fd29aSsetje 	 * for a compressed file and do our own
3750986fd29aSsetje 	 * buffering.
3751986fd29aSsetje 	 */
3752986fd29aSsetje 	if (_modrootloaded) {
3753986fd29aSsetje 		file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT);
3754986fd29aSsetje 		file->_bsize = MAXBSIZE;
37551d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
37561d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		/* Check if the file is compressed */
3757215280f2SKrishnendu Sadhukhan - Sun Microsystems 		file->_iscmp = kobj_is_compressed(fd);
3758986fd29aSsetje 	} else {
3759986fd29aSsetje 		if (kobj_boot_compinfo(fd, &cbuf) != 0) {
3760986fd29aSsetje 			kobj_close_file(file);
3761986fd29aSsetje 			return ((struct _buf *)-1);
3762986fd29aSsetje 		}
3763986fd29aSsetje 		file->_iscmp = cbuf.iscmp;
3764986fd29aSsetje 		if (file->_iscmp) {
3765986fd29aSsetje 			if (kobj_comp_setup(file, &cbuf) != 0) {
3766986fd29aSsetje 				kobj_close_file(file);
3767986fd29aSsetje 				return ((struct _buf *)-1);
3768986fd29aSsetje 			}
3769986fd29aSsetje 		} else {
3770986fd29aSsetje 			file->_base = kobj_zalloc(cbuf.blksize, KM_WAIT|KM_TMP);
3771986fd29aSsetje 			file->_bsize = cbuf.blksize;
3772986fd29aSsetje 		}
3773986fd29aSsetje 	}
37747c478bd9Sstevel@tonic-gate 	return (file);
37757c478bd9Sstevel@tonic-gate }
37767c478bd9Sstevel@tonic-gate 
3777986fd29aSsetje static int
kobj_comp_setup(struct _buf * file,struct compinfo * cip)3778986fd29aSsetje kobj_comp_setup(struct _buf *file, struct compinfo *cip)
3779986fd29aSsetje {
3780986fd29aSsetje 	struct comphdr *hdr;
3781986fd29aSsetje 
3782986fd29aSsetje 	/*
3783986fd29aSsetje 	 * read the compressed image into memory,
3784986fd29aSsetje 	 * so we can deompress from there
3785986fd29aSsetje 	 */
3786986fd29aSsetje 	file->_dsize = cip->fsize;
3787986fd29aSsetje 	file->_dbuf = kobj_alloc(cip->fsize, KM_WAIT|KM_TMP);
3788986fd29aSsetje 	if (kobj_read(file->_fd, file->_dbuf, cip->fsize, 0) != cip->fsize) {
3789986fd29aSsetje 		kobj_free(file->_dbuf, cip->fsize);
3790986fd29aSsetje 		return (-1);
3791986fd29aSsetje 	}
3792986fd29aSsetje 
3793986fd29aSsetje 	hdr = kobj_comphdr(file);
37941d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	if (hdr->ch_magic != CH_MAGIC_ZLIB || hdr->ch_version != CH_VERSION ||
3795986fd29aSsetje 	    hdr->ch_algorithm != CH_ALG_ZLIB || hdr->ch_fsize == 0 ||
3796de710d24SJosef 'Jeff' Sipek 	    !ISP2(hdr->ch_blksize)) {
3797986fd29aSsetje 		kobj_free(file->_dbuf, cip->fsize);
3798986fd29aSsetje 		return (-1);
3799986fd29aSsetje 	}
3800986fd29aSsetje 	file->_base = kobj_alloc(hdr->ch_blksize, KM_WAIT|KM_TMP);
3801986fd29aSsetje 	file->_bsize = hdr->ch_blksize;
3802986fd29aSsetje 	return (0);
3803986fd29aSsetje }
3804986fd29aSsetje 
38057c478bd9Sstevel@tonic-gate void
kobj_close_file(struct _buf * file)38067c478bd9Sstevel@tonic-gate kobj_close_file(struct _buf *file)
38077c478bd9Sstevel@tonic-gate {
38087c478bd9Sstevel@tonic-gate 	kobj_close(file->_fd);
3809986fd29aSsetje 	if (file->_base != NULL)
3810986fd29aSsetje 		kobj_free(file->_base, file->_bsize);
3811986fd29aSsetje 	if (file->_dbuf != NULL)
3812986fd29aSsetje 		kobj_free(file->_dbuf, file->_dsize);
38137c478bd9Sstevel@tonic-gate 	kobj_free(file->_name, strlen(file->_name)+1);
38147c478bd9Sstevel@tonic-gate 	kobj_free(file, sizeof (struct _buf));
38157c478bd9Sstevel@tonic-gate }
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate int
kobj_read_file(struct _buf * file,char * buf,uint_t size,uint_t off)3818986fd29aSsetje kobj_read_file(struct _buf *file, char *buf, uint_t size, uint_t off)
38197c478bd9Sstevel@tonic-gate {
38207c478bd9Sstevel@tonic-gate 	int b_size, c_size;
38217c478bd9Sstevel@tonic-gate 	int b_off;	/* Offset into buffer for start of bcopy */
38227c478bd9Sstevel@tonic-gate 	int count = 0;
38237c478bd9Sstevel@tonic-gate 	int page_addr;
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	if (_moddebug & MODDEBUG_ERRMSG) {
38267c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "kobj_read_file: size=%x,", size);
38277c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, " offset=%x at", off);
3828c61137dfSJohn Levon 		_kobj_printf(ops, " buf=%lx\n", (uintptr_t)buf);
38297c478bd9Sstevel@tonic-gate 	}
38307c478bd9Sstevel@tonic-gate 
38311d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	/*
38321d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	 * Handle compressed (gzip for now) file here. First get the
38331d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	 * compressed size, then read the image into memory and finally
38341d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	 * call zlib to decompress the image at the supplied memory buffer.
38351d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	 */
3836215280f2SKrishnendu Sadhukhan - Sun Microsystems 	if (file->_iscmp == CH_MAGIC_GZIP) {
38371d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		ulong_t dlen;
38381d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		vattr_t vattr;
38391d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		struct vnode *vp = (struct vnode *)file->_fd;
38401d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		ssize_t resid;
38411d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		int err = 0;
38421d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38431d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		if (VOP_GETATTR(vp, &vattr, 0, kcred, NULL) != 0)
38441d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			return (-1);
38451d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38461d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		file->_dbuf = kobj_alloc(vattr.va_size, KM_WAIT|KM_TMP);
38471d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		file->_dsize = vattr.va_size;
38481d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38491d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		/* Read the compressed file into memory */
38501d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		if ((err = vn_rdwr(UIO_READ, vp, file->_dbuf, vattr.va_size,
38511d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		    (offset_t)(0), UIO_SYSSPACE, 0, (rlim64_t)0, CRED(),
38521d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		    &resid)) != 0) {
38531d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38541d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			_kobj_printf(ops, "kobj_read_file :vn_rdwr() failed, "
38551d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			    "error code 0x%x\n", err);
38561d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			return (-1);
38571d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		}
38581d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38591d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		dlen = size;
38601d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38611d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		/* Decompress the image at the supplied memory buffer */
38621d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		if ((err = z_uncompress(buf, &dlen, file->_dbuf,
38631d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		    vattr.va_size)) != Z_OK) {
38641d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			_kobj_printf(ops, "kobj_read_file: z_uncompress "
38651d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			    "failed, error code : 0x%x\n", err);
38661d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			return (-1);
38671d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		}
38681d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38691d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		if (dlen != size) {
38701d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			_kobj_printf(ops, "kobj_read_file: z_uncompress "
3871c61137dfSJohn Levon 			    "failed to uncompress (size returned 0x%lx , "
38721d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			    "expected size: 0x%x)\n", dlen, size);
38731d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			return (-1);
38741d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		}
38751d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38761d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		return (0);
38771d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	}
38781d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
38797c478bd9Sstevel@tonic-gate 	while (size) {
3880986fd29aSsetje 		page_addr = F_PAGE(file, off);
38817c478bd9Sstevel@tonic-gate 		b_size = file->_size;
38827c478bd9Sstevel@tonic-gate 		/*
38837c478bd9Sstevel@tonic-gate 		 * If we have the filesystem page the caller's referring to
38847c478bd9Sstevel@tonic-gate 		 * and we have something in the buffer,
38857c478bd9Sstevel@tonic-gate 		 * satisfy as much of the request from the buffer as we can.
38867c478bd9Sstevel@tonic-gate 		 */
38877c478bd9Sstevel@tonic-gate 		if (page_addr == file->_off && b_size > 0) {
3888986fd29aSsetje 			b_off = B_OFFSET(file, off);
38897c478bd9Sstevel@tonic-gate 			c_size = b_size - b_off;
38907c478bd9Sstevel@tonic-gate 			/*
38917c478bd9Sstevel@tonic-gate 			 * If there's nothing to copy, we're at EOF.
38927c478bd9Sstevel@tonic-gate 			 */
38937c478bd9Sstevel@tonic-gate 			if (c_size <= 0)
38947c478bd9Sstevel@tonic-gate 				break;
38957c478bd9Sstevel@tonic-gate 			if (c_size > size)
38967c478bd9Sstevel@tonic-gate 				c_size = size;
38977c478bd9Sstevel@tonic-gate 			if (buf) {
38987c478bd9Sstevel@tonic-gate 				if (_moddebug & MODDEBUG_ERRMSG)
38997c478bd9Sstevel@tonic-gate 					_kobj_printf(ops, "copying %x bytes\n",
39007c478bd9Sstevel@tonic-gate 					    c_size);
39017c478bd9Sstevel@tonic-gate 				bcopy(file->_base+b_off, buf, c_size);
39027c478bd9Sstevel@tonic-gate 				size -= c_size;
39037c478bd9Sstevel@tonic-gate 				off += c_size;
39047c478bd9Sstevel@tonic-gate 				buf += c_size;
39057c478bd9Sstevel@tonic-gate 				count += c_size;
39067c478bd9Sstevel@tonic-gate 			} else {
39077c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "kobj_read: system error");
39087c478bd9Sstevel@tonic-gate 				count = -1;
39097c478bd9Sstevel@tonic-gate 				break;
39107c478bd9Sstevel@tonic-gate 			}
39117c478bd9Sstevel@tonic-gate 		} else {
39127c478bd9Sstevel@tonic-gate 			/*
39137c478bd9Sstevel@tonic-gate 			 * If the caller's offset is page aligned and
39147c478bd9Sstevel@tonic-gate 			 * the caller want's at least a filesystem page and
39157c478bd9Sstevel@tonic-gate 			 * the caller provided a buffer,
39167c478bd9Sstevel@tonic-gate 			 * read directly into the caller's buffer.
39177c478bd9Sstevel@tonic-gate 			 */
39187c478bd9Sstevel@tonic-gate 			if (page_addr == off &&
3919986fd29aSsetje 			    (c_size = F_BLKS(file, size)) && buf) {
3920986fd29aSsetje 				c_size = kobj_read_blks(file, buf, c_size,
3921b1b8ab34Slling 				    page_addr);
39227c478bd9Sstevel@tonic-gate 				if (c_size < 0) {
39237c478bd9Sstevel@tonic-gate 					count = -1;
39247c478bd9Sstevel@tonic-gate 					break;
39257c478bd9Sstevel@tonic-gate 				}
39267c478bd9Sstevel@tonic-gate 				count += c_size;
3927986fd29aSsetje 				if (c_size != F_BLKS(file, size))
39287c478bd9Sstevel@tonic-gate 					break;
39297c478bd9Sstevel@tonic-gate 				size -= c_size;
39307c478bd9Sstevel@tonic-gate 				off += c_size;
39317c478bd9Sstevel@tonic-gate 				buf += c_size;
39327c478bd9Sstevel@tonic-gate 			/*
39337c478bd9Sstevel@tonic-gate 			 * Otherwise, read into our buffer and copy next time
39347c478bd9Sstevel@tonic-gate 			 * around the loop.
39357c478bd9Sstevel@tonic-gate 			 */
39367c478bd9Sstevel@tonic-gate 			} else {
39377c478bd9Sstevel@tonic-gate 				file->_off = page_addr;
3938986fd29aSsetje 				c_size = kobj_read_blks(file, file->_base,
3939986fd29aSsetje 				    file->_bsize, page_addr);
39407c478bd9Sstevel@tonic-gate 				file->_ptr = file->_base;
39417c478bd9Sstevel@tonic-gate 				file->_cnt = c_size;
39427c478bd9Sstevel@tonic-gate 				file->_size = c_size;
39437c478bd9Sstevel@tonic-gate 				/*
39447c478bd9Sstevel@tonic-gate 				 * If a _filbuf call or nothing read, break.
39457c478bd9Sstevel@tonic-gate 				 */
39467c478bd9Sstevel@tonic-gate 				if (buf == NULL || c_size <= 0) {
39477c478bd9Sstevel@tonic-gate 					count = c_size;
39487c478bd9Sstevel@tonic-gate 					break;
39497c478bd9Sstevel@tonic-gate 				}
39507c478bd9Sstevel@tonic-gate 			}
39517c478bd9Sstevel@tonic-gate 			if (_moddebug & MODDEBUG_ERRMSG)
39527c478bd9Sstevel@tonic-gate 				_kobj_printf(ops, "read %x bytes\n", c_size);
39537c478bd9Sstevel@tonic-gate 		}
39547c478bd9Sstevel@tonic-gate 	}
39557c478bd9Sstevel@tonic-gate 	if (_moddebug & MODDEBUG_ERRMSG)
39567c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "count = %x\n", count);
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 	return (count);
39597c478bd9Sstevel@tonic-gate }
39607c478bd9Sstevel@tonic-gate 
3961986fd29aSsetje static int
kobj_read_blks(struct _buf * file,char * buf,uint_t size,uint_t off)3962986fd29aSsetje kobj_read_blks(struct _buf *file, char *buf, uint_t size, uint_t off)
3963986fd29aSsetje {
3964986fd29aSsetje 	int ret;
3965986fd29aSsetje 
3966986fd29aSsetje 	ASSERT(B_OFFSET(file, size) == 0 && B_OFFSET(file, off) == 0);
3967986fd29aSsetje 	if (file->_iscmp) {
3968986fd29aSsetje 		uint_t blks;
3969986fd29aSsetje 		int nret;
3970986fd29aSsetje 
3971986fd29aSsetje 		ret = 0;
3972986fd29aSsetje 		for (blks = size / file->_bsize; blks != 0; blks--) {
3973986fd29aSsetje 			nret = kobj_uncomp_blk(file, buf, off);
3974986fd29aSsetje 			if (nret == -1)
3975986fd29aSsetje 				return (-1);
3976986fd29aSsetje 			buf += nret;
3977986fd29aSsetje 			off += nret;
3978986fd29aSsetje 			ret += nret;
3979986fd29aSsetje 			if (nret < file->_bsize)
3980986fd29aSsetje 				break;
3981986fd29aSsetje 		}
3982986fd29aSsetje 	} else
3983986fd29aSsetje 		ret = kobj_read(file->_fd, buf, size, off);
3984986fd29aSsetje 	return (ret);
3985986fd29aSsetje }
3986986fd29aSsetje 
3987986fd29aSsetje static int
kobj_uncomp_blk(struct _buf * file,char * buf,uint_t off)3988986fd29aSsetje kobj_uncomp_blk(struct _buf *file, char *buf, uint_t off)
3989986fd29aSsetje {
3990986fd29aSsetje 	struct comphdr *hdr = kobj_comphdr(file);
3991986fd29aSsetje 	ulong_t dlen, slen;
3992986fd29aSsetje 	caddr_t src;
3993986fd29aSsetje 	int i;
3994986fd29aSsetje 
3995986fd29aSsetje 	dlen = file->_bsize;
3996986fd29aSsetje 	i = off / file->_bsize;
3997986fd29aSsetje 	src = file->_dbuf + hdr->ch_blkmap[i];
3998986fd29aSsetje 	if (i == hdr->ch_fsize / file->_bsize)
399979755401Ssetje 		slen = file->_dsize - hdr->ch_blkmap[i];
4000986fd29aSsetje 	else
4001986fd29aSsetje 		slen = hdr->ch_blkmap[i + 1] - hdr->ch_blkmap[i];
4002986fd29aSsetje 	if (z_uncompress(buf, &dlen, src, slen) != Z_OK)
4003986fd29aSsetje 		return (-1);
4004986fd29aSsetje 	return (dlen);
4005986fd29aSsetje }
4006986fd29aSsetje 
40077c478bd9Sstevel@tonic-gate int
kobj_filbuf(struct _buf * f)40087c478bd9Sstevel@tonic-gate kobj_filbuf(struct _buf *f)
40097c478bd9Sstevel@tonic-gate {
4010986fd29aSsetje 	if (kobj_read_file(f, NULL, f->_bsize, f->_off + f->_size) > 0)
40117c478bd9Sstevel@tonic-gate 		return (kobj_getc(f));
40127c478bd9Sstevel@tonic-gate 	return (-1);
40137c478bd9Sstevel@tonic-gate }
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate void
kobj_free(void * address,size_t size)40167c478bd9Sstevel@tonic-gate kobj_free(void *address, size_t size)
40177c478bd9Sstevel@tonic-gate {
40187c478bd9Sstevel@tonic-gate 	if (standalone)
40197c478bd9Sstevel@tonic-gate 		return;
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate 	kmem_free(address, size);
40227c478bd9Sstevel@tonic-gate 	kobj_stat.nfree_calls++;
40237c478bd9Sstevel@tonic-gate 	kobj_stat.nfree += size;
40247c478bd9Sstevel@tonic-gate }
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate void *
kobj_zalloc(size_t size,int flag)40277c478bd9Sstevel@tonic-gate kobj_zalloc(size_t size, int flag)
40287c478bd9Sstevel@tonic-gate {
40297c478bd9Sstevel@tonic-gate 	void *v;
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate 	if ((v = kobj_alloc(size, flag)) != 0) {
40327c478bd9Sstevel@tonic-gate 		bzero(v, size);
40337c478bd9Sstevel@tonic-gate 	}
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	return (v);
40367c478bd9Sstevel@tonic-gate }
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate void *
kobj_alloc(size_t size,int flag)40397c478bd9Sstevel@tonic-gate kobj_alloc(size_t size, int flag)
40407c478bd9Sstevel@tonic-gate {
40417c478bd9Sstevel@tonic-gate 	/*
40427c478bd9Sstevel@tonic-gate 	 * If we are running standalone in the
40437c478bd9Sstevel@tonic-gate 	 * linker, we ask boot for memory.
40447c478bd9Sstevel@tonic-gate 	 * Either it's temporary memory that we lose
40457c478bd9Sstevel@tonic-gate 	 * once boot is mapped out or we allocate it
40467c478bd9Sstevel@tonic-gate 	 * permanently using the dynamic data segment.
40477c478bd9Sstevel@tonic-gate 	 */
40487c478bd9Sstevel@tonic-gate 	if (standalone) {
4049986fd29aSsetje #if defined(_OBP)
4050986fd29aSsetje 		if (flag & (KM_TMP | KM_SCRATCH))
4051986fd29aSsetje 			return (bop_temp_alloc(size, MINALIGN));
4052986fd29aSsetje #else
40537c478bd9Sstevel@tonic-gate 		if (flag & (KM_TMP | KM_SCRATCH))
40547c478bd9Sstevel@tonic-gate 			return (BOP_ALLOC(ops, 0, size, MINALIGN));
40557c478bd9Sstevel@tonic-gate #endif
40567c478bd9Sstevel@tonic-gate 		return (kobj_segbrk(&_edata, size, MINALIGN, 0));
40577c478bd9Sstevel@tonic-gate 	}
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	kobj_stat.nalloc_calls++;
40607c478bd9Sstevel@tonic-gate 	kobj_stat.nalloc += size;
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 	return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP));
40637c478bd9Sstevel@tonic-gate }
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate /*
40667c478bd9Sstevel@tonic-gate  * Allow the "mod" system to sync up with the work
40677c478bd9Sstevel@tonic-gate  * already done by kobj during the initial loading
40687c478bd9Sstevel@tonic-gate  * of the kernel.  This also gives us a chance
40697c478bd9Sstevel@tonic-gate  * to reallocate memory that belongs to boot.
40707c478bd9Sstevel@tonic-gate  */
40717c478bd9Sstevel@tonic-gate void
kobj_sync(void)40727c478bd9Sstevel@tonic-gate kobj_sync(void)
40737c478bd9Sstevel@tonic-gate {
40747c478bd9Sstevel@tonic-gate 	struct modctl_list *lp, **lpp;
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 	/*
4077ae115bc7Smrj 	 * The module path can be set in /etc/system via 'moddir' commands
40787c478bd9Sstevel@tonic-gate 	 */
40797c478bd9Sstevel@tonic-gate 	if (default_path != NULL)
4080ae115bc7Smrj 		kobj_module_path = default_path;
40817c478bd9Sstevel@tonic-gate 	else
4082ae115bc7Smrj 		default_path = kobj_module_path;
40837c478bd9Sstevel@tonic-gate 
40847c478bd9Sstevel@tonic-gate 	ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t),
40857c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t),
40887c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	/*
40917c478bd9Sstevel@tonic-gate 	 * Move symbol tables from boot memory to ksyms_arena.
40927c478bd9Sstevel@tonic-gate 	 */
40937c478bd9Sstevel@tonic-gate 	for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
40947c478bd9Sstevel@tonic-gate 		for (lp = *lpp; lp != NULL; lp = lp->modl_next)
40957c478bd9Sstevel@tonic-gate 			kobj_export_module(mod(lp));
40967c478bd9Sstevel@tonic-gate 	}
40977c478bd9Sstevel@tonic-gate }
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate caddr_t
kobj_segbrk(caddr_t * spp,size_t size,size_t align,caddr_t limit)41007c478bd9Sstevel@tonic-gate kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit)
41017c478bd9Sstevel@tonic-gate {
41027c478bd9Sstevel@tonic-gate 	uintptr_t va, pva;
41037c478bd9Sstevel@tonic-gate 	size_t alloc_pgsz = kobj_mmu_pagesize;
41047c478bd9Sstevel@tonic-gate 	size_t alloc_align = BO_NO_ALIGN;
41057c478bd9Sstevel@tonic-gate 	size_t alloc_size;
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate 	/*
41087c478bd9Sstevel@tonic-gate 	 * If we are using "large" mappings for the kernel,
41097c478bd9Sstevel@tonic-gate 	 * request aligned memory from boot using the
41107c478bd9Sstevel@tonic-gate 	 * "large" pagesize.
41117c478bd9Sstevel@tonic-gate 	 */
41127c478bd9Sstevel@tonic-gate 	if (lg_pagesize) {
41137c478bd9Sstevel@tonic-gate 		alloc_align = lg_pagesize;
41147c478bd9Sstevel@tonic-gate 		alloc_pgsz = lg_pagesize;
41157c478bd9Sstevel@tonic-gate 	}
41161ee144f5Sjg 
41171ee144f5Sjg #if defined(__sparc)
41181ee144f5Sjg 	/* account for redzone */
41191ee144f5Sjg 	if (limit)
41201ee144f5Sjg 		limit -= alloc_pgsz;
41211ee144f5Sjg #endif	/* __sparc */
41221ee144f5Sjg 
41237c478bd9Sstevel@tonic-gate 	va = ALIGN((uintptr_t)*spp, align);
41247c478bd9Sstevel@tonic-gate 	pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz);
41257c478bd9Sstevel@tonic-gate 	/*
41267c478bd9Sstevel@tonic-gate 	 * Need more pages?
41277c478bd9Sstevel@tonic-gate 	 */
41287c478bd9Sstevel@tonic-gate 	if (va + size > pva) {
4129ae115bc7Smrj 		uintptr_t npva;
4130ae115bc7Smrj 
41317c478bd9Sstevel@tonic-gate 		alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz);
41327c478bd9Sstevel@tonic-gate 		/*
41337c478bd9Sstevel@tonic-gate 		 * Check for overlapping segments.
41347c478bd9Sstevel@tonic-gate 		 */
4135ae115bc7Smrj 		if (limit && limit <= *spp + alloc_size) {
41367c478bd9Sstevel@tonic-gate 			return ((caddr_t)0);
4137ae115bc7Smrj 		}
41387c478bd9Sstevel@tonic-gate 
4139ae115bc7Smrj 		npva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva,
4140b1b8ab34Slling 		    alloc_size, alloc_align);
4141ae115bc7Smrj 
41424da99751SToomas Soome 		if (npva == 0) {
4143ae115bc7Smrj 			_kobj_printf(ops, "BOP_ALLOC failed, 0x%lx bytes",
41447c478bd9Sstevel@tonic-gate 			    alloc_size);
4145ae115bc7Smrj 			_kobj_printf(ops, " aligned %lx", alloc_align);
41467c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " at 0x%lx\n", pva);
4147ae115bc7Smrj 			return (NULL);
41487c478bd9Sstevel@tonic-gate 		}
41497c478bd9Sstevel@tonic-gate 	}
41507c478bd9Sstevel@tonic-gate 	*spp = (caddr_t)(va + size);
41517c478bd9Sstevel@tonic-gate 
41527c478bd9Sstevel@tonic-gate 	return ((caddr_t)va);
41537c478bd9Sstevel@tonic-gate }
41547c478bd9Sstevel@tonic-gate 
41557c478bd9Sstevel@tonic-gate /*
41567c478bd9Sstevel@tonic-gate  * Calculate the number of output hash buckets.
41577c478bd9Sstevel@tonic-gate  * We use the next prime larger than n / 4,
41587c478bd9Sstevel@tonic-gate  * so the average hash chain is about 4 entries.
41597c478bd9Sstevel@tonic-gate  * More buckets would just be a waste of memory.
41607c478bd9Sstevel@tonic-gate  */
41617c478bd9Sstevel@tonic-gate uint_t
kobj_gethashsize(uint_t n)41627c478bd9Sstevel@tonic-gate kobj_gethashsize(uint_t n)
41637c478bd9Sstevel@tonic-gate {
41647c478bd9Sstevel@tonic-gate 	int f;
41657c478bd9Sstevel@tonic-gate 	int hsize = MAX(n / 4, 2);
41667c478bd9Sstevel@tonic-gate 
41677c478bd9Sstevel@tonic-gate 	for (f = 2; f * f <= hsize; f++)
41687c478bd9Sstevel@tonic-gate 		if (hsize % f == 0)
41697c478bd9Sstevel@tonic-gate 			hsize += f = 1;
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate 	return (hsize);
41727c478bd9Sstevel@tonic-gate }
41737c478bd9Sstevel@tonic-gate 
4174b1b8ab34Slling /*
4175b1b8ab34Slling  * Get the file size.
4176b1b8ab34Slling  *
4177b1b8ab34Slling  * Before root is mounted, files are compressed in the boot_archive ramdisk
4178b1b8ab34Slling  * (in the memory). kobj_fstat would return the compressed file size.
4179b1b8ab34Slling  * In order to get the uncompressed file size, read the file to the end and
4180b1b8ab34Slling  * count its size.
4181b1b8ab34Slling  */
4182b1b8ab34Slling int
kobj_get_filesize(struct _buf * file,uint64_t * size)4183b1b8ab34Slling kobj_get_filesize(struct _buf *file, uint64_t *size)
4184b1b8ab34Slling {
41851d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	int err = 0;
41861d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	ssize_t resid;
41871d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	uint32_t buf;
41881d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
4189b1b8ab34Slling 	if (_modrootloaded) {
4190b1b8ab34Slling 		struct bootstat bst;
4191b1b8ab34Slling 
4192b1b8ab34Slling 		if (kobj_fstat(file->_fd, &bst) != 0)
4193b1b8ab34Slling 			return (EIO);
4194b1b8ab34Slling 		*size = bst.st_size;
41951d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
4196215280f2SKrishnendu Sadhukhan - Sun Microsystems 		if (file->_iscmp == CH_MAGIC_GZIP) {
41971d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			/*
41981d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			 * Read the last 4 bytes of the compressed (gzip)
41991d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			 * image to get the size of its uncompressed
42001d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			 * version.
42011d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			 */
42021d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			if ((err = vn_rdwr(UIO_READ, (struct vnode *)file->_fd,
42031d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			    (char *)(&buf), 4, (offset_t)(*size - 4),
42041d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			    UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
42051d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			    != 0) {
42061d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 				_kobj_printf(ops, "kobj_get_filesize: "
42071d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 				    "vn_rdwr() failed with error 0x%x\n", err);
42081d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 				return (-1);
42091d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			}
42101d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
42111d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 			*size =  (uint64_t)buf;
42121d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		}
4213b1b8ab34Slling 	} else {
4214986fd29aSsetje 
4215986fd29aSsetje #if defined(_OBP)
4216986fd29aSsetje 		struct bootstat bsb;
4217986fd29aSsetje 
4218986fd29aSsetje 		if (file->_iscmp) {
4219986fd29aSsetje 			struct comphdr *hdr = kobj_comphdr(file);
4220986fd29aSsetje 
4221986fd29aSsetje 			*size = hdr->ch_fsize;
4222986fd29aSsetje 		} else if (kobj_boot_fstat(file->_fd, &bsb) != 0)
4223986fd29aSsetje 			return (EIO);
4224986fd29aSsetje 		else
4225986fd29aSsetje 			*size = bsb.st_size;
4226986fd29aSsetje #else
4227b1b8ab34Slling 		char *buf;
4228b1b8ab34Slling 		int count;
4229b1b8ab34Slling 		uint64_t offset = 0;
4230b1b8ab34Slling 
4231b1b8ab34Slling 		buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
4232b1b8ab34Slling 		do {
4233b1b8ab34Slling 			count = kobj_read_file(file, buf, MAXBSIZE, offset);
4234b1b8ab34Slling 			if (count < 0) {
4235b1b8ab34Slling 				kmem_free(buf, MAXBSIZE);
4236b1b8ab34Slling 				return (EIO);
4237b1b8ab34Slling 			}
4238b1b8ab34Slling 			offset += count;
4239b1b8ab34Slling 		} while (count == MAXBSIZE);
4240b1b8ab34Slling 		kmem_free(buf, MAXBSIZE);
4241b1b8ab34Slling 
4242b1b8ab34Slling 		*size = offset;
4243986fd29aSsetje #endif
4244b1b8ab34Slling 	}
4245b1b8ab34Slling 
4246b1b8ab34Slling 	return (0);
4247b1b8ab34Slling }
4248b1b8ab34Slling 
42497c478bd9Sstevel@tonic-gate static char *
basename(char * s)42507c478bd9Sstevel@tonic-gate basename(char *s)
42517c478bd9Sstevel@tonic-gate {
42527c478bd9Sstevel@tonic-gate 	char *p, *q;
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate 	q = NULL;
42557c478bd9Sstevel@tonic-gate 	p = s;
42567c478bd9Sstevel@tonic-gate 	do {
42577c478bd9Sstevel@tonic-gate 		if (*p == '/')
42587c478bd9Sstevel@tonic-gate 			q = p;
42597c478bd9Sstevel@tonic-gate 	} while (*p++);
42607c478bd9Sstevel@tonic-gate 	return (q ? q + 1 : s);
42617c478bd9Sstevel@tonic-gate }
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate void
kobj_stat_get(kobj_stat_t * kp)42647c478bd9Sstevel@tonic-gate kobj_stat_get(kobj_stat_t *kp)
42657c478bd9Sstevel@tonic-gate {
42667c478bd9Sstevel@tonic-gate 	*kp = kobj_stat;
42677c478bd9Sstevel@tonic-gate }
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate int
kobj_getpagesize()42707c478bd9Sstevel@tonic-gate kobj_getpagesize()
42717c478bd9Sstevel@tonic-gate {
42727c478bd9Sstevel@tonic-gate 	return (lg_pagesize);
42737c478bd9Sstevel@tonic-gate }
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate void
kobj_textwin_alloc(struct module * mp)42767c478bd9Sstevel@tonic-gate kobj_textwin_alloc(struct module *mp)
42777c478bd9Sstevel@tonic-gate {
42787c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
42797c478bd9Sstevel@tonic-gate 
42807c478bd9Sstevel@tonic-gate 	if (mp->textwin != NULL)
42817c478bd9Sstevel@tonic-gate 		return;
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate 	/*
42847c478bd9Sstevel@tonic-gate 	 * If the text is not contained in the heap, then it is not contained
42857c478bd9Sstevel@tonic-gate 	 * by a writable mapping.  (Specifically, it's on the nucleus page.)
42867c478bd9Sstevel@tonic-gate 	 * We allocate a read/write mapping for this module's text to allow
42877c478bd9Sstevel@tonic-gate 	 * the text to be patched without calling hot_patch_kernel_text()
42887c478bd9Sstevel@tonic-gate 	 * (which is quite slow).
42897c478bd9Sstevel@tonic-gate 	 */
42907c478bd9Sstevel@tonic-gate 	if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) {
42917c478bd9Sstevel@tonic-gate 		uintptr_t text = (uintptr_t)mp->text;
42927c478bd9Sstevel@tonic-gate 		uintptr_t size = (uintptr_t)mp->text_size;
42937c478bd9Sstevel@tonic-gate 		uintptr_t i;
42947c478bd9Sstevel@tonic-gate 		caddr_t va;
42957c478bd9Sstevel@tonic-gate 		size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) -
42967c478bd9Sstevel@tonic-gate 		    (text & PAGEMASK);
42977c478bd9Sstevel@tonic-gate 
42987c478bd9Sstevel@tonic-gate 		va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP);
42997c478bd9Sstevel@tonic-gate 
43007c478bd9Sstevel@tonic-gate 		for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) {
43017c478bd9Sstevel@tonic-gate 			hat_devload(kas.a_hat, va, PAGESIZE,
43027c478bd9Sstevel@tonic-gate 			    hat_getpfnum(kas.a_hat, (caddr_t)i),
43037c478bd9Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE,
43047c478bd9Sstevel@tonic-gate 			    HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
43057c478bd9Sstevel@tonic-gate 			va += PAGESIZE;
43067c478bd9Sstevel@tonic-gate 		}
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate 		mp->textwin = mp->textwin_base + (text & PAGEOFFSET);
43097c478bd9Sstevel@tonic-gate 	} else {
43107c478bd9Sstevel@tonic-gate 		mp->textwin = mp->text;
43117c478bd9Sstevel@tonic-gate 	}
43127c478bd9Sstevel@tonic-gate }
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate void
kobj_textwin_free(struct module * mp)43157c478bd9Sstevel@tonic-gate kobj_textwin_free(struct module *mp)
43167c478bd9Sstevel@tonic-gate {
43177c478bd9Sstevel@tonic-gate 	uintptr_t text = (uintptr_t)mp->text;
43187c478bd9Sstevel@tonic-gate 	uintptr_t tsize = (uintptr_t)mp->text_size;
43197c478bd9Sstevel@tonic-gate 	size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) -
43207c478bd9Sstevel@tonic-gate 	    (text & PAGEMASK));
43217c478bd9Sstevel@tonic-gate 
43227c478bd9Sstevel@tonic-gate 	mp->textwin = NULL;
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	if (mp->textwin_base == NULL)
43257c478bd9Sstevel@tonic-gate 		return;
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK);
43287c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, mp->textwin_base, size);
43297c478bd9Sstevel@tonic-gate 	mp->textwin_base = NULL;
43307c478bd9Sstevel@tonic-gate }
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate static char *
find_libmacro(char * name)43337c478bd9Sstevel@tonic-gate find_libmacro(char *name)
43347c478bd9Sstevel@tonic-gate {
43357c478bd9Sstevel@tonic-gate 	int lmi;
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate 	for (lmi = 0; lmi < NLIBMACROS; lmi++) {
43387c478bd9Sstevel@tonic-gate 		if (strcmp(name, libmacros[lmi].lmi_macroname) == 0)
43397c478bd9Sstevel@tonic-gate 			return (libmacros[lmi].lmi_list);
43407c478bd9Sstevel@tonic-gate 	}
43417c478bd9Sstevel@tonic-gate 	return (NULL);
43427c478bd9Sstevel@tonic-gate }
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate /*
43457c478bd9Sstevel@tonic-gate  * Check for $MACRO in tail (string to expand) and expand it in path at pathend
43467c478bd9Sstevel@tonic-gate  * returns path if successful, else NULL
43477c478bd9Sstevel@tonic-gate  * Support multiple $MACROs expansion and the first valid path will be returned
43487c478bd9Sstevel@tonic-gate  * Caller's responsibility to provide enough space in path to expand
43497c478bd9Sstevel@tonic-gate  */
43507c478bd9Sstevel@tonic-gate char *
expand_libmacro(char * tail,char * path,char * pathend)43517c478bd9Sstevel@tonic-gate expand_libmacro(char *tail, char *path, char *pathend)
43527c478bd9Sstevel@tonic-gate {
43537c478bd9Sstevel@tonic-gate 	char c, *p, *p1, *p2, *path2, *endp;
43547c478bd9Sstevel@tonic-gate 	int diff, lmi, macrolen, valid_macro, more_macro;
43557c478bd9Sstevel@tonic-gate 	struct _buf *file;
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 	/*
43587c478bd9Sstevel@tonic-gate 	 * check for $MACROS between nulls or slashes
43597c478bd9Sstevel@tonic-gate 	 */
43607c478bd9Sstevel@tonic-gate 	p = strchr(tail, '$');
43617c478bd9Sstevel@tonic-gate 	if (p == NULL)
43627c478bd9Sstevel@tonic-gate 		return (NULL);
43637c478bd9Sstevel@tonic-gate 	for (lmi = 0; lmi < NLIBMACROS; lmi++) {
43647c478bd9Sstevel@tonic-gate 		macrolen = libmacros[lmi].lmi_macrolen;
43657c478bd9Sstevel@tonic-gate 		if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0)
43667c478bd9Sstevel@tonic-gate 			break;
43677c478bd9Sstevel@tonic-gate 	}
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	valid_macro = 0;
43707c478bd9Sstevel@tonic-gate 	if (lmi < NLIBMACROS) {
43717c478bd9Sstevel@tonic-gate 		/*
43727c478bd9Sstevel@tonic-gate 		 * The following checks are used to restrict expansion of
43737c478bd9Sstevel@tonic-gate 		 * macros to those that form a full directory/file name
43747c478bd9Sstevel@tonic-gate 		 * and to keep the behavior same as before.  If this
43757c478bd9Sstevel@tonic-gate 		 * restriction is removed or no longer valid in the future,
43767c478bd9Sstevel@tonic-gate 		 * the checks below can be deleted.
43777c478bd9Sstevel@tonic-gate 		 */
43787c478bd9Sstevel@tonic-gate 		if ((p == tail) || (*(p - 1) == '/')) {
43797c478bd9Sstevel@tonic-gate 			c = *(p + macrolen + 1);
43807c478bd9Sstevel@tonic-gate 			if (c == '/' || c == '\0')
43817c478bd9Sstevel@tonic-gate 				valid_macro = 1;
43827c478bd9Sstevel@tonic-gate 		}
43837c478bd9Sstevel@tonic-gate 	}
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	if (!valid_macro) {
43867c478bd9Sstevel@tonic-gate 		p2 = strchr(p, '/');
43877c478bd9Sstevel@tonic-gate 		/*
43887c478bd9Sstevel@tonic-gate 		 * if no more macro to expand, then just copy whatever left
43897c478bd9Sstevel@tonic-gate 		 * and check whether it exists
43907c478bd9Sstevel@tonic-gate 		 */
43917c478bd9Sstevel@tonic-gate 		if (p2 == NULL || strchr(p2, '$') == NULL) {
43927c478bd9Sstevel@tonic-gate 			(void) strcpy(pathend, tail);
43937c478bd9Sstevel@tonic-gate 			if ((file = kobj_open_path(path, 1, 1)) !=
43947c478bd9Sstevel@tonic-gate 			    (struct _buf *)-1) {
43957c478bd9Sstevel@tonic-gate 				kobj_close_file(file);
43967c478bd9Sstevel@tonic-gate 				return (path);
43977c478bd9Sstevel@tonic-gate 			} else
43987c478bd9Sstevel@tonic-gate 				return (NULL);
43997c478bd9Sstevel@tonic-gate 		} else {
44007c478bd9Sstevel@tonic-gate 			/*
44017c478bd9Sstevel@tonic-gate 			 * copy all chars before '/' and call expand_libmacro()
44027c478bd9Sstevel@tonic-gate 			 * again
44037c478bd9Sstevel@tonic-gate 			 */
44047c478bd9Sstevel@tonic-gate 			diff = p2 - tail;
44057c478bd9Sstevel@tonic-gate 			bcopy(tail, pathend, diff);
44067c478bd9Sstevel@tonic-gate 			pathend += diff;
44077c478bd9Sstevel@tonic-gate 			*(pathend) = '\0';
44087c478bd9Sstevel@tonic-gate 			return (expand_libmacro(p2, path, pathend));
44097c478bd9Sstevel@tonic-gate 		}
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	more_macro = 0;
44137c478bd9Sstevel@tonic-gate 	if (c != '\0') {
44147c478bd9Sstevel@tonic-gate 		endp = p + macrolen + 1;
44157c478bd9Sstevel@tonic-gate 		if (strchr(endp, '$') != NULL)
44167c478bd9Sstevel@tonic-gate 			more_macro = 1;
44177c478bd9Sstevel@tonic-gate 	} else
44187c478bd9Sstevel@tonic-gate 		endp = NULL;
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 	/*
44217c478bd9Sstevel@tonic-gate 	 * copy lmi_list and split it into components.
44227c478bd9Sstevel@tonic-gate 	 * then put the part of tail before $MACRO into path
44237c478bd9Sstevel@tonic-gate 	 * at pathend
44247c478bd9Sstevel@tonic-gate 	 */
44257c478bd9Sstevel@tonic-gate 	diff = p - tail;
44267c478bd9Sstevel@tonic-gate 	if (diff > 0)
44277c478bd9Sstevel@tonic-gate 		bcopy(tail, pathend, diff);
44287c478bd9Sstevel@tonic-gate 	path2 = pathend + diff;
44297c478bd9Sstevel@tonic-gate 	p1 = libmacros[lmi].lmi_list;
44307c478bd9Sstevel@tonic-gate 	while (p1 && (*p1 != '\0')) {
44317c478bd9Sstevel@tonic-gate 		p2 = strchr(p1, ':');
44327c478bd9Sstevel@tonic-gate 		if (p2) {
44337c478bd9Sstevel@tonic-gate 			diff = p2 - p1;
44347c478bd9Sstevel@tonic-gate 			bcopy(p1, path2, diff);
44357c478bd9Sstevel@tonic-gate 			*(path2 + diff) = '\0';
44367c478bd9Sstevel@tonic-gate 		} else {
44377c478bd9Sstevel@tonic-gate 			diff = strlen(p1);
44387c478bd9Sstevel@tonic-gate 			bcopy(p1, path2, diff + 1);
44397c478bd9Sstevel@tonic-gate 		}
44407c478bd9Sstevel@tonic-gate 		/* copy endp only if there isn't any more macro to expand */
44417c478bd9Sstevel@tonic-gate 		if (!more_macro && (endp != NULL))
44427c478bd9Sstevel@tonic-gate 			(void) strcat(path2, endp);
44437c478bd9Sstevel@tonic-gate 		file = kobj_open_path(path, 1, 1);
44447c478bd9Sstevel@tonic-gate 		if (file != (struct _buf *)-1) {
44457c478bd9Sstevel@tonic-gate 			kobj_close_file(file);
44467c478bd9Sstevel@tonic-gate 			/*
44477c478bd9Sstevel@tonic-gate 			 * if more macros to expand then call expand_libmacro(),
44487c478bd9Sstevel@tonic-gate 			 * else return path which has the whole path
44497c478bd9Sstevel@tonic-gate 			 */
44507c478bd9Sstevel@tonic-gate 			if (!more_macro || (expand_libmacro(endp, path,
44517c478bd9Sstevel@tonic-gate 			    path2 + diff) != NULL)) {
44527c478bd9Sstevel@tonic-gate 				return (path);
44537c478bd9Sstevel@tonic-gate 			}
44547c478bd9Sstevel@tonic-gate 		}
44557c478bd9Sstevel@tonic-gate 		if (p2)
44567c478bd9Sstevel@tonic-gate 			p1 = ++p2;
44577c478bd9Sstevel@tonic-gate 		else
44587c478bd9Sstevel@tonic-gate 			return (NULL);
44597c478bd9Sstevel@tonic-gate 	}
44607c478bd9Sstevel@tonic-gate 	return (NULL);
44617c478bd9Sstevel@tonic-gate }
44627c478bd9Sstevel@tonic-gate 
4463986fd29aSsetje char *kobj_file_buf;
4464986fd29aSsetje int kobj_file_bufsize;
4465986fd29aSsetje 
44667c478bd9Sstevel@tonic-gate /*
44677c478bd9Sstevel@tonic-gate  * This code is for the purpose of manually recording which files
44687c478bd9Sstevel@tonic-gate  * needs to go into the boot archive on any given system.
44697c478bd9Sstevel@tonic-gate  *
44707c478bd9Sstevel@tonic-gate  * To enable the code, set kobj_file_bufsize in /etc/system
44717c478bd9Sstevel@tonic-gate  * and reboot the system, then use mdb to look at kobj_file_buf.
44727c478bd9Sstevel@tonic-gate  */
44737c478bd9Sstevel@tonic-gate static void
kobj_record_file(char * filename)44747c478bd9Sstevel@tonic-gate kobj_record_file(char *filename)
44757c478bd9Sstevel@tonic-gate {
44767c478bd9Sstevel@tonic-gate 	static char *buf;
44777c478bd9Sstevel@tonic-gate 	static int size = 0;
44787c478bd9Sstevel@tonic-gate 	int n;
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 	if (kobj_file_bufsize == 0)	/* don't bother */
44817c478bd9Sstevel@tonic-gate 		return;
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	if (kobj_file_buf == NULL) {	/* allocate buffer */
44847c478bd9Sstevel@tonic-gate 		size = kobj_file_bufsize;
44857c478bd9Sstevel@tonic-gate 		buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP);
44867c478bd9Sstevel@tonic-gate 	}
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	n = snprintf(buf, size, "%s\n", filename);
44897c478bd9Sstevel@tonic-gate 	if (n > size)
44907c478bd9Sstevel@tonic-gate 		n = size;
44917c478bd9Sstevel@tonic-gate 	size -= n;
44927c478bd9Sstevel@tonic-gate 	buf += n;
44937c478bd9Sstevel@tonic-gate }
44947c478bd9Sstevel@tonic-gate 
4495ea8dc4b6Seschrock static int
kobj_boot_fstat(int fd,struct bootstat * stp)4496ea8dc4b6Seschrock kobj_boot_fstat(int fd, struct bootstat *stp)
4497ea8dc4b6Seschrock {
4498986fd29aSsetje #if defined(_OBP)
4499ea8dc4b6Seschrock 	if (!standalone && _ioquiesced)
4500ea8dc4b6Seschrock 		return (-1);
4501ea8dc4b6Seschrock 	return (BOP_FSTAT(ops, fd, stp));
4502ea8dc4b6Seschrock #else
4503ea8dc4b6Seschrock 	return (BRD_FSTAT(bfs_ops, fd, stp));
4504ea8dc4b6Seschrock #endif
4505ea8dc4b6Seschrock }
4506ea8dc4b6Seschrock 
45077c478bd9Sstevel@tonic-gate static int
kobj_boot_open(char * filename,int flags)45087c478bd9Sstevel@tonic-gate kobj_boot_open(char *filename, int flags)
45097c478bd9Sstevel@tonic-gate {
4510986fd29aSsetje #if defined(_OBP)
4511986fd29aSsetje 
45127c478bd9Sstevel@tonic-gate 	/*
45137c478bd9Sstevel@tonic-gate 	 * If io via bootops is quiesced, it means boot is no longer
45147c478bd9Sstevel@tonic-gate 	 * available to us.  We make it look as if we can't open the
45157c478bd9Sstevel@tonic-gate 	 * named file - which is reasonably accurate.
45167c478bd9Sstevel@tonic-gate 	 */
45177c478bd9Sstevel@tonic-gate 	if (!standalone && _ioquiesced)
45187c478bd9Sstevel@tonic-gate 		return (-1);
45197c478bd9Sstevel@tonic-gate 
4520986fd29aSsetje 	kobj_record_file(filename);
4521986fd29aSsetje 	return (BOP_OPEN(filename, flags));
45227c478bd9Sstevel@tonic-gate #else /* x86 */
45237c478bd9Sstevel@tonic-gate 	kobj_record_file(filename);
45247c478bd9Sstevel@tonic-gate 	return (BRD_OPEN(bfs_ops, filename, flags));
45257c478bd9Sstevel@tonic-gate #endif
45267c478bd9Sstevel@tonic-gate }
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate static int
kobj_boot_close(int fd)45297c478bd9Sstevel@tonic-gate kobj_boot_close(int fd)
45307c478bd9Sstevel@tonic-gate {
4531986fd29aSsetje #if defined(_OBP)
45327c478bd9Sstevel@tonic-gate 	if (!standalone && _ioquiesced)
45337c478bd9Sstevel@tonic-gate 		return (-1);
45347c478bd9Sstevel@tonic-gate 
4535986fd29aSsetje 	return (BOP_CLOSE(fd));
45367c478bd9Sstevel@tonic-gate #else /* x86 */
45377c478bd9Sstevel@tonic-gate 	return (BRD_CLOSE(bfs_ops, fd));
45387c478bd9Sstevel@tonic-gate #endif
45397c478bd9Sstevel@tonic-gate }
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
45427c478bd9Sstevel@tonic-gate static int
kobj_boot_seek(int fd,off_t hi,off_t lo)45437c478bd9Sstevel@tonic-gate kobj_boot_seek(int fd, off_t hi, off_t lo)
45447c478bd9Sstevel@tonic-gate {
4545986fd29aSsetje #if defined(_OBP)
4546986fd29aSsetje 	return (BOP_SEEK(fd, lo) == -1 ? -1 : 0);
45477c478bd9Sstevel@tonic-gate #else
45487c478bd9Sstevel@tonic-gate 	return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET));
45497c478bd9Sstevel@tonic-gate #endif
45507c478bd9Sstevel@tonic-gate }
45517c478bd9Sstevel@tonic-gate 
45527c478bd9Sstevel@tonic-gate static int
kobj_boot_read(int fd,caddr_t buf,size_t size)45537c478bd9Sstevel@tonic-gate kobj_boot_read(int fd, caddr_t buf, size_t size)
45547c478bd9Sstevel@tonic-gate {
4555986fd29aSsetje #if defined(_OBP)
4556986fd29aSsetje 	return (BOP_READ(fd, buf, size));
45577c478bd9Sstevel@tonic-gate #else
45587c478bd9Sstevel@tonic-gate 	return (BRD_READ(bfs_ops, fd, buf, size));
45597c478bd9Sstevel@tonic-gate #endif
45607c478bd9Sstevel@tonic-gate }
4561986fd29aSsetje 
4562986fd29aSsetje static int
kobj_boot_compinfo(int fd,struct compinfo * cb)4563986fd29aSsetje kobj_boot_compinfo(int fd, struct compinfo *cb)
4564986fd29aSsetje {
4565986fd29aSsetje 	return (boot_compinfo(fd, cb));
4566986fd29aSsetje }
45671d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
45681d7f3fadSKrishnendu Sadhukhan - Sun Microsystems /*
45691d7f3fadSKrishnendu Sadhukhan - Sun Microsystems  * Check if the file is compressed (for now we handle only gzip).
4570215280f2SKrishnendu Sadhukhan - Sun Microsystems  * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
45711d7f3fadSKrishnendu Sadhukhan - Sun Microsystems  */
45721d7f3fadSKrishnendu Sadhukhan - Sun Microsystems static int
kobj_is_compressed(intptr_t fd)45731d7f3fadSKrishnendu Sadhukhan - Sun Microsystems kobj_is_compressed(intptr_t fd)
45741d7f3fadSKrishnendu Sadhukhan - Sun Microsystems {
45751d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	struct vnode *vp = (struct vnode *)fd;
45761d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	ssize_t resid;
45771d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	uint16_t magic_buf;
45781d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	int err = 0;
45791d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
45801d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	if ((err = vn_rdwr(UIO_READ, vp, (caddr_t)((intptr_t)&magic_buf),
45811d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	    sizeof (magic_buf), (offset_t)(0),
45821d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	    UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) != 0) {
45831d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
45841d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		_kobj_printf(ops, "kobj_is_compressed: vn_rdwr() failed, "
45851d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		    "error code 0x%x\n", err);
45861d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 		return (0);
45871d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	}
45881d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
45891d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	if (magic_buf == CH_MAGIC_GZIP)
4590215280f2SKrishnendu Sadhukhan - Sun Microsystems 		return (CH_MAGIC_GZIP);
45911d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 
45921d7f3fadSKrishnendu Sadhukhan - Sun Microsystems 	return (0);
45931d7f3fadSKrishnendu Sadhukhan - Sun Microsystems }
4594