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