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