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]
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>
40 #include <sys/reboot.h>
41 #include <sys/bootconf.h>
42 #include <sys/debug.h>
45 #include <sys/vnode.h>
49 #include <vm/seg_kp.h>
50 #include <vm/seg_kmem.h>
52 #include <sys/elf_notes.h>
53 #include <sys/vmsystm.h>
55 #include <sys/atomic.h>
60 #include <sys/ksyms.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>
72 #include <krtld/reloc.h>
73 #include <krtld/kobj_kdi.h>
75 #include <sys/crypto/elfsign.h>
78 #include <sys/bootvfs.h>
82 * do_symbols() error codes
84 #define DOSYM_UNDEF -1 /* undefined symbol */
85 #define DOSYM_UNSAFE -2 /* MT-unsafe driver symbol */
88 static void synthetic_bootaux(char *, val_t
*);
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
[];
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
163 #define KOBJ_MARK(s) if (kobj_debug & D_DEBUG) \
164 (_kobj_printf(ops, "%d", __LINE__), _kobj_printf(ops, ": %s\n", s))
166 #define KOBJ_MARK(s) /* discard */
169 #define MODPATH_PROPNAME "module-path"
172 static char slash_moddir_suffix_slash
[] = MODDIR_SUFFIX
"/";
174 #define slash_moddir_suffix_slash ""
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
{
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 */
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
235 static char *suppress_sym_list
[] =
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 */
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.
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
)
322 return (ip
== NULL
? 0 : *ip
);
326 get_weakish_pointer(void **ptrp
)
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
351 * Load, bind and relocate all modules that
352 * form the primary kernel. At this point, our
353 * externals have not been relocated.
359 struct bootops
*bootvec
,
365 char filename
[MAXPATHLEN
];
368 * Save these to pass on to
369 * the booted standalone.
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
391 _kobj_printf(ops
, "\nWARNING: Standalone debuggers such as "
392 "kadb are no longer supported\n\n");
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
404 if (BOP_MOUNTROOT() != BOOT_SVC_OK
) {
405 _kobj_printf(ops
, "can't mount boot fs\n");
410 /* on x86, we always boot with a ramdisk */
411 (void) kobj_boot_mountroot();
414 * Now that the ramdisk is mounted, finish boot property
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
426 * We do this early as DTrace static probes both call undefined
427 * references. We have to process those relocations before calling
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
);
439 * Save the interesting attribute-values
440 * (scanned by kobj_boot).
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
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)
470 if (bind_primary(bootaux
, KOBJ_LM_PRIMARY
) == -1)
473 entry
= bootaux
[BA_ENTRY
].ba_val
;
480 if (boothowto
& RB_VERBOSE
)
481 kobj_lm_dump(KOBJ_LM_PRIMARY
);
485 if (boothowto
& RB_KMDB
) {
486 if (load_kmdb(bootaux
) < 0)
498 kobj_sync_instruction_memory(s_text
, e_text
- s_text
);
501 if (kobj_debug
& D_DEBUG
)
503 "krtld: transferring control to: 0x%p\n", entry
);
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
));
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
);
522 if (kobj_debug
& D_DEBUG
)
524 "krtld: really transferring control to: 0x%p\n", entry
);
527 /* restore printf/bcopy/bzero vectors before returning */
528 kobj_restore_vectors();
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
);
538 * krtld was directly called from startup
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"
549 _kobj_printf(ops
, " %s\n", filename
);
550 _kobj_printf(ops
, "in the boot archive. Please verify that this"
552 _kobj_printf(ops
, "matches what is found in the boot archive.\n");
553 bop_panic("Unable to boot");
558 * Synthesize additional metadata that describes the executable as
559 * krtld's caller didn't do it.
562 synthetic_bootaux(char *filename
, val_t
*bootaux
)
572 KOBJ_MARK("synthetic_bootaux()");
574 file
= kobj_open_file(filename
);
575 if (file
== (struct _buf
*)-1) {
576 _kobj_printf(ops
, "krtld: failed to open '%s'\n", filename
);
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",
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",
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
;
615 KOBJ_MARK("synthetic_bootaux() done");
620 * Set up any global information derived
621 * from attribute/values in the boot or
625 attr_val(val_t
*bootaux
)
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
) {
646 * Bounds of the various segments.
648 if (!(phdr
->p_flags
& PF_X
)) {
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
;
659 ASSERT(phdr
->p_vaddr
== 0);
662 if (phdr
->p_flags
& PF_W
) {
663 _data
= (caddr_t
)phdr
->p_vaddr
;
664 _edata
= _data
+ phdr
->p_memsz
;
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
) +
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
)
695 int i
, lsize
, osize
, nsize
, allocsize
;
697 char path
[MAXPATHLEN
];
700 if (kobj_debug
& D_DEBUG
)
701 _kobj_printf(ops
, "module path '%s'\n", kobj_module_path
);
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
);
712 * We don't have the following information
713 * since this module is an executable and not
716 mp
->symtbl_section
= 0;
721 * Since this module is the only exception,
722 * we cons up some section headers.
725 mp
->symhdr
= kobj_zalloc(sizeof (Shdr
), KM_WAIT
);
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
) {
739 mp
->symspace
= mp
->symtbl
= (char *)dyn
->d_un
.d_ptr
;
740 mp
->symhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
743 mp
->nsyms
= *((uint_t
*)dyn
->d_un
.d_ptr
+ 1);
744 mp
->hashsize
= *(uint_t
*)dyn
->d_un
.d_ptr
;
747 mp
->strings
= (char *)dyn
->d_un
.d_ptr
;
748 mp
->strhdr
->sh_addr
= dyn
->d_un
.d_ptr
;
751 mp
->strhdr
->sh_size
= dyn
->d_un
.d_val
;
754 mp
->symhdr
->sh_entsize
= dyn
->d_un
.d_val
;
760 * Collapse any DT_NEEDED entries into one string.
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
) {
773 libname
= mp
->strings
+ dyn
->d_un
.d_val
;
774 if (strchr(libname
, '$') != NULL
) {
775 if ((_lib
= expand_libmacro(libname
,
776 path
, path
)) != NULL
)
779 _kobj_printf(ops
, "krtld: "
780 "load_exec: fail to "
781 "expand %s\n", libname
);
783 lsize
= strlen(libname
);
785 if (nsize
+ 1 > allocsize
) {
786 KOBJ_MARK("grow depends_on");
787 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
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 */
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
;
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.
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
);
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
;
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
)
855 sym_insert(mp
, mp
->strings
+ sp
->st_name
, i
);
858 KOBJ_MARK("load_exec done");
863 * Set up the linker module (if it's compiled in, LDNAME is NULL)
866 load_linker(val_t
*bootaux
)
868 struct module
*kmp
= (struct module
*)kobj_modules
->mod_mp
;
875 char *dlname
= (char *)bootaux
[BA_LDNAME
].ba_ptr
;
878 * On some architectures, krtld is compiled into the kernel.
883 cp
= add_primary(dlname
, KOBJ_LM_PRIMARY
);
885 mp
= kobj_zalloc(sizeof (struct module
), KM_WAIT
);
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
;
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
;
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
)
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
);
986 mutex_enter(&kobj_lock
);
989 (*knl
)->kn_prev
= knp
;
994 mutex_exit(&kobj_lock
);
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
);
1007 if (tknp
= knp
->kn_next
)
1008 tknp
->kn_prev
= knp
->kn_prev
;
1011 if (tknp
= knp
->kn_prev
)
1012 tknp
->kn_next
= knp
->kn_next
;
1014 *knl
= knp
->kn_next
;
1016 mutex_exit(&kobj_lock
);
1022 * Notify all interested callbacks of a specified change in module state.
1025 kobj_notify(int type
, struct modctl
*modp
)
1027 kobj_notify_list_t
*knp
;
1029 if (modp
->mod_loadflags
& MOD_NONOTIFY
|| standalone
)
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.
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
);
1060 (void) strcat(path
, " ");
1061 return (strcat(path
, MOD_DEFPATH
));
1064 static struct modctl
*
1065 add_primary(const char *filename
, int lmid
)
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 */
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
) {
1096 cp
->mod_prev
= cp
->mod_next
= cp
;
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
);
1110 bind_primary(val_t
*bootaux
, int lmid
)
1112 struct modctl_list
*linkmap
= kobj_lm_lookup(lmid
);
1113 struct modctl_list
*lp
;
1117 * Do common symbols.
1119 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1123 * Don't do common section relocations for modules that
1126 if (mp
->flags
& (KOBJ_EXEC
|KOBJ_INTERP
))
1129 if (do_common(mp
) < 0)
1136 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1139 if (do_symbols(mp
, 0) < 0)
1146 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
1149 if (mp
->flags
& KOBJ_EXEC
) {
1151 Word relasz
= 0, relaent
= 0;
1155 for (dyn
= (Dyn
*)bootaux
[BA_DYNAMIC
].ba_ptr
;
1156 dyn
->d_tag
!= DT_NULL
; dyn
++) {
1157 switch (dyn
->d_tag
) {
1160 relasz
= dyn
->d_un
.d_val
;
1164 relaent
= dyn
->d_un
.d_val
;
1168 rela
= (char *)dyn
->d_un
.d_ptr
;
1172 rela
= (char *)dyn
->d_un
.d_ptr
;
1177 relaent
== 0 || rela
== NULL
) {
1178 _kobj_printf(ops
, "krtld: bind_primary(): "
1179 "no relocation information found for "
1180 "module %s\n", mp
->filename
);
1184 if (kobj_debug
& D_RELOCATIONS
)
1185 _kobj_printf(ops
, "krtld: relocating: file=%s "
1186 "KOBJ_EXEC\n", mp
->filename
);
1188 if (do_relocate(mp
, rela
, shtype
, relasz
/relaent
,
1189 relaent
, (Addr
)mp
->text
) < 0)
1192 if (do_relocations(mp
) < 0)
1196 kobj_sync_instruction_memory(mp
->text
, mp
->text_size
);
1199 for (lp
= linkmap
; lp
; lp
= lp
->modl_next
) {
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
1208 if (mp
->flags
& KOBJ_EXEC
) {
1212 file
= kobj_open_file(mp
->filename
);
1213 if (file
== (struct _buf
*)-1)
1215 if (kobj_read_file(file
, (char *)&mp
->hdr
,
1216 sizeof (mp
->hdr
), 0) < 0)
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)
1223 if (get_syms(mp
, file
) < 0)
1225 if (get_ctf(mp
, file
) < 0)
1227 kobj_close_file(file
);
1228 mp
->flags
|= KOBJ_RELOCATED
;
1230 } else if (mp
->flags
& KOBJ_INTERP
) {
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)
1241 if (get_ctf(mp
, file
) < 0)
1243 kobj_close_file(file
);
1244 mp
->flags
|= KOBJ_RELOCATED
;
1251 static struct modctl
*
1252 mod_already_loaded(char *modname
)
1254 struct modctl
*mctl
= kobj_modules
;
1257 if (strcmp(modname
, mctl
->mod_filename
) == 0)
1259 mctl
= mctl
->mod_next
;
1261 } while (mctl
!= kobj_modules
);
1267 * Load all the primary dependent modules.
1270 load_primary(struct module
*mp
, int lmid
)
1275 char modname
[MODMAXNAMELEN
];
1277 if ((p
= mp
->depends_on
) == NULL
)
1280 /* CONSTANTCONDITION */
1285 while (*p
&& (*p
== ' ' || *p
== '\t'))
1291 while (*p
&& *p
!= ' ' && *p
!= '\t')
1299 * Check for dup dependencies.
1301 if (strcmp(modname
, "dtracestubs") == 0 ||
1302 mod_already_loaded(modname
) != NULL
)
1305 cp
= add_primary(modname
, lmid
);
1310 (void) kobj_load_module(cp
, 1);
1313 if ((dmp
= cp
->mod_mp
) == NULL
) {
1315 cp
->mod_installed
= 0;
1316 cp
->mod_loadcnt
= 0;
1320 add_dependent(mp
, dmp
);
1321 dmp
->flags
|= KOBJ_PRIM
;
1326 if (load_primary(dmp
, lmid
) == -1) {
1328 cp
->mod_installed
= 0;
1329 cp
->mod_loadcnt
= 0;
1337 console_is_usb_serial(void)
1342 if ((len
= BOP_GETPROPLEN(ops
, "console")) == -1)
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
);
1354 load_kmdb(val_t
*bootaux
)
1356 struct modctl
*mctl
;
1360 if (console_is_usb_serial()) {
1361 _kobj_printf(ops
, "kmdb not loaded "
1362 "(unsupported on usb serial console)\n");
1366 _kobj_printf(ops
, "Loading kmdb...\n");
1368 if ((mctl
= add_primary("misc/kmdbmod", KOBJ_LM_DEBUGGER
)) == NULL
)
1372 (void) kobj_load_module(mctl
, 1);
1375 if ((mp
= mctl
->mod_mp
) == NULL
)
1378 mp
->flags
|= KOBJ_PRIM
;
1380 if (load_primary(mp
, KOBJ_LM_DEBUGGER
) < 0)
1383 if (boothowto
& RB_VERBOSE
)
1384 kobj_lm_dump(KOBJ_LM_DEBUGGER
);
1386 if (bind_primary(bootaux
, KOBJ_LM_DEBUGGER
) < 0)
1389 if ((sym
= lookup_one(mctl
->mod_mp
, "kctl_boot_activate")) == NULL
)
1393 if (kobj_debug
& D_DEBUG
) {
1394 _kobj_printf(ops
, "calling kctl_boot_activate() @ 0x%lx\n",
1396 _kobj_printf(ops
, "\tops 0x%p\n", ops
);
1397 _kobj_printf(ops
, "\tromp 0x%p\n", romp
);
1401 if (((kctl_boot_activate_f
*)sym
->st_value
)(ops
, romp
, 0,
1402 (const char **)kobj_kmdb_argv
) < 0)
1409 * Return a string listing module dependencies.
1412 depends_on(struct module
*mp
)
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
)
1424 q
= (char *)sp
->st_value
;
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.
1435 "Warning: %s uses deprecated _depends_on interface.\n",
1437 _kobj_printf(ops
, "Please notify module developer or vendor.\n");
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",
1450 depstr
= (char *)kobj_alloc(strlen(q
) + 1, KM_WAIT
);
1451 (void) strcpy(depstr
, q
);
1457 kobj_getmodinfo(void *xmp
, struct modinfo
*modinfo
)
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.
1475 kobj_export_ksyms(struct module
*mp
)
1477 Sym
*esp
= (Sym
*)(mp
->symtbl
+ mp
->symhdr
->sh_size
);
1483 size_t symsize
= mp
->symhdr
->sh_entsize
;
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)
1499 if (sym_lookup(omp
, osp
) == NULL
)
1501 name
= omp
->strings
+ osp
->st_name
;
1502 namelen
= strlen(name
);
1503 if (ELF_ST_BIND(osp
->st_info
) == STB_LOCAL
)
1506 strsize
+= namelen
+ 1;
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
);
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)
1570 if (sym_lookup(omp
, osp
) == NULL
)
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
);
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
)) {
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
) {
1597 if (shp
->sh_addr
!= (uintptr_t)NULL
) {
1598 kobj_free((void *)shp
->sh_addr
,
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
));
1615 kobj_export_ctf(struct module
*mp
)
1617 char *data
= mp
->ctfdata
;
1618 size_t size
= mp
->ctfsize
;
1621 if (_moddebug
& MODDEBUG_NOCTF
) {
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
);
1636 kobj_export_module(struct module
*mp
)
1638 kobj_export_ksyms(mp
);
1639 kobj_export_ctf(mp
);
1641 mp
->flags
|= KOBJ_EXPORTED
;
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
;
1653 for (dynp
= (Dyn
*)dyndata
; dynp
&& dynp
->d_tag
!= DT_NULL
; dynp
++) {
1654 switch (dynp
->d_tag
) {
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
) {
1667 path
= kobj_alloc(MAXPATHLEN
, KM_WAIT
);
1668 if ((_lib
= expand_libmacro(libname
, path
,
1672 _kobj_printf(ops
, "krtld: "
1673 "process_dynamic: failed to expand "
1678 lsize
= strlen(libname
);
1680 if (nsize
+ 1 > allocsize
) {
1681 tmp
= kobj_alloc(allocsize
+ MAXPATHLEN
,
1683 if (depstr
!= NULL
) {
1684 bcopy(depstr
, tmp
, osize
);
1685 kobj_free(depstr
, allocsize
);
1688 allocsize
+= MAXPATHLEN
;
1690 bcopy(libname
, depstr
+ osize
, lsize
);
1691 *(depstr
+ nsize
) = ' '; /* separator */
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
;
1707 * finish up the depends string (if any)
1709 if (depstr
!= NULL
) {
1710 *(depstr
+ nsize
- 1) = '\0'; /* overwrite separator w/term */
1712 kobj_free(path
, MAXPATHLEN
);
1714 tmp
= kobj_alloc(nsize
, KM_WAIT
);
1715 bcopy(depstr
, tmp
, nsize
);
1716 kobj_free(depstr
, allocsize
);
1719 mp
->depends_on
= depstr
;
1726 do_dynamic(struct module
*mp
, struct _buf
*file
)
1728 Shdr
*dshp
, *dstrp
, *shp
;
1729 char *dyndata
, *dstrdata
;
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
) {
1739 _kobj_printf(ops
, "krtld: get_dynamic: %s, ",
1742 "multiple dynamic sections\n");
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
);
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",
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
);
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
);
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
);
1802 kobj_set_ctf(struct module
*mp
, caddr_t data
, size_t size
)
1805 if (mp
->ctfdata
!= NULL
) {
1806 if (vmem_contains(ctf_arena
, mp
->ctfdata
,
1808 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
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
1829 kobj_load_module(struct modctl
*modp
, int use_path
)
1831 char *filename
= modp
->mod_filename
;
1832 char *modname
= modp
->mod_modname
;
1836 struct module
*mp
= NULL
;
1837 #ifdef MODDIR_SUFFIX
1838 int no_suffixdir_drv
= 0;
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
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);
1857 if (file
== (struct _buf
*)-1) {
1858 kobj_free(mp
, sizeof (*mp
));
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;
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",
1878 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1879 kobj_free(mp
, sizeof (*mp
));
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",
1887 kobj_free(mp
->filename
, strlen(file
->_name
) + 1);
1888 kobj_free(mp
, sizeof (*mp
));
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",
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
);
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
));
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
));
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
);
1953 if (do_dynamic(mp
, file
) < 0) {
1954 _kobj_printf(ops
, "%s error reading dynamic section\n",
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",
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
);
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.
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) {
2014 _kobj_printf(ops
, "WARNING: mod_load: "
2015 "MT-unsafe module '%s' rejected\n",
2019 _kobj_printf(ops
, "WARNING: mod_load: "
2020 "cannot load module '%s'\n",
2023 _kobj_printf(ops
, "WARNING: %s: ",
2026 "unable to resolve dependency, "
2027 "module '%s' not found\n",
2033 kobj_free(dependent_modname
, MODMAXNAMELEN
);
2037 /* process relocation tables */
2038 if (do_relocations(mp
) < 0) {
2039 _kobj_printf(ops
, "%s error doing relocations\n",
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
);
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
)
2076 if (kobj_load_module(modp
, 0) != 0)
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
) {
2086 if (kobj_debug
& D_DEBUG
) {
2087 _kobj_printf(ops
, "krtld: failed to resolve deps "
2088 "for primary %s\n", modp
->mod_modname
);
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) {
2103 if (kobj_debug
& D_DEBUG
) {
2104 _kobj_printf(ops
, "krtld: failed to relocate "
2105 "primary %s\n", modp
->mod_modname
);
2115 module_assign(struct modctl
*cp
, struct module
*mp
)
2121 mutex_enter(&mod_lock
);
2124 mutex_exit(&mod_lock
);
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
);
2153 free_module_data(struct module
*mp
)
2155 struct module_list
*lp
, *tmp
;
2156 int ksyms_exported
= 0;
2162 kobj_free((char *)tmp
, sizeof (*tmp
));
2165 rw_enter(&ksyms_lock
, RW_WRITER
);
2167 if (vmem_contains(ksyms_arena
, mp
->symspace
, mp
->symsize
)) {
2168 vmem_free(ksyms_arena
, mp
->symspace
, mp
->symsize
);
2171 if (mp
->flags
& KOBJ_NOKSYMS
)
2173 kobj_free(mp
->symspace
, mp
->symsize
);
2176 rw_exit(&ksyms_lock
);
2179 if (vmem_contains(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
))
2180 vmem_free(ctf_arena
, mp
->ctfdata
, mp
->ctfsize
);
2182 kobj_free(mp
->ctfdata
, mp
->ctfsize
);
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
)) {
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
) {
2201 if (shp
->sh_addr
!= 0)
2202 kobj_free((void *)shp
->sh_addr
,
2208 if (!(mp
->flags
& KOBJ_PRIM
)) {
2209 kobj_free(mp
->shdrs
,
2210 mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2215 vmem_free(data_arena
, (void *)mp
->bss
, mp
->bss_size
);
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
));
2235 kobj_texthole_free(mp
->sdt_tab
, mp
->sdt_size
);
2237 vmem_free(text_arena
, mp
->text
, mp
->text_size
);
2239 vmem_free(data_arena
, mp
->data
, mp
->data_size
);
2241 kobj_free(mp
->depends_on
, strlen(mp
->depends_on
)+1);
2243 kobj_free(mp
->filename
, strlen(mp
->filename
)+1);
2245 kobj_free((char *)mp
, sizeof (*mp
));
2249 get_progbits(struct module
*mp
, struct _buf
*file
)
2251 struct proginfo
*tp
, *dp
, *sdp
;
2253 reloc_dest_t dest
= NULL
;
2255 uintptr_t text
= 0, data
, textptr
;
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)
2269 mp
->text_size
= tp
->size
;
2270 mp
->data_size
= dp
->size
;
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
,
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
,
2289 mp
->data
= kobj_segbrk(&_edata
, mp
->data_size
, dp
->align
, 0);
2291 if (mp
->text
== NULL
|| mp
->data
== NULL
)
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
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
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
);
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
);
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.
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.
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
))
2372 if ((shp
->sh_flags
& SHF_WRITE
) == 0)
2377 bits_ptr
= ALIGN(bits_ptr
, shp
->sh_addralign
);
2379 if (shp
->sh_type
== SHT_NOBITS
) {
2383 bzero((caddr_t
)bits_ptr
, shp
->sh_size
);
2384 shp
->sh_type
= SHT_PROGBITS
;
2386 if (kobj_read_file(file
, (char *)bits_ptr
,
2387 shp
->sh_size
, shp
->sh_offset
) < 0)
2391 if (shp
->sh_flags
& SHF_WRITE
) {
2392 shp
->sh_addr
= bits_ptr
;
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)
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.
2418 kobj_free(mp
->shdrs
, mp
->hdr
.e_shentsize
* mp
->hdr
.e_shnum
);
2422 (void) kobj_free(tp
, sizeof (struct proginfo
));
2423 (void) kobj_free(dp
, sizeof (struct proginfo
));
2424 (void) kobj_free(sdp
, sizeof (struct proginfo
));
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.
2435 kobj_suppress_warning(char *symname
)
2439 for (i
= 0; suppress_sym_list
[i
] != NULL
; i
++) {
2440 if (strcmp(suppress_sym_list
[i
], symname
) == 0)
2448 get_syms(struct module
*mp
, struct _buf
*file
)
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
) {
2464 mp
->symtbl_section
= shn
;
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, ",
2485 _kobj_printf(ops
, "error reading section %d\n",
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
2500 if (mp
->flags
& KOBJ_EXEC
)
2502 _kobj_printf(ops
, "krtld: get_syms: %s ",
2504 _kobj_printf(ops
, "no SHT_SYMTAB symbol table found\n");
2509 * get the associated string table header
2511 if ((mp
->symhdr
== 0) || (mp
->symhdr
->sh_link
>= mp
->hdr
.e_shnum
))
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)
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 ",
2548 _kobj_printf(ops
, "in symbol %d\n", i
);
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
)
2560 if (sp
->st_name
>= mp
->strhdr
->sh_size
)
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.
2583 ksp
->st_value
< (uintptr_t)stubs_base
||
2584 ksp
->st_value
>= (uintptr_t)stubs_end
) &&
2585 !(mp
->flags
& KOBJ_IGNMULDEF
)) {
2587 "%s symbol ", file
->_name
);
2589 "%s multiply defined\n", symname
);
2594 sym_insert(mp
, symname
, i
);
2601 get_ctf(struct module
*mp
, struct _buf
*file
)
2603 char *shstrtab
, *ctfdata
;
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, ",
2614 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2615 mp
->hdr
.e_shstrndx
);
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, ",
2626 _kobj_printf(ops
, "error reading section %u\n",
2627 mp
->hdr
.e_shstrndx
);
2628 kobj_free(shstrtab
, shstrlen
);
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
);
2648 mp
->ctfdata
= ctfdata
;
2649 mp
->ctfsize
= shp
->sh_size
;
2654 kobj_free(shstrtab
, shstrlen
);
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.
2667 crypto_es_hash(struct module
*mp
, char *hash
, char *shstrtab
)
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)
2681 * The check should ideally be shp->sh_type == SHT_NOBITS.
2682 * However, we can't do that check here as get_progbits()
2685 if (strcmp(shstrtab
+ shp
->sh_name
, ".bss") == 0)
2688 if (kobj_debug
& D_DEBUG
)
2690 "krtld: crypto_es_hash: updating hash with"
2691 " %s data size=%d\n", shstrtab
+ shp
->sh_name
,
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.
2708 get_signature(struct module
*mp
, struct _buf
*file
)
2710 char *shstrtab
, *sigdata
= NULL
;
2715 if (mp
->hdr
.e_shstrndx
>= mp
->hdr
.e_shnum
) {
2716 _kobj_printf(ops
, "krtld: get_signature: %s, ",
2718 _kobj_printf(ops
, "corrupt e_shstrndx %u\n",
2719 mp
->hdr
.e_shstrndx
);
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, ",
2730 _kobj_printf(ops
, "error reading section %u\n",
2731 mp
->hdr
.e_shstrndx
);
2732 kobj_free(shstrtab
, shstrlen
);
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",
2750 kobj_free(sigdata
, sigsize
);
2751 kobj_free(shstrtab
, shstrlen
);
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
);
2764 mp
->sigdata
= sigdata
;
2765 mp
->sigsize
= sigsize
;
2770 if (sigdata
!= NULL
) {
2771 crypto_es_hash(mp
, sigdata
+ shp
->sh_size
, shstrtab
);
2774 kobj_free(shstrtab
, shstrlen
);
2778 add_dependent(struct module
*mp
, struct module
*dep
)
2780 struct module_list
*lp
;
2782 for (lp
= mp
->head
; lp
; lp
= lp
->next
) {
2784 return; /* already on the list */
2788 lp
= kobj_zalloc(sizeof (*lp
), KM_WAIT
);
2793 mp
->tail
->next
= lp
;
2801 do_dependents(struct modctl
*modp
, char *modname
, size_t modnamelen
)
2807 char *err_modname
= NULL
;
2811 if ((p
= mp
->depends_on
) == NULL
)
2818 while (*p
&& (*p
== ' ' || *p
== '\t'))
2826 while (*p
&& *p
!= ' ' && *p
!= '\t') {
2827 if (c
< modnamelen
- 1) {
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);
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);
2853 if ((req
= mod_load_requisite(modp
, modname
)) == NULL
) {
2855 if (_moddebug
& MODDEBUG_LOADMSG
) {
2856 #endif /* KOBJ_DEBUG */
2858 "%s: unable to resolve dependency, ",
2860 _kobj_printf(ops
, "cannot load module '%s'\n",
2864 #endif /* KOBJ_DEBUG */
2865 if (err_modname
== NULL
) {
2867 * This must be the same size as the modname
2870 err_modname
= kobj_zalloc(MODMAXNAMELEN
,
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
);
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
);
2909 do_common(struct module
*mp
)
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)
2921 * increase bss_size by the maximum delta that could be
2922 * computed by the ALIGN below
2924 mp
->bss_size
+= mp
->bss_align
;
2927 mp
->bss
= (uintptr_t)kobj_segbrk(&_edata
, mp
->bss_size
,
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)
2941 do_symbols(struct module
*mp
, Elf64_Addr bss_base
)
2953 * Nothing left to do (optimization).
2955 if (mp
->flags
& KOBJ_RESOLVED
)
2958 assign
= (bss_base
) ? 1 : 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
)
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 ",
2978 _kobj_printf(ops
, "not supported '%s'\n",
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
;
2992 if (sp
->st_shndx
== SHN_UNDEF
) {
2995 if (strncmp(name
, sdt_prefix
, strlen(sdt_prefix
)) == 0)
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",
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)
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
);
3036 sp
->st_shndx
= SHN_ABS
;
3037 sp
->st_value
= bss_ptr
;
3039 bss_ptr
+= sp
->st_size
;
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
;
3054 kobj_hash_name(const char *p
)
3061 hval
= (hval
<< 4) + *p
++;
3062 if ((g
= (hval
& 0xf0000000)) != 0)
3069 /* look for name in all modules */
3071 kobj_getsymvalue(char *name
, int kernelonly
)
3074 struct modctl
*modp
;
3076 uintptr_t value
= 0;
3078 if ((sp
= kobj_lookup_kernel(name
)) != NULL
)
3079 return ((uintptr_t)sp
->st_value
);
3082 return (0); /* didn't find it in the kernel so give up */
3084 mutex_enter(&mod_lock
);
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
;
3093 } while ((modp
= modp
->mod_next
) != &modules
);
3094 mutex_exit(&mod_lock
);
3098 /* look for a symbol near value. */
3100 kobj_getsymname(uintptr_t value
, ulong_t
*offset
)
3103 struct modctl
*modp
;
3105 struct modctl_list
*lp
;
3109 * Loop through the primary kernel modules.
3111 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3114 if ((name
= kobj_searchsym(mp
, value
, offset
)) != NULL
)
3118 mutex_enter(&mod_lock
);
3121 mp
= (struct module
*)modp
->mod_mp
;
3122 if (mp
&& !(mp
->flags
& KOBJ_PRIM
) && modp
->mod_loaded
&&
3123 (name
= kobj_searchsym(mp
, value
, offset
)))
3125 } while ((modp
= modp
->mod_next
) != &modules
);
3126 mutex_exit(&mod_lock
);
3130 /* return address of symbol and size */
3133 kobj_getelfsym(char *name
, void *mp
, int *size
)
3138 sp
= kobj_lookup_kernel(name
);
3140 sp
= lookup_one(mp
, name
);
3145 *size
= (int)sp
->st_size
;
3146 return ((uintptr_t)sp
->st_value
);
3150 kobj_lookup(struct module
*mod
, const char *name
)
3154 sp
= lookup_one(mod
, name
);
3159 return ((uintptr_t)sp
->st_value
);
3163 kobj_searchsym(struct module
*mp
, uintptr_t value
, ulong_t
*offset
)
3172 *offset
= (ulong_t
)-1l; /* assume not found */
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
)
3190 if (ELF_ST_TYPE(sym
->st_info
) != STT_OBJECT
&&
3191 ELF_ST_TYPE(sym
->st_info
) != STT_FUNC
)
3195 curval
= (uintptr_t)sym
->st_value
;
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
)
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
)
3215 /* Prefer the larger of the two functions */
3216 if (sym
->st_size
<= cursym
->st_size
)
3219 } else if (value
- curval
>= *offset
) {
3223 *offset
= (ulong_t
)(value
- curval
);
3229 return (strtabptr
+ cursym
->st_name
);
3233 kobj_lookup_all(struct module
*mp
, char *name
, int include_self
)
3236 struct module_list
*mlp
;
3237 struct modctl_list
*clp
;
3240 if (include_self
&& (sp
= lookup_one(mp
, name
)) != NULL
)
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
)
3250 * Loop through the primary kernel modules.
3252 for (clp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); clp
; clp
= clp
->modl_next
) {
3255 if (mmp
== NULL
|| mp
== mmp
)
3258 if ((sp
= lookup_one(mmp
, name
)) != NULL
&&
3259 ELF_ST_BIND(sp
->st_info
) != STB_LOCAL
)
3266 kobj_lookup_kernel(const char *name
)
3268 struct modctl_list
*lp
;
3273 * Loop through the primary kernel modules.
3275 for (lp
= kobj_lm_lookup(KOBJ_LM_PRIMARY
); lp
; lp
= lp
->modl_next
) {
3281 if ((sp
= lookup_one(mp
, name
)) != NULL
)
3288 lookup_one(struct module
*mp
, const char *name
)
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
)
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.
3313 sym_lookup(struct module
*mp
, Sym
*ksp
)
3315 char *name
= mp
->strings
+ ksp
->st_name
;
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
);
3329 sym_insert(struct module
*mp
, char *name
, symid_t index
)
3334 if (kobj_debug
& D_SYMBOLS
) {
3335 static struct module
*lastmp
= NULL
;
3339 "krtld: symbol entry: file=%s\n",
3342 "krtld:\tsymndx\tvalue\t\t"
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
);
3354 for (ip
= &mp
->buckets
[kobj_hash_name(name
) % mp
->hashsize
]; *ip
;
3355 ip
= &mp
->chains
[*ip
]) {
3362 kobj_boot_mod_lookup(const char *modname
)
3364 struct modctl
*mctl
= kobj_modules
;
3367 if (strcmp(modname
, mctl
->mod_modname
) == 0)
3369 } while ((mctl
= mctl
->mod_next
) != kobj_modules
);
3375 * Determine if the module exists.
3378 kobj_path_exists(char *name
, int use_path
)
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)
3389 kobj_close_file(file
);
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.
3399 kobj_open_path(char *name
, int use_path
, int use_moddir_suffix
)
3408 #if !defined(MODDIR_SUFFIX)
3409 use_moddir_suffix
= B_FALSE
;
3413 pathp
= ""; /* use name as specified */
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
);
3436 if (*pathp
!= '\0' && *pathp
!= '/')
3437 *p
++ = '/'; /* path must start with '/' */
3438 while (*pathp
&& *pathp
!= ':' && *pathp
!= ' ')
3440 if (p
!= fullname
&& p
[-1] != '/')
3442 if (use_moddir_suffix
) {
3443 char *b
= basename(name
);
3446 /* copy everything up to the base name */
3448 while (q
!= b
&& *q
)
3450 s
= slash_moddir_suffix_slash
;
3462 if ((file
= kobj_open_file(fullname
)) != (struct _buf
*)-1) {
3463 kobj_free(fullname
, maxpathlen
);
3466 while (*pathp
== ' ' || *pathp
== ':')
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);
3481 kobj_open(char *filename
)
3486 if (_modrootloaded
) {
3487 struct kobjopen_tctl
*ltp
= kobjopen_alloc(filename
);
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
);
3502 * 1098067: module creds should not be those of the
3505 cred_t
*saved_cred
= curthread
->t_cred
;
3506 curthread
->t_cred
= kcred
;
3507 Errno
= vn_openat(filename
, UIO_SYSSPACE
, FREAD
, 0, &vp
,
3509 curthread
->t_cred
= saved_cred
;
3514 if (_moddebug
& MODDEBUG_ERRMSG
) {
3516 "kobj_open: vn_open of %s fails, ",
3518 _kobj_printf(ops
, "Errno = %d\n", Errno
);
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
);
3529 fd
= kobj_boot_open(filename
, 0);
3531 if (_moddebug
& MODDEBUG_ERRMSG
) {
3534 "kobj_open: can't open %s\n", filename
);
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.
3548 kobjopen_thread(struct kobjopen_tctl
*ltp
)
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
),
3558 mutex_enter(&cpr_lk
);
3559 CALLB_CPR_EXIT(&cpr_i
);
3560 mutex_destroy(&cpr_lk
);
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(<p
->sema
, 0, NULL
, SEMA_DEFAULT
, NULL
);
3581 * free a kobjopen thread control structure
3584 kobjopen_free(struct kobjopen_tctl
*ltp
)
3586 sema_destroy(<p
->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
)
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(),
3602 "vn_rdwr failed with error 0x%x\n", stat
);
3605 return (size
- resid
);
3609 if (kobj_boot_seek((int)descr
, (off_t
)0, offset
) != 0) {
3611 "kobj_read: seek 0x%x failed\n", offset
);
3615 count
= kobj_boot_read((int)descr
, buf
, size
);
3617 if (_moddebug
& MODDEBUG_ERRMSG
) {
3619 "kobj_read: req %d bytes, ", size
);
3620 _kobj_printf(ops
, "got %d\n", count
);
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
);
3638 (void) kobj_boot_close((int)descr
);
3642 kobj_fstat(intptr_t descr
, struct bootstat
*buf
)
3647 if (_modrootloaded
) {
3649 struct vnode
*vp
= (struct vnode
*)descr
;
3650 if (fop_getattr(vp
, &vattr
, 0, kcred
, NULL
) != 0)
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
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
;
3678 return (kobj_boot_fstat((int)descr
, buf
));
3683 kobj_open_file(char *name
)
3686 struct compinfo cbuf
;
3689 if ((fd
= kobj_open(name
)) == -1) {
3690 return ((struct _buf
*)-1);
3693 file
= kobj_zalloc(sizeof (struct _buf
), KM_WAIT
|KM_TMP
);
3695 file
->_name
= kobj_alloc(strlen(name
)+1, KM_WAIT
|KM_TMP
);
3696 file
->_cnt
= file
->_size
= file
->_off
= 0;
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
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
);
3713 if (kobj_boot_compinfo(fd
, &cbuf
) != 0) {
3714 kobj_close_file(file
);
3715 return ((struct _buf
*)-1);
3717 file
->_iscmp
= cbuf
.iscmp
;
3719 if (kobj_comp_setup(file
, &cbuf
) != 0) {
3720 kobj_close_file(file
);
3721 return ((struct _buf
*)-1);
3724 file
->_base
= kobj_zalloc(cbuf
.blksize
, KM_WAIT
|KM_TMP
);
3725 file
->_bsize
= cbuf
.blksize
;
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
);
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
);
3754 file
->_base
= kobj_alloc(hdr
->ch_blksize
, KM_WAIT
|KM_TMP
);
3755 file
->_bsize
= hdr
->ch_blksize
;
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
)
3775 int b_off
; /* Offset into buffer for start of bcopy */
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
) {
3793 struct vnode
*vp
= (struct vnode
*)file
->_fd
;
3797 if (fop_getattr(vp
, &vattr
, 0, kcred
, NULL
) != 0)
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(),
3808 _kobj_printf(ops
, "kobj_read_file :vn_rdwr() failed, "
3809 "error code 0x%x\n", err
);
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
);
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
);
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.
3852 if (_moddebug
& MODDEBUG_ERRMSG
)
3853 _kobj_printf(ops
, "copying %x bytes\n",
3855 bcopy(file
->_base
+b_off
, buf
, c_size
);
3861 _kobj_printf(ops
, "kobj_read: system error");
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
,
3881 if (c_size
!= F_BLKS(file
, size
))
3887 * Otherwise, read into our buffer and copy next time
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) {
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
);
3916 kobj_read_blks(struct _buf
*file
, char *buf
, uint_t size
, uint_t off
)
3920 ASSERT(B_OFFSET(file
, size
) == 0 && B_OFFSET(file
, off
) == 0);
3926 for (blks
= size
/ file
->_bsize
; blks
!= 0; blks
--) {
3927 nret
= kobj_uncomp_blk(file
, buf
, off
);
3933 if (nret
< file
->_bsize
)
3937 ret
= kobj_read(file
->_fd
, buf
, size
, off
);
3942 kobj_uncomp_blk(struct _buf
*file
, char *buf
, uint_t off
)
3944 struct comphdr
*hdr
= kobj_comphdr(file
);
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
];
3955 slen
= hdr
->ch_blkmap
[i
+ 1] - hdr
->ch_blkmap
[i
];
3956 if (z_uncompress(buf
, &dlen
, src
, slen
) != Z_OK
)
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
));
3970 kobj_free(void *address
, size_t size
)
3975 kmem_free(address
, size
);
3976 kobj_stat
.nfree_calls
++;
3977 kobj_stat
.nfree
+= size
;
3981 kobj_zalloc(size_t size
, int flag
)
3985 if ((v
= kobj_alloc(size
, flag
)) != 0) {
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.
4004 if (flag
& (KM_TMP
| KM_SCRATCH
))
4005 return (bop_temp_alloc(size
, MINALIGN
));
4007 if (flag
& (KM_TMP
| KM_SCRATCH
))
4008 return (BOP_ALLOC(ops
, 0, size
, MINALIGN
));
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.
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
;
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
));
4054 kobj_segbrk(caddr_t
*spp
, size_t size
, size_t align
, caddr_t limit
)
4057 size_t alloc_pgsz
= kobj_mmu_pagesize
;
4058 size_t alloc_align
= BO_NO_ALIGN
;
4062 * If we are using "large" mappings for the kernel,
4063 * request aligned memory from boot using the
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
);
4077 if (va
+ size
> pva
) {
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",
4094 _kobj_printf(ops
, " aligned %lx", alloc_align
);
4095 _kobj_printf(ops
, " at 0x%lx\n", pva
);
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.
4111 kobj_gethashsize(uint_t n
)
4114 int hsize
= MAX(n
/ 4, 2);
4116 for (f
= 2; f
* f
<= hsize
; f
++)
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
4132 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
4138 if (_modrootloaded
) {
4139 struct bootstat bst
;
4141 if (kobj_fstat(file
->_fd
, &bst
) != 0)
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
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
))
4155 _kobj_printf(ops
, "kobj_get_filesize: "
4156 "vn_rdwr() failed with error 0x%x\n", err
);
4160 *size
= (uint64_t)buf
;
4165 struct bootstat bsb
;
4168 struct comphdr
*hdr
= kobj_comphdr(file
);
4170 *size
= hdr
->ch_fsize
;
4171 } else if (kobj_boot_fstat(file
->_fd
, &bsb
) != 0)
4174 *size
= bsb
.st_size
;
4178 uint64_t offset
= 0;
4180 buf
= kmem_alloc(MAXBSIZE
, KM_SLEEP
);
4182 count
= kobj_read_file(file
, buf
, MAXBSIZE
, offset
);
4184 kmem_free(buf
, MAXBSIZE
);
4188 } while (count
== MAXBSIZE
);
4189 kmem_free(buf
, MAXBSIZE
);
4209 return (q
? q
+ 1 : s
);
4213 kobj_stat_get(kobj_stat_t
*kp
)
4221 return (lg_pagesize
);
4225 kobj_textwin_alloc(struct module
*mp
)
4227 ASSERT(MUTEX_HELD(&mod_lock
));
4229 if (mp
->textwin
!= NULL
)
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
;
4244 size_t sz
= ((text
+ size
+ PAGESIZE
- 1) & 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
);
4257 mp
->textwin
= mp
->textwin_base
+ (text
& PAGEOFFSET
);
4259 mp
->textwin
= mp
->text
;
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
) -
4273 if (mp
->textwin_base
== NULL
)
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
;
4282 find_libmacro(char *name
)
4286 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4287 if (strcmp(name
, libmacros
[lmi
].lmi_macroname
) == 0)
4288 return (libmacros
[lmi
].lmi_list
);
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
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
;
4307 * check for $MACROS between nulls or slashes
4309 p
= strchr(tail
, '$');
4312 for (lmi
= 0; lmi
< NLIBMACROS
; lmi
++) {
4313 macrolen
= libmacros
[lmi
].lmi_macrolen
;
4314 if (strncmp(p
+ 1, libmacros
[lmi
].lmi_macroname
, macrolen
) == 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')
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
);
4350 * copy all chars before '/' and call expand_libmacro()
4354 bcopy(tail
, pathend
, diff
);
4357 return (expand_libmacro(p2
, path
, pathend
));
4363 endp
= p
+ macrolen
+ 1;
4364 if (strchr(endp
, '$') != NULL
)
4370 * copy lmi_list and split it into components.
4371 * then put the part of tail before $MACRO into path
4376 bcopy(tail
, pathend
, diff
);
4377 path2
= pathend
+ diff
;
4378 p1
= libmacros
[lmi
].lmi_list
;
4379 while (p1
&& (*p1
!= '\0')) {
4380 p2
= strchr(p1
, ':');
4383 bcopy(p1
, path2
, diff
);
4384 *(path2
+ diff
) = '\0';
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
)) {
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
);
4425 tnf_unsplice_probes(uint_t what
, struct modctl
*mod
)
4427 tnf_probe_control_t
**p
;
4429 struct module
*mp
= mod
->mod_mp
;
4431 if (!(mp
->flags
& KOBJ_TNF_PROBE
))
4434 for (p
= &__tnf_probe_list_head
; *p
; )
4435 if (kobj_addrcheck(mp
, (char *)*p
) == 0)
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
;
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
)
4454 static int add_notify
= 1;
4457 tnf_probe_control_t
*pl
;
4459 for (pl
= plist
; pl
->next
; )
4463 mutex_enter(&mod_lock
);
4464 tnf_changed_probe_list
= 1;
4465 pl
->next
= __tnf_probe_list_head
;
4466 __tnf_probe_list_head
= plist
;
4468 mutex_exit(&mod_lock
);
4475 for (tl
= tlist
; tl
->tag_version
; )
4476 tl
= (tnf_tag_data_t
*)tl
->tag_version
;
4479 mutex_enter(&mod_lock
);
4480 tl
->tag_version
= (tnf_tag_version_t
*)__tnf_tag_list_head
;
4481 __tnf_tag_list_head
= tlist
;
4483 mutex_exit(&mod_lock
);
4486 if (!boot_load
&& result
&& add_notify
) {
4487 tnf_add_notifyunload(tnf_unsplice_probes
);
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.
4504 kobj_record_file(char *filename
)
4507 static int size
= 0;
4510 if (kobj_file_bufsize
== 0) /* don't bother */
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
);
4526 kobj_boot_fstat(int fd
, struct bootstat
*stp
)
4529 if (!standalone
&& _ioquiesced
)
4531 return (BOP_FSTAT(ops
, fd
, stp
));
4533 return (BRD_FSTAT(bfs_ops
, fd
, stp
));
4538 kobj_boot_open(char *filename
, int flags
)
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
)
4550 kobj_record_file(filename
);
4551 return (BOP_OPEN(filename
, flags
));
4553 kobj_record_file(filename
);
4554 return (BRD_OPEN(bfs_ops
, filename
, flags
));
4559 kobj_boot_close(int fd
)
4562 if (!standalone
&& _ioquiesced
)
4565 return (BOP_CLOSE(fd
));
4567 return (BRD_CLOSE(bfs_ops
, fd
));
4573 kobj_boot_seek(int fd
, off_t hi
, off_t lo
)
4576 return (BOP_SEEK(fd
, lo
) == -1 ? -1 : 0);
4578 return (BRD_SEEK(bfs_ops
, fd
, lo
, SEEK_SET
));
4583 kobj_boot_read(int fd
, caddr_t buf
, size_t size
)
4586 return (BOP_READ(fd
, buf
, size
));
4588 return (BRD_READ(bfs_ops
, fd
, buf
, size
));
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.
4603 kobj_is_compressed(intptr_t fd
)
4605 struct vnode
*vp
= (struct vnode
*)fd
;
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
);
4619 if (magic_buf
== CH_MAGIC_GZIP
)
4620 return (CH_MAGIC_GZIP
);