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