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