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