preprocessor cleanup: __sparc
[unleashed/tickless.git] / kernel / krtld / kobj.c
blob199632886ea9509c484ca879f819896f67cf6c81
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2011 Bayard G. Bell <buffer.g.overflow@gmail.com>.
27 * All rights reserved. Use is subject to license terms.
31 * Kernel's linker/loader
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/user.h>
39 #include <sys/kmem.h>
40 #include <sys/reboot.h>
41 #include <sys/bootconf.h>
42 #include <sys/debug.h>
43 #include <sys/uio.h>
44 #include <sys/file.h>
45 #include <sys/vnode.h>
46 #include <sys/user.h>
47 #include <sys/mman.h>
48 #include <vm/as.h>
49 #include <vm/seg_kp.h>
50 #include <vm/seg_kmem.h>
51 #include <sys/elf.h>
52 #include <sys/elf_notes.h>
53 #include <sys/vmsystm.h>
54 #include <sys/kdi.h>
55 #include <sys/atomic.h>
56 #include <sys/kmdb.h>
58 #include <sys/link.h>
59 #include <sys/kobj.h>
60 #include <sys/ksyms.h>
61 #include <sys/disp.h>
62 #include <sys/modctl.h>
63 #include <sys/varargs.h>
64 #include <sys/kstat.h>
65 #include <sys/kobj_impl.h>
66 #include <sys/fs/decomp.h>
67 #include <sys/callb.h>
68 #include <sys/cmn_err.h>
69 #include <sys/tnf_probe.h>
70 #include <sys/zmod.h>
72 #include <krtld/reloc.h>
73 #include <krtld/kobj_kdi.h>
74 #include <sys/sha1.h>
75 #include <sys/crypto/elfsign.h>
77 #if !defined(_OBP)
78 #include <sys/bootvfs.h>
79 #endif
82 * do_symbols() error codes
84 #define DOSYM_UNDEF -1 /* undefined symbol */
85 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */
87 #if !defined(_OBP)
88 static void synthetic_bootaux(char *, val_t *);
89 #endif
91 static struct module *load_exec(val_t *, char *);
92 static void load_linker(val_t *);
93 static struct modctl *add_primary(const char *filename, int);
94 static int bind_primary(val_t *, int);
95 static int load_primary(struct module *, int);
96 static int load_kmdb(val_t *);
97 static int get_progbits(struct module *, struct _buf *);
98 static int get_syms(struct module *, struct _buf *);
99 static int get_ctf(struct module *, struct _buf *);
100 static void get_signature(struct module *, struct _buf *);
101 static int do_common(struct module *);
102 static void add_dependent(struct module *, struct module *);
103 static int do_dependents(struct modctl *, char *, size_t);
104 static int do_symbols(struct module *, Elf64_Addr);
105 static void module_assign(struct modctl *, struct module *);
106 static void free_module_data(struct module *);
107 static char *depends_on(struct module *);
108 static char *getmodpath(const char *);
109 static char *basename(char *);
110 static void attr_val(val_t *);
111 static char *find_libmacro(char *);
112 static char *expand_libmacro(char *, char *, char *);
113 static int read_bootflags(void);
114 static int kobj_comp_setup(struct _buf *, struct compinfo *);
115 static int kobj_uncomp_blk(struct _buf *, caddr_t, uint_t);
116 static int kobj_read_blks(struct _buf *, caddr_t, uint_t, uint_t);
117 static int kobj_boot_open(char *, int);
118 static int kobj_boot_close(int);
119 static int kobj_boot_seek(int, off_t, off_t);
120 static int kobj_boot_read(int, caddr_t, size_t);
121 static int kobj_boot_fstat(int, struct bootstat *);
122 static int kobj_boot_compinfo(int, struct compinfo *);
123 extern int kobj_boot_mountroot(void);
125 static Sym *lookup_one(struct module *, const char *);
126 static void sym_insert(struct module *, char *, symid_t);
127 static Sym *sym_lookup(struct module *, Sym *);
129 static struct kobjopen_tctl *kobjopen_alloc(char *filename);
130 static void kobjopen_free(struct kobjopen_tctl *ltp);
131 static void kobjopen_thread(struct kobjopen_tctl *ltp);
132 static int kobj_is_compressed(intptr_t);
134 extern int kcopy(const void *, void *, size_t);
135 extern int elf_mach_ok(Ehdr *);
136 extern int alloc_gottable(struct module *, caddr_t *, caddr_t *);
138 static void tnf_unsplice_probes(uint_t, struct modctl *);
139 extern tnf_probe_control_t *__tnf_probe_list_head;
140 extern tnf_tag_data_t *__tnf_tag_list_head;
142 extern int modrootloaded;
143 extern int swaploaded;
144 extern int bop_io_quiesced;
145 extern int last_module_id;
147 extern char stubs_base[];
148 extern char stubs_end[];
150 #ifdef KOBJ_DEBUG
152 * Values that can be or'd in to kobj_debug and their effects:
154 * D_DEBUG - misc. debugging information.
155 * D_SYMBOLS - list symbols and their values as they are entered
156 * into the hash table
157 * D_RELOCATIONS - display relocation processing information
158 * D_LOADING - display information about each module as it
159 * is loaded.
161 int kobj_debug = 0;
163 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
164 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
165 #else
166 #define KOBJ_MARK(s) /* discard */
167 #endif
169 #define MODPATH_PROPNAME "module-path"
171 #ifdef MODDIR_SUFFIX
172 static char slash_moddir_suffix_slash[] = MODDIR_SUFFIX "/";
173 #else
174 #define slash_moddir_suffix_slash ""
175 #endif
177 #define _moddebug get_weakish_int(&moddebug)
178 #define _modrootloaded get_weakish_int(&modrootloaded)
179 #define _swaploaded get_weakish_int(&swaploaded)
180 #define _ioquiesced get_weakish_int(&bop_io_quiesced)
182 #define mod(X) (struct module *)((X)->modl_modp->mod_mp)
184 void *romp; /* rom vector (opaque to us) */
185 struct bootops *ops; /* bootops vector */
186 void *dbvec; /* debug vector */
189 * kobjopen thread control structure
191 struct kobjopen_tctl {
192 ksema_t sema;
193 char *name; /* name of file */
194 struct vnode *vp; /* vnode return from vn_open() */
195 int Errno; /* error return from vnopen */
199 * Structure for defining dynamically expandable library macros
202 struct lib_macro_info {
203 char *lmi_list; /* ptr to list of possible choices */
204 char *lmi_macroname; /* pointer to macro name */
205 ushort_t lmi_ba_index; /* index into bootaux vector */
206 ushort_t lmi_macrolen; /* macro length */
207 } libmacros[] = {
208 { NULL, "CPU", BA_CPU, 0 },
209 { NULL, "MMU", BA_MMU, 0 }
212 #define NLIBMACROS sizeof (libmacros) / sizeof (struct lib_macro_info)
214 char *boot_cpu_compatible_list; /* make $CPU available */
216 char *kobj_module_path; /* module search path */
217 vmem_t *text_arena; /* module text arena */
218 static vmem_t *data_arena; /* module data & bss arena */
219 static vmem_t *ctf_arena; /* CTF debug data arena */
220 static struct modctl *kobj_modules = NULL; /* modules loaded */
221 int kobj_mmu_pagesize; /* system pagesize */
222 static int lg_pagesize; /* "large" pagesize */
223 static int kobj_last_module_id = 0; /* id assignment */
224 static kmutex_t kobj_lock; /* protects mach memory list */
227 * The following functions have been implemented by the kernel.
228 * However, many 3rd party drivers provide their own implementations
229 * of these functions. When such drivers are loaded, messages
230 * indicating that these symbols have been multiply defined will be
231 * emitted to the console. To avoid alarming customers for no good
232 * reason, we simply suppress such warnings for the following set of
233 * functions.
235 static char *suppress_sym_list[] =
237 "strstr",
238 "strncat",
239 "strlcat",
240 "strlcpy",
241 "strspn",
242 "memcpy",
243 "memset",
244 "memmove",
245 "memcmp",
246 "memchr",
247 "__udivdi3",
248 "__divdi3",
249 "__umoddi3",
250 "__moddi3",
251 NULL /* This entry must exist */
254 /* indexed by KOBJ_NOTIFY_* */
255 static kobj_notify_list_t *kobj_notifiers[KOBJ_NOTIFY_MAX + 1];
258 * TNF probe management globals
260 tnf_probe_control_t *__tnf_probe_list_head = NULL;
261 tnf_tag_data_t *__tnf_tag_list_head = NULL;
262 int tnf_changed_probe_list = 0;
265 * Prefix for statically defined tracing (SDT) DTrace probes.
267 const char *sdt_prefix = "__dtrace_probe_";
270 * Beginning and end of the kernel's dynamic text/data segments.
272 static caddr_t _text;
273 static caddr_t _etext;
274 static caddr_t _data;
277 * The sparc linker doesn't create a memory location
278 * for a variable named _edata, so _edata can only be
279 * referred to, not modified. krtld needs a static
280 * variable to modify it - within krtld, of course -
281 * outside of krtld, e_data is used in all kernels.
283 extern caddr_t _edata;
285 Addr dynseg = 0; /* load address of "dynamic" segment */
286 size_t dynsize; /* "dynamic" segment size */
289 int standalone = 1; /* an unwholey kernel? */
290 int use_iflush; /* iflush after relocations */
293 * _kobj_printf()
295 * Common printf function pointer. Can handle only one conversion
296 * specification in the format string. Some of the functions invoked
297 * through this function pointer cannot handle more that one conversion
298 * specification in the format string.
300 void (*_kobj_printf)(void *, const char *, ...); /* printf routine */
303 * Standalone function pointers for use within krtld.
304 * Many platforms implement optimized platmod versions of
305 * utilities such as bcopy and any such are not yet available
306 * until the kernel is more completely stitched together.
307 * See kobj_impl.h
309 void (*kobj_bcopy)(const void *, void *, size_t);
310 void (*kobj_bzero)(void *, size_t);
311 size_t (*kobj_strlcat)(char *, const char *, size_t);
313 static kobj_stat_t kobj_stat;
315 #define MINALIGN 8 /* at least a double-word */
318 get_weakish_int(int *ip)
320 if (standalone)
321 return (0);
322 return (ip == NULL ? 0 : *ip);
325 static void *
326 get_weakish_pointer(void **ptrp)
328 if (standalone)
329 return (0);
330 return (ptrp == NULL ? 0 : *ptrp);
334 * XXX fix dependencies on "kernel"; this should work
335 * for other standalone binaries as well.
337 * XXX Fix hashing code to use one pointer to
338 * hash entries.
339 * |----------|
340 * | nbuckets |
341 * |----------|
342 * | nchains |
343 * |----------|
344 * | bucket[] |
345 * |----------|
346 * | chain[] |
347 * |----------|
351 * Load, bind and relocate all modules that
352 * form the primary kernel. At this point, our
353 * externals have not been relocated.
355 void
356 kobj_init(
357 void *romvec,
358 void *dvec,
359 struct bootops *bootvec,
360 val_t *bootaux)
362 struct module *mp;
363 struct modctl *modp;
364 Addr entry;
365 char filename[MAXPATHLEN];
368 * Save these to pass on to
369 * the booted standalone.
371 romp = romvec;
372 dbvec = dvec;
374 ops = bootvec;
375 kobj_setup_standalone_vectors();
377 KOBJ_MARK("Entered kobj_init()");
379 (void) BOP_GETPROP(ops, "whoami", filename);
382 * We don't support standalone debuggers anymore. The use of kadb
383 * will interfere with the later use of kmdb. Let the user mend
384 * their ways now. Users will reach this message if they still
385 * have the kadb binary on their system (perhaps they used an old
386 * bfu, or maybe they intentionally copied it there) and have
387 * specified its use in a way that eluded our checking in the boot
388 * program.
390 if (dvec != NULL) {
391 _kobj_printf(ops, "\nWARNING: Standalone debuggers such as "
392 "kadb are no longer supported\n\n");
393 goto fail;
396 #if defined(_OBP)
398 * OBP allows us to read both the ramdisk and
399 * the underlying root fs when root is a disk.
400 * This can lower incidences of unbootable systems
401 * when the archive is out-of-date with the /etc
402 * state files.
404 if (BOP_MOUNTROOT() != BOOT_SVC_OK) {
405 _kobj_printf(ops, "can't mount boot fs\n");
406 goto fail;
408 #else
410 /* on x86, we always boot with a ramdisk */
411 (void) kobj_boot_mountroot();
414 * Now that the ramdisk is mounted, finish boot property
415 * initialization.
417 boot_prop_finish();
421 * The early boot code does -not- hand us any of the dynamic
422 * metadata about the executable. In particular, it does not read
423 * in, map or otherwise look at the program headers. We fake all
424 * that up now.
426 * We do this early as DTrace static probes both call undefined
427 * references. We have to process those relocations before calling
428 * any of them.
430 * OBP tells kobj_start() where the ELF image is in memory, so it
431 * synthesized bootaux before kobj_init() was called
433 if (bootaux[BA_PHDR].ba_ptr == NULL)
434 synthetic_bootaux(filename, bootaux);
436 #endif /* _OBP */
439 * Save the interesting attribute-values
440 * (scanned by kobj_boot).
442 attr_val(bootaux);
445 * Set the module search path.
447 kobj_module_path = getmodpath(filename);
449 boot_cpu_compatible_list = find_libmacro("CPU");
452 * These two modules have actually been
453 * loaded by boot, but we finish the job
454 * by introducing them into the world of
455 * loadable modules.
458 mp = load_exec(bootaux, filename);
459 load_linker(bootaux);
462 * Load all the primary dependent modules.
464 if (load_primary(mp, KOBJ_LM_PRIMARY) == -1)
465 goto fail;
468 * Glue it together.
470 if (bind_primary(bootaux, KOBJ_LM_PRIMARY) == -1)
471 goto fail;
473 entry = bootaux[BA_ENTRY].ba_val;
476 * Get the boot flags
478 bootflags(ops);
480 if (boothowto & RB_VERBOSE)
481 kobj_lm_dump(KOBJ_LM_PRIMARY);
483 kobj_kdi_init();
485 if (boothowto & RB_KMDB) {
486 if (load_kmdb(bootaux) < 0)
487 goto fail;
491 * Post setup.
493 s_text = _text;
494 e_text = _etext;
495 s_data = _data;
496 e_data = _edata;
498 kobj_sync_instruction_memory(s_text, e_text - s_text);
500 #ifdef KOBJ_DEBUG
501 if (kobj_debug & D_DEBUG)
502 _kobj_printf(ops,
503 "krtld: transferring control to: 0x%p\n", entry);
504 #endif
507 * Make sure the mod system knows about the modules already loaded.
509 last_module_id = kobj_last_module_id;
510 bcopy(kobj_modules, &modules, sizeof (modules));
511 modp = &modules;
512 do {
513 if (modp->mod_next == kobj_modules)
514 modp->mod_next = &modules;
515 if (modp->mod_prev == kobj_modules)
516 modp->mod_prev = &modules;
517 } while ((modp = modp->mod_next) != &modules);
519 standalone = 0;
521 #ifdef KOBJ_DEBUG
522 if (kobj_debug & D_DEBUG)
523 _kobj_printf(ops,
524 "krtld: really transferring control to: 0x%p\n", entry);
525 #endif
527 /* restore printf/bcopy/bzero vectors before returning */
528 kobj_restore_vectors();
530 #if defined(_DBOOT)
532 * krtld was called from a dboot ELF section, the embedded
533 * dboot code contains the real entry via bootaux
535 exitto((caddr_t)entry);
536 #else
538 * krtld was directly called from startup
540 return;
541 #endif
543 fail:
545 _kobj_printf(ops, "krtld: error during initial load/link phase\n");
546 _kobj_printf(ops, "\n");
547 _kobj_printf(ops, "krtld could neither locate nor resolve symbols"
548 " for:\n");
549 _kobj_printf(ops, " %s\n", filename);
550 _kobj_printf(ops, "in the boot archive. Please verify that this"
551 " file\n");
552 _kobj_printf(ops, "matches what is found in the boot archive.\n");
553 bop_panic("Unable to boot");
556 #if !defined(_OBP)
558 * Synthesize additional metadata that describes the executable as
559 * krtld's caller didn't do it.
561 static void
562 synthetic_bootaux(char *filename, val_t *bootaux)
564 Ehdr ehdr;
565 caddr_t phdrbase;
566 struct _buf *file;
567 int i, n;
570 * Elf header
572 KOBJ_MARK("synthetic_bootaux()");
573 KOBJ_MARK(filename);
574 file = kobj_open_file(filename);
575 if (file == (struct _buf *)-1) {
576 _kobj_printf(ops, "krtld: failed to open '%s'\n", filename);
577 return;
579 KOBJ_MARK("reading program headers");
580 if (kobj_read_file(file, (char *)&ehdr, sizeof (ehdr), 0) < 0) {
581 _kobj_printf(ops, "krtld: %s: failed to read ehder\n",
582 filename);
583 return;
587 * Program headers
589 bootaux[BA_PHNUM].ba_val = ehdr.e_phnum;
590 bootaux[BA_PHENT].ba_val = ehdr.e_phentsize;
591 n = ehdr.e_phentsize * ehdr.e_phnum;
593 phdrbase = kobj_alloc(n, KM_WAIT | KM_TMP);
595 if (kobj_read_file(file, phdrbase, n, ehdr.e_phoff) < 0) {
596 _kobj_printf(ops, "krtld: %s: failed to read phdrs\n",
597 filename);
598 return;
600 bootaux[BA_PHDR].ba_ptr = phdrbase;
601 kobj_close_file(file);
602 KOBJ_MARK("closed file");
605 * Find the dynamic section address
607 for (i = 0; i < ehdr.e_phnum; i++) {
608 Phdr *phdr = (Phdr *)(phdrbase + ehdr.e_phentsize * i);
610 if (phdr->p_type == PT_DYNAMIC) {
611 bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr;
612 break;
615 KOBJ_MARK("synthetic_bootaux() done");
617 #endif /* !_OBP */
620 * Set up any global information derived
621 * from attribute/values in the boot or
622 * aux vector.
624 static void
625 attr_val(val_t *bootaux)
627 Phdr *phdr;
628 int phnum, phsize;
629 int i;
631 KOBJ_MARK("attr_val()");
632 kobj_mmu_pagesize = bootaux[BA_PAGESZ].ba_val;
633 lg_pagesize = bootaux[BA_LPAGESZ].ba_val;
634 use_iflush = bootaux[BA_IFLUSH].ba_val;
636 phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr;
637 phnum = bootaux[BA_PHNUM].ba_val;
638 phsize = bootaux[BA_PHENT].ba_val;
639 for (i = 0; i < phnum; i++) {
640 phdr = (Phdr *)(bootaux[BA_PHDR].ba_val + i * phsize);
642 if (phdr->p_type != PT_LOAD) {
643 continue;
646 * Bounds of the various segments.
648 if (!(phdr->p_flags & PF_X)) {
649 #if defined(_RELSEG)
651 * sparc kernel puts the dynamic info
652 * into a separate segment, which is
653 * free'd in bop_fini()
655 ASSERT(phdr->p_vaddr != 0);
656 dynseg = phdr->p_vaddr;
657 dynsize = phdr->p_memsz;
658 #else
659 ASSERT(phdr->p_vaddr == 0);
660 #endif
661 } else {
662 if (phdr->p_flags & PF_W) {
663 _data = (caddr_t)phdr->p_vaddr;
664 _edata = _data + phdr->p_memsz;
665 } else {
666 _text = (caddr_t)phdr->p_vaddr;
667 _etext = _text + phdr->p_memsz;
672 /* To do the kobj_alloc, _edata needs to be set. */
673 for (i = 0; i < NLIBMACROS; i++) {
674 if (bootaux[libmacros[i].lmi_ba_index].ba_ptr != NULL) {
675 libmacros[i].lmi_list = kobj_alloc(
676 strlen(bootaux[libmacros[i].lmi_ba_index].ba_ptr) +
677 1, KM_WAIT);
678 (void) strcpy(libmacros[i].lmi_list,
679 bootaux[libmacros[i].lmi_ba_index].ba_ptr);
681 libmacros[i].lmi_macrolen = strlen(libmacros[i].lmi_macroname);
686 * Set up the booted executable.
688 static struct module *
689 load_exec(val_t *bootaux, char *filename)
691 struct modctl *cp;
692 struct module *mp;
693 Dyn *dyn;
694 Sym *sp;
695 int i, lsize, osize, nsize, allocsize;
696 char *libname, *tmp;
697 char path[MAXPATHLEN];
699 #ifdef KOBJ_DEBUG
700 if (kobj_debug & D_DEBUG)
701 _kobj_printf(ops, "module path '%s'\n", kobj_module_path);
702 #endif
704 KOBJ_MARK("add_primary");
705 cp = add_primary(filename, KOBJ_LM_PRIMARY);
707 KOBJ_MARK("struct module");
708 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
709 cp->mod_mp = mp;
712 * We don't have the following information
713 * since this module is an executable and not
714 * a relocatable .o.
716 mp->symtbl_section = 0;
717 mp->shdrs = NULL;
718 mp->strhdr = NULL;
721 * Since this module is the only exception,
722 * we cons up some section headers.
724 KOBJ_MARK("symhdr");
725 mp->symhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
727 KOBJ_MARK("strhdr");
728 mp->strhdr = kobj_zalloc(sizeof (Shdr), KM_WAIT);
730 mp->symhdr->sh_type = SHT_SYMTAB;
731 mp->strhdr->sh_type = SHT_STRTAB;
733 * Scan the dynamic structure.
735 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
736 dyn->d_tag != DT_NULL; dyn++) {
737 switch (dyn->d_tag) {
738 case DT_SYMTAB:
739 mp->symspace = mp->symtbl = (char *)dyn->d_un.d_ptr;
740 mp->symhdr->sh_addr = dyn->d_un.d_ptr;
741 break;
742 case DT_HASH:
743 mp->nsyms = *((uint_t *)dyn->d_un.d_ptr + 1);
744 mp->hashsize = *(uint_t *)dyn->d_un.d_ptr;
745 break;
746 case DT_STRTAB:
747 mp->strings = (char *)dyn->d_un.d_ptr;
748 mp->strhdr->sh_addr = dyn->d_un.d_ptr;
749 break;
750 case DT_STRSZ:
751 mp->strhdr->sh_size = dyn->d_un.d_val;
752 break;
753 case DT_SYMENT:
754 mp->symhdr->sh_entsize = dyn->d_un.d_val;
755 break;
760 * Collapse any DT_NEEDED entries into one string.
762 nsize = osize = 0;
763 allocsize = MAXPATHLEN;
765 KOBJ_MARK("depends_on");
766 mp->depends_on = kobj_alloc(allocsize, KM_WAIT);
768 for (dyn = (Dyn *) bootaux[BA_DYNAMIC].ba_ptr;
769 dyn->d_tag != DT_NULL; dyn++)
770 if (dyn->d_tag == DT_NEEDED) {
771 char *_lib;
773 libname = mp->strings + dyn->d_un.d_val;
774 if (strchr(libname, '$') != NULL) {
775 if ((_lib = expand_libmacro(libname,
776 path, path)) != NULL)
777 libname = _lib;
778 else
779 _kobj_printf(ops, "krtld: "
780 "load_exec: fail to "
781 "expand %s\n", libname);
783 lsize = strlen(libname);
784 nsize += lsize;
785 if (nsize + 1 > allocsize) {
786 KOBJ_MARK("grow depends_on");
787 tmp = kobj_alloc(allocsize + MAXPATHLEN,
788 KM_WAIT);
789 bcopy(mp->depends_on, tmp, osize);
790 kobj_free(mp->depends_on, allocsize);
791 mp->depends_on = tmp;
792 allocsize += MAXPATHLEN;
794 bcopy(libname, mp->depends_on + osize, lsize);
795 *(mp->depends_on + nsize) = ' '; /* separate */
796 nsize++;
797 osize = nsize;
799 if (nsize) {
800 mp->depends_on[nsize - 1] = '\0'; /* terminate the string */
802 * alloc with exact size and copy whatever it got over
804 KOBJ_MARK("realloc depends_on");
805 tmp = kobj_alloc(nsize, KM_WAIT);
806 bcopy(mp->depends_on, tmp, nsize);
807 kobj_free(mp->depends_on, allocsize);
808 mp->depends_on = tmp;
809 } else {
810 kobj_free(mp->depends_on, allocsize);
811 mp->depends_on = NULL;
814 mp->flags = KOBJ_EXEC|KOBJ_PRIM; /* NOT a relocatable .o */
815 mp->symhdr->sh_size = mp->nsyms * mp->symhdr->sh_entsize;
817 * We allocate our own table since we don't
818 * hash undefined references.
820 KOBJ_MARK("chains");
821 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
822 KOBJ_MARK("buckets");
823 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
825 mp->text = _text;
826 mp->data = _data;
828 mp->text_size = _etext - _text;
829 mp->data_size = _edata - _data;
831 cp->mod_text = mp->text;
832 cp->mod_text_size = mp->text_size;
834 mp->filename = cp->mod_filename;
836 #ifdef KOBJ_DEBUG
837 if (kobj_debug & D_LOADING) {
838 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
839 _kobj_printf(ops, "\ttext: 0x%p", mp->text);
840 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
841 _kobj_printf(ops, "\tdata: 0x%p", mp->data);
842 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
844 #endif /* KOBJ_DEBUG */
847 * Insert symbols into the hash table.
849 for (i = 0; i < mp->nsyms; i++) {
850 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
852 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
853 continue;
855 sym_insert(mp, mp->strings + sp->st_name, i);
858 KOBJ_MARK("load_exec done");
859 return (mp);
863 * Set up the linker module (if it's compiled in, LDNAME is NULL)
865 static void
866 load_linker(val_t *bootaux)
868 struct module *kmp = (struct module *)kobj_modules->mod_mp;
869 struct module *mp;
870 struct modctl *cp;
871 int i;
872 Shdr *shp;
873 Sym *sp;
874 int shsize;
875 char *dlname = (char *)bootaux[BA_LDNAME].ba_ptr;
878 * On some architectures, krtld is compiled into the kernel.
880 if (dlname == NULL)
881 return;
883 cp = add_primary(dlname, KOBJ_LM_PRIMARY);
885 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
887 cp->mod_mp = mp;
888 mp->hdr = *(Ehdr *)bootaux[BA_LDELF].ba_ptr;
889 shsize = mp->hdr.e_shentsize * mp->hdr.e_shnum;
890 mp->shdrs = kobj_alloc(shsize, KM_WAIT);
891 bcopy(bootaux[BA_LDSHDR].ba_ptr, mp->shdrs, shsize);
893 for (i = 1; i < (int)mp->hdr.e_shnum; i++) {
894 shp = (Shdr *)(mp->shdrs + (i * mp->hdr.e_shentsize));
896 if (shp->sh_flags & SHF_ALLOC) {
897 if (shp->sh_flags & SHF_WRITE) {
898 if (mp->data == NULL)
899 mp->data = (char *)shp->sh_addr;
900 } else if (mp->text == NULL) {
901 mp->text = (char *)shp->sh_addr;
904 if (shp->sh_type == SHT_SYMTAB) {
905 mp->symtbl_section = i;
906 mp->symhdr = shp;
907 mp->symspace = mp->symtbl = (char *)shp->sh_addr;
910 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
911 mp->flags = KOBJ_INTERP|KOBJ_PRIM;
912 mp->strhdr = (Shdr *)
913 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
914 mp->strings = (char *)mp->strhdr->sh_addr;
915 mp->hashsize = kobj_gethashsize(mp->nsyms);
917 mp->symsize = mp->symhdr->sh_size + mp->strhdr->sh_size + sizeof (int) +
918 (mp->hashsize + mp->nsyms) * sizeof (symid_t);
920 mp->chains = kobj_zalloc(mp->nsyms * sizeof (symid_t), KM_WAIT);
921 mp->buckets = kobj_zalloc(mp->hashsize * sizeof (symid_t), KM_WAIT);
923 mp->bss = bootaux[BA_BSS].ba_val;
924 mp->bss_align = 0; /* pre-aligned during allocation */
925 mp->bss_size = (uintptr_t)_edata - mp->bss;
926 mp->text_size = _etext - mp->text;
927 mp->data_size = _edata - mp->data;
928 mp->filename = cp->mod_filename;
929 cp->mod_text = mp->text;
930 cp->mod_text_size = mp->text_size;
933 * Now that we've figured out where the linker is,
934 * set the limits for the booted object.
936 kmp->text_size = (size_t)(mp->text - kmp->text);
937 kmp->data_size = (size_t)(mp->data - kmp->data);
938 kobj_modules->mod_text_size = kmp->text_size;
940 #ifdef KOBJ_DEBUG
941 if (kobj_debug & D_LOADING) {
942 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
943 _kobj_printf(ops, "\ttext:0x%p", mp->text);
944 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
945 _kobj_printf(ops, "\tdata:0x%p", mp->data);
946 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
948 #endif /* KOBJ_DEBUG */
951 * Insert the symbols into the hash table.
953 for (i = 0; i < mp->nsyms; i++) {
954 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
956 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
957 continue;
958 if (ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
959 if (sp->st_shndx == SHN_COMMON)
960 sp->st_shndx = SHN_ABS;
962 sym_insert(mp, mp->strings + sp->st_name, i);
967 static kobj_notify_list_t **
968 kobj_notify_lookup(uint_t type)
970 ASSERT(type != 0 && type < sizeof (kobj_notifiers) /
971 sizeof (kobj_notify_list_t *));
973 return (&kobj_notifiers[type]);
977 kobj_notify_add(kobj_notify_list_t *knp)
979 kobj_notify_list_t **knl;
981 knl = kobj_notify_lookup(knp->kn_type);
983 knp->kn_next = NULL;
984 knp->kn_prev = NULL;
986 mutex_enter(&kobj_lock);
988 if (*knl != NULL) {
989 (*knl)->kn_prev = knp;
990 knp->kn_next = *knl;
992 (*knl) = knp;
994 mutex_exit(&kobj_lock);
995 return (0);
999 kobj_notify_remove(kobj_notify_list_t *knp)
1001 kobj_notify_list_t **knl = kobj_notify_lookup(knp->kn_type);
1002 kobj_notify_list_t *tknp;
1004 mutex_enter(&kobj_lock);
1006 /* LINTED */
1007 if (tknp = knp->kn_next)
1008 tknp->kn_prev = knp->kn_prev;
1010 /* LINTED */
1011 if (tknp = knp->kn_prev)
1012 tknp->kn_next = knp->kn_next;
1013 else
1014 *knl = knp->kn_next;
1016 mutex_exit(&kobj_lock);
1018 return (0);
1022 * Notify all interested callbacks of a specified change in module state.
1024 static void
1025 kobj_notify(int type, struct modctl *modp)
1027 kobj_notify_list_t *knp;
1029 if (modp->mod_loadflags & MOD_NONOTIFY || standalone)
1030 return;
1032 mutex_enter(&kobj_lock);
1034 for (knp = *(kobj_notify_lookup(type)); knp != NULL; knp = knp->kn_next)
1035 knp->kn_func(type, modp);
1038 * KDI notification must be last (it has to allow for work done by the
1039 * other notification callbacks), so we call it manually.
1041 kobj_kdi_mod_notify(type, modp);
1043 mutex_exit(&kobj_lock);
1047 * Create the module path.
1049 static char *
1050 getmodpath(const char *filename)
1052 char *path = kobj_zalloc(MAXPATHLEN, KM_WAIT);
1055 * Platform code gets first crack, then add
1056 * the default components
1058 mach_modpath(path, filename);
1059 if (*path != '\0')
1060 (void) strcat(path, " ");
1061 return (strcat(path, MOD_DEFPATH));
1064 static struct modctl *
1065 add_primary(const char *filename, int lmid)
1067 struct modctl *cp;
1069 cp = kobj_zalloc(sizeof (struct modctl), KM_WAIT);
1071 cp->mod_filename = kobj_alloc(strlen(filename) + 1, KM_WAIT);
1074 * For symbol lookup, we assemble our own
1075 * modctl list of the primary modules.
1078 (void) strcpy(cp->mod_filename, filename);
1079 cp->mod_modname = basename(cp->mod_filename);
1081 /* set values for modinfo assuming that the load will work */
1082 cp->mod_prim = 1;
1083 cp->mod_loaded = 1;
1084 cp->mod_installed = 1;
1085 cp->mod_loadcnt = 1;
1086 cp->mod_loadflags = MOD_NOAUTOUNLOAD;
1088 cp->mod_id = kobj_last_module_id++;
1091 * Link the module in. We'll pass this info on
1092 * to the mod squad later.
1094 if (kobj_modules == NULL) {
1095 kobj_modules = cp;
1096 cp->mod_prev = cp->mod_next = cp;
1097 } else {
1098 cp->mod_prev = kobj_modules->mod_prev;
1099 cp->mod_next = kobj_modules;
1100 kobj_modules->mod_prev->mod_next = cp;
1101 kobj_modules->mod_prev = cp;
1104 kobj_lm_append(lmid, cp);
1106 return (cp);
1109 static int
1110 bind_primary(val_t *bootaux, int lmid)
1112 struct modctl_list *linkmap = kobj_lm_lookup(lmid);
1113 struct modctl_list *lp;
1114 struct module *mp;
1117 * Do common symbols.
1119 for (lp = linkmap; lp; lp = lp->modl_next) {
1120 mp = mod(lp);
1123 * Don't do common section relocations for modules that
1124 * don't need it.
1126 if (mp->flags & (KOBJ_EXEC|KOBJ_INTERP))
1127 continue;
1129 if (do_common(mp) < 0)
1130 return (-1);
1134 * Resolve symbols.
1136 for (lp = linkmap; lp; lp = lp->modl_next) {
1137 mp = mod(lp);
1139 if (do_symbols(mp, 0) < 0)
1140 return (-1);
1144 * Do relocations.
1146 for (lp = linkmap; lp; lp = lp->modl_next) {
1147 mp = mod(lp);
1149 if (mp->flags & KOBJ_EXEC) {
1150 Dyn *dyn;
1151 Word relasz = 0, relaent = 0;
1152 Word shtype;
1153 char *rela = NULL;
1155 for (dyn = (Dyn *)bootaux[BA_DYNAMIC].ba_ptr;
1156 dyn->d_tag != DT_NULL; dyn++) {
1157 switch (dyn->d_tag) {
1158 case DT_RELASZ:
1159 case DT_RELSZ:
1160 relasz = dyn->d_un.d_val;
1161 break;
1162 case DT_RELAENT:
1163 case DT_RELENT:
1164 relaent = dyn->d_un.d_val;
1165 break;
1166 case DT_RELA:
1167 shtype = SHT_RELA;
1168 rela = (char *)dyn->d_un.d_ptr;
1169 break;
1170 case DT_REL:
1171 shtype = SHT_REL;
1172 rela = (char *)dyn->d_un.d_ptr;
1173 break;
1176 if (relasz == 0 ||
1177 relaent == 0 || rela == NULL) {
1178 _kobj_printf(ops, "krtld: bind_primary(): "
1179 "no relocation information found for "
1180 "module %s\n", mp->filename);
1181 return (-1);
1183 #ifdef KOBJ_DEBUG
1184 if (kobj_debug & D_RELOCATIONS)
1185 _kobj_printf(ops, "krtld: relocating: file=%s "
1186 "KOBJ_EXEC\n", mp->filename);
1187 #endif
1188 if (do_relocate(mp, rela, shtype, relasz/relaent,
1189 relaent, (Addr)mp->text) < 0)
1190 return (-1);
1191 } else {
1192 if (do_relocations(mp) < 0)
1193 return (-1);
1196 kobj_sync_instruction_memory(mp->text, mp->text_size);
1199 for (lp = linkmap; lp; lp = lp->modl_next) {
1200 mp = mod(lp);
1203 * We need to re-read the full symbol table for the boot file,
1204 * since we couldn't use the full one before. We also need to
1205 * load the CTF sections of both the boot file and the
1206 * interpreter (us).
1208 if (mp->flags & KOBJ_EXEC) {
1209 struct _buf *file;
1210 int n;
1212 file = kobj_open_file(mp->filename);
1213 if (file == (struct _buf *)-1)
1214 return (-1);
1215 if (kobj_read_file(file, (char *)&mp->hdr,
1216 sizeof (mp->hdr), 0) < 0)
1217 return (-1);
1218 n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
1219 mp->shdrs = kobj_alloc(n, KM_WAIT);
1220 if (kobj_read_file(file, mp->shdrs, n,
1221 mp->hdr.e_shoff) < 0)
1222 return (-1);
1223 if (get_syms(mp, file) < 0)
1224 return (-1);
1225 if (get_ctf(mp, file) < 0)
1226 return (-1);
1227 kobj_close_file(file);
1228 mp->flags |= KOBJ_RELOCATED;
1230 } else if (mp->flags & KOBJ_INTERP) {
1231 struct _buf *file;
1234 * The interpreter path fragment in mp->filename
1235 * will already have the module directory suffix
1236 * in it (if appropriate).
1238 file = kobj_open_path(mp->filename, 1, 0);
1239 if (file == (struct _buf *)-1)
1240 return (-1);
1241 if (get_ctf(mp, file) < 0)
1242 return (-1);
1243 kobj_close_file(file);
1244 mp->flags |= KOBJ_RELOCATED;
1248 return (0);
1251 static struct modctl *
1252 mod_already_loaded(char *modname)
1254 struct modctl *mctl = kobj_modules;
1256 do {
1257 if (strcmp(modname, mctl->mod_filename) == 0)
1258 return (mctl);
1259 mctl = mctl->mod_next;
1261 } while (mctl != kobj_modules);
1263 return (NULL);
1267 * Load all the primary dependent modules.
1269 static int
1270 load_primary(struct module *mp, int lmid)
1272 struct modctl *cp;
1273 struct module *dmp;
1274 char *p, *q;
1275 char modname[MODMAXNAMELEN];
1277 if ((p = mp->depends_on) == NULL)
1278 return (0);
1280 /* CONSTANTCONDITION */
1281 while (1) {
1283 * Skip space.
1285 while (*p && (*p == ' ' || *p == '\t'))
1286 p++;
1288 * Get module name.
1290 q = modname;
1291 while (*p && *p != ' ' && *p != '\t')
1292 *q++ = *p++;
1294 if (q == modname)
1295 break;
1297 *q = '\0';
1299 * Check for dup dependencies.
1301 if (strcmp(modname, "dtracestubs") == 0 ||
1302 mod_already_loaded(modname) != NULL)
1303 continue;
1305 cp = add_primary(modname, lmid);
1306 cp->mod_busy = 1;
1308 * Load it.
1310 (void) kobj_load_module(cp, 1);
1311 cp->mod_busy = 0;
1313 if ((dmp = cp->mod_mp) == NULL) {
1314 cp->mod_loaded = 0;
1315 cp->mod_installed = 0;
1316 cp->mod_loadcnt = 0;
1317 return (-1);
1320 add_dependent(mp, dmp);
1321 dmp->flags |= KOBJ_PRIM;
1324 * Recurse.
1326 if (load_primary(dmp, lmid) == -1) {
1327 cp->mod_loaded = 0;
1328 cp->mod_installed = 0;
1329 cp->mod_loadcnt = 0;
1330 return (-1);
1333 return (0);
1336 static int
1337 console_is_usb_serial(void)
1339 char *console;
1340 int len, ret;
1342 if ((len = BOP_GETPROPLEN(ops, "console")) == -1)
1343 return (0);
1345 console = kobj_zalloc(len, KM_WAIT|KM_TMP);
1346 (void) BOP_GETPROP(ops, "console", console);
1347 ret = (strcmp(console, "usb-serial") == 0);
1348 kobj_free(console, len);
1350 return (ret);
1353 static int
1354 load_kmdb(val_t *bootaux)
1356 struct modctl *mctl;
1357 struct module *mp;
1358 Sym *sym;
1360 if (console_is_usb_serial()) {
1361 _kobj_printf(ops, "kmdb not loaded "
1362 "(unsupported on usb serial console)\n");
1363 return (0);
1366 _kobj_printf(ops, "Loading kmdb...\n");
1368 if ((mctl = add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER)) == NULL)
1369 return (-1);
1371 mctl->mod_busy = 1;
1372 (void) kobj_load_module(mctl, 1);
1373 mctl->mod_busy = 0;
1375 if ((mp = mctl->mod_mp) == NULL)
1376 return (-1);
1378 mp->flags |= KOBJ_PRIM;
1380 if (load_primary(mp, KOBJ_LM_DEBUGGER) < 0)
1381 return (-1);
1383 if (boothowto & RB_VERBOSE)
1384 kobj_lm_dump(KOBJ_LM_DEBUGGER);
1386 if (bind_primary(bootaux, KOBJ_LM_DEBUGGER) < 0)
1387 return (-1);
1389 if ((sym = lookup_one(mctl->mod_mp, "kctl_boot_activate")) == NULL)
1390 return (-1);
1392 #ifdef KOBJ_DEBUG
1393 if (kobj_debug & D_DEBUG) {
1394 _kobj_printf(ops, "calling kctl_boot_activate() @ 0x%lx\n",
1395 sym->st_value);
1396 _kobj_printf(ops, "\tops 0x%p\n", ops);
1397 _kobj_printf(ops, "\tromp 0x%p\n", romp);
1399 #endif
1401 if (((kctl_boot_activate_f *)sym->st_value)(ops, romp, 0,
1402 (const char **)kobj_kmdb_argv) < 0)
1403 return (-1);
1405 return (0);
1409 * Return a string listing module dependencies.
1411 static char *
1412 depends_on(struct module *mp)
1414 Sym *sp;
1415 char *depstr, *q;
1418 * The module doesn't have a depends_on value, so let's try it the
1419 * old-fashioned way - via "_depends_on"
1421 if ((sp = lookup_one(mp, "_depends_on")) == NULL)
1422 return (NULL);
1424 q = (char *)sp->st_value;
1426 #ifdef KOBJ_DEBUG
1428 * _depends_on is a deprecated interface, so we warn about its use
1429 * irrespective of subsequent processing errors. How else are we going
1430 * to be able to deco this interface completely?
1431 * Changes initially limited to DEBUG because third-party modules
1432 * should be flagged to developers before general use base.
1434 _kobj_printf(ops,
1435 "Warning: %s uses deprecated _depends_on interface.\n",
1436 mp->filename);
1437 _kobj_printf(ops, "Please notify module developer or vendor.\n");
1438 #endif
1441 * Idiot checks. Make sure it's
1442 * in-bounds and NULL terminated.
1444 if (kobj_addrcheck(mp, q) || q[sp->st_size - 1] != '\0') {
1445 _kobj_printf(ops, "Error processing dependency for %s\n",
1446 mp->filename);
1447 return (NULL);
1450 depstr = (char *)kobj_alloc(strlen(q) + 1, KM_WAIT);
1451 (void) strcpy(depstr, q);
1453 return (depstr);
1456 void
1457 kobj_getmodinfo(void *xmp, struct modinfo *modinfo)
1459 struct module *mp;
1460 mp = (struct module *)xmp;
1462 modinfo->mi_base = mp->text;
1463 modinfo->mi_size = mp->text_size + mp->data_size;
1467 * kobj_export_ksyms() performs the following services:
1469 * (1) Migrates the symbol table from boot/kobj memory to the ksyms arena.
1470 * (2) Removes unneeded symbols to save space.
1471 * (3) Reduces memory footprint by using VM_BESTFIT allocations.
1472 * (4) Makes the symbol table visible to /dev/ksyms.
1474 static void
1475 kobj_export_ksyms(struct module *mp)
1477 Sym *esp = (Sym *)(mp->symtbl + mp->symhdr->sh_size);
1478 Sym *sp, *osp;
1479 char *name;
1480 size_t namelen;
1481 struct module *omp;
1482 uint_t nsyms;
1483 size_t symsize = mp->symhdr->sh_entsize;
1484 size_t locals = 1;
1485 size_t strsize;
1488 * Make a copy of the original module structure.
1490 omp = kobj_alloc(sizeof (struct module), KM_WAIT);
1491 bcopy(mp, omp, sizeof (struct module));
1494 * Compute the sizes of the new symbol table sections.
1496 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
1497 if (osp->st_value == 0)
1498 continue;
1499 if (sym_lookup(omp, osp) == NULL)
1500 continue;
1501 name = omp->strings + osp->st_name;
1502 namelen = strlen(name);
1503 if (ELF_ST_BIND(osp->st_info) == STB_LOCAL)
1504 locals++;
1505 nsyms++;
1506 strsize += namelen + 1;
1509 mp->nsyms = nsyms;
1510 mp->hashsize = kobj_gethashsize(mp->nsyms);
1513 * ksyms_lock must be held as writer during any operation that
1514 * modifies ksyms_arena, including allocation from same, and
1515 * must not be dropped until the arena is vmem_walk()able.
1517 rw_enter(&ksyms_lock, RW_WRITER);
1520 * Allocate space for the new section headers (symtab and strtab),
1521 * symbol table, buckets, chains, and strings.
1523 mp->symsize = (2 * sizeof (Shdr)) + (nsyms * symsize) +
1524 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + strsize;
1526 if (mp->flags & KOBJ_NOKSYMS) {
1527 mp->symspace = kobj_alloc(mp->symsize, KM_WAIT);
1528 } else {
1529 mp->symspace = vmem_alloc(ksyms_arena, mp->symsize,
1530 VM_BESTFIT | VM_SLEEP);
1532 bzero(mp->symspace, mp->symsize);
1535 * Divvy up symspace.
1537 mp->shdrs = mp->symspace;
1538 mp->symhdr = (Shdr *)mp->shdrs;
1539 mp->strhdr = (Shdr *)(mp->symhdr + 1);
1540 mp->symtbl = (char *)(mp->strhdr + 1);
1541 mp->buckets = (symid_t *)(mp->symtbl + (nsyms * symsize));
1542 mp->chains = (symid_t *)(mp->buckets + mp->hashsize);
1543 mp->strings = (char *)(mp->chains + nsyms);
1546 * Fill in the new section headers (symtab and strtab).
1548 mp->hdr.e_shnum = 2;
1549 mp->symtbl_section = 0;
1551 mp->symhdr->sh_type = SHT_SYMTAB;
1552 mp->symhdr->sh_addr = (Addr)mp->symtbl;
1553 mp->symhdr->sh_size = nsyms * symsize;
1554 mp->symhdr->sh_link = 1;
1555 mp->symhdr->sh_info = locals;
1556 mp->symhdr->sh_addralign = sizeof (Addr);
1557 mp->symhdr->sh_entsize = symsize;
1559 mp->strhdr->sh_type = SHT_STRTAB;
1560 mp->strhdr->sh_addr = (Addr)mp->strings;
1561 mp->strhdr->sh_size = strsize;
1562 mp->strhdr->sh_addralign = 1;
1565 * Construct the new symbol table.
1567 for (nsyms = strsize = 1, osp = (Sym *)omp->symtbl; osp < esp; osp++) {
1568 if (osp->st_value == 0)
1569 continue;
1570 if (sym_lookup(omp, osp) == NULL)
1571 continue;
1572 name = omp->strings + osp->st_name;
1573 namelen = strlen(name);
1574 sp = (Sym *)(mp->symtbl + symsize * nsyms);
1575 bcopy(osp, sp, symsize);
1576 bcopy(name, mp->strings + strsize, namelen);
1577 sp->st_name = strsize;
1578 sym_insert(mp, name, nsyms);
1579 nsyms++;
1580 strsize += namelen + 1;
1583 rw_exit(&ksyms_lock);
1586 * Free the old section headers -- we'll never need them again.
1588 if (!(mp->flags & KOBJ_PRIM)) {
1589 uint_t shn;
1590 Shdr *shp;
1592 for (shn = 1; shn < omp->hdr.e_shnum; shn++) {
1593 shp = (Shdr *)(omp->shdrs + shn * omp->hdr.e_shentsize);
1594 switch (shp->sh_type) {
1595 case SHT_RELA:
1596 case SHT_REL:
1597 if (shp->sh_addr != (uintptr_t)NULL) {
1598 kobj_free((void *)shp->sh_addr,
1599 shp->sh_size);
1601 break;
1604 kobj_free(omp->shdrs, omp->hdr.e_shentsize * omp->hdr.e_shnum);
1607 * Discard the old symbol table and our copy of the module strucure.
1609 if (!(mp->flags & KOBJ_PRIM))
1610 kobj_free(omp->symspace, omp->symsize);
1611 kobj_free(omp, sizeof (struct module));
1614 static void
1615 kobj_export_ctf(struct module *mp)
1617 char *data = mp->ctfdata;
1618 size_t size = mp->ctfsize;
1620 if (data != NULL) {
1621 if (_moddebug & MODDEBUG_NOCTF) {
1622 mp->ctfdata = NULL;
1623 mp->ctfsize = 0;
1624 } else {
1625 mp->ctfdata = vmem_alloc(ctf_arena, size,
1626 VM_BESTFIT | VM_SLEEP);
1627 bcopy(data, mp->ctfdata, size);
1630 if (!(mp->flags & KOBJ_PRIM))
1631 kobj_free(data, size);
1635 void
1636 kobj_export_module(struct module *mp)
1638 kobj_export_ksyms(mp);
1639 kobj_export_ctf(mp);
1641 mp->flags |= KOBJ_EXPORTED;
1644 static int
1645 process_dynamic(struct module *mp, char *dyndata, char *strdata)
1647 char *path = NULL, *depstr = NULL;
1648 int allocsize = 0, osize = 0, nsize = 0;
1649 char *libname, *tmp;
1650 int lsize;
1651 Dyn *dynp;
1653 for (dynp = (Dyn *)dyndata; dynp && dynp->d_tag != DT_NULL; dynp++) {
1654 switch (dynp->d_tag) {
1655 case DT_NEEDED:
1657 * Read the DT_NEEDED entries, expanding the macros they
1658 * contain (if any), and concatenating them into a
1659 * single space-separated dependency list.
1661 libname = (ulong_t)dynp->d_un.d_ptr + strdata;
1663 if (strchr(libname, '$') != NULL) {
1664 char *_lib;
1666 if (path == NULL)
1667 path = kobj_alloc(MAXPATHLEN, KM_WAIT);
1668 if ((_lib = expand_libmacro(libname, path,
1669 path)) != NULL)
1670 libname = _lib;
1671 else {
1672 _kobj_printf(ops, "krtld: "
1673 "process_dynamic: failed to expand "
1674 "%s\n", libname);
1678 lsize = strlen(libname);
1679 nsize += lsize;
1680 if (nsize + 1 > allocsize) {
1681 tmp = kobj_alloc(allocsize + MAXPATHLEN,
1682 KM_WAIT);
1683 if (depstr != NULL) {
1684 bcopy(depstr, tmp, osize);
1685 kobj_free(depstr, allocsize);
1687 depstr = tmp;
1688 allocsize += MAXPATHLEN;
1690 bcopy(libname, depstr + osize, lsize);
1691 *(depstr + nsize) = ' '; /* separator */
1692 nsize++;
1693 osize = nsize;
1694 break;
1696 case DT_FLAGS_1:
1697 if (dynp->d_un.d_val & DF_1_IGNMULDEF)
1698 mp->flags |= KOBJ_IGNMULDEF;
1699 if (dynp->d_un.d_val & DF_1_NOKSYMS)
1700 mp->flags |= KOBJ_NOKSYMS;
1702 break;
1707 * finish up the depends string (if any)
1709 if (depstr != NULL) {
1710 *(depstr + nsize - 1) = '\0'; /* overwrite separator w/term */
1711 if (path != NULL)
1712 kobj_free(path, MAXPATHLEN);
1714 tmp = kobj_alloc(nsize, KM_WAIT);
1715 bcopy(depstr, tmp, nsize);
1716 kobj_free(depstr, allocsize);
1717 depstr = tmp;
1719 mp->depends_on = depstr;
1722 return (0);
1725 static int
1726 do_dynamic(struct module *mp, struct _buf *file)
1728 Shdr *dshp, *dstrp, *shp;
1729 char *dyndata, *dstrdata;
1730 int dshn, shn, rc;
1732 /* find and validate the dynamic section (if any) */
1734 for (dshp = NULL, shn = 1; shn < mp->hdr.e_shnum; shn++) {
1735 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
1736 switch (shp->sh_type) {
1737 case SHT_DYNAMIC:
1738 if (dshp != NULL) {
1739 _kobj_printf(ops, "krtld: get_dynamic: %s, ",
1740 mp->filename);
1741 _kobj_printf(ops,
1742 "multiple dynamic sections\n");
1743 return (-1);
1744 } else {
1745 dshp = shp;
1746 dshn = shn;
1748 break;
1752 if (dshp == NULL)
1753 return (0);
1755 if (dshp->sh_link > mp->hdr.e_shnum) {
1756 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1757 _kobj_printf(ops, "no section for sh_link %d\n", dshp->sh_link);
1758 return (-1);
1760 dstrp = (Shdr *)(mp->shdrs + dshp->sh_link * mp->hdr.e_shentsize);
1762 if (dstrp->sh_type != SHT_STRTAB) {
1763 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1764 _kobj_printf(ops, "sh_link not a string table for section %d\n",
1765 dshn);
1766 return (-1);
1769 /* read it from disk */
1771 dyndata = kobj_alloc(dshp->sh_size, KM_WAIT|KM_TMP);
1772 if (kobj_read_file(file, dyndata, dshp->sh_size, dshp->sh_offset) < 0) {
1773 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1774 _kobj_printf(ops, "error reading section %d\n", dshn);
1776 kobj_free(dyndata, dshp->sh_size);
1777 return (-1);
1780 dstrdata = kobj_alloc(dstrp->sh_size, KM_WAIT|KM_TMP);
1781 if (kobj_read_file(file, dstrdata, dstrp->sh_size,
1782 dstrp->sh_offset) < 0) {
1783 _kobj_printf(ops, "krtld: get_dynamic: %s, ", mp->filename);
1784 _kobj_printf(ops, "error reading section %d\n", dshp->sh_link);
1786 kobj_free(dyndata, dshp->sh_size);
1787 kobj_free(dstrdata, dstrp->sh_size);
1788 return (-1);
1791 /* pull the interesting pieces out */
1793 rc = process_dynamic(mp, dyndata, dstrdata);
1795 kobj_free(dyndata, dshp->sh_size);
1796 kobj_free(dstrdata, dstrp->sh_size);
1798 return (rc);
1801 void
1802 kobj_set_ctf(struct module *mp, caddr_t data, size_t size)
1804 if (!standalone) {
1805 if (mp->ctfdata != NULL) {
1806 if (vmem_contains(ctf_arena, mp->ctfdata,
1807 mp->ctfsize)) {
1808 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
1809 } else {
1810 kobj_free(mp->ctfdata, mp->ctfsize);
1816 * The order is very important here. We need to make sure that
1817 * consumers, at any given instant, see a consistent state. We'd
1818 * rather they see no CTF data than the address of one buffer and the
1819 * size of another.
1821 mp->ctfdata = NULL;
1822 membar_producer();
1823 mp->ctfsize = size;
1824 mp->ctfdata = data;
1825 membar_producer();
1829 kobj_load_module(struct modctl *modp, int use_path)
1831 char *filename = modp->mod_filename;
1832 char *modname = modp->mod_modname;
1833 int i;
1834 int n;
1835 struct _buf *file;
1836 struct module *mp = NULL;
1837 #ifdef MODDIR_SUFFIX
1838 int no_suffixdir_drv = 0;
1839 #endif
1841 mp = kobj_zalloc(sizeof (struct module), KM_WAIT);
1844 * We need to prevent kmdb's symbols from leaking into /dev/ksyms.
1845 * kmdb contains a bunch of symbols with well-known names, symbols
1846 * which will mask the real versions, thus causing no end of trouble
1847 * for mdb.
1849 if (strcmp(modp->mod_modname, "kmdbmod") == 0)
1850 mp->flags |= KOBJ_NOKSYMS;
1852 file = kobj_open_path(filename, use_path, 1);
1853 if (file == (struct _buf *)-1) {
1854 #ifdef MODDIR_SUFFIX
1855 file = kobj_open_path(filename, use_path, 0);
1856 #endif
1857 if (file == (struct _buf *)-1) {
1858 kobj_free(mp, sizeof (*mp));
1859 goto bad;
1861 #ifdef MODDIR_SUFFIX
1863 * There is no driver module in the ISA specific (suffix)
1864 * subdirectory but there is a module in the parent directory.
1866 if (strncmp(filename, "drv/", 4) == 0) {
1867 no_suffixdir_drv = 1;
1869 #endif
1872 mp->filename = kobj_alloc(strlen(file->_name) + 1, KM_WAIT);
1873 (void) strcpy(mp->filename, file->_name);
1875 if (kobj_read_file(file, (char *)&mp->hdr, sizeof (mp->hdr), 0) < 0) {
1876 _kobj_printf(ops, "kobj_load_module: %s read header failed\n",
1877 modname);
1878 kobj_free(mp->filename, strlen(file->_name) + 1);
1879 kobj_free(mp, sizeof (*mp));
1880 goto bad;
1882 for (i = 0; i < SELFMAG; i++) {
1883 if (mp->hdr.e_ident[i] != ELFMAG[i]) {
1884 if (_moddebug & MODDEBUG_ERRMSG)
1885 _kobj_printf(ops, "%s not an elf module\n",
1886 modname);
1887 kobj_free(mp->filename, strlen(file->_name) + 1);
1888 kobj_free(mp, sizeof (*mp));
1889 goto bad;
1893 * It's ELF, but is it our ISA? Interpreting the header
1894 * from a file for a byte-swapped ISA could cause a huge
1895 * and unsatisfiable value to be passed to kobj_alloc below
1896 * and therefore hang booting.
1898 if (!elf_mach_ok(&mp->hdr)) {
1899 if (_moddebug & MODDEBUG_ERRMSG)
1900 _kobj_printf(ops, "%s not an elf module for this ISA\n",
1901 modname);
1902 kobj_free(mp->filename, strlen(file->_name) + 1);
1903 kobj_free(mp, sizeof (*mp));
1904 #ifdef MODDIR_SUFFIX
1906 * The driver mod is not in the ISA specific subdirectory
1907 * and the module in the parent directory is not our ISA.
1908 * If it is our ISA, for now we will silently succeed.
1910 if (no_suffixdir_drv == 1) {
1911 cmn_err(CE_CONT, "?NOTICE: %s: 64-bit driver module"
1912 " not found\n", modname);
1914 #endif
1915 goto bad;
1919 * All modules, save for unix, should be relocatable (as opposed to
1920 * dynamic). Dynamic modules come with PLTs and GOTs, which can't
1921 * currently be processed by krtld.
1923 if (mp->hdr.e_type != ET_REL) {
1924 if (_moddebug & MODDEBUG_ERRMSG)
1925 _kobj_printf(ops, "%s isn't a relocatable (ET_REL) "
1926 "module\n", modname);
1927 kobj_free(mp->filename, strlen(file->_name) + 1);
1928 kobj_free(mp, sizeof (*mp));
1929 goto bad;
1932 n = mp->hdr.e_shentsize * mp->hdr.e_shnum;
1933 mp->shdrs = kobj_alloc(n, KM_WAIT);
1935 if (kobj_read_file(file, mp->shdrs, n, mp->hdr.e_shoff) < 0) {
1936 _kobj_printf(ops, "kobj_load_module: %s error reading "
1937 "section headers\n", modname);
1938 kobj_free(mp->shdrs, n);
1939 kobj_free(mp->filename, strlen(file->_name) + 1);
1940 kobj_free(mp, sizeof (*mp));
1941 goto bad;
1944 kobj_notify(KOBJ_NOTIFY_MODLOADING, modp);
1945 module_assign(modp, mp);
1947 /* read in sections */
1948 if (get_progbits(mp, file) < 0) {
1949 _kobj_printf(ops, "%s error reading sections\n", modname);
1950 goto bad;
1953 if (do_dynamic(mp, file) < 0) {
1954 _kobj_printf(ops, "%s error reading dynamic section\n",
1955 modname);
1956 goto bad;
1959 modp->mod_text = mp->text;
1960 modp->mod_text_size = mp->text_size;
1962 /* read in symbols; adjust values for each section's real address */
1963 if (get_syms(mp, file) < 0) {
1964 _kobj_printf(ops, "%s error reading symbols\n",
1965 modname);
1966 goto bad;
1970 * If we didn't dependency information from the dynamic section, look
1971 * for it the old-fashioned way.
1973 if (mp->depends_on == NULL)
1974 mp->depends_on = depends_on(mp);
1976 if (get_ctf(mp, file) < 0) {
1977 _kobj_printf(ops, "%s debug information will not "
1978 "be available\n", modname);
1981 /* primary kernel modules do not have a signature section */
1982 if (!(mp->flags & KOBJ_PRIM))
1983 get_signature(mp, file);
1985 #ifdef KOBJ_DEBUG
1986 if (kobj_debug & D_LOADING) {
1987 _kobj_printf(ops, "krtld: file=%s\n", mp->filename);
1988 _kobj_printf(ops, "\ttext:0x%p", mp->text);
1989 _kobj_printf(ops, " size: 0x%x\n", mp->text_size);
1990 _kobj_printf(ops, "\tdata:0x%p", mp->data);
1991 _kobj_printf(ops, " dsize: 0x%x\n", mp->data_size);
1993 #endif /* KOBJ_DEBUG */
1996 * For primary kernel modules, we defer
1997 * symbol resolution and relocation until
1998 * all primary objects have been loaded.
2000 if (!standalone) {
2001 int ddrval, dcrval;
2002 char *dependent_modname;
2003 /* load all dependents */
2004 dependent_modname = kobj_zalloc(MODMAXNAMELEN, KM_WAIT);
2005 ddrval = do_dependents(modp, dependent_modname, MODMAXNAMELEN);
2008 * resolve undefined and common symbols,
2009 * also allocates common space
2011 if ((dcrval = do_common(mp)) < 0) {
2012 switch (dcrval) {
2013 case DOSYM_UNSAFE:
2014 _kobj_printf(ops, "WARNING: mod_load: "
2015 "MT-unsafe module '%s' rejected\n",
2016 modname);
2017 break;
2018 case DOSYM_UNDEF:
2019 _kobj_printf(ops, "WARNING: mod_load: "
2020 "cannot load module '%s'\n",
2021 modname);
2022 if (ddrval == -1) {
2023 _kobj_printf(ops, "WARNING: %s: ",
2024 modname);
2025 _kobj_printf(ops,
2026 "unable to resolve dependency, "
2027 "module '%s' not found\n",
2028 dependent_modname);
2030 break;
2033 kobj_free(dependent_modname, MODMAXNAMELEN);
2034 if (dcrval < 0)
2035 goto bad;
2037 /* process relocation tables */
2038 if (do_relocations(mp) < 0) {
2039 _kobj_printf(ops, "%s error doing relocations\n",
2040 modname);
2041 goto bad;
2044 if (mp->destination) {
2045 off_t off = (uintptr_t)mp->destination & PAGEOFFSET;
2046 caddr_t base = (caddr_t)mp->destination - off;
2047 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
2049 hat_unload(kas.a_hat, base, size, HAT_UNLOAD_UNLOCK);
2050 vmem_free(heap_arena, base, size);
2053 /* sync_instruction_memory */
2054 kobj_sync_instruction_memory(mp->text, mp->text_size);
2055 kobj_export_module(mp);
2056 kobj_notify(KOBJ_NOTIFY_MODLOADED, modp);
2058 kobj_close_file(file);
2059 return (0);
2060 bad:
2061 if (file != (struct _buf *)-1)
2062 kobj_close_file(file);
2063 if (modp->mod_mp != NULL)
2064 free_module_data(modp->mod_mp);
2066 module_assign(modp, NULL);
2067 return ((file == (struct _buf *)-1) ? ENOENT : EINVAL);
2071 kobj_load_primary_module(struct modctl *modp)
2073 struct modctl *dep;
2074 struct module *mp;
2076 if (kobj_load_module(modp, 0) != 0)
2077 return (-1);
2079 mp = modp->mod_mp;
2080 mp->flags |= KOBJ_PRIM;
2082 /* Bind new module to its dependents */
2083 if (mp->depends_on != NULL && (dep =
2084 mod_already_loaded(mp->depends_on)) == NULL) {
2085 #ifdef KOBJ_DEBUG
2086 if (kobj_debug & D_DEBUG) {
2087 _kobj_printf(ops, "krtld: failed to resolve deps "
2088 "for primary %s\n", modp->mod_modname);
2090 #endif
2091 return (-1);
2094 add_dependent(mp, dep->mod_mp);
2097 * Relocate it. This module may not be part of a link map, so we
2098 * can't use bind_primary.
2100 if (do_common(mp) < 0 || do_symbols(mp, 0) < 0 ||
2101 do_relocations(mp) < 0) {
2102 #ifdef KOBJ_DEBUG
2103 if (kobj_debug & D_DEBUG) {
2104 _kobj_printf(ops, "krtld: failed to relocate "
2105 "primary %s\n", modp->mod_modname);
2107 #endif
2108 return (-1);
2111 return (0);
2114 static void
2115 module_assign(struct modctl *cp, struct module *mp)
2117 if (standalone) {
2118 cp->mod_mp = mp;
2119 return;
2121 mutex_enter(&mod_lock);
2122 cp->mod_mp = mp;
2123 cp->mod_gencount++;
2124 mutex_exit(&mod_lock);
2127 void
2128 kobj_unload_module(struct modctl *modp)
2130 struct module *mp = modp->mod_mp;
2132 if ((_moddebug & MODDEBUG_KEEPTEXT) && mp) {
2133 _kobj_printf(ops, "text for %s ", mp->filename);
2134 _kobj_printf(ops, "was at %p\n", mp->text);
2135 mp->text = NULL; /* don't actually free it */
2138 kobj_notify(KOBJ_NOTIFY_MODUNLOADING, modp);
2141 * Null out mod_mp first, so consumers (debuggers) know not to look
2142 * at the module structure any more.
2144 mutex_enter(&mod_lock);
2145 modp->mod_mp = NULL;
2146 mutex_exit(&mod_lock);
2148 kobj_notify(KOBJ_NOTIFY_MODUNLOADED, modp);
2149 free_module_data(mp);
2152 static void
2153 free_module_data(struct module *mp)
2155 struct module_list *lp, *tmp;
2156 int ksyms_exported = 0;
2158 lp = mp->head;
2159 while (lp) {
2160 tmp = lp;
2161 lp = lp->next;
2162 kobj_free((char *)tmp, sizeof (*tmp));
2165 rw_enter(&ksyms_lock, RW_WRITER);
2166 if (mp->symspace) {
2167 if (vmem_contains(ksyms_arena, mp->symspace, mp->symsize)) {
2168 vmem_free(ksyms_arena, mp->symspace, mp->symsize);
2169 ksyms_exported = 1;
2170 } else {
2171 if (mp->flags & KOBJ_NOKSYMS)
2172 ksyms_exported = 1;
2173 kobj_free(mp->symspace, mp->symsize);
2176 rw_exit(&ksyms_lock);
2178 if (mp->ctfdata) {
2179 if (vmem_contains(ctf_arena, mp->ctfdata, mp->ctfsize))
2180 vmem_free(ctf_arena, mp->ctfdata, mp->ctfsize);
2181 else
2182 kobj_free(mp->ctfdata, mp->ctfsize);
2185 if (mp->sigdata)
2186 kobj_free(mp->sigdata, mp->sigsize);
2189 * We did not get far enough into kobj_export_ksyms() to free allocated
2190 * buffers because we encounted error conditions. Free the buffers.
2192 if ((ksyms_exported == 0) && (mp->shdrs != NULL)) {
2193 uint_t shn;
2194 Shdr *shp;
2196 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2197 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2198 switch (shp->sh_type) {
2199 case SHT_RELA:
2200 case SHT_REL:
2201 if (shp->sh_addr != 0)
2202 kobj_free((void *)shp->sh_addr,
2203 shp->sh_size);
2204 break;
2207 err_free_done:
2208 if (!(mp->flags & KOBJ_PRIM)) {
2209 kobj_free(mp->shdrs,
2210 mp->hdr.e_shentsize * mp->hdr.e_shnum);
2214 if (mp->bss)
2215 vmem_free(data_arena, (void *)mp->bss, mp->bss_size);
2217 if (mp->fbt_tab)
2218 kobj_texthole_free(mp->fbt_tab, mp->fbt_size);
2220 if (mp->textwin_base)
2221 kobj_textwin_free(mp);
2223 if (mp->sdt_probes != NULL) {
2224 sdt_probedesc_t *sdp = mp->sdt_probes, *next;
2226 while (sdp != NULL) {
2227 next = sdp->sdpd_next;
2228 kobj_free(sdp->sdpd_name, strlen(sdp->sdpd_name) + 1);
2229 kobj_free(sdp, sizeof (sdt_probedesc_t));
2230 sdp = next;
2234 if (mp->sdt_tab)
2235 kobj_texthole_free(mp->sdt_tab, mp->sdt_size);
2236 if (mp->text)
2237 vmem_free(text_arena, mp->text, mp->text_size);
2238 if (mp->data)
2239 vmem_free(data_arena, mp->data, mp->data_size);
2240 if (mp->depends_on)
2241 kobj_free(mp->depends_on, strlen(mp->depends_on)+1);
2242 if (mp->filename)
2243 kobj_free(mp->filename, strlen(mp->filename)+1);
2245 kobj_free((char *)mp, sizeof (*mp));
2248 static int
2249 get_progbits(struct module *mp, struct _buf *file)
2251 struct proginfo *tp, *dp, *sdp;
2252 Shdr *shp;
2253 reloc_dest_t dest = NULL;
2254 uintptr_t bits_ptr;
2255 uintptr_t text = 0, data, textptr;
2256 uint_t shn;
2257 int err = -1;
2259 tp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2260 dp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2261 sdp = kobj_zalloc(sizeof (struct proginfo), KM_WAIT|KM_TMP);
2263 * loop through sections to find out how much space we need
2264 * for text, data, (also bss that is already assigned)
2266 if (get_progbits_size(mp, tp, dp, sdp) < 0)
2267 goto done;
2269 mp->text_size = tp->size;
2270 mp->data_size = dp->size;
2272 if (standalone) {
2273 caddr_t limit = _data;
2275 if (lg_pagesize && _text + lg_pagesize < limit)
2276 limit = _text + lg_pagesize;
2278 mp->text = kobj_segbrk(&_etext, mp->text_size,
2279 tp->align, limit);
2281 * If we can't grow the text segment, try the
2282 * data segment before failing.
2284 if (mp->text == NULL) {
2285 mp->text = kobj_segbrk(&_edata, mp->text_size,
2286 tp->align, 0);
2289 mp->data = kobj_segbrk(&_edata, mp->data_size, dp->align, 0);
2291 if (mp->text == NULL || mp->data == NULL)
2292 goto done;
2294 } else {
2295 if (text_arena == NULL)
2296 kobj_vmem_init(&text_arena, &data_arena);
2299 * some architectures may want to load the module on a
2300 * page that is currently read only. It may not be
2301 * possible for those architectures to remap their page
2302 * on the fly. So we provide a facility for them to hang
2303 * a private hook where the memory they assign the module
2304 * is not the actual place where the module loads.
2306 * In this case there are two addresses that deal with the
2307 * modload.
2308 * 1) the final destination of the module
2309 * 2) the address that is used to view the newly
2310 * loaded module until all the relocations relative to 1
2311 * above are completed.
2313 * That is what dest is used for below.
2315 mp->text_size += tp->align;
2316 mp->data_size += dp->align;
2318 mp->text = kobj_text_alloc(text_arena, mp->text_size);
2321 * a remap is taking place. Align the text ptr relative
2322 * to the secondary mapping. That is where the bits will
2323 * be read in.
2325 if (kvseg.s_base != NULL && !vmem_contains(heaptext_arena,
2326 mp->text, mp->text_size)) {
2327 off_t off = (uintptr_t)mp->text & PAGEOFFSET;
2328 size_t size = P2ROUNDUP(mp->text_size + off, PAGESIZE);
2329 caddr_t map = vmem_alloc(heap_arena, size, VM_SLEEP);
2330 caddr_t orig = mp->text - off;
2331 pgcnt_t pages = size / PAGESIZE;
2333 dest = (reloc_dest_t)(map + off);
2334 text = ALIGN((uintptr_t)dest, tp->align);
2336 while (pages--) {
2337 hat_devload(kas.a_hat, map, PAGESIZE,
2338 hat_getpfnum(kas.a_hat, orig),
2339 PROT_READ | PROT_WRITE | PROT_EXEC,
2340 HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
2341 map += PAGESIZE;
2342 orig += PAGESIZE;
2345 * Since we set up a non-cacheable mapping, we need
2346 * to flush any old entries in the cache that might
2347 * be left around from the read-only mapping.
2349 dcache_flushall();
2351 if (mp->data_size)
2352 mp->data = vmem_alloc(data_arena, mp->data_size,
2353 VM_SLEEP | VM_BESTFIT);
2355 textptr = (uintptr_t)mp->text;
2356 textptr = ALIGN(textptr, tp->align);
2357 mp->destination = dest;
2360 * This is the case where a remap is not being done.
2362 if (text == 0)
2363 text = ALIGN((uintptr_t)mp->text, tp->align);
2364 data = ALIGN((uintptr_t)mp->data, dp->align);
2366 /* now loop though sections assigning addresses and loading the data */
2367 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2368 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2369 if (!(shp->sh_flags & SHF_ALLOC))
2370 continue;
2372 if ((shp->sh_flags & SHF_WRITE) == 0)
2373 bits_ptr = text;
2374 else
2375 bits_ptr = data;
2377 bits_ptr = ALIGN(bits_ptr, shp->sh_addralign);
2379 if (shp->sh_type == SHT_NOBITS) {
2381 * Zero bss.
2383 bzero((caddr_t)bits_ptr, shp->sh_size);
2384 shp->sh_type = SHT_PROGBITS;
2385 } else {
2386 if (kobj_read_file(file, (char *)bits_ptr,
2387 shp->sh_size, shp->sh_offset) < 0)
2388 goto done;
2391 if (shp->sh_flags & SHF_WRITE) {
2392 shp->sh_addr = bits_ptr;
2393 } else {
2394 textptr = ALIGN(textptr, shp->sh_addralign);
2395 shp->sh_addr = textptr;
2396 textptr += shp->sh_size;
2399 bits_ptr += shp->sh_size;
2400 if ((shp->sh_flags & SHF_WRITE) == 0)
2401 text = bits_ptr;
2402 else
2403 data = bits_ptr;
2406 err = 0;
2407 done:
2409 * Free and mark as freed the section headers here so that
2410 * free_module_data() does not have to worry about this buffer.
2412 * This buffer is freed here because one of the possible reasons
2413 * for error is a section with non-zero sh_addr and in that case
2414 * free_module_data() would have no way of recognizing that this
2415 * buffer was unallocated.
2417 if (err != 0) {
2418 kobj_free(mp->shdrs, mp->hdr.e_shentsize * mp->hdr.e_shnum);
2419 mp->shdrs = NULL;
2422 (void) kobj_free(tp, sizeof (struct proginfo));
2423 (void) kobj_free(dp, sizeof (struct proginfo));
2424 (void) kobj_free(sdp, sizeof (struct proginfo));
2426 return (err);
2430 * Go through suppress_sym_list to see if "multiply defined"
2431 * warning of this symbol should be suppressed. Return 1 if
2432 * warning should be suppressed, 0 otherwise.
2434 static int
2435 kobj_suppress_warning(char *symname)
2437 int i;
2439 for (i = 0; suppress_sym_list[i] != NULL; i++) {
2440 if (strcmp(suppress_sym_list[i], symname) == 0)
2441 return (1);
2444 return (0);
2447 static int
2448 get_syms(struct module *mp, struct _buf *file)
2450 uint_t shn;
2451 Shdr *shp;
2452 uint_t i;
2453 Sym *sp, *ksp;
2454 char *symname;
2455 int dosymtab = 0;
2458 * Find the interesting sections.
2460 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2461 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2462 switch (shp->sh_type) {
2463 case SHT_SYMTAB:
2464 mp->symtbl_section = shn;
2465 mp->symhdr = shp;
2466 dosymtab++;
2467 break;
2469 case SHT_RELA:
2470 case SHT_REL:
2472 * Already loaded.
2474 if (shp->sh_addr)
2475 continue;
2477 /* KM_TMP since kobj_free'd in do_relocations */
2478 shp->sh_addr = (Addr)
2479 kobj_alloc(shp->sh_size, KM_WAIT|KM_TMP);
2481 if (kobj_read_file(file, (char *)shp->sh_addr,
2482 shp->sh_size, shp->sh_offset) < 0) {
2483 _kobj_printf(ops, "krtld: get_syms: %s, ",
2484 mp->filename);
2485 _kobj_printf(ops, "error reading section %d\n",
2486 shn);
2487 return (-1);
2489 break;
2494 * This is true for a stripped executable. In the case of
2495 * 'unix' it can be stripped but it still contains the SHT_DYNSYM,
2496 * and since that symbol information is still present everything
2497 * is just fine.
2499 if (!dosymtab) {
2500 if (mp->flags & KOBJ_EXEC)
2501 return (0);
2502 _kobj_printf(ops, "krtld: get_syms: %s ",
2503 mp->filename);
2504 _kobj_printf(ops, "no SHT_SYMTAB symbol table found\n");
2505 return (-1);
2509 * get the associated string table header
2511 if ((mp->symhdr == 0) || (mp->symhdr->sh_link >= mp->hdr.e_shnum))
2512 return (-1);
2513 mp->strhdr = (Shdr *)
2514 (mp->shdrs + mp->symhdr->sh_link * mp->hdr.e_shentsize);
2516 mp->nsyms = mp->symhdr->sh_size / mp->symhdr->sh_entsize;
2517 mp->hashsize = kobj_gethashsize(mp->nsyms);
2520 * Allocate space for the symbol table, buckets, chains, and strings.
2522 mp->symsize = mp->symhdr->sh_size +
2523 (mp->hashsize + mp->nsyms) * sizeof (symid_t) + mp->strhdr->sh_size;
2524 mp->symspace = kobj_zalloc(mp->symsize, KM_WAIT|KM_SCRATCH);
2526 mp->symtbl = mp->symspace;
2527 mp->buckets = (symid_t *)(mp->symtbl + mp->symhdr->sh_size);
2528 mp->chains = mp->buckets + mp->hashsize;
2529 mp->strings = (char *)(mp->chains + mp->nsyms);
2531 if (kobj_read_file(file, mp->symtbl,
2532 mp->symhdr->sh_size, mp->symhdr->sh_offset) < 0 ||
2533 kobj_read_file(file, mp->strings,
2534 mp->strhdr->sh_size, mp->strhdr->sh_offset) < 0)
2535 return (-1);
2538 * loop through the symbol table adjusting values to account
2539 * for where each section got loaded into memory. Also
2540 * fill in the hash table.
2542 for (i = 1; i < mp->nsyms; i++) {
2543 sp = (Sym *)(mp->symtbl + i * mp->symhdr->sh_entsize);
2544 if (sp->st_shndx < SHN_LORESERVE) {
2545 if (sp->st_shndx >= mp->hdr.e_shnum) {
2546 _kobj_printf(ops, "%s bad shndx ",
2547 file->_name);
2548 _kobj_printf(ops, "in symbol %d\n", i);
2549 return (-1);
2551 shp = (Shdr *)
2552 (mp->shdrs +
2553 sp->st_shndx * mp->hdr.e_shentsize);
2554 if (!(mp->flags & KOBJ_EXEC))
2555 sp->st_value += shp->sh_addr;
2558 if (sp->st_name == 0 || sp->st_shndx == SHN_UNDEF)
2559 continue;
2560 if (sp->st_name >= mp->strhdr->sh_size)
2561 return (-1);
2563 symname = mp->strings + sp->st_name;
2565 if (!(mp->flags & KOBJ_EXEC) &&
2566 ELF_ST_BIND(sp->st_info) == STB_GLOBAL) {
2567 ksp = kobj_lookup_all(mp, symname, 0);
2569 if (ksp && ELF_ST_BIND(ksp->st_info) == STB_GLOBAL &&
2570 !kobj_suppress_warning(symname) &&
2571 sp->st_shndx != SHN_UNDEF &&
2572 sp->st_shndx != SHN_COMMON &&
2573 ksp->st_shndx != SHN_UNDEF &&
2574 ksp->st_shndx != SHN_COMMON) {
2576 * Unless this symbol is a stub, it's multiply
2577 * defined. Multiply-defined symbols are
2578 * usually bad, but some objects (kmdb) have
2579 * a legitimate need to have their own
2580 * copies of common functions.
2582 if ((standalone ||
2583 ksp->st_value < (uintptr_t)stubs_base ||
2584 ksp->st_value >= (uintptr_t)stubs_end) &&
2585 !(mp->flags & KOBJ_IGNMULDEF)) {
2586 _kobj_printf(ops,
2587 "%s symbol ", file->_name);
2588 _kobj_printf(ops,
2589 "%s multiply defined\n", symname);
2594 sym_insert(mp, symname, i);
2597 return (0);
2600 static int
2601 get_ctf(struct module *mp, struct _buf *file)
2603 char *shstrtab, *ctfdata;
2604 size_t shstrlen;
2605 Shdr *shp;
2606 uint_t i;
2608 if (_moddebug & MODDEBUG_NOCTF)
2609 return (0); /* do not attempt to even load CTF data */
2611 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
2612 _kobj_printf(ops, "krtld: get_ctf: %s, ",
2613 mp->filename);
2614 _kobj_printf(ops, "corrupt e_shstrndx %u\n",
2615 mp->hdr.e_shstrndx);
2616 return (-1);
2619 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
2620 shstrlen = shp->sh_size;
2621 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
2623 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
2624 _kobj_printf(ops, "krtld: get_ctf: %s, ",
2625 mp->filename);
2626 _kobj_printf(ops, "error reading section %u\n",
2627 mp->hdr.e_shstrndx);
2628 kobj_free(shstrtab, shstrlen);
2629 return (-1);
2632 for (i = 0; i < mp->hdr.e_shnum; i++) {
2633 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
2635 if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
2636 strcmp(shstrtab + shp->sh_name, ".SUNW_ctf") == 0) {
2637 ctfdata = kobj_alloc(shp->sh_size, KM_WAIT|KM_SCRATCH);
2639 if (kobj_read_file(file, ctfdata, shp->sh_size,
2640 shp->sh_offset) < 0) {
2641 _kobj_printf(ops, "krtld: get_ctf: %s, error "
2642 "reading .SUNW_ctf data\n", mp->filename);
2643 kobj_free(ctfdata, shp->sh_size);
2644 kobj_free(shstrtab, shstrlen);
2645 return (-1);
2648 mp->ctfdata = ctfdata;
2649 mp->ctfsize = shp->sh_size;
2650 break;
2654 kobj_free(shstrtab, shstrlen);
2655 return (0);
2658 #define SHA1_DIGEST_LENGTH 20 /* SHA1 digest length in bytes */
2661 * Return the hash of the ELF sections that are memory resident.
2662 * i.e. text and data. We skip a SHT_NOBITS section since it occupies
2663 * no space in the file. We use SHA1 here since libelfsign uses
2664 * it and both places need to use the same algorithm.
2666 static void
2667 crypto_es_hash(struct module *mp, char *hash, char *shstrtab)
2669 uint_t shn;
2670 Shdr *shp;
2671 SHA1_CTX ctx;
2673 SHA1Init(&ctx);
2675 for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2676 shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
2677 if (!(shp->sh_flags & SHF_ALLOC) || shp->sh_size == 0)
2678 continue;
2681 * The check should ideally be shp->sh_type == SHT_NOBITS.
2682 * However, we can't do that check here as get_progbits()
2683 * resets the type.
2685 if (strcmp(shstrtab + shp->sh_name, ".bss") == 0)
2686 continue;
2687 #ifdef KOBJ_DEBUG
2688 if (kobj_debug & D_DEBUG)
2689 _kobj_printf(ops,
2690 "krtld: crypto_es_hash: updating hash with"
2691 " %s data size=%d\n", shstrtab + shp->sh_name,
2692 shp->sh_size);
2693 #endif
2694 ASSERT(shp->sh_addr != (uintptr_t)NULL);
2695 SHA1Update(&ctx, (const uint8_t *)shp->sh_addr, shp->sh_size);
2698 SHA1Final((uchar_t *)hash, &ctx);
2702 * Get the .SUNW_signature section for the module, it it exists.
2704 * This section exists only for crypto modules. None of the
2705 * primary modules have this section currently.
2707 static void
2708 get_signature(struct module *mp, struct _buf *file)
2710 char *shstrtab, *sigdata = NULL;
2711 size_t shstrlen;
2712 Shdr *shp;
2713 uint_t i;
2715 if (mp->hdr.e_shstrndx >= mp->hdr.e_shnum) {
2716 _kobj_printf(ops, "krtld: get_signature: %s, ",
2717 mp->filename);
2718 _kobj_printf(ops, "corrupt e_shstrndx %u\n",
2719 mp->hdr.e_shstrndx);
2720 return;
2723 shp = (Shdr *)(mp->shdrs + mp->hdr.e_shstrndx * mp->hdr.e_shentsize);
2724 shstrlen = shp->sh_size;
2725 shstrtab = kobj_alloc(shstrlen, KM_WAIT|KM_TMP);
2727 if (kobj_read_file(file, shstrtab, shstrlen, shp->sh_offset) < 0) {
2728 _kobj_printf(ops, "krtld: get_signature: %s, ",
2729 mp->filename);
2730 _kobj_printf(ops, "error reading section %u\n",
2731 mp->hdr.e_shstrndx);
2732 kobj_free(shstrtab, shstrlen);
2733 return;
2736 for (i = 0; i < mp->hdr.e_shnum; i++) {
2737 shp = (Shdr *)(mp->shdrs + i * mp->hdr.e_shentsize);
2738 if (shp->sh_size != 0 && shp->sh_name < shstrlen &&
2739 strcmp(shstrtab + shp->sh_name,
2740 ELF_SIGNATURE_SECTION) == 0) {
2741 filesig_vers_t filesig_version;
2742 size_t sigsize = shp->sh_size + SHA1_DIGEST_LENGTH;
2743 sigdata = kobj_alloc(sigsize, KM_WAIT|KM_SCRATCH);
2745 if (kobj_read_file(file, sigdata, shp->sh_size,
2746 shp->sh_offset) < 0) {
2747 _kobj_printf(ops, "krtld: get_signature: %s,"
2748 " error reading .SUNW_signature data\n",
2749 mp->filename);
2750 kobj_free(sigdata, sigsize);
2751 kobj_free(shstrtab, shstrlen);
2752 return;
2754 filesig_version = ((struct filesignatures *)sigdata)->
2755 filesig_sig.filesig_version;
2756 if (!(filesig_version == FILESIG_VERSION1 ||
2757 filesig_version == FILESIG_VERSION3)) {
2758 /* skip versions we don't understand */
2759 kobj_free(sigdata, sigsize);
2760 kobj_free(shstrtab, shstrlen);
2761 return;
2764 mp->sigdata = sigdata;
2765 mp->sigsize = sigsize;
2766 break;
2770 if (sigdata != NULL) {
2771 crypto_es_hash(mp, sigdata + shp->sh_size, shstrtab);
2774 kobj_free(shstrtab, shstrlen);
2777 static void
2778 add_dependent(struct module *mp, struct module *dep)
2780 struct module_list *lp;
2782 for (lp = mp->head; lp; lp = lp->next) {
2783 if (lp->mp == dep)
2784 return; /* already on the list */
2787 if (lp == NULL) {
2788 lp = kobj_zalloc(sizeof (*lp), KM_WAIT);
2790 lp->mp = dep;
2791 lp->next = NULL;
2792 if (mp->tail)
2793 mp->tail->next = lp;
2794 else
2795 mp->head = lp;
2796 mp->tail = lp;
2800 static int
2801 do_dependents(struct modctl *modp, char *modname, size_t modnamelen)
2803 struct module *mp;
2804 struct modctl *req;
2805 char *d, *p, *q;
2806 int c;
2807 char *err_modname = NULL;
2809 mp = modp->mod_mp;
2811 if ((p = mp->depends_on) == NULL)
2812 return (0);
2814 for (;;) {
2816 * Skip space.
2818 while (*p && (*p == ' ' || *p == '\t'))
2819 p++;
2821 * Get module name.
2823 d = p;
2824 q = modname;
2825 c = 0;
2826 while (*p && *p != ' ' && *p != '\t') {
2827 if (c < modnamelen - 1) {
2828 *q++ = *p;
2829 c++;
2831 p++;
2834 if (q == modname)
2835 break;
2837 if (c == modnamelen - 1) {
2838 char *dep = kobj_alloc(p - d + 1, KM_WAIT|KM_TMP);
2840 (void) strncpy(dep, d, p - d + 1);
2841 dep[p - d] = '\0';
2843 _kobj_printf(ops, "%s: dependency ", modp->mod_modname);
2844 _kobj_printf(ops, "'%s' too long ", dep);
2845 _kobj_printf(ops, "(max %d chars)\n", modnamelen);
2847 kobj_free(dep, p - d + 1);
2849 return (-1);
2852 *q = '\0';
2853 if ((req = mod_load_requisite(modp, modname)) == NULL) {
2854 #ifndef KOBJ_DEBUG
2855 if (_moddebug & MODDEBUG_LOADMSG) {
2856 #endif /* KOBJ_DEBUG */
2857 _kobj_printf(ops,
2858 "%s: unable to resolve dependency, ",
2859 modp->mod_modname);
2860 _kobj_printf(ops, "cannot load module '%s'\n",
2861 modname);
2862 #ifndef KOBJ_DEBUG
2864 #endif /* KOBJ_DEBUG */
2865 if (err_modname == NULL) {
2867 * This must be the same size as the modname
2868 * one.
2870 err_modname = kobj_zalloc(MODMAXNAMELEN,
2871 KM_WAIT);
2874 * We can use strcpy() here without fearing
2875 * the NULL terminator because the size of
2876 * err_modname is the same as one of modname,
2877 * and it's filled with zeros.
2879 (void) strcpy(err_modname, modname);
2881 continue;
2884 add_dependent(mp, req->mod_mp);
2885 mod_release_mod(req);
2889 if (err_modname != NULL) {
2891 * Copy the first module name where you detect an error to keep
2892 * its behavior the same as before.
2893 * This way keeps minimizing the memory use for error
2894 * modules, and this might be important at boot time because
2895 * the memory usage is a crucial factor for booting in most
2896 * cases. You can expect more verbose messages when using
2897 * a debug kernel or setting a bit in moddebug.
2899 bzero(modname, MODMAXNAMELEN);
2900 (void) strcpy(modname, err_modname);
2901 kobj_free(err_modname, MODMAXNAMELEN);
2902 return (-1);
2905 return (0);
2908 static int
2909 do_common(struct module *mp)
2911 int err;
2914 * first time through, assign all symbols defined in other
2915 * modules, and count up how much common space will be needed
2916 * (bss_size and bss_align)
2918 if ((err = do_symbols(mp, 0)) < 0)
2919 return (err);
2921 * increase bss_size by the maximum delta that could be
2922 * computed by the ALIGN below
2924 mp->bss_size += mp->bss_align;
2925 if (mp->bss_size) {
2926 if (standalone)
2927 mp->bss = (uintptr_t)kobj_segbrk(&_edata, mp->bss_size,
2928 MINALIGN, 0);
2929 else
2930 mp->bss = (uintptr_t)vmem_alloc(data_arena,
2931 mp->bss_size, VM_SLEEP | VM_BESTFIT);
2932 bzero((void *)mp->bss, mp->bss_size);
2933 /* now assign addresses to all common symbols */
2934 if ((err = do_symbols(mp, ALIGN(mp->bss, mp->bss_align))) < 0)
2935 return (err);
2937 return (0);
2940 static int
2941 do_symbols(struct module *mp, Elf64_Addr bss_base)
2943 int bss_align;
2944 uintptr_t bss_ptr;
2945 int err;
2946 int i;
2947 Sym *sp, *sp1;
2948 char *name;
2949 int assign;
2950 int resolved = 1;
2953 * Nothing left to do (optimization).
2955 if (mp->flags & KOBJ_RESOLVED)
2956 return (0);
2958 assign = (bss_base) ? 1 : 0;
2959 bss_ptr = bss_base;
2960 bss_align = 0;
2961 err = 0;
2963 for (i = 1; i < mp->nsyms; i++) {
2964 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * i);
2966 * we know that st_name is in bounds, since get_sections
2967 * has already checked all of the symbols
2969 name = mp->strings + sp->st_name;
2970 if (sp->st_shndx != SHN_UNDEF && sp->st_shndx != SHN_COMMON)
2971 continue;
2973 * TLS symbols are ignored in the kernel
2975 if (ELF_ST_TYPE(sp->st_info) == STT_TLS) {
2976 _kobj_printf(ops, "%s: TLS symbol ",
2977 mp->filename);
2978 _kobj_printf(ops, "not supported '%s'\n",
2979 name);
2980 err = DOSYM_UNDEF;
2981 continue;
2984 if (ELF_ST_BIND(sp->st_info) != STB_LOCAL) {
2985 if ((sp1 = kobj_lookup_all(mp, name, 0)) != NULL) {
2986 sp->st_shndx = SHN_ABS;
2987 sp->st_value = sp1->st_value;
2988 continue;
2992 if (sp->st_shndx == SHN_UNDEF) {
2993 resolved = 0;
2995 if (strncmp(name, sdt_prefix, strlen(sdt_prefix)) == 0)
2996 continue;
2999 * If it's not a weak reference and it's
3000 * not a primary object, it's an error.
3001 * (Primary objects may take more than
3002 * one pass to resolve)
3004 if (!(mp->flags & KOBJ_PRIM) &&
3005 ELF_ST_BIND(sp->st_info) != STB_WEAK) {
3006 _kobj_printf(ops, "%s: undefined symbol",
3007 mp->filename);
3008 _kobj_printf(ops, " '%s'\n", name);
3010 * Try to determine whether this symbol
3011 * represents a dependency on obsolete
3012 * unsafe driver support. This is just
3013 * to make the warning more informative.
3015 if (strcmp(name, "sleep") == 0 ||
3016 strcmp(name, "unsleep") == 0 ||
3017 strcmp(name, "wakeup") == 0 ||
3018 strcmp(name, "bsd_compat_ioctl") == 0 ||
3019 strcmp(name, "unsafe_driver") == 0 ||
3020 strncmp(name, "spl", 3) == 0 ||
3021 strncmp(name, "i_ddi_spl", 9) == 0)
3022 err = DOSYM_UNSAFE;
3023 if (err == 0)
3024 err = DOSYM_UNDEF;
3026 continue;
3029 * It's a common symbol - st_value is the
3030 * required alignment.
3032 if (sp->st_value > bss_align)
3033 bss_align = sp->st_value;
3034 bss_ptr = ALIGN(bss_ptr, sp->st_value);
3035 if (assign) {
3036 sp->st_shndx = SHN_ABS;
3037 sp->st_value = bss_ptr;
3039 bss_ptr += sp->st_size;
3041 if (err)
3042 return (err);
3043 if (assign == 0 && mp->bss == (uintptr_t)NULL) {
3044 mp->bss_align = bss_align;
3045 mp->bss_size = bss_ptr;
3046 } else if (resolved) {
3047 mp->flags |= KOBJ_RESOLVED;
3050 return (0);
3053 uint_t
3054 kobj_hash_name(const char *p)
3056 uint_t g;
3057 uint_t hval;
3059 hval = 0;
3060 while (*p) {
3061 hval = (hval << 4) + *p++;
3062 if ((g = (hval & 0xf0000000)) != 0)
3063 hval ^= g >> 24;
3064 hval &= ~g;
3066 return (hval);
3069 /* look for name in all modules */
3070 uintptr_t
3071 kobj_getsymvalue(char *name, int kernelonly)
3073 Sym *sp;
3074 struct modctl *modp;
3075 struct module *mp;
3076 uintptr_t value = 0;
3078 if ((sp = kobj_lookup_kernel(name)) != NULL)
3079 return ((uintptr_t)sp->st_value);
3081 if (kernelonly)
3082 return (0); /* didn't find it in the kernel so give up */
3084 mutex_enter(&mod_lock);
3085 modp = &modules;
3086 do {
3087 mp = (struct module *)modp->mod_mp;
3088 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
3089 (sp = lookup_one(mp, name))) {
3090 value = (uintptr_t)sp->st_value;
3091 break;
3093 } while ((modp = modp->mod_next) != &modules);
3094 mutex_exit(&mod_lock);
3095 return (value);
3098 /* look for a symbol near value. */
3099 char *
3100 kobj_getsymname(uintptr_t value, ulong_t *offset)
3102 char *name = NULL;
3103 struct modctl *modp;
3105 struct modctl_list *lp;
3106 struct module *mp;
3109 * Loop through the primary kernel modules.
3111 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
3112 mp = mod(lp);
3114 if ((name = kobj_searchsym(mp, value, offset)) != NULL)
3115 return (name);
3118 mutex_enter(&mod_lock);
3119 modp = &modules;
3120 do {
3121 mp = (struct module *)modp->mod_mp;
3122 if (mp && !(mp->flags & KOBJ_PRIM) && modp->mod_loaded &&
3123 (name = kobj_searchsym(mp, value, offset)))
3124 break;
3125 } while ((modp = modp->mod_next) != &modules);
3126 mutex_exit(&mod_lock);
3127 return (name);
3130 /* return address of symbol and size */
3132 uintptr_t
3133 kobj_getelfsym(char *name, void *mp, int *size)
3135 Sym *sp;
3137 if (mp == NULL)
3138 sp = kobj_lookup_kernel(name);
3139 else
3140 sp = lookup_one(mp, name);
3142 if (sp == NULL)
3143 return (0);
3145 *size = (int)sp->st_size;
3146 return ((uintptr_t)sp->st_value);
3149 uintptr_t
3150 kobj_lookup(struct module *mod, const char *name)
3152 Sym *sp;
3154 sp = lookup_one(mod, name);
3156 if (sp == NULL)
3157 return (0);
3159 return ((uintptr_t)sp->st_value);
3162 char *
3163 kobj_searchsym(struct module *mp, uintptr_t value, ulong_t *offset)
3165 Sym *symtabptr;
3166 char *strtabptr;
3167 int symnum;
3168 Sym *sym;
3169 Sym *cursym;
3170 uintptr_t curval;
3172 *offset = (ulong_t)-1l; /* assume not found */
3173 cursym = NULL;
3175 if (kobj_addrcheck(mp, (void *)value) != 0)
3176 return (NULL); /* not in this module */
3178 strtabptr = mp->strings;
3179 symtabptr = (Sym *)mp->symtbl;
3182 * Scan the module's symbol table for a symbol <= value
3184 for (symnum = 1, sym = symtabptr + 1;
3185 symnum < mp->nsyms; symnum++, sym = (Sym *)
3186 ((uintptr_t)sym + mp->symhdr->sh_entsize)) {
3187 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
3188 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
3189 continue;
3190 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
3191 ELF_ST_TYPE(sym->st_info) != STT_FUNC)
3192 continue;
3195 curval = (uintptr_t)sym->st_value;
3197 if (curval > value)
3198 continue;
3201 * If one or both are functions...
3203 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC || (cursym != NULL &&
3204 ELF_ST_TYPE(cursym->st_info) == STT_FUNC)) {
3205 /* Ignore if the address is out of the bounds */
3206 if (value - sym->st_value >= sym->st_size)
3207 continue;
3209 if (cursym != NULL &&
3210 ELF_ST_TYPE(cursym->st_info) == STT_FUNC) {
3211 /* Prefer the function to the non-function */
3212 if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
3213 continue;
3215 /* Prefer the larger of the two functions */
3216 if (sym->st_size <= cursym->st_size)
3217 continue;
3219 } else if (value - curval >= *offset) {
3220 continue;
3223 *offset = (ulong_t)(value - curval);
3224 cursym = sym;
3226 if (cursym == NULL)
3227 return (NULL);
3229 return (strtabptr + cursym->st_name);
3232 Sym *
3233 kobj_lookup_all(struct module *mp, char *name, int include_self)
3235 Sym *sp;
3236 struct module_list *mlp;
3237 struct modctl_list *clp;
3238 struct module *mmp;
3240 if (include_self && (sp = lookup_one(mp, name)) != NULL)
3241 return (sp);
3243 for (mlp = mp->head; mlp; mlp = mlp->next) {
3244 if ((sp = lookup_one(mlp->mp, name)) != NULL &&
3245 ELF_ST_BIND(sp->st_info) != STB_LOCAL)
3246 return (sp);
3250 * Loop through the primary kernel modules.
3252 for (clp = kobj_lm_lookup(KOBJ_LM_PRIMARY); clp; clp = clp->modl_next) {
3253 mmp = mod(clp);
3255 if (mmp == NULL || mp == mmp)
3256 continue;
3258 if ((sp = lookup_one(mmp, name)) != NULL &&
3259 ELF_ST_BIND(sp->st_info) != STB_LOCAL)
3260 return (sp);
3262 return (NULL);
3265 Sym *
3266 kobj_lookup_kernel(const char *name)
3268 struct modctl_list *lp;
3269 struct module *mp;
3270 Sym *sp;
3273 * Loop through the primary kernel modules.
3275 for (lp = kobj_lm_lookup(KOBJ_LM_PRIMARY); lp; lp = lp->modl_next) {
3276 mp = mod(lp);
3278 if (mp == NULL)
3279 continue;
3281 if ((sp = lookup_one(mp, name)) != NULL)
3282 return (sp);
3284 return (NULL);
3287 static Sym *
3288 lookup_one(struct module *mp, const char *name)
3290 symid_t *ip;
3291 char *name1;
3292 Sym *sp;
3294 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3295 ip = &mp->chains[*ip]) {
3296 sp = (Sym *)(mp->symtbl +
3297 mp->symhdr->sh_entsize * *ip);
3298 name1 = mp->strings + sp->st_name;
3299 if (strcmp(name, name1) == 0 &&
3300 ELF_ST_TYPE(sp->st_info) != STT_FILE &&
3301 sp->st_shndx != SHN_UNDEF &&
3302 sp->st_shndx != SHN_COMMON)
3303 return (sp);
3305 return (NULL);
3309 * Lookup a given symbol pointer in the module's symbol hash. If the symbol
3310 * is hashed, return the symbol pointer; otherwise return NULL.
3312 static Sym *
3313 sym_lookup(struct module *mp, Sym *ksp)
3315 char *name = mp->strings + ksp->st_name;
3316 symid_t *ip;
3317 Sym *sp;
3319 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3320 ip = &mp->chains[*ip]) {
3321 sp = (Sym *)(mp->symtbl + mp->symhdr->sh_entsize * *ip);
3322 if (sp == ksp)
3323 return (ksp);
3325 return (NULL);
3328 static void
3329 sym_insert(struct module *mp, char *name, symid_t index)
3331 symid_t *ip;
3333 #ifdef KOBJ_DEBUG
3334 if (kobj_debug & D_SYMBOLS) {
3335 static struct module *lastmp = NULL;
3336 Sym *sp;
3337 if (lastmp != mp) {
3338 _kobj_printf(ops,
3339 "krtld: symbol entry: file=%s\n",
3340 mp->filename);
3341 _kobj_printf(ops,
3342 "krtld:\tsymndx\tvalue\t\t"
3343 "symbol name\n");
3344 lastmp = mp;
3346 sp = (Sym *)(mp->symtbl +
3347 index * mp->symhdr->sh_entsize);
3348 _kobj_printf(ops, "krtld:\t[%3d]", index);
3349 _kobj_printf(ops, "\t0x%lx", sp->st_value);
3350 _kobj_printf(ops, "\t%s\n", name);
3353 #endif
3354 for (ip = &mp->buckets[kobj_hash_name(name) % mp->hashsize]; *ip;
3355 ip = &mp->chains[*ip]) {
3358 *ip = index;
3361 struct modctl *
3362 kobj_boot_mod_lookup(const char *modname)
3364 struct modctl *mctl = kobj_modules;
3366 do {
3367 if (strcmp(modname, mctl->mod_modname) == 0)
3368 return (mctl);
3369 } while ((mctl = mctl->mod_next) != kobj_modules);
3371 return (NULL);
3375 * Determine if the module exists.
3378 kobj_path_exists(char *name, int use_path)
3380 struct _buf *file;
3382 file = kobj_open_path(name, use_path, 1);
3383 #ifdef MODDIR_SUFFIX
3384 if (file == (struct _buf *)-1)
3385 file = kobj_open_path(name, use_path, 0);
3386 #endif /* MODDIR_SUFFIX */
3387 if (file == (struct _buf *)-1)
3388 return (0);
3389 kobj_close_file(file);
3390 return (1);
3394 * fullname is dynamically allocated to be able to hold the
3395 * maximum size string that can be constructed from name.
3396 * path is exactly like the shell PATH variable.
3398 struct _buf *
3399 kobj_open_path(char *name, int use_path, int use_moddir_suffix)
3401 char *p, *q;
3402 char *pathp;
3403 char *pathpsave;
3404 char *fullname;
3405 int maxpathlen;
3406 struct _buf *file;
3408 #if !defined(MODDIR_SUFFIX)
3409 use_moddir_suffix = B_FALSE;
3410 #endif
3412 if (!use_path)
3413 pathp = ""; /* use name as specified */
3414 else
3415 pathp = kobj_module_path;
3416 /* use configured default path */
3418 pathpsave = pathp; /* keep this for error reporting */
3421 * Allocate enough space for the largest possible fullname.
3422 * since path is of the form <directory> : <directory> : ...
3423 * we're potentially allocating a little more than we need to
3424 * but we'll allocate the exact amount when we find the right directory.
3425 * (The + 3 below is one for NULL terminator and one for the '/'
3426 * we might have to add at the beginning of path and one for
3427 * the '/' between path and name.)
3429 maxpathlen = strlen(pathp) + strlen(name) + 3;
3430 /* sizeof includes null */
3431 maxpathlen += sizeof (slash_moddir_suffix_slash) - 1;
3432 fullname = kobj_zalloc(maxpathlen, KM_WAIT);
3434 for (;;) {
3435 p = fullname;
3436 if (*pathp != '\0' && *pathp != '/')
3437 *p++ = '/'; /* path must start with '/' */
3438 while (*pathp && *pathp != ':' && *pathp != ' ')
3439 *p++ = *pathp++;
3440 if (p != fullname && p[-1] != '/')
3441 *p++ = '/';
3442 if (use_moddir_suffix) {
3443 char *b = basename(name);
3444 char *s;
3446 /* copy everything up to the base name */
3447 q = name;
3448 while (q != b && *q)
3449 *p++ = *q++;
3450 s = slash_moddir_suffix_slash;
3451 while (*s)
3452 *p++ = *s++;
3453 /* copy the rest */
3454 while (*b)
3455 *p++ = *b++;
3456 } else {
3457 q = name;
3458 while (*q)
3459 *p++ = *q++;
3461 *p = 0;
3462 if ((file = kobj_open_file(fullname)) != (struct _buf *)-1) {
3463 kobj_free(fullname, maxpathlen);
3464 return (file);
3466 while (*pathp == ' ' || *pathp == ':')
3467 pathp++;
3468 if (*pathp == 0)
3469 break;
3472 kobj_free(fullname, maxpathlen);
3473 if (_moddebug & MODDEBUG_ERRMSG) {
3474 _kobj_printf(ops, "can't open %s,", name);
3475 _kobj_printf(ops, " path is %s\n", pathpsave);
3477 return ((struct _buf *)-1);
3480 intptr_t
3481 kobj_open(char *filename)
3483 struct vnode *vp;
3484 int fd;
3486 if (_modrootloaded) {
3487 struct kobjopen_tctl *ltp = kobjopen_alloc(filename);
3488 int Errno;
3491 * Hand off the open to a thread who has a
3492 * stack size capable handling the request.
3494 if (curthread != &t0) {
3495 (void) thread_create(NULL, DEFAULTSTKSZ * 2,
3496 kobjopen_thread, ltp, 0, &p0, TS_RUN, maxclsyspri);
3497 sema_p(&ltp->sema);
3498 Errno = ltp->Errno;
3499 vp = ltp->vp;
3500 } else {
3502 * 1098067: module creds should not be those of the
3503 * caller
3505 cred_t *saved_cred = curthread->t_cred;
3506 curthread->t_cred = kcred;
3507 Errno = vn_openat(filename, UIO_SYSSPACE, FREAD, 0, &vp,
3508 0, 0, rootdir, -1);
3509 curthread->t_cred = saved_cred;
3511 kobjopen_free(ltp);
3513 if (Errno) {
3514 if (_moddebug & MODDEBUG_ERRMSG) {
3515 _kobj_printf(ops,
3516 "kobj_open: vn_open of %s fails, ",
3517 filename);
3518 _kobj_printf(ops, "Errno = %d\n", Errno);
3520 return (-1);
3521 } else {
3522 if (_moddebug & MODDEBUG_ERRMSG) {
3523 _kobj_printf(ops, "kobj_open: '%s'", filename);
3524 _kobj_printf(ops, " vp = %p\n", vp);
3526 return ((intptr_t)vp);
3528 } else {
3529 fd = kobj_boot_open(filename, 0);
3531 if (_moddebug & MODDEBUG_ERRMSG) {
3532 if (fd < 0)
3533 _kobj_printf(ops,
3534 "kobj_open: can't open %s\n", filename);
3535 else {
3536 _kobj_printf(ops, "kobj_open: '%s'", filename);
3537 _kobj_printf(ops, " descr = 0x%x\n", fd);
3540 return ((intptr_t)fd);
3545 * Calls to kobj_open() are handled off to this routine as a separate thread.
3547 static void
3548 kobjopen_thread(struct kobjopen_tctl *ltp)
3550 kmutex_t cpr_lk;
3551 callb_cpr_t cpr_i;
3553 mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
3554 CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "kobjopen");
3555 ltp->Errno = vn_open(ltp->name, UIO_SYSSPACE, FREAD, 0, &(ltp->vp),
3556 0, 0);
3557 sema_v(&ltp->sema);
3558 mutex_enter(&cpr_lk);
3559 CALLB_CPR_EXIT(&cpr_i);
3560 mutex_destroy(&cpr_lk);
3561 thread_exit();
3565 * allocate and initialize a kobjopen thread structure
3567 static struct kobjopen_tctl *
3568 kobjopen_alloc(char *filename)
3570 struct kobjopen_tctl *ltp = kmem_zalloc(sizeof (*ltp), KM_SLEEP);
3572 ASSERT(filename != NULL);
3574 ltp->name = kmem_alloc(strlen(filename) + 1, KM_SLEEP);
3575 bcopy(filename, ltp->name, strlen(filename) + 1);
3576 sema_init(&ltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
3577 return (ltp);
3581 * free a kobjopen thread control structure
3583 static void
3584 kobjopen_free(struct kobjopen_tctl *ltp)
3586 sema_destroy(&ltp->sema);
3587 kmem_free(ltp->name, strlen(ltp->name) + 1);
3588 kmem_free(ltp, sizeof (*ltp));
3592 kobj_read(intptr_t descr, char *buf, uint_t size, uint_t offset)
3594 int stat;
3595 ssize_t resid;
3597 if (_modrootloaded) {
3598 if ((stat = vn_rdwr(UIO_READ, (struct vnode *)descr, buf, size,
3599 (offset_t)offset, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(),
3600 &resid)) != 0) {
3601 _kobj_printf(ops,
3602 "vn_rdwr failed with error 0x%x\n", stat);
3603 return (-1);
3605 return (size - resid);
3606 } else {
3607 int count = 0;
3609 if (kobj_boot_seek((int)descr, (off_t)0, offset) != 0) {
3610 _kobj_printf(ops,
3611 "kobj_read: seek 0x%x failed\n", offset);
3612 return (-1);
3615 count = kobj_boot_read((int)descr, buf, size);
3616 if (count < size) {
3617 if (_moddebug & MODDEBUG_ERRMSG) {
3618 _kobj_printf(ops,
3619 "kobj_read: req %d bytes, ", size);
3620 _kobj_printf(ops, "got %d\n", count);
3623 return (count);
3627 void
3628 kobj_close(intptr_t descr)
3630 if (_moddebug & MODDEBUG_ERRMSG)
3631 _kobj_printf(ops, "kobj_close: 0x%lx\n", descr);
3633 if (_modrootloaded) {
3634 struct vnode *vp = (struct vnode *)descr;
3635 (void) fop_close(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
3636 VN_RELE(vp);
3637 } else
3638 (void) kobj_boot_close((int)descr);
3642 kobj_fstat(intptr_t descr, struct bootstat *buf)
3644 if (buf == NULL)
3645 return (-1);
3647 if (_modrootloaded) {
3648 vattr_t vattr;
3649 struct vnode *vp = (struct vnode *)descr;
3650 if (fop_getattr(vp, &vattr, 0, kcred, NULL) != 0)
3651 return (-1);
3654 * The vattr and bootstat structures are similar, but not
3655 * identical. We do our best to fill in the bootstat structure
3656 * from the contents of vattr (transfering only the ones that
3657 * are obvious.
3660 buf->st_mode = (uint32_t)vattr.va_mode;
3661 buf->st_nlink = (uint32_t)vattr.va_nlink;
3662 buf->st_uid = (int32_t)vattr.va_uid;
3663 buf->st_gid = (int32_t)vattr.va_gid;
3664 buf->st_rdev = (uint64_t)vattr.va_rdev;
3665 buf->st_size = (uint64_t)vattr.va_size;
3666 buf->st_atim.tv_sec = (int64_t)vattr.va_atime.tv_sec;
3667 buf->st_atim.tv_nsec = (int64_t)vattr.va_atime.tv_nsec;
3668 buf->st_mtim.tv_sec = (int64_t)vattr.va_mtime.tv_sec;
3669 buf->st_mtim.tv_nsec = (int64_t)vattr.va_mtime.tv_nsec;
3670 buf->st_ctim.tv_sec = (int64_t)vattr.va_ctime.tv_sec;
3671 buf->st_ctim.tv_nsec = (int64_t)vattr.va_ctime.tv_nsec;
3672 buf->st_blksize = (int32_t)vattr.va_blksize;
3673 buf->st_blocks = (int64_t)vattr.va_nblocks;
3675 return (0);
3678 return (kobj_boot_fstat((int)descr, buf));
3682 struct _buf *
3683 kobj_open_file(char *name)
3685 struct _buf *file;
3686 struct compinfo cbuf;
3687 intptr_t fd;
3689 if ((fd = kobj_open(name)) == -1) {
3690 return ((struct _buf *)-1);
3693 file = kobj_zalloc(sizeof (struct _buf), KM_WAIT|KM_TMP);
3694 file->_fd = fd;
3695 file->_name = kobj_alloc(strlen(name)+1, KM_WAIT|KM_TMP);
3696 file->_cnt = file->_size = file->_off = 0;
3697 file->_ln = 1;
3698 file->_ptr = file->_base;
3699 (void) strcpy(file->_name, name);
3702 * Before root is mounted, we must check
3703 * for a compressed file and do our own
3704 * buffering.
3706 if (_modrootloaded) {
3707 file->_base = kobj_zalloc(MAXBSIZE, KM_WAIT);
3708 file->_bsize = MAXBSIZE;
3710 /* Check if the file is compressed */
3711 file->_iscmp = kobj_is_compressed(fd);
3712 } else {
3713 if (kobj_boot_compinfo(fd, &cbuf) != 0) {
3714 kobj_close_file(file);
3715 return ((struct _buf *)-1);
3717 file->_iscmp = cbuf.iscmp;
3718 if (file->_iscmp) {
3719 if (kobj_comp_setup(file, &cbuf) != 0) {
3720 kobj_close_file(file);
3721 return ((struct _buf *)-1);
3723 } else {
3724 file->_base = kobj_zalloc(cbuf.blksize, KM_WAIT|KM_TMP);
3725 file->_bsize = cbuf.blksize;
3728 return (file);
3731 static int
3732 kobj_comp_setup(struct _buf *file, struct compinfo *cip)
3734 struct comphdr *hdr;
3737 * read the compressed image into memory,
3738 * so we can deompress from there
3740 file->_dsize = cip->fsize;
3741 file->_dbuf = kobj_alloc(cip->fsize, KM_WAIT|KM_TMP);
3742 if (kobj_read(file->_fd, file->_dbuf, cip->fsize, 0) != cip->fsize) {
3743 kobj_free(file->_dbuf, cip->fsize);
3744 return (-1);
3747 hdr = kobj_comphdr(file);
3748 if (hdr->ch_magic != CH_MAGIC_ZLIB || hdr->ch_version != CH_VERSION ||
3749 hdr->ch_algorithm != CH_ALG_ZLIB || hdr->ch_fsize == 0 ||
3750 !ISP2(hdr->ch_blksize)) {
3751 kobj_free(file->_dbuf, cip->fsize);
3752 return (-1);
3754 file->_base = kobj_alloc(hdr->ch_blksize, KM_WAIT|KM_TMP);
3755 file->_bsize = hdr->ch_blksize;
3756 return (0);
3759 void
3760 kobj_close_file(struct _buf *file)
3762 kobj_close(file->_fd);
3763 if (file->_base != NULL)
3764 kobj_free(file->_base, file->_bsize);
3765 if (file->_dbuf != NULL)
3766 kobj_free(file->_dbuf, file->_dsize);
3767 kobj_free(file->_name, strlen(file->_name)+1);
3768 kobj_free(file, sizeof (struct _buf));
3772 kobj_read_file(struct _buf *file, char *buf, uint_t size, uint_t off)
3774 int b_size, c_size;
3775 int b_off; /* Offset into buffer for start of bcopy */
3776 int count = 0;
3777 int page_addr;
3779 if (_moddebug & MODDEBUG_ERRMSG) {
3780 _kobj_printf(ops, "kobj_read_file: size=%x,", size);
3781 _kobj_printf(ops, " offset=%x at", off);
3782 _kobj_printf(ops, " buf=%x\n", buf);
3786 * Handle compressed (gzip for now) file here. First get the
3787 * compressed size, then read the image into memory and finally
3788 * call zlib to decompress the image at the supplied memory buffer.
3790 if (file->_iscmp == CH_MAGIC_GZIP) {
3791 ulong_t dlen;
3792 vattr_t vattr;
3793 struct vnode *vp = (struct vnode *)file->_fd;
3794 ssize_t resid;
3795 int err = 0;
3797 if (fop_getattr(vp, &vattr, 0, kcred, NULL) != 0)
3798 return (-1);
3800 file->_dbuf = kobj_alloc(vattr.va_size, KM_WAIT|KM_TMP);
3801 file->_dsize = vattr.va_size;
3803 /* Read the compressed file into memory */
3804 if ((err = vn_rdwr(UIO_READ, vp, file->_dbuf, vattr.va_size,
3805 (offset_t)(0), UIO_SYSSPACE, 0, (rlim64_t)0, CRED(),
3806 &resid)) != 0) {
3808 _kobj_printf(ops, "kobj_read_file :vn_rdwr() failed, "
3809 "error code 0x%x\n", err);
3810 return (-1);
3813 dlen = size;
3815 /* Decompress the image at the supplied memory buffer */
3816 if ((err = z_uncompress(buf, &dlen, file->_dbuf,
3817 vattr.va_size)) != Z_OK) {
3818 _kobj_printf(ops, "kobj_read_file: z_uncompress "
3819 "failed, error code : 0x%x\n", err);
3820 return (-1);
3823 if (dlen != size) {
3824 _kobj_printf(ops, "kobj_read_file: z_uncompress "
3825 "failed to uncompress (size returned 0x%x , "
3826 "expected size: 0x%x)\n", dlen, size);
3827 return (-1);
3830 return (0);
3833 while (size) {
3834 page_addr = F_PAGE(file, off);
3835 b_size = file->_size;
3837 * If we have the filesystem page the caller's referring to
3838 * and we have something in the buffer,
3839 * satisfy as much of the request from the buffer as we can.
3841 if (page_addr == file->_off && b_size > 0) {
3842 b_off = B_OFFSET(file, off);
3843 c_size = b_size - b_off;
3845 * If there's nothing to copy, we're at EOF.
3847 if (c_size <= 0)
3848 break;
3849 if (c_size > size)
3850 c_size = size;
3851 if (buf) {
3852 if (_moddebug & MODDEBUG_ERRMSG)
3853 _kobj_printf(ops, "copying %x bytes\n",
3854 c_size);
3855 bcopy(file->_base+b_off, buf, c_size);
3856 size -= c_size;
3857 off += c_size;
3858 buf += c_size;
3859 count += c_size;
3860 } else {
3861 _kobj_printf(ops, "kobj_read: system error");
3862 count = -1;
3863 break;
3865 } else {
3867 * If the caller's offset is page aligned and
3868 * the caller want's at least a filesystem page and
3869 * the caller provided a buffer,
3870 * read directly into the caller's buffer.
3872 if (page_addr == off &&
3873 (c_size = F_BLKS(file, size)) && buf) {
3874 c_size = kobj_read_blks(file, buf, c_size,
3875 page_addr);
3876 if (c_size < 0) {
3877 count = -1;
3878 break;
3880 count += c_size;
3881 if (c_size != F_BLKS(file, size))
3882 break;
3883 size -= c_size;
3884 off += c_size;
3885 buf += c_size;
3887 * Otherwise, read into our buffer and copy next time
3888 * around the loop.
3890 } else {
3891 file->_off = page_addr;
3892 c_size = kobj_read_blks(file, file->_base,
3893 file->_bsize, page_addr);
3894 file->_ptr = file->_base;
3895 file->_cnt = c_size;
3896 file->_size = c_size;
3898 * If a _filbuf call or nothing read, break.
3900 if (buf == NULL || c_size <= 0) {
3901 count = c_size;
3902 break;
3905 if (_moddebug & MODDEBUG_ERRMSG)
3906 _kobj_printf(ops, "read %x bytes\n", c_size);
3909 if (_moddebug & MODDEBUG_ERRMSG)
3910 _kobj_printf(ops, "count = %x\n", count);
3912 return (count);
3915 static int
3916 kobj_read_blks(struct _buf *file, char *buf, uint_t size, uint_t off)
3918 int ret;
3920 ASSERT(B_OFFSET(file, size) == 0 && B_OFFSET(file, off) == 0);
3921 if (file->_iscmp) {
3922 uint_t blks;
3923 int nret;
3925 ret = 0;
3926 for (blks = size / file->_bsize; blks != 0; blks--) {
3927 nret = kobj_uncomp_blk(file, buf, off);
3928 if (nret == -1)
3929 return (-1);
3930 buf += nret;
3931 off += nret;
3932 ret += nret;
3933 if (nret < file->_bsize)
3934 break;
3936 } else
3937 ret = kobj_read(file->_fd, buf, size, off);
3938 return (ret);
3941 static int
3942 kobj_uncomp_blk(struct _buf *file, char *buf, uint_t off)
3944 struct comphdr *hdr = kobj_comphdr(file);
3945 ulong_t dlen, slen;
3946 caddr_t src;
3947 int i;
3949 dlen = file->_bsize;
3950 i = off / file->_bsize;
3951 src = file->_dbuf + hdr->ch_blkmap[i];
3952 if (i == hdr->ch_fsize / file->_bsize)
3953 slen = file->_dsize - hdr->ch_blkmap[i];
3954 else
3955 slen = hdr->ch_blkmap[i + 1] - hdr->ch_blkmap[i];
3956 if (z_uncompress(buf, &dlen, src, slen) != Z_OK)
3957 return (-1);
3958 return (dlen);
3962 kobj_filbuf(struct _buf *f)
3964 if (kobj_read_file(f, NULL, f->_bsize, f->_off + f->_size) > 0)
3965 return (kobj_getc(f));
3966 return (-1);
3969 void
3970 kobj_free(void *address, size_t size)
3972 if (standalone)
3973 return;
3975 kmem_free(address, size);
3976 kobj_stat.nfree_calls++;
3977 kobj_stat.nfree += size;
3980 void *
3981 kobj_zalloc(size_t size, int flag)
3983 void *v;
3985 if ((v = kobj_alloc(size, flag)) != 0) {
3986 bzero(v, size);
3989 return (v);
3992 void *
3993 kobj_alloc(size_t size, int flag)
3996 * If we are running standalone in the
3997 * linker, we ask boot for memory.
3998 * Either it's temporary memory that we lose
3999 * once boot is mapped out or we allocate it
4000 * permanently using the dynamic data segment.
4002 if (standalone) {
4003 #if defined(_OBP)
4004 if (flag & (KM_TMP | KM_SCRATCH))
4005 return (bop_temp_alloc(size, MINALIGN));
4006 #else
4007 if (flag & (KM_TMP | KM_SCRATCH))
4008 return (BOP_ALLOC(ops, 0, size, MINALIGN));
4009 #endif
4010 return (kobj_segbrk(&_edata, size, MINALIGN, 0));
4013 kobj_stat.nalloc_calls++;
4014 kobj_stat.nalloc += size;
4016 return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP));
4020 * Allow the "mod" system to sync up with the work
4021 * already done by kobj during the initial loading
4022 * of the kernel. This also gives us a chance
4023 * to reallocate memory that belongs to boot.
4025 void
4026 kobj_sync(void)
4028 struct modctl_list *lp, **lpp;
4031 * The module path can be set in /etc/system via 'moddir' commands
4033 if (default_path != NULL)
4034 kobj_module_path = default_path;
4035 else
4036 default_path = kobj_module_path;
4038 ksyms_arena = vmem_create("ksyms", NULL, 0, sizeof (uint64_t),
4039 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4041 ctf_arena = vmem_create("ctf", NULL, 0, sizeof (uint_t),
4042 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
4045 * Move symbol tables from boot memory to ksyms_arena.
4047 for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
4048 for (lp = *lpp; lp != NULL; lp = lp->modl_next)
4049 kobj_export_module(mod(lp));
4053 caddr_t
4054 kobj_segbrk(caddr_t *spp, size_t size, size_t align, caddr_t limit)
4056 uintptr_t va, pva;
4057 size_t alloc_pgsz = kobj_mmu_pagesize;
4058 size_t alloc_align = BO_NO_ALIGN;
4059 size_t alloc_size;
4062 * If we are using "large" mappings for the kernel,
4063 * request aligned memory from boot using the
4064 * "large" pagesize.
4066 if (lg_pagesize) {
4067 alloc_align = lg_pagesize;
4068 alloc_pgsz = lg_pagesize;
4072 va = ALIGN((uintptr_t)*spp, align);
4073 pva = P2ROUNDUP((uintptr_t)*spp, alloc_pgsz);
4075 * Need more pages?
4077 if (va + size > pva) {
4078 uintptr_t npva;
4080 alloc_size = P2ROUNDUP(size - (pva - va), alloc_pgsz);
4082 * Check for overlapping segments.
4084 if (limit && limit <= *spp + alloc_size) {
4085 return ((caddr_t)0);
4088 npva = (uintptr_t)BOP_ALLOC(ops, (caddr_t)pva,
4089 alloc_size, alloc_align);
4091 if (npva == (uintptr_t)NULL) {
4092 _kobj_printf(ops, "BOP_ALLOC failed, 0x%lx bytes",
4093 alloc_size);
4094 _kobj_printf(ops, " aligned %lx", alloc_align);
4095 _kobj_printf(ops, " at 0x%lx\n", pva);
4096 return (NULL);
4099 *spp = (caddr_t)(va + size);
4101 return ((caddr_t)va);
4105 * Calculate the number of output hash buckets.
4106 * We use the next prime larger than n / 4,
4107 * so the average hash chain is about 4 entries.
4108 * More buckets would just be a waste of memory.
4110 uint_t
4111 kobj_gethashsize(uint_t n)
4113 int f;
4114 int hsize = MAX(n / 4, 2);
4116 for (f = 2; f * f <= hsize; f++)
4117 if (hsize % f == 0)
4118 hsize += f = 1;
4120 return (hsize);
4124 * Get the file size.
4126 * Before root is mounted, files are compressed in the boot_archive ramdisk
4127 * (in the memory). kobj_fstat would return the compressed file size.
4128 * In order to get the uncompressed file size, read the file to the end and
4129 * count its size.
4132 kobj_get_filesize(struct _buf *file, uint64_t *size)
4134 int err = 0;
4135 ssize_t resid;
4136 uint32_t buf;
4138 if (_modrootloaded) {
4139 struct bootstat bst;
4141 if (kobj_fstat(file->_fd, &bst) != 0)
4142 return (EIO);
4143 *size = bst.st_size;
4145 if (file->_iscmp == CH_MAGIC_GZIP) {
4147 * Read the last 4 bytes of the compressed (gzip)
4148 * image to get the size of its uncompressed
4149 * version.
4151 if ((err = vn_rdwr(UIO_READ, (struct vnode *)file->_fd,
4152 (char *)(&buf), 4, (offset_t)(*size - 4),
4153 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
4154 != 0) {
4155 _kobj_printf(ops, "kobj_get_filesize: "
4156 "vn_rdwr() failed with error 0x%x\n", err);
4157 return (-1);
4160 *size = (uint64_t)buf;
4162 } else {
4164 #if defined(_OBP)
4165 struct bootstat bsb;
4167 if (file->_iscmp) {
4168 struct comphdr *hdr = kobj_comphdr(file);
4170 *size = hdr->ch_fsize;
4171 } else if (kobj_boot_fstat(file->_fd, &bsb) != 0)
4172 return (EIO);
4173 else
4174 *size = bsb.st_size;
4175 #else
4176 char *buf;
4177 int count;
4178 uint64_t offset = 0;
4180 buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
4181 do {
4182 count = kobj_read_file(file, buf, MAXBSIZE, offset);
4183 if (count < 0) {
4184 kmem_free(buf, MAXBSIZE);
4185 return (EIO);
4187 offset += count;
4188 } while (count == MAXBSIZE);
4189 kmem_free(buf, MAXBSIZE);
4191 *size = offset;
4192 #endif
4195 return (0);
4198 static char *
4199 basename(char *s)
4201 char *p, *q;
4203 q = NULL;
4204 p = s;
4205 do {
4206 if (*p == '/')
4207 q = p;
4208 } while (*p++);
4209 return (q ? q + 1 : s);
4212 void
4213 kobj_stat_get(kobj_stat_t *kp)
4215 *kp = kobj_stat;
4219 kobj_getpagesize()
4221 return (lg_pagesize);
4224 void
4225 kobj_textwin_alloc(struct module *mp)
4227 ASSERT(MUTEX_HELD(&mod_lock));
4229 if (mp->textwin != NULL)
4230 return;
4233 * If the text is not contained in the heap, then it is not contained
4234 * by a writable mapping. (Specifically, it's on the nucleus page.)
4235 * We allocate a read/write mapping for this module's text to allow
4236 * the text to be patched without calling hot_patch_kernel_text()
4237 * (which is quite slow).
4239 if (!vmem_contains(heaptext_arena, mp->text, mp->text_size)) {
4240 uintptr_t text = (uintptr_t)mp->text;
4241 uintptr_t size = (uintptr_t)mp->text_size;
4242 uintptr_t i;
4243 caddr_t va;
4244 size_t sz = ((text + size + PAGESIZE - 1) & PAGEMASK) -
4245 (text & PAGEMASK);
4247 va = mp->textwin_base = vmem_alloc(heap_arena, sz, VM_SLEEP);
4249 for (i = text & PAGEMASK; i < text + size; i += PAGESIZE) {
4250 hat_devload(kas.a_hat, va, PAGESIZE,
4251 hat_getpfnum(kas.a_hat, (caddr_t)i),
4252 PROT_READ | PROT_WRITE,
4253 HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
4254 va += PAGESIZE;
4257 mp->textwin = mp->textwin_base + (text & PAGEOFFSET);
4258 } else {
4259 mp->textwin = mp->text;
4263 void
4264 kobj_textwin_free(struct module *mp)
4266 uintptr_t text = (uintptr_t)mp->text;
4267 uintptr_t tsize = (uintptr_t)mp->text_size;
4268 size_t size = (((text + tsize + PAGESIZE - 1) & PAGEMASK) -
4269 (text & PAGEMASK));
4271 mp->textwin = NULL;
4273 if (mp->textwin_base == NULL)
4274 return;
4276 hat_unload(kas.a_hat, mp->textwin_base, size, HAT_UNLOAD_UNLOCK);
4277 vmem_free(heap_arena, mp->textwin_base, size);
4278 mp->textwin_base = NULL;
4281 static char *
4282 find_libmacro(char *name)
4284 int lmi;
4286 for (lmi = 0; lmi < NLIBMACROS; lmi++) {
4287 if (strcmp(name, libmacros[lmi].lmi_macroname) == 0)
4288 return (libmacros[lmi].lmi_list);
4290 return (NULL);
4294 * Check for $MACRO in tail (string to expand) and expand it in path at pathend
4295 * returns path if successful, else NULL
4296 * Support multiple $MACROs expansion and the first valid path will be returned
4297 * Caller's responsibility to provide enough space in path to expand
4299 char *
4300 expand_libmacro(char *tail, char *path, char *pathend)
4302 char c, *p, *p1, *p2, *path2, *endp;
4303 int diff, lmi, macrolen, valid_macro, more_macro;
4304 struct _buf *file;
4307 * check for $MACROS between nulls or slashes
4309 p = strchr(tail, '$');
4310 if (p == NULL)
4311 return (NULL);
4312 for (lmi = 0; lmi < NLIBMACROS; lmi++) {
4313 macrolen = libmacros[lmi].lmi_macrolen;
4314 if (strncmp(p + 1, libmacros[lmi].lmi_macroname, macrolen) == 0)
4315 break;
4318 valid_macro = 0;
4319 if (lmi < NLIBMACROS) {
4321 * The following checks are used to restrict expansion of
4322 * macros to those that form a full directory/file name
4323 * and to keep the behavior same as before. If this
4324 * restriction is removed or no longer valid in the future,
4325 * the checks below can be deleted.
4327 if ((p == tail) || (*(p - 1) == '/')) {
4328 c = *(p + macrolen + 1);
4329 if (c == '/' || c == '\0')
4330 valid_macro = 1;
4334 if (!valid_macro) {
4335 p2 = strchr(p, '/');
4337 * if no more macro to expand, then just copy whatever left
4338 * and check whether it exists
4340 if (p2 == NULL || strchr(p2, '$') == NULL) {
4341 (void) strcpy(pathend, tail);
4342 if ((file = kobj_open_path(path, 1, 1)) !=
4343 (struct _buf *)-1) {
4344 kobj_close_file(file);
4345 return (path);
4346 } else
4347 return (NULL);
4348 } else {
4350 * copy all chars before '/' and call expand_libmacro()
4351 * again
4353 diff = p2 - tail;
4354 bcopy(tail, pathend, diff);
4355 pathend += diff;
4356 *(pathend) = '\0';
4357 return (expand_libmacro(p2, path, pathend));
4361 more_macro = 0;
4362 if (c != '\0') {
4363 endp = p + macrolen + 1;
4364 if (strchr(endp, '$') != NULL)
4365 more_macro = 1;
4366 } else
4367 endp = NULL;
4370 * copy lmi_list and split it into components.
4371 * then put the part of tail before $MACRO into path
4372 * at pathend
4374 diff = p - tail;
4375 if (diff > 0)
4376 bcopy(tail, pathend, diff);
4377 path2 = pathend + diff;
4378 p1 = libmacros[lmi].lmi_list;
4379 while (p1 && (*p1 != '\0')) {
4380 p2 = strchr(p1, ':');
4381 if (p2) {
4382 diff = p2 - p1;
4383 bcopy(p1, path2, diff);
4384 *(path2 + diff) = '\0';
4385 } else {
4386 diff = strlen(p1);
4387 bcopy(p1, path2, diff + 1);
4389 /* copy endp only if there isn't any more macro to expand */
4390 if (!more_macro && (endp != NULL))
4391 (void) strcat(path2, endp);
4392 file = kobj_open_path(path, 1, 1);
4393 if (file != (struct _buf *)-1) {
4394 kobj_close_file(file);
4396 * if more macros to expand then call expand_libmacro(),
4397 * else return path which has the whole path
4399 if (!more_macro || (expand_libmacro(endp, path,
4400 path2 + diff) != NULL)) {
4401 return (path);
4404 if (p2)
4405 p1 = ++p2;
4406 else
4407 return (NULL);
4409 return (NULL);
4412 static void
4413 tnf_add_notifyunload(kobj_notify_f *fp)
4415 kobj_notify_list_t *entry;
4417 entry = kobj_alloc(sizeof (kobj_notify_list_t), KM_WAIT);
4418 entry->kn_type = KOBJ_NOTIFY_MODUNLOADING;
4419 entry->kn_func = fp;
4420 (void) kobj_notify_add(entry);
4423 /* ARGSUSED */
4424 static void
4425 tnf_unsplice_probes(uint_t what, struct modctl *mod)
4427 tnf_probe_control_t **p;
4428 tnf_tag_data_t **q;
4429 struct module *mp = mod->mod_mp;
4431 if (!(mp->flags & KOBJ_TNF_PROBE))
4432 return;
4434 for (p = &__tnf_probe_list_head; *p; )
4435 if (kobj_addrcheck(mp, (char *)*p) == 0)
4436 *p = (*p)->next;
4437 else
4438 p = &(*p)->next;
4440 for (q = &__tnf_tag_list_head; *q; )
4441 if (kobj_addrcheck(mp, (char *)*q) == 0)
4442 *q = (tnf_tag_data_t *)(*q)->tag_version;
4443 else
4444 q = (tnf_tag_data_t **)&(*q)->tag_version;
4446 tnf_changed_probe_list = 1;
4450 tnf_splice_probes(int boot_load, tnf_probe_control_t *plist,
4451 tnf_tag_data_t *tlist)
4453 int result = 0;
4454 static int add_notify = 1;
4456 if (plist) {
4457 tnf_probe_control_t *pl;
4459 for (pl = plist; pl->next; )
4460 pl = pl->next;
4462 if (!boot_load)
4463 mutex_enter(&mod_lock);
4464 tnf_changed_probe_list = 1;
4465 pl->next = __tnf_probe_list_head;
4466 __tnf_probe_list_head = plist;
4467 if (!boot_load)
4468 mutex_exit(&mod_lock);
4469 result = 1;
4472 if (tlist) {
4473 tnf_tag_data_t *tl;
4475 for (tl = tlist; tl->tag_version; )
4476 tl = (tnf_tag_data_t *)tl->tag_version;
4478 if (!boot_load)
4479 mutex_enter(&mod_lock);
4480 tl->tag_version = (tnf_tag_version_t *)__tnf_tag_list_head;
4481 __tnf_tag_list_head = tlist;
4482 if (!boot_load)
4483 mutex_exit(&mod_lock);
4484 result = 1;
4486 if (!boot_load && result && add_notify) {
4487 tnf_add_notifyunload(tnf_unsplice_probes);
4488 add_notify = 0;
4490 return (result);
4493 char *kobj_file_buf;
4494 int kobj_file_bufsize;
4497 * This code is for the purpose of manually recording which files
4498 * needs to go into the boot archive on any given system.
4500 * To enable the code, set kobj_file_bufsize in /etc/system
4501 * and reboot the system, then use mdb to look at kobj_file_buf.
4503 static void
4504 kobj_record_file(char *filename)
4506 static char *buf;
4507 static int size = 0;
4508 int n;
4510 if (kobj_file_bufsize == 0) /* don't bother */
4511 return;
4513 if (kobj_file_buf == NULL) { /* allocate buffer */
4514 size = kobj_file_bufsize;
4515 buf = kobj_file_buf = kobj_alloc(size, KM_WAIT|KM_TMP);
4518 n = snprintf(buf, size, "%s\n", filename);
4519 if (n > size)
4520 n = size;
4521 size -= n;
4522 buf += n;
4525 static int
4526 kobj_boot_fstat(int fd, struct bootstat *stp)
4528 #if defined(_OBP)
4529 if (!standalone && _ioquiesced)
4530 return (-1);
4531 return (BOP_FSTAT(ops, fd, stp));
4532 #else
4533 return (BRD_FSTAT(bfs_ops, fd, stp));
4534 #endif
4537 static int
4538 kobj_boot_open(char *filename, int flags)
4540 #if defined(_OBP)
4543 * If io via bootops is quiesced, it means boot is no longer
4544 * available to us. We make it look as if we can't open the
4545 * named file - which is reasonably accurate.
4547 if (!standalone && _ioquiesced)
4548 return (-1);
4550 kobj_record_file(filename);
4551 return (BOP_OPEN(filename, flags));
4552 #else /* x86 */
4553 kobj_record_file(filename);
4554 return (BRD_OPEN(bfs_ops, filename, flags));
4555 #endif
4558 static int
4559 kobj_boot_close(int fd)
4561 #if defined(_OBP)
4562 if (!standalone && _ioquiesced)
4563 return (-1);
4565 return (BOP_CLOSE(fd));
4566 #else /* x86 */
4567 return (BRD_CLOSE(bfs_ops, fd));
4568 #endif
4571 /*ARGSUSED*/
4572 static int
4573 kobj_boot_seek(int fd, off_t hi, off_t lo)
4575 #if defined(_OBP)
4576 return (BOP_SEEK(fd, lo) == -1 ? -1 : 0);
4577 #else
4578 return (BRD_SEEK(bfs_ops, fd, lo, SEEK_SET));
4579 #endif
4582 static int
4583 kobj_boot_read(int fd, caddr_t buf, size_t size)
4585 #if defined(_OBP)
4586 return (BOP_READ(fd, buf, size));
4587 #else
4588 return (BRD_READ(bfs_ops, fd, buf, size));
4589 #endif
4592 static int
4593 kobj_boot_compinfo(int fd, struct compinfo *cb)
4595 return (boot_compinfo(fd, cb));
4599 * Check if the file is compressed (for now we handle only gzip).
4600 * It returns CH_MAGIC_GZIP if the file is compressed and 0 otherwise.
4602 static int
4603 kobj_is_compressed(intptr_t fd)
4605 struct vnode *vp = (struct vnode *)fd;
4606 ssize_t resid;
4607 uint16_t magic_buf;
4608 int err = 0;
4610 if ((err = vn_rdwr(UIO_READ, vp, (caddr_t)((intptr_t)&magic_buf),
4611 sizeof (magic_buf), (offset_t)(0),
4612 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) != 0) {
4614 _kobj_printf(ops, "kobj_is_compressed: vn_rdwr() failed, "
4615 "error code 0x%x\n", err);
4616 return (0);
4619 if (magic_buf == CH_MAGIC_GZIP)
4620 return (CH_MAGIC_GZIP);
4622 return (0);